From f6252e57c770fea10439659f35a69a783ff849d3 Mon Sep 17 00:00:00 2001 From: Cyrille Bagard Date: Mon, 1 Jun 2009 17:49:32 +0000 Subject: Provided found MIPS routines. git-svn-id: svn://svn.gna.org/svn/chrysalide/trunk@69 abbe820e-26c8-41b2-8c08-b7b2b41f8b0a --- ChangeLog | 31 ++++++++++++++++ src/analysis/binary.c | 8 ++--- src/analysis/line_code.c | 2 -- src/analysis/line_comment.c | 10 +++++- src/format/elf/Makefile.am | 3 +- src/format/elf/e_elf.c | 28 ++++++++++----- src/format/elf/elf-int.h | 1 + src/format/elf/helper_mips.c | 85 ++++++++++++++++++++++++++++++++++++++++++++ src/format/elf/helper_mips.h | 41 +++++++++++++++++++++ src/format/elf/section.c | 5 +-- src/format/elf/symbol.c | 65 +++++++++++++++++++++++++++++++-- 11 files changed, 259 insertions(+), 20 deletions(-) create mode 100644 src/format/elf/helper_mips.c create mode 100644 src/format/elf/helper_mips.h diff --git a/ChangeLog b/ChangeLog index 39f5365..c8c6eb2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,37 @@ 09-06-01 Cyrille Bagard * src/analysis/binary.c: + Restore prototype labels. Update calls relative to routines. + + * src/analysis/line_code.c: + Remove useless code. + + * src/analysis/line_comment.c: + Fix many bugs. + + * src/format/elf/e_elf.c: + Add more verbose debug, load the .MIPS.stubs section and provide + found routines again. + + * src/format/elf/elf-int.h: + Define the ELF_ST_TYPE macro. + + * src/format/elf/helper_mips.c: + * src/format/elf/helper_mips.h: + New entries: provide specific help to ELF support for MIPS architectures. + + * src/format/elf/Makefile.am: + Add helper_mips.[ch] to libformatelf_la_SOURCES. + + * src/format/elf/section.c: + Fix two bugs about data lengths. + + * src/format/elf/symbol.c: + Create a new management of dynamic symbols. + +09-06-01 Cyrille Bagard + + * src/analysis/binary.c: * src/analysis/prototype.c: * src/analysis/prototype.h: * src/format/dwarf/Makefile.am: diff --git a/src/analysis/binary.c b/src/analysis/binary.c index d25a458..88201ac 100644 --- a/src/analysis/binary.c +++ b/src/analysis/binary.c @@ -645,14 +645,14 @@ void disassemble_openida_binary(openida_binary *binary) } /* Ajout des prototypes de fonctions */ -#if 0 + for (k = 0; k < routines_count; k++) { - routine_offset = get_binary_routine_offset(routines[k]); + routine_offset = g_binary_routine_get_address(routines[k]); if (!(base <= routine_offset && routine_offset < (base + len))) continue; - routine_desc = routine_to_string(routines[k]); + routine_desc = g_binary_routine_to_string(routines[k]); line = g_comment_line_new(routine_offset, routine_desc, &binary->options); g_rendering_line_insert_into_lines(&binary->lines, line, true); @@ -660,7 +660,7 @@ void disassemble_openida_binary(openida_binary *binary) free(routine_desc); } -#endif + } } diff --git a/src/analysis/line_code.c b/src/analysis/line_code.c index 44bb59f..a234055 100644 --- a/src/analysis/line_code.c +++ b/src/analysis/line_code.c @@ -106,8 +106,6 @@ static void g_code_line_init(GCodeLine *line) parent = G_RENDERING_LINE(line); - parent->offset = 0; - parent->type = RLT_CODE; parent->get_bin_len = (get_bin_len_fc)g_code_line_get_binary_len; diff --git a/src/analysis/line_comment.c b/src/analysis/line_comment.c index 6df7b96..ea6d6d6 100644 --- a/src/analysis/line_comment.c +++ b/src/analysis/line_comment.c @@ -99,6 +99,13 @@ static void g_comment_line_class_init(GCommentLineClass *klass) static void g_comment_line_init(GCommentLine *line) { + GRenderingLine *parent; /* Instance parente */ + + parent = G_RENDERING_LINE(line); + + parent->type = RLT_PROTOTYPE/* TODO */; + + parent->refresh_markup = (refresh_markup_fc)g_comment_line_refresh_markup; } @@ -171,7 +178,7 @@ void g_comment_line_refresh_markup(GCommentLine *line) G_RENDERING_LINE(line)->max_bin_len); len += G_RENDERING_LINE(line)->max_bin_len; - content[len] = '\0'; + content[len - 1] = '\0'; } @@ -231,6 +238,7 @@ GRenderingLine *g_comment_line_new(uint64_t offset, const char *comment, const d G_RENDERING_LINE(result)->offset = offset; result->comment = strdup(comment); + result->options = options; return G_RENDERING_LINE(result); diff --git a/src/format/elf/Makefile.am b/src/format/elf/Makefile.am index ce23fb4..fa481ca 100644 --- a/src/format/elf/Makefile.am +++ b/src/format/elf/Makefile.am @@ -1,9 +1,10 @@ noinst_LTLIBRARIES = libformatelf.la -libformatelf_la_SOURCES = \ +libformatelf_la_SOURCES = \ e_elf.h e_elf.c \ elf-int.h \ + helper_mips.h helper_mips.c \ section.h section.c \ strings.h strings.c \ symbol.h symbol.c diff --git a/src/format/elf/e_elf.c b/src/format/elf/e_elf.c index 373df2b..1de4ac0 100644 --- a/src/format/elf/e_elf.c +++ b/src/format/elf/e_elf.c @@ -204,15 +204,15 @@ elf_format *load_elf(const uint8_t *content, off_t length) test = read_elf_section_names(result); - printf("ok ? %d\n", test); + printf("section names ok ? %d\n", test); test = find_all_elf_strings(result); - printf("ok ? %d\n", test); + printf("strings ok ? %d\n", test); test = load_elf_symbols(result); - printf("ok ? %d\n", test); + printf("symbols ok ? %d\n", test); @@ -326,6 +326,18 @@ bin_part **get_elf_default_code_parts(const elf_format *format, size_t *count) } + if (find_elf_section_content_by_name(format, ".MIPS.stubs", &offset, &size, &voffset)) + { + part = create_bin_part(); + + set_bin_part_name(part, ".MIPS.stubs"); + set_bin_part_values(part, offset, size, voffset); + + result = (bin_part **)realloc(result, ++(*count) * sizeof(bin_part *)); + result[*count - 1] = part; + + } + if (find_elf_section_content_by_name(format, ".init", &offset, &size, &voffset)) { part = create_bin_part(); @@ -583,16 +595,16 @@ GBinRoutine **get_all_elf_routines(const elf_format *format, size_t *count) result = (GBinRoutine **)calloc(format->sym_count, sizeof(GBinRoutine *)); *count = format->sym_count; - /* + for (i = 0; i < format->sym_count; i++) { - result[i] = create_binary_routine(); + result[i] = g_binary_routine_new(); - set_binary_routine_offset(result[i], format->symbols[i].address); - set_binary_routine_name(result[i], strdup(format->symbols[i].name)); + g_binary_routine_set_address(result[i], format->symbols[i].address); + g_binary_routine_set_name(result[i], strdup(format->symbols[i].name)); } - */ + return result; } diff --git a/src/format/elf/elf-int.h b/src/format/elf/elf-int.h index 4ee3a08..dd8909d 100644 --- a/src/format/elf/elf-int.h +++ b/src/format/elf/elf-int.h @@ -117,6 +117,7 @@ typedef union _Elf_Sym #define ELF_SYM(fmt, sb, fld) (fmt->is_32b ? sb.sym32.fld : sb.sym64.fld) +#define ELF_ST_TYPE ELF32_ST_TYPE diff --git a/src/format/elf/helper_mips.c b/src/format/elf/helper_mips.c new file mode 100644 index 0000000..b56db5d --- /dev/null +++ b/src/format/elf/helper_mips.c @@ -0,0 +1,85 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * helper_mips.c - gestion auxiliaire de l'architecture MIPS + * + * Copyright (C) 2009 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 "helper_mips.h" + + +#include + + +#include "elf-int.h" + + + +/****************************************************************************** +* * +* Paramètres : format = description de l'exécutable à compléter. * +* dyn_start = début des informations dynamiques associées. * +* dyn_size = taille de la zone associée. * +* 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 : Déduit les adresses effectives des appels externes. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool g_elf_format_find_mips_dynamic_symbols(elf_format *format, off_t dyn_start, off_t dyn_size, off_t str_start, off_t str_size) +{ + off_t iter; /* Boucle de parcours */ + Elf_Sym symbol; /* Symbole ELF lu */ + off_t name_pos; /* Localisation du nom */ + + if (dyn_size % ELF_SIZEOF_SYM(format) != 0) return false; + + for (iter = dyn_start; iter < (dyn_start + dyn_size); iter += ELF_SIZEOF_SYM(format)) + { + memcpy(&symbol, &EXE_FORMAT(format)->content[iter], ELF_SIZEOF_SYM(format)); + + if (ELF_ST_TYPE(ELF_SYM(format, symbol, st_info)) != STT_FUNC) continue; + + if (ELF_SYM(format, symbol, st_value) == 0) continue; + + name_pos = ELF_SYM(format, symbol, st_name); + + /* Sécurité anti-débordements */ + if (name_pos >= str_size) continue; + + /* Si le symbole possède un nom... */ + if (strlen(&EXE_FORMAT(format)->content[str_start + name_pos]) > 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 + name_pos]; + format->symbols[format->sym_count - 1].address = ELF_SYM(format, symbol, st_value); + + } + + } + + return true; + +} diff --git a/src/format/elf/helper_mips.h b/src/format/elf/helper_mips.h new file mode 100644 index 0000000..6a26e03 --- /dev/null +++ b/src/format/elf/helper_mips.h @@ -0,0 +1,41 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * helper_mips.h - prototypes pour la gestion auxiliaire de l'architecture MIPS + * + * Copyright (C) 2009 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_HELPER_MIPS_H +#define _FORMAT_ELF_HELPER_MIPS_H + + +#include +#include + + +#include "e_elf.h" + + + +/* Déduit les adresses effectives des appels externes. */ +bool g_elf_format_find_mips_dynamic_symbols(elf_format *, off_t, off_t, off_t, off_t); + + + +#endif /* _FORMAT_ELF_HELPER_MIPS_H */ diff --git a/src/format/elf/section.c b/src/format/elf/section.c index 7ad8363..c619293 100644 --- a/src/format/elf/section.c +++ b/src/format/elf/section.c @@ -50,11 +50,12 @@ bool read_elf_section_names(elf_format *format) 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; + + 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; + 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; diff --git a/src/format/elf/symbol.c b/src/format/elf/symbol.c index fbb6fd8..eaf11a4 100644 --- a/src/format/elf/symbol.c +++ b/src/format/elf/symbol.c @@ -94,6 +94,10 @@ bool load_elf_symbols(elf_format *format) off_t str_size; /* Taille de section */ off_t rel_start; /* Début de section */ off_t rel_size; /* Taille de section */ + + + bool test; /* Bilan d'une recherche */ + off_t dyn_start; /* Début de section */ off_t dyn_size; /* Taille de section */ @@ -133,7 +137,56 @@ bool load_elf_symbols(elf_format *format) } - /* Relocalisations dynamiques */ + /* Liaison dynamique (si elle existe) */ + + test = find_elf_section_by_name(format, ".dynsym", §ion); + + if (!test) + { + test = find_elf_section_by_type(format, SHT_HASH, §ions, &count); + + if (test) + test = find_elf_section_by_index(format, ELF_SHDR(format, §ions[0], sh_link), §ion); + + } + + if (test) + { + get_elf_section_content(format, §ion, &dyn_start, &dyn_size, NULL); + + result &= find_elf_section_by_index(format, ELF_SHDR(format, §ion, sh_link), §ion); + + } + + if (result) + { + get_elf_section_content(format, §ion, &str_start, &str_size, NULL); + + switch (get_elf_target_machine(format)) + { + case FTM_MIPS: + result = g_elf_format_find_mips_dynamic_symbols(format, dyn_start, dyn_size, str_start, str_size); + break; + + default: + break; + + } + + } + + + + + + + + + + + + +#if 0 find_elf_section_by_type(format, SHT_REL, §ions, &count); @@ -171,6 +224,14 @@ bool load_elf_symbols(elf_format *format) /* Récupération (seconde partie) */ + + /* switch ... */ + + g_elf_format_translate_mips_external_calls(format); + exit(0); + + + if (result) { #if 0 @@ -182,7 +243,7 @@ bool load_elf_symbols(elf_format *format) #endif } - +#endif return result; -- cgit v0.11.2-87-g4458