so, "string table index" is just an offset. implemented string resolving

This commit is contained in:
mykola2312 2024-08-25 19:58:19 +03:00
parent 14b9562fb0
commit 8f7a832961
2 changed files with 26 additions and 26 deletions

View file

@ -174,7 +174,7 @@ relf_value_t relf_open(relf_t* relf, const char* path)
section->flags = hdr->sh_flags;
// we will resolve names when string table is resolved
section->name = NULL;
section->si_name = hdr->sh_name;
section->f_offset = hdr->sh_offset;
section->f_size = hdr->sh_size;
@ -185,8 +185,6 @@ relf_value_t relf_open(relf_t* relf, const char* path)
section->info = hdr->sh_info;
section->entsize = hdr->sh_entsize;
TRACE_SECTION(section);
}
}
else
@ -202,7 +200,7 @@ relf_value_t relf_open(relf_t* relf, const char* path)
section->flags = hdr->sh_flags;
// we will resolve names when string table is resolved
section->name = NULL;
section->si_name = hdr->sh_name;
section->f_offset = hdr->sh_offset;
section->f_size = hdr->sh_size;
@ -213,8 +211,6 @@ relf_value_t relf_open(relf_t* relf, const char* path)
section->info = hdr->sh_info;
section->entsize = hdr->sh_entsize;
TRACE_SECTION(section);
}
}
@ -225,27 +221,26 @@ relf_value_t relf_open(relf_t* relf, const char* path)
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.
// resolve strings
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)
const char* strtab = (const char*)relf->image
+ relf->sections[e_shstrndx].f_offset;
for (unsigned i = 0; i < relf->section_num; i++)
{
TRACE("str %s\n", cur);
cur += strlen(cur) + 1;
relf->string_num++;
}
relf_section_t* section = &relf->sections[i];
section->name = strtab + section->si_name;
TRACE("string_num %u\n", relf->string_num);
TRACE_SECTION(section);
}
}
return RELF_ERROR(RELF_OK);
}
void relf_close(relf_t* relf)
{
relf_unmap(relf);
free(relf->segments);
free(relf->sections);
}

View file

@ -58,7 +58,10 @@ typedef struct {
uint32_t type;
uint32_t flags;
union {
uint32_t si_name; // index in string table
const char* name;
};
uint64_t f_offset;
uint64_t f_size;
@ -72,6 +75,11 @@ typedef struct {
uint64_t entsize;
} relf_section_t;
typedef struct {
const char** strings;
unsigned string_num;
} relf_string_table_t;
// relf instance
typedef struct {
void* image;
@ -85,9 +93,6 @@ typedef struct {
relf_section_t* sections;
unsigned section_num;
const char** strings;
unsigned string_num;
} relf_t;
// opens ELF file, checks ELF magic and maps it into memory