26 Elf_t *elf_create(uint8_t *buffer,
size_t len)
33 if ((elf->header->ident[1] !=
'E') || (elf->header->ident[2] !=
'L') ||
34 (elf->header->ident[3] !=
'F') || (elf->header->ident[0] != 127))
36 printf(
"ELF file: ident check failed!");
41 if (elf->header->ident[4] != 1)
43 printf(
"ELF file: wrong bit length!");
51 (uint8_t *) elf->section_headers, &buffer[elf->header->shoff],
56 &elf->section_headers[elf->header->shstrndx];
59 char *shstrtab = (
char *) malloc(shstrtab_section->size);
61 (uint8_t *) shstrtab, &buffer[shstrtab_section->offset],
62 shstrtab_section->size);
66 for (
int i = 0; i < elf->header->shnum; i++)
68 const char *str = shstrtab + elf->section_headers[i].name;
69 if (!strcmp(str,
".symtab"))
70 symtab = &elf->section_headers[i];
71 if (!strcmp(str,
".strtab"))
72 strtab = &elf->section_headers[i];
77 printf(
"ELF: symbol table not found!");
84 memcpy((uint8_t *) elf->symtab, &buffer[symtab->offset], symtab->size);
89 printf(
"ELF: string table not found!");
94 elf->strtabsz = strtab->size;
95 elf->strtab = (
char *) malloc(strtab->size);
96 memcpy((uint8_t *) elf->strtab, &buffer[strtab->offset], strtab->size);
106 for (
size_t i = 0; i < elf->symtabsz; i++)
107 if (!strcmp((
char *) (elf->strtab + elf->symtab[i].name), name))
108 return &(elf->symtab[i]);
115 for (
size_t i = 0; i < elf->header->shnum; i++)
117 if (elf->section_headers[i].type == SHT_REL &&
118 elf->section_headers[i].info == sym->shndx)
120 relsh = &(elf->section_headers[i]);
127 for (
size_t i = 0; i < (relsh->size /
sizeof(
ElfRel_t)); i++)
131 if (tmp->offset == sym->value)
139 ElfSymbol_t *relsym = &(elf->symtab[R_SYM(rel->info)]);
140 if (ST_TYPE(relsym->info) != 3)
142 return &elf->buffer[elf->section_headers[relsym->shndx].offset + ptr];