From 5f64b12f3359e5f2c923fb35d330cec4cb0f4a30 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Wed, 11 Mar 2015 13:06:06 +0000
Subject: Loaded the ELF header at a proper virtual address.

git-svn-id: svn://svn.gna.org/svn/chrysalide/trunk@488 abbe820e-26c8-41b2-8c08-b7b2b41f8b0a
---
 ChangeLog                    | 19 +++++++++++++++++++
 src/analysis/disass/output.c | 41 ++++++++++++++++++++++++++---------------
 src/arch/arm/v7/post.c       | 28 ++++++++++++++++++++++++++--
 src/format/elf/program.c     | 42 ++++++++++++++++++++++++++++++++++++++++++
 src/format/elf/program.h     |  3 +++
 src/format/elf/symbols.c     | 14 +++++++++++++-
 src/format/symbol.c          |  1 -
 src/plugins/plugin.c         |  2 --
 8 files changed, 129 insertions(+), 21 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index cd2fa23..b020300 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,24 @@
 15-03-11  Cyrille Bagard <nocbos@gmail.com>
 
+	* src/analysis/disass/output.c:
+	Avoid to get stuck because a symbol can not be found and inserted.
+
+	* src/arch/arm/v7/post.c:
+	Add more debug code.
+
+	* src/format/elf/program.c:
+	* src/format/elf/program.h:
+	Build a full location from a physical offset.
+
+	* src/format/elf/symbols.c:
+	Load the ELF header at a proper virtual address.
+
+	* src/format/symbol.c:
+	* src/plugins/plugin.c:
+	Remove debug code.
+
+15-03-11  Cyrille Bagard <nocbos@gmail.com>
+
 	* src/plugins/plugin.c:
 	Fix an overflow when logging messages for plugins.
 
diff --git a/src/analysis/disass/output.c b/src/analysis/disass/output.c
index 355d458..f1e8a3b 100644
--- a/src/analysis/disass/output.c
+++ b/src/analysis/disass/output.c
@@ -24,10 +24,14 @@
 #include "output.h"
 
 
+#include <i18n.h>
+
+
 #include "../../arch/processor.h"
 #include "../../common/extstr.h"
 #include "../../decomp/lang/asm.h"
 #include "../../format/format.h"
+#include "../../gui/panels/log.h"
 
 
 
@@ -80,6 +84,7 @@ void print_disassembled_instructions(GCodeBuffer *buffer, const GExeFormat *form
 
     const vmpa2t *paddr;                    /* Adresse de portion          */
 
+    int compared;                           /* Bilan d'une comparaison     */
 
     const char *label;                      /* Etiquette ciblant un symbole*/
 
@@ -178,16 +183,32 @@ void print_disassembled_instructions(GCodeBuffer *buffer, const GExeFormat *form
             saddr = get_mrange_addr(g_binary_symbol_get_range(symbols[sym_index]));
 
 
-            printf("OUTPUT [%zu] :: 0x%08x - 0x%08x :: 0x%08x - 0x%08x   '%s'\n",
+            printf("OUTPUT [%zu] :: (instr) 0x%08x - 0x%08x :: (sym) 0x%08x - 0x%08x   '%s' (cmp=%d)\n",
                    sym_index,
                    (unsigned int)get_phy_addr(iaddr),
                    (unsigned int)get_virt_addr(iaddr),
                    (unsigned int)get_phy_addr(saddr),
                    (unsigned int)get_virt_addr(saddr),
-                   g_binary_symbol_to_string(symbols[sym_index]));
+                   g_binary_symbol_to_string(symbols[sym_index]),
+                   cmp_vmpa(iaddr, saddr));
 
 
-            if (cmp_vmpa_by_virt(iaddr, saddr) == 0)
+            /* On écarte les symboles qu'on ne sait pas réintroduire */
+            for (compared = cmp_vmpa(iaddr, saddr);
+                 compared > 0;
+                 compared = cmp_vmpa(iaddr, saddr))
+            {
+                log_variadic_message(LMT_BAD_BINARY, _("Unable to find a proper location for symbol '%s'"),
+                                     g_binary_symbol_to_string(symbols[sym_index]));
+
+                if (++sym_index == sym_count)
+                    goto no_more_symbol_finally;
+
+                saddr = get_mrange_addr(g_binary_symbol_get_range(symbols[sym_index]));
+
+            }
+
+            if (compared == 0)
             {
                 /* Etiquette ? */
 
@@ -209,8 +230,7 @@ void print_disassembled_instructions(GCodeBuffer *buffer, const GExeFormat *form
 
         }
 
-
-
+ no_more_symbol_finally:
 
 
 
@@ -221,16 +241,7 @@ void print_disassembled_instructions(GCodeBuffer *buffer, const GExeFormat *form
             iaddr = get_mrange_addr(g_arch_instruction_get_range(iter));
             saddr = get_mrange_addr(g_binary_symbol_get_range(symbols[sym_index]));
 
-            /*
-            if (saddr->virtual == 0x8590)
-            {
-                printf("instr = 0x%08x   sym = 0x%08x\n", iaddr->virtual, saddr->virtual);
-                printf("COMP :: %d\n", cmp_vmpa_by_virt(iaddr, saddr));
-                if (cmp_vmpa_by_virt(iaddr, saddr) == 0) exit(0);
-            }
-            */
-
-            if (cmp_vmpa_by_virt(iaddr, saddr) == 0)
+            if (cmp_vmpa(iaddr, saddr) == 0)
             {
 
                 /* Point d'entrée ? */
diff --git a/src/arch/arm/v7/post.c b/src/arch/arm/v7/post.c
index c7b0f64..dfeb720 100644
--- a/src/arch/arm/v7/post.c
+++ b/src/arch/arm/v7/post.c
@@ -260,8 +260,18 @@ void post_process_ldr_instructions(GArchInstruction *instr, GProcContext *contex
         {
             addr &= ~0x1;
 
+            do
+            {
+                
+                const mrange_t *_range;
 
-            printf("RESOLVING FOR 0x%08x\n", (unsigned int)addr);
+                _range = g_arch_instruction_get_range(instr);
+
+                printf("@ 0x%08x RESOLVING FOR 0x%08x\n",
+                       (unsigned int)_range->addr.virtual, (unsigned int)addr);
+
+
+            } while (0);
 
             init_vmpa(&target, VMPA_NO_PHYSICAL, addr);
             init_mrange(&trange, &target, 0);
@@ -284,7 +294,21 @@ void post_process_ldr_instructions(GArchInstruction *instr, GProcContext *contex
             g_target_operand_resolve(G_TARGET_OPERAND(new), format);
 
         }
-        else printf("RESOLVED FOR 0x%08x\n", (unsigned int)addr);
+        else
+            
+            do
+            {
+                
+                const mrange_t *_range;
+
+                _range = g_arch_instruction_get_range(instr);
+
+                printf("@ 0x%08x RESOLVED FOR 0x%08x\n",
+                       (unsigned int)_range->addr.virtual, (unsigned int)addr);
+
+
+            } while (0);
+
 
         g_arch_instruction_replace_operand(instr, new, op);
 
diff --git a/src/format/elf/program.c b/src/format/elf/program.c
index b7c23cd..d842b8e 100644
--- a/src/format/elf/program.c
+++ b/src/format/elf/program.c
@@ -143,3 +143,45 @@ bool translate_address_into_offset_using_elf_programs(const GElfFormat *format,
     return result;
 
 }
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : format  = description de l'exécutable à consulter.           *
+*                addr    = adresse virtuelle à retrouver.                     *
+*                pos     = position correspondante. [OUT]                     *
+*                                                                             *
+*  Description : Fournit l'emplacement correspondant à une position physique. *
+*                                                                             *
+*  Retour      : Bilan de l'opération.                                        *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+bool translate_offset_into_vmpa_using_elf_programs(const GElfFormat *format, phys_t off, vmpa2t *addr)
+{
+    bool result;                            /* Bilan à retourner           */
+    uint16_t i;                             /* Boucle de parcours          */
+    elf_phdr program;                       /* Programme à analyser        */
+    virt_t virt;                            /* Adresse virtuelle calculée  */
+
+    result = false;
+
+    for (i = 0; i < ELF_HDR(format, format->header, e_phnum) && !result; i++)
+    {
+        find_elf_program_by_index(format, i, &program);
+
+        if (ELF_PHDR(format, program, p_offset) <= off
+            && off < (ELF_PHDR(format, program, p_offset) + ELF_PHDR(format, program, p_filesz)))
+        {
+            virt = ELF_PHDR(format, program, p_vaddr) + ELF_PHDR(format, program, p_offset) - off;
+            init_vmpa(addr, off, virt);
+            result = true;
+        }
+
+    }
+
+    return result;
+
+}
diff --git a/src/format/elf/program.h b/src/format/elf/program.h
index 2126cfb..f2f724f 100644
--- a/src/format/elf/program.h
+++ b/src/format/elf/program.h
@@ -39,6 +39,9 @@ bool find_elf_program_by_index(const GElfFormat *, uint16_t, elf_phdr *);
 /* Fournit la position correspondant à une adresse virtuelle. */
 bool translate_address_into_offset_using_elf_programs(const GElfFormat *, vmpa_t, off_t *);
 
+/* Fournit l'emplacement correspondant à une position physique. */
+bool translate_offset_into_vmpa_using_elf_programs(const GElfFormat *, phys_t, vmpa2t *);
+
 
 
 #endif  /* _FORMAT_ELF_PROGRAM_H */
diff --git a/src/format/elf/symbols.c b/src/format/elf/symbols.c
index 1c3da14..a4ef2bc 100644
--- a/src/format/elf/symbols.c
+++ b/src/format/elf/symbols.c
@@ -531,6 +531,7 @@ static bool annotate_elf_header(GElfFormat *format)
     const bin_t *content;                   /* Contenu binaire à lire      */
     off_t length;                           /* Taille totale du contenu    */
     vmpa2t *pos;                            /* Localisation des symboles   */
+    bool status;                            /* Bilan d'une récupération    */
     const char *text;                       /* Texte constant à insérer    */
     GArchInstruction *instr;                /* Instruction décodée         */
     GArchOperand *operand;                  /* Opérande à venir modifier   */
@@ -542,6 +543,9 @@ static bool annotate_elf_header(GElfFormat *format)
 
     pos = make_vmpa(0, 0x123);
 
+    status = translate_offset_into_vmpa_using_elf_programs(format, 0, pos);
+    assert(status);
+
     /* ELFMAG (0) */
 
     instr = g_raw_instruction_new_array_old(content, MDS_8_BITS, 4, pos, length, format->endian);
@@ -954,6 +958,7 @@ static bool annotate_elf_program_header_table(GElfFormat *format)
     off_t length;                           /* Taille totale du contenu    */
     off_t offset;                           /* Tête de lecture du bbinaire */
     vmpa2t *pos;                            /* Localisation des symboles   */
+    bool status;                            /* Bilan d'une récupération    */
     uint16_t e_phnum;                       /* Nombre d'éléments 'Program' */
     uint16_t i;                             /* Boucle de parcours          */
     elf_phdr phdr;                          /* En-tête de programme ELF    */
@@ -973,6 +978,9 @@ static bool annotate_elf_program_header_table(GElfFormat *format)
 
     pos = make_vmpa(offset, 0x5500);
 
+    status = translate_offset_into_vmpa_using_elf_programs(format, offset, pos);
+    assert(status);
+
     e_phnum = ELF_HDR(format, format->header, e_phnum);
 
     for (i = 0; i < e_phnum; i++)
@@ -1236,6 +1244,7 @@ static bool annotate_elf_section_header_table(GElfFormat *format)
     off_t offset;                           /* Tête de lecture du bbinaire */
     elf_shdr strings;                       /* Section des descriptions    */
     vmpa2t *pos;                            /* Localisation des symboles   */
+    bool status;                            /* Bilan d'une récupération    */
     uint16_t e_shnum;                       /* Nombre d'éléments 'Program' */
     uint16_t i;                             /* Boucle de parcours          */
     elf_shdr shdr;                          /* En-tête de programme ELF    */
@@ -1257,7 +1266,10 @@ static bool annotate_elf_section_header_table(GElfFormat *format)
 
     offset = ELF_HDR(format, format->header, e_shoff);
 
-    pos = make_vmpa(offset, 0x9900);
+    pos = make_vmpa(offset, 0x99900);
+
+    //status = translate_offset_into_vmpa_using_elf_programs(format, offset, pos);
+    //assert(status);
 
     e_shnum = ELF_HDR(format, format->header, e_shnum);
 
diff --git a/src/format/symbol.c b/src/format/symbol.c
index 37dc45e..d360591 100644
--- a/src/format/symbol.c
+++ b/src/format/symbol.c
@@ -222,7 +222,6 @@ SymbolType g_binary_symbol_get_target_type(const GBinSymbol *symbol)
 
 const char *g_binary_symbol_to_string(const GBinSymbol *symbol)
 {
-    return "AAAA";
     const char *result;                     /* Désignation à retourner     */
 
     switch (symbol->type)
diff --git a/src/plugins/plugin.c b/src/plugins/plugin.c
index fb970b3..fca22c1 100644
--- a/src/plugins/plugin.c
+++ b/src/plugins/plugin.c
@@ -218,8 +218,6 @@ GPluginModule *g_plugin_module_new(const gchar *filename)
         category = MASK_PLUGIN_CATEGORY(result->interface->actions[i]);
         sub = MASK_PLUGIN_SUB_CATEGORY(result->interface->actions[i]);
 
-        printf(" GET cat = 0x%08x - sub = 0x%08x\n", category, sub);
-
         switch (category)
         {
             case DPC_BASIC:
-- 
cgit v0.11.2-87-g4458