summaryrefslogtreecommitdiff
path: root/plugins/readelf/section.c
blob: ed22069e409b256e172bb8b0773227299261918f (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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435

/* Chrysalide - Outil d'analyse de fichiers binaires
 * header.c - annotation des en-têtes de section de binaires ELF
 *
 * Copyright (C) 2015 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "section.h"


#include <i18n.h>
#include <arch/raw.h>
#include <common/extstr.h>
#include <format/symbol.h>
#include <format/elf/elf-int.h>
#include <format/elf/section.h>



/* Charge tous les symboles liés à un en-tête de section ELF. */
static bool annotate_elf_section_header(GElfFormat *, SourceEndian, const elf_shdr *, vmpa2t *);



/******************************************************************************
*                                                                             *
*  Paramètres  : format  = description de l'exécutable à compléter.           *
*                endian  = boutisme présentement utilisé.                     *
*                strings = section renvoyant vers des chaînes de caractères.  *
*                pos     = tête de lecture à déplacer. [OUT]                  *
*                                                                             *
*  Description : Charge tous les symboles liés à un en-tête de section ELF.   *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool annotate_elf_section_header(GElfFormat *format, SourceEndian endian, const elf_shdr *strings, vmpa2t *pos)
{
    elf_shdr shdr;                          /* En-tête de programme ELF    */
    GBinContent *content;                   /* Contenu binaire à lire      */
    const char *secname;                    /* Nom d'une section analysée  */
    ImmOperandDisplay disp;                 /* Afficahge de valeur         */
    const char *text;                       /* Texte constant à insérer    */
    GArchInstruction *instr;                /* Instruction décodée         */
    GArchOperand *operand;                  /* Opérande à venir modifier   */
    GDbComment *comment;                    /* Définition de commentaire   */
    GBinSymbol *symbol;                     /* Symbole à intégrer          */
    char *dtext;                            /* Texte dynamique à créer     */
    bool filled;                            /* Suivi de mise en place      */

    if (!read_elf_section_header(format, get_phy_addr(pos), &shdr))
        return false;

    content = g_binary_format_get_content(G_BIN_FORMAT(format));

    /* Champ "sh_name" */

    secname = extract_name_from_elf_string_section(format, strings,
                                                   ELF_SHDR(format, shdr, sh_name));

    if (secname == NULL)
        dtext = strdup(_("Section name: <invalid>"));
    else
    {
        dtext = strdup(_("Section name: '"));
        dtext = stradd(dtext, secname);
        dtext = stradd(dtext, "'");
    }

    instr = g_raw_instruction_new_array(content, MDS_32_BITS, 1, pos, endian);

    SET_IMM_DISPLAY(instr, operand, 0, IOD_DEC);

    ADD_RAW_AS_SYM(format, symbol, instr, comment, dtext);

    free(dtext);

    /* Champ "sh_type" */

    disp = IOD_DEC;

    switch (ELF_SHDR(format, shdr, sh_type))
    {
        case SHT_NULL:
            text = _("Section type: unused");
            break;
        case SHT_PROGBITS:
            text = _("Section type: program data");
            break;
        case SHT_SYMTAB:
            text = _("Section type: symbol table");
            break;
        case SHT_STRTAB:
            text = _("Section type: string table");
            break;
        case SHT_RELA:
            text = _("Section type: relocation entries with addends");
            break;
        case SHT_HASH:
            text = _("Section type: symbol hash table");
            break;
        case SHT_DYNAMIC:
            text = _("Section type: dynamic linking information");
            break;
        case SHT_NOTE:
            text = _("Section type: notes");
            break;
        case SHT_NOBITS:
            text = _("Section type: program space with no data (bss)");
            break;
        case SHT_REL:
            text = _("Section type: relocation entries, no addends");
            break;
        case SHT_SHLIB:
            text = _("Section type: reserved");
            break;
        case SHT_DYNSYM:
            text = _("Section type: dynamic linker symbol table");
            break;
        case SHT_INIT_ARRAY:
            text = _("Section type: array of constructors");
            break;
        case SHT_FINI_ARRAY:
            text = _("Section type: array of destructors");
            break;
        case SHT_PREINIT_ARRAY:
            text = _("Section type: array of pre-constructors");
            break;
        case SHT_GROUP:
            text = _("Section type: section group");
            break;
        case SHT_SYMTAB_SHNDX:
            text = _("Section type: extended section indeces");
            break;
        case SHT_LOOS ... SHT_HIOS:
            disp = IOD_HEX;
            switch (ELF_SHDR(format, shdr, sh_type))
            {
                case SHT_GNU_ATTRIBUTES:
                    text = _("Section type: object attributes");
                    break;
                case SHT_GNU_HASH:
                    text = _("Section type: GNU-style hash table");
                    break;
                case SHT_GNU_LIBLIST:
                    text = _("Section type: prelink library list");
                    break;
                case SHT_CHECKSUM:
                    text = _("Section type: checksum for DSO content");
                    break;
                case SHT_LOSUNW ... SHT_HISUNW:
                    switch (ELF_SHDR(format, shdr, sh_type))
                    {
                        case SHT_SUNW_move:
                            text = _("Section type: SHT_SUNW_move");
                            break;
                        case SHT_SUNW_COMDAT:
                            text = _("Section type: SHT_SUNW_COMDAT");
                            break;
                        case SHT_SUNW_syminfo:
                            text = _("Section type: SHT_SUNW_syminfo");
                            break;
                        case SHT_GNU_verdef:
                            text = _("Section type: version definition section");
                            break;
                        case SHT_GNU_verneed:
                            text = _("Section type: version needs section");
                            break;
                        case SHT_GNU_versym:
                            text = _("Section type: version symbol table");
                            break;
                        default:
                            text = _("Section type: Sun-specific");
                            break;
                    }
                    break;
                default:
                    text = _("Section type: OS-specific");
                    break;
            }
            break;
        case SHT_LOPROC ... SHT_HIPROC:
            disp = IOD_HEX;
            text = _("Section type: processor-specific");
            break;
        case SHT_LOUSER ... SHT_HIUSER:
            disp = IOD_HEX;
            text = _("Section type: application-specific");
            break;
        default:
            disp = IOD_HEX;
            text = _("Section type: unknown");
            break;
    }

    instr = g_raw_instruction_new_array(content, MDS_32_BITS, 1, pos, endian);

    SET_IMM_DISPLAY(instr, operand, 0, disp);

    ADD_RAW_AS_SYM(format, symbol, instr, comment, text);

    /* Champ "sh_flags"... */

    dtext = strdup(_("Section flags: "));
    filled = false;

    if (ELF_SHDR(format, shdr, sh_type) & SHF_WRITE)
    {
        dtext = stradd(dtext, "W");
        filled = true;
    }

    if (ELF_SHDR(format, shdr, sh_type) & SHF_ALLOC)
    {
        dtext = stradd(dtext, "A");
        filled = true;
    }

    if (ELF_SHDR(format, shdr, sh_type) & SHF_EXECINSTR)
    {
        dtext = stradd(dtext, "X");
        filled = true;
    }

    if (ELF_SHDR(format, shdr, sh_type) & SHF_MERGE)
    {
        dtext = stradd(dtext, "M");
        filled = true;
    }

    if (ELF_SHDR(format, shdr, sh_type) & SHF_LINK_ORDER)
    {
        dtext = stradd(dtext, "L");
        filled = true;
    }

    if (ELF_SHDR(format, shdr, sh_type) & SHF_TLS)
    {
        dtext = stradd(dtext, "T");
        filled = true;
    }

    if (!filled)
        dtext = stradd(dtext, _("none"));

    if (format->is_32b)
    {
        /* Champ "sh_flags" (suite) */

        instr = g_raw_instruction_new_array(content, MDS_32_BITS, 1, pos, endian);

        ADD_RAW_AS_SYM(format, symbol, instr, comment, dtext);

        free(dtext);

        /* Champ "sh_addr" */

        instr = g_raw_instruction_new_array(content, MDS_32_BITS, 1, pos, endian);

        ADD_RAW_AS_SYM(format, symbol, instr, comment, _("Section virtual addr at execution"));

        /* Champ "sh_offset" */

        instr = g_raw_instruction_new_array(content, MDS_32_BITS, 1, pos, endian);

        ADD_RAW_AS_SYM(format, symbol, instr, comment, _("Section file offset"));

        /* Champ "sh_size" */

        instr = g_raw_instruction_new_array(content, MDS_32_BITS, 1, pos, endian);

        SET_IMM_DISPLAY(instr, operand, 0, IOD_DEC);

        ADD_RAW_AS_SYM(format, symbol, instr, comment, _("Section size in bytes"));

    }
    else
    {
        /* Champ "sh_flags" (suite) */

        instr = g_raw_instruction_new_array(content, MDS_64_BITS, 1, pos, endian);

        ADD_RAW_AS_SYM(format, symbol, instr, comment, dtext);

        free(dtext);

        /* Champ "sh_addr" */

        instr = g_raw_instruction_new_array(content, MDS_64_BITS, 1, pos, endian);

        ADD_RAW_AS_SYM(format, symbol, instr, comment, _("Section virtual addr at execution"));

        /* Champ "sh_offset" */

        instr = g_raw_instruction_new_array(content, MDS_64_BITS, 1, pos, endian);

        ADD_RAW_AS_SYM(format, symbol, instr, comment, _("Section file offset"));

        /* Champ "sh_size" */

        instr = g_raw_instruction_new_array(content, MDS_64_BITS, 1, pos, endian);

        SET_IMM_DISPLAY(instr, operand, 0, IOD_DEC);

        ADD_RAW_AS_SYM(format, symbol, instr, comment, _("Section size in bytes"));

    }

    /* Champ "sh_link" */

    instr = g_raw_instruction_new_array(content, MDS_32_BITS, 1, pos, endian);

    ADD_RAW_AS_SYM(format, symbol, instr, comment, _("Link to another section"));

    /* Champ "sh_info" */

    instr = g_raw_instruction_new_array(content, MDS_32_BITS, 1, pos, endian);

    ADD_RAW_AS_SYM(format, symbol, instr, comment, _("Additional section information"));

    if (format->is_32b)
    {
        /* Champ "sh_addralign" */

        instr = g_raw_instruction_new_array(content, MDS_32_BITS, 1, pos, endian);

        ADD_RAW_AS_SYM(format, symbol, instr, comment, _("Section alignment"));

        /* Champ "sh_entsize" */

        instr = g_raw_instruction_new_array(content, MDS_32_BITS, 1, pos, endian);

        SET_IMM_DISPLAY(instr, operand, 0, IOD_DEC);

        ADD_RAW_AS_SYM(format, symbol, instr, comment, _("Entry size if section holds table"));

    }
    else
    {
        /* Champ "sh_addralign" */

        instr = g_raw_instruction_new_array(content, MDS_64_BITS, 1, pos, endian);

        ADD_RAW_AS_SYM(format, symbol, instr, comment, _("Section alignment"));

        /* Champ "sh_entsize" */

        instr = g_raw_instruction_new_array(content, MDS_64_BITS, 1, pos, endian);

        SET_IMM_DISPLAY(instr, operand, 0, IOD_DEC);

        ADD_RAW_AS_SYM(format, symbol, instr, comment, _("Entry size if section holds table"));

    }

    g_object_unref(G_OBJECT(content));

    return true;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à compléter.            *
*                status = barre de statut à tenir informée.                   *
*                                                                             *
*  Description : Charge tous les symboles liés aux en-têtes de section ELF.   *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool annotate_elf_section_header_table(GElfFormat *format, GtkStatusStack *status)
{
    bool result;                            /* Bilan à retourner           */
    const elf_header *header;               /* En-tête principale          */
    SourceEndian endian;                    /* Boutisme utilisé            */
    elf_shdr strings;                       /* Section des descriptions    */
    off_t offset;                           /* Tête de lecture du binaire  */
    vmpa2t pos;                             /* Localisation des symboles   */
    uint16_t e_shnum;                       /* Nombre d'éléments 'Program' */
    activity_id_t msg;                      /* Message de progression      */
    uint16_t i;                             /* Boucle de parcours          */

    result = true;

    header = g_elf_format_get_header(format);
    endian = g_binary_format_get_endianness(G_BIN_FORMAT(format));

    if (!find_elf_section_by_index(format, ELF_HDR(format, *header, e_shstrndx), &strings))
        return false;

    offset = ELF_HDR(format, *header, e_shoff);

    init_vmpa(&pos, offset, 0x9900);

    e_shnum = ELF_HDR(format, *header, e_shnum);

    msg = gtk_status_stack_add_activity(status, _("Writing annotations for all Elf section headers..."), e_shnum);

    for (i = 0; i < e_shnum && result; i++)
    {
        result = annotate_elf_section_header(format, endian, &strings, &pos);

        gtk_status_stack_update_activity_value(status, msg, 1);

    }

    gtk_status_stack_remove_activity(status, msg);

    return true;

}