From 680a2ea1523c79741461649b6528c083d2cec603 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Mon, 12 Sep 2016 23:21:53 +0200
Subject: Fixed two more bugs when annotating Dex code items.

---
 ChangeLog              |  9 +++++++++
 plugins/readdex/code.c | 31 +++++++++++--------------------
 src/arch/raw.c         | 39 +++++++++++++++++++++++++++++++++++++++
 src/arch/raw.h         |  3 +++
 4 files changed, 62 insertions(+), 20 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 8a08249..e9d72c2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+16-09-12  Cyrille Bagard <nocbos@gmail.com>
+
+	* plugins/readdex/code.c:
+	Fix two more bugs when annotating Dex code items.
+
+	* src/arch/raw.c:
+	* src/arch/raw.h:
+	Provide a way to build signed LEB128 operands.
+
 16-09-11  Cyrille Bagard <nocbos@gmail.com>
 
 	* plugins/readdex/class.c:
diff --git a/plugins/readdex/code.c b/plugins/readdex/code.c
index 24813ea..d13ce52 100644
--- a/plugins/readdex/code.c
+++ b/plugins/readdex/code.c
@@ -37,7 +37,7 @@
 
 
 /* Commente les définitions d'une protection contre exceptions. */
-static bool annotate_dex_try_item(const GDexFormat *, vmpa2t *, const vmpa2t *);
+static bool annotate_dex_try_item(const GDexFormat *, vmpa2t *);
 
 /*Commente les définitions des listes de gestion d'exceptions. */
 static bool annotate_dex_encoded_catch_handler_list(const GDexFormat *, vmpa2t *);
@@ -76,7 +76,6 @@ bool annotate_dex_code_item(const GDexFormat *format, uleb128_t offset)
     char *text;                             /* Texte constant à insérer    */
     uint16_t tries_size;                    /* Nombre de gestionnaires     */
     uint32_t insns_size;                    /* Nombre d'instructions       */
-    vmpa2t handlers;                        /* Base des gestionnaires      */
     uint16_t i;                             /* Boucle de parcours          */
 
     content = g_binary_format_get_content(G_BIN_FORMAT(format));
@@ -193,13 +192,13 @@ bool annotate_dex_code_item(const GDexFormat *format, uleb128_t offset)
 
     /* tries */
 
-    copy_vmpa(&handlers, &pos);
-    advance_vmpa(&handlers, tries_size * (4 + 2 + 2));
-
     result = true;
 
     for (i = 0; i < tries_size && result; i++)
-        result = annotate_dex_try_item(format, &pos, &handlers);
+        result = annotate_dex_try_item(format, &pos);
+
+    if (tries_size > 0 && result)
+        result = annotate_dex_encoded_catch_handler_list(format, &pos);
 
     /* Nettoyage final */
 
@@ -212,9 +211,8 @@ bool annotate_dex_code_item(const GDexFormat *format, uleb128_t offset)
 
 /******************************************************************************
 *                                                                             *
-*  Paramètres  : format   = description de l'exécutable à compléter.          *
-*                pos      = tête de lecture pour les symboles.                *
-*                handlers = position de départ pour les gestionnaires.        *
+*  Paramètres  : format = description de l'exécutable à compléter.            *
+*                pos    = tête de lecture pour les symboles.                  *
 *                                                                             *
 *  Description : Commente les définitions d'une protection contre exceptions. *
 *                                                                             *
@@ -224,7 +222,7 @@ bool annotate_dex_code_item(const GDexFormat *format, uleb128_t offset)
 *                                                                             *
 ******************************************************************************/
 
-static bool annotate_dex_try_item(const GDexFormat *format, vmpa2t *pos, const vmpa2t *handlers)
+static bool annotate_dex_try_item(const GDexFormat *format, vmpa2t *pos)
 {
     bool result;                            /* Bilan à retourner           */
     GBinContent *content;                   /* Contenu binaire à lire      */
@@ -235,8 +233,8 @@ static bool annotate_dex_try_item(const GDexFormat *format, vmpa2t *pos, const v
     GDbComment *comment;                    /* Définition de commentaire   */
     GBinSymbol *symbol;                     /* Symbole à intégrer          */
     char *text;                             /* Texte constant à insérer    */
-    uint16_t handler_off;                   /* Décallage à appliquer       */
-    vmpa2t handler;                         /* Lecture à poursuivre        */
+
+    result = true;
 
     content = g_binary_format_get_content(G_BIN_FORMAT(format));
 
@@ -283,13 +281,6 @@ static bool annotate_dex_try_item(const GDexFormat *format, vmpa2t *pos, const v
 
     free(text);
 
-    g_imm_operand_get_value(G_IMM_OPERAND(operand), MDS_16_BITS, &handler_off);
-
-    copy_vmpa(&handler, handlers);
-    advance_vmpa(&handler, handler_off);
-
-    result = annotate_dex_encoded_catch_handler_list(format, &handler);
-
     /* Nettoyage final */
 
     g_object_unref(G_OBJECT(content));
@@ -392,7 +383,7 @@ static bool annotate_dex_encoded_catch_handler(const GDexFormat *format, vmpa2t
     /* size */
 
     copy_vmpa(&start, pos);
-    instr = g_raw_instruction_new_uleb128(content, pos);
+    instr = g_raw_instruction_new_sleb128(content, pos);
 
     SET_IMM_DISPLAY(instr, operand, 0, IOD_DEC);
 
diff --git a/src/arch/raw.c b/src/arch/raw.c
index 8344c23..67d2507 100644
--- a/src/arch/raw.c
+++ b/src/arch/raw.c
@@ -291,6 +291,45 @@ GArchInstruction *g_raw_instruction_new_uleb128(const GBinContent *content, vmpa
 /******************************************************************************
 *                                                                             *
 *  Paramètres  : content = flux de données à analyser.                        *
+*                addr    = position courante dans ce flux. [OUT]              *
+*                                                                             *
+*  Description : Crée une instruction de type 'db/dw/etc' pour un sleb128.    *
+*                                                                             *
+*  Retour      : Instruction mise en place.                                   *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+GArchInstruction *g_raw_instruction_new_sleb128(const GBinContent *content, vmpa2t *addr)
+{
+    GArchInstruction *result;               /* Instruction à retourner     */
+    vmpa2t start;                           /* Départ original de lecture  */
+    leb128_t value;                         /* Valeur sleb128 à représenter*/
+    MemoryDataSize leb_size;                /* Taille de la valeur         */
+
+    copy_vmpa(&start, addr);
+
+    if (!g_binary_content_read_leb128(content, addr, &value))
+        goto grinu_error;
+
+    leb_size = MDS_FROM_BYTES(compute_vmpa_diff(&start, addr));
+    assert(leb_size != MDS_UNDEFINED);
+
+    result = g_raw_instruction_new_from_value(&start, leb_size, (uint64_t)value);
+
+    return result;
+
+ grinu_error:
+
+    return NULL;
+
+}
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : content = flux de données à analyser.                        *
 *                size    = taille de chacun des éléments à représenter.       *
 *                count   = nombre de ces éléments.                            *
 *                addr    = position courante dans ce flux. [OUT]              *
diff --git a/src/arch/raw.h b/src/arch/raw.h
index 18205ed..a2b19b3 100644
--- a/src/arch/raw.h
+++ b/src/arch/raw.h
@@ -58,6 +58,9 @@ GArchInstruction *g_raw_instruction_new_from_value(const vmpa2t *, MemoryDataSiz
 /* Crée une instruction de type 'db/dw/etc' pour un uleb128. */
 GArchInstruction *g_raw_instruction_new_uleb128(const GBinContent *content, vmpa2t *);
 
+/* Crée une instruction de type 'db/dw/etc' pour un sleb128. */
+GArchInstruction *g_raw_instruction_new_sleb128(const GBinContent *content, vmpa2t *);
+
 /* Crée une instruction de type 'db/dw/etc' étendue. */
 GArchInstruction *g_raw_instruction_new_array(const GBinContent *, MemoryDataSize, size_t, vmpa2t *, SourceEndian);
 
-- 
cgit v0.11.2-87-g4458