summaryrefslogtreecommitdiff
path: root/plugins/dwarf/info.c
blob: 7a2be441f4f28dfaab5ffa987c7eee6fad9b6460 (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

/* Chrysalide - Outil d'analyse de fichiers binaires
 * info.c - lecture des informations principales du format DWARF
 *
 * Copyright (C) 2008-2018 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  Chrysalide 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.
 *
 *  Chrysalide 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 Chrysalide.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "info.h"


#include <i18n.h>
#include <analysis/contents/restricted.h>
#include <core/nproc.h>
#include <glibext/seq.h>
#include <core/global.h>


#include "die.h"
#include "format-int.h"
#include "utils.h"



#define RANGE_ALLOC_BLOCK 100


/* Rassemblement des informations utiles */
typedef struct _work_data
{
    GDwarfFormat *format;                   /* Format à manipuler          */
    mrange_t *ranges;                       /* Espace des DIE à charger    */

} work_data;

/* Procède au chargement d'un DIE de la section debug_info. */
static bool extract_dies_from_debug_info(const work_data *, size_t, GtkStatusStack *, activity_id_t);



/******************************************************************************
*                                                                             *
*  Paramètres  : format = informations de débogage DWARF à compléter.         *
*                gid    = groupe de travail impliqué.                         *
                 status = barre de statut à tenir informée.                   *

*                                                                             *
*  Description : Charge les informations depuis une section ".debug_info".    *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool load_dwarf_debug_information(GDwarfFormat *format, wgroup_id_t gid, GtkStatusStack *status)
{
    bool result;                            /* Bilan à renvoyer            */
    GExeFormat *exe;                        /* Exécutable associé          */
    mrange_t range;                         /* Couverture d'une section    */
    GBinContent *content;                   /* Contenu binaire à lire      */
    GBinContent *restricted;                /* Limitation des traitements  */
    vmpa2t stop;                            /* Point d'arrivée à atteindre */
    mrange_t *ranges;                       /* Séquences de zones à traiter*/
    size_t count;                           /* Nombre de ces séquences     */
    size_t allocated;                       /* Quantité d'allocations      */
    SourceEndian endian;                    /* Boutisme du format parent   */
    vmpa2t iter;                            /* Tête de lecture mouvante    */
    vmpa2t start;                           /* Sauvegarde de position      */
    dw_compil_unit_header header;           /* Unité à cerner puis traiter */
    vmpa2t next;                            /* Départ de l'unité suivante  */
    phys_t size;                            /* Taille complète d'une unité */
    work_data data;                         /* Données à communiquer       */
    guint runs_count;                       /* Qté d'exécutions parallèles */
    size_t run_size;                        /* Volume réparti par exécution*/
    GWorkQueue *queue;                      /* Gestionnaire de différés    */
    activity_id_t msg;                      /* Message de progression      */
    guint i;                                /* Boucle de parcours          */
    size_t begin;                           /* Début de bloc de traitement */
    size_t end;                             /* Fin d'un bloc de traitement */
    GSeqWork *work;                         /* Tâche de chargement à lancer*/

    exe = G_DBG_FORMAT(format)->executable;

    result = g_exe_format_get_section_range_by_name(exe, ".debug_info", &range);

    if (result)
    {
        content = G_BIN_FORMAT(format)->content;

        restricted = g_restricted_content_new(content, &range);

        compute_mrange_end_addr(&range, &stop);

        /* Constitution des zones de travail */

        ranges = NULL;
        count = 0;
        allocated = 0;

        endian = g_binary_format_get_endianness(G_BIN_FORMAT(exe));

        for (copy_vmpa(&iter, get_mrange_addr(&range));
             result && cmp_vmpa(&iter, &stop) < 0;
             copy_vmpa(&iter, &next))
        {
            copy_vmpa(&start, &iter);

            result = read_dwarf_compil_unit_header(restricted, &iter, endian, &header, &next);
            if (!result) break;

            if (count == allocated)
            {
                allocated += RANGE_ALLOC_BLOCK;
                ranges = realloc(ranges, allocated * sizeof(mrange_t));
            }

            size = compute_vmpa_diff(&start, &next);

            init_mrange(&ranges[count++], &start, size);

        }

        if (!result)
            goto exit;

        /* Préparation des réceptacles */

        format->info = calloc(count, sizeof(dw_die *));
        format->info_count = count;

        data.format = format;
        data.ranges = ranges;

        /* Lancement des travaux */

        runs_count = get_max_online_threads();

        run_size = count / runs_count;

        queue = get_work_queue();

        msg = gtk_status_stack_add_activity(status, _("Loading all information from the .debug_info section..."),
                                            get_mrange_length(&range));

        for (i = 0; i < runs_count; i++)
        {
            begin = i * run_size;

            if ((i + 1) == runs_count)
                end = count;
            else
                end = begin + run_size;

            work = g_seq_work_new_boolean(&data, begin, end, msg,
                                          (seq_work_bool_cb)extract_dies_from_debug_info, &result);

            g_work_queue_schedule_work(queue, G_DELAYED_WORK(work), gid);

        }

        g_work_queue_wait_for_completion(queue, gid);

        gtk_status_stack_remove_activity(status, msg);

 exit:

        if (ranges != NULL)
            free(ranges);

        g_object_unref(G_OBJECT(restricted));

    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : data   = ensemble d'informations utiles à l'opération.       *
*                i      = indice des éléments à traiter.                      *
*                status = barre de statut à tenir informée.                   *
*                id     = identifiant du message affiché à l'utilisateur.     *
*                                                                             *
*  Description : Procède au chargement d'un DIE de la section debug_info.     *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool extract_dies_from_debug_info(const work_data *data, size_t i, GtkStatusStack *status, activity_id_t id)
{
    bool result;                            /* Bilan à retourner           */
    GDwarfFormat *format;                   /* Format en cours d'analyse   */
    GBinContent *content;                   /* Contenu binaire à lire      */
    GExeFormat *exe;                        /* Exécutable associé          */
    SourceEndian endian;                    /* Boutisme du format parent   */
    vmpa2t iter;                            /* Tête de lecture mouvante    */
    dw_compil_unit_header header;           /* Unité à cerner puis traiter */
    vmpa2t next;                            /* Départ de l'unité suivante  */
    dw_abbrev_brotherhood *abbrevs;         /* Série d'abréviations        */

    format = data->format;

    /**
     * Comme les informations peuvent aller taper ailleurs dans le binaire
     * (par exemple dans la section debug_str pour certaine valeur, on ne peut
     * pas restreinte le contenu au seul espace traité.
     *
     * L'en-tête lui même a déjà été valide, donc on ne s'embête pas à distinguer
     * différents cas ici.
     */

    content = G_BIN_FORMAT(format)->content;

    exe = G_DBG_FORMAT(format)->executable;

    endian = g_binary_format_get_endianness(G_BIN_FORMAT(exe));

    copy_vmpa(&iter, get_mrange_addr(&data->ranges[i]));

    result = read_dwarf_compil_unit_header(content, &iter, endian, &header, &next);
    if (!result) goto exit;

    abbrevs = load_all_dwarf_abbreviations(format, &header);
    if (abbrevs == NULL) goto exit;

    result = build_dwarf_die(format, content, &iter, &header, abbrevs, &format->info[i]);

    free_all_dwarf_abbreviations(abbrevs);

    gtk_status_stack_update_activity_value(status, id, get_mrange_length(&data->ranges[i]));

 exit:

    return result;

}