/* OpenIDA - Outil d'analyse de fichiers binaires
* format_elf.c - support du format 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 "elf.h"
#include
#include
#include
#include
#include
#include
#include
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, uint64_t *voffset)
{
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;
*voffset = data.sh_addr;
result = true;
}
}
free(names);
return result;
}