diff options
| -rw-r--r-- | ChangeLog | 43 | ||||
| -rw-r--r-- | configure.ac | 2 | ||||
| -rwxr-xr-x | src/analysis/Makefile.am | 1 | ||||
| -rw-r--r-- | src/analysis/binary.c | 385 | ||||
| -rw-r--r-- | src/analysis/binary.h | 3 | ||||
| -rw-r--r-- | src/analysis/delayed.c | 786 | ||||
| -rw-r--r-- | src/analysis/delayed.h | 74 | ||||
| -rw-r--r-- | src/common/dllist.h | 2 | ||||
| -rw-r--r-- | src/editor.c | 19 | ||||
| -rw-r--r-- | src/format/exe_format.c | 6 | ||||
| -rw-r--r-- | src/gtkext/Makefile.am | 1 | ||||
| -rw-r--r-- | src/gtkext/gtkblockview.c | 2 | ||||
| -rw-r--r-- | src/gtkext/gtkextstatusbar.c | 228 | ||||
| -rw-r--r-- | src/gtkext/gtkextstatusbar.h | 88 | ||||
| -rw-r--r-- | src/main.c | 9 | ||||
| -rw-r--r-- | src/project.c | 78 | ||||
| -rw-r--r-- | src/project.h | 4 | 
17 files changed, 1389 insertions, 342 deletions
| @@ -1,3 +1,46 @@ +09-08-01  Cyrille Bagard <nocbos@gmail.com> + +	* configure.ac: +	Add "VOID:OBJECT,OBJECT" to iodamarshal.list. + +	* src/analysis/binary.c: +	* src/analysis/binary.h: +	Remove code and use the new functions from delayed.[ch]. +	Provide and use signals to catch disassembly end. + +	* src/analysis/delayed.c: +	* src/analysis/delayed.h: +	New entries: process disassembling in a dedicated thread. + +	* src/analysis/Makefile.am: +	Add delayed.[ch] to libanalysis_la_SOURCES. + +	* src/common/dllist.h: +	Fix a bug in dl_list_del(). + +	* src/editor.c: +	Update the code ; use the new status bar. + +	* src/format/exe_format.c: +	Handle NULL parameters in get_bin_part_values(). + +	* src/gtkext/gtkblockview.c: +	Reduce the number of lines drawn. + +	* src/gtkext/gtkextstatusbar.c: +	* src/gtkext/gtkextstatusbar.h: +	New entries: provide a status bar with a progress bar. + +	* src/gtkext/Makefile.am: +	Add gtkextstatusbar.[ch] to libgtkext_la_SOURCES. + +	* src/main.c: +	Load the delayed functions manager and update calls for creating projects. + +	* src/project.c: +	* src/project.h: +	Display a binary only when its disassembly is completed. +  09-07-27  Cyrille Bagard <nocbos@gmail.com>  	* plugins/stackvars/stackvars.c: diff --git a/configure.ac b/configure.ac index 88b680d..2570718 100644 --- a/configure.ac +++ b/configure.ac @@ -225,7 +225,7 @@ AC_SUBST(LIBGRAPH_LIBS)  AC_CONFIG_FILES([stamp-h po/Makefile.in], [echo timestamp > stamp-h]) -AC_CONFIG_COMMANDS([marshal], [echo "VOID:UINT64,UINT64" > src/gtkext/iodamarshal.list]) +AC_CONFIG_COMMANDS([marshal], [echo -e "VOID:UINT64,UINT64\nVOID:OBJECT,OBJECT" > src/gtkext/iodamarshal.list])  AC_CONFIG_FILES([Makefile                   pixmaps/Makefile diff --git a/src/analysis/Makefile.am b/src/analysis/Makefile.am index 5f6beed..2a456ff 100755 --- a/src/analysis/Makefile.am +++ b/src/analysis/Makefile.am @@ -3,6 +3,7 @@ noinst_LTLIBRARIES  = libanalysis.la  libanalysis_la_SOURCES =				\  	binary.h binary.c					\ +	delayed.h delayed.c					\  	line.h line.c						\  	line-int.h							\  	line_code.h line_code.c				\ diff --git a/src/analysis/binary.c b/src/analysis/binary.c index c0c6e7c..9351c48 100644 --- a/src/analysis/binary.c +++ b/src/analysis/binary.c @@ -35,8 +35,9 @@  #include <sys/types.h> -#include "line_code.h" -#include "line_comment.h" +#include "delayed.h" +#include "line_code.h"          /* TODO : supprimer ? */ +#include "line_comment.h"       /* TODO : supprimer ? */  #include "line_prologue.h"  #include "prototype.h"  #include "../common/extstr.h" @@ -79,6 +80,10 @@ struct _GOpenidaBinaryClass  {      GObjectClass parent;                    /* A laisser en premier        */ +    /* Signaux */ + +    void (* disassembly_done) (GOpenidaBinary *); +  }; @@ -94,17 +99,8 @@ bin_t *map_binary_file(const char *, off_t *);  /* Construit la description d'introduction du désassemblage. */  GRenderingLine *build_binary_prologue(const char *, const uint8_t *, off_t); -/* Procède au désassemblage basique d'un contenu binaire. */ -void disassemble_openida_binary(GOpenidaBinary *); - -/* Etablit les liens entres les différentes lignes de code. */ -void establish_links_in_openida_binary(const GOpenidaBinary *); - -/* S'assure que toutes les routines ont une taille définie. */ -void limit_all_routines_in_openida_binary(const GOpenidaBinary *); - -/* Cherche l'adresse de fin d'une routine. */ -vmpa_t find_best_ending_address_for_routine(GRenderingLine *, size_t, const vmpa_t *, const off_t *, size_t); +/* Acquitte la fin d'un désasemblage différé et complet. */ +void ack_completed_disassembly(GDelayedManager *, GOpenidaBinary *, GRenderingLine *, GOpenidaBinary *); @@ -126,6 +122,13 @@ G_DEFINE_TYPE(GOpenidaBinary, g_openida_binary, G_TYPE_OBJECT);  static void g_openida_binary_class_init(GOpenidaBinaryClass *klass)  { +    g_signal_new("disassembly-done", +                 G_TYPE_OPENIDA_BINARY, +                 G_SIGNAL_RUN_LAST, +                 G_STRUCT_OFFSET(GOpenidaBinaryClass, disassembly_done), +                 NULL, NULL, +                 g_cclosure_marshal_VOID__VOID, +                 G_TYPE_NONE, 0);  } @@ -201,8 +204,6 @@ GOpenidaBinary *g_openida_binary_new_from_file(const char *filename)      g_rendering_options_show_address(result->options, MRD_GRAPH, true);      g_rendering_options_show_code(result->options, MRD_GRAPH, false); -    disassemble_openida_binary(result); -      return result;   lbf_error: @@ -298,6 +299,43 @@ bool g_openida_binary_save(const GOpenidaBinary *binary, xmlDocPtr xdoc, xmlXPat  /******************************************************************************  *                                                                             * +*  Paramètres  : binary = élément binaire à traiter.                          * +*                                                                             * +*  Description : Lance l'analyse d'un élément binaire chargé.                 * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +void g_openida_binary_analyse(GOpenidaBinary *binary) +{ +    GDelayedManager *manager;               /* Gestionnaire de différés    */ +    bin_part **parts;                       /* Parties d'élément binaire   */ +    size_t parts_count;                     /* Nombre de ces parties       */ + +    manager = get_delayed_manager(); + +    parts = /* !!! */get_elf_default_code_parts(binary->format, &parts_count); +    qsort(parts, parts_count, sizeof(bin_part *), compare_bin_parts); + + + +    g_signal_connect(manager, "disassembly-completed", +                     G_CALLBACK(ack_completed_disassembly), binary); + +    g_delayed_manager_schedule_disassembly(manager, binary, parts, parts_count); + + + + + +} + + +/****************************************************************************** +*                                                                             *  *  Paramètres  : binary = élément binaire à consulter.                        *  *                                                                             *  *  Description : Fournit une description humaine d'un élément binaire.        * @@ -349,7 +387,8 @@ const char *g_openida_binary_get_filename(const GOpenidaBinary *binary)  bin_t *g_openida_binary_get_data(const GOpenidaBinary *binary, off_t *length)  { -    *length = binary->bin_length; +    if (length != NULL) +        *length = binary->bin_length;      return binary->bin_data; @@ -553,9 +592,12 @@ GRenderingLine *build_binary_prologue(const char *filename, const uint8_t *data,  /******************************************************************************  *                                                                             * -*  Paramètres  : binary = binaire dont le contenu est à analyser.             * +*  Paramètres  : manager = gestionnaire des traitements en parallèle.         * +*                binary  = binaire dont le contenu est à analyser.            * +*                lines   = lignes de rendu produites par le désasemblage.     * +*                user    = élément binaire à l'origine du traitement.         *  *                                                                             * -*  Description : Procède au désassemblage basique d'un contenu binaire.       * +*  Description : Acquitte la fin d'un désasemblage différé et complet.        *  *                                                                             *  *  Retour      : -                                                            *  *                                                                             * @@ -563,99 +605,25 @@ GRenderingLine *build_binary_prologue(const char *filename, const uint8_t *data,  *                                                                             *  ******************************************************************************/ -void disassemble_openida_binary(GOpenidaBinary *binary) +void ack_completed_disassembly(GDelayedManager *manager, GOpenidaBinary *binary, GRenderingLine *lines, GOpenidaBinary *user)  { - - -    GArchInstruction *instr; - -    GBinRoutine **routines;                 /* Liste des routines trouvées */ -    size_t routines_count;                  /* Nombre de ces routines      */ - -    bin_part **parts; -    size_t parts_count; - - - -    GRenderingLine *line; - - -    off_t start; -    off_t pos; -    off_t len; - -    uint64_t base = 0; -    vmpa_t addr = 0; - -    size_t i; - -    size_t k; - -    uint64_t routine_offset;                /* Point de départ de routine  */ -    char *routine_desc;                     /* Prototype d'une routine     */ - +    GRenderingLine *line;                   /* "Première" ligne de rendu   */      GPluginModule **pglist;                 /* Liste de greffons           */      size_t pgcount;                         /* Taille de cette liste       */ +    size_t i;                               /* Boucle de parcours          */ +    /* Si ce n'est pas pour nous... */ +    if (binary != user) return; -    binary->lines = build_binary_prologue(binary->filename, binary->bin_data, binary->bin_length); - - -    routines = get_all_exe_routines(binary->format, &routines_count); - - - - -    parts = /* !!! */get_elf_default_code_parts(binary->format, &parts_count); -    qsort(parts, parts_count, sizeof(bin_part *), compare_bin_parts); - -    printf("PARTS COUNT :: %d\n", parts_count); - -    for (i = 0; i < parts_count; i++) -    { -        get_bin_part_values(parts[i], &pos, &len, &base); - -        /* Décodage des instructions */ - -        start = pos; -        pos = 0; - -        while (pos < len) -        { -            addr = base + pos; - - -            instr = g_arch_processor_decode_instruction(binary->proc, &binary->bin_data[start], &pos, len, start, addr); - -            line = g_code_line_new(addr, instr, binary->options); -            g_rendering_line_add_to_lines(&binary->lines, line); - -        } - -        /* Ajout des prototypes de fonctions */ - -        for (k = 0; k < routines_count; k++) -        { -            routine_offset = g_binary_routine_get_address(routines[k]); - -            if (!(base <= routine_offset && routine_offset < (base + len))) continue; - -            routine_desc = g_binary_routine_to_string(routines[k]); +    binary->lines = lines; -            line = g_comment_line_new(routine_offset, routine_desc, binary->options); -            g_rendering_line_insert_into_lines(&binary->lines, line, true); -            free(routine_desc); -        } -    } -    establish_links_in_openida_binary(binary); -    limit_all_routines_in_openida_binary(binary); -    line = g_rendering_line_find_by_address(binary->lines, NULL, get_exe_entry_point(binary->format)); +    line = g_rendering_line_find_by_address(lines, NULL, get_exe_entry_point(binary->format));      if (line != NULL) g_rendering_line_add_flag(line, RLF_ENTRY_POINT);      /* Action post-désassemblage */ @@ -671,223 +639,6 @@ void disassemble_openida_binary(GOpenidaBinary *binary)      } -} - - -/****************************************************************************** -*                                                                             * -*  Paramètres  : binary = binaire dont le contenu est à lier.                 * -*                                                                             * -*  Description : Etablit les liens entres les différentes lignes de code.     * -*                                                                             * -*  Retour      : -                                                            * -*                                                                             * -*  Remarques   : -                                                            * -*                                                                             * -******************************************************************************/ - -void establish_links_in_openida_binary(const GOpenidaBinary *binary) -{ -    GBinRoutine **routines;                 /* Liste des routines trouvées */ -    size_t routines_count;                  /* Nombre de ces routines      */ -    size_t i;                               /* Boucle de parcours          */ -    vmpa_t start;                           /* Adresse de départ           */ -    vmpa_t end;                             /* Adresse de fin              */ -    GRenderingLine *iter;                   /* Boucle de parcours          */ -    GArchInstruction *instr;                /* Instruction à ausculter     */ -    vmpa_t addr;                            /* Adresse référencée          */ -    InstructionLinkType type;               /* Type de référence           */ -    GRenderingLine *target;                 /* Ligne visée par la référence*/ - -    routines = get_all_exe_routines(binary->format, &routines_count); - -    for (i = 0; i < routines_count; i++) -    { -        start = g_binary_routine_get_address(routines[i]); -        end = start + g_binary_routine_get_size(routines[i]); - -        for (iter = g_rendering_line_find_by_address(binary->lines, NULL, start); -             iter != NULL; -             iter = g_rendering_line_get_next_iter(binary->lines, iter, NULL)) -        { -            /* Si on sort de la zone... */ -            if (get_rendering_line_address(iter) >= end) break; - -            /* On ne traite que du code ici ! */ -            if (!G_IS_CODE_LINE(iter)) continue; - -            instr = g_code_line_get_instruction(G_CODE_LINE(iter)); -            type = g_arch_instruction_get_link(instr, &addr); - -            switch (type) -            { -                case ILT_NONE: -                    break; - -                case ILT_JUMP: - -                    target = g_rendering_line_find_by_address(binary->lines, NULL, addr); - -                    if (target != NULL) -                        g_rendering_line_link_with(iter, target, type); - -                    break; - -                case ILT_JUMP_IF_FALSE: -                    break; - -                case ILT_JUMP_IF_TRUE: - -                    target = g_rendering_line_find_by_address(binary->lines, NULL, addr); - -                    if (target != NULL) -                    { -                        g_rendering_line_link_with(iter, target, type); - -                        target = g_rendering_line_get_next_iter(binary->lines, iter, NULL); -                        if (target != NULL) -                            g_rendering_line_link_with(iter, target, ILT_JUMP_IF_FALSE); - -                    } - -                    break; - -                case ILT_CALL: - -                    target = g_rendering_line_find_by_address(binary->lines, NULL, addr); - -                    if (target != NULL) -                        g_rendering_line_link_with(iter, target, type); - -                    break; - -            } - -        } - -    } - -} - - -/****************************************************************************** -*                                                                             * -*  Paramètres  : binary = binaire dont le contenu est à lier.                 * -*                                                                             * -*  Description : S'assure que toutes les routines ont une taille définie.     * -*                                                                             * -*  Retour      : -                                                            * -*                                                                             * -*  Remarques   : -                                                            * -*                                                                             * -******************************************************************************/ - -void limit_all_routines_in_openida_binary(const GOpenidaBinary *binary) -{ -    GBinRoutine **routines;                 /* Liste des routines trouvées */ -    size_t routines_count;                  /* Nombre de ces routines      */ -    size_t i;                               /* Boucle de parcours          */ -    vmpa_t *starts;                         /* Adresses de départ          */ -    off_t *lengths;                         /* Tailles des routines        */ -    GRenderingLine *line;                   /* Ligne de départ / d'arrivée */ -    vmpa_t start;                           /* Adresse de début de routine */ -    vmpa_t last;                            /* Meilleur dernière adresse   */ -    GArchInstruction *instr;                /* Instruction à ausculter     */ -    off_t length;                           /* Taille du code              */ - -    routines = get_all_exe_routines(binary->format, &routines_count); -    if (routines_count == 0) return; - -    qsort(routines, routines_count, sizeof(GBinRoutine *), g_binary_routine_rcompare); - -    starts = (vmpa_t *)calloc(routines_count, sizeof(vmpa_t)); -    lengths = (off_t *)calloc(routines_count, sizeof(off_t)); - -    for (i = 0; i < routines_count; i++) -    { -        starts[i] = g_binary_routine_get_address(routines[i]); -        lengths[i] = g_binary_routine_get_size(routines[i]); -    } - -    for (i = 0; i < routines_count; i++) -    { -        if (lengths[i] > 0) continue; - -        start = g_binary_routine_get_address(routines[i]); -        line = g_rendering_line_find_by_address(binary->lines, NULL, start); - -        last = find_best_ending_address_for_routine(line, i, starts, lengths, routines_count); - -        line = g_rendering_line_find_by_address(binary->lines, NULL, last); -        line = g_rendering_line_loop_for_code(line, NULL); - -        instr = g_code_line_get_instruction(G_CODE_LINE(line)); -        g_arch_instruction_get_location(instr, NULL, &length, NULL); - -        lengths[i] = last - start + length; -        g_binary_routine_set_size(routines[i], lengths[i]); - -    } - -    free(starts); -    free(lengths); - -} - - -/****************************************************************************** -*                                                                             * -*  Paramètres  : line    = ligne de départ du parcours.                       * -*                index   = indice de la routine traitée dans la liste.        * -*                starts  = adresse de départ des autres routines.             * -*                lengths = taille des différentes routines, valides ou nulles.* -*                count   = quantité de routines présentes.                    * -*                                                                             * -*  Description : Cherche l'adresse de fin d'une routine.                      * -*                                                                             * -*  Retour      : Plus grande adresse de dernière instruction de routine.      * -*                                                                             * -*  Remarques   : -                                                            * -*                                                                             * -******************************************************************************/ - -vmpa_t find_best_ending_address_for_routine(GRenderingLine *line, size_t index, const vmpa_t *starts, const off_t *lengths, size_t count) -{ -    vmpa_t result;                          /* Haute adresse à remonter    */ -    GRenderingLine *iter;                   /* Boucle de parcours #1       */ -    vmpa_t candidate;                       /* Candidat potentiel          */ -    size_t i;                               /* Boucle de parcours #2       */ -    GArchInstruction *instr;                /* Instruction à ausculter     */ - -    result = starts[index]; - -    for (iter = line; iter != NULL; iter = g_rendering_line_get_next_iter(line, iter, NULL)) -    { -        if (!G_IS_CODE_LINE(iter)) continue; - -        candidate = get_rendering_line_address(iter); - -        /* Regarde si on n'empiète pas sur une autre routine */ - -        for (i = 0; i < count; i++) -        { -            if (i == index) continue; - -            if (starts[i] <= candidate && candidate < (starts[i] + lengths[i])) -                break; - -        } - -        if (i != count) break; -        else result = candidate; - -        /* Retour de fonction ? */ - -        instr = g_code_line_get_instruction(G_CODE_LINE(iter)); -        if (g_arch_instruction_is_return(instr)) break; - -    } - -    return result; +    g_signal_emit_by_name(binary, "disassembly-done");  } diff --git a/src/analysis/binary.h b/src/analysis/binary.h index ad0429f..e6fc698 100644 --- a/src/analysis/binary.h +++ b/src/analysis/binary.h @@ -62,6 +62,9 @@ GOpenidaBinary *g_openida_binary_new_from_xml(xmlXPathContextPtr, const char *);  /* Ecrit une sauvegarde du binaire dans un fichier XML. */  bool g_openida_binary_save(const GOpenidaBinary *, xmlDocPtr, xmlXPathContextPtr, const char *); +/* Lance l'analyse d'un élément binaire chargé. */ +void g_openida_binary_analyse(GOpenidaBinary *); +  /* Fournit une description humaine d'un élément binaire. */  const char *g_openida_binary_to_string(const GOpenidaBinary *); diff --git a/src/analysis/delayed.c b/src/analysis/delayed.c new file mode 100644 index 0000000..1906713 --- /dev/null +++ b/src/analysis/delayed.c @@ -0,0 +1,786 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * delayed.c - gestion des actions d'analyse différées + * + * Copyright (C) 2009 Cyrille Bagard + * + *  This file is part of OpenIDA. + * + *  OpenIDA is free software; you can redistribute it and/or modify + *  it under the terms of the GNU General Public License as published by + *  the Free Software Foundation; either version 3 of the License, or + *  (at your option) any later version. + * + *  OpenIDA is distributed in the hope that it will be useful, + *  but WITHOUT ANY WARRANTY; without even the implied warranty of + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the + *  GNU General Public License for more details. + * + *  You should have received a copy of the GNU General Public License + *  along with Foobar.  If not, see <http://www.gnu.org/licenses/>. + */ + + +#include "delayed.h" + + +#include <malloc.h> + + +#include "line_code.h" +#include "line_comment.h" +#include "../common/dllist.h" +#include "../gtkext/gtkextstatusbar.h" +#include "../gtkext/iodamarshal.h" + + + +#ifndef _ +#   define _(str) (str) +#endif + + + + +/* Ensembles binaires à désassembler */ +typedef struct _disassembly_task +{ +    GOpenidaBinary *owner;                  /* Destinataire final          */ + +    DL_LIST_ITEM(link);                     /* Lien vers les maillons      */ + +    bin_part **parts;                       /* Parties binaires à traiter  */ +    size_t count;                           /* Nombre de ces parties       */ + +} disassembly_task; + + +#define disassembly_task_list_add_tail(new, head) dl_list_add_tail(new, head, disassembly_task, link) +#define disassembly_task_list_del(item, head) dl_list_del(item, head, disassembly_task, link) + + +/* Crée un tâche de désassemblage différé. */ +static disassembly_task *create_disassembly_task(GOpenidaBinary *, bin_part **, size_t); + +/* Efface une tâche de désassemblage de la mémoire. */ +static void delete_disassembly_task(disassembly_task *); + + + + + + +/* Gestionnaire des analyses différées (instance) */ +struct _GDelayedManager +{ +    GObject parent;                         /* A laisser en premier        */ + +    GtkExtStatusBar *statusbar;             /* Barre de statut principale  */ + +    disassembly_task *disassemblies;        /* Binaires à désassembler     */ +    GMutex *disass_mutex;                   /* Verrou pour l'accès         */ +    GCond *disass_cond;                     /* Réveil pour un traitement   */ + +    GThread *disassemble;                   /* Procédures de désassemblage */ + +}; + +/* Gestionnaire des analyses différées (classe) */ +struct _GDelayedManagerClass +{ +    GObjectClass parent;                    /* A laisser en premier        */ + +    /* Signaux */ + +    void (* disassembly_completed) (GDelayedManager *, GOpenidaBinary *, GRenderingLine *); + +}; + + +/* Initialise la classe des gestionnaires d'analyses différées. */ +static void g_delayed_manager_class_init(GDelayedManagerClass *); + +/* Initialise un gestionnaire d'analyses différées. */ +static void g_delayed_manager_init(GDelayedManager *); + +/* Crée un gestionnaire d'analyses différées. */ +static GDelayedManager *g_delayed_manager_new(GObject *); + +/* Assure le désassemblage en différé. */ +static void *process_disassemblies(GDelayedManager *); + +/* Procède au désassemblage basique d'un contenu binaire. */ +static GRenderingLine *disassemble_binary_parts(disassembly_task *, GBinRoutine **, size_t, GtkExtStatusBar *, guint); + +/* Etablit les liens entres les différentes lignes de code. */ +static void establish_links_between_lines(GRenderingLine *, GBinRoutine **, size_t, GtkExtStatusBar *, guint); + +/* S'assure que toutes les routines ont une taille définie. */ +static void limit_all_routines(GRenderingLine *, GBinRoutine **, size_t, GtkExtStatusBar *, guint); + +/* Cherche l'adresse de fin d'une routine. */ +static vmpa_t find_best_ending_address_for_routine(GRenderingLine *, size_t, const vmpa_t *, const off_t *, size_t); + + + + + + + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : owner = binaire chargé en attente des résultats.             * +*                parts = parties binaires à désassembler.                     * +*                count = nombre de parties à traiter.                         * +*                                                                             * +*  Description : Crée un tâche de désassemblage différé.                      * +*                                                                             * +*  Retour      : Tâche créée.                                                 * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static disassembly_task *create_disassembly_task(GOpenidaBinary *owner, bin_part **parts, size_t count) +{ +    disassembly_task *result;               /* Tâche à retourner           */ + +    result = (disassembly_task *)calloc(1, sizeof(disassembly_task)); + +    result->owner = owner; + +    DL_LIST_ITEM_INIT(&result->link); + +    result->parts = parts; +    result->count = count; + +    return result; + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : task = tâche à libérer de la mémoire.                        * +*                                                                             * +*  Description : Efface une tâche de désassemblage de la mémoire.             * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void delete_disassembly_task(disassembly_task *task) +{ +    /* TODO +    result->parts = parts; +    result->count = count; +    */ + +    free(task); + +} + + + + + + + + + + + + + + + + +/* Indique le type défini pour le gestionnaire des analyses différées. */ +G_DEFINE_TYPE(GDelayedManager, g_delayed_manager, G_TYPE_OBJECT); + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : klass = classe à initialiser.                                * +*                                                                             * +*  Description : Initialise la classe des gestionnaires d'analyses différées. * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void g_delayed_manager_class_init(GDelayedManagerClass *klass) +{ +    g_signal_new("disassembly-completed", +                 G_TYPE_DELAYED_MANAGER, +                 G_SIGNAL_RUN_LAST, +                 G_STRUCT_OFFSET(GDelayedManagerClass, disassembly_completed), +                 NULL, NULL, +                 g_cclosure_user_marshal_VOID__OBJECT_OBJECT, +                 G_TYPE_NONE, 2, G_TYPE_OPENIDA_BINARY, G_TYPE_RENDERING_LINE); + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : manager = instance à initialiser.                            * +*                                                                             * +*  Description : Initialise un gestionnaire d'analyses différées.             * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void g_delayed_manager_init(GDelayedManager *manager) +{ + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : ref = espace de référencements global.                       * +*                                                                             * +*  Description : Crée un gestionnaire d'analyses différées.                   * +*                                                                             * +*  Retour      : Gestionnaire mis en place ou NULL en cas d'échec.            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static GDelayedManager *g_delayed_manager_new(GObject *ref) +{ +    GDelayedManager *result;                /* Adresse à retourner         */ +    GError *error;                          /* Bilan de création de thread */ + +    result = g_object_new(G_TYPE_DELAYED_MANAGER, NULL); + +    result->statusbar = g_object_get_data(ref, "statusbar"); + +    result->disass_mutex = g_mutex_new(); +    if (result->disass_mutex == NULL) +        goto dmn_error; + +    result->disass_cond = g_cond_new(); +    if (result->disass_cond == NULL) +        goto dmn_error; + +    result->disassemble = g_thread_create((GThreadFunc)process_disassemblies, result, FALSE, &error); +    if (!result->disassemble) +        goto dmn_error; + +    return result; + + dmn_error: + +    /* TODO */ + +    return NULL; + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : manager = gestionnaire des actions à mener.                  * +*                                                                             * +*  Description : Assure le désassemblage en différé.                          * +*                                                                             * +*  Retour      : Bilan de l'opération.                                        * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void *process_disassemblies(GDelayedManager *manager) +{ +    disassembly_task *task;                 /* Désassemblage à effectuer   */ +    GBinRoutine **routines;                 /* Liste des routines trouvées */ +    size_t routines_count;                  /* Nombre de ces routines      */ +    guint id;                               /* Identifiant de statut       */ +    GRenderingLine *lines;                  /* Nouvelles lignes de rendu   */ + +    while (1) +    { +        g_mutex_lock(manager->disass_mutex); + +        if (dl_list_empty(manager->disassemblies)) +            g_cond_wait(manager->disass_cond, manager->disass_mutex); + +        task = manager->disassemblies; +        disassembly_task_list_del(task, &manager->disassemblies); + +        g_mutex_unlock(manager->disass_mutex); + +        routines = get_all_exe_routines(g_openida_binary_get_format(task->owner), &routines_count); +        qsort(routines, routines_count, sizeof(GBinRoutine *), g_binary_routine_rcompare); + +        /* Première étape */ + +        id = gtk_extended_status_bar_push(manager->statusbar, _("Disassembling..."), true); + +        lines = disassemble_binary_parts(task, routines, routines_count, +                                         manager->statusbar, id); + +        gtk_extended_status_bar_remove(manager->statusbar, id); + +        /* Seconde étape */ + +        id = gtk_extended_status_bar_push(manager->statusbar, _("Establishing links..."), true); + +        establish_links_between_lines(lines, routines, routines_count, +                                      manager->statusbar, id); + +        gtk_extended_status_bar_remove(manager->statusbar, id); + +        /* Troisième  étape */ + +        id = gtk_extended_status_bar_push(manager->statusbar, _("Finding remaining limits..."), true); + +        limit_all_routines(lines, routines, routines_count, +                           manager->statusbar, id); + +        gtk_extended_status_bar_remove(manager->statusbar, id); + +        /* Fin */ + +        g_signal_emit_by_name(manager, "disassembly-completed", task->owner, lines); + +        delete_disassembly_task(task); + +    } + +    return NULL; + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : task      = tâche à l'origine du traitement.                 * +*                routines  = prototypes existants à insérer.                  * +*                count     = quantité de ces prototypes.                      * +*                statusbar = barre de statut avec progression à mettre à jour.* +*                id        = identifiant du message affiché à l'utilisateur.  * +*                                                                             * +*  Description : Procède au désassemblage basique d'un contenu binaire.       * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static GRenderingLine *disassemble_binary_parts(disassembly_task *task, GBinRoutine **routines, size_t count, GtkExtStatusBar *statusbar, guint id) +{ +    GRenderingLine *result;                 /* Ligne de rendu à retourner  */ +    GArchProcessor *proc;                   /* Architecture du binaire     */ +    GRenderingOptions *options;             /* Options de désassemblage    */ +    bin_t *bin_data;                        /* Données binaires à lire     */ +    size_t i;                               /* Boucle de parcours #1       */ +    off_t sum;                              /* Somme de toutes les tailles */ +    off_t done;                             /* Quantité déjà traitée       */ +    off_t pos;                              /* Début d'une zone binaire    */ +    off_t len;                              /* Taille de cette même zone   */ +    vmpa_t base;                            /* Adresse de la zone binaire  */ +    off_t start;                            /* Conservation du pt de départ*/ +    vmpa_t addr;                            /* Adresse d'une instruction   */ +    GArchInstruction *instr;                /* Instruction décodée         */ +    GRenderingLine *line;                   /* Nouvelle ligne de rendu     */ +    size_t k;                               /* Boucle de parcours #2       */ +    uint64_t routine_offset;                /* Point de départ de routine  */ +    char *routine_desc;                     /* Prototype d'une routine     */ + +    result = NULL; + +    proc = get_arch_processor_from_format(g_openida_binary_get_format(task->owner)); +    options = g_openida_binary_get_options(task->owner); +    bin_data = g_openida_binary_get_data(task->owner, NULL); + +    /* Préparation du suivi de la progression */ + +    sum = 0; + +    for (i = 0; i < task->count; i++) +    { +        get_bin_part_values(task->parts[i], NULL, &len, NULL); +        sum += len; +    } + +    done = 0; + +    for (i = 0; i < task->count; i++) +    { +        get_bin_part_values(task->parts[i], &pos, &len, &base); + +        /* Décodage des instructions */ + +        start = pos; +        pos = 0; + +        while (pos < len) +        { +            addr = base + pos; + +            instr = g_arch_processor_decode_instruction(proc, &bin_data[start], +                                                        &pos, len, start, addr); + +            line = g_code_line_new(addr, instr, options); +            g_rendering_line_add_to_lines(&result, line); + +            if (pos < len) +                gtk_extended_status_bar_update_activity(statusbar, id, (done + pos) * 1.0 / sum); + +        } + +        done += len; +        gtk_extended_status_bar_update_activity(statusbar, id, done * 1.0 / sum); + +        /* Ajout des prototypes de fonctions */ + +        for (k = 0; k < count; k++) +        { +            routine_offset = g_binary_routine_get_address(routines[k]); + +            if (!(base <= routine_offset && routine_offset < (base + len))) continue; + +            routine_desc = g_binary_routine_to_string(routines[k]); + +            line = g_comment_line_new(routine_offset, routine_desc, options); +            g_rendering_line_insert_into_lines(&result, line, true); + +            free(routine_desc); + +        } + +    } + +    return result; + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : lines     = lignes de rendu à relier.                        * +*                routines  = prototypes existants à insérer.                  * +*                count     = quantité de ces prototypes.                      * +*                statusbar = barre de statut avec progression à mettre à jour.* +*                id        = identifiant du message affiché à l'utilisateur.  * +*                                                                             * +*  Description : Etablit les liens entres les différentes lignes de code.     * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void establish_links_between_lines(GRenderingLine *lines, GBinRoutine **routines, size_t count, GtkExtStatusBar *statusbar, guint id) +{ +    size_t i;                               /* Boucle de parcours          */ +    vmpa_t start;                           /* Adresse de départ           */ +    vmpa_t end;                             /* Adresse de fin              */ +    GRenderingLine *iter;                   /* Boucle de parcours          */ +    GArchInstruction *instr;                /* Instruction à ausculter     */ +    vmpa_t addr;                            /* Adresse référencée          */ +    InstructionLinkType type;               /* Type de référence           */ +    GRenderingLine *target;                 /* Ligne visée par la référence*/ + +    for (i = 0; i < count; i++) +    { +        start = g_binary_routine_get_address(routines[i]); +        end = start + g_binary_routine_get_size(routines[i]); + +        for (iter = g_rendering_line_find_by_address(lines, NULL, start); +             iter != NULL; +             iter = g_rendering_line_get_next_iter(lines, iter, NULL)) +        { +            /* Si on sort de la zone... */ +            if (get_rendering_line_address(iter) >= end) break; + +            /* On ne traite que du code ici ! */ +            if (!G_IS_CODE_LINE(iter)) continue; + +            instr = g_code_line_get_instruction(G_CODE_LINE(iter)); +            type = g_arch_instruction_get_link(instr, &addr); + +            switch (type) +            { +                case ILT_NONE: +                    break; + +                case ILT_JUMP: + +                    target = g_rendering_line_find_by_address(lines, NULL, addr); + +                    if (target != NULL) +                        g_rendering_line_link_with(iter, target, type); + +                    break; + +                case ILT_JUMP_IF_FALSE: +                    break; + +                case ILT_JUMP_IF_TRUE: + +                    target = g_rendering_line_find_by_address(lines, NULL, addr); + +                    if (target != NULL) +                    { +                        g_rendering_line_link_with(iter, target, type); + +                        target = g_rendering_line_get_next_iter(lines, iter, NULL); +                        if (target != NULL) +                            g_rendering_line_link_with(iter, target, ILT_JUMP_IF_FALSE); + +                    } + +                    break; + +                case ILT_CALL: + +                    target = g_rendering_line_find_by_address(lines, NULL, addr); + +                    if (target != NULL) +                        g_rendering_line_link_with(iter, target, type); + +                    break; + +            } + +        } + +        gtk_extended_status_bar_update_activity(statusbar, id, (i + 1) * 1.0 / count); + +    } + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : lines     = lignes de rendu à parcourir.                     * +*                routines  = prototypes existants à insérer.                  * +*                count     = quantité de ces prototypes.                      * +*                statusbar = barre de statut avec progression à mettre à jour.* +*                id        = identifiant du message affiché à l'utilisateur.  * +*                                                                             * +*  Description : S'assure que toutes les routines ont une taille définie.     * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void limit_all_routines(GRenderingLine *lines, GBinRoutine **routines, size_t count, GtkExtStatusBar *statusbar, guint id) +{ +    size_t i;                               /* Boucle de parcours          */ +    vmpa_t *starts;                         /* Adresses de départ          */ +    off_t *lengths;                         /* Tailles des routines        */ +    GRenderingLine *line;                   /* Ligne de départ / d'arrivée */ +    vmpa_t start;                           /* Adresse de début de routine */ +    vmpa_t last;                            /* Meilleur dernière adresse   */ +    GArchInstruction *instr;                /* Instruction à ausculter     */ +    off_t length;                           /* Taille du code              */ + +    if (count == 0) return; + +    starts = (vmpa_t *)calloc(count, sizeof(vmpa_t)); +    lengths = (off_t *)calloc(count, sizeof(off_t)); + +    for (i = 0; i < count; i++) +    { +        starts[i] = g_binary_routine_get_address(routines[i]); +        lengths[i] = g_binary_routine_get_size(routines[i]); + +        gtk_extended_status_bar_update_activity(statusbar, id, (i + 1) * 1.0 / (count * 2)); + +    } + +    for (i = 0; i < count; i++) +    { +        if (lengths[i] > 0) continue; + +        start = g_binary_routine_get_address(routines[i]); +        line = g_rendering_line_find_by_address(lines, NULL, start); + +        last = find_best_ending_address_for_routine(line, i, starts, lengths, count); + +        line = g_rendering_line_find_by_address(lines, NULL, last); +        line = g_rendering_line_loop_for_code(line, NULL); + +        instr = g_code_line_get_instruction(G_CODE_LINE(line)); +        g_arch_instruction_get_location(instr, NULL, &length, NULL); + +        lengths[i] = last - start + length; +        g_binary_routine_set_size(routines[i], lengths[i]); + +        gtk_extended_status_bar_update_activity(statusbar, id, (i + 1 + count) * 1.0 / (count * 2)); + +    } + +    free(starts); +    free(lengths); + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : line    = ligne de départ du parcours.                       * +*                index   = indice de la routine traitée dans la liste.        * +*                starts  = adresse de départ des autres routines.             * +*                lengths = taille des différentes routines, valides ou nulles.* +*                count   = quantité de routines présentes.                    * +*                                                                             * +*  Description : Cherche l'adresse de fin d'une routine.                      * +*                                                                             * +*  Retour      : Plus grande adresse de dernière instruction de routine.      * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static vmpa_t find_best_ending_address_for_routine(GRenderingLine *line, size_t index, const vmpa_t *starts, const off_t *lengths, size_t count) +{ +    vmpa_t result;                          /* Haute adresse à remonter    */ +    GRenderingLine *iter;                   /* Boucle de parcours #1       */ +    vmpa_t candidate;                       /* Candidat potentiel          */ +    size_t i;                               /* Boucle de parcours #2       */ +    GArchInstruction *instr;                /* Instruction à ausculter     */ + +    result = starts[index]; + +    for (iter = line; iter != NULL; iter = g_rendering_line_get_next_iter(line, iter, NULL)) +    { +        if (!G_IS_CODE_LINE(iter)) continue; + +        candidate = get_rendering_line_address(iter); + +        /* Regarde si on n'empiète pas sur une autre routine */ + +        for (i = 0; i < count; i++) +        { +            if (i == index) continue; + +            if (starts[i] <= candidate && candidate < (starts[i] + lengths[i])) +                break; + +        } + +        if (i != count) break; +        else result = candidate; + +        /* Retour de fonction ? */ + +        instr = g_code_line_get_instruction(G_CODE_LINE(iter)); +        if (g_arch_instruction_is_return(instr)) break; + +    } + +    return result; + +} + + + + + + + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : manager = gestionnaire des actions à mener.                  * +*                binary  = élément binaire concerné par la procédure.         * +*                parts   = blocs binaires à désassembler.                     * +*                count   = quantité de ces blocs binaires.                    * +*                                                                             * +*  Description : Place un nouveau désassemblage en attente.                   * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +void g_delayed_manager_schedule_disassembly(GDelayedManager *manager, GOpenidaBinary *binary, bin_part **parts, size_t count) +{ +    disassembly_task *task;                 /* Nouveau désassemblage       */ + +    task = create_disassembly_task(binary, parts, count); + +    g_mutex_lock(manager->disass_mutex); + +    disassembly_task_list_add_tail(task, &manager->disassemblies); + +    g_cond_signal(manager->disass_cond); + +    g_mutex_unlock(manager->disass_mutex); + +} + + + + + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : manager = nouveau gestionnaire à mémoriser ou NULL.          * +*                                                                             * +*  Description : Fournit le gestionnaire d'analyse en différé courant.        * +*                                                                             * +*  Retour      : Gestionnaire d'analyse en différé courant.                   * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +GDelayedManager *_get_delayed_manager(GDelayedManager *manager) +{ +    static GDelayedManager *result = NULL;  /* Singleton à retourner       */ + +    if (manager != NULL) +        result = manager; + +    return result; + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : ref = espace de référencements global.                       * +*                                                                             * +*  Description : Procède au chargement du gestionnaire d'analyse différées.   * +*                                                                             * +*  Retour      : true pour indiquer un chargement réussi, false sinon.        * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +bool init_delayed_manager(GObject *ref) +{ +    GDelayedManager *manager;               /* Singleton à mettre en place */ + +    manager = g_delayed_manager_new(ref); + +    if (manager != NULL) +        _get_delayed_manager(manager); + +    return (manager != NULL); + +} diff --git a/src/analysis/delayed.h b/src/analysis/delayed.h new file mode 100644 index 0000000..5548531 --- /dev/null +++ b/src/analysis/delayed.h @@ -0,0 +1,74 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * delayed.h - prototypes pour la gestion des actions d'analyse différées + * + * Copyright (C) 2009 Cyrille Bagard + * + *  This file is part of OpenIDA. + * + *  OpenIDA is free software; you can redistribute it and/or modify + *  it under the terms of the GNU General Public License as published by + *  the Free Software Foundation; either version 3 of the License, or + *  (at your option) any later version. + * + *  OpenIDA is distributed in the hope that it will be useful, + *  but WITHOUT ANY WARRANTY; without even the implied warranty of + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the + *  GNU General Public License for more details. + * + *  You should have received a copy of the GNU General Public License + *  along with Foobar.  If not, see <http://www.gnu.org/licenses/>. + */ + + +#ifndef _ANALYSIS_DELAYED_H +#define _ANALYSIS_DELAYED_H + + +#include <glib-object.h> +#include <stdbool.h> + + +#include "binary.h" + + +#include "../format/exe_format.h"   /* TODO : Voir pour n'include que GBinPart */ + + + +#define G_TYPE_DELAYED_MANAGER               g_delayed_manager_get_type() +#define G_DELAYED_MANAGER(obj)               (G_TYPE_CHECK_INSTANCE_CAST((obj), g_delayed_manager_get_type(), GDelayedManager)) +#define G_IS_DELAYED_MANAGER(obj)            (G_TYPE_CHECK_INSTANCE_TYPE((obj), g_delayed_manager_get_type())) +#define G_DELAYED_MANAGER_GET_IFACE(inst)    (G_TYPE_INSTANCE_GET_INTERFACE((inst), g_delayed_manager_get_type(), GDelayedManagerIface)) +#define G_DELAYED_MANAGER_GET_CLASS(obj)     (G_TYPE_INSTANCE_GET_CLASS((obj), G_TYPE_DELAYED_MANAGER, GDelayedManagerClass)) + + +/* Gestionnaire des analyses différées (instance) */ +typedef struct _GDelayedManager GDelayedManager; + +/* Gestionnaire des analyses différées (classe) */ +typedef struct _GDelayedManagerClass GDelayedManagerClass; + + +/* Indique le type défini pour le gestionnaire des analyses différées. */ +GType g_delayed_manager_get_type(void); + +/* Place un nouveau désassemblage en attente. */ +void g_delayed_manager_schedule_disassembly(GDelayedManager *, GOpenidaBinary *, bin_part **, size_t); + + + + + +#define get_delayed_manager() _get_delayed_manager(NULL) + + +/* Fournit le gestionnaire d'analyse en différé courant. */ +GDelayedManager *_get_delayed_manager(GDelayedManager *); + +/* Procède au chargement du gestionnaire d'analyse différées. */ +bool init_delayed_manager(GObject *); + + + +#endif  /* _ANALYSIS_DELAYED_H */ diff --git a/src/common/dllist.h b/src/common/dllist.h index 1bd1ead..5941f64 100644 --- a/src/common/dllist.h +++ b/src/common/dllist.h @@ -95,7 +95,7 @@ void __dl_list_splice(dl_list_item *, dl_list_head);      {                                                                               \          dl_list_item *hmbr = &(*head)->member;                                      \          __dl_list_del(&item->member, &hmbr);                                        \ -        *(head) = container_of(hmbr, type, member);                                 \ +        *(head) = (hmbr ? container_of(hmbr, type, member) : NULL);                 \          DL_LIST_ITEM_INIT(&item->member);                                           \      }                                                                               \      while(0) diff --git a/src/editor.c b/src/editor.c index 76c8181..6b088c5 100644 --- a/src/editor.c +++ b/src/editor.c @@ -44,6 +44,7 @@  #include "pan_strings.h"  #include "analysis/binary.h"  #include "gtkext/easygtk.h" +#include "gtkext/gtkextstatusbar.h"  #include "gtkext/gtkbinview.h"  #include "gtkext/gtkdockpanel.h" @@ -180,7 +181,7 @@ GtkWidget *create_editor(void)    GtkWidget *hpaned1;    GtkWidget *scrolledwindow2;    GtkWidget *snippet; -  GtkWidget *statusbar1; +  GtkWidget *statusbar;    GtkWidget *view; @@ -203,6 +204,7 @@ GtkWidget *create_editor(void)      GtkWidget *hbuttonbox;                  /* Support horizontal de btns. */      GtkWidget *button;                      /* Btn. de commande inférieur  */  #endif +    GtkWidget *button;                      /* Btn. de commande inférieur  */ @@ -511,9 +513,12 @@ GtkWidget *create_editor(void)    */ -  statusbar1 = gtk_statusbar_new (); -  gtk_widget_show (statusbar1); -  gtk_box_pack_start (GTK_BOX (vbox1), statusbar1, FALSE, FALSE, 0); +    statusbar = gtk_extended_status_bar_new(); +    g_object_set_data(ref, "statusbar", statusbar); + +    gtk_widget_show(statusbar); + +    gtk_box_pack_start(GTK_BOX(vbox1), statusbar, FALSE, FALSE, 0); @@ -573,7 +578,7 @@ void mcb_file_new_project(GtkMenuItem *menuitem, gpointer data)  {      openida_project *project;               /* Nouveau projet courant      */ -    project = create_empty_openida_project(); +    project = create_empty_openida_project(G_OBJECT(data));      set_current_openida_project(project); @@ -611,7 +616,7 @@ void mcb_file_open_project(GtkMenuItem *menuitem, gpointer data)      {          filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); -        project = g_openida_project_new_from_xml(filename); +        project = g_openida_project_new_from_xml(G_OBJECT(data), filename);          if (project != NULL)          { @@ -935,7 +940,7 @@ void mcb_select_sections(GtkMenuItem *menuitem, gpointer data)  {      GtkWidget *dialog;                      /* Boîte de dialogue à montrer */ -    dialog = create_sections_dialog(create_empty_openida_project()/* FIXME */); +    dialog = create_sections_dialog(create_empty_openida_project(G_OBJECT(data))/* FIXME */);      gtk_widget_show(dialog);  } diff --git a/src/format/exe_format.c b/src/format/exe_format.c index cda17f7..92db2fd 100644 --- a/src/format/exe_format.c +++ b/src/format/exe_format.c @@ -157,9 +157,9 @@ void set_bin_part_values(bin_part *part, off_t offset, off_t size, uint64_t voff  void get_bin_part_values(const bin_part *part, off_t *offset, off_t *size, uint64_t *voffset)  { -    *offset = part->offset; -    *size = part->size; -    *voffset = part->voffset; +    if (offset != NULL) *offset = part->offset; +    if (size != NULL) *size = part->size; +    if (voffset != NULL) *voffset = part->voffset;  } diff --git a/src/gtkext/Makefile.am b/src/gtkext/Makefile.am index 51bea0f..fbfa0b6 100644 --- a/src/gtkext/Makefile.am +++ b/src/gtkext/Makefile.am @@ -5,6 +5,7 @@ noinst_LTLIBRARIES = libgtkext.la  libgtkext_la_SOURCES =					\  	easygtk.h easygtk.c					\ +	gtkextstatusbar.h gtkextstatusbar.c	\  	gtkbinview-int.h					\  	gtkbinview.h gtkbinview.c			\  	gtkblockview.h gtkblockview.c		\ diff --git a/src/gtkext/gtkblockview.c b/src/gtkext/gtkblockview.c index ad0aded..2659b0d 100644 --- a/src/gtkext/gtkblockview.c +++ b/src/gtkext/gtkblockview.c @@ -357,7 +357,7 @@ static gboolean gtk_block_view_expose(GtkWidget *widget, GdkEventExpose *event)      y = event->area.y - y; -    for ( ; iter != NULL; +    for ( ; iter != NULL && y < (event->area.y + event->area.height);            iter = g_rendering_line_get_next_iter(view->lines, iter, view->last))      {          g_rendering_line_draw(iter, GDK_DRAWABLE(widget->window), view->gc, diff --git a/src/gtkext/gtkextstatusbar.c b/src/gtkext/gtkextstatusbar.c new file mode 100644 index 0000000..cb6851d --- /dev/null +++ b/src/gtkext/gtkextstatusbar.c @@ -0,0 +1,228 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * gtkextstatusbar.h - prototypes pour la barre de statut améliorée + * + * Copyright (C) 2009 Cyrille Bagard + * + *  This file is part of OpenIDA. + * + *  OpenIDA is free software; you can redistribute it and/or modify + *  it under the terms of the GNU General Public License as published by + *  the Free Software Foundation; either version 3 of the License, or + *  (at your option) any later version. + * + *  OpenIDA is distributed in the hope that it will be useful, + *  but WITHOUT ANY WARRANTY; without even the implied warranty of + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the + *  GNU General Public License for more details. + * + *  You should have received a copy of the GNU General Public License + *  along with Foobar.  If not, see <http://www.gnu.org/licenses/>. + */ + + +#include "gtkextstatusbar.h" + + +#include <malloc.h> +#include <string.h> + + + +/* Initialise la classe des barres de statut améliorées. */ +static void gtk_extended_status_bar_class_init(GtkExtStatusBarClass *); + +/* Initialise une instance de barre de statut améliorée. */ +static void gtk_extended_status_bar_init(GtkExtStatusBar *); + + + +/* Détermine le type de la barre de statut améliorée. */ +G_DEFINE_TYPE(GtkExtStatusBar, gtk_extended_status_bar, GTK_TYPE_STATUSBAR) + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : klass = classe GTK à initialiser.                            * +*                                                                             * +*  Description : Initialise la classe des barres de statut améliorées.        * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void gtk_extended_status_bar_class_init(GtkExtStatusBarClass *klass) +{ + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : bar = instance GTK à initialiser.                            * +*                                                                             * +*  Description : Initialise une instance de barre de statut améliorée.        * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +static void gtk_extended_status_bar_init(GtkExtStatusBar *bar) +{ +    bar->context = gtk_statusbar_get_context_id(GTK_STATUSBAR(bar), ""); + +    bar->progress = GTK_PROGRESS_BAR(gtk_progress_bar_new()); + +    gtk_widget_set_size_request(GTK_WIDGET(bar->progress), 200, -1); + +    gtk_progress_bar_set_fraction(bar->progress, 0.5); +    gtk_progress_bar_set_text(bar->progress, "50%"); + +    gtk_box_pack_start(GTK_BOX(bar), GTK_WIDGET(bar->progress), FALSE, FALSE, 4); + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : -                                                            * +*                                                                             * +*  Description : Crée une nouvelle instance de barre de statut.               * +*                                                                             * +*  Retour      : Composant GTK mis en place.                                  * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +GtkWidget *gtk_extended_status_bar_new(void) +{ +    return g_object_new(GTK_TYPE_EXT_STATUS_BAR, NULL); + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : bar         = barre de statut à manipuler.                   * +*                message     = message à afficher pour l'utilisateur.         * +*                progressive = utilisation de la barre de progression.        * +*                                                                             * +*  Description : Place un nouveau message dans la barre de statut.            * +*                                                                             * +*  Retour      : Identifiant du nouveau statut défini.                        * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +guint gtk_extended_status_bar_push(GtkExtStatusBar *bar, const gchar *message, gboolean progressive) +{ +    guint result;                           /* Identifiant à retourner     */ + +    result = gtk_statusbar_push(GTK_STATUSBAR(bar), bar->context, message); + +    bar->msg_count++; +    bar->msg_id = (guint *)realloc(bar->msg_id, bar->msg_count * sizeof(guint)); +    bar->is_progressive = (gboolean *)realloc(bar->is_progressive, bar->msg_count * sizeof(gboolean)); + +    bar->msg_id[bar->msg_count - 1] = result; +    bar->is_progressive[bar->msg_count - 1] = progressive; + +    if (progressive) +    { +        gtk_progress_bar_set_fraction(bar->progress, 0.0); +        gtk_progress_bar_set_text(bar->progress, "0%"); + +        gtk_widget_show(GTK_WIDGET(bar->progress)); + +    } +    else gtk_widget_hide(GTK_WIDGET(bar->progress)); + +    return result; + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : bar   = barre de statut à manipuler.                         * +*                id    = identifiant du message concerné.                     * +*                value = valeur actuelle de la progression.                   * +*                                                                             * +*  Description : Met à jour la barre de progression de la barre de statut.    * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +void gtk_extended_status_bar_update_activity(GtkExtStatusBar *bar, guint id, gdouble value) +{ +    gchar percent[5];                       /* Pourcentage en version txt. */ + +    if (bar->msg_count == 0) return; + +    gdk_threads_enter(); + +    if (id == bar->msg_id[bar->msg_count - 1] && bar->is_progressive[bar->msg_count - 1]) +    { +        g_snprintf(percent, 5, "%.0f%%", value * 100); + +        gtk_progress_bar_set_fraction(bar->progress, value); +        gtk_progress_bar_set_text(bar->progress, percent); + +    } + +    gdk_flush (); + +    gdk_threads_leave(); + +} + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : bar = barre de statut à manipuler.                           * +*                id  = identifiant du statut à supprimer.                     * +*                                                                             * +*  Description : Retire de la barre un statut, visible ou non.                * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +void gtk_extended_status_bar_remove(GtkExtStatusBar *bar, guint id) +{ +    size_t i;                               /* Boucle de parcours          */ + +    gtk_statusbar_remove(GTK_STATUSBAR(bar), bar->context, id); + +    for (i = 0; i < bar->msg_count; i++) +        if (bar->msg_id[i] == id) +            break; + +    if ((i + 1) < bar->msg_count) +    { +        memmove(&bar->msg_id[i], &bar->msg_id[i + 1], (bar->msg_count - i - 1) * sizeof(guint)); +        memmove(&bar->is_progressive[i], &bar->is_progressive[i + 1], (bar->msg_count - i - 1) * sizeof(gboolean)); +    } + +    bar->msg_count--; +    bar->msg_id = (guint *)realloc(bar->msg_id, bar->msg_count * sizeof(guint)); +    bar->is_progressive = (gboolean *)realloc(bar->is_progressive, bar->msg_count * sizeof(gboolean)); + +    if (bar->msg_count > 0 && bar->is_progressive[bar->msg_count - 1]) +        gtk_widget_show(GTK_WIDGET(bar->progress)); + +    else +        gtk_widget_hide(GTK_WIDGET(bar->progress)); + +} diff --git a/src/gtkext/gtkextstatusbar.h b/src/gtkext/gtkextstatusbar.h new file mode 100644 index 0000000..2342b56 --- /dev/null +++ b/src/gtkext/gtkextstatusbar.h @@ -0,0 +1,88 @@ + +/* OpenIDA - Outil d'analyse de fichiers binaires + * gtkextstatusbar.h - prototypes pour la barre de statut améliorée + * + * Copyright (C) 2009 Cyrille Bagard + * + *  This file is part of OpenIDA. + * + *  OpenIDA is free software; you can redistribute it and/or modify + *  it under the terms of the GNU General Public License as published by + *  the Free Software Foundation; either version 3 of the License, or + *  (at your option) any later version. + * + *  OpenIDA is distributed in the hope that it will be useful, + *  but WITHOUT ANY WARRANTY; without even the implied warranty of + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the + *  GNU General Public License for more details. + * + *  You should have received a copy of the GNU General Public License + *  along with Foobar.  If not, see <http://www.gnu.org/licenses/>. + */ + + +#ifndef _GTKEXT_GTKEXTSTATUSBAR_H +#define _GTKEXT_GTKEXTSTATUSBAR_H + + +#include <gtk/gtk.h> + + + +G_BEGIN_DECLS + + + +#define GTK_TYPE_EXT_STATUS_BAR            (gtk_extended_status_bar_get_type()) +#define GTK_EXT_STATUS_BAR(obj)            GTK_CHECK_CAST(obj, gtk_extended_status_bar_get_type (), GtkExtStatusBar) +#define GTK_EXT_STATUS_BAR_CLASS(klass)    GTK_CHECK_CLASS_CAST(klass, gtk_extended_status_bar_get_type(), GtkExtStatusBarClass) +#define GTK_IS_EXT_STATUS_BAR(obj)         GTK_CHECK_TYPE(obj, gtk_extended_status_bar_get_type()) + + +typedef struct _GtkExtStatusBar GtkExtStatusBar; +typedef struct _GtkExtStatusBarClass GtkExtStatusBarClass; + + + +struct _GtkExtStatusBar +{ +    GtkStatusbar bar;                       /* Présence obligatoire en 1er */ + +    guint context;                          /* Nouvel identifiant          */ + +    GtkProgressBar *progress;               /* Barre de progression        */ + +    guint *msg_id;                          /* Liste des identifiants      */ +    gboolean *is_progressive;               /* Utilisations de progression */ +    size_t msg_count;                       /* Nombre de messages empilés  */ + +}; + +struct _GtkExtStatusBarClass +{ +    GtkStatusbarClass parent_class;         /* Présence obligatoire en 1er */ + +}; + + +/* Détermine le type de la barre de statut améliorée. */ +GtkType gtk_extended_status_bar_get_type(void); + +/* Crée une nouvelle instance de barre de statut. */ +GtkWidget *gtk_extended_status_bar_new(void); + +/* Place un nouveau message dans la barre de statut. */ +guint gtk_extended_status_bar_push(GtkExtStatusBar *, const gchar *, gboolean); + +/* Met à jour la barre de progression de la barre de statut. */ +void gtk_extended_status_bar_update_activity(GtkExtStatusBar *, guint, gdouble); + +/* Retire de la barre un statut, visible ou non. */ +void gtk_extended_status_bar_remove(GtkExtStatusBar *, guint); + + + +G_END_DECLS + + +#endif  /* _GTKEXT_GTKEXTSTATUSBAR_H */ @@ -31,6 +31,7 @@  #include "editor.h"  #include "params.h"  #include "project.h" +#include "analysis/delayed.h"  #include "arch/processor.h"  #include "format/exe_format.h"  #include "format/mangling/demangler.h" @@ -95,19 +96,19 @@ int main(int argc, char **argv)      editor = create_editor();      gtk_widget_show(editor); +    init_delayed_manager(G_OBJECT(editor)); +      init_all_plugins(G_OBJECT(editor));      /* Charge le dernier projet */      filename = get_string_config_value(config, MPT_RECENT_PROJECT_1); -    if (filename == NULL) project = create_empty_openida_project(); -    else project = g_openida_project_new_from_xml(filename); +    if (filename == NULL) project = create_empty_openida_project(G_OBJECT(editor)); +    else project = g_openida_project_new_from_xml(G_OBJECT(editor), filename);      set_current_openida_project(project); -    display_openida_project(project, G_OBJECT(editor)); -      /* Exécution du programme */      gdk_threads_enter(); diff --git a/src/project.c b/src/project.c index 40ddb14..fde1eb2 100644 --- a/src/project.c +++ b/src/project.c @@ -62,6 +62,8 @@ GtkWidget *get_loaded_binary_view(const loaded_binary *, BinaryView);  /* Propriétés d'un ensemble de fichiers ouverts */  struct openida_project  { +    GObject* ref;                           /* Espace de référencement     */ +      char *filename;                         /* Lieu d'enregistrement       */      loaded_binary **binaries;               /* Fichiers binaires associés  */ @@ -74,6 +76,12 @@ struct openida_project +/* Assure l'intégration d'un élément binaire dans un projet. */ +void display_new_binary_of_openida_project(GOpenidaBinary *, openida_project *); + + + + @@ -194,7 +202,7 @@ openida_project *_get_current_openida_project(openida_project *project)  /******************************************************************************  *                                                                             * -*  Paramètres  : -                                                            * +*  Paramètres  : ref = espace de référencement global.                        *  *                                                                             *  *  Description : Crée un projet vide.                                         *  *                                                                             * @@ -204,12 +212,14 @@ openida_project *_get_current_openida_project(openida_project *project)  *                                                                             *  ******************************************************************************/ -openida_project *create_empty_openida_project(void) +openida_project *create_empty_openida_project(GObject *ref)  {      openida_project *result;                /* Adresse à retourner         */      result = (openida_project *)calloc(1, sizeof(openida_project)); +    result->ref = ref; +      return result;  } @@ -217,7 +227,8 @@ openida_project *create_empty_openida_project(void)  /******************************************************************************  *                                                                             * -*  Paramètres  : filename = chemin d'accès au fichier à charger.              * +*  Paramètres  : ref      = espace de référencement global.                   * +*                filename = chemin d'accès au fichier à charger.              *  *                                                                             *  *  Description : Crée un projet à partir du contenu XML d'un fichier.         *  *                                                                             * @@ -227,7 +238,7 @@ openida_project *create_empty_openida_project(void)  *                                                                             *  ******************************************************************************/ -openida_project *g_openida_project_new_from_xml(const char *filename) +openida_project *g_openida_project_new_from_xml(GObject *ref, const char *filename)  {      openida_project *result;                /* Adresse à retourner         */      xmlDocPtr xdoc;                         /* Structure XML chargée       */ @@ -240,7 +251,7 @@ openida_project *g_openida_project_new_from_xml(const char *filename)      if (!open_xml_file(filename, &xdoc, &context)) return NULL; -    result = (openida_project *)calloc(1, sizeof(openida_project)); +    result = create_empty_openida_project(ref);      /* Chargement des éléments binaires attachés */ @@ -259,7 +270,11 @@ openida_project *g_openida_project_new_from_xml(const char *filename)          free(access);          if (binary != NULL) -            attach_binary_to_openida_project(result, binary); +        { +            g_signal_connect(binary, "disassembly-done", +                             G_CALLBACK(display_new_binary_of_openida_project), result); +            g_openida_binary_analyse(binary); +        }      } @@ -655,3 +670,54 @@ void display_openida_project(const openida_project *project, GObject *ref)  } + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : binary  = élément binaire tout juste désassemblé.            * +*                project = projet dont le contenu est à compléter.            * +*                                                                             * +*  Description : Assure l'intégration d'un élément binaire dans un projet.    * +*                                                                             * +*  Retour      : -                                                            * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +void display_new_binary_of_openida_project(GOpenidaBinary *binary, openida_project *project) +{ +    size_t index;                           /* Indice du nouveau binaire   */ +    GtkDockPanel *dpanel;                   /* Support de panneaux         */ +    GtkWidget *view;                        /* Affichage du code binaire   */ +    GtkDockItem *ditem;                     /* Panneau avec ses infos.     */ +    GtkBinView *binview;                    /* Affichage à faire défiler   */ + + + + +    attach_binary_to_openida_project(project, binary); +    index = project->binaries_count - 1; + +    dpanel = GTK_DOCK_PANEL(g_object_get_data(project->ref, "binpanel")); + +    view = get_loaded_binary_view(project->binaries[index], BVW_BLOCK); + +    ditem = gtk_dock_item_new(g_openida_binary_to_string(binary), view); +    gtk_dock_panel_add_item(dpanel, ditem); + + + +    g_object_set_data(project->ref, "current_binary", binary); + + +    get_view_for_openida_project_binary(project, binary, BVW_BLOCK, &binview); +    g_object_set_data(project->ref, "binview", binview); + + +    reload_symbols_panel_content(get_panel(PNT_SYMBOLS), g_openida_binary_get_format(binary)); + + + + +} diff --git a/src/project.h b/src/project.h index 68ddf16..815d922 100644 --- a/src/project.h +++ b/src/project.h @@ -58,10 +58,10 @@ typedef struct openida_project openida_project;  openida_project *_get_current_openida_project(openida_project *);  /* Crée un projet vide. */ -openida_project *create_empty_openida_project(void); +openida_project *create_empty_openida_project(GObject *);  /* Crée un projet à partir du contenu XML d'un fichier. */ -openida_project *g_openida_project_new_from_xml(const char *); +openida_project *g_openida_project_new_from_xml(GObject *, const char *);  /* Procède à l'enregistrement d'un projet donné. */  bool g_openida_project_save(openida_project *, const char *); | 
