linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Stephane Eranian <eranian@google.com>
To: linux-kernel@vger.kernel.org
Cc: acme@kernel.org, jolsa@redhat.com, peterz@infradead.org,
	mingo@elte.hu, anton@ozlabs.org, namhyung@kernel.org,
	Stefano Sanfilippo <ssanfilippo@chromium.org>,
	Ross McIlroy <rmcilroy@chromium.org>
Subject: [PATCH 7/9] perf/jit: generate .eh_frame/.eh_frame_hdr in DSO
Date: Thu, 13 Oct 2016 03:59:41 -0700	[thread overview]
Message-ID: <1476356383-30100-8-git-send-email-eranian@google.com> (raw)
In-Reply-To: <1476356383-30100-1-git-send-email-eranian@google.com>

From: Stefano Sanfilippo <ssanfilippo@chromium.org>

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>
---
 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 = NULL;
 	int symlen;
 	int retval = -1;
@@ -196,7 +280,7 @@ jit_write_elf(int fd, uint64_t load_addr, const char *sym,
 	ehdr->e_type = ET_DYN;
 	ehdr->e_entry = GEN_ELF_TEXT_OFFSET;
 	ehdr->e_version = EV_CURRENT;
-	ehdr->e_shstrndx= 2; /* shdr index for section name */
+	ehdr->e_shstrndx= unwinding ? 4 : 2; /* shdr index for section name */
 
 	/*
 	 * setup text section
@@ -233,6 +317,18 @@ jit_write_elf(int fd, uint64_t load_addr, const char *sym,
 	shdr->sh_entsize = 0;
 
 	/*
+	 * Setup .eh_frame_hdr and .eh_frame
+	 */
+	if (unwinding) {
+		eh_frame_base_offset = ALIGN_8(GEN_ELF_TEXT_OFFSET + csize);
+		retval = jit_add_eh_frame_info(e, unwinding,
+					       unwinding_header_size, unwinding_size,
+					       eh_frame_base_offset);
+		if (retval)
+			goto error;
+	}
+
+	/*
 	 * setup section headers string table
 	 */
 	scn = elf_newscn(e);
@@ -300,7 +396,7 @@ jit_write_elf(int fd, uint64_t load_addr, const char *sym,
 	shdr->sh_type = SHT_SYMTAB;
 	shdr->sh_flags = 0;
 	shdr->sh_entsize = sizeof(Elf_Sym);
-	shdr->sh_link = 4; /* index of .strtab section */
+	shdr->sh_link = unwinding ? 6 : 4; /* index of .strtab section */
 
 	/*
 	 * 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 @@
 
 /* genelf.c */
 int jit_write_elf(int fd, uint64_t code_addr, const char *sym,
-		  const void *code, int csize, void *debug, int nr_debug_entries);
+		  const void *code, int csize, void *debug, int nr_debug_entries,
+		  void *unwinding, uint64_t unwinding_header_size, uint64_t unwinding_size);
 #ifdef HAVE_DWARF_SUPPORT
 /* genelf_debug.c */
 int 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 fdddeca26545..c47fba63274b 100644
--- a/tools/perf/util/jitdump.c
+++ b/tools/perf/util/jitdump.c
@@ -72,7 +72,10 @@ jit_emit_elf(char *filename,
 	     const void *code,
 	     int csize,
 	     void *debug,
-	     int nr_debug_entries)
+	     int nr_debug_entries,
+	     void *unwinding,
+	     uint32_t unwinding_header_size,
+	     uint32_t unwinding_size)
 {
 	int ret, fd;
 
@@ -85,7 +88,8 @@ jit_emit_elf(char *filename,
 		return -1;
 	}
 
-        ret = jit_write_elf(fd, code_addr, sym, (const void *)code, csize, debug, nr_debug_entries);
+	ret = jit_write_elf(fd, code_addr, sym, (const void *)code, csize, debug, nr_debug_entries,
+			    unwinding, unwinding_header_size, unwinding_size);
 
         close(fd);
 
@@ -412,7 +416,8 @@ static int jit_repipe_code_load(struct jit_buf_desc *jd, union jr_entry *jr)
 
 	size = PERF_ALIGN(size, sizeof(u64));
 	uaddr = (uintptr_t)code;
-	ret = jit_emit_elf(filename, sym, addr, (const void *)uaddr, csize, jd->debug_data, jd->nr_debug_entries);
+	ret = jit_emit_elf(filename, sym, addr, (const void *)uaddr, csize, jd->debug_data, jd->nr_debug_entries,
+			   jd->unwinding_data, jd->eh_frame_hdr_size, jd->unwinding_size);
 
 	if (jd->debug_data && jd->nr_debug_entries) {
 		free(jd->debug_data);
-- 
1.9.1

  parent reply	other threads:[~2016-10-13 11:03 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-13 10:59 [PATCH 0/9] perf/jit: various improvements Stephane Eranian
2016-10-13 10:59 ` [PATCH 1/9] perf/jit: improve error messages from JVMTI Stephane Eranian
2016-10-13 20:05   ` Nilay Vaish
2016-10-14 11:13     ` Arnaldo Carvalho de Melo
2016-10-14 12:57       ` Stephane Eranian
2016-10-14 15:20         ` Arnaldo Carvalho de Melo
2016-10-17 13:52           ` Stephane Eranian
2016-10-24 18:58   ` [tip:perf/core] perf jit: Improve " tip-bot for Stephane Eranian
2016-10-13 10:59 ` [PATCH 2/9] perf/jit: enable jitdump support without dwarf Stephane Eranian
2016-10-13 18:16   ` Arnaldo Carvalho de Melo
2016-10-13 18:37     ` Arnaldo Carvalho de Melo
2016-10-13 18:51       ` Arnaldo Carvalho de Melo
2016-10-13 19:03         ` Arnaldo Carvalho de Melo
2016-10-13 19:44           ` Arnaldo Carvalho de Melo
2016-10-24 18:58   ` [tip:perf/core] perf jit: Enable " tip-bot for Maciej Debski
2016-10-13 10:59 ` [PATCH 3/9] perf/jit: remove unecessary padding in jitdump file Stephane Eranian
2016-10-24 18:59   ` [tip:perf/core] perf jit: Remove " tip-bot for Stephane Eranian
2016-10-13 10:59 ` [PATCH 4/9] perf/jit: make perf skip unknown records Stephane Eranian
2016-10-24 18:59   ` [tip:perf/core] perf jit: Make " tip-bot for Stefano Sanfilippo
2016-10-13 10:59 ` [PATCH 5/9] perf/jit: do not assume pgoff is zero Stephane Eranian
2016-10-24 19:00   ` [tip:perf/core] perf jit: Do " tip-bot for Stefano Sanfilippo
2016-10-13 10:59 ` [PATCH 6/9] perf/jit: add unwinding support Stephane Eranian
2016-10-24 19:00   ` [tip:perf/core] perf jit: Add " tip-bot for Stefano Sanfilippo
2016-10-13 10:59 ` Stephane Eranian [this message]
2016-10-24 19:01   ` [tip:perf/core] perf jit: Generate .eh_frame/.eh_frame_hdr in DSO tip-bot for Stefano Sanfilippo
2016-10-13 10:59 ` [PATCH 8/9] perf/jit: Check JITHEADER_VERSION Stephane Eranian
2016-10-24 19:01   ` [tip:perf/core] perf jit: " tip-bot for Stefano Sanfilippo
2016-10-13 10:59 ` [PATCH 9/9] perf/jit: add jitdump format specification document Stephane Eranian
2016-10-24 19:02   ` [tip:perf/core] perf jit: Add " tip-bot for Stephane Eranian

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1476356383-30100-8-git-send-email-eranian@google.com \
    --to=eranian@google.com \
    --cc=acme@kernel.org \
    --cc=anton@ozlabs.org \
    --cc=jolsa@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rmcilroy@chromium.org \
    --cc=ssanfilippo@chromium.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).