5 #include <libelfu/libelfu.h>
8 static char* symstr(ElfuScn *symtab, size_t off)
11 assert(symtab->linkptr);
12 assert(symtab->linkptr->data.d_buf);
13 assert(off < symtab->linkptr->data.d_size);
15 return &(((char*)symtab->linkptr->data.d_buf)[off]);
19 static ElfuSymtab* symtabFromScn32(ElfuScn *ms, ElfuScn**origScnArr)
25 assert(ms->data.d_buf);
29 st = malloc(sizeof(*st));
31 ELFU_WARN("elfu_mSymtabFromScn32: malloc() failed for st.\n");
35 CIRCLEQ_INIT(&st->syms);
38 for (i = 1; (i + 1) * sizeof(Elf32_Sym) <= ms->shdr.sh_size; i++) {
39 Elf32_Sym *cursym = &(((Elf32_Sym*)ms->data.d_buf)[i]);
40 ElfuSym *sym = malloc(sizeof(*sym));
43 sym->name = symstr(ms, cursym->st_name);
44 sym->value = cursym->st_value;
45 sym->size = cursym->st_size;
46 sym->bind = ELF32_ST_BIND(cursym->st_info);
47 sym->type = ELF32_ST_TYPE(cursym->st_info);
48 sym->other = cursym->st_other;
50 switch (cursym->st_shndx) {
57 sym->scnptr = origScnArr[cursym->st_shndx - 1];
61 CIRCLEQ_INSERT_TAIL(&st->syms, sym, elem);
75 static ElfuReltab* reltabFromScn32(ElfuScn *ms)
81 assert(ms->data.d_buf);
84 rt = malloc(sizeof(*rt));
86 ELFU_WARN("elfu_mReltabFromScn32: malloc() failed for rt.\n");
90 CIRCLEQ_INIT(&rt->rels);
93 for (i = 0; (i + 1) * sizeof(Elf32_Rel) <= ms->shdr.sh_size; i++) {
94 Elf32_Rel *currel = &(((Elf32_Rel*)ms->data.d_buf)[i]);
97 rel = malloc(sizeof(*rel));
100 rel->offset = currel->r_offset;
102 rel->sym = ELF32_R_SYM(currel->r_info);
103 rel->type = ELF32_R_TYPE(currel->r_info);
108 CIRCLEQ_INSERT_TAIL(&rt->rels, rel, elem);
122 static int cmpScnOffs(const void *ms1, const void *ms2)
127 ElfuScn *s1 = *(ElfuScn**)ms1;
128 ElfuScn *s2 = *(ElfuScn**)ms2;
134 if (s1->shdr.sh_offset < s2->shdr.sh_offset) {
136 } else if (s1->shdr.sh_offset == s2->shdr.sh_offset) {
138 } else /* if (s1->shdr.sh_offset > s2->shdr.sh_offset) */ {
145 static ElfuPhdr* parentPhdr(ElfuElf *me, ElfuScn *ms)
152 CIRCLEQ_FOREACH(mp, &me->phdrList, elem) {
153 if (mp->phdr.p_type != PT_LOAD) {
157 if (PHDR_CONTAINS_SCN_IN_MEMORY(&mp->phdr, &ms->shdr)) {
161 /* Give sections a second chance if they do not have any sh_addr
163 /* Actually we don't, because it's ambiguous.
164 * Re-enable for experiments with strangely-formatted files.
165 if (ms->shdr.sh_addr == 0
166 && PHDR_CONTAINS_SCN_IN_FILE(&mp->phdr, &ms->shdr)
167 && OFFS_END(ms->shdr.sh_offset, ms->shdr.sh_size)
168 <= OFFS_END(mp->phdr.p_offset, mp->phdr.p_memsz)) {
178 static ElfuPhdr* modelFromPhdr(GElf_Phdr *phdr)
184 mp = malloc(sizeof(ElfuPhdr));
186 ELFU_WARN("modelFromPhdr: malloc() failed for ElfuPhdr.\n");
192 CIRCLEQ_INIT(&mp->childScnList);
193 CIRCLEQ_INIT(&mp->childPhdrList);
199 static ElfuScn* modelFromSection(Elf_Scn *scn)
205 ms = malloc(sizeof(ElfuScn));
207 ELFU_WARN("modelFromSection: malloc() failed for ElfuScn.\n");
212 assert(gelf_getshdr(scn, &ms->shdr) == &ms->shdr);
215 /* Copy each data part in source segment */
216 ms->data.d_align = 1;
217 ms->data.d_buf = NULL;
219 ms->data.d_type = ELF_T_BYTE;
220 ms->data.d_size = ms->shdr.sh_size;
221 ms->data.d_version = elf_version(EV_NONE);
222 if (ms->shdr.sh_type != SHT_NOBITS
223 && ms->shdr.sh_size > 0) {
226 ms->data.d_buf = malloc(ms->shdr.sh_size);
227 if (!ms->data.d_buf) {
228 ELFU_WARN("modelFromSection: malloc() failed for data buffer (%jx bytes).\n", ms->shdr.sh_size);
232 /* A non-empty section should contain at least one data block. */
233 data = elf_rawdata(scn, NULL);
236 ms->data.d_align = data->d_align;
237 ms->data.d_type = data->d_type;
238 ms->data.d_version = data->d_version;
241 if (data->d_off + data->d_size > ms->shdr.sh_size) {
242 ELFU_WARN("modelFromSection: libelf delivered a bogus data blob. Skipping\n");
244 memcpy(ms->data.d_buf + data->d_off, data->d_buf, data->d_size);
247 data = elf_rawdata(scn, data);
272 ElfuElf* elfu_mFromElf(Elf *e)
276 size_t i, numPhdr, numShdr;
277 ElfuScn **secArray = NULL;
280 if (elfu_eCheck(e)) {
284 me = malloc(sizeof(ElfuElf));
286 ELFU_WARN("elfu_mFromElf: malloc() failed for ElfuElf.\n");
292 CIRCLEQ_INIT(&me->phdrList);
293 CIRCLEQ_INIT(&me->orphanScnList);
296 me->elfclass = gelf_getclass(e);
297 assert(me->elfclass != ELFCLASSNONE);
298 assert(gelf_getehdr(e, &me->ehdr) == &me->ehdr);
301 /* Get the section string table index */
302 if (elf_getshdrstrndx(e, &shstrndx) != 0) {
308 assert(!elf_getphdrnum(e, &numPhdr));
309 for (i = 0; i < numPhdr; i++) {
313 assert(gelf_getphdr(e, i, &phdr) == &phdr);
315 mp = modelFromPhdr(&phdr);
320 CIRCLEQ_INSERT_TAIL(&me->phdrList, mp, elem);
326 /* Find PHDR -> PHDR dependencies (needs sorted sections) */
327 CIRCLEQ_FOREACH(mp, &me->phdrList, elem) {
330 if (mp->phdr.p_type != PT_LOAD) {
334 CIRCLEQ_FOREACH(mp2, &me->phdrList, elem) {
339 if (mp->phdr.p_vaddr <= mp2->phdr.p_vaddr
340 && OFFS_END(mp2->phdr.p_vaddr, mp2->phdr.p_memsz) <= OFFS_END(mp->phdr.p_vaddr, mp->phdr.p_memsz)) {
341 CIRCLEQ_INSERT_TAIL(&mp->childPhdrList, mp2, elemChildPhdr);
349 assert(!elf_getshdrnum(e, &numShdr));
351 secArray = malloc((numShdr - 1) * sizeof(*secArray));
353 ELFU_WARN("elfu_mFromElf: malloc() failed for secArray.\n");
357 for (i = 1; i < numShdr; i++) {
361 scn = elf_getscn(e, i);
364 ms = modelFromSection(scn);
377 /* Find sh_link and sh_info dependencies (needs sections in original order) */
378 for (i = 0; i < numShdr - 1; i++) {
379 ElfuScn *ms = secArray[i];
381 switch (ms->shdr.sh_type) {
384 if (ms->shdr.sh_info > 0) {
385 ms->infoptr = secArray[ms->shdr.sh_info - 1];
393 case SHT_GNU_verneed:
394 if (ms->shdr.sh_link > 0) {
395 ms->linkptr = secArray[ms->shdr.sh_link - 1];
401 /* Parse symtabs (needs sections in original order) */
402 for (i = 0; i < numShdr - 1; i++) {
403 ElfuScn *ms = secArray[i];
405 switch (ms->shdr.sh_type) {
407 if (me->elfclass == ELFCLASS32) {
408 ms->symtab = symtabFromScn32(ms, secArray);
409 } else if (me->elfclass == ELFCLASS64) {
418 /* Parse relocations */
419 for (i = 0; i < numShdr - 1; i++) {
420 ElfuScn *ms = secArray[i];
422 switch (ms->shdr.sh_type) {
424 if (me->elfclass == ELFCLASS32) {
425 ms->reltab = reltabFromScn32(ms);
426 } else if (me->elfclass == ELFCLASS64) {
432 if (me->elfclass == ELFCLASS32) {
434 } else if (me->elfclass == ELFCLASS64) {
443 /* Sort sections by file offset */
444 qsort(secArray, numShdr - 1, sizeof(*secArray), cmpScnOffs);
447 /* Find PHDR -> Section dependencies (needs sorted sections) */
448 for (i = 0; i < numShdr - 1; i++) {
449 ElfuScn *ms = secArray[i];
451 ElfuPhdr *parent = parentPhdr(me, ms);
454 GElf_Off shaddr = parent->phdr.p_vaddr +
455 (ms->shdr.sh_offset - parent->phdr.p_offset);
457 if (ms->shdr.sh_addr == 0) {
458 ms->shdr.sh_addr = shaddr;
460 assert(ms->shdr.sh_addr == shaddr);
463 CIRCLEQ_INSERT_TAIL(&parent->childScnList, ms, elemChildScn);
465 CIRCLEQ_INSERT_TAIL(&me->orphanScnList, ms, elemChildScn);
479 // TODO: Free data structures
482 ELFU_WARN("elfu_mFromElf: Failed to load file.\n");