From 14b9562fb09c158bd409eeed3052ac0a498dda26 Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Sun, 25 Aug 2024 15:39:34 +0300 Subject: [PATCH] working on string table parsing --- src/relf/relf.c | 29 +++++++++++++++++++++++++++++ src/relf/relf.h | 7 +++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/relf/relf.c b/src/relf/relf.c index 69f1842..76ad91f 100644 --- a/src/relf/relf.c +++ b/src/relf/relf.c @@ -217,6 +217,35 @@ relf_value_t relf_open(relf_t* relf, const char* path) TRACE_SECTION(section); } } + + // 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); } diff --git a/src/relf/relf.h b/src/relf/relf.h index e056c19..473110c 100644 --- a/src/relf/relf.h +++ b/src/relf/relf.h @@ -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