Add elf_core.c into the EXTRA_SOURCES for the static library
[mit.git] / elf_core.c
blob1525c36ed6acf0c811b8d48c0553f051b59cf99a
1 void *PERBIT(get_section)(void *file,
2 unsigned long fsize,
3 const char *secname,
4 unsigned long *secsize,
5 int conv)
7 ElfPERBIT(Ehdr) *hdr;
8 ElfPERBIT(Shdr) *sechdrs;
9 ElfPERBIT(Off) e_shoff;
10 ElfPERBIT(Half) e_shnum, e_shstrndx;
12 const char *secnames;
13 unsigned int i;
15 if (fsize > 0 && fsize < sizeof(*hdr))
16 return NULL;
18 hdr = file;
19 e_shoff = END(hdr->e_shoff, conv);
20 e_shnum = END(hdr->e_shnum, conv);
21 e_shstrndx = END(hdr->e_shstrndx, conv);
23 if (fsize > 0 && fsize < e_shoff + e_shnum * sizeof(sechdrs[0]))
24 return NULL;
26 sechdrs = file + e_shoff;
28 if (fsize > 0 && fsize < END(sechdrs[e_shstrndx].sh_offset, conv))
29 return NULL;
31 /* Find section by name, return pointer and size. */
33 secnames = file + END(sechdrs[e_shstrndx].sh_offset, conv);
34 for (i = 1; i < e_shnum; i++) {
35 if (streq(secnames + END(sechdrs[i].sh_name, conv), secname)) {
36 *secsize = END(sechdrs[i].sh_size, conv);
37 return file + END(sechdrs[i].sh_offset, conv);
40 *secsize = 0;
41 return NULL;