diff options
Diffstat (limited to 'src/arch/target.c')
-rw-r--r-- | src/arch/target.c | 325 |
1 files changed, 325 insertions, 0 deletions
diff --git a/src/arch/target.c b/src/arch/target.c new file mode 100644 index 0000000..93ccbe4 --- /dev/null +++ b/src/arch/target.c @@ -0,0 +1,325 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * target.c - opérandes ciblant un symbole + * + * Copyright (C) 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 <http://www.gnu.org/licenses/>. + */ + + +#include "target.h" + + +#include <inttypes.h> +#include <malloc.h> +#include <stdarg.h> +#include <stdio.h> +#include <string.h> + + +#include "operand-int.h" +#include "../common/extstr.h" +#include "../format/format.h" + + + +/* Définition d'un opérande ciblant idéalement un symbole connu (instance) */ +struct _GTargetOperand +{ + GArchOperand parent; /* Instance parente */ + + MemoryDataSize size; /* Taille de l'opérande */ + virt_t addr; /* Adresse de l'élément visé */ + + GBinSymbol *symbol; /* Eventuel symbole associé */ + phys_t diff; /* Position dans le symbole */ + +}; + + +/* Définition d'un opérande ciblant idéalement un symbole connu (classe) */ +struct _GTargetOperandClass +{ + GArchOperandClass parent; /* Classe parente */ + +}; + + +/* Initialise la classe des opérandes ciblant des symboles. */ +static void g_target_operand_class_init(GTargetOperandClass *); + +/* Initialise la classe des opérandes ciblant des symboles. */ +static void g_target_operand_init(GTargetOperand *); + +/* Supprime toutes les références externes. */ +static void g_target_operand_dispose(GTargetOperand *); + +/* Procède à la libération totale de la mémoire. */ +static void g_target_operand_finalize(GTargetOperand *); + +/* Traduit un opérande en version humainement lisible. */ +static void g_target_operand_print(const GTargetOperand *, GBufferLine *, AsmSyntax); + + + +/* Indique le type défini pour un opérande de valeur numérique. */ +G_DEFINE_TYPE(GTargetOperand, g_target_operand, G_TYPE_ARCH_OPERAND); + + + +/****************************************************************************** +* * +* Paramètres : klass = classe à initialiser. * +* * +* Description : Initialise la classe des opérandes ciblant des symboles. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_target_operand_class_init(GTargetOperandClass *klass) +{ + GObjectClass *object; /* Autre version de la classe */ + GArchOperandClass *operand; /* Version de classe parente */ + + object = G_OBJECT_CLASS(klass); + operand = G_ARCH_OPERAND_CLASS(klass); + + object->dispose = (GObjectFinalizeFunc/* ! */)g_target_operand_dispose; + object->finalize = (GObjectFinalizeFunc)g_target_operand_finalize; + + operand->print = (operand_print_fc)g_target_operand_print; + +} + + +/****************************************************************************** +* * +* Paramètres : operand = instance à initialiser. * +* * +* Description : Initialise la classe des opérandes ciblant des symboles. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_target_operand_init(GTargetOperand *operand) +{ + +} + + +/****************************************************************************** +* * +* Paramètres : operand = instance d'objet GLib à traiter. * +* * +* Description : Supprime toutes les références externes. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_target_operand_dispose(GTargetOperand *operand) +{ + if (operand->symbol != NULL) + g_object_unref(G_OBJECT(operand->symbol)); + + G_OBJECT_CLASS(g_target_operand_parent_class)->dispose(G_OBJECT(operand)); + +} + + +/****************************************************************************** +* * +* Paramètres : operand = instance d'objet GLib à traiter. * +* * +* Description : Procède à la libération totale de la mémoire. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_target_operand_finalize(GTargetOperand *operand) +{ + G_OBJECT_CLASS(g_target_operand_parent_class)->finalize(G_OBJECT(operand)); + +} + + +/****************************************************************************** +* * +* Paramètres : size = taille des adresse mémoire virtuelles. * +* addr = adresse virtuelle d'un élément à retrouver. * +* * +* Description : Crée un opérande réprésentant une valeur numérique. * +* * +* Retour : Instruction mise en place. * +* * +* Remarques : - * +* * +******************************************************************************/ + +GArchOperand *g_target_operand_new(MemoryDataSize size, virt_t addr) +{ + GTargetOperand *result; /* Opérande à retourner */ + + result = g_object_new(G_TYPE_TARGET_OPERAND, NULL); + + result->size = size; + result->addr = addr; + + return G_ARCH_OPERAND(result); + +} + + +/****************************************************************************** +* * +* Paramètres : operand = opérande à traiter. * +* line = ligne tampon où imprimer l'opérande donné. * +* syntax = type de représentation demandée. * +* * +* Description : Traduit un opérande en version humainement lisible. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_target_operand_print(const GTargetOperand *operand, GBufferLine *line, AsmSyntax syntax) +{ + const char *label; /* Etiquette liée à un symbole */ + vmpa2t tmp; /* Coquille vide pour argument */ + VMPA_BUFFER(value); /* Adresse brute à imprimer */ + size_t len; /* Taille de l'élément inséré */ + + if (operand->symbol != NULL) + { + if (operand->diff > 0) + g_buffer_line_insert_text(line, BLC_MAIN, "<", 1, RTT_LTGT); + + label = g_binary_symbol_get_label(operand->symbol); + g_buffer_line_insert_text(line, BLC_MAIN, label, strlen(label), RTT_LABEL); + + if (operand->diff > 0) + { + g_buffer_line_insert_text(line, BLC_MAIN, "+", 1, RTT_SIGNS); + + init_vmpa(&tmp, operand->diff, VMPA_NO_VIRTUAL); + vmpa2_phys_to_string(&tmp, MDS_4_BITS, value, &len); + + g_buffer_line_insert_text(line, BLC_MAIN, value, len, RTT_LABEL); + + g_buffer_line_insert_text(line, BLC_MAIN, ">", 1, RTT_LTGT); + + } + + } + else + { + init_vmpa(&tmp, VMPA_NO_PHYSICAL, operand->addr); + vmpa2_virt_to_string(&tmp, operand->size, value, &len); + + g_buffer_line_insert_text(line, BLC_MAIN, value, len, RTT_LABEL); + + } + +} + + +/****************************************************************************** +* * +* Paramètres : operand = structure dont le contenu est à consulter. * +* * +* Description : Renseigne la taille de la valeur indiquée à la construction. * +* * +* Retour : Taille de la valeur représentée en mémoire. * +* * +* Remarques : - * +* * +******************************************************************************/ + +MemoryDataSize g_target_operand_get_size(const GTargetOperand *operand) +{ + return operand->size; + +} + + +/****************************************************************************** +* * +* Paramètres : operand = structure dont le contenu est à définir. * +* * +* Description : Fournit l'adresse en mémoire de l'élément visé. * +* * +* Retour : Adresse en mémoire virtuelle associée. * +* * +* Remarques : - * +* * +******************************************************************************/ + +virt_t g_target_operand_get_addr(const GTargetOperand *operand) +{ + return operand->addr; + +} + + +/****************************************************************************** +* * +* Paramètres : operand = opérande dont le contenu est à raffiner. * +* format = format du binaire d'origine à consulter. * +* * +* Description : Tente une résolution de symbole. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool g_target_operand_resolve(GTargetOperand *operand, const GBinFormat *format) +{ + bool result; /* Bilan à retourner */ + vmpa2t addr; /* Adresse de recherche */ + + if (operand->symbol != NULL) + g_object_unref(G_OBJECT(operand->symbol)); + + operand->symbol = NULL; + operand->diff = 0; + + init_vmpa(&addr, VMPA_NO_PHYSICAL, operand->addr); + + result = g_binary_format_resolve_symbol(format, &addr, &operand->symbol, &operand->diff); + + /** + * En cas de succès, le compteur de références du symbole trouvé a été incrémenté. + */ + + return result; + +} |