From 04dfbc68e7cd5036017f097a67ba5f0288ddace0 Mon Sep 17 00:00:00 2001 From: Cyrille Bagard Date: Tue, 16 Dec 2014 21:13:42 +0000 Subject: Created operands for resolving symbols in disassembly code. git-svn-id: svn://svn.gna.org/svn/chrysalide/trunk@442 abbe820e-26c8-41b2-8c08-b7b2b41f8b0a --- ChangeLog | 57 +++++++ src/analysis/disass/fetch.c | 10 ++ src/analysis/disass/links.c | 4 + src/analysis/disass/loop.c | 4 + src/analysis/disass/output.c | 2 +- src/analysis/routine.h | 4 +- src/arch/Makefile.am | 1 + src/arch/arm/v7/Makefile.am | 1 + src/arch/arm/v7/helpers.c | 2 +- src/arch/arm/v7/opdefs/bl_A8825.d | 12 +- src/arch/arm/v7/post.c | 216 +++++++++++++++++++++++++ src/arch/arm/v7/post.h | 41 +++++ src/arch/arm/v7/processor.c | 28 +++- src/arch/immediate.c | 1 + src/arch/instruction-int.h | 2 + src/arch/instruction.c | 48 ++++++ src/arch/instruction.h | 12 ++ src/arch/target.c | 325 ++++++++++++++++++++++++++++++++++++++ src/arch/target.h | 71 +++++++++ src/format/dex/method.h | 2 + src/format/format.c | 6 + src/glibext/gbuffersegment.c | 1 + src/glibext/gbuffersegment.h | 2 + themes/segments.css | 9 ++ 24 files changed, 851 insertions(+), 10 deletions(-) create mode 100644 src/arch/arm/v7/post.c create mode 100644 src/arch/arm/v7/post.h create mode 100644 src/arch/target.c create mode 100644 src/arch/target.h diff --git a/ChangeLog b/ChangeLog index 0803f95..58f7624 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,60 @@ +14-12-16 Cyrille Bagard + + * src/analysis/disass/fetch.c: + Execute post operations on instructions after disassembling. + + * src/analysis/disass/links.c: + * src/analysis/disass/loop.c: + Fix headers. + + * src/analysis/disass/output.c: + Use the new theme item for labels. + + * src/analysis/routine.h: + Fix headers. + + * src/arch/arm/v7/helpers.c: + Fix a bug when computing the Most Significant Bit of a value. + + * src/arch/arm/v7/Makefile.am: + Add the 'post.[ch]' files to libarcharmv7_la_SOURCES. + + * src/arch/arm/v7/opdefs/bl_A8825.d: + Fix definitions. + + * src/arch/arm/v7/post.c: + * src/arch/arm/v7/post.h: + New entries: define new post handlers for 'bl' and 'blx' opcodes. + + * src/arch/arm/v7/processor.c: + Manually set post operations for instructions. + + * src/arch/immediate.c: + Bad hack: leak an internal value. This needs to be fixed! + + * src/arch/instruction.c: + * src/arch/instruction.h: + * src/arch/instruction-int.h: + Set and use post disassembling callbacks. + + * src/arch/Makefile.am: + Add the 'target.[ch]' files to libarch_la_SOURCES. + + * src/arch/target.c: + * src/arch/target.h: + New entries: create operands for resolving symbols in disassembly code. + + * src/format/dex/method.h: + Fix headers. + + * src/format/format.c: + Sort all symbols when their list gets modified. + + * src/glibext/gbuffersegment.c: + * src/glibext/gbuffersegment.h: + * themes/segments.css: + Introduce a new theme item for labels. + 14-12-15 Cyrille Bagard * src/analysis/disass/output.c: diff --git a/src/analysis/disass/fetch.c b/src/analysis/disass/fetch.c index f10178a..dea6645 100644 --- a/src/analysis/disass/fetch.c +++ b/src/analysis/disass/fetch.c @@ -430,6 +430,16 @@ static GArchInstruction *load_code_binary(const GLoadedBinary *binary, const vmp //done += (new_phy - old_phy); //gtk_extended_status_bar_update_activity(statusbar, id, done * 1.0 / sum); + + + + + + g_arch_instruction_call_post_prod_function(instr, format); /* FIXME */ + + + + } return result; diff --git a/src/analysis/disass/links.c b/src/analysis/disass/links.c index d437682..dc870fc 100644 --- a/src/analysis/disass/links.c +++ b/src/analysis/disass/links.c @@ -25,6 +25,10 @@ +#include "../../arch/instruction.h" + + + /****************************************************************************** * * * Paramètres : list = ensemble d'instructions à relier. * diff --git a/src/analysis/disass/loop.c b/src/analysis/disass/loop.c index 79e3786..0a06748 100644 --- a/src/analysis/disass/loop.c +++ b/src/analysis/disass/loop.c @@ -30,6 +30,10 @@ +#include "../../arch/instruction.h" + + + /* Suivi du flot d'exécution */ typedef struct _exec_flow { diff --git a/src/analysis/disass/output.c b/src/analysis/disass/output.c index 97924cd..eb53206 100644 --- a/src/analysis/disass/output.c +++ b/src/analysis/disass/output.c @@ -189,7 +189,7 @@ void print_disassembled_instructions(GCodeBuffer *buffer, const GExeFormat *form g_buffer_line_fill_mrange(line, msize, msize); g_buffer_line_start_merge_at(line, BLC_ASSEMBLY_HEAD); - g_buffer_line_insert_text(line, BLC_ASSEMBLY_HEAD, label, strlen(label), RTT_COMMENT); + g_buffer_line_insert_text(line, BLC_ASSEMBLY_HEAD, label, strlen(label), RTT_LABEL); g_buffer_line_insert_text(line, BLC_ASSEMBLY_HEAD, ":", 1, RTT_PUNCT); diff --git a/src/analysis/routine.h b/src/analysis/routine.h index 3aa33ac..177547b 100644 --- a/src/analysis/routine.h +++ b/src/analysis/routine.h @@ -32,10 +32,12 @@ #include "block.h" #include "variable.h" -#include "../arch/instruction.h" +//#include "../arch/instruction.h" #include "../decomp/instruction.h" +typedef struct _GArchInstruction GArchInstruction; + /* Type de routine traitée */ typedef enum _RoutineType diff --git a/src/arch/Makefile.am b/src/arch/Makefile.am index 1c63016..33747fb 100644 --- a/src/arch/Makefile.am +++ b/src/arch/Makefile.am @@ -16,6 +16,7 @@ libarch_la_SOURCES = \ raw.h raw.c \ register-int.h \ register.h register.c \ + target.h target.c \ translate.h \ vmpa.h vmpa.c diff --git a/src/arch/arm/v7/Makefile.am b/src/arch/arm/v7/Makefile.am index 0bb88aa..704b3a6 100644 --- a/src/arch/arm/v7/Makefile.am +++ b/src/arch/arm/v7/Makefile.am @@ -5,6 +5,7 @@ libarcharmv7_la_SOURCES = \ arm.h arm.c \ helpers.h helpers.c \ instruction.h instruction.c \ + post.h post.c \ processor.h processor.c \ pseudo.h pseudo.c \ register.h register.c \ diff --git a/src/arch/arm/v7/helpers.c b/src/arch/arm/v7/helpers.c index 8acfeb5..45c6ba5 100644 --- a/src/arch/arm/v7/helpers.c +++ b/src/arch/arm/v7/helpers.c @@ -145,7 +145,7 @@ GArchOperand *sign_extend_armv7_imm(uint32_t value, bool topbit, unsigned int si mds = MDS_ ## sz ## _BITS_SIGNED; \ val ## sz = value; \ if (topbit) \ - for (i = msb + 1; i < sz; i++) \ + for (i = msb; i < sz; i++) \ val ## sz |= (1 << i); \ result = g_imm_operand_new_from_value(mds, val ## sz); \ break; diff --git a/src/arch/arm/v7/opdefs/bl_A8825.d b/src/arch/arm/v7/opdefs/bl_A8825.d index c9ef5d0..dc9c922 100644 --- a/src/arch/arm/v7/opdefs/bl_A8825.d +++ b/src/arch/arm/v7/opdefs/bl_A8825.d @@ -25,7 +25,7 @@ @encoding(T1) { - @word 1 1 1 1 0 S(1) imm10H(10) 1 1 J1(1) 1 J2(1) imm10L(10) H(1) + @word 1 1 1 1 0 S(1) imm10(10) 1 1 J1(1) 1 J2(1) imm11(11) @syntax @@ -33,7 +33,7 @@ I1 = NOT(J1 EOR S) I2 = NOT(J2 EOR S) - imm32 = SignExtend(S:I1:I2:imm10H:imm10L:'00', s, 32) + imm32 = SignExtend(S:I1:I2:imm10:imm11:'0', S, 32) } @@ -41,15 +41,15 @@ @encoding(T2) { - @word 1 1 1 1 0 S(1) imm10(10) 1 1 J1(1) 0 J2(1) imm11(11) + @word 1 1 1 1 0 S(1) imm10H(10) 1 1 J1(1) 0 J2(1) imm10L(10) H(1) - @syntax + @syntax "blx" @conv { I1 = NOT(J1 EOR S) I2 = NOT(J2 EOR S) - imm32 = SignExtend(S:I1:I2:imm10:imm11:'0', S, 32) + imm32 = SignExtend(S:I1:I2:imm10H:imm10L:'00', S, 32) } @@ -73,7 +73,7 @@ @word cond(4) 1 0 1 H(1) imm24(24) - @syntax + @syntax "blx" @conv { diff --git a/src/arch/arm/v7/post.c b/src/arch/arm/v7/post.c new file mode 100644 index 0000000..8ba12d8 --- /dev/null +++ b/src/arch/arm/v7/post.c @@ -0,0 +1,216 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * post.c - traitements complémentaires à la phase de désassemblage + * + * 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 "post.h" + + + + + +#include "../../target.h" + + + + + +/****************************************************************************** +* * +* Paramètres : instr = instruction ARMv7 à traiter. * +* format = acès aux données du binaire d'origine. * +* * +* Description : Complète un désassemblage accompli pour une instruction. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +void post_process_thumb_instruction_bl(GArchInstruction *instr, GBinFormat *format) +{ + + + const mrange_t *range; + + virt_t pc; + + + + + + GArchOperand *op; + vmpa_t val; + GArchOperand *new; + + vmpa2t target; + mrange_t trange; + VMPA_BUFFER(loc); + char name[5 + VMPA_MAX_LEN]; + GBinRoutine *routine; /* Nouvelle routine trouvée */ + GBinSymbol *symbol; /* Nouveau symbole construit */ + + + + + range = g_arch_instruction_get_range(instr); + + pc = get_virt_addr(get_mrange_addr(range)) + 4 /* PC++ */; + printf(" -- PC = 0x%x\n", (unsigned int)pc); + + + op = g_arch_instruction_get_operand(instr, 0); + + g_imm_operand_to_vmpa_t(G_IMM_OPERAND(op), &val); + + printf(" -> %llx = %lld ==>> 0x%x\n", val, val, (unsigned int)(pc + val)); + + + + new = g_target_operand_new(MDS_32_BITS, pc + val); + + if (!g_target_operand_resolve(G_TARGET_OPERAND(new), format)) + { + init_vmpa(&target, VMPA_NO_PHYSICAL, pc + val); + init_mrange(&trange, &target, 0); + + vmpa2_virt_to_string(&target, MDS_32_BITS, loc, NULL); + snprintf(name, sizeof(name), "loc_%s", loc + 2); + + routine = g_binary_routine_new(); + g_binary_routine_set_name(routine, strdup(name)); + //routine = try_to_demangle_routine(name); + + g_binary_routine_set_range(routine, &trange); + + symbol = g_binary_symbol_new(STP_ROUTINE, NULL, ~0); + g_binary_symbol_attach_routine(symbol, routine); + g_binary_format_add_symbol(G_BIN_FORMAT(format), symbol); + + + + g_target_operand_resolve(G_TARGET_OPERAND(new), format); + + + } + + + g_arch_instruction_replace_operand(instr, new, op); + //g_arch_instruction_attach_extra_operand(result, new); + + + +} + + + + + +/****************************************************************************** +* * +* Paramètres : instr = instruction ARMv7 à traiter. * +* format = acès aux données du binaire d'origine. * +* * +* Description : Complète un désassemblage accompli pour une instruction. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +void post_process_thumb_instruction_blx(GArchInstruction *instr, GBinFormat *format) +{ + + + + + + const mrange_t *range; + + virt_t pc; + + + + GArchOperand *op; + vmpa_t val; + GArchOperand *new; + + vmpa2t target; + mrange_t trange; + VMPA_BUFFER(loc); + char name[5 + VMPA_MAX_LEN]; + GBinRoutine *routine; /* Nouvelle routine trouvée */ + GBinSymbol *symbol; /* Nouveau symbole construit */ + + + + range = g_arch_instruction_get_range(instr); + + pc = get_virt_addr(get_mrange_addr(range)) + 4 /* PC++ */; + + printf(" -- PC = 0x%x -> 0x%x\n", (unsigned int)pc, (unsigned int)pc - pc % 4); + pc = pc - pc % 4; + + op = g_arch_instruction_get_operand(instr, 0); + + g_imm_operand_to_vmpa_t(G_IMM_OPERAND(op), &val); + + printf(" -> %llx = %lld ==>> 0x%x\n", val, val, (unsigned int)(pc + val)); + + + + new = g_target_operand_new(MDS_32_BITS, pc + val); + + if (!g_target_operand_resolve(G_TARGET_OPERAND(new), format)) + { + init_vmpa(&target, VMPA_NO_PHYSICAL, pc + val); + init_mrange(&trange, &target, 0); + + vmpa2_virt_to_string(&target, MDS_32_BITS, loc, NULL); + snprintf(name, sizeof(name), "loc_%s", loc + 2); + + routine = g_binary_routine_new(); + g_binary_routine_set_name(routine, strdup(name)); + //routine = try_to_demangle_routine(name); + + g_binary_routine_set_range(routine, &trange); + + symbol = g_binary_symbol_new(STP_ROUTINE, NULL, ~0); + g_binary_symbol_attach_routine(symbol, routine); + g_binary_format_add_symbol(G_BIN_FORMAT(format), symbol); + + + + g_target_operand_resolve(G_TARGET_OPERAND(new), format); + + + } + + g_arch_instruction_replace_operand(instr, new, op); + //g_arch_instruction_attach_extra_operand(result, new); + + + + +} + diff --git a/src/arch/arm/v7/post.h b/src/arch/arm/v7/post.h new file mode 100644 index 0000000..d9ad99e --- /dev/null +++ b/src/arch/arm/v7/post.h @@ -0,0 +1,41 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * post.h - prototypes pour les traitements complémentaires à la phase de désassemblage + * + * 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 . + */ + + +#ifndef _ARCH_ARM_V7_POST_H +#define _ARCH_ARM_V7_POST_H + + +#include "../../instruction.h" +#include "../../../format/format.h" + + + +/* Complète un désassemblage accompli pour une instruction. */ +void post_process_thumb_instruction_bl(GArchInstruction *, GBinFormat *); + +/* Complète un désassemblage accompli pour une instruction. */ +void post_process_thumb_instruction_blx(GArchInstruction *, GBinFormat *); + + + +#endif /* _ARCH_ARM_V7_POST_H */ diff --git a/src/arch/arm/v7/processor.c b/src/arch/arm/v7/processor.c index dac9639..2b8617b 100644 --- a/src/arch/arm/v7/processor.c +++ b/src/arch/arm/v7/processor.c @@ -200,7 +200,7 @@ GArmV7Processor *g_armv7_processor_new(void) * Remarques : - * * * ******************************************************************************/ - +#include "post.h" static GArchInstruction *g_armv7_processor_disassemble(const GArmV7Processor *proc, GArmContext *ctx, const bin_t *data, vmpa2t *pos, phys_t end) { GArchInstruction *result; /* Instruction à renvoyer */ @@ -209,6 +209,9 @@ static GArchInstruction *g_armv7_processor_disassemble(const GArmV7Processor *pr uint16_t raw16; /* Donnée 16 bits à analyser */ uint32_t raw32; /* Donnée 32 bits à analyser */ + + + start = get_phy_addr(pos); diff = 4; @@ -258,6 +261,29 @@ static GArchInstruction *g_armv7_processor_disassemble(const GArmV7Processor *pr result = g_raw_instruction_new_array(data, MDS_32_BITS, 1, pos, end, G_ARCH_PROCESSOR(proc)->endianness); + + + + + if (strcmp(g_arch_instruction_get_keyword(result, 0), "bl") == 0/* && pc == 0x000085b2*/) + { + + + g_arch_instruction_set_post_prod_function(result, post_process_thumb_instruction_bl); + + + + } + + if (strcmp(g_arch_instruction_get_keyword(result, 0), "blx") == 0/* && pc == 0x000085b2*/) + { + + g_arch_instruction_set_post_prod_function(result, post_process_thumb_instruction_blx); + + + } + + return result; } diff --git a/src/arch/immediate.c b/src/arch/immediate.c index 574fc3d..f8ef432 100644 --- a/src/arch/immediate.c +++ b/src/arch/immediate.c @@ -857,6 +857,7 @@ bool g_imm_operand_to_vmpa_t(const GImmOperand *operand, vmpa_t *addr) *addr = operand->unsigned_imm.val64; break; default: + *addr = operand->signed_imm.val32;////// result = false; break; } diff --git a/src/arch/instruction-int.h b/src/arch/instruction-int.h index f96753a..aedd468 100644 --- a/src/arch/instruction-int.h +++ b/src/arch/instruction-int.h @@ -58,6 +58,8 @@ struct _GArchInstruction const char *suffix; /* Complément au nom affiché */ char *cached_keyword; /* Désignation complète */ + instr_post_prod_fc post_prod; /* A remplacer par un signal... */ + mrange_t range; /* Emplacement en mémoire */ /* ------- %< ----------- */ diff --git a/src/arch/instruction.c b/src/arch/instruction.c index c8640b4..a96f0e4 100644 --- a/src/arch/instruction.c +++ b/src/arch/instruction.c @@ -167,6 +167,51 @@ void g_arch_instruction_append_suffix(GArchInstruction *instr, const char *suffi /****************************************************************************** * * +* Paramètres : instr = instruction quelconque à modifier. * +* post = fonction à appeler sur commande. * +* * +* Description : Définit une fonction de post-traitement après désassemblage. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +void g_arch_instruction_set_post_prod_function(GArchInstruction *instr, instr_post_prod_fc post) +{ + instr->post_prod = post; + +} + + +/****************************************************************************** +* * +* Paramètres : instr = instruction quelconque à traiter. * +* data = données éventuelles associées à l'opération. * +* * +* Description : Complète un désassemblage accompli pour une instruction. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +void g_arch_instruction_call_post_prod_function(GArchInstruction *instr, void *data) +{ + if (instr->post_prod != NULL) + instr->post_prod(instr, data); + +} + + + + + + +/****************************************************************************** +* * * Paramètres : instr = instruction quelconque à modifier. * * address = adresse virtuelle et/ou position physique. * * length = taille de l'instruction. * @@ -321,7 +366,10 @@ void g_arch_instruction_replace_operand(GArchInstruction *instr, GArchOperand *n break; if (i < instr->operands_count) + { + g_object_unref(G_OBJECT(instr->operands[i])); instr->operands[i] = new; + } } diff --git a/src/arch/instruction.h b/src/arch/instruction.h index 287e34f..7b9fe66 100644 --- a/src/arch/instruction.h +++ b/src/arch/instruction.h @@ -35,6 +35,7 @@ #include "../decomp/context.h" #include "../decomp/instruction.h" //#include "../format/executable.h" +#include "../format/format.h" @@ -59,6 +60,17 @@ GType g_arch_instruction_get_type(void); /* Etend la désignation d'un nom d'instruction. */ void g_arch_instruction_append_suffix(GArchInstruction *, const char *); +/* Complète un désassemblage accompli pour une instruction. */ +typedef void (* instr_post_prod_fc)(GArchInstruction *, void *); + +/* Définit une fonction de post-traitement après désassemblage. */ +void g_arch_instruction_set_post_prod_function(GArchInstruction *, instr_post_prod_fc); + +/* Complète un désassemblage accompli pour une instruction. */ +void g_arch_instruction_call_post_prod_function(GArchInstruction *, void *); + + + /* Définit la localisation d'une instruction. */ void g_arch_instruction_set_range(GArchInstruction *, const mrange_t *); 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 . + */ + + +#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) + { + 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; + +} diff --git a/src/arch/target.h b/src/arch/target.h new file mode 100644 index 0000000..ada1a6c --- /dev/null +++ b/src/arch/target.h @@ -0,0 +1,71 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * target.h - prototypes pour les 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 . + */ + + +#ifndef _ARCH_TARGET_H +#define _ARCH_TARGET_H + + +#include +#include + + +#include "archbase.h" +#include "operand.h" +#include "vmpa.h" +#include "../format/format.h" + + + +#define G_TYPE_TARGET_OPERAND g_target_operand_get_type() +#define G_TARGET_OPERAND(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), g_target_operand_get_type(), GTargetOperand)) +#define G_IS_TARGET_OPERAND(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), g_target_operand_get_type())) +#define G_TARGET_OPERAND_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), G_TYPE_TARGET_OPERAND, GTargetOperandClass)) +#define G_IS_TARGET_OPERAND_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), G_TYPE_TARGET_OPERAND)) +#define G_TARGET_OPERAND_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), G_TYPE_TARGET_OPERAND, GTargetOperandClass)) + + +/* Définition d'un opérande ciblant idéalement un symbole connu (instance) */ +typedef struct _GTargetOperand GTargetOperand; + +/* Définition d'un opérande ciblant idéalement un symbole connu (classe) */ +typedef struct _GTargetOperandClass GTargetOperandClass; + + +/* Indique le type défini pour un opérande d'architecture. */ +GType g_target_operand_get_type(void); + +/* Crée un opérande réprésentant une valeur numérique. */ +GArchOperand *g_target_operand_new(MemoryDataSize, virt_t); + +/* 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é. */ +virt_t g_target_operand_get_addr(const GTargetOperand *); + +/* Tente une résolution de symbole. */ +bool g_target_operand_resolve(GTargetOperand *, const GBinFormat *); + + + +#endif /* _ARCH_TARGET_H */ diff --git a/src/format/dex/method.h b/src/format/dex/method.h index e4e483b..472e42d 100644 --- a/src/format/dex/method.h +++ b/src/format/dex/method.h @@ -32,6 +32,8 @@ #include "dex_def.h" +#include "../part.h" + #define G_TYPE_DEX_METHOD (g_dex_method_get_type()) #define G_DEX_METHOD(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), G_TYPE_DEX_METHOD, GDexMethod)) diff --git a/src/format/format.c b/src/format/format.c index ff62b80..cb43a94 100644 --- a/src/format/format.c +++ b/src/format/format.c @@ -198,6 +198,12 @@ void g_binary_format_add_symbol(GBinFormat *format, GBinSymbol *symbol) format->symbols[format->symbols_count - 1] = symbol; + + qsort(format->symbols, format->symbols_count, + sizeof(GBinSymbol *), (__compar_fn_t)g_binary_symbol_cmp); + + + } diff --git a/src/glibext/gbuffersegment.c b/src/glibext/gbuffersegment.c index 1e1a9b9..3d9e5a9 100644 --- a/src/glibext/gbuffersegment.c +++ b/src/glibext/gbuffersegment.c @@ -67,6 +67,7 @@ static const char *_segment_names[RTT_COUNT] = { [RTT_VIRT_ADDR_PAD] = SEGMENT_NAME("virt-addr-padding"), [RTT_VIRT_ADDR] = SEGMENT_NAME("virt-addr"), [RTT_RAW_CODE] = SEGMENT_NAME("raw-code"), + [RTT_LABEL] = SEGMENT_NAME("label"), [RTT_INSTRUCTION] = SEGMENT_NAME("instruction"), [RTT_IMMEDIATE] = SEGMENT_NAME("immediate"), [RTT_REGISTER] = SEGMENT_NAME("register"), diff --git a/src/glibext/gbuffersegment.h b/src/glibext/gbuffersegment.h index 2dd0b6d..f5cd4b2 100644 --- a/src/glibext/gbuffersegment.h +++ b/src/glibext/gbuffersegment.h @@ -54,6 +54,8 @@ typedef enum _RenderingTagType RTT_VIRT_ADDR, /* Adresse virtuelle */ RTT_RAW_CODE, /* Code binaire brut */ + RTT_LABEL, /* Etiquette sur une adresse */ + RTT_INSTRUCTION, /* Code binaire brut */ RTT_IMMEDIATE, /* Valeur immédiate */ diff --git a/themes/segments.css b/themes/segments.css index daeac45..9a4a589 100644 --- a/themes/segments.css +++ b/themes/segments.css @@ -71,6 +71,15 @@ } +.segment-label { + + color: #ffd200; + + font-style: normal; + font-weight: normal; + +} + .segment-instruction { color: #bebebe; -- cgit v0.11.2-87-g4458