summaryrefslogtreecommitdiff
path: root/src/analysis/disass/fetch.c
blob: a1df5e3e45abe44a05675640ad44f8628093762e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141

/* OpenIDA - Outil d'analyse de fichiers binaires
 * fetch.c - récupération d'instructions à partir de binaire brut
 *
 * Copyright (C) 2010-2012 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 "fetch.h"

#ifdef DEBUG
#   include "../../arch/artificial.h"
#endif



/******************************************************************************
*                                                                             *
*  Paramètres  : binary    = représentation de binaire chargé.                *
*                parts     = parties binaires à désassembler.                 *
*                count     = nombre de parties à traiter.                     *
*                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   : -                                                            *
*                                                                             *
******************************************************************************/

GArchInstruction *disassemble_binary_parts(const GLoadedBinary *binary, GBinPart **parts, size_t count, GtkExtStatusBar *statusbar, guint id)
{
    GArchInstruction *result;               /* Liste d'instr. à renvoyer   */
    GBinFormat *format;                     /* Format du fichier binaire   */
    GArchProcessor *proc;                   /* Architecture du binaire     */
    off_t bin_length;                       /* Taille des données à lire   */
    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       */
#ifdef DEBUG
    unsigned int valid;                     /* Instructions traduites      */
    unsigned int db;                        /* Instructions non décodées   */
#endif
    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*/
    GProcContext *context;                  /* Contexte pour le décodage   */
    vmpa_t addr;                            /* Adresse d'une instruction   */
    GArchInstruction *instr;                /* Instruction décodée         */

    result = NULL;

    format = G_BIN_FORMAT(g_loaded_binary_get_format(binary));
    proc = get_arch_processor_from_format(G_EXE_FORMAT(format));
    bin_data = g_loaded_binary_get_data(binary, &bin_length);

    /* Préparation du suivi de la progression */

    sum = 0;

    for (i = 0; i < count; i++)
    {
        g_binary_part_get_values(parts[i], NULL, &len, NULL);
        if (len > bin_length) continue;
        sum += len;
    }

    done = 0;

    for (i = 0; i < count; i++)
    {
        g_binary_part_get_values(parts[i], &pos, &len, &base);

        if (len > bin_length) continue;

        context = g_arch_processor_get_context(proc);

        /* Décodage des instructions */

#ifdef DEBUG
        valid = 0;
        db = 0;
#endif

        start = pos;
        pos = 0;

        while (pos < len)
        {
            addr = base + pos;

            instr = g_arch_processor_decode_instruction(proc, context, &bin_data[start],
                                                        &pos, len, start, addr, format);
            g_arch_instruction_add_to_list(&result, instr);

#ifdef DEBUG
            if (G_IS_DB_INSTRUCTION(instr) && !g_db_instruction_is_skipped(G_DB_INSTRUCTION(instr)))
                db++;
            else
                valid++;
#endif

            if (pos < len)
                gtk_extended_status_bar_update_activity(statusbar, id, (done + pos) * 1.0 / sum);

        }

        if (context != NULL)
            g_object_unref(context);

#ifdef DEBUG
        g_binary_part_set_checkup(parts[i], valid, db);
#endif

        done += len;
        gtk_extended_status_bar_update_activity(statusbar, id, done * 1.0 / sum);

    }

    return result;

}