From 5dbb4e6a9f0dcb75abf9e7abdc0d8a98f66af147 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Wed, 15 Jul 2009 22:35:11 +0000
Subject: Restored the routine address resolution.

git-svn-id: svn://svn.gna.org/svn/chrysalide/trunk@96 abbe820e-26c8-41b2-8c08-b7b2b41f8b0a
---
 ChangeLog                   | 17 +++++++++++++++++
 src/arch/immediate.c        | 10 +++++-----
 src/format/elf/e_elf.c      | 35 +++++++++++++++++++++--------------
 src/format/elf/e_elf.h      |  2 +-
 src/format/elf/elf-int.h    |  2 +-
 src/format/elf/strings.c    | 16 ++++++++--------
 src/format/elf/strings.h    |  2 +-
 src/format/exe_format-int.h |  2 +-
 src/format/exe_format.c     | 12 ++++++------
 src/format/exe_format.h     |  2 +-
 10 files changed, 62 insertions(+), 38 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 375be3e..75dd22f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,22 @@
 09-07-16  Cyrille Bagard <nocbos@gmail.com>
 
+	* src/arch/immediate.c:
+	Fix types: uint64_t -> vmpa_t.
+
+	* src/format/elf/e_elf.c:
+	* src/format/elf/e_elf.h:
+	Restore the routine address resolution.
+
+	* src/format/elf/elf-int.h:
+	* src/format/elf/strings.c:
+	* src/format/elf/strings.h:
+	* src/format/exe_format.c:
+	* src/format/exe_format.h:
+	* src/format/exe_format-int.h:
+	Fix types: uint64_t -> vmpa_t.
+
+09-07-16  Cyrille Bagard <nocbos@gmail.com>
+
 	* src/analysis/binary.c:
 	Remove a GCC warning and compute links for unverified conditions.
 
diff --git a/src/arch/immediate.c b/src/arch/immediate.c
index 3bc4712..c57919f 100644
--- a/src/arch/immediate.c
+++ b/src/arch/immediate.c
@@ -342,7 +342,7 @@ static char *g_imm_operand_get_text(const GImmOperand *operand, const exe_format
     char *result;                           /* Chaîne à retourner          */
     char *label;                            /* Etiquette de symbole        */
     SymbolType symtype;                     /* Type de symbole             */
-    vmpa_t offset;                          /* Décallage final constaté    */
+    vmpa_t address;                         /* Décallage final constaté    */
     char buffer[256];                       /* Complément d'information    */
 
     /* Valeur brute */
@@ -435,15 +435,15 @@ static char *g_imm_operand_get_text(const GImmOperand *operand, const exe_format
 
     if (operand->size == AOS_32_BITS_SIGNED || operand->size == AOS_32_BITS_UNSIGNED)   /* FIXME */
     {
-        offset = operand->unsigned_imm.val32; /* FIXME !!! */
+        address = operand->unsigned_imm.val32; /* FIXME !!! */
 
-        if (resolve_exe_symbol(format, &label, &symtype, &offset))
+        if (resolve_exe_symbol(format, &label, &symtype, &address))
         {
             switch (symtype)
             {
                 case STP_SECTION:
-                    if (offset == 0) snprintf(buffer, 256, " &lt;%s&gt;", label);
-                    else snprintf(buffer, 256, " &lt;%s+0x%llx&gt;", label, offset);
+                    if (address == 0) snprintf(buffer, 256, " &lt;%s&gt;", label);
+                    else snprintf(buffer, 256, " &lt;%s+0x%llx&gt;", label, address);
                     result = stradd(result, buffer);
                     break;
 
diff --git a/src/format/elf/e_elf.c b/src/format/elf/e_elf.c
index 081e245..a91e279 100644
--- a/src/format/elf/e_elf.c
+++ b/src/format/elf/e_elf.c
@@ -513,7 +513,7 @@ size_t get_elf_resolved_items(const elf_format *format, char ***labels, Resolved
     {
         (*labels)[start + i] = strndup(format->strings[i].value, format->strings[i].len);
         (*types)[start + i] = RTP_STRING;
-        (*offsets)[start + i] = format->strings[i].vaddress;
+        (*offsets)[start + i] = format->strings[i].address;
 
         (*labels)[start + i] = escape_crlf((*labels)[start + i]);
 
@@ -526,10 +526,10 @@ size_t get_elf_resolved_items(const elf_format *format, char ***labels, Resolved
 
 /******************************************************************************
 *                                                                             *
-*  Paramètres  : format = informations chargées à consulter.                  *
-*                label  = étiquette du symbole si trouvé. [OUT]               *
-*                type   = type du symbole trouvé. [OUT]                       *
-*                offset = adresse à cibler, puis décallage final. [OUT]       *
+*  Paramètres  : format  = informations chargées à consulter.                 *
+*                label   = étiquette du symbole si trouvé. [OUT]              *
+*                type    = type du symbole trouvé. [OUT]                      *
+*                address = adresse à cibler, puis décallage final. [OUT]      *
 *                                                                             *
 *  Description : Recherche le symbole correspondant à une adresse.            *
 *                                                                             *
@@ -539,35 +539,42 @@ size_t get_elf_resolved_items(const elf_format *format, char ***labels, Resolved
 *                                                                             *
 ******************************************************************************/
 
-bool resolve_elf_symbol(const elf_format *format, char **label, SymbolType *type, uint64_t *offset)
+bool resolve_elf_symbol(const elf_format *format, char **label, SymbolType *type, vmpa_t *address)
 {
     bool result;                            /* Bilan à retourner           */
     size_t best_index;                      /* Meilleur symbole trouvé     */
-    uint64_t best_addr;                     /* Meilleure adresse trouvée   */
+    vmpa_t best_addr;                       /* Meilleure adresse trouvée   */
+    vmpa_t addr;                            /* Adresse de routine          */
     size_t i;                               /* Boucle de parcours          */
 
-    if (resolve_elf_strings(format, label, offset))
+    if (resolve_elf_strings(format, label, address))
     {
         *type = STP_STRING;
         return true;
     }
 
-    best_addr = UINT64_MAX;
+    best_index = format->routines_count;    /* Pour GCC */
+    best_addr = UINT64_MAX; /* FIXME */
 
-    for (i = 0; i < format->sym_count; i++)
-        if (format->symbols[i].address <= *offset && (*offset - format->symbols[i].address) < best_addr)
+    for (i = 0; i < format->routines_count; i++)
+    {
+        addr = g_binary_routine_get_address(format->routines[i]);
+
+        if (addr <= *address && (*address - addr) < best_addr)
         {
             best_index = i;
-            best_addr = *offset - format->symbols[i].address;
+            best_addr = *address - addr;
         }
 
+    }
+
     result = (best_addr != UINT64_MAX);
 
     if (result)
     {
-        *label = strdup(format->symbols[best_index].name);
+        *label = strdup(g_binary_routine_get_name(format->routines[best_index]));
         *type = STP_SECTION;
-        *offset -= format->symbols[best_index].address;
+        *address -= g_binary_routine_get_address(format->routines[best_index]);
     }
 
     return result;
diff --git a/src/format/elf/e_elf.h b/src/format/elf/e_elf.h
index 9327fe6..b8abe05 100644
--- a/src/format/elf/e_elf.h
+++ b/src/format/elf/e_elf.h
@@ -53,7 +53,7 @@ bin_part **get_elf_default_code_parts(const elf_format *, size_t *);
 size_t get_elf_symbols(const elf_format *, char ***, SymbolType **, uint64_t **);
 
 /* Recherche le symbole correspondant à une adresse. */
-bool resolve_elf_symbol(const elf_format *, char **, SymbolType *, uint64_t *);
+bool resolve_elf_symbol(const elf_format *, char **, SymbolType *, vmpa_t *);
 
 /* Fournit le prototype de toutes les routines détectées. */
 GBinRoutine **get_all_elf_routines(const elf_format *, size_t *);
diff --git a/src/format/elf/elf-int.h b/src/format/elf/elf-int.h
index b828a88..4339475 100644
--- a/src/format/elf/elf-int.h
+++ b/src/format/elf/elf-int.h
@@ -39,7 +39,7 @@ typedef struct _elf_string
 {
     const char *value;                      /* Valeur humainement lisible  */
     size_t len;                             /* Longueur de la chaîne       */
-    uint64_t vaddress;                      /* Adresse de localisation     */
+    vmpa_t address;                         /* Adresse de localisation     */
 
 } elf_string;
 
diff --git a/src/format/elf/strings.c b/src/format/elf/strings.c
index 3178d69..2bcd911 100644
--- a/src/format/elf/strings.c
+++ b/src/format/elf/strings.c
@@ -145,7 +145,7 @@ bool parse_elf_string_data(elf_format *format, const off_t start, const off_t si
 
             format->strings[format->str_count - 1].value = strndup((const char *)&EXE_FORMAT(format)->content[i], end - i);
             format->strings[format->str_count - 1].len = end - i;
-            format->strings[format->str_count - 1].vaddress = vaddress + i - start;
+            format->strings[format->str_count - 1].address = vaddress + i - start;
 
             i = end;
 
@@ -158,9 +158,9 @@ bool parse_elf_string_data(elf_format *format, const off_t start, const off_t si
 
 /******************************************************************************
 *                                                                             *
-*  Paramètres  : format   = informations chargées à consulter.                *
-*                label    = étiquette allouée du symbole si trouvé. [OUT]     *
-*                vaddress = adresse à cibler, puis décallage final. [OUT]     *
+*  Paramètres  : format  = informations chargées à consulter.                 *
+*                label   = étiquette allouée du symbole si trouvé. [OUT]      *
+*                vaddres = adresse à cibler, puis décallage final. [OUT]      *
 *                                                                             *
 *  Description : Recherche une chaîne correspondant à une adresse.            *
 *                                                                             *
@@ -170,7 +170,7 @@ bool parse_elf_string_data(elf_format *format, const off_t start, const off_t si
 *                                                                             *
 ******************************************************************************/
 
-bool resolve_elf_strings(const elf_format *format, char **label, uint64_t *vaddress)
+bool resolve_elf_strings(const elf_format *format, char **label, vmpa_t *address)
 {
     bool result;                            /* Bilan de recherche remonté  */
     size_t real_start;                      /* Début de chaîne effective   */
@@ -179,10 +179,10 @@ bool resolve_elf_strings(const elf_format *format, char **label, uint64_t *vaddr
     result = false;
 
     for (i = 0; i < format->str_count && !result; i++)
-        if (format->strings[i].vaddress <= *vaddress
-            && *vaddress < (format->strings[i].vaddress + format->strings[i].len))
+        if (format->strings[i].address <= *address
+            && *address < (format->strings[i].address + format->strings[i].len))
         {
-            real_start = *vaddress - format->strings[i].vaddress;
+            real_start = *address - format->strings[i].address;
             *label = strndup(&format->strings[i].value[real_start],
                              format->strings[i].len - real_start);
 
diff --git a/src/format/elf/strings.h b/src/format/elf/strings.h
index c636774..f9b17ac 100644
--- a/src/format/elf/strings.h
+++ b/src/format/elf/strings.h
@@ -33,7 +33,7 @@
 bool find_all_elf_strings(elf_format *);
 
 /* Recherche une chaîne correspondant à une adresse. */
-bool resolve_elf_strings(const elf_format *, char **, uint64_t *);
+bool resolve_elf_strings(const elf_format *, char **, vmpa_t *);
 
 
 
diff --git a/src/format/exe_format-int.h b/src/format/exe_format-int.h
index 781b1c3..1245d44 100644
--- a/src/format/exe_format-int.h
+++ b/src/format/exe_format-int.h
@@ -67,7 +67,7 @@ typedef size_t (* get_symbols_fc) (const exe_format *, char ***, SymbolType **,
 typedef size_t (* get_resolved_fc) (const exe_format *, char ***, ResolvedType **, uint64_t **);
 
 /* Recherche le symbole correspondant à une adresse. */
-typedef bool (* resolve_symbol_fc) (const exe_format *, char **, SymbolType *, uint64_t *);
+typedef bool (* resolve_symbol_fc) (const exe_format *, char **, SymbolType *, vmpa_t *);
 
 /* Fournit le prototype de toutes les routines détectées. */
 typedef GBinRoutine ** (* get_all_routines_fc) (const exe_format *, size_t *);
diff --git a/src/format/exe_format.c b/src/format/exe_format.c
index ffd9fde..cda17f7 100644
--- a/src/format/exe_format.c
+++ b/src/format/exe_format.c
@@ -464,10 +464,10 @@ size_t get_exe_resolved_items(const exe_format *format, char ***labels, Resolved
 
 /******************************************************************************
 *                                                                             *
-*  Paramètres  : format = informations chargées à consulter.                  *
-*                label  = étiquette du symbole si trouvé. [OUT]               *
-*                type   = type du symbole trouvé. [OUT]                       *
-*                offset = adresse à cibler, puis décallage final. [OUT]       *
+*  Paramètres  : format  = informations chargées à consulter.                 *
+*                label   = étiquette du symbole si trouvé. [OUT]              *
+*                type    = type du symbole trouvé. [OUT]                      *
+*                address = adresse à cibler, puis décallage final. [OUT]      *
 *                                                                             *
 *  Description : Recherche le symbole correspondant à une adresse.            *
 *                                                                             *
@@ -477,9 +477,9 @@ size_t get_exe_resolved_items(const exe_format *format, char ***labels, Resolved
 *                                                                             *
 ******************************************************************************/
 
-bool resolve_exe_symbol(const exe_format *format, char **label, SymbolType *type, uint64_t *offset)
+bool resolve_exe_symbol(const exe_format *format, char **label, SymbolType *type, vmpa_t *address)
 {
-    return format->resolve_symbol(format, label, type, offset);
+    return format->resolve_symbol(format, label, type, address);
 
 }
 
diff --git a/src/format/exe_format.h b/src/format/exe_format.h
index a6d91f2..32e35ff 100644
--- a/src/format/exe_format.h
+++ b/src/format/exe_format.h
@@ -145,7 +145,7 @@ size_t get_exe_symbols(const exe_format *, char ***, SymbolType **, uint64_t **)
 size_t get_exe_resolved_items(const exe_format *, char ***, ResolvedType **, uint64_t **);
 
 /* Recherche le symbole correspondant à une adresse. */
-bool resolve_exe_symbol(const exe_format *, char **, SymbolType *, uint64_t *);
+bool resolve_exe_symbol(const exe_format *, char **, SymbolType *, vmpa_t *);
 
 /* Fournit le prototype de toutes les routines détectées. */
 GBinRoutine **get_all_exe_routines(const exe_format *, size_t *);
-- 
cgit v0.11.2-87-g4458