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
|
/* Chrysalide - Outil d'analyse de fichiers binaires
* program.c - annotation des en-têtes de programme de binaires ELF
*
* Copyright (C) 2015-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 "program.h"
#include <string.h>
#include <i18n.h>
#include <common/cpp.h>
#include <common/extstr.h>
#include <plugins/elf/elf-int.h>
#include <plugins/fmtp/parser.h>
/* Définition des champs */
static field_desc_switch _elf_prgm_types[] = {
{ .fixed = PT_NULL, .desc = __("Segment type: unused") },
{ .fixed = PT_LOAD, .desc = __("Segment type: loadable program segment") },
{ .fixed = PT_DYNAMIC, .desc = __("Segment type: dynamic linking information") },
{ .fixed = PT_INTERP, .desc = __("Segment type: program interpreter") },
{ .fixed = PT_NOTE, .desc = __("Segment type: auxiliary information") },
{ .fixed = PT_SHLIB, .desc = __("Segment type: reserved") },
{ .fixed = PT_PHDR, .desc = __("Segment type: entry for header table itself") },
{ .fixed = PT_TLS, .desc = __("Segment type: thread-local storage segment") },
{ .fixed = PT_GNU_EH_FRAME, .desc = __("Segment type: GCC .eh_frame_hdr segment") },
{ .fixed = PT_GNU_STACK, .desc = __("Segment type: indicates stack executability") },
{ .fixed = PT_GNU_RELRO, .desc = __("Segment type: read-only after relocation") },
{ .fixed = PT_SUNWSTACK, .desc = __("Segment type: Sun Stack segment") },
{ .lower = PT_LOSUNW, .upper = PT_HISUNW, .desc = __("Segment type: Sun specific segment") },
{ .lower = PT_LOOS, .upper = PT_HIOS, .desc = __("Segment type: OS-specific") },
{ .lower = PT_LOPROC, .upper = PT_HIPROC, .desc = __("Segment type: processor-specific") }
};
static fmt_field_def _elf_phdr_base[] = {
{
.name = "p_type",
.size = MDS_32_BITS,
.repeat = 1,
SWITCH_COMMENT(_elf_prgm_types, __("Segment type: unknown"))
}
};
static fmt_field_def _elf_phdr_32b_a[] = {
{
.name = "p_offset",
.size = MDS_32_BITS,
.repeat = 1,
PLAIN_COMMENT(__("Segment file offset"))
},
{
.name = "p_vaddr",
.size = MDS_32_BITS,
.repeat = 1,
PLAIN_COMMENT(__("Segment virtual address"))
},
{
.name = "p_paddr",
.size = MDS_32_BITS,
.repeat = 1,
PLAIN_COMMENT(__("Segment physical address"))
},
{
.name = "p_filesz",
.size = MDS_32_BITS,
.repeat = 1,
PLAIN_COMMENT(__("Segment size in file"))
},
{
.name = "p_memsz",
.size = MDS_32_BITS,
.repeat = 1,
PLAIN_COMMENT(__("Segment size in memory"))
}
};
static fmt_field_def _elf_phdr_32b_b[] = {
{
.name = "p_align",
.size = MDS_32_BITS,
.repeat = 1,
PLAIN_COMMENT(__("Segment alignment"))
}
};
static fmt_field_def _elf_phdr_64b[] = {
{
.name = "p_offset",
.size = MDS_64_BITS,
.repeat = 1,
PLAIN_COMMENT(__("Segment file offset"))
},
{
.name = "p_vaddr",
.size = MDS_64_BITS,
.repeat = 1,
PLAIN_COMMENT(__("Segment virtual address"))
},
{
.name = "p_paddr",
.size = MDS_64_BITS,
.repeat = 1,
PLAIN_COMMENT(__("Segment physical address"))
},
{
.name = "p_filesz",
.size = MDS_64_BITS,
.repeat = 1,
PLAIN_COMMENT(__("Segment size in file"))
},
{
.name = "p_memsz",
.size = MDS_64_BITS,
.repeat = 1,
PLAIN_COMMENT(__("Segment size in memory"))
},
{
.name = "p_align",
.size = MDS_64_BITS,
.repeat = 1,
PLAIN_COMMENT(__("Segment alignment"))
}
};
/* Charge tous les symboles liés à un en-tête de programme ELF. */
static bool annotate_elf_program_header(GElfFormat *, GPreloadInfo *, SourceEndian, vmpa2t *);
/******************************************************************************
* *
* Paramètres : format = description de l'exécutable à compléter. *
* info = informations à constituer en avance de phase. *
* endian = boutisme présentement utilisé. *
* pos = tête de lecture à déplacer. [OUT] *
* *
* Description : Charge tous les symboles liés à un en-tête de programme ELF. *
* *
* Retour : Bilan de l'opération. *
* *
* Remarques : - *
* *
******************************************************************************/
static bool annotate_elf_program_header(GElfFormat *format, GPreloadInfo *info, SourceEndian endian, vmpa2t *pos)
{
bool result; /* Bilan à retourner */
elf_phdr phdr; /* En-tête de programme ELF */
fmt_field_def flags_field; /* Définition des drapeaux */
char *rights; /* Reconstruction dynamique */
comment_part parts[2]; /* Mise en place des parties */
GBinFormat *bformat; /* Autre version du format */
result = read_elf_program_header(format, get_phy_addr(pos), &phdr);
if (!result)
goto aeph_exit;
/* Préparation de la partie des drapeaux */
memset(&flags_field, 0, sizeof(flags_field));
flags_field.name = "p_flags";
flags_field.size = MDS_32_BITS;
flags_field.repeat = 1;
rights = NULL;
if (ELF_PHDR(format, phdr, p_flags) & PF_R)
rights = stradd(rights, "R");
if (ELF_PHDR(format, phdr, p_flags) & PF_W)
rights = stradd(rights, "W");
if (ELF_PHDR(format, phdr, p_flags) & PF_X)
rights = stradd(rights, "X");
if (ELF_PHDR(format, phdr, p_flags) & PF_MASKOS)
rights = stradd(rights, "o");
if (ELF_PHDR(format, phdr, p_flags) & PF_MASKPROC)
rights = stradd(rights, "p");
if (rights == NULL)
{
flags_field.ctype = FCT_PLAIN;
flags_field.comment.plain = __("Segment flags: none");
}
else
{
parts[0].is_static = true;
parts[0].avoid_i18n = false;
parts[0].static_text = __("Segment flags: ");
parts[1].is_static = false;
parts[1].avoid_i18n = true;
parts[1].dynamic_text = rights;
flags_field.ctype = FCT_MULTI;
flags_field.comment.parts = parts;
flags_field.comment.pcount = ARRAY_SIZE(parts);
}
/* Interprétation générale */
bformat = G_BIN_FORMAT(format);
result = parse_field_definitions(PARSING_DEFS(_elf_phdr_base), bformat, info, pos, NULL);
if (format->is_32b)
{
if (result)
result = parse_field_definitions(PARSING_DEFS(_elf_phdr_32b_a), bformat, info, pos, NULL);
if (result)
result = parse_field_definitions(&flags_field, 1, bformat, info, pos, NULL);
if (result)
result = parse_field_definitions(PARSING_DEFS(_elf_phdr_32b_b), bformat, info, pos, NULL);
}
else
{
if (result)
result = parse_field_definitions(&flags_field, 1, bformat, info, pos, NULL);
if (result)
result = parse_field_definitions(PARSING_DEFS(_elf_phdr_64b), bformat, info, pos, NULL);
}
aeph_exit:
return result;
}
/******************************************************************************
* *
* Paramètres : format = description de l'exécutable à compléter. *
* info = informations à constituer en avance de phase. *
* status = barre de statut à tenir informée. *
* *
* Description : Charge tous les symboles liés aux en-têtes de programme ELF. *
* *
* Retour : Bilan de l'opération. *
* *
* Remarques : - *
* *
******************************************************************************/
bool annotate_elf_program_header_table(GElfFormat *format, GPreloadInfo *info, GtkStatusStack *status)
{
bool result; /* Bilan à retourner */
const elf_header *header; /* En-tête principale */
SourceEndian endian; /* Boutisme utilisé */
phys_t offset; /* Tête de lecture du bbinaire */
vmpa2t pos; /* Localisation des symboles */
uint16_t e_phnum; /* 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));
offset = ELF_HDR(format, *header, e_phoff);
if (!g_exe_format_translate_offset_into_vmpa(G_EXE_FORMAT(format), offset, &pos))
return false;
e_phnum = ELF_HDR(format, *header, e_phnum);
msg = gtk_status_stack_add_activity(status, _("Writing annotations for all Elf program headers..."), e_phnum);
for (i = 0; i < e_phnum && result; i++)
{
result = annotate_elf_program_header(format, info, endian, &pos);
gtk_status_stack_update_activity_value(status, msg, 1);
}
gtk_status_stack_remove_activity(status, msg);
return result;
}
|