27Elf_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)) {
35 printf(
"ELF file: ident check failed!");
40 if (elf->header->ident[4] != 1) {
41 printf(
"ELF file: wrong bit length!");
46 elf->section_headers =
48 memcpy((uint8_t*)elf->section_headers, &buffer[elf->header->shoff],
55 char* shstrtab = (
char*)malloc(shstrtab_section->size);
56 memcpy((uint8_t*)shstrtab, &buffer[shstrtab_section->offset], shstrtab_section->size);
60 for (
int i = 0; i < elf->header->shnum; i++) {
61 const char* str = shstrtab + elf->section_headers[i].name;
62 if (!strcmp(str,
".symtab"))
63 symtab = &elf->section_headers[i];
64 if (!strcmp(str,
".strtab"))
65 strtab = &elf->section_headers[i];
69 printf(
"ELF: symbol table not found!");
74 memcpy((uint8_t*)elf->symtab, &buffer[symtab->offset], symtab->size);
78 printf(
"ELF: string table not found!");
81 elf->strtabsz = strtab->size;
82 elf->strtab = (
char*)malloc(strtab->size);
83 memcpy((uint8_t*)elf->strtab, &buffer[strtab->offset], strtab->size);
92 for (
size_t i = 0; i < elf->symtabsz; i++)
93 if (!strcmp((
char*)(elf->strtab + elf->symtab[i].name), name))
94 return &(elf->symtab[i]);
100 for (
size_t i = 0; i < elf->header->shnum; i++) {
101 if (elf->section_headers[i].type == SHT_REL && elf->section_headers[i].info == sym->shndx) {
102 relsh = &(elf->section_headers[i]);
109 for (
size_t i = 0; i < (relsh->size /
sizeof(
ElfRel_t)); i++) {
111 if (tmp->offset == sym->value) {
118 ElfSymbol_t* relsym = &(elf->symtab[R_SYM(rel->info)]);
119 if (ST_TYPE(relsym->info) != 3)
121 return &elf->buffer[elf->section_headers[relsym->shndx].offset + ptr];