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