/* 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 . */ #include "target.h" #include #include #include #include #include #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 /* FIXME */ && g_binary_symbol_get_label(operand->symbol) != NULL /* FIXME */) { 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_append_text(line, BLC_MAIN, label, strlen(label), RTT_LABEL, G_OBJECT(operand)); if (operand->diff > 0) { g_buffer_line_append_text(line, BLC_MAIN, "+", 1, RTT_SIGNS, G_OBJECT(operand)); init_vmpa(&tmp, operand->diff, VMPA_NO_VIRTUAL); vmpa2_phys_to_string(&tmp, MDS_4_BITS, value, &len); g_buffer_line_append_text(line, BLC_MAIN, value, len, RTT_LABEL, G_OBJECT(operand)); 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_append_text(line, BLC_MAIN, value, len, RTT_LABEL, G_OBJECT(operand)); } } /****************************************************************************** * * * 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. * * strict = indique si la résolution doit être parfaite ou non.* * * * Description : Tente une résolution de symbole. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ bool g_target_operand_resolve(GTargetOperand *operand, GBinFormat *format, bool strict) { 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, strict, &operand->symbol, &operand->diff); return result; } /****************************************************************************** * * * Paramètres : operand = opérande dont le contenu est à raffiner. * * diff = décallage entre le symbole et l'adresse initiale. * * * * Description : Fournit les indications concernant le symbole associé. * * * * Retour : Symbole résolu ou NULL si aucun. * * * * Remarques : - * * * ******************************************************************************/ GBinSymbol *g_target_operand_get_symbol(const GTargetOperand *operand, phys_t *diff) { if (diff != NULL) *diff = operand->diff; return operand->symbol; }