summaryrefslogtreecommitdiff
path: root/plugins/dwarf/symbols.c
blob: 27a897a48a2626c839a4c9734c2cbf144013e00b (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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316

/* Chrysalide - Outil d'analyse de fichiers binaires
 * symbols.c - gestion des symboles d'un DWARF
 *
 * Copyright (C) 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 "symbols.h"


#include "die.h"
#include "dwarf-int.h"
#include "form.h"
#include "../../core/params.h"
#include "../../mangling/demangler.h"



/* Charge les informations d'une routine en tant que symbole. */
static bool load_routine_as_symbol_from_dwarf(GDwarfFormat *, const dw_die *, const dw_abbrev *, bool);

/* Charge les informations d'un objet en tant que symbole. */
static bool load_object_as_symbol_from_dwarf(GDwarfFormat *, const dw_die *, const dw_abbrev *, bool);



/******************************************************************************
*                                                                             *
*  Paramètres  : format   = description de l'exécutable à compléter.          *
*                die      = entrée d'informations de débogage à utiliser.     *
*                abbrev   = abréviation déjà chargée sur laquelle s'appuyer.  *
*                use_virt = oriente le choix de la distinction ultime.        *
*                                                                             *
*  Description : Charge les informations d'une routine en tant que symbole.   *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool load_routine_as_symbol_from_dwarf(GDwarfFormat *format, const dw_die *die, const dw_abbrev *abbrev, bool use_virt)
{
    DwarfForm form;                         /* Type d'une valeur d'attribut*/
    const dw_form_value *value;             /* Valeur concrète d'attribut  */
    virt_t virt;                            /* Adresse virtuelle de départ */
    bool status;                            /* Bilan d'une récupération    */
    virt_t len;                             /* Taille de la zone couverte  */
    vmpa2t addr;                            /* Localisation complète       */
    mrange_t range;                         /* Espace de couverture total  */
    const char *name;                       /* Désignation humaine         */
    char alt_name[6 + VMPA_MAX_LEN];        /* Nom abstrait de substitution*/
    GBinRoutine *routine;                   /* Nouvelle routine trouvée    */
    GBinSymbol *symbol;                     /* Nouveau symbole construit   */

    /* Surface couverte */

    value = dw_die_peek_extended_value(die, DW_AT_low_pc, &form);
    if (value == NULL) goto lrasfd_bad_start;

    status = translate_form_into_address(value, form, &virt);
    if (!status) goto lrasfd_bad_start;

    value = dw_die_peek_extended_value(die, DW_AT_high_pc, &form);
    if (value == NULL) goto lrasfd_bad_start;

    status &= translate_form_into_address(value, form, &len);
    if (!status) goto lrasfd_bad_start;

    if (!g_exe_format_translate_address_into_vmpa(G_DBG_FORMAT(format)->executable, virt, &addr))
        init_vmpa(&addr, VMPA_NO_PHYSICAL, virt);

    init_mrange(&range, &addr, len);

    /* Désignation humaine */

    value = dw_die_peek_extended_value(die, DW_AT_name, &form);
    if (value == NULL) goto lrasfd_bad_name;

    name = translate_form_into_string(value, form);

    if (name == NULL)
    {
        strcpy(alt_name, "func_");

        if (use_virt)
            vmpa2_virt_to_string(&addr, MDS_UNDEFINED, alt_name + 5, NULL);
        else
            vmpa2_phys_to_string(&addr, MDS_UNDEFINED, alt_name + 5, NULL);

        name = alt_name;

    }

    /* Intégration en bonne et due forme */

    routine = g_binary_format_decode_routine(G_BIN_FORMAT(format), name);
    symbol = G_BIN_SYMBOL(routine);

    g_binary_symbol_set_range(symbol, &range);

    g_binary_format_add_symbol(G_BIN_FORMAT(format), symbol);




    printf(" --> [valid ?= %d] start @ 0x%08llx\n", status, virt);
    printf(" --> [valid ?= %d]   len = 0x%08llx\n", status, len);
    printf(" --> [valid ?= %d]  name = '%s'\n", status, name);


    return true;

 lrasfd_bad_start:
 lrasfd_bad_name:

    return false;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format   = description de l'exécutable à compléter.          *
*                die      = entrée d'informations de débogage à utiliser.     *
*                abbrev   = abréviation déjà chargée sur laquelle s'appuyer.  *
*                use_virt = oriente le choix de la distinction ultime.        *
*                                                                             *
*  Description : Charge les informations d'un objet en tant que symbole.      *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool load_object_as_symbol_from_dwarf(GDwarfFormat *format, const dw_die *die, const dw_abbrev *abbrev, bool use_virt)
{
    DwarfForm form;                         /* Type d'une valeur d'attribut*/
    const dw_form_value *value;             /* Valeur concrète d'attribut  */
    virt_t virt;                            /* Adresse virtuelle de départ */
    bool status;                            /* Bilan d'une récupération    */
    virt_t len;                             /* Taille de la zone couverte  */
    vmpa2t addr;                            /* Localisation complète       */
    mrange_t range;                         /* Espace de couverture total  */
    const char *name;                       /* Désignation humaine         */
    char alt_name[5 + VMPA_MAX_LEN];        /* Nom abstrait de substitution*/
    GBinRoutine *routine;                   /* Nouvelle routine trouvée    */
    GBinSymbol *symbol;                     /* Nouveau symbole construit   */

    /* Surface couverte */






    /*
    value = dw_die_peek_extended_value(die, DW_AT_low_pc, &form);
    if (value == NULL) goto lrasfd_bad_start;

    status = translate_form_into_address(value, form, &virt);
    if (!status) goto lrasfd_bad_start;

    value = dw_die_peek_extended_value(die, DW_AT_high_pc, &form);
    if (value == NULL) goto lrasfd_bad_start;

    status &= translate_form_into_address(value, form, &len);
    if (!status) goto lrasfd_bad_start;

    if (!g_exe_format_translate_address_into_vmpa(G_DBG_FORMAT(format)->executable, virt, &addr))
        init_vmpa(&addr, VMPA_NO_PHYSICAL, virt);

    init_mrange(&range, &addr, len);
    */





    /* Désignation humaine */

    value = dw_die_peek_extended_value(die, DW_AT_name, &form);
    if (value == NULL) goto lrasfd_bad_name;

    name = translate_form_into_string(value, form);

    if (name == NULL)
    {
        strcpy(alt_name, "obj_");

        if (use_virt)
            vmpa2_virt_to_string(&addr, MDS_UNDEFINED, alt_name + 5, NULL);
        else
            vmpa2_phys_to_string(&addr, MDS_UNDEFINED, alt_name + 5, NULL);

        name = alt_name;

    }

    /* Intégration en bonne et due forme */

    /*
    routine = try_to_demangle_routine(name);

    g_binary_symbol_set_range(G_BIN_SYMBOL(routine), &range);

    symbol = g_binary_symbol_new(NULL, STP_OBJECT);
    g_binary_symbol_attach_routine(symbol, routine);

    g_binary_format_add_symbol(G_BIN_FORMAT(format), symbol);
    */



    //printf(" --> [valid ?= %d] start @ 0x%08llx\n", status, virt);
    //printf(" --> [valid ?= %d]   len = 0x%08llx\n", status, len);
    //printf(" --> [valid ?= %d]  name = '%s'\n", status, name);


    return true;

 lrasfd_bad_start:
 lrasfd_bad_name:

    return false;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à compléter.            *
*                                                                             *
*  Description : Charge en mémoire la liste humaine des symboles.             *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool load_dwarf_symbols(GDwarfFormat *format)
{
    bool result;                            /* Bilan à retourner           */
    bool no_name;                           /* Choix de construction de nom*/

    typedef struct _die_visit_info
    {
        GDwarfFormat *format;
        bool use_virt;

    } die_visit_info;

    die_visit_info vinfo;                   /* Information pour visiteur   */


    bool catch_dwarf_symbol(const dw_die *die, die_visit_info *info)
    {
        const dw_abbrev *abbrev;            /* Lien vers la représentation */
        DwarfTag tag;                       /* Etiquette à analyser        */
        bool status;                        /* Bilan d'un chargement       */

        abbrev = dw_die_get_abbrev(die);
        tag = dwarf_abbreviation_get_tag(abbrev);

        switch (tag)
        {
            case DW_TAG_subprogram:
                printf(" DIE ==> %p -> %p // tag = %x\n", die, abbrev, tag);
                status = load_routine_as_symbol_from_dwarf(info->format, die, abbrev, info->use_virt);
                break;

            case DW_TAG_variable:
                printf(" DIE ==> %p -> %p // tag = %x\n", die, abbrev, tag);
                status = load_object_as_symbol_from_dwarf(info->format, die, abbrev, info->use_virt);
                break;

            default:
                status = true;
                break;

        }

        return status;

    }


    if (!g_generic_config_get_value(get_main_configuration(), MPK_FORMAT_NO_NAME, &no_name))
        return false;

    vinfo.format = format;
    vinfo.use_virt = no_name;

    result = dw_die_visit(format->info_die, (visit_dies_fc)catch_dwarf_symbol, &vinfo);

    return result;

}