summaryrefslogtreecommitdiff
path: root/src/format/dwarf/info.c
blob: 0dc5048f006995c97e4423a166f63ced198189bc (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
317
318
319
320
321
322
323
324

/* OpenIDA - Outil d'analyse de fichiers binaires
 * info.c - lecture des informations principales du format DWARF
 *
 * Copyright (C) 2008 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 "info.h"


#include <malloc.h>


#include "abbrev.h"
#include "dwarf-int.h"
#include "utils.h"


/* Informations utiles d'une unité de compilation */
typedef struct _compil_unit
{
    off_t endpos;                           /* Position d'unité suivante   */

    off_t offset;                           /* Position dans les abréviat° */
    uint8_t ptrsize;                        /* Taille des adresses mémoire */

} compil_unit;




/* Procède à la lecture d'une unité de compilation. */
bool read_dwarf_compilation_unit(dwarf_format *, off_t *, compil_unit *);

/* Enregistre toutes les déclarations de fonction trouvées. */
bool look_for_dwarf_subprograms(dwarf_format *, off_t *, const compil_unit *);





/******************************************************************************
*                                                                             *
*  Paramètres  : format = informations de débogage à compléter.               *
*                                                                             *
*  Description : Charge les informations trouvées dans un DWARF.              *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool load_dwarf_information(dwarf_format *format)
{
    bool result;                            /* Bilan à renvoyer            */




    off_t offset;
    off_t start;
    off_t size;

    bool test;

    int i;


    compil_unit cu;



    result = true;

    test = find_exe_section(DBG_FORMAT(format)->e_format, ".debug_info", &start, &size, NULL);

    offset = start;


    printf(" -> offset=%d   size=%d\n", offset, size);



    for (i = 0; i < size; i++)
    {
        if (i % 25 == 0) printf("\n");
        printf("0x%02hhx ", DBG_FORMAT(format)->content[offset + i]);
    }

    printf("\n");



    while (offset < (start + size) && result)
    {
        printf("-------------\n");

        result = read_dwarf_compilation_unit(format, &offset, &cu);

        if (result)
            look_for_dwarf_subprograms(format, &offset, &cu);

        //break;

    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = informations de débogage à effacer.                 *
*                                                                             *
*  Description : Décharge les informations trouvées dans un DWARF.            *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void unload_dwarf_information(dwarf_format *format)
{



}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = informations de débogage à compléter.               *
*                pos    = tête de lecture à mettre à jour. [OUT]              *
*                cu     = unité de compilation lue. [OUT]                     *
*                                                                             *
*  Description : Procède à la lecture d'une unité de compilation.             *
*                                                                             *
*  Retour      : true en cas de succès de la lecture, false sinon.            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool read_dwarf_compilation_unit(dwarf_format *format, off_t *pos, compil_unit *cu)
{
    bool result;                            /* Bilan à retourner           */
    off_t ulength;                          /* Taille de l'unité           */
    uint16_t version;                       /* Version du format DWARF     */
    off_t abbrev_pos;                       /* Position dans les abréviat° */
    uint8_t memsize;                        /* Taille des adresses mémoire */

    off_t oldpos;

    uint8_t index;



    result = true;


    if (read_unit_length(format, pos, &ulength))
        printf("Unit Length :: %d  (0x%x)\n", ulength, ulength);

    else printf("error ul\n");

    cu->endpos = *pos + ulength;

    oldpos = *pos;

    if (read_uhalf(format, pos, &version))
        printf("version :: %hd\n", version);

    else printf("error version\n");

    if (version > 3) return false;

    if (read_abbrev_offset(format, pos, &abbrev_pos))
        printf("abbrev offset :: %d\n", abbrev_pos);

    else printf("error abbrev offset\n");

    if (read_address_size(format, pos, &memsize))
        printf("mem size :: %hhd\n", memsize);

    else printf("error memsize\n");


    cu->offset = abbrev_pos;
    cu->ptrsize = memsize;


    printf(" =+> Next :: 0x%02hhx 0x%02hhx 0x%02hhx 0x%02hhx 0x%02hhx\n",
           DBG_FORMAT(format)->content[*pos],
           DBG_FORMAT(format)->content[*pos + 1],
           DBG_FORMAT(format)->content[*pos + 2],
           DBG_FORMAT(format)->content[*pos + 3],
           DBG_FORMAT(format)->content[*pos + 4]);



    /*
    *pos = oldpos + ulength;
    */


    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = informations de débogage à compléter.               *
*                pos    = tête de lecture à mettre à jour. [OUT]              *
*                cu     = unité de compilation courante.                      *
*                                                                             *
*  Description : Enregistre toutes les déclarations de fonction trouvées.     *
*                                                                             *
*  Retour      : true en cas de succès de la lecture, false sinon.            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool look_for_dwarf_subprograms(dwarf_format *format, off_t *pos, const compil_unit *cu)
{
    bool result;                            /* Bilan à retourner           */
    uint8_t index;                          /* Indice de l'abbréviation    */

    result = true;


    off_t oldpos;

    const dw_abbrev *abbrev;


    uint64_t low_pc;
    uint64_t high_pc;


    while (*pos < cu->endpos)
    {

        if (read_address_size/*leb128*/(format, pos, &index))
            printf("abbrev index :: %hhd\n", index);
        else printf("abbrev index error\n");

        /* Contraintes d'alignement... */
        if (index == 0) continue;


        abbrev = find_dwarf_abbreviations(format, &cu->offset, index);


        printf(" --> %p\n", abbrev);

        printf("    == 0x%02x (matched ? %d)\n", abbrev->tag, abbrev->tag == DWT_SUBPROGRAM);


        oldpos = *pos;

        if (abbrev->tag == DWT_SUBPROGRAM)
        {


        if (read_dwarf_abbrev_attribute(format, &oldpos, abbrev, DWA_LOW_PC, &low_pc))
            printf(" ## LOW PC :: 0x%08x\n", low_pc);
        else printf(" error: no low pc\n");

        oldpos = *pos;


        if (read_dwarf_abbrev_attribute(format, &oldpos, abbrev, DWA_HIGH_PC, &high_pc))
            printf(" ## HIGH PC :: 0x%08x\n", high_pc);
        else printf(" error: no high pc\n");


        }



        if (!skip_dwarf_abbrev(format, pos, abbrev))
            printf("error skipping :(\n");


        printf("    == progress %d\n", *pos - oldpos);
        printf("    == %d < %d\n", *pos, cu->endpos);


    }


    printf(" =+> Next :: 0x%02hhx 0x%02hhx 0x%02hhx 0x%02hhx 0x%02hhx\n",
           DBG_FORMAT(format)->content[*pos],
           DBG_FORMAT(format)->content[*pos + 1],
           DBG_FORMAT(format)->content[*pos + 2],
           DBG_FORMAT(format)->content[*pos + 3],
           DBG_FORMAT(format)->content[*pos + 4]);



    return result;

}