From 0f914ad3fdcc1ebac5789b55b9677e7868016e21 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Sun, 18 Nov 2018 19:46:08 +0100
Subject: Allowed to attach debug information to formats.

---
 plugins/dex/format.c          |  2 +-
 plugins/elf/format.c          |  2 +-
 plugins/pychrysalide/plugin.c |  6 +-----
 src/format/executable-int.h   |  2 +-
 src/format/executable.c       | 31 ++++++++++++++++++++++++++++---
 src/format/format.c           |  2 --
 src/plugins/pglist.h          |  3 +++
 src/plugins/plugin-def.h      |  4 ++--
 src/plugins/plugin-int.h      |  8 ++++----
 src/plugins/plugin.c          | 25 ++++++++++++-------------
 src/plugins/plugin.h          |  6 +++---
 11 files changed, 56 insertions(+), 35 deletions(-)

diff --git a/plugins/dex/format.c b/plugins/dex/format.c
index b657fe6..5ae0ec8 100644
--- a/plugins/dex/format.c
+++ b/plugins/dex/format.c
@@ -407,7 +407,7 @@ static bool g_dex_format_analyze(GDexFormat *format, wgroup_id_t gid, GtkStatusS
 
     g_executable_format_setup_portions(exe, status);
 
-    if (!g_executable_format_complete_loading(exe, status))
+    if (!g_executable_format_complete_loading(exe, gid, status))
         goto gdfa_error;
 
     result = true;
diff --git a/plugins/elf/format.c b/plugins/elf/format.c
index 340c2c7..d6bdc5e 100644
--- a/plugins/elf/format.c
+++ b/plugins/elf/format.c
@@ -403,7 +403,7 @@ static bool g_elf_format_analyze(GElfFormat *format, wgroup_id_t gid, GtkStatusS
     if (!find_all_elf_strings(format, gid, status))
         goto gefa_error;
 
-    if (!g_executable_format_complete_loading(exe, status))
+    if (!g_executable_format_complete_loading(exe, gid, status))
         goto gefa_error;
 
     result = true;
diff --git a/plugins/pychrysalide/plugin.c b/plugins/pychrysalide/plugin.c
index 34a0129..fb2044d 100644
--- a/plugins/pychrysalide/plugin.c
+++ b/plugins/pychrysalide/plugin.c
@@ -408,10 +408,6 @@ GPluginModule *g_python_plugin_new(const char *modname, const char *filename)
 
                         switch (action)
                         {
-                            case PGA_FORMAT_LOADER_LAST:
-                                /* TODO */
-                                break;
-
                             default:
                                 log_variadic_message(LMT_WARNING,
                                                      _("Unknown action '0x%02x' in plugin '%s'..."),
@@ -757,7 +753,7 @@ static bool py_plugin_module_define_constants(PyTypeObject *obj_type)
 
     result &= PyDict_AddIntMacro(obj_type, PGA_FORMAT_ANALYSIS_STARTED);
     result &= PyDict_AddIntMacro(obj_type, PGA_FORMAT_PRELOAD);
-    result &= PyDict_AddIntMacro(obj_type, PGA_FORMAT_LOADER_LAST);
+    result &= PyDict_AddIntMacro(obj_type, PGA_FORMAT_ATTACH_DEBUG);
     result &= PyDict_AddIntMacro(obj_type, PGA_FORMAT_ANALYSIS_ENDED);
     result &= PyDict_AddIntMacro(obj_type, PGA_FORMAT_POST_ANALYSIS_STARTED);
     result &= PyDict_AddIntMacro(obj_type, PGA_FORMAT_POST_ANALYSIS_ENDED);
diff --git a/src/format/executable-int.h b/src/format/executable-int.h
index 58eba25..2f3ea73 100644
--- a/src/format/executable-int.h
+++ b/src/format/executable-int.h
@@ -86,7 +86,7 @@ struct _GExeFormatClass
 void g_executable_format_setup_portions(GExeFormat *, GtkStatusStack *);
 
 /* Effectue les ultimes opérations de chargement d'un binaire. */
-bool g_executable_format_complete_loading(GExeFormat *, GtkStatusStack *);
+bool g_executable_format_complete_loading(GExeFormat *, wgroup_id_t, GtkStatusStack *);
 
 /* Fournit l'emplacement correspondant à une position physique. */
 bool g_exe_format_without_virt_translate_offset_into_vmpa(const GExeFormat *, phys_t, vmpa2t *);
diff --git a/src/format/executable.c b/src/format/executable.c
index e70840b..fcc0acd 100644
--- a/src/format/executable.c
+++ b/src/format/executable.c
@@ -33,6 +33,7 @@
 #include "executable-int.h"
 #include "format.h"
 #include "../core/logs.h"
+#include "../plugins/pglist.h"
 
 
 
@@ -326,6 +327,7 @@ void g_executable_format_setup_portions(GExeFormat *format, GtkStatusStack *stat
 /******************************************************************************
 *                                                                             *
 *  Paramètres  : format = instance à traiter.                                 *
+*                gid    = groupe de travail dédié.                            *
 *                status = barre de statut à tenir informée.                   *
 *                                                                             *
 *  Description : Effectue les ultimes opérations de chargement d'un binaire.  *
@@ -336,14 +338,37 @@ void g_executable_format_setup_portions(GExeFormat *format, GtkStatusStack *stat
 *                                                                             *
 ******************************************************************************/
 
-bool g_executable_format_complete_loading(GExeFormat *format, GtkStatusStack *status)
+bool g_executable_format_complete_loading(GExeFormat *format, wgroup_id_t gid, GtkStatusStack *status)
 {
     bool result;                            /* Bilan à faire remonter      */
+    size_t count;                           /* Qté d'infos supplémentaires */
+    size_t i;                               /* Boucle de parcours          */
+    GDbgFormat *dbg;                        /* Informations de débogage    */
     GBinFormat *base;                       /* Version basique du format   */
 
-    base = G_BIN_FORMAT(format);
+    result = true;
+
+    attach_debug_format(format);
+
+    count = g_exe_format_count_debug_info(format);
+
+    for (i = 0; i < count && result; i++)
+    {
+        dbg = g_exe_format_get_debug_info(format, i);
+
+        result = g_binary_format_analyze(G_BIN_FORMAT(dbg), gid, status);
+
+        g_object_unref(G_OBJECT(dbg));
 
-    result = g_binary_format_complete_loading(base, status);
+    }
+
+    if (result)
+    {
+        base = G_BIN_FORMAT(format);
+
+        result = g_binary_format_complete_loading(base, status);
+
+    }
 
     return result;
 
diff --git a/src/format/format.c b/src/format/format.c
index de57e4c..05e4597 100644
--- a/src/format/format.c
+++ b/src/format/format.c
@@ -203,8 +203,6 @@ static void g_binary_format_finalize(GBinFormat *format)
 
 bool g_binary_format_complete_loading(GBinFormat *format, GtkStatusStack *status)
 {
-    handle_binary_format(PGA_FORMAT_LOADER_LAST, format, status);
-
     g_binary_format_delete_duplicated_symbols(format);
 
     return true;
diff --git a/src/plugins/pglist.h b/src/plugins/pglist.h
index 31c9c29..0c8f6af 100644
--- a/src/plugins/pglist.h
+++ b/src/plugins/pglist.h
@@ -109,6 +109,9 @@ GPluginModule **get_all_plugins_for_action(PluginAction, size_t *);
 #define preload_binary_format(a, f, i, s) \
     process_all_plugins_for(a, g_plugin_module_preload_binary_format, f, i, s)
 
+#define attach_debug_format(f) \
+    process_all_plugins_for(PGA_FORMAT_ATTACH_DEBUG, g_plugin_module_attach_debug_format, f)
+
 /* DPS_DISASSEMBLY */
 
 #define process_disassembly_event(a, b, s, c) \
diff --git a/src/plugins/plugin-def.h b/src/plugins/plugin-def.h
index fd05b00..96b04b1 100644
--- a/src/plugins/plugin-def.h
+++ b/src/plugins/plugin-def.h
@@ -147,7 +147,7 @@ typedef enum _PluginAction
     PGA_FORMAT_PRELOAD               = DPC_BINARY_PROCESSING | DPS_FORMAT | DEFINE_PLUGIN_ACTION(1),
 
     /* Accompagnement du chargement */
-    PGA_FORMAT_LOADER_LAST           = DPC_BINARY_PROCESSING | DPS_FORMAT | DEFINE_PLUGIN_ACTION(2),
+    PGA_FORMAT_ATTACH_DEBUG          = DPC_BINARY_PROCESSING | DPS_FORMAT | DEFINE_PLUGIN_ACTION(2),
 
     /* Fin de l'analyse d'un format */
     PGA_FORMAT_ANALYSIS_ENDED        = DPC_BINARY_PROCESSING | DPS_FORMAT | DEFINE_PLUGIN_ACTION(3),
@@ -155,7 +155,7 @@ typedef enum _PluginAction
     /* Début de la vague finale d'analyse d'un format */
     PGA_FORMAT_POST_ANALYSIS_STARTED = DPC_BINARY_PROCESSING | DPS_FORMAT | DEFINE_PLUGIN_ACTION(4),
 
-    /* Début de la vague finale d'analyse d'un format */
+    /* Fin de la vague finale d'analyse d'un format */
     PGA_FORMAT_POST_ANALYSIS_ENDED   = DPC_BINARY_PROCESSING | DPS_FORMAT | DEFINE_PLUGIN_ACTION(5),
 
     /**
diff --git a/src/plugins/plugin-int.h b/src/plugins/plugin-int.h
index cee6242..8df3527 100644
--- a/src/plugins/plugin-int.h
+++ b/src/plugins/plugin-int.h
@@ -53,12 +53,12 @@ typedef void (* pg_include_theme_fc) (const GPluginModule *, PluginAction, char
 /* Assure l'interprétation d'un format en différé. */
 typedef bool (* pg_handle_format_analysis_fc) (const GPluginModule *, PluginAction, GBinFormat *, wgroup_id_t, GtkStatusStack *);
 
-/* Procède à une opération liée au format de fichier uniquement. */
-typedef bool (* pg_handle_format_fc) (const GPluginModule *, PluginAction, GBinFormat *, GtkStatusStack *);
-
 /* Procède à un préchargement de format de fichier. */
 typedef bool (* pg_preload_format_fc) (const GPluginModule *, PluginAction, GBinFormat *, GPreloadInfo *, GtkStatusStack *);
 
+/* Procède au rattachement d'éventuelles infos de débogage. */
+typedef void (* pg_attach_debug) (const GPluginModule *, PluginAction, GExeFormat *);
+
 /* Exécute une action pendant un désassemblage de binaire. */
 typedef void (* pg_process_disassembly_fc) (const GPluginModule *, PluginAction, GLoadedBinary *, GtkStatusStack *, GProcContext *);
 
@@ -89,8 +89,8 @@ struct _GPluginModule
     pg_handle_loaded_fc handle_loaded;      /* Traitement de contenu chargé*/
 
     pg_handle_format_analysis_fc handle_fmt_analysis; /* Analyse de format */
-    pg_handle_format_fc handle_format;      /* Manipulation du format      */
     pg_preload_format_fc preload_format;    /* Préchargement d'un format   */
+    pg_attach_debug attach_debug;           /* Informations de débogage    */
 
     pg_process_disassembly_fc process_disass; /* Catégorie 'désassemblage' */
 
diff --git a/src/plugins/plugin.c b/src/plugins/plugin.c
index 161d6a6..1608032 100644
--- a/src/plugins/plugin.c
+++ b/src/plugins/plugin.c
@@ -377,15 +377,15 @@ GPluginModule *g_plugin_module_new(const gchar *filename)
                                     goto bad_plugin;
                                 break;
 
-                            case PGA_FORMAT_LOADER_LAST:
+                            case PGA_FORMAT_PRELOAD:
                                 if (!load_plugin_symbol(result->module,
-                                                        "handle_binary_format", &result->handle_format))
+                                                        "preload_binary_format", &result->preload_format))
                                     goto bad_plugin;
                                 break;
 
-                            case PGA_FORMAT_PRELOAD:
+                            case PGA_FORMAT_ATTACH_DEBUG:
                                 if (!load_plugin_symbol(result->module,
-                                                        "preload_binary_format", &result->preload_format))
+                                                        "chrysalide_plugin_attach_debug", &result->attach_debug))
                                     goto bad_plugin;
                                 break;
 
@@ -869,9 +869,10 @@ bool g_plugin_module_handle_binary_format_analysis(const GPluginModule *plugin,
 *  Paramètres  : plugin = greffon à manipuler.                                *
 *                action = type d'action attendue.                             *
 *                format = format de binaire à manipuler pendant l'opération.  *
+*                info   = informations à constituer en avance de phase.       *
 *                status = barre de statut à tenir informée.                   *
 *                                                                             *
-*  Description : Procède à une opération liée au format de fichier uniquement.*
+*  Description : Procède à un préchargement de format de fichier.             *
 *                                                                             *
 *  Retour      : Bilan de l'exécution du traitement.                          *
 *                                                                             *
@@ -879,9 +880,9 @@ bool g_plugin_module_handle_binary_format_analysis(const GPluginModule *plugin,
 *                                                                             *
 ******************************************************************************/
 
-bool g_plugin_module_handle_binary_format(const GPluginModule *plugin, PluginAction action, GBinFormat *format, GtkStatusStack *status)
+bool g_plugin_module_preload_binary_format(const GPluginModule *plugin, PluginAction action, GBinFormat *format, GPreloadInfo *info, GtkStatusStack *status)
 {
-    return plugin->handle_format(plugin, action, format, status);
+    return plugin->preload_format(plugin, action, format, info, status);
 
 }
 
@@ -891,20 +892,18 @@ bool g_plugin_module_handle_binary_format(const GPluginModule *plugin, PluginAct
 *  Paramètres  : plugin = greffon à manipuler.                                *
 *                action = type d'action attendue.                             *
 *                format = format de binaire à manipuler pendant l'opération.  *
-*                info   = informations à constituer en avance de phase.       *
-*                status = barre de statut à tenir informée.                   *
 *                                                                             *
-*  Description : Procède à un préchargement de format de fichier.             *
+*  Description : Procède au rattachement d'éventuelles infos de débogage.     *
 *                                                                             *
-*  Retour      : Bilan de l'exécution du traitement.                          *
+*  Retour      : -                                                            *
 *                                                                             *
 *  Remarques   : -                                                            *
 *                                                                             *
 ******************************************************************************/
 
-bool g_plugin_module_preload_binary_format(const GPluginModule *plugin, PluginAction action, GBinFormat *format, GPreloadInfo *info, GtkStatusStack *status)
+void g_plugin_module_attach_debug_format(const GPluginModule *plugin, PluginAction action, GExeFormat *format)
 {
-    return plugin->preload_format(plugin, action, format, info, status);
+    plugin->attach_debug(plugin, action, format);
 
 }
 
diff --git a/src/plugins/plugin.h b/src/plugins/plugin.h
index 1127604..86dba25 100644
--- a/src/plugins/plugin.h
+++ b/src/plugins/plugin.h
@@ -103,12 +103,12 @@ void g_plugin_module_handle_loaded_content(const GPluginModule *, PluginAction,
 /* Procède à une opération liée à l'analyse d'un format. */
 bool g_plugin_module_handle_binary_format_analysis(const GPluginModule *, PluginAction, GBinFormat *, wgroup_id_t, GtkStatusStack *);
 
-/* Procède à une opération liée au format de fichier uniquement. */
-bool g_plugin_module_handle_binary_format(const GPluginModule *, PluginAction, GBinFormat *, GtkStatusStack *);
-
 /* Procède à un préchargement de format de fichier. */
 bool g_plugin_module_preload_binary_format(const GPluginModule *, PluginAction, GBinFormat *, GPreloadInfo *, GtkStatusStack *);
 
+/* Procède au rattachement d'éventuelles infos de débogage. */
+void g_plugin_module_attach_debug_format(const GPluginModule *, PluginAction, GExeFormat *);
+
 /* Exécute une action pendant un désassemblage de binaire. */
 void g_plugin_module_process_disassembly_event(const GPluginModule *, PluginAction, GLoadedBinary *, GtkStatusStack *, GProcContext *);
 
-- 
cgit v0.11.2-87-g4458