diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2008-07-29 22:31:55 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2008-07-29 22:31:55 (GMT) |
commit | 57758c2cd11938e3e4c6e45b983cee4c2269ff44 (patch) | |
tree | ade6e434f0a5e68dec379c20432a09dd1cc75a1a /src/format/elf | |
parent | 4d004094424bb1fc52082f2e442c4c43a5525fe1 (diff) |
Loaded the executable part of an ELF file.
git-svn-id: svn://svn.gna.org/svn/chrysalide/trunk@10 abbe820e-26c8-41b2-8c08-b7b2b41f8b0a
Diffstat (limited to 'src/format/elf')
-rw-r--r-- | src/format/elf/format_elf.c | 101 | ||||
-rw-r--r-- | src/format/elf/format_elf.h | 6 |
2 files changed, 107 insertions, 0 deletions
diff --git a/src/format/elf/format_elf.c b/src/format/elf/format_elf.c index f043d0a..b70eef5 100644 --- a/src/format/elf/format_elf.c +++ b/src/format/elf/format_elf.c @@ -22,3 +22,104 @@ #include "elf.h" + + + + + + + + + + +#include <ctype.h> +#include <elf.h> +#include <malloc.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + + +char *read_section_names(const uint8_t *content, Elf32_Off offset) +{ + char *result; + Elf32_Shdr section; + + result = NULL; + + memcpy(§ion, &content[offset], sizeof(Elf32_Shdr)); + + result = (char *)calloc(section.sh_size + 1, sizeof(char)); + + memcpy(result, &content[section.sh_offset], section.sh_size); + + return result; + +} + + +bool find_target_section(const uint8_t *content, const char *target, const char *names, Elf32_Off offset, Elf32_Shdr *data) +{ + bool result; + Elf32_Shdr section; + + result = false; + + memcpy(§ion, &content[offset], sizeof(Elf32_Shdr)); + + result = (strcmp(target, &names[section.sh_name]) == 0); + + if (result) + { + printf("section: %s (0x%08x)\n", &names[section.sh_name], section.sh_addr); + *data = section; + } + + return result; + +} + + + +bool find_text_data(const uint8_t *content, off_t *offset, off_t *size) +{ + bool result; + Elf32_Ehdr header; + char *names; + Elf32_Half i; + Elf32_Shdr data; + + result = false; + + memcpy(&header, content, sizeof(Elf32_Ehdr)); + + names = read_section_names(content, header.e_shoff + header.e_shentsize * header.e_shstrndx); + if (names == NULL) + { + fprintf(stderr, "no section header string table\n"); + return NULL; + } + + for (i = 0; i < header.e_shnum; i++) + { + if (i == header.e_shstrndx) continue; + + if (find_target_section(content, ".text", names, header.e_shoff + header.e_shentsize * i, &data)) + { + printf("Find it !\n"); + + *offset = data.sh_offset; + *size = data.sh_size; + + result = true; + + } + + } + + free(names); + + return result; + +} diff --git a/src/format/elf/format_elf.h b/src/format/elf/format_elf.h index 894623a..feca71d 100644 --- a/src/format/elf/format_elf.h +++ b/src/format/elf/format_elf.h @@ -26,9 +26,15 @@ +#include <stdbool.h> +#include <stdint.h> +#include <sys/types.h> +bool find_text_data(const uint8_t *content, off_t *, off_t *); + + #endif /* _FORMAT_ELF_H */ |