[PATCH 7/9] perf/jit: generate .eh_frame/.eh_frame_hdr in DSO

2016-10-13 Thread Stephane Eranian
From: Stefano Sanfilippo 

When the jit_buf_desc contains unwinding information, it is emitted
as eh_frame unwinding sections in the DSOs generated by perf inject.

The unwinding information is required to unwind of JITed code which
do not maintain the frame pointer register during function calls.
It can be emitted by V8 / Chromium when the --perf_prof_unwinding_info
is passed to V8.

The eh_frame and eh_frame_hdr sections are emitted immediately after
the .text.

The .eh_frame is aligned at a 8-byte boundary, and .eh_frame_hdr
at a 4-byte one. Since size of the .eh_frame is required to be a
multiple of the word size, which means there will never be additional
padding between it and the .eh_frame_hdr on machines where the word
size is 4 or 8 bytes.

However, additional padding might be inserted between .text and
.eh_frame to reach the correct alignment, which will always be
8 bytes, also on 32bit machines. The reasoning behind this choice is
that 4 extra bytes of padding worst case are not a large cost for the
advantage of removing word-size dependent offset calculations when
emitting the jitdump.

Signed-off-by: Stefano Sanfilippo 
Signed-off-by: Ross McIlroy 
Reviewed-by: Stephane Eranian 
---
 tools/perf/util/genelf.c  | 102 --
 tools/perf/util/genelf.h  |   3 +-
 tools/perf/util/jitdump.c |  11 +++--
 3 files changed, 109 insertions(+), 7 deletions(-)

diff --git a/tools/perf/util/genelf.c b/tools/perf/util/genelf.c
index 14a73acc549c..57845109e662 100644
--- a/tools/perf/util/genelf.c
+++ b/tools/perf/util/genelf.c
@@ -69,6 +69,8 @@ static char shd_string_table[] = {
'.', 'd', 'e', 'b', 'u', 'g', '_', 'l', 'i', 'n', 'e', 0, /* 52 */
'.', 'd', 'e', 'b', 'u', 'g', '_', 'i', 'n', 'f', 'o', 0, /* 64 */
'.', 'd', 'e', 'b', 'u', 'g', '_', 'a', 'b', 'b', 'r', 'e', 'v', 0, /* 
76 */
+   '.', 'e', 'h', '_', 'f', 'r', 'a', 'm', 'e', '_', 'h', 'd', 'r', 0, /* 
90 */
+   '.', 'e', 'h', '_', 'f', 'r', 'a', 'm', 'e', 0, /* 104 */
 };
 
 static struct buildid_note {
@@ -149,6 +151,86 @@ gen_build_id(struct buildid_note *note, unsigned long 
load_addr, const void *cod
 }
 #endif
 
+static int
+jit_add_eh_frame_info(Elf *e, void* unwinding, uint64_t unwinding_header_size,
+ uint64_t unwinding_size, uint64_t base_offset)
+{
+   Elf_Data *d;
+   Elf_Scn *scn;
+   Elf_Shdr *shdr;
+   uint64_t unwinding_table_size = unwinding_size - unwinding_header_size;
+
+   /*
+* setup eh_frame section
+*/
+   scn = elf_newscn(e);
+   if (!scn) {
+   warnx("cannot create section");
+   return -1;
+   }
+
+   d = elf_newdata(scn);
+   if (!d) {
+   warnx("cannot get new data");
+   return -1;
+   }
+
+   d->d_align = 8;
+   d->d_off = 0LL;
+   d->d_buf = unwinding;
+   d->d_type = ELF_T_BYTE;
+   d->d_size = unwinding_table_size;
+   d->d_version = EV_CURRENT;
+
+   shdr = elf_getshdr(scn);
+   if (!shdr) {
+   warnx("cannot get section header");
+   return -1;
+   }
+
+   shdr->sh_name = 104;
+   shdr->sh_type = SHT_PROGBITS;
+   shdr->sh_addr = base_offset;
+   shdr->sh_flags = SHF_ALLOC;
+   shdr->sh_entsize = 0;
+
+   /*
+* setup eh_frame_hdr section
+*/
+   scn = elf_newscn(e);
+   if (!scn) {
+   warnx("cannot create section");
+   return -1;
+   }
+
+   d = elf_newdata(scn);
+   if (!d) {
+   warnx("cannot get new data");
+   return -1;
+   }
+
+   d->d_align = 4;
+   d->d_off = 0LL;
+   d->d_buf = unwinding + unwinding_table_size;
+   d->d_type = ELF_T_BYTE;
+   d->d_size = unwinding_header_size;
+   d->d_version = EV_CURRENT;
+
+   shdr = elf_getshdr(scn);
+   if (!shdr) {
+   warnx("cannot get section header");
+   return -1;
+   }
+
+   shdr->sh_name = 90;
+   shdr->sh_type = SHT_PROGBITS;
+   shdr->sh_addr = base_offset + unwinding_table_size;
+   shdr->sh_flags = SHF_ALLOC;
+   shdr->sh_entsize = 0;
+
+   return 0;
+}
+
 /*
  * fd: file descriptor open for writing for the output file
  * load_addr: code load address (could be zero, just used for buildid)
@@ -159,13 +241,15 @@ gen_build_id(struct buildid_note *note, unsigned long 
load_addr, const void *cod
 int
 jit_write_elf(int fd, uint64_t load_addr, const char *sym,
  const void *code, int csize,
- void *debug __maybe_unused, int nr_debug_entries __maybe_unused)
+ void *debug __maybe_unused, int nr_debug_entries __maybe_unused,
+ void *unwinding, uint64_t unwinding_header_size, uint64_t 
unwinding_size)
 {
Elf *e;
Elf_Data *d;
Elf_Scn *scn;

[PATCH 7/9] perf/jit: generate .eh_frame/.eh_frame_hdr in DSO

2016-10-13 Thread Stephane Eranian
From: Stefano Sanfilippo 

When the jit_buf_desc contains unwinding information, it is emitted
as eh_frame unwinding sections in the DSOs generated by perf inject.

The unwinding information is required to unwind of JITed code which
do not maintain the frame pointer register during function calls.
It can be emitted by V8 / Chromium when the --perf_prof_unwinding_info
is passed to V8.

The eh_frame and eh_frame_hdr sections are emitted immediately after
the .text.

The .eh_frame is aligned at a 8-byte boundary, and .eh_frame_hdr
at a 4-byte one. Since size of the .eh_frame is required to be a
multiple of the word size, which means there will never be additional
padding between it and the .eh_frame_hdr on machines where the word
size is 4 or 8 bytes.

However, additional padding might be inserted between .text and
.eh_frame to reach the correct alignment, which will always be
8 bytes, also on 32bit machines. The reasoning behind this choice is
that 4 extra bytes of padding worst case are not a large cost for the
advantage of removing word-size dependent offset calculations when
emitting the jitdump.

Signed-off-by: Stefano Sanfilippo 
Signed-off-by: Ross McIlroy 
Reviewed-by: Stephane Eranian 
---
 tools/perf/util/genelf.c  | 102 --
 tools/perf/util/genelf.h  |   3 +-
 tools/perf/util/jitdump.c |  11 +++--
 3 files changed, 109 insertions(+), 7 deletions(-)

diff --git a/tools/perf/util/genelf.c b/tools/perf/util/genelf.c
index 14a73acc549c..57845109e662 100644
--- a/tools/perf/util/genelf.c
+++ b/tools/perf/util/genelf.c
@@ -69,6 +69,8 @@ static char shd_string_table[] = {
'.', 'd', 'e', 'b', 'u', 'g', '_', 'l', 'i', 'n', 'e', 0, /* 52 */
'.', 'd', 'e', 'b', 'u', 'g', '_', 'i', 'n', 'f', 'o', 0, /* 64 */
'.', 'd', 'e', 'b', 'u', 'g', '_', 'a', 'b', 'b', 'r', 'e', 'v', 0, /* 
76 */
+   '.', 'e', 'h', '_', 'f', 'r', 'a', 'm', 'e', '_', 'h', 'd', 'r', 0, /* 
90 */
+   '.', 'e', 'h', '_', 'f', 'r', 'a', 'm', 'e', 0, /* 104 */
 };
 
 static struct buildid_note {
@@ -149,6 +151,86 @@ gen_build_id(struct buildid_note *note, unsigned long 
load_addr, const void *cod
 }
 #endif
 
+static int
+jit_add_eh_frame_info(Elf *e, void* unwinding, uint64_t unwinding_header_size,
+ uint64_t unwinding_size, uint64_t base_offset)
+{
+   Elf_Data *d;
+   Elf_Scn *scn;
+   Elf_Shdr *shdr;
+   uint64_t unwinding_table_size = unwinding_size - unwinding_header_size;
+
+   /*
+* setup eh_frame section
+*/
+   scn = elf_newscn(e);
+   if (!scn) {
+   warnx("cannot create section");
+   return -1;
+   }
+
+   d = elf_newdata(scn);
+   if (!d) {
+   warnx("cannot get new data");
+   return -1;
+   }
+
+   d->d_align = 8;
+   d->d_off = 0LL;
+   d->d_buf = unwinding;
+   d->d_type = ELF_T_BYTE;
+   d->d_size = unwinding_table_size;
+   d->d_version = EV_CURRENT;
+
+   shdr = elf_getshdr(scn);
+   if (!shdr) {
+   warnx("cannot get section header");
+   return -1;
+   }
+
+   shdr->sh_name = 104;
+   shdr->sh_type = SHT_PROGBITS;
+   shdr->sh_addr = base_offset;
+   shdr->sh_flags = SHF_ALLOC;
+   shdr->sh_entsize = 0;
+
+   /*
+* setup eh_frame_hdr section
+*/
+   scn = elf_newscn(e);
+   if (!scn) {
+   warnx("cannot create section");
+   return -1;
+   }
+
+   d = elf_newdata(scn);
+   if (!d) {
+   warnx("cannot get new data");
+   return -1;
+   }
+
+   d->d_align = 4;
+   d->d_off = 0LL;
+   d->d_buf = unwinding + unwinding_table_size;
+   d->d_type = ELF_T_BYTE;
+   d->d_size = unwinding_header_size;
+   d->d_version = EV_CURRENT;
+
+   shdr = elf_getshdr(scn);
+   if (!shdr) {
+   warnx("cannot get section header");
+   return -1;
+   }
+
+   shdr->sh_name = 90;
+   shdr->sh_type = SHT_PROGBITS;
+   shdr->sh_addr = base_offset + unwinding_table_size;
+   shdr->sh_flags = SHF_ALLOC;
+   shdr->sh_entsize = 0;
+
+   return 0;
+}
+
 /*
  * fd: file descriptor open for writing for the output file
  * load_addr: code load address (could be zero, just used for buildid)
@@ -159,13 +241,15 @@ gen_build_id(struct buildid_note *note, unsigned long 
load_addr, const void *cod
 int
 jit_write_elf(int fd, uint64_t load_addr, const char *sym,
  const void *code, int csize,
- void *debug __maybe_unused, int nr_debug_entries __maybe_unused)
+ void *debug __maybe_unused, int nr_debug_entries __maybe_unused,
+ void *unwinding, uint64_t unwinding_header_size, uint64_t 
unwinding_size)
 {
Elf *e;
Elf_Data *d;
Elf_Scn *scn;
Elf_Ehdr *ehdr;
Elf_Shdr *shdr;
+   uint64_t eh_frame_base_offset;
char *strsym