[ktap] [PATCH 1/4] symbol: add elf_section_by_name() helper (based on linux/perf/symbol-elf.c)

  • From: Azat Khuzhin <a3at.mail@xxxxxxxxx>
  • To: ktap@xxxxxxxxxxxxx
  • Date: Sat, 9 Nov 2013 23:44:01 +0400

Add a helper for searching section in elf by it name.
Drop from original unused arguments, and variables.

Signed-off-by: Azat Khuzhin <a3at.mail@xxxxxxxxx>
---
 userspace/symbol.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/userspace/symbol.c b/userspace/symbol.c
index 2e83281..dfbc40d 100644
--- a/userspace/symbol.c
+++ b/userspace/symbol.c
@@ -32,6 +32,26 @@
 
 #include <libelf.h>
 
+static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
+                                   GElf_Shdr *shp, const char *name)
+{
+       Elf_Scn *scn = NULL;
+
+       /* Elf is corrupted/truncated, avoid calling elf_strptr. */
+       if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL))
+               return NULL;
+
+       while ((scn = elf_nextscn(elf, scn)) != NULL) {
+               char *str;
+
+               gelf_getshdr(scn, shp);
+               str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
+               if (!strcmp(name, str))
+                       break;
+       }
+
+       return scn;
+}
 
 /**
  * @return v_addr of "LOAD" program header, that have zero offset.
-- 
1.8.4.rc3


Other related posts:

  • » [ktap] [PATCH 1/4] symbol: add elf_section_by_name() helper (based on linux/perf/symbol-elf.c) - Azat Khuzhin