diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2008-10-29 20:14:05 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2008-10-29 20:14:05 (GMT) |
commit | 8a30afc05eed869865ba4dc9c107119f7ec00fe4 (patch) | |
tree | 18743934be1c2355c99788e49efb7a7a43e335f1 /src/format/elf/section.c | |
parent | 2e5893f9261ba59e06fadcc6ddfa9a1253e286b3 (diff) |
Do not relied on section names anymore.
git-svn-id: svn://svn.gna.org/svn/chrysalide/trunk@39 abbe820e-26c8-41b2-8c08-b7b2b41f8b0a
Diffstat (limited to 'src/format/elf/section.c')
-rw-r--r-- | src/format/elf/section.c | 186 |
1 files changed, 144 insertions, 42 deletions
diff --git a/src/format/elf/section.c b/src/format/elf/section.c index f16e002..7ad8363 100644 --- a/src/format/elf/section.c +++ b/src/format/elf/section.c @@ -32,13 +32,6 @@ -/* Teste si une section correspond à celle recherchée. */ -bool find_target_elf_section(const elf_format *, const char *, Elf32_Off, Elf32_Shdr *); - - - - - /****************************************************************************** * * * Paramètres : format = description de l'exécutable à compléter. * @@ -75,12 +68,11 @@ bool read_elf_section_names(elf_format *format) /****************************************************************************** * * -* Paramètres : format = description de l'exécutable à consulter. * -* target = nom de la section recherchée. * -* offset = position de la section à tester. * -* data = description de la section trouvée. [OUT] * +* Paramètres : format = description de l'exécutable à consulter. * +* name = nom de la section recherchée. * +* section = ensemble d'informations à faire remonter. [OUT] * * * -* Description : Teste si une section correspond à celle recherchée. * +* Description : Recherche une section donnée au sein de binaire par nom. * * * * Retour : Bilan de l'opération. * * * @@ -88,20 +80,23 @@ bool read_elf_section_names(elf_format *format) * * ******************************************************************************/ -bool find_target_elf_section(const elf_format *format, const char *target, Elf32_Off offset, Elf32_Shdr *data) +bool find_elf_section_by_name(const elf_format *format, const char *name, Elf_Shdr *section) { - bool result; /* Conclusion à retourner */ - Elf32_Shdr section; /* Section à analyser */ + bool result; /* Bilan à retourner */ + uint16_t i; /* Boucle de parcours */ - result = false; + /* Si on perd notre temps... */ + if (format->sec_size == 0) return false; - if ((offset + sizeof(Elf32_Shdr)) >= EXE_FORMAT(format)->length) return false; + result = false; - memcpy(§ion, &EXE_FORMAT(format)->content[offset], sizeof(Elf32_Shdr)); + for (i = 0; i < format->header.e_shnum && !result; i++) + { + find_elf_section_by_index(format, i, section); - result = (strcmp(target, &format->sec_names[section.sh_name]) == 0); + result = (strcmp(name, &format->sec_names[ELF_SHDR(format, section, sh_name)]) == 0); - if (result) *data = section; + } return result; @@ -110,13 +105,109 @@ bool find_target_elf_section(const elf_format *format, const char *target, Elf32 /****************************************************************************** * * +* Paramètres : format = description de l'exécutable à consulter. * +* type = type de la section recherchée. * +* sections = tableau d'informations à faire remonter. [OUT] * +* count = nombre d'éléments présents dans le tableau. [OUT] * +* * +* Description : Recherche une section donnée au sein de binaire par type. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool find_elf_section_by_type(const elf_format *format, uint16_t type, Elf_Shdr **sections, size_t *count) +{ + uint16_t i; /* Boucle de parcours */ + Elf_Shdr section; /* Section à analyser */ + + *sections = NULL; + *count = 0; + + for (i = 0; i < format->header.e_shnum; i++) + { + find_elf_section_by_index(format, i, §ion); + + if (type == ELF_SHDR(format, §ion, sh_type)) + { + *sections = (Elf_Shdr *)realloc(*sections, ++(*count) * sizeof(Elf_Shdr)); + (*sections)[*count - 1] = section; + } + + } + + return (*count > 0); + +} + + +/****************************************************************************** +* * +* Paramètres : format = description de l'exécutable à consulter. * +* index = indice de la section recherchée. * +* section = ensemble d'informations à faire remonter. [OUT] * +* * +* Description : Recherche une section donnée au sein de binaire par indice. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool find_elf_section_by_index(const elf_format *format, uint16_t index, Elf_Shdr *section) +{ + off_t offset; /* Emplacement à venir lire */ + + if (index >= format->header.e_shnum) return false; + + offset = format->header.e_shoff + format->header.e_shentsize * index; + + memcpy(section, &EXE_FORMAT(format)->content[offset], ELF_SIZEOF_SHDR(format)); + + return true; + +} + + +/****************************************************************************** +* * +* Paramètres : format = description de l'exécutable à consulter. * +* section = section à consulter. * +* offset = position de la section trouvée. [OUT] * +* size = taille de la section trouvée. [OUT] * +* voffset = adresse virtuelle de la section trouvée. [OUT] * +* * +* Description : Fournit les adresses et taille contenues dans une section. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +void get_elf_section_content(const elf_format *format, const Elf_Shdr *section, off_t *offset, off_t *size, uint64_t *voffset) +{ + *offset = ELF_SHDR(format, section, sh_offset); + *size = ELF_SHDR(format, section, sh_size); + + if (voffset != NULL) + *voffset = ELF_SHDR(format, section, sh_addr); + +} + + +/****************************************************************************** +* * * Paramètres : format = description de l'exécutable à consulter. * -* target = nom de la section recherchée. * +* name = nom de la section recherchée. * * offset = position de la section trouvée. [OUT] * * size = taille de la section trouvée. [OUT] * * voffset = adresse virtuelle de la section trouvée. [OUT] * * * -* Description : Recherche une section donnée au sein de binaire. * +* Description : Recherche une zone donnée au sein de binaire par nom. * * * * Retour : Bilan de l'opération. * * * @@ -124,35 +215,46 @@ bool find_target_elf_section(const elf_format *format, const char *target, Elf32 * * ******************************************************************************/ -bool find_elf_section(const elf_format *format, const char *target, off_t *offset, off_t *size, uint64_t *voffset) +bool find_elf_section_content_by_name(const elf_format *format, const char *name, off_t *offset, off_t *size, uint64_t *voffset) { - bool result; - Elf32_Half i; - Elf32_Shdr data; + bool result; /* Bilan à retourner */ + Elf_Shdr section; /* Section trouvée ou non */ - /* Si on perd notre temps... */ - if (format->sec_size == 0) return false; + result = find_elf_section_by_name(format, name, §ion); - result = false; + if (result) + get_elf_section_content(format, §ion, offset, size, voffset); - for (i = 0; i < format->header.e_shnum; i++) - { - if (i == format->header.e_shstrndx) continue; + return result; + +} - if (find_target_elf_section(format, target, - format->header.e_shoff + format->header.e_shentsize * i, &data)) - { - *offset = data.sh_offset; - *size = data.sh_size; - if (voffset != NULL) - *voffset = data.sh_addr; +/****************************************************************************** +* * +* Paramètres : format = description de l'exécutable à consulter. * +* index = indice de la section recherchée. * +* offset = position de la section trouvée. [OUT] * +* size = taille de la section trouvée. [OUT] * +* voffset = adresse virtuelle de la section trouvée. [OUT] * +* * +* Description : Recherche une zone donnée au sein de binaire par indice. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ - result = true; +bool find_elf_section_content_by_index(const elf_format *format, uint16_t index, off_t *offset, off_t *size, uint64_t *voffset) +{ + bool result; /* Bilan à retourner */ + Elf_Shdr section; /* Section trouvée ou non */ - } + result = find_elf_section_by_index(format, index, §ion); - } + if (result) + get_elf_section_content(format, §ion, offset, size, voffset); return result; |