summaryrefslogtreecommitdiff
path: root/src/analysis/disass/fetch.c
blob: 4686fe2f4cb2b14436d901279c2d9342f99cd82f (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263

/* Chrysalide - Outil d'analyse de fichiers binaires
 * fetch.c - récupération d'instructions à partir de binaire brut
 *
 * Copyright (C) 2010-2013 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  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"


#include "../../arch/artificial.h"








/******************************************************************************
*                                                                             *
*  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 *load_raw_binary(const GLoadedBinary *binary, const vmpa2t *base, off_t end, GtkExtStatusBar *statusbar, bstatus_id_t 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     */
    vmpa2t *pos;                            /* Boucle de parcours          */
    vmpa2t *prev;                           /* Boucle de parcours          */
    off_t old_phy;                          /* Ancienne position physique  */
    GArchInstruction *instr;                /* Instruction décodée         */
    off_t new_phy;                          /* Nouvelle position physique  */

    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);

    end = bin_length;

    pos = local_dup_vmpa(base);
    prev = local_dup_vmpa(base);

    old_phy = get_phy_addr(prev);

    while (old_phy < end)
    {
        instr = g_db_instruction_new_from_data(bin_data, pos, end, proc);
        if (instr == NULL) break;

        new_phy = get_phy_addr(pos);

        g_arch_instruction_set_location(instr, prev, new_phy - old_phy);
        g_arch_instruction_add_to_list(&result, instr);

        copy_vmpa(prev, pos);
        old_phy = get_phy_addr(prev);

        //done += (new_phy - old_phy);
        //gtk_extended_status_bar_update_activity(statusbar, id, done * 1.0 / sum);

    }

    return result;

}






































#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, bstatus_id_t 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;
        len += start;

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

            instr = g_arch_processor_decode_instruction(proc, context, bin_data,
                                                        &pos, len, 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 - start) * 1.0 / sum);

        }

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

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

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

    }

    return result;

}