From a4f40b1971fe208bd9c25adebaeff5614aee87ee Mon Sep 17 00:00:00 2001 From: Cyrille Bagard Date: Thu, 19 Jul 2018 21:00:34 +0200 Subject: Created an interface for jumping to addresses from operands. --- plugins/dalvik/link.c | 25 ++--- plugins/dalvik/operands/pool.c | 74 ++++++++++++- plugins/pychrysalide/analysis/content.c | 1 - plugins/pychrysalide/arch/Makefile.am | 1 + plugins/pychrysalide/arch/module.c | 3 + plugins/pychrysalide/arch/targetableop.c | 178 +++++++++++++++++++++++++++++++ plugins/pychrysalide/arch/targetableop.h | 42 ++++++++ src/analysis/disass/links.c | 9 +- src/arch/Makefile.am | 2 + src/arch/immediate.c | 71 +++++++++++- src/arch/link.c | 22 ++-- src/arch/target.c | 89 ++++++++++++---- src/arch/target.h | 3 - src/arch/targetableop-int.h | 51 +++++++++ src/arch/targetableop.c | 85 +++++++++++++++ src/arch/targetableop.h | 61 +++++++++++ src/gui/menus/edition.c | 44 +++++--- 17 files changed, 685 insertions(+), 76 deletions(-) create mode 100644 plugins/pychrysalide/arch/targetableop.c create mode 100644 plugins/pychrysalide/arch/targetableop.h create mode 100644 src/arch/targetableop-int.h create mode 100644 src/arch/targetableop.c create mode 100644 src/arch/targetableop.h diff --git a/plugins/dalvik/link.c b/plugins/dalvik/link.c index 8eda3c9..8e34485 100644 --- a/plugins/dalvik/link.c +++ b/plugins/dalvik/link.c @@ -32,7 +32,7 @@ #include #include -#include +#include #include #include @@ -142,11 +142,10 @@ void handle_links_for_dalvik_string(GArchInstruction *instr, GArchProcessor *pro void handle_dalvik_packed_switch_links(GArchInstruction *instr, GArchProcessor *proc, GProcContext *context, GExeFormat *format) { GArchOperand *op; /* Opérande numérique en place */ + const mrange_t *range; /* Emplacement de l'instruction*/ bool defined; /* Adresse définie ? */ vmpa2t addr; /* Adresse de destination */ - virt_t virt; /* Adresse virtuelle */ GArchInstruction *switch_ins; /* Instruction de branchements */ - const mrange_t *range; /* Zone d'occupation */ const vmpa2t *start_addr; /* Adresse de référentiel */ const int32_t *keys; /* Conditions de sauts */ const int32_t *targets; /* Positions relatives liées */ @@ -171,23 +170,17 @@ void handle_dalvik_packed_switch_links(GArchInstruction *instr, GArchProcessor * g_arch_instruction_unlock_operands(instr); - defined = false; - - if (G_IS_TARGET_OPERAND(op)) + if (G_IS_TARGETABLE_OPERAND(op)) { - g_target_operand_get_addr(G_TARGET_OPERAND(op), &addr); - defined = true; - } + range = g_arch_instruction_get_range(instr); - else if (G_IS_IMM_OPERAND(op)) - { - if (g_imm_operand_to_virt_t(G_IMM_OPERAND(op), &virt)) - { - init_vmpa(&addr, VMPA_NO_PHYSICAL, virt); - defined = true; - } + defined = g_targetable_operand_get_addr(G_TARGETABLE_OPERAND(op), get_mrange_addr(range), + G_BIN_FORMAT(format), proc, &addr); } + else + defined = false; + g_object_unref(G_OBJECT(op)); if (defined) diff --git a/plugins/dalvik/operands/pool.c b/plugins/dalvik/operands/pool.c index 1fd264b..4e2183b 100644 --- a/plugins/dalvik/operands/pool.c +++ b/plugins/dalvik/operands/pool.c @@ -32,6 +32,7 @@ #include +#include #include #include @@ -63,6 +64,9 @@ static void g_dalvik_pool_operand_class_init(GDalvikPoolOperandClass *); /* Initialise une instance d'opérande de constante Dalvik. */ static void g_dalvik_pool_operand_init(GDalvikPoolOperand *); +/* Procède à l'initialisation de l'interface de ciblage. */ +static void g_dalvik_pool_operand_targetable_interface_init(GTargetableOperandInterface *); + /* Supprime toutes les références externes. */ static void g_dalvik_pool_operand_dispose(GDalvikPoolOperand *); @@ -88,8 +92,17 @@ static bool g_dalvik_pool_operand_serialize(const GDalvikPoolOperand *, GAsmStor +/* ----------------------- INTERFACE DE CIBLAGE POUR OPERANDE ----------------------- */ + + +/* Obtient l'adresse de la cible visée par un opérande. */ +static bool g_dalvik_pool_operand_get_addr(const GDalvikPoolOperand *, const vmpa2t *, GBinFormat *, GArchProcessor *, vmpa2t *); + + + /* Indique le type défini par la GLib pour un un élément de table de constantes Dalvik. */ -G_DEFINE_TYPE(GDalvikPoolOperand, g_dalvik_pool_operand, G_TYPE_ARCH_OPERAND); +G_DEFINE_TYPE_WITH_CODE(GDalvikPoolOperand, g_dalvik_pool_operand, G_TYPE_ARCH_OPERAND, + G_IMPLEMENT_INTERFACE(G_TYPE_TARGETABLE_OPERAND, g_dalvik_pool_operand_targetable_interface_init)); /****************************************************************************** @@ -146,6 +159,25 @@ static void g_dalvik_pool_operand_init(GDalvikPoolOperand *operand) /****************************************************************************** * * +* Paramètres : iface = interface GLib à initialiser. * +* * +* Description : Procède à l'initialisation de l'interface de ciblage. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_dalvik_pool_operand_targetable_interface_init(GTargetableOperandInterface *iface) +{ + iface->get_addr = (get_targetable_addr_fc)g_dalvik_pool_operand_get_addr; + +} + + +/****************************************************************************** +* * * Paramètres : binary = instance d'objet GLib à traiter. * * * * Description : Supprime toutes les références externes. * @@ -350,7 +382,7 @@ static void g_dalvik_pool_operand_print(const GDalvikPoolOperand *operand, GBuff g_object_unref(G_OBJECT(routine)); g_buffer_line_append_text(line, BLC_ASSEMBLY, "<", 1, RTT_HOOK, NULL); - g_buffer_line_append_text(line, BLC_ASSEMBLY, tmp, strlen(tmp), RTT_VAR_NAME, NULL); + g_buffer_line_append_text(line, BLC_ASSEMBLY, tmp, strlen(tmp), RTT_VAR_NAME, G_OBJECT(operand)); g_buffer_line_append_text(line, BLC_ASSEMBLY, ">", 1, RTT_HOOK, NULL); } @@ -550,3 +582,41 @@ static bool g_dalvik_pool_operand_serialize(const GDalvikPoolOperand *operand, G return result; } + + + +/* ---------------------------------------------------------------------------------- */ +/* INTERFACE DE CIBLAGE POUR OPERANDE */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : operand = operande à consulter. * +* src = localisation de l'instruction mère. * +* format = format reconnu pour le binaire chargé. * +* proc = architecture associée à ce même binaire. * +* addr = localisation de la cible. [OUT] * +* * +* Description : Obtient l'adresse de la cible visée par un opérande. * +* * +* Retour : true si la cible est valide, false sinon. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static bool g_dalvik_pool_operand_get_addr(const GDalvikPoolOperand *operand, const vmpa2t *src, GBinFormat *format, GArchProcessor *proc, vmpa2t *addr) +{ + bool result; /* Bilan à retourner */ + + result = false; + + if (operand->type == DPT_METHOD) + { + result = false; + } + + return result; + +} diff --git a/plugins/pychrysalide/analysis/content.c b/plugins/pychrysalide/analysis/content.c index 05f1c74..4be318d 100644 --- a/plugins/pychrysalide/analysis/content.c +++ b/plugins/pychrysalide/analysis/content.c @@ -454,7 +454,6 @@ PyTypeObject *get_python_binary_content_type(void) "data", py_binary_content_get_all_bytes, NULL, "Provide all the content bytes at once.", NULL }, - { NULL } }; diff --git a/plugins/pychrysalide/arch/Makefile.am b/plugins/pychrysalide/arch/Makefile.am index eb4b127..da66457 100644 --- a/plugins/pychrysalide/arch/Makefile.am +++ b/plugins/pychrysalide/arch/Makefile.am @@ -9,6 +9,7 @@ libpychrysaarch_la_SOURCES = \ operand.h operand.c \ processor.h processor.c \ raw.h raw.c \ + targetableop.h targetableop.c \ vmpa.h vmpa.c libpychrysaarch_la_LIBADD = diff --git a/plugins/pychrysalide/arch/module.c b/plugins/pychrysalide/arch/module.c index 54b470c..9c10965 100644 --- a/plugins/pychrysalide/arch/module.c +++ b/plugins/pychrysalide/arch/module.c @@ -37,6 +37,7 @@ #include "operand.h" #include "processor.h" #include "raw.h" +#include "targetableop.h" #include "vmpa.h" #include "../access.h" #include "../helpers.h" @@ -143,6 +144,8 @@ bool add_arch_module_to_python_module(PyObject *super) result &= py_base_define_constants(Py_TYPE(module)); + result &= register_python_targetable_operand(module); + result &= register_python_arch_instruction(module); result &= register_python_arch_operand(module); result &= register_python_arch_processor(module); diff --git a/plugins/pychrysalide/arch/targetableop.c b/plugins/pychrysalide/arch/targetableop.c new file mode 100644 index 0000000..8951492 --- /dev/null +++ b/plugins/pychrysalide/arch/targetableop.c @@ -0,0 +1,178 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * targetableop.c - prototypes pour l'équivalent Python du fichier "arch/targetableop.c" + * + * Copyright (C) 2018 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include "targetableop.h" + + +#include +#include + + +#include + + +#include + + +#include "processor.h" +#include "vmpa.h" +#include "../format/format.h" + + + +/* Obtient l'adresse de la cible visée par un opérande. */ +static PyObject *py_targetable_operand_get_addr(PyObject *, PyObject *); + + + +/****************************************************************************** +* * +* Paramètres : self = contenu binaire à manipuler. * +* args = non utilisé ici. * +* * +* Description : Obtient l'adresse de la cible visée par un opérande. * +* * +* Retour : Localisation de la cible ou None. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_targetable_operand_get_addr(PyObject *self, PyObject *args) +{ + PyObject *result; /* Instance à retourner */ + PyObject *src_obj; /* Objet pour une position */ + PyObject *format_obj; /* Objet pour un format */ + PyObject *proc_obj; /* Objet pour une architecture */ + int ret; /* Bilan de lecture des args. */ + vmpa2t *src; /* Version native d'adresse */ + GBinFormat *format; /* Instance GLib du format */ + GArchProcessor *proc; /* Instance GLib de l'archi. */ + GTargetableOperand *operand; /* Instance à manipuler */ + vmpa2t addr; /* Localisation à cibler */ + bool defined; /* Cible définie ? */ + + ret = PyArg_ParseTuple(args, "O!", + get_python_vmpa_type(), &src_obj, + get_python_binary_format_type(), &format_obj, + get_python_arch_processor_type(), &proc_obj); + if (!ret) return NULL; + + src = get_internal_vmpa(src_obj); + assert(src != NULL); + + format = G_BIN_FORMAT(pygobject_get(format_obj)); + proc = G_ARCH_PROCESSOR(pygobject_get(proc_obj)); + + operand = G_TARGETABLE_OPERAND(pygobject_get(self)); + assert(operand != NULL); + + defined = g_targetable_operand_get_addr(operand, src, format, proc, &addr); + + if (defined) + result = build_from_internal_vmpa(&addr); + else + { + result = Py_None; + Py_INCREF(result); + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Fournit un accès à une définition de type à diffuser. * +* * +* Retour : Définition d'objet pour Python. * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyTypeObject *get_python_targetable_operand_type(void) +{ + static PyMethodDef py_targetable_operand_methods[] = { + { + "get_addr", py_targetable_operand_get_addr, + METH_VARARGS, + "get_addr($self, src, format, proc, /)\n--\n\nGet a target address from an operand." + }, + { NULL } + }; + + static PyGetSetDef py_targetable_operand_getseters[] = { + { NULL } + }; + + static PyTypeObject py_targetable_operand_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.arch.TargetableOperand", + .tp_basicsize = sizeof(PyObject), + + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + + .tp_doc = "PyChrysalide targetable operand", + + .tp_methods = py_targetable_operand_methods, + .tp_getset = py_targetable_operand_getseters + + }; + + return &py_targetable_operand_type; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Prend en charge l'objet 'pychrysalide.....TargetableOperand'.* +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool register_python_targetable_operand(PyObject *module) +{ + PyTypeObject *py_targetable_operand_type; /* Type 'TargetableOperand' */ + PyObject *dict; /* Dictionnaire du module */ + + py_targetable_operand_type = get_python_targetable_operand_type(); + + dict = PyModule_GetDict(module); + pyg_register_interface(dict, "TargetableOperand", G_TYPE_TARGETABLE_OPERAND, py_targetable_operand_type); + + return true; + +} diff --git a/plugins/pychrysalide/arch/targetableop.h b/plugins/pychrysalide/arch/targetableop.h new file mode 100644 index 0000000..d79bb87 --- /dev/null +++ b/plugins/pychrysalide/arch/targetableop.h @@ -0,0 +1,42 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * targetableop.h - prototypes pour l'équivalent Python du fichier "arch/targetableop.h" + * + * Copyright (C) 2018 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#ifndef _PLUGINS_PYCHRYSALIDE_ARCH_TARGETABLEOP_H +#define _PLUGINS_PYCHRYSALIDE_ARCH_TARGETABLEOP_H + + +#include +#include + + + +/* Fournit un accès à une définition de type à diffuser. */ +PyTypeObject *get_python_targetable_operand_type(void); + +/* Prend en charge l'objet 'pychrysalide.arch.TargetableOperand'. */ +bool register_python_targetable_operand(PyObject *); + + + +#endif /* _PLUGINS_PYCHRYSALIDE_ARCH_TARGETABLEOP_H */ diff --git a/src/analysis/disass/links.c b/src/analysis/disass/links.c index 0207d6b..ffdee36 100644 --- a/src/analysis/disass/links.c +++ b/src/analysis/disass/links.c @@ -30,6 +30,7 @@ #include "../../arch/instruction.h" #include "../../arch/raw.h" #include "../../arch/target.h" +#include "../../arch/targetableop.h" @@ -262,6 +263,7 @@ void establish_links_for_instruction(GArchInstruction *instr, GBinFormat *format size_t count; /* Nombre d'opérandes présents */ size_t i; /* Boucle de parcours */ GArchOperand *op; /* Opérande numérique en place */ + const mrange_t *range; /* Emplacement de l'instruction*/ vmpa2t addr; /* Localisation plus complète */ GArchInstruction *target; /* Instruction visée au final */ @@ -280,9 +282,12 @@ void establish_links_for_instruction(GArchInstruction *instr, GBinFormat *format convert_immediate_into_target(instr, i, format); op = _g_arch_instruction_get_operand(instr, i); - if (!G_IS_TARGET_OPERAND(op)) goto next_op; + if (!G_IS_TARGETABLE_OPERAND(op)) goto next_op; - g_target_operand_get_addr(G_TARGET_OPERAND(op), &addr); + range = g_arch_instruction_get_range(instr); + + if (!g_targetable_operand_get_addr(G_TARGETABLE_OPERAND(op), get_mrange_addr(range), format, proc, &addr)) + goto next_op; target = g_arch_processor_find_instr_by_address(proc, &addr); diff --git a/src/arch/Makefile.am b/src/arch/Makefile.am index 8710b13..b600557 100644 --- a/src/arch/Makefile.am +++ b/src/arch/Makefile.am @@ -20,6 +20,8 @@ libarch_la_SOURCES = \ register.h register.c \ storage.h storage.c \ target.h target.c \ + targetableop-int.h \ + targetableop.h targetableop.c \ undefined.h undefined.c \ vmpa.h vmpa.c diff --git a/src/arch/immediate.c b/src/arch/immediate.c index 1c084ea..5519059 100644 --- a/src/arch/immediate.c +++ b/src/arch/immediate.c @@ -38,6 +38,7 @@ #include "operand-int.h" +#include "targetableop-int.h" #include "../common/asm.h" #include "../common/extstr.h" #include "../format/format.h" @@ -86,6 +87,9 @@ static void g_imm_operand_class_init(GImmOperandClass *); /* Initialise la classe des lignes de descriptions initiales. */ static void g_imm_operand_init(GImmOperand *); +/* Procède à l'initialisation de l'interface de ciblage. */ +static void g_imm_operand_targetable_interface_init(GTargetableOperandInterface *); + /* Supprime toutes les références externes. */ static void g_imm_operand_dispose(GImmOperand *); @@ -120,8 +124,17 @@ static bool g_imm_operand_serialize(const GImmOperand *, GAsmStorage *, packed_b +/* ----------------------- INTERFACE DE CIBLAGE POUR OPERANDE ----------------------- */ + + +/* Obtient l'adresse de la cible visée par un opérande. */ +static bool g_imm_operand_get_addr(const GImmOperand *, const vmpa2t *, GBinFormat *, GArchProcessor *, vmpa2t *); + + + /* Indique le type défini pour un opérande de valeur numérique. */ -G_DEFINE_TYPE(GImmOperand, g_imm_operand, G_TYPE_ARCH_OPERAND); +G_DEFINE_TYPE_WITH_CODE(GImmOperand, g_imm_operand, G_TYPE_ARCH_OPERAND, + G_IMPLEMENT_INTERFACE(G_TYPE_TARGETABLE_OPERAND, g_imm_operand_targetable_interface_init)); @@ -182,6 +195,25 @@ static void g_imm_operand_init(GImmOperand *operand) /****************************************************************************** * * +* Paramètres : iface = interface GLib à initialiser. * +* * +* Description : Procède à l'initialisation de l'interface de ciblage. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_imm_operand_targetable_interface_init(GTargetableOperandInterface *iface) +{ + iface->get_addr = (get_targetable_addr_fc)g_imm_operand_get_addr; + +} + + +/****************************************************************************** +* * * Paramètres : operand = instance d'objet GLib à traiter. * * * * Description : Supprime toutes les références externes. * @@ -1464,3 +1496,40 @@ static bool g_imm_operand_serialize(const GImmOperand *operand, GAsmStorage *sto return result; } + + + +/* ---------------------------------------------------------------------------------- */ +/* INTERFACE DE CIBLAGE POUR OPERANDE */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : operand = operande à consulter. * +* src = localisation de l'instruction mère. * +* format = format reconnu pour le binaire chargé. * +* proc = architecture associée à ce même binaire. * +* addr = localisation de la cible. [OUT] * +* * +* Description : Obtient l'adresse de la cible visée par un opérande. * +* * +* Retour : true si la cible est valide, false sinon. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static bool g_imm_operand_get_addr(const GImmOperand *operand, const vmpa2t *src, GBinFormat *format, GArchProcessor *proc, vmpa2t *addr) +{ + bool result; /* Bilan à retourner */ + virt_t virt; /* Adresse virtuelle */ + + result = g_imm_operand_to_virt_t(operand, &virt); + + if (result) + result = g_exe_format_translate_address_into_vmpa(G_EXE_FORMAT(format), virt, addr); + + return result; + +} diff --git a/src/arch/link.c b/src/arch/link.c index c11fe54..f09621e 100644 --- a/src/arch/link.c +++ b/src/arch/link.c @@ -27,7 +27,7 @@ #include -#include "target.h" +#include "targetableop.h" @@ -99,11 +99,10 @@ void handle_jump_as_link(GArchInstruction *instr, GArchProcessor *proc, GProcCon void handle_branch_as_link(GArchInstruction *instr, GArchProcessor *proc, GProcContext *context, GExeFormat *format, size_t index) { GArchOperand *op; /* Opérande numérique en place */ + const mrange_t *range; /* Emplacement de l'instruction*/ bool defined; /* Adresse définie ? */ vmpa2t addr; /* Adresse de destination */ - virt_t virt; /* Adresse virtuelle */ GArchInstruction *target; /* Ligne visée par la référence*/ - const mrange_t *range; /* Emplacement d'instruction */ vmpa2t next; /* Position suivante */ g_arch_instruction_lock_operands(instr); @@ -114,20 +113,17 @@ void handle_branch_as_link(GArchInstruction *instr, GArchProcessor *proc, GProcC g_arch_instruction_unlock_operands(instr); - defined = false; - - if (G_IS_TARGET_OPERAND(op)) + if (G_IS_TARGETABLE_OPERAND(op)) { - g_target_operand_get_addr(G_TARGET_OPERAND(op), &addr); - defined = true; - } + range = g_arch_instruction_get_range(instr); - else if (G_IS_IMM_OPERAND(op)) - { - if (g_imm_operand_to_virt_t(G_IMM_OPERAND(op), &virt)) - defined = g_exe_format_translate_address_into_vmpa(format, virt, &addr); + defined = g_targetable_operand_get_addr(G_TARGETABLE_OPERAND(op), get_mrange_addr(range), + G_BIN_FORMAT(format), proc, &addr); } + else + defined = false; + g_object_unref(G_OBJECT(op)); if (defined) diff --git a/src/arch/target.c b/src/arch/target.c index 97b2192..6872efe 100644 --- a/src/arch/target.c +++ b/src/arch/target.c @@ -33,6 +33,7 @@ #include "operand-int.h" +#include "targetableop-int.h" #include "../analysis/routine.h" #include "../common/extstr.h" #include "../format/format.h" @@ -69,6 +70,9 @@ static void g_target_operand_class_init(GTargetOperandClass *); /* Initialise la classe des opérandes ciblant des symboles. */ static void g_target_operand_init(GTargetOperand *); +/* Procède à l'initialisation de l'interface de ciblage. */ +static void g_target_operand_targetable_interface_init(GTargetableOperandInterface *); + /* Supprime toutes les références externes. */ static void g_target_operand_dispose(GTargetOperand *); @@ -97,8 +101,17 @@ static bool g_target_operand_serialize(const GTargetOperand *, GAsmStorage *, pa +/* ----------------------- INTERFACE DE CIBLAGE POUR OPERANDE ----------------------- */ + + +/* Obtient l'adresse de la cible visée par un opérande. */ +static bool g_target_operand_get_addr(const GTargetOperand *, const vmpa2t *, GBinFormat *, GArchProcessor *, vmpa2t *); + + + /* Indique le type défini pour un opérande de valeur numérique. */ -G_DEFINE_TYPE(GTargetOperand, g_target_operand, G_TYPE_ARCH_OPERAND); +G_DEFINE_TYPE_WITH_CODE(GTargetOperand, g_target_operand, G_TYPE_ARCH_OPERAND, + G_IMPLEMENT_INTERFACE(G_TYPE_TARGETABLE_OPERAND, g_target_operand_targetable_interface_init)); @@ -161,6 +174,25 @@ static void g_target_operand_init(GTargetOperand *operand) /****************************************************************************** * * +* Paramètres : iface = interface GLib à initialiser. * +* * +* Description : Procède à l'initialisation de l'interface de ciblage. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_target_operand_targetable_interface_init(GTargetableOperandInterface *iface) +{ + iface->get_addr = (get_targetable_addr_fc)g_target_operand_get_addr; + +} + + +/****************************************************************************** +* * * Paramètres : operand = instance d'objet GLib à traiter. * * * * Description : Supprime toutes les références externes. * @@ -452,26 +484,6 @@ MemoryDataSize g_target_operand_get_size(const GTargetOperand *operand) /****************************************************************************** * * -* Paramètres : operand = structure dont le contenu est à consulter. * -* addr = localisation à renseigner. [OUT] * -* * -* Description : Fournit l'adresse en mémoire de l'élément visé. * -* * -* Retour : - * -* * -* Remarques : - * -* * -******************************************************************************/ - -void g_target_operand_get_addr(const GTargetOperand *operand, vmpa2t *addr) -{ - copy_vmpa(addr, &operand->addr); - -} - - -/****************************************************************************** -* * * Paramètres : operand = opérande dont le contenu est à raffiner. [OUT] * * format = format du binaire d'origine à consulter. * * strict = indique la perfection attendue de la résolution. * @@ -659,3 +671,38 @@ static bool g_target_operand_serialize(const GTargetOperand *operand, GAsmStorag return result; } + + + +/* ---------------------------------------------------------------------------------- */ +/* INTERFACE DE CIBLAGE POUR OPERANDE */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : operand = operande à consulter. * +* src = localisation de l'instruction mère. * +* format = format reconnu pour le binaire chargé. * +* proc = architecture associée à ce même binaire. * +* addr = localisation de la cible. [OUT] * +* * +* Description : Obtient l'adresse de la cible visée par un opérande. * +* * +* Retour : true si la cible est valide, false sinon. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static bool g_target_operand_get_addr(const GTargetOperand *operand, const vmpa2t *src, GBinFormat *format, GArchProcessor *proc, vmpa2t *addr) +{ + bool result; /* Bilan à retourner */ + + result = true; + + copy_vmpa(addr, &operand->addr); + + return result; + +} diff --git a/src/arch/target.h b/src/arch/target.h index 3c68fda..f7a673c 100644 --- a/src/arch/target.h +++ b/src/arch/target.h @@ -59,9 +59,6 @@ GArchOperand *g_target_operand_new(MemoryDataSize, const vmpa2t *); /* Renseigne la taille de la valeur indiquée à la construction. */ MemoryDataSize g_target_operand_get_size(const GTargetOperand *); -/* Fournit l'adresse en mémoire de l'élément visé. */ -void g_target_operand_get_addr(const GTargetOperand *, vmpa2t *); - /* Tente une résolution de symbole. */ bool g_target_operand_resolve(GTargetOperand *, GBinFormat *, bool); diff --git a/src/arch/targetableop-int.h b/src/arch/targetableop-int.h new file mode 100644 index 0000000..9afce11 --- /dev/null +++ b/src/arch/targetableop-int.h @@ -0,0 +1,51 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * targetableop-int.h - définitions internes propres aux opérandes ciblant une portion de désassemblage + * + * Copyright (C) 2018 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 . + */ + + +#ifndef _GLIBEXT_TARGETABLEOP_INT_H +#define _GLIBEXT_TARGETABLEOP_INT_H + + +#include "targetableop.h" + + + +/* Obtient l'adresse de la cible visée par un opérande. */ +typedef bool (* get_targetable_addr_fc) (const GTargetableOperand *, const vmpa2t *, GBinFormat *, GArchProcessor *, vmpa2t *); + + +/* Opérande ciblant une portion de désassemblage (interface) */ +struct _GTargetableOperandIface +{ + GTypeInterface base_iface; /* A laisser en premier */ + + get_targetable_addr_fc get_addr; /* Obtention de la cible */ + +}; + + +/* Redéfinition */ +typedef GTargetableOperandIface GTargetableOperandInterface; + + + +#endif /* _GLIBEXT_TARGETABLEOP_INT_H */ diff --git a/src/arch/targetableop.c b/src/arch/targetableop.c new file mode 100644 index 0000000..eab26a8 --- /dev/null +++ b/src/arch/targetableop.c @@ -0,0 +1,85 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * targetableop.c - opérandes ciblant une portion de désassemblage + * + * Copyright (C) 2018 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 "targetableop.h" + + +#include "targetableop-int.h" + + + +/* Procède à l'initialisation de l'interface de ciblage. */ +static void g_targetable_operand_default_init(GTargetableOperandInterface *); + + + +/* Détermine le type d'une interface pour le ciblage d'une portion de désassemblage. */ +G_DEFINE_INTERFACE(GTargetableOperand, g_targetable_operand, G_TYPE_OBJECT) + + +/****************************************************************************** +* * +* Paramètres : iface = interface GLib à initialiser. * +* * +* Description : Procède à l'initialisation de l'interface de ciblage. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_targetable_operand_default_init(GTargetableOperandInterface *iface) +{ + +} + + +/****************************************************************************** +* * +* Paramètres : operand = operande à consulter. * +* src = localisation de l'instruction mère. * +* format = format reconnu pour le binaire chargé. * +* proc = architecture associée à ce même binaire. * +* addr = localisation de la cible. [OUT] * +* * +* Description : Obtient l'adresse de la cible visée par un opérande. * +* * +* Retour : true si la cible est valide, false sinon. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool g_targetable_operand_get_addr(const GTargetableOperand *operand, const vmpa2t *src, GBinFormat *format, GArchProcessor *proc, vmpa2t *addr) +{ + bool result; /* Bilan à retourner */ + GTargetableOperandIface *iface; /* Interface utilisée */ + + iface = G_TARGETABLE_OPERAND_GET_IFACE(operand); + + result = iface->get_addr(operand, src, format, proc, addr); + + return result; + +} diff --git a/src/arch/targetableop.h b/src/arch/targetableop.h new file mode 100644 index 0000000..f6cc7bf --- /dev/null +++ b/src/arch/targetableop.h @@ -0,0 +1,61 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * targetableop.h - prototypes pour les opérandes ciblant une portion de désassemblage + * + * Copyright (C) 2018 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 . + */ + + +#ifndef _GLIBEXT_TARGETABLEOP_H +#define _GLIBEXT_TARGETABLEOP_H + + +#include +#include + + +#include "vmpa.h" +#include "../arch/processor.h" +#include "../format/format.h" + + + +#define G_TYPE_TARGETABLE_OPERAND (g_targetable_operand_get_type()) +#define G_TARGETABLE_OPERAND(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), G_TYPE_TARGETABLE_OPERAND, GTargetableOperand)) +#define G_TARGETABLE_OPERAND_CLASS(vtable) (G_TYPE_CHECK_CLASS_CAST((vtable), G_TYPE_TARGETABLE_OPERAND, GTargetableOperandIface)) +#define G_IS_TARGETABLE_OPERAND(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), G_TYPE_TARGETABLE_OPERAND)) +#define G_IS_TARGETABLE_OPERAND_CLASS(vtable) (G_TYPE_CHECK_CLASS_TYPE((vtable), G_TYPE_TARGETABLE_OPERAND)) +#define G_TARGETABLE_OPERAND_GET_IFACE(inst) (G_TYPE_INSTANCE_GET_INTERFACE((inst), G_TYPE_TARGETABLE_OPERAND, GTargetableOperandIface)) + + +/* Opérande ciblant une portion de désassemblage (coquille vide) */ +typedef struct _GTargetableOperand GTargetableOperand; + +/* Opérande ciblant une portion de désassemblage (interface) */ +typedef struct _GTargetableOperandIface GTargetableOperandIface; + + +/* Détermine le type d'une interface pour le ciblage d'une portion de désassemblage. */ +GType g_targetable_operand_get_type(void) G_GNUC_CONST; + +/* Obtient l'adresse de la cible visée par un opérande. */ +bool g_targetable_operand_get_addr(const GTargetableOperand *, const vmpa2t *, GBinFormat *, GArchProcessor *, vmpa2t *); + + + +#endif /* _GLIBEXT_TARGETABLEOP_H */ diff --git a/src/gui/menus/edition.c b/src/gui/menus/edition.c index 80c0796..04dd9df 100644 --- a/src/gui/menus/edition.c +++ b/src/gui/menus/edition.c @@ -37,7 +37,7 @@ #include "../dialogs/goto.h" #include "../dialogs/gotox.h" #include "../../analysis/db/items/switcher.h" -#include "../../arch/target.h" +#include "../../arch/targetableop.h" #include "../../glibext/gbinarycursor.h" #include "../../gtkext/easygtk.h" #include "../../gtkext/gtkblockdisplay.h" @@ -366,7 +366,7 @@ void update_access_for_cursor_in_menu_edition(GLoadedPanel *panel, const GLineCu /* Bascule des opérandes numériques */ - access = (G_IS_IMM_OPERAND(creator)); + access = G_IS_IMM_OPERAND(creator); item = GTK_WIDGET(g_object_get_data(ref, "mnu_edit_switch_hex")); gtk_widget_set_sensitive(item, access); @@ -385,7 +385,7 @@ void update_access_for_cursor_in_menu_edition(GLoadedPanel *panel, const GLineCu /* Suivi de cibles */ - access = ((G_IS_TARGET_OPERAND(creator) || G_IS_IMM_OPERAND(creator))); + access = G_IS_TARGETABLE_OPERAND(creator); item = GTK_WIDGET(g_object_get_data(ref, "mnu_edit_follow_ref")); gtk_widget_set_sensitive(item, access); @@ -538,9 +538,13 @@ static void mcb_edition_follow_ref(GtkMenuItem *menuitem, gpointer unused) { GLoadedPanel *panel; /* Afficheur effectif de code */ GObject *creator; /* Créateur à l'orgine du seg. */ + GLoadedBinary *binary; /* Binaire en cours d'étude */ + GBinFormat *format; /* Format binaire associé */ + GArchProcessor *proc; /* Architecture associée */ + GLineCursor *cursor; /* Curseur courant */ + vmpa2t iaddr; /* Emplacement de l'instruction*/ bool defined; /* Adresse définie ? */ vmpa2t addr; /* Adresse de destination */ - virt_t virt; /* Adresse virtuelle */ panel = get_current_view(); assert(GTK_IS_BLOCK_DISPLAY(panel) || GTK_IS_GRAPH_DISPLAY(panel)); @@ -548,23 +552,29 @@ static void mcb_edition_follow_ref(GtkMenuItem *menuitem, gpointer unused) creator = gtk_display_panel_get_active_object(GTK_DISPLAY_PANEL(panel)); assert(creator != NULL); - defined = false; - - if (G_IS_TARGET_OPERAND(creator)) + if (G_IS_TARGETABLE_OPERAND(creator)) { - g_target_operand_get_addr(G_TARGET_OPERAND(creator), &addr); - defined = true; - } + binary = G_LOADED_BINARY(get_current_content()); + + format = G_BIN_FORMAT(g_loaded_binary_get_format(binary)); + proc = g_loaded_binary_get_processor(binary); + + cursor = g_loaded_panel_get_cursor(panel); + g_binary_cursor_get_info(G_BINARY_CURSOR(cursor), &iaddr); + g_object_unref(G_OBJECT(cursor)); + + defined = g_targetable_operand_get_addr(G_TARGETABLE_OPERAND(creator), &iaddr, format, proc, &addr); + + g_object_unref(G_OBJECT(proc)); + g_object_unref(G_OBJECT(format)); + + g_object_unref(G_OBJECT(binary)); - else if (G_IS_IMM_OPERAND(creator)) - { - if (g_imm_operand_to_virt_t(G_IMM_OPERAND(creator), &virt)) - { - init_vmpa(&addr, VMPA_NO_PHYSICAL, virt); - defined = true; - } } + else + defined = false; + if (defined) gtk_display_panel_request_move(GTK_DISPLAY_PANEL(panel), &addr); -- cgit v0.11.2-87-g4458