/* Chrysalide - Outil d'analyse de fichiers binaires * fetch.c - récupération d'instructions à partir de binaire brut * * Copyright (C) 2010-2014 Cyrille Bagard * * This file is part of Chrysalide. * * 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 "fetch.h" #include #include "area.h" /* Suit un flot d'exécution pour désassembler du code. */ static void follow_execution_flow(const GLoadedBinary *, GProcContext *, mem_area *, size_t, status_blob_info *, virt_t); /****************************************************************************** * * * Paramètres : binary = représentation de binaire chargé. * * ctx = contexte offert en soutien à un désassemblage. * * areas = liste de zones contenant des données à traiter. * * count = nombre de ces aires à disposition. * * info = informations liées à l'affichage de la progression. * * virt = adresse d'un point de départ d'un traitement. * * * * Description : Suit un flot d'exécution pour désassembler du code. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void follow_execution_flow(const GLoadedBinary *binary, GProcContext *ctx, mem_area *areas, size_t count, status_blob_info *info, virt_t virt) { vmpa2t addr; /* Conversion en pleine adresse*/ mem_area *area; /* Zone de désassemblage */ printf("-- follow 0x%08x\n", (unsigned int)virt); g_proc_context_push_drop_point(ctx, virt); while (g_proc_context_has_drop_points(ctx)) { virt = g_proc_context_pop_drop_point(ctx); init_vmpa(&addr, VMPA_NO_PHYSICAL, virt); printf(" ++ point 0x%08x\n", (unsigned int)virt); area = find_memory_area_by_addr(areas, count, &addr); load_code_from_mem_area(area, areas, count, binary, ctx, &addr, info); printf(" ++\n"); } } /****************************************************************************** * * * Paramètres : binary = représentation de binaire chargé. * * statusbar = barre de statut avec progression à mettre à jour.* * id = identifiant du message affiché à l'utilisateur. * * * * Description : Procède au désassemblage basique d'un contenu binaire. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ GArchInstruction *disassemble_binary_content(const GLoadedBinary *binary, GtkExtStatusBar *statusbar) { GArchInstruction *result; /* Instruction désassemblées */ GBinFormat *format; /* Format du fichier binaire */ GArchProcessor *proc; /* Architecture du binaire */ GProcContext *ctx; /* Contexte de désassemblage */ off_t length; /* Taille des données à lire */ mem_area *areas; /* Zone de productions */ size_t count; /* Nombre de ces zones */ status_blob_info *info; /* Informations de progression */ virt_t virt; /* Point d'accroche virtuelle */ GBinSymbol **symbols; /* Symboles à représenter */ size_t sym_count; /* Qté de symboles présents */ size_t i; /* Boucle de parcours */ const mrange_t *range; /* Couverture d'un symbole */ const vmpa2t *addr; /* Point de départ du symbole */ double done; /* Portion de travail accompli */ format = G_BIN_FORMAT(g_loaded_binary_get_format(binary)); proc = get_arch_processor_from_format(G_EXE_FORMAT(format)); ctx = g_arch_processor_get_context(proc); /* Définition à la découpe des parties à traiter */ g_loaded_binary_get_data(binary, &length); areas = compute_memory_areas(G_EXE_FORMAT(format), length, &count); /** * Première phase de désassemblage : suivi des chemins tracés. */ info = init_progessive_status(statusbar, _("Disassembling following the execution flow..."), 0, length); /* Insertion du point de départ */ virt = g_binary_format_get_entry_point(format); follow_execution_flow(binary, ctx, areas, count, info, 0x84c5); follow_execution_flow(binary, ctx, areas, count, info, 0x8a65); follow_execution_flow(binary, ctx, areas, count, info, virt); /* Symboles exécutables présents et passés à travers les mailles */ symbols = g_binary_format_get_symbols(format, &sym_count); for (i = 0; i < sym_count; i++) { if (g_binary_symbol_get_target_type(symbols[i]) != STP_FUNCTION) continue; range = g_binary_symbol_get_range(symbols[i]); addr = get_mrange_addr(range); virt = get_virt_addr(addr); follow_execution_flow(binary, ctx, areas, count, info, virt); } printf(" ------------------------------------------- follow done\n"); done = get_current_progessive_status(info); fini_progessive_status(info); /** * Seconde phase : on comble les trous laissés. */ info = init_progessive_status(statusbar, _("Disassembling the remaining instructions..."), done, length); ensure_all_mem_areas_are_filled(areas, count, binary, ctx, info); fini_progessive_status(info); /** * Troisième et dernière phase : récolte des fruits. */ info = init_progessive_status(statusbar, _("Collecting disassembled instructions..."), 0, length); result = collect_instructions_from_mem_areas(areas, count); fini_progessive_status(info); /* free */ return result; }