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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
|
/* $OpenBSD: ctfconv.c,v 1.17 2018/08/08 20:15:17 mestre Exp $ */
/*
* Copyright (c) 2016-2017 Martin Pieuchot
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/queue.h>
#include <sys/tree.h>
#include <sys/ctf.h>
#include <assert.h>
#include <elf.h>
#include <err.h>
#include <fcntl.h>
#include <locale.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "itype.h"
#include "xmalloc.h"
#ifndef nitems
#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
#endif
#define DEBUG_ABBREV ".debug_abbrev"
#define DEBUG_INFO ".debug_info"
#define DEBUG_LINE ".debug_line"
#define DEBUG_STR ".debug_str"
__dead void usage(void);
int convert(const char *);
int generate(const char *, const char *, int);
int elf_convert(char *, size_t);
void elf_sort(void);
struct itype *find_symb(struct itype *, size_t);
void dump_type(struct itype *);
void dump_func(struct itype *, int *);
void dump_obj(struct itype *, int *);
/* elf.c */
int iself(const char *, size_t);
int elf_getshstab(const char *, size_t, const char **, size_t *);
ssize_t elf_getsymtab(const char *, size_t, const char *, size_t,
const Elf_Sym **, size_t *, const char **, size_t *);
ssize_t elf_getsection(char *, size_t, const char *, const char *,
size_t, const char **, size_t *);
/* parse.c */
void dwarf_parse(const char *, size_t, const char *, size_t);
const char *ctf_enc2name(unsigned short);
/* lists of parsed types and functions */
struct itype_queue itypeq = TAILQ_HEAD_INITIALIZER(itypeq);
struct itype_queue ifuncq = TAILQ_HEAD_INITIALIZER(ifuncq);
struct itype_queue iobjq = TAILQ_HEAD_INITIALIZER(iobjq);
__dead void
usage(void)
{
fprintf(stderr, "usage: %s [-d] -l label -o outfile file\n",
getprogname());
exit(1);
}
int
main(int argc, char *argv[])
{
const char *filename, *label = NULL, *outfile = NULL;
int dump = 0;
int ch, error = 0;
struct itype *it;
setlocale(LC_ALL, "");
while ((ch = getopt(argc, argv, "dl:o:")) != -1) {
switch (ch) {
case 'd':
dump = 1; /* ctfdump(1)-like SUNW_ctf sections */
break;
case 'l':
if (label != NULL)
usage();
label = optarg;
break;
case 'o':
if (outfile != NULL)
usage();
outfile = optarg;
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc != 1)
usage();
/* Either dump the sections, or write it out. */
if ((dump && (outfile != NULL || label != NULL)) ||
(!dump && (outfile == NULL || label == NULL)))
usage();
filename = *argv;
if (unveil(filename, "r") == -1)
err(1, "unveil");
if (outfile != NULL) {
if (unveil(outfile, "wc") == -1)
err(1, "unveil");
}
if (pledge("stdio rpath wpath cpath", NULL) == -1)
err(1, "pledge");
error = convert(filename);
if (error != 0)
return error;
if (outfile != NULL) {
if (pledge("stdio wpath cpath", NULL) == -1)
err(1, "pledge");
error = generate(outfile, label, 1);
if (error != 0)
return error;
}
if (dump) {
if (pledge("stdio", NULL) == -1)
err(1, "pledge");
int fidx = -1, oidx = -1;
TAILQ_FOREACH(it, &iobjq, it_symb)
dump_obj(it, &oidx);
printf("\n");
TAILQ_FOREACH(it, &ifuncq, it_symb)
dump_func(it, &fidx);
printf("\n");
TAILQ_FOREACH(it, &itypeq, it_next) {
if (it->it_flags & (ITF_FUNC|ITF_OBJ))
continue;
dump_type(it);
}
return 0;
}
return 0;
}
int
convert(const char *path)
{
struct stat st;
int fd, error = 1;
char *p;
fd = open(path, O_RDONLY);
if (fd == -1) {
warn("open %s", path);
return 1;
}
if (fstat(fd, &st) == -1) {
warn("fstat %s", path);
close(fd);
return 1;
}
if ((uintmax_t)st.st_size > SIZE_MAX) {
warnx("file too big to fit memory");
close(fd);
return 1;
}
p = mmap(NULL, st.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
if (p == MAP_FAILED)
err(1, "mmap");
if (iself(p, st.st_size))
error = elf_convert(p, st.st_size);
munmap(p, st.st_size);
close(fd);
return error;
}
const char *dstrbuf;
size_t dstrlen;
const char *strtab;
const Elf_Sym *symtab;
size_t strtabsz, nsymb;
int
elf_convert(char *p, size_t filesize)
{
const char *shstab;
const char *infobuf, *abbuf;
size_t infolen, ablen;
size_t shstabsz;
/* Find section header string table location and size. */
if (elf_getshstab(p, filesize, &shstab, &shstabsz))
return 1;
/* Find symbol table and associated string table. */
if (elf_getsymtab(p, filesize, shstab, shstabsz, &symtab, &nsymb,
&strtab, &strtabsz) == -1)
warnx("symbol table not found");
/* Find abbreviation location and size. */
if (elf_getsection(p, filesize, DEBUG_ABBREV, shstab, shstabsz, &abbuf,
&ablen) == -1) {
warnx("%s section not found", DEBUG_ABBREV);
return 1;
}
if (elf_getsection(p, filesize, DEBUG_INFO, shstab, shstabsz, &infobuf,
&infolen) == -1) {
warnx("%s section not found", DEBUG_INFO);
return 1;
}
/* Find string table location and size. */
if (elf_getsection(p, filesize, DEBUG_STR, shstab, shstabsz, &dstrbuf,
&dstrlen) == -1)
warnx("%s section not found", DEBUG_STR);
dwarf_parse(infobuf, infolen, abbuf, ablen);
/* Sort functions */
elf_sort();
return 0;
}
struct itype *
find_symb(struct itype *tmp, size_t stroff)
{
struct itype *it;
char *sname, *p;
if (strtab == NULL || stroff >= strtabsz)
return NULL;
/*
* Skip local suffix
*
* FIXME: only skip local copies.
*/
sname = xstrdup(strtab + stroff);
if ((p = strtok(sname, ".")) == NULL) {
free(sname);
return NULL;
}
strlcpy(tmp->it_name, p, ITNAME_MAX);
free(sname);
it = RB_FIND(isymb_tree, &isymbt, tmp);
/* Restore original name */
if (it == NULL)
strlcpy(tmp->it_name, (strtab + stroff), ITNAME_MAX);
return it;
}
void
elf_sort(void)
{
struct itype *it, tmp;
size_t i;
memset(&tmp, 0, sizeof(tmp));
for (i = 0; i < nsymb; i++) {
const Elf_Sym *st = &symtab[i];
if (st->st_shndx == SHN_UNDEF || st->st_shndx == SHN_COMMON)
continue;
switch (ELF_ST_TYPE(st->st_info)) {
case STT_FUNC:
tmp.it_flags = ITF_FUNC;
break;
case STT_OBJECT:
tmp.it_flags = ITF_OBJ;
break;
default:
continue;
}
it = find_symb(&tmp, st->st_name);
if (it == NULL) {
/* Insert 'unknown' entry to match symbol order. */
it = it_dup(&tmp);
it->it_refp = it;
#ifdef DEBUG
warnx("symbol not found: %s", it_name(it));
#endif
}
if (it->it_flags & ITF_INSERTED) {
#ifdef DEBUG
warnx("%s: already inserted", it_name(it));
#endif
it = it_dup(it);
}
/* Save symbol index for dump. */
it->it_ref = i;
it->it_flags |= ITF_INSERTED;
if (it->it_flags & ITF_FUNC)
TAILQ_INSERT_TAIL(&ifuncq, it, it_symb);
else
TAILQ_INSERT_TAIL(&iobjq, it, it_symb);
}
}
const char *
type_name(struct itype *it)
{
const char *name;
name = it_name(it);
if (name == NULL)
return "(anon)";
return name;
}
/* Display parsed types a la ctfdump(1) */
void
dump_type(struct itype *it)
{
struct imember *im;
#ifdef DEBUG
switch (it->it_type) {
case CTF_K_POINTER:
case CTF_K_TYPEDEF:
case CTF_K_VOLATILE:
case CTF_K_CONST:
case CTF_K_RESTRICT:
case CTF_K_ARRAY:
case CTF_K_FUNCTION:
if (it->it_refp == NULL) {
printf("unresolved: %s type=%d\n", it_name(it),
it->it_type);
return;
}
default:
break;
}
#endif
switch (it->it_type) {
case CTF_K_FLOAT:
case CTF_K_INTEGER:
printf(" [%u] %s %s encoding=%s offset=0 bits=%u\n",
it->it_idx,
(it->it_type == CTF_K_INTEGER) ? "INTEGER" : "FLOAT",
it_name(it), ctf_enc2name(it->it_enc), it->it_size);
break;
case CTF_K_POINTER:
printf(" <%u> POINTER %s refers to %u\n", it->it_idx,
type_name(it), it->it_refp->it_idx);
break;
case CTF_K_TYPEDEF:
printf(" <%u> TYPEDEF %s refers to %u\n",
it->it_idx, it_name(it), it->it_refp->it_idx);
break;
case CTF_K_VOLATILE:
printf(" <%u> VOLATILE %s refers to %u\n", it->it_idx,
type_name(it), it->it_refp->it_idx);
break;
case CTF_K_CONST:
printf(" <%u> CONST %s refers to %u\n", it->it_idx,
type_name(it), it->it_refp->it_idx);
break;
case CTF_K_RESTRICT:
printf(" <%u> RESTRICT %s refers to %u\n", it->it_idx,
it_name(it), it->it_refp->it_idx);
break;
case CTF_K_ARRAY:
printf(" [%u] ARRAY %s content: %u index: %u nelems: %u\n",
it->it_idx, type_name(it), it->it_refp->it_idx, long_tidx,
it->it_nelems);
printf("\n");
break;
case CTF_K_STRUCT:
case CTF_K_UNION:
printf(" [%u] %s %s (%u bytes)\n", it->it_idx,
(it->it_type == CTF_K_STRUCT) ? "STRUCT" : "UNION",
type_name(it), it->it_size);
TAILQ_FOREACH(im, &it->it_members, im_next) {
printf("\t%s type=%u off=%zu\n",
(im_name(im) == NULL) ? "unknown" : im_name(im),
im->im_refp ? im->im_refp->it_idx : 0, im->im_off);
}
printf("\n");
break;
case CTF_K_ENUM:
printf(" [%u] ENUM %s\n", it->it_idx, type_name(it));
TAILQ_FOREACH(im, &it->it_members, im_next) {
printf("\t%s = %zu\n", im_name(im), im->im_ref);
}
printf("\n");
break;
case CTF_K_FUNCTION:
printf(" [%u] FUNCTION (%s) returns: %u args: (",
it->it_idx, (it_name(it) != NULL) ? it_name(it) : "anon",
it->it_refp->it_idx);
TAILQ_FOREACH(im, &it->it_members, im_next) {
printf("%u%s", im->im_refp->it_idx,
TAILQ_NEXT(im, im_next) ? ", " : "");
}
printf(")\n");
break;
default:
assert(0 == 1);
}
}
void
dump_func(struct itype *it, int *idx)
{
struct imember *im;
(*idx)++;
if (it->it_type == CTF_K_UNKNOWN && it->it_nelems == 0)
return;
printf(" [%u] FUNC (%s) returns: %u args: (", (*idx),
(it_name(it) != NULL) ? it_name(it) : "unknown",
it->it_refp->it_idx);
TAILQ_FOREACH(im, &it->it_members, im_next) {
printf("%u%s", im->im_refp->it_idx,
TAILQ_NEXT(im, im_next) ? ", " : "");
}
printf(")\n");
}
void
dump_obj(struct itype *it, int *idx)
{
int l;
(*idx)++;
l = printf(" [%u] %u", (*idx), it->it_refp->it_idx);
printf("%*s %s (%llu)\n", 14 - l, "", it_name(it), it->it_ref);
}
const char *
ctf_enc2name(unsigned short enc)
{
static const char *enc_name[] = { "SIGNED", "CHAR", "SIGNED CHAR",
"BOOL", "SIGNED BOOL" };
static char invalid[7];
if (enc == CTF_INT_VARARGS)
return "VARARGS";
if (enc > 0 && enc < nitems(enc_name))
return enc_name[enc - 1];
snprintf(invalid, sizeof(invalid), "0x%x", enc);
return invalid;
}
|