From c930f8b060034abefbae48ad76a9b8d1e55b1ec9 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Sat, 19 May 2018 13:27:16 +0200
Subject: Tracked deeper the reference counts for ARMv7 registers.

---
 plugins/arm/v7/fetch.c             |  4 +++-
 plugins/arm/v7/link.c              |  2 ++
 plugins/arm/v7/operands/register.c | 23 -----------------------
 plugins/arm/v7/operands/register.h |  3 ---
 plugins/arm/v7/operands/reglist.c  |  8 +++++++-
 5 files changed, 12 insertions(+), 28 deletions(-)

diff --git a/plugins/arm/v7/fetch.c b/plugins/arm/v7/fetch.c
index bf60d9c..09278c9 100644
--- a/plugins/arm/v7/fetch.c
+++ b/plugins/arm/v7/fetch.c
@@ -606,7 +606,7 @@ void apply_write_back_from_registers(GArchInstruction *instr, GArchProcessor *pr
 
     regop = G_ARMV7_REGISTER_OPERAND(op);
 
-    reg_ref = G_ARCH_REGISTER(g_armv7_register_operand_get(regop));
+    reg_ref = g_register_operand_get_register(G_REGISTER_OPERAND(regop));
 
     op = _g_arch_instruction_get_operand(instr, 1);
     assert(G_IS_ARMV7_REGLIST_OPERAND(op));
@@ -623,6 +623,8 @@ void apply_write_back_from_registers(GArchInstruction *instr, GArchProcessor *pr
 
         inside = (g_arch_register_compare(reg_ref, G_ARCH_REGISTER(reg)) == 0);
 
+        g_object_unref(G_OBJECT(reg));
+
     }
 
     if (!inside)
diff --git a/plugins/arm/v7/link.c b/plugins/arm/v7/link.c
index 441fa50..67f4226 100644
--- a/plugins/arm/v7/link.c
+++ b/plugins/arm/v7/link.c
@@ -107,6 +107,8 @@ void handle_armv7_return_from_pop(GArchInstruction *instr, GArchProcessor *proc,
         if (g_arm_register_get_index(reg) == 15 /* pc */)
             g_arch_instruction_set_flag(instr, AIF_RETURN_POINT);
 
+        g_object_unref(G_OBJECT(reg));
+
     }
 
     g_object_unref(G_OBJECT(op));
diff --git a/plugins/arm/v7/operands/register.c b/plugins/arm/v7/operands/register.c
index 412d0f9..e6b3751 100644
--- a/plugins/arm/v7/operands/register.c
+++ b/plugins/arm/v7/operands/register.c
@@ -226,29 +226,6 @@ GArchOperand *g_armv7_register_operand_new(GArmV7Register *reg)
 /******************************************************************************
 *                                                                             *
 *  Paramètres  : operand = opérande représentant un registre.                 *
-*                                                                             *
-*  Description : Fournit le registre ARMv7 associé à l'opérande.              *
-*                                                                             *
-*  Retour      : Représentation interne du registre.                          *
-*                                                                             *
-*  Remarques   : -                                                            *
-*                                                                             *
-******************************************************************************/
-
-const GArmV7Register *g_armv7_register_operand_get(const GArmV7RegisterOperand *operand)
-{
-    GArmV7Register *result;                 /* Instance à retourner        */
-
-    result = G_ARMV7_REGISTER(G_REGISTER_OPERAND(operand)->reg);
-
-    return result;
-
-}
-
-
-/******************************************************************************
-*                                                                             *
-*  Paramètres  : operand = opérande représentant un registre.                 *
 *                wback   = indique si le registre est mis à jour après coup.  *
 *                                                                             *
 *  Description : Détermine si le registre est mis à jour après l'opération.   *
diff --git a/plugins/arm/v7/operands/register.h b/plugins/arm/v7/operands/register.h
index 2bd13c6..4a9c0c3 100644
--- a/plugins/arm/v7/operands/register.h
+++ b/plugins/arm/v7/operands/register.h
@@ -57,9 +57,6 @@ GType g_armv7_register_operand_get_type(void);
 /* Crée un opérande visant un registre ARMv7. */
 GArchOperand *g_armv7_register_operand_new(GArmV7Register *);
 
-/* Fournit le registre ARMv7 associé à l'opérande. */
-const GArmV7Register *g_armv7_register_operand_get(const GArmV7RegisterOperand *);
-
 /* Détermine si le registre est mis à jour après l'opération. */
 void g_armv7_register_operand_write_back(GArmV7RegisterOperand *, bool);
 
diff --git a/plugins/arm/v7/operands/reglist.c b/plugins/arm/v7/operands/reglist.c
index 005aff7..33a3fcd 100644
--- a/plugins/arm/v7/operands/reglist.c
+++ b/plugins/arm/v7/operands/reglist.c
@@ -364,9 +364,15 @@ size_t g_armv7_reglist_count_registers(const GArmV7RegListOperand *operand)
 
 GArmV7Register *g_armv7_reglist_operand_get_register(const GArmV7RegListOperand *operand, size_t index)
 {
+    GArmV7Register *result;                 /* Instance à retourner        */
+
     assert(index < operand->count);
 
-    return operand->registers[index];
+    result = operand->registers[index];
+
+    g_object_ref(G_OBJECT(result));
+
+    return result;
 
 }
 
-- 
cgit v0.11.2-87-g4458