/* Chrysalide - Outil d'analyse de fichiers binaires * block.c - encadrement des instructions par blocs * * Copyright (C) 2016-2017 Cyrille Bagard * * This file is part of Chrysalide. * * Chrysalide 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. * * Chrysalide 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 Chrysalide. If not, see . */ #include "block.h" #include #include "../block-int.h" #include "../../glibext/gbinarycursor.h" /* ------------------------ MISE EN PLACE DES BLOCS BASIQUES ------------------------ */ /* Description d'un bloc basique d'instructions (instance) */ struct _GBasicBlock { GCodeBlock parent; /* A laisser en premier */ GLoadedBinary *binary; /* Binaire chargé et associé */ GArchInstruction *first; /* Première instruction */ GArchInstruction *last; /* Dernière instruction */ }; /* Description d'un bloc basique d'instructions (classe) */ struct _GBasicBlockClass { GCodeBlockClass parent; /* A laisser en premier */ }; /* Initialise la classe des blocs d'instructions basique. */ static void g_basic_block_class_init(GBasicBlockClass *); /* Initialise un bloc d'instructions basique. */ static void g_basic_block_init(GBasicBlock *); /* Supprime toutes les références externes. */ static void g_basic_block_dispose(GBasicBlock *); /* Procède à la libération totale de la mémoire. */ static void g_basic_block_finalize(GBasicBlock *); /* Détermine si un bloc de code débute à une adresse donnée. */ static bool g_basic_block_is_starting_with(const GBasicBlock *, const vmpa2t *); /* Etablit les liens entre un bloc de code et ses voisins. */ static void g_basic_block_resolve_links(GBasicBlock *, const GBlockList *); /* Fournit la représentation graphique d'un bloc de code. */ static GBufferView *g_basic_block_build_view(const GBasicBlock *, segcnt_list *); /* ---------------------------------------------------------------------------------- */ /* MISE EN PLACE DES BLOCS BASIQUES */ /* ---------------------------------------------------------------------------------- */ /* Indique le type défini pour un bloc d'instructions basique. */ G_DEFINE_TYPE(GBasicBlock, g_basic_block, G_TYPE_CODE_BLOCK); /****************************************************************************** * * * Paramètres : class = classe à initialiser. * * * * Description : Initialise la classe des blocs d'instructions basique. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_basic_block_class_init(GBasicBlockClass *class) { GObjectClass *object; /* Autre version de la classe */ GCodeBlockClass *block; /* Version parente de la classe*/ object = G_OBJECT_CLASS(class); object->dispose = (GObjectFinalizeFunc/* ! */)g_basic_block_dispose; object->finalize = (GObjectFinalizeFunc)g_basic_block_finalize; block = G_CODE_BLOCK_CLASS(class); block->is_starting = (block_is_starting_fc)g_basic_block_is_starting_with; block->link = (block_resolve_links_fc)g_basic_block_resolve_links; block->build = (block_build_view_fc)g_basic_block_build_view; } /****************************************************************************** * * * Paramètres : block = instance à initialiser. * * * * Description : Initialise un bloc d'instructions basique. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_basic_block_init(GBasicBlock *block) { } /****************************************************************************** * * * Paramètres : block = instance d'objet GLib à traiter. * * * * Description : Supprime toutes les références externes. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_basic_block_dispose(GBasicBlock *block) { g_clear_object(&block->binary); g_clear_object(&block->first); g_clear_object(&block->last); G_OBJECT_CLASS(g_basic_block_parent_class)->dispose(G_OBJECT(block)); } /****************************************************************************** * * * Paramètres : block = instance d'objet GLib à traiter. * * * * Description : Procède à la libération totale de la mémoire. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_basic_block_finalize(GBasicBlock *block) { G_OBJECT_CLASS(g_basic_block_parent_class)->finalize(G_OBJECT(block)); } /****************************************************************************** * * * Paramètres : binary = binaire chargé contenant les instructions. * * first = première instruction du bloc. * * last = dernière instruction du bloc. * * bits = liste des blocs dominés. * * * * Description : Crée un bloc basique d'exécution d'instructions. * * * * Retour : Adresse de la structure mise en place. * * * * Remarques : - * * * ******************************************************************************/ GCodeBlock *g_basic_block_new(GLoadedBinary *binary, GArchInstruction *first, GArchInstruction *last, const bitfield_t *bits) { GBasicBlock *result; /* Structure à retourner */ GCodeBlock *parent; /* Version parente d'instance */ result = g_object_new(G_TYPE_BASIC_BLOCK, NULL); result->binary = binary; g_object_ref(G_OBJECT(binary)); result->first = first; result->last = last; g_object_ref(G_OBJECT(first)); g_object_ref(G_OBJECT(last)); parent = G_CODE_BLOCK(result); parent->domination = dup_bit_field(bits); return parent; } /****************************************************************************** * * * Paramètres : block = bloc de code à consulter. * * addr = localisation à comparer. * * * * Description : Détermine si un bloc de code débute à une adresse donnée. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static bool g_basic_block_is_starting_with(const GBasicBlock *block, const vmpa2t *addr) { bool result; /* Bilan à retourner */ const mrange_t *range; /* Couverture d'instruction */ range = g_arch_instruction_get_range(block->first); result = (cmp_vmpa(addr, get_mrange_addr(range)) == 0); return result; } /****************************************************************************** * * * Paramètres : block = bloc de code à mettre à jour. * * list = ensemble des blocs de code à disposition. * * * * Description : Etablit les liens entre un bloc de code et ses voisins. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_basic_block_resolve_links(GBasicBlock *block, const GBlockList *list) { size_t dcount; /* Nombre de liens de dest. */ size_t i; /* Boucle de parcours */ const instr_link_t *dest; /* Instr. visée par une autre */ const mrange_t *range; /* Couverture d'instruction */ GCodeBlock *target; /* Bloc ciblé par un lien */ g_arch_instruction_lock_dest(block->last); dcount = g_arch_instruction_count_destinations(block->last); for (i = 0; i < dcount; i++) { dest = g_arch_instruction_get_destination(block->last, i); range = g_arch_instruction_get_range(dest->linked); target = g_block_list_find_by_starting_addr(list, get_mrange_addr(range)); /** * Les sauts ne se font pas toujours à l'intérieur d'une même fonction. * Par exemple sous ARM : * * 00008358 : * .... * 8362: f7ff bfcf b.w 8304 <_init+0x38> * .... * */ if (target != NULL) { g_code_block_link_with(G_CODE_BLOCK(block), target, dest->type); g_object_unref(G_OBJECT(target)); } unref_instr_link(dest); } g_arch_instruction_unlock_dest(block->last); } /****************************************************************************** * * * Paramètres : block = bloc de code à manipuler. * * highlighted = gestionnaire de surbrillance pour segments. * * * * Description : Fournit la représentation graphique d'un bloc de code. * * * * Retour : Vue d'un cache de lignes. * * * * Remarques : - * * * ******************************************************************************/ static GBufferView *g_basic_block_build_view(const GBasicBlock *block, segcnt_list *highlighted) { GBufferView *result; /* Instance à retourner */ const mrange_t *first_range; /* Couverture d'instruction #1 */ const mrange_t *last_range; /* Couverture d'instruction #2 */ GLineCursor *start; /* Départ de zone couverture */ GLineCursor *end; /* Fin de zone couverture */ GBufferCache *cache; /* Tampon brut à découper */ first_range = g_arch_instruction_get_range(block->first); last_range = g_arch_instruction_get_range(block->last); start = g_binary_cursor_new(); g_binary_cursor_update(G_BINARY_CURSOR(start), get_mrange_addr(first_range)); end = g_binary_cursor_new(); g_binary_cursor_update(G_BINARY_CURSOR(end), get_mrange_addr(last_range)); cache = g_loaded_binary_get_disassembled_cache(block->binary); result = g_buffer_view_new(cache, highlighted); g_buffer_view_restrict(result, start, end); return result; } /****************************************************************************** * * * Paramètres : block = bloc d'instructions à consulter. * * first = instruction de départ du bloc. [OUT] * * last = dernière instruction du bloc. [OUT] * * * * Description : Fournit les instructions limites d'un bloc basique. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ void g_basic_block_get_boundary(const GBasicBlock *block, GArchInstruction **first, GArchInstruction **last) { if (first != NULL) { *first = block->first; g_object_ref(G_OBJECT(*first)); } if (last != NULL) { *last = block->last; g_object_ref(G_OBJECT(*last)); } }