/* OpenIDA - Outil d'analyse de fichiers binaires * section.h - prototypes pour la gestion des sections d'un ELF * * Copyright (C) 2008 Cyrille Bagard * * This file is part of OpenIDA. * * OpenIDA is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * OpenIDA is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Foobar. If not, see . */ #include "section.h" #include #include #include "elf-int.h" /****************************************************************************** * * * Paramètres : format = description de l'exécutable à compléter. * * * * Description : Charge en mémoire la liste humaine des sections. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ bool read_elf_section_names(elf_format *format) { off_t offset; /* Position des données */ Elf32_Shdr section; /* Section visée */ offset = format->header.e_shoff + format->header.e_shentsize * format->header.e_shstrndx; if ((offset + sizeof(Elf32_Shdr)) > EXE_FORMAT(format)->length) return false; memcpy(§ion, &EXE_FORMAT(format)->content[offset], sizeof(Elf32_Shdr)); if ((section.sh_offset + section.sh_size) > EXE_FORMAT(format)->length) return false; format->sec_names = (char *)calloc(section.sh_size + 1, sizeof(char)); format->sec_size = section.sh_size; memcpy(format->sec_names, &EXE_FORMAT(format)->content[section.sh_offset], section.sh_size); return true; } /****************************************************************************** * * * Paramètres : format = description de l'exécutable à consulter. * * name = nom de la section recherchée. * * section = ensemble d'informations à faire remonter. [OUT] * * * * Description : Recherche une section donnée au sein de binaire par nom. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ bool find_elf_section_by_name(const elf_format *format, const char *name, Elf_Shdr *section) { bool result; /* Bilan à retourner */ uint16_t i; /* Boucle de parcours */ /* Si on perd notre temps... */ if (format->sec_size == 0) return false; result = false; for (i = 0; i < format->header.e_shnum && !result; i++) { find_elf_section_by_index(format, i, section); result = (strcmp(name, &format->sec_names[ELF_SHDR(format, section, sh_name)]) == 0); } return result; } /****************************************************************************** * * * 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. * * 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 zone donnée au sein de binaire par nom. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ 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; /* Bilan à retourner */ Elf_Shdr section; /* Section trouvée ou non */ result = find_elf_section_by_name(format, name, §ion); if (result) get_elf_section_content(format, §ion, offset, size, voffset); return result; } /****************************************************************************** * * * 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 : - * * * ******************************************************************************/ 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; }