/* 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"
/* 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. *
* *
* 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. *
* target = nom de la section recherchée. *
* offset = position de la section à tester. *
* data = description de la section trouvée. [OUT] *
* *
* Description : Teste si une section correspond à celle recherchée. *
* *
* Retour : Bilan de l'opération. *
* *
* Remarques : - *
* *
******************************************************************************/
bool find_target_elf_section(const elf_format *format, const char *target, Elf32_Off offset, Elf32_Shdr *data)
{
bool result; /* Conclusion à retourner */
Elf32_Shdr section; /* Section à analyser */
result = false;
if ((offset + sizeof(Elf32_Shdr)) >= EXE_FORMAT(format)->length) return false;
memcpy(§ion, &EXE_FORMAT(format)->content[offset], sizeof(Elf32_Shdr));
result = (strcmp(target, &format->sec_names[section.sh_name]) == 0);
if (result) *data = section;
return result;
}
/******************************************************************************
* *
* Paramètres : format = description de l'exécutable à consulter. *
* target = 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. *
* *
* Retour : Bilan de l'opération. *
* *
* Remarques : - *
* *
******************************************************************************/
bool find_elf_section(const elf_format *format, const char *target, off_t *offset, off_t *size, uint64_t *voffset)
{
bool result;
Elf32_Half i;
Elf32_Shdr data;
/* Si on perd notre temps... */
if (format->sec_size == 0) return false;
result = false;
for (i = 0; i < format->header.e_shnum; i++)
{
if (i == format->header.e_shstrndx) continue;
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;
result = true;
}
}
return result;
}