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; section->flags = hdr->sh_flags;
// we will resolve names when string table is resolved // 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_offset = hdr->sh_offset;
section->f_size = hdr->sh_size; 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->info = hdr->sh_info;
section->entsize = hdr->sh_entsize; section->entsize = hdr->sh_entsize;
TRACE_SECTION(section);
} }
} }
else else
@ -202,7 +200,7 @@ relf_value_t relf_open(relf_t* relf, const char* path)
section->flags = hdr->sh_flags; section->flags = hdr->sh_flags;
// we will resolve names when string table is resolved // 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_offset = hdr->sh_offset;
section->f_size = hdr->sh_size; 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->info = hdr->sh_info;
section->entsize = hdr->sh_entsize; section->entsize = hdr->sh_entsize;
TRACE_SECTION(section);
} }
} }
@ -225,27 +221,26 @@ relf_value_t relf_open(relf_t* relf, const char* path)
else else
e_shstrndx = ((const Elf32_Ehdr*)relf->image)->e_shstrndx; e_shstrndx = ((const Elf32_Ehdr*)relf->image)->e_shstrndx;
// NOTE: I should handle SHN_LORESERVE in e_phnum, e_shnum and e_shstrndx // resolve strings
// 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) if (e_shstrndx != SHN_UNDEF)
{ {
// we have string table, so to ease parsing const char* strtab = (const char*)relf->image
// we gonna allocate array of char pointers + relf->sections[e_shstrndx].f_offset;
// but first we need enumerate and find out for (unsigned i = 0; i < relf->section_num; i++)
// 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); relf_section_t* section = &relf->sections[i];
cur += strlen(cur) + 1; section->name = strtab + section->si_name;
relf->string_num++;
}
TRACE("string_num %u\n", relf->string_num); TRACE_SECTION(section);
}
} }
return RELF_ERROR(RELF_OK); 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 type;
uint32_t flags; uint32_t flags;
const char* name; union {
uint32_t si_name; // index in string table
const char* name;
};
uint64_t f_offset; uint64_t f_offset;
uint64_t f_size; uint64_t f_size;
@ -72,6 +75,11 @@ typedef struct {
uint64_t entsize; uint64_t entsize;
} relf_section_t; } relf_section_t;
typedef struct {
const char** strings;
unsigned string_num;
} relf_string_table_t;
// relf instance // relf instance
typedef struct { typedef struct {
void* image; void* image;
@ -85,9 +93,6 @@ typedef struct {
relf_section_t* sections; relf_section_t* sections;
unsigned section_num; unsigned section_num;
const char** strings;
unsigned string_num;
} relf_t; } relf_t;
// opens ELF file, checks ELF magic and maps it into memory // opens ELF file, checks ELF magic and maps it into memory