diff options
Diffstat (limited to 'src/lookup/section-by-name.c')
-rw-r--r-- | src/lookup/section-by-name.c | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/lookup/section-by-name.c b/src/lookup/section-by-name.c new file mode 100644 index 0000000..1697140 --- /dev/null +++ b/src/lookup/section-by-name.c @@ -0,0 +1,39 @@ +#include <string.h> + +#include <libelf.h> +#include <gelf.h> + +#include <libelfu/libelfu.h> + + +Elf_Scn* elfu_sectionByName(Elf *e, char *name) +{ + size_t shstrndx; + Elf_Scn *scn; + + if (elf_getshdrstrndx(e, &shstrndx) != 0) { + return NULL; + } + + scn = elf_getscn(e, 1); + while (scn) { + GElf_Shdr shdr; + char *curname; + + if (gelf_getshdr(scn, &shdr) != &shdr) { + return NULL; + } + + /* elf_strptr returns NULL if there was an error */ + curname = elf_strptr(e, shstrndx, shdr.sh_name); + + /* strcmp... but we really have no bounds on the lengths here */ + if (!strcmp(curname, name)) { + return scn; + } + + scn = elf_nextscn(e, scn); + } + + return NULL; +} |