From ada3040b9b2b6d0a2d6e2157b3f79e772e36b2d7 Mon Sep 17 00:00:00 2001 From: Cyrille Bagard Date: Sun, 31 Aug 2008 20:49:30 +0000 Subject: Looked for symbols of function. git-svn-id: svn://svn.gna.org/svn/chrysalide/trunk@23 abbe820e-26c8-41b2-8c08-b7b2b41f8b0a --- ChangeLog | 22 ++++ src/binary.c | 23 ++++- src/format/elf/Makefile.am | 3 +- src/format/elf/e_elf.c | 5 + src/format/elf/elf-int.h | 11 ++ src/format/elf/symbol.c | 243 +++++++++++++++++++++++++++++++++++++++++++++ src/format/elf/symbol.h | 40 ++++++++ src/gtksnippet.c | 23 +++++ src/gtksnippet.h | 4 + 9 files changed, 369 insertions(+), 5 deletions(-) create mode 100644 src/format/elf/symbol.c create mode 100644 src/format/elf/symbol.h diff --git a/ChangeLog b/ChangeLog index eb4fb7d..3516c02 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,27 @@ 2008-08-31 Cyrille Bagard + * src/binary.c: + Display the found [and sorted] symbols. + + * src/format/elf/e_elf.c: + Load the ELF symbols. + + * src/format/elf/elf-int.h: + Define an ELF symbol (name and address). + + * src/format/elf/Makefile.am: + Add symbol.[ch] to libformatelf_a_SOURCES. + + * src/format/elf/symbol.c: + * src/format/elf/symbol.h: + New entries: look for symbols of function. + + * src/gtksnippet.c: + * src/gtksnippet.h: + Add a way to compare two lines of code. + +2008-08-31 Cyrille Bagard + * src/arch/x86/instruction.h: * src/arch/x86/opcodes.h: Handle the new opcodes. diff --git a/src/binary.c b/src/binary.c index 862a4d0..b7ee15a 100644 --- a/src/binary.c +++ b/src/binary.c @@ -25,6 +25,7 @@ #include +#include #include #include #include @@ -165,14 +166,23 @@ void fill_snippet(GtkSnippet *snippet) - comments_count = get_dwarf_comments(dformat, &comments, &offsets); + //comments_count = get_dwarf_comments(dformat, &comments, &offsets); + + comments = NULL; + offsets = NULL; + comments_count = 0; + + get_elf_symbol_comments(format, &comments, &offsets, &comments_count); comments_list = (code_line_info **)calloc(comments_count, sizeof(code_line_info *)); for (i = 0; i < comments_count; i++) comments_list[i] = create_code_line_info(offsets[i], NULL, strdup(comments[i])); - i = 0; + + qsort(comments_list, comments_count, sizeof(code_line_info *), compare_code_line_info); + + find_exe_section(format, ".text", &pos, &len, &base); @@ -187,6 +197,9 @@ void fill_snippet(GtkSnippet *snippet) offset = base; + for (i = 0; i < comments_count; i++) + if (comments_list[i]->offset >= base) break; + gtk_snippet_set_processor(snippet, proc); @@ -206,7 +219,7 @@ void fill_snippet(GtkSnippet *snippet) offset = base + pos; /* Si on a un commentaire pour cette ligne... */ - if (comments_count > 0 && offsets[i] == offset) + if (i < comments_count && comments_list[i]->offset == offset) { list = (code_line_info **)realloc(list, ++list_len * sizeof(code_line_info *)); list[list_len - 1] = comments_list[i++]; @@ -232,8 +245,10 @@ void fill_snippet(GtkSnippet *snippet) /* TODO: free() */ } - ret = munmap(bin_data, length); + /**** + ret = munmap(bin_data, length); + ****/ /* gtk_snippet_build_content(snippet); diff --git a/src/format/elf/Makefile.am b/src/format/elf/Makefile.am index cb5682d..4886115 100644 --- a/src/format/elf/Makefile.am +++ b/src/format/elf/Makefile.am @@ -4,7 +4,8 @@ lib_LIBRARIES = libformatelf.a libformatelf_a_SOURCES = \ e_elf.h e_elf.c \ elf-int.h \ - section.h section.c + section.h section.c \ + symbol.h symbol.c libformatelf_a_CFLAGS = $(AM_CFLAGS) diff --git a/src/format/elf/e_elf.c b/src/format/elf/e_elf.c index 1c08cf3..a6399ab 100644 --- a/src/format/elf/e_elf.c +++ b/src/format/elf/e_elf.c @@ -30,6 +30,7 @@ #include "elf-int.h" #include "section.h" +#include "symbol.h" @@ -66,6 +67,10 @@ elf_format *load_elf(const uint8_t *content, off_t length) printf("ok ? %d\n", test); + test = load_elf_symbols(result); + + printf("ok ? %d\n", test); + return result; diff --git a/src/format/elf/elf-int.h b/src/format/elf/elf-int.h index 0fd325f..2ec33c3 100644 --- a/src/format/elf/elf-int.h +++ b/src/format/elf/elf-int.h @@ -34,6 +34,15 @@ +/* Symbole trouvé */ +typedef struct _elf_symbol +{ + const char *name; /* Désignation du symbole */ + uint64_t address; /* Adresse du symbole */ + +} elf_symbol; + + /* Description du format ELF */ struct _elf_format @@ -45,6 +54,8 @@ struct _elf_format char *sec_names; /* Noms des sections */ size_t sec_size; /* Taille de ces définitions */ + elf_symbol *symbols; /* Liste des symboles */ + size_t sym_count; /* Taille de cette liste */ }; diff --git a/src/format/elf/symbol.c b/src/format/elf/symbol.c new file mode 100644 index 0000000..729c154 --- /dev/null +++ b/src/format/elf/symbol.c @@ -0,0 +1,243 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * symbol.c - gestion des symboles 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 "symbol.h" + + +#include +#include +#include + + + +#include "elf-int.h" +#include "section.h" + + + + + +/* Charge en mémoire la liste humaine des symboles (32 bits). */ +bool load_elf_symbol_table_32(elf_format *, const off_t *, const off_t *, const off_t *, const off_t *); + +/* Charge en mémoire la liste humaine des symboles (64 bits). */ +bool load_elf_symbol_table_64(elf_format *, const off_t *, const off_t *, const off_t *, const off_t *); + + + + +/****************************************************************************** +* * +* Paramètres : format = description de l'exécutable à compléter. * +* * +* Description : Charge en mémoire la liste humaine des symboles. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool load_elf_symbols(elf_format *format) +{ + bool result; /* Bilan à retourner */ + + + off_t sym_start; /* Début de section */ + off_t sym_size; /* Taille de section */ + off_t str_start; /* Début de section */ + off_t str_size; /* Taille de section */ + + + result = find_elf_section(format, ".symtab", &sym_start, &sym_size, NULL); + + result &= find_elf_section(format, ".strtab", &str_start, &str_size, NULL); + + + if (result) + { + + + + result = load_elf_symbol_table_32(format, &sym_start, &sym_size, &str_start, &str_start); + + + + } + + return result; + +} + + + + + + +/****************************************************************************** +* * +* Paramètres : format = description de l'exécutable à compléter. * +* sym_start = début de la zone à traiter. * +* sym_size = taille de la zone à traiter. * +* str_start = début de la zone de chaîne de caractères. * +* str_size = taille de la zone de chaînes de caractères. * +* * +* Description : Charge en mémoire la liste humaine des symboles (32 bits). * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool load_elf_symbol_table_32(elf_format *format, const off_t *sym_start, const off_t *sym_size, const off_t *str_start, const off_t *str_size) +{ + off_t iter; /* Boucle de parcours */ + Elf32_Sym symbol; /* Symbole ELF lu */ + + if (*sym_size % sizeof(Elf32_Sym) != 0) return false; + + for (iter = *sym_start; iter < (*sym_start + *sym_size); iter += sizeof(Elf32_Sym)) + { + memcpy(&symbol, &EXE_FORMAT(format)->content[iter], sizeof(Elf32_Sym)); + + if (!(ELF32_ST_TYPE(symbol.st_info) == STT_FUNC + || (ELF32_ST_TYPE(symbol.st_info) == STT_NOTYPE + && ELF32_ST_BIND(symbol.st_info) == STB_GLOBAL))) continue; + + if (symbol.st_value == 0) continue; + + /* Sécurité anti-débordements */ + if (symbol.st_name >= *str_size) continue; + + /* Si le symbole possède un nom... */ + if (strlen(&EXE_FORMAT(format)->content[*str_start + symbol.st_name]) > 0) + { + format->symbols = (elf_symbol *)realloc(format->symbols, ++format->sym_count * sizeof(elf_symbol)); + + format->symbols[format->sym_count - 1].name = &EXE_FORMAT(format)->content[*str_start + symbol.st_name]; + format->symbols[format->sym_count - 1].address = symbol.st_value; + + } + + } + + return true; + +} + + +/****************************************************************************** +* * +* Paramètres : format = description de l'exécutable à compléter. * +* sym_start = début de la zone à traiter. * +* sym_size = taille de la zone à traiter. * +* str_start = début de la zone de chaîne de caractères. * +* str_size = taille de la zone de chaînes de caractères. * +* * +* Description : Charge en mémoire la liste humaine des symboles (64 bits). * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool load_elf_symbol_table_64(elf_format *format, const off_t *sym_start, const off_t *sym_size, const off_t *str_start, const off_t *str_size) +{ + off_t iter; /* Boucle de parcours */ + Elf64_Sym symbol; /* Symbole ELF lu */ + + if (*sym_size % sizeof(Elf64_Sym) != 0) return false; + + for (iter = *sym_start; iter < (*sym_start + *sym_size); iter += sizeof(Elf64_Sym)) + { + memcpy(&symbol, &EXE_FORMAT(format)->content[iter], sizeof(Elf64_Sym)); + + if (!(ELF64_ST_TYPE(symbol.st_info) == STT_FUNC + || (ELF64_ST_TYPE(symbol.st_info) == STT_NOTYPE + && ELF64_ST_BIND(symbol.st_info) == STB_GLOBAL))) continue; + + if (symbol.st_value == 0) continue; + + /* Sécurité anti-débordements */ + if (symbol.st_name >= *str_size) continue; + + /* Si le symbole possède un nom... */ + if (strlen(&EXE_FORMAT(format)->content[*str_start + symbol.st_name]) > 0) + { + format->symbols = (elf_symbol *)realloc(format->symbols, ++format->sym_count * sizeof(elf_symbol)); + + format->symbols[format->sym_count - 1].name = &EXE_FORMAT(format)->content[*str_start + symbol.st_name]; + format->symbols[format->sym_count - 1].address = symbol.st_value; + + } + + } + + return true; + +} + + +/****************************************************************************** +* * +* Paramètres : format = informations chargées à consulter. * +* comments = liste des commentaires à insérer. [OUT] * +* offsets = liste des indices des commentaires. [OUT] * +* count = taille des listes construites. [OUT] * +* * +* Description : Récupère tous les commentaires à insérer dans le code. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +void get_elf_symbol_comments(const elf_format *format, char ***comments, uint64_t **offsets, size_t *count) +{ + size_t i; /* Boucle de parcours */ + size_t len; /* Longueur d'une désignation */ + + if (format->sym_count > 0) + { + *comments = (char **)calloc(*count + format->sym_count, sizeof(char *)); + *offsets = (uint64_t *)calloc(*count + format->sym_count, sizeof(uint64_t)); + + for (i = 0; i < format->sym_count; i++) + { + len = strlen(format->symbols[i].name); + + (*comments)[i] = (char *)calloc(len + 9, sizeof(char)); + snprintf((*comments)[i], len + 9, "<%s>", format->symbols[i].name); + + (*offsets)[i] = format->symbols[i].address; + + } + + *count += format->sym_count; + + } + +} diff --git a/src/format/elf/symbol.h b/src/format/elf/symbol.h new file mode 100644 index 0000000..0a8f203 --- /dev/null +++ b/src/format/elf/symbol.h @@ -0,0 +1,40 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * symbol.h - prototypes pour la gestion des symboles 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 . + */ + + +#ifndef _FORMAT_ELF_SYMBOL_H +#define _FORMAT_ELF_SYMBOL_H + + +#include "e_elf.h" + + + +/* Charge en mémoire la liste humaine des symboles. */ +bool load_elf_symbols(elf_format *); + +/* Récupère tous les commentaires à insérer dans le code. */ +void get_elf_symbol_comments(const elf_format *, char ***, uint64_t **, size_t *); + + + +#endif /* _FORMAT_ELF_SYMBOL_H */ diff --git a/src/gtksnippet.c b/src/gtksnippet.c index 6470fcb..7e5e012 100644 --- a/src/gtksnippet.c +++ b/src/gtksnippet.c @@ -90,7 +90,30 @@ void delete_code_line_info(code_line_info *line) } +/****************************************************************************** +* * +* Paramètres : a = premières informations à consulter. * +* b = secondes informations à consulter. * +* * +* Description : Etablit la comparaison entre deux lignes de représentation. * +* * +* Retour : Bilan : -1 (a < b), 0 (a == b) ou 1 (a > b). * +* * +* Remarques : - * +* * +******************************************************************************/ + +int compare_code_line_info(const code_line_info **a, const code_line_info **b) +{ + int result; /* Bilan à renvoyer */ + + if ((*a)->offset < (*b)->offset) result = -1; + else if((*a)->offset > (*b)->offset) result = 1; + else result = 0; + return result; + +} diff --git a/src/gtksnippet.h b/src/gtksnippet.h index 1858796..6e89a3c 100644 --- a/src/gtksnippet.h +++ b/src/gtksnippet.h @@ -56,6 +56,10 @@ code_line_info *create_code_line_info(uint64_t, asm_instr *, const char *); /* Supprime une ligne de représentation. */ void delete_code_line_info(code_line_info *); +/* Etablit la comparaison entre deux lignes de représentation. */ +int compare_code_line_info(const code_line_info **, const code_line_info **); + + -- cgit v0.11.2-87-g4458