working on string table parsing

This commit is contained in:
mykola2312 2024-08-25 15:39:34 +03:00
parent 44112dc4a7
commit 14b9562fb0
2 changed files with 34 additions and 2 deletions

View file

@ -218,5 +218,34 @@ relf_value_t relf_open(relf_t* relf, const char* path)
}
}
// load string table
unsigned e_shstrndx = SHN_UNDEF;
if (relf->type == RELF_64BIT)
e_shstrndx = ((const Elf64_Ehdr*)relf->image)->e_shstrndx;
else
e_shstrndx = ((const Elf32_Ehdr*)relf->image)->e_shstrndx;
// NOTE: I should handle SHN_LORESERVE in e_phnum, e_shnum and e_shstrndx
// but that's non-existent use case when ELF has number of segments
// and sections that are exceeding uint16_t limit, so I don't care.
if (e_shstrndx != SHN_UNDEF)
{
// we have string table, so to ease parsing
// we gonna allocate array of char pointers
// but first we need enumerate and find out
// how many strings there are
const char* cur = (const char*)
relf->image + relf->sections[e_shstrndx].f_offset;
const char* end = cur + relf->sections[e_shstrndx].f_size;
while (cur < end)
{
TRACE("str %s\n", cur);
cur += strlen(cur) + 1;
relf->string_num++;
}
TRACE("string_num %u\n", relf->string_num);
}
return RELF_ERROR(RELF_OK);
}

View file

@ -80,11 +80,14 @@ typedef struct {
// is it 64 or 32 bit mode
relf_type_t type;
relf_segment_t* segments;
unsigned segment_num;
relf_section_t* sections;
unsigned section_num;
relf_segment_t* segments;
relf_section_t* sections;
const char** strings;
unsigned string_num;
} relf_t;
// opens ELF file, checks ELF magic and maps it into memory