aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefano Sanfilippo <ssanfilippo@chromium.org>2016-10-13 06:59:41 -0400
committerArnaldo Carvalho de Melo <acme@redhat.com>2016-10-24 10:07:40 -0400
commit086f9f3d7897d8081b18b949caa631b937c5891e (patch)
tree36eb29f095d9b5e3e73e4f7a94378d90e5672b80
parent0284fecd13b6db3ecd4c2b1bf3e72b105edce24b (diff)
perf jit: Generate .eh_frame/.eh_frame_hdr in DSO
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 <ssanfilippo@chromium.org> Signed-off-by: Ross McIlroy <rmcilroy@chromium.org> Reviewed-by: Stephane Eranian <eranian@google.com> Cc: Anton Blanchard <anton@ozlabs.org> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Link: http://lkml.kernel.org/r/1476356383-30100-8-git-send-email-eranian@google.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
-rw-r--r--tools/perf/util/genelf.c102
-rw-r--r--tools/perf/util/genelf.h3
-rw-r--r--tools/perf/util/jitdump.c11
3 files changed, 109 insertions, 7 deletions
diff --git a/tools/perf/util/genelf.c b/tools/perf/util/genelf.c
index 30dece345b9f..c540d47583e7 100644
--- a/tools/perf/util/genelf.c
+++ b/tools/perf/util/genelf.c
@@ -73,6 +73,8 @@ static char shd_string_table[] = {
73 '.', 'd', 'e', 'b', 'u', 'g', '_', 'l', 'i', 'n', 'e', 0, /* 52 */ 73 '.', 'd', 'e', 'b', 'u', 'g', '_', 'l', 'i', 'n', 'e', 0, /* 52 */
74 '.', 'd', 'e', 'b', 'u', 'g', '_', 'i', 'n', 'f', 'o', 0, /* 64 */ 74 '.', 'd', 'e', 'b', 'u', 'g', '_', 'i', 'n', 'f', 'o', 0, /* 64 */
75 '.', 'd', 'e', 'b', 'u', 'g', '_', 'a', 'b', 'b', 'r', 'e', 'v', 0, /* 76 */ 75 '.', 'd', 'e', 'b', 'u', 'g', '_', 'a', 'b', 'b', 'r', 'e', 'v', 0, /* 76 */
76 '.', 'e', 'h', '_', 'f', 'r', 'a', 'm', 'e', '_', 'h', 'd', 'r', 0, /* 90 */
77 '.', 'e', 'h', '_', 'f', 'r', 'a', 'm', 'e', 0, /* 104 */
76}; 78};
77 79
78static struct buildid_note { 80static struct buildid_note {
@@ -153,6 +155,86 @@ gen_build_id(struct buildid_note *note, unsigned long load_addr, const void *cod
153} 155}
154#endif 156#endif
155 157
158static int
159jit_add_eh_frame_info(Elf *e, void* unwinding, uint64_t unwinding_header_size,
160 uint64_t unwinding_size, uint64_t base_offset)
161{
162 Elf_Data *d;
163 Elf_Scn *scn;
164 Elf_Shdr *shdr;
165 uint64_t unwinding_table_size = unwinding_size - unwinding_header_size;
166
167 /*
168 * setup eh_frame section
169 */
170 scn = elf_newscn(e);
171 if (!scn) {
172 warnx("cannot create section");
173 return -1;
174 }
175
176 d = elf_newdata(scn);
177 if (!d) {
178 warnx("cannot get new data");
179 return -1;
180 }
181
182 d->d_align = 8;
183 d->d_off = 0LL;
184 d->d_buf = unwinding;
185 d->d_type = ELF_T_BYTE;
186 d->d_size = unwinding_table_size;
187 d->d_version = EV_CURRENT;
188
189 shdr = elf_getshdr(scn);
190 if (!shdr) {
191 warnx("cannot get section header");
192 return -1;
193 }
194
195 shdr->sh_name = 104;
196 shdr->sh_type = SHT_PROGBITS;
197 shdr->sh_addr = base_offset;
198 shdr->sh_flags = SHF_ALLOC;
199 shdr->sh_entsize = 0;
200
201 /*
202 * setup eh_frame_hdr section
203 */
204 scn = elf_newscn(e);
205 if (!scn) {
206 warnx("cannot create section");
207 return -1;
208 }
209
210 d = elf_newdata(scn);
211 if (!d) {
212 warnx("cannot get new data");
213 return -1;
214 }
215
216 d->d_align = 4;
217 d->d_off = 0LL;
218 d->d_buf = unwinding + unwinding_table_size;
219 d->d_type = ELF_T_BYTE;
220 d->d_size = unwinding_header_size;
221 d->d_version = EV_CURRENT;
222
223 shdr = elf_getshdr(scn);
224 if (!shdr) {
225 warnx("cannot get section header");
226 return -1;
227 }
228
229 shdr->sh_name = 90;
230 shdr->sh_type = SHT_PROGBITS;
231 shdr->sh_addr = base_offset + unwinding_table_size;
232 shdr->sh_flags = SHF_ALLOC;
233 shdr->sh_entsize = 0;
234
235 return 0;
236}
237
156/* 238/*
157 * fd: file descriptor open for writing for the output file 239 * fd: file descriptor open for writing for the output file
158 * load_addr: code load address (could be zero, just used for buildid) 240 * load_addr: code load address (could be zero, just used for buildid)
@@ -163,13 +245,15 @@ gen_build_id(struct buildid_note *note, unsigned long load_addr, const void *cod
163int 245int
164jit_write_elf(int fd, uint64_t load_addr, const char *sym, 246jit_write_elf(int fd, uint64_t load_addr, const char *sym,
165 const void *code, int csize, 247 const void *code, int csize,
166 void *debug __maybe_unused, int nr_debug_entries __maybe_unused) 248 void *debug __maybe_unused, int nr_debug_entries __maybe_unused,
249 void *unwinding, uint64_t unwinding_header_size, uint64_t unwinding_size)
167{ 250{
168 Elf *e; 251 Elf *e;
169 Elf_Data *d; 252 Elf_Data *d;
170 Elf_Scn *scn; 253 Elf_Scn *scn;
171 Elf_Ehdr *ehdr; 254 Elf_Ehdr *ehdr;
172 Elf_Shdr *shdr; 255 Elf_Shdr *shdr;
256 uint64_t eh_frame_base_offset;
173 char *strsym = NULL; 257 char *strsym = NULL;
174 int symlen; 258 int symlen;
175 int retval = -1; 259 int retval = -1;
@@ -200,7 +284,7 @@ jit_write_elf(int fd, uint64_t load_addr, const char *sym,
200 ehdr->e_type = ET_DYN; 284 ehdr->e_type = ET_DYN;
201 ehdr->e_entry = GEN_ELF_TEXT_OFFSET; 285 ehdr->e_entry = GEN_ELF_TEXT_OFFSET;
202 ehdr->e_version = EV_CURRENT; 286 ehdr->e_version = EV_CURRENT;
203 ehdr->e_shstrndx= 2; /* shdr index for section name */ 287 ehdr->e_shstrndx= unwinding ? 4 : 2; /* shdr index for section name */
204 288
205 /* 289 /*
206 * setup text section 290 * setup text section
@@ -237,6 +321,18 @@ jit_write_elf(int fd, uint64_t load_addr, const char *sym,
237 shdr->sh_entsize = 0; 321 shdr->sh_entsize = 0;
238 322
239 /* 323 /*
324 * Setup .eh_frame_hdr and .eh_frame
325 */
326 if (unwinding) {
327 eh_frame_base_offset = ALIGN_8(GEN_ELF_TEXT_OFFSET + csize);
328 retval = jit_add_eh_frame_info(e, unwinding,
329 unwinding_header_size, unwinding_size,
330 eh_frame_base_offset);
331 if (retval)
332 goto error;
333 }
334
335 /*
240 * setup section headers string table 336 * setup section headers string table
241 */ 337 */
242 scn = elf_newscn(e); 338 scn = elf_newscn(e);
@@ -304,7 +400,7 @@ jit_write_elf(int fd, uint64_t load_addr, const char *sym,
304 shdr->sh_type = SHT_SYMTAB; 400 shdr->sh_type = SHT_SYMTAB;
305 shdr->sh_flags = 0; 401 shdr->sh_flags = 0;
306 shdr->sh_entsize = sizeof(Elf_Sym); 402 shdr->sh_entsize = sizeof(Elf_Sym);
307 shdr->sh_link = 4; /* index of .strtab section */ 403 shdr->sh_link = unwinding ? 6 : 4; /* index of .strtab section */
308 404
309 /* 405 /*
310 * setup symbols string table 406 * setup symbols string table
diff --git a/tools/perf/util/genelf.h b/tools/perf/util/genelf.h
index 5c933ac71451..2424bd9862a3 100644
--- a/tools/perf/util/genelf.h
+++ b/tools/perf/util/genelf.h
@@ -3,7 +3,8 @@
3 3
4/* genelf.c */ 4/* genelf.c */
5int jit_write_elf(int fd, uint64_t code_addr, const char *sym, 5int jit_write_elf(int fd, uint64_t code_addr, const char *sym,
6 const void *code, int csize, void *debug, int nr_debug_entries); 6 const void *code, int csize, void *debug, int nr_debug_entries,
7 void *unwinding, uint64_t unwinding_header_size, uint64_t unwinding_size);
7#ifdef HAVE_DWARF_SUPPORT 8#ifdef HAVE_DWARF_SUPPORT
8/* genelf_debug.c */ 9/* genelf_debug.c */
9int jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_entries); 10int jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_entries);
diff --git a/tools/perf/util/jitdump.c b/tools/perf/util/jitdump.c
index 9bae66cc78f2..6a2688da3c4a 100644
--- a/tools/perf/util/jitdump.c
+++ b/tools/perf/util/jitdump.c
@@ -72,7 +72,10 @@ jit_emit_elf(char *filename,
72 const void *code, 72 const void *code,
73 int csize, 73 int csize,
74 void *debug, 74 void *debug,
75 int nr_debug_entries) 75 int nr_debug_entries,
76 void *unwinding,
77 uint32_t unwinding_header_size,
78 uint32_t unwinding_size)
76{ 79{
77 int ret, fd; 80 int ret, fd;
78 81
@@ -85,7 +88,8 @@ jit_emit_elf(char *filename,
85 return -1; 88 return -1;
86 } 89 }
87 90
88 ret = jit_write_elf(fd, code_addr, sym, (const void *)code, csize, debug, nr_debug_entries); 91 ret = jit_write_elf(fd, code_addr, sym, (const void *)code, csize, debug, nr_debug_entries,
92 unwinding, unwinding_header_size, unwinding_size);
89 93
90 close(fd); 94 close(fd);
91 95
@@ -412,7 +416,8 @@ static int jit_repipe_code_load(struct jit_buf_desc *jd, union jr_entry *jr)
412 416
413 size = PERF_ALIGN(size, sizeof(u64)); 417 size = PERF_ALIGN(size, sizeof(u64));
414 uaddr = (uintptr_t)code; 418 uaddr = (uintptr_t)code;
415 ret = jit_emit_elf(filename, sym, addr, (const void *)uaddr, csize, jd->debug_data, jd->nr_debug_entries); 419 ret = jit_emit_elf(filename, sym, addr, (const void *)uaddr, csize, jd->debug_data, jd->nr_debug_entries,
420 jd->unwinding_data, jd->eh_frame_hdr_size, jd->unwinding_size);
416 421
417 if (jd->debug_data && jd->nr_debug_entries) { 422 if (jd->debug_data && jd->nr_debug_entries) {
418 free(jd->debug_data); 423 free(jd->debug_data);