diff options
author | Cyrille Bagard <nocbos@gmail.com> | 2008-08-08 09:28:16 (GMT) |
---|---|---|
committer | Cyrille Bagard <nocbos@gmail.com> | 2008-08-08 09:28:16 (GMT) |
commit | 904476ddf621d9513bf90a3fa396d2e6c1ea2952 (patch) | |
tree | f8966baa2ea80d2c2068a85b24ead1e651d17dc9 /src/format/elf | |
parent | 0153acaaf41e97477f6c484a92c2dd46f5122300 (diff) |
Prepared to handle the DWARF debug format.
git-svn-id: svn://svn.gna.org/svn/chrysalide/trunk@15 abbe820e-26c8-41b2-8c08-b7b2b41f8b0a
Diffstat (limited to 'src/format/elf')
-rw-r--r-- | src/format/elf/Makefile.am | 4 | ||||
-rw-r--r-- | src/format/elf/e_elf.c | 72 | ||||
-rw-r--r-- | src/format/elf/e_elf.h (renamed from src/format/elf/format_elf.h) | 20 | ||||
-rw-r--r-- | src/format/elf/elf-int.h | 55 | ||||
-rw-r--r-- | src/format/elf/format_elf.c | 126 | ||||
-rw-r--r-- | src/format/elf/section.c | 156 | ||||
-rw-r--r-- | src/format/elf/section.h | 40 |
7 files changed, 340 insertions, 133 deletions
diff --git a/src/format/elf/Makefile.am b/src/format/elf/Makefile.am index 3cf5f1c..cb5682d 100644 --- a/src/format/elf/Makefile.am +++ b/src/format/elf/Makefile.am @@ -2,7 +2,9 @@ lib_LIBRARIES = libformatelf.a libformatelf_a_SOURCES = \ - format_elf.h format_elf.c + e_elf.h e_elf.c \ + elf-int.h \ + section.h section.c libformatelf_a_CFLAGS = $(AM_CFLAGS) diff --git a/src/format/elf/e_elf.c b/src/format/elf/e_elf.c new file mode 100644 index 0000000..1c08cf3 --- /dev/null +++ b/src/format/elf/e_elf.c @@ -0,0 +1,72 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * e_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 <http://www.gnu.org/licenses/>. + */ + + +#include "e_elf.h" + + +#include <malloc.h> +#include <string.h> + + +#include "elf-int.h" +#include "section.h" + + + + +/****************************************************************************** +* * +* Paramètres : content = contenu binaire à parcourir. * +* length = taille du contenu en question. * +* * +* Description : Prend en charge un nouvel ELF. * +* * +* Retour : Adresse de la structure mise en place ou NULL en cas d'échec.* +* * +* Remarques : - * +* * +******************************************************************************/ + +elf_format *load_elf(const uint8_t *content, off_t length) +{ + elf_format *result; /* Structure à retourner */ + bool test; /* Bilan d'une initialisation */ + + result = (elf_format *)calloc(1, sizeof(elf_format)); + + EXE_FORMAT(result)->content = content; + EXE_FORMAT(result)->length = length; + + EXE_FORMAT(result)->find_section = (find_section_fc)find_elf_section; + + memcpy(&result->header, content, sizeof(Elf32_Ehdr)); + + + test = read_elf_section_names(result); + + printf("ok ? %d\n", test); + + + return result; + +} diff --git a/src/format/elf/format_elf.h b/src/format/elf/e_elf.h index 742d036..fabee77 100644 --- a/src/format/elf/format_elf.h +++ b/src/format/elf/e_elf.h @@ -1,6 +1,6 @@ /* OpenIDA - Outil d'analyse de fichiers binaires - * format_elf.h - prototypes pour le support du format ELF + * e_elf.h - prototypes pour le support du format ELF * * Copyright (C) 2008 Cyrille Bagard * @@ -21,9 +21,8 @@ */ -#ifndef _FORMAT_ELF_H -#define _FORMAT_ELF_H - +#ifndef _FORMAT_ELF_ELF_H +#define _FORMAT_ELF_ELF_H #include <stdbool.h> @@ -32,9 +31,18 @@ -bool find_text_data(const uint8_t *content, off_t *, off_t *, uint64_t *); +/* Description du format ELF */ +typedef struct _elf_format elf_format; + + + +/* Prend en charge un nouvel ELF. */ +elf_format *load_elf(const uint8_t *, off_t); + + + -#endif /* _FORMAT_ELF_H */ +#endif /* _FORMAT_ELF_ELF_H */ diff --git a/src/format/elf/elf-int.h b/src/format/elf/elf-int.h new file mode 100644 index 0000000..0fd325f --- /dev/null +++ b/src/format/elf/elf-int.h @@ -0,0 +1,55 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * elf-int.h - prototypes pour les structures internes 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 <http://www.gnu.org/licenses/>. + */ + + +#ifndef _FORMAT_ELF_ELF_INT_H +#define _FORMAT_ELF_ELF_INT_H + + + +#include <elf.h> +#include <sys/types.h> + + +#include "../exe_format-int.h" + + + + +/* Description du format ELF */ +struct _elf_format +{ + exe_format dummy; /* A laisser en premier */ + + Elf32_Ehdr header; /* En-tête du format */ + + char *sec_names; /* Noms des sections */ + size_t sec_size; /* Taille de ces définitions */ + + +}; + + + + + +#endif /* _FORMAT_ELF_ELF_INT_H */ diff --git a/src/format/elf/format_elf.c b/src/format/elf/format_elf.c deleted file mode 100644 index 9f929fb..0000000 --- a/src/format/elf/format_elf.c +++ /dev/null @@ -1,126 +0,0 @@ - -/* 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 <http://www.gnu.org/licenses/>. - */ - - -#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, 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; - -} diff --git a/src/format/elf/section.c b/src/format/elf/section.c new file mode 100644 index 0000000..a055f47 --- /dev/null +++ b/src/format/elf/section.c @@ -0,0 +1,156 @@ + +/* 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 <http://www.gnu.org/licenses/>. + */ + + +#include "section.h" + + +#include <malloc.h> +#include <string.h> + + +#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; + + 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; + +} diff --git a/src/format/elf/section.h b/src/format/elf/section.h new file mode 100644 index 0000000..b84fe76 --- /dev/null +++ b/src/format/elf/section.h @@ -0,0 +1,40 @@ + +/* 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 <http://www.gnu.org/licenses/>. + */ + + +#ifndef _FORMAT_ELF_SECTION_H +#define _FORMAT_ELF_SECTION_H + + +#include "e_elf.h" + + + +/* Charge en mémoire la liste humaine des sections. */ +bool read_elf_section_names(elf_format *); + +/* Recherche une section donnée au sein de binaire. */ +bool find_elf_section(const elf_format *, const char *, off_t *, off_t *, uint64_t *); + + + +#endif /* _FORMAT_ELF_SECTION_H */ |