mv main.c -> elfucli.c, make it scriptable via args
[centaur.git] / src / elfops / section-by-name.c
1 #include <string.h>
2 #include <libelfu/libelfu.h>
3
4
5 Elf_Scn* elfu_eScnByName(Elf *e, char *name)
6 {
7   size_t shstrndx;
8   Elf_Scn *scn;
9
10   if (elf_getshdrstrndx(e, &shstrndx) != 0) {
11     return NULL;
12   }
13
14   scn = elf_getscn(e, 1);
15   while (scn) {
16     GElf_Shdr shdr;
17     char *curname;
18
19     if (gelf_getshdr(scn, &shdr) != &shdr) {
20       return NULL;
21     }
22
23     /* elf_strptr returns NULL if there was an error */
24     curname = elf_strptr(e, shstrndx, shdr.sh_name);
25
26     /* strcmp... but we really have no bounds on the lengths here */
27     if (!strcmp(curname, name)) {
28       return scn;
29     }
30
31     scn = elf_nextscn(e, scn);
32   }
33
34   return NULL;
35 }