mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Steve Clevenger <scclevenger@os.amperecomputing.com>
To: james.clark@arm.com, mike.leach@linaro.org
Cc: suzuki.poulose@arm.com, leo.yan@linaro.org,
	ilkka@os.ampercomputing.com, coresight@lists.linaro.org,
	linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org,
	"steve.c.clevenger.ampere" <scclevenger@os.amperecomputing.com>
Subject: [PATCH 1/5] Add dso__is_pie call to identify ELF PIE
Date: Tue, 20 Aug 2024 16:11:35 -0600	[thread overview]
Message-ID: <323e931fe9f8f080eb0dfc2e29d112dd7edf1fb2.1724104248.git.scclevenger@os.amperecomputing.com> (raw)
In-Reply-To: <cover.1724104248.git.scclevenger@os.amperecomputing.com>

From: "steve.c.clevenger.ampere" <scclevenger@os.amperecomputing.com>

Add dso__is_pie global to read the .dynamic section DT_FLAGS_1 entry for
the DF_1_PIE flag. This identifies position executable code.
 
Signed-off-by: steve.c.clevenger.ampere <scclevenger@os.amperecomputing.com>
---
 tools/perf/util/symbol-elf.c | 55 ++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)

diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
index e398abfd13a0..1d4bd222b314 100644
--- a/tools/perf/util/symbol-elf.c
+++ b/tools/perf/util/symbol-elf.c
@@ -662,6 +662,61 @@ static int dso__synthesize_plt_got_symbols(struct dso *dso, Elf *elf,
 	return err;
 }
 
+/*
+ * Check dynamic section DT_FLAGS_1 for a Position Independent
+ * Executable (PIE).
+ */
+bool dso__is_pie(struct dso *dso)
+{
+	Elf *elf = NULL;
+	Elf_Scn *scn = NULL;
+	GElf_Ehdr ehdr;
+	GElf_Shdr shdr;
+	bool is_pie = false;
+	char dso_path[PATH_MAX];
+	int fd = -1;
+
+	if (!dso || (elf_version(EV_CURRENT) == EV_NONE))
+		return is_pie;	// false
+
+	dso__build_id_filename(dso, dso_path, sizeof(dso_path), false);
+
+	fd = open(dso_path, O_RDONLY);
+
+	if (fd < 0) {
+		pr_debug("%s: cannot read cached %s.\n", __func__, dso_path);
+		return is_pie;	// false
+	}
+
+	elf = elf_begin(fd, ELF_C_READ, NULL);
+	gelf_getehdr(elf, &ehdr);
+
+	if (ehdr.e_type == ET_DYN) {
+		scn = elf_section_by_name(elf, &ehdr, &shdr, ".dynamic", NULL);
+		if (scn) {	// check DT_FLAGS_1
+			Elf_Data *data;
+			GElf_Dyn *entry;
+			int n_entries = shdr.sh_size / sizeof(GElf_Dyn);
+
+			data = (Elf_Data *) elf_getdata(scn, NULL);
+			for (int i = 0; i < n_entries; i++) {
+				entry = ((GElf_Dyn *) data->d_buf) + i;
+				if (entry->d_tag == DT_FLAGS_1) {
+					if ((entry->d_un.d_val & DF_1_PIE) != 0) {
+						is_pie = true;
+						break;
+					}
+				}
+			} // end for
+		}
+	}
+
+	elf_end(elf);
+	close(fd);
+
+	return is_pie;
+}
+
 /*
  * We need to check if we have a .dynsym, so that we can handle the
  * .plt, synthesizing its symbols, that aren't on the symtabs (be it
-- 
2.25.1


  parent reply	other threads:[~2024-08-20 22:12 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-20 22:11 [PATCH 0/5] arm-cs-trace-disasm.py/perf must accommodate non-zero DSO text offset Steve Clevenger
2024-08-20 22:11 ` [PATCH 5/5] Adjust objdump start/end range per map pgoff parameter Steve Clevenger
2024-08-20 22:11 ` [PATCH 4/5] Add map pgoff to python dictionary based on MAPPING_TYPE Steve Clevenger
2024-08-20 22:11 ` [PATCH 3/5] Force MAPPING_TYPE__IDENTIY for PIE Steve Clevenger
2024-08-20 22:11 ` [PATCH 2/5] Add dso__is_pie prototype Steve Clevenger
2024-08-20 22:11 ` Steve Clevenger [this message]
2024-08-26 13:13   ` [PATCH 1/5] Add dso__is_pie call to identify ELF PIE Leo Yan
2024-08-26 21:33     ` Steve Clevenger

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=323e931fe9f8f080eb0dfc2e29d112dd7edf1fb2.1724104248.git.scclevenger@os.amperecomputing.com \
    --to=scclevenger@os.amperecomputing.com \
    --cc=coresight@lists.linaro.org \
    --cc=ilkka@os.ampercomputing.com \
    --cc=james.clark@arm.com \
    --cc=leo.yan@linaro.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mike.leach@linaro.org \
    --cc=suzuki.poulose@arm.com \
    /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

all inboxes | Powered by JetHome®