2 * Copyright (c) 2016 Martin Pieuchot <mpi@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/param.h>
18 #include <sys/types.h>
20 #include <sys/exec_elf.h>
39 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
42 #define SUNW_CTF ".SUNW_ctf"
44 #define DUMP_OBJECT (1 << 0)
45 #define DUMP_FUNCTION (1 << 1)
46 #define DUMP_HEADER (1 << 2)
47 #define DUMP_LABEL (1 << 3)
48 #define DUMP_STRTAB (1 << 4)
49 #define DUMP_STATISTIC (1 << 5)
50 #define DUMP_TYPE (1 << 6)
52 int dump(const char *, uint32_t);
53 int iself(const char *, size_t);
54 int isctf(const char *, size_t);
55 __dead void usage(void);
57 int ctf_dump(const char *, size_t, uint32_t);
58 unsigned int ctf_dump_type(struct ctf_header *, const char *, off_t,
59 unsigned int, unsigned int);
60 const char *ctf_kind2name(unsigned short);
61 const char *ctf_off2name(struct ctf_header *, const char *, off_t,
64 int elf_dump(const char *, size_t, uint32_t);
65 int elf_getshstrtab(const char *, size_t, const char **, size_t *);
66 int elf_getsymtab(const char *, const char *, size_t,
67 const Elf_Sym **, size_t *);
68 int elf_getstrtab(const char *, const char *, size_t,
69 const char **, size_t *);
72 char *decompress(const char *, size_t, off_t);
76 main(int argc, char *argv[])
82 setlocale(LC_ALL, "");
84 while ((ch = getopt(argc, argv, "dfhlst")) != -1) {
90 flags |= DUMP_FUNCTION;
112 while ((filename = *argv++) != NULL)
113 error |= dump(filename, flags);
119 dump(const char *path, uint32_t flags)
125 fd = open(path, O_RDONLY);
130 if (fstat(fd, &st) == -1) {
134 if (st.st_size < (off_t)sizeof(struct ctf_header)) {
135 warnx("file too small to be CTF");
138 if ((uintmax_t)st.st_size > SIZE_MAX) {
139 warnx("file too big to fit memory");
143 p = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
147 if (iself(p, st.st_size)) {
148 error = elf_dump(p, st.st_size, flags);
149 } else if (isctf(p, st.st_size)) {
150 error = ctf_dump(p, st.st_size, flags);
153 munmap(p, st.st_size);
160 iself(const char *p, size_t filesize)
162 Elf_Ehdr *eh = (Elf_Ehdr *)p;
164 if (eh->e_ehsize < sizeof(Elf_Ehdr) || !IS_ELF(*eh)) {
165 warnx("file is not ELF");
168 if (eh->e_ident[EI_CLASS] != ELFCLASS) {
169 warnx("unexpected word size %u", eh->e_ident[EI_CLASS]);
172 if (eh->e_ident[EI_VERSION] != ELF_TARG_VER) {
173 warnx("unexpected version %u", eh->e_ident[EI_VERSION]);
176 if (eh->e_ident[EI_DATA] >= ELFDATANUM) {
177 warnx("unexpected data format %u", eh->e_ident[EI_DATA]);
180 if (eh->e_shoff > filesize) {
181 warnx("bogus section table offset 0x%llx", eh->e_shoff);
184 if (eh->e_shentsize < sizeof(Elf_Shdr)) {
185 warnx("bogus section header size %u", eh->e_shentsize);
188 if (eh->e_shnum > (filesize - eh->e_shoff) / eh->e_shentsize) {
189 warnx("bogus section header count %u", eh->e_shnum);
192 if (eh->e_shstrndx >= eh->e_shnum) {
193 warnx("bogus string table index %u", eh->e_shstrndx);
201 elf_getshstrtab(const char *p, size_t filesize, const char **shstrtab,
202 size_t *shstrtabsize)
204 Elf_Ehdr *eh = (Elf_Ehdr *)p;
207 sh = (Elf_Shdr *)(p + eh->e_shoff + eh->e_shstrndx * eh->e_shentsize);
208 if (sh->sh_type != SHT_STRTAB) {
209 warnx("unexpected string table type");
212 if (sh->sh_offset > filesize) {
213 warnx("bogus string table offset");
216 if (sh->sh_size > filesize - sh->sh_offset) {
217 warnx("bogus string table size");
220 if (shstrtab != NULL)
221 *shstrtab = p + sh->sh_offset;
222 if (shstrtabsize != NULL)
223 *shstrtabsize = sh->sh_size;
229 elf_getsymtab(const char *p, const char *shstrtab, size_t shstrtabsize,
230 const Elf_Sym **symtab, size_t *nsymb)
232 Elf_Ehdr *eh = (Elf_Ehdr *)p;
236 for (i = 0; i < eh->e_shnum; i++) {
237 sh = (Elf_Shdr *)(p + eh->e_shoff + i * eh->e_shentsize);
239 if (sh->sh_type != SHT_SYMTAB)
242 if ((sh->sh_link >= eh->e_shnum) ||
243 (sh->sh_name >= shstrtabsize))
246 if (strncmp(shstrtab + sh->sh_name, ELF_SYMTAB,
247 strlen(ELF_SYMTAB)) == 0) {
249 *symtab = (Elf_Sym *)(p + sh->sh_offset);
251 *nsymb = (sh->sh_size / sh->sh_entsize);
261 elf_getstrtab(const char *p, const char *shstrtab, size_t shstrtabsize,
262 const char **strtab, size_t *strtabsize)
264 Elf_Ehdr *eh = (Elf_Ehdr *)p;
268 for (i = 0; i < eh->e_shnum; i++) {
269 sh = (Elf_Shdr *)(p + eh->e_shoff + i * eh->e_shentsize);
271 if (sh->sh_type != SHT_STRTAB)
274 if ((sh->sh_link >= eh->e_shnum) ||
275 (sh->sh_name >= shstrtabsize))
278 if (strncmp(shstrtab + sh->sh_name, ELF_STRTAB,
279 strlen(ELF_STRTAB)) == 0) {
281 *strtab = p + sh->sh_offset;
282 if (strtabsize != NULL)
283 *strtabsize = sh->sh_size;
293 const Elf_Sym *symtab;
294 size_t strtabsize, nsymb;
297 elf_idx2sym(size_t *idx, unsigned char type)
302 for (i = *idx + 1; i < nsymb; i++) {
305 if (ELF_ST_TYPE(st->st_info) != type)
309 return strtab + st->st_name;
316 elf_dump(const char *p, size_t filesize, uint32_t flags)
318 Elf_Ehdr *eh = (Elf_Ehdr *)p;
320 const char *shstrtab;
321 size_t i, shstrtabsize;
323 /* Find section header string table location and size. */
324 if (elf_getshstrtab(p, filesize, &shstrtab, &shstrtabsize))
327 /* Find symbol table location and number of symbols. */
328 if (elf_getsymtab(p, shstrtab, shstrtabsize, &symtab, &nsymb))
329 warnx("symbol table not found");
331 /* Find string table location and size. */
332 if (elf_getstrtab(p, shstrtab, shstrtabsize, &strtab, &strtabsize))
333 warnx("string table not found");
335 /* Find CTF section and dump it. */
336 for (i = 0; i < eh->e_shnum; i++) {
337 sh = (Elf_Shdr *)(p + eh->e_shoff + i * eh->e_shentsize);
339 if ((sh->sh_link >= eh->e_shnum) ||
340 (sh->sh_name >= shstrtabsize))
343 if (strncmp(shstrtab + sh->sh_name, SUNW_CTF, strlen(SUNW_CTF)))
346 if (!isctf(p + sh->sh_offset, sh->sh_size))
349 return ctf_dump(p + sh->sh_offset, sh->sh_size, flags);
352 warnx("%s section not found", SUNW_CTF);
357 isctf(const char *p, size_t filesize)
359 struct ctf_header *cth = (struct ctf_header *)p;
360 off_t dlen = cth->cth_stroff + cth->cth_strlen;
362 if (cth->cth_magic != CTF_MAGIC || cth->cth_version != CTF_VERSION)
365 if (dlen > filesize && !(cth->cth_flags & CTF_F_COMPRESS)) {
366 warnx("bogus file size");
370 if ((cth->cth_lbloff & 3) || (cth->cth_objtoff & 1) ||
371 (cth->cth_funcoff & 1) || (cth->cth_typeoff & 3)) {
372 warnx("wrongly aligned offset");
376 if ((cth->cth_lbloff >= dlen) || (cth->cth_objtoff >= dlen) ||
377 (cth->cth_funcoff >= dlen) || (cth->cth_typeoff >= dlen)) {
378 warnx("truncated file");
382 if ((cth->cth_lbloff > cth->cth_objtoff) ||
383 (cth->cth_objtoff > cth->cth_funcoff) ||
384 (cth->cth_funcoff > cth->cth_typeoff) ||
385 (cth->cth_typeoff > cth->cth_stroff)) {
386 warnx("corrupted file");
394 ctf_dump(const char *p, size_t size, uint32_t flags)
396 struct ctf_header *cth = (struct ctf_header *)p;
397 char *data = (char *)p;
398 off_t dlen = cth->cth_stroff + cth->cth_strlen;
400 if (cth->cth_flags & CTF_F_COMPRESS) {
401 data = decompress(p + sizeof(*cth), size - sizeof(*cth), dlen);
406 if (flags & DUMP_HEADER) {
407 printf("cth_magic = 0x%04x\n", cth->cth_magic);
408 printf("cth_version = %d\n", cth->cth_version);
409 printf("cth_flags = 0x%02x\n", cth->cth_flags);
410 printf("cth_parlabel = %s\n",
411 ctf_off2name(cth, data, dlen, cth->cth_parname));
412 printf("cth_parname = %s\n",
413 ctf_off2name(cth, data, dlen, cth->cth_parname));
414 printf("cth_lbloff = %d\n", cth->cth_lbloff);
415 printf("cth_objtoff = %d\n", cth->cth_objtoff);
416 printf("cth_funcoff = %d\n", cth->cth_funcoff);
417 printf("cth_typeoff = %d\n", cth->cth_typeoff);
418 printf("cth_stroff = %d\n", cth->cth_stroff);
419 printf("cth_strlen = %d\n", cth->cth_strlen);
422 if (flags & DUMP_LABEL) {
423 unsigned int lbloff = cth->cth_lbloff;
424 struct ctf_lblent *ctl;
426 while (lbloff < cth->cth_objtoff) {
427 ctl = (struct ctf_lblent *)(data + lbloff);
429 printf("%5u %s\n", ctl->ctl_typeidx,
430 ctf_off2name(cth, data, dlen, ctl->ctl_label));
432 lbloff += sizeof(*ctl);
436 if (flags & DUMP_OBJECT) {
437 unsigned int objtoff = cth->cth_objtoff;
438 size_t idx = 0, i = 0;
443 while (objtoff < cth->cth_funcoff) {
444 dsp = (unsigned short *)(data + objtoff);
446 l = printf("[%zu] %u", i++, *dsp);
447 if ((s = elf_idx2sym(&idx, STT_OBJECT)) != NULL)
448 printf("%*s %s (%zu)\n", (12 - l), "", s, idx);
452 objtoff += sizeof(*dsp);
456 if (flags & DUMP_FUNCTION) {
457 unsigned short *fsp, kind, vlen;
458 size_t idx = 0, i = 0;
462 fsp = (unsigned short *)(data + cth->cth_funcoff);
463 while (fsp < (unsigned short *)(data + cth->cth_typeoff)) {
464 kind = CTF_INFO_KIND(*fsp);
465 vlen = CTF_INFO_VLEN(*fsp);
468 if (kind == CTF_K_UNKNOWN && vlen == 0)
471 l = printf("%u [%zu] FUNC", vlen, i++);
472 if ((s = elf_idx2sym(&idx, STT_FUNC)) != NULL)
474 printf(" returns: %u args: (", *fsp++);
476 printf("%u%s", *fsp++, (vlen > 0) ? ", " : "");
481 if (flags & DUMP_STRTAB) {
482 unsigned int offset = 0;
485 while (offset < cth->cth_strlen) {
486 str = data + cth->cth_stroff + offset;
488 printf("[%u] ", offset);
490 offset += printf("%s\n", str);
498 if (flags & DUMP_TYPE) {
499 unsigned int idx = 1, offset = 0;
501 while (offset < cth->cth_stroff)
502 offset += ctf_dump_type(cth, data, dlen, offset, idx++);
506 if (cth->cth_flags & CTF_F_COMPRESS)
513 ctf_dump_type(struct ctf_header *cth, const char *data, off_t dlen,
514 unsigned int offset, unsigned int idx)
516 const struct ctf_type *ctt;
517 unsigned short kind, vlen, root;
518 unsigned int toff, tlen = 0;
520 const char *name, *kname;
522 ctt = (struct ctf_type *)(data + cth->cth_typeoff + offset);
523 kind = CTF_INFO_KIND(ctt->ctt_info);
524 vlen = CTF_INFO_VLEN(ctt->ctt_info);
525 root = CTF_INFO_ISROOT(ctt->ctt_info);
526 name = ctf_off2name(cth, data, dlen, ctt->ctt_name);
529 printf("<%u> ", idx);
531 printf("[%u] ", idx);
533 if ((kname = ctf_kind2name(kind)) != NULL)
534 printf("%s %s", kname, name);
536 if (ctt->ctt_size <= CTF_MAX_SIZE) {
537 size = ctt->ctt_size;
538 toff = sizeof(struct ctf_stype);
540 size = CTF_TYPE_LSIZE(ctt);
541 toff = sizeof(struct ctf_type);
549 tlen = sizeof(unsigned int);
554 tlen = sizeof(struct ctf_array);
557 tlen = (vlen + (vlen & 1)) * sizeof(unsigned short);
561 printf(" (%llu bytes)", size);
562 if (size < CTF_LSTRUCT_THRESH)
563 tlen = vlen * sizeof(struct ctf_member);
565 tlen = vlen * sizeof(struct ctf_lmember);
568 tlen = vlen * sizeof(struct ctf_enum);
571 vlen = sizeof(unsigned int);
577 printf(" refers to %u", ctt->ctt_type);
580 errx(1, "incorrect type %u at offset %u", kind, offset);
589 ctf_kind2name(unsigned short kind)
591 static const char *kind_name[] = { NULL, "INTEGER", "FLOAT", "POINTER",
592 "ARRAYS", "FUNCTION", "STRUCT", "UNION", "ENUM", "FORWARD",
593 "TYPEDEF", "VOLATILE", "CONST", "RESTRICT" };
595 if (kind >= nitems(kind_name))
598 return kind_name[kind];
602 ctf_off2name(struct ctf_header *cth, const char *data, off_t dlen,
607 if (CTF_NAME_STID(offset) != CTF_STRTAB_0)
610 if (CTF_NAME_OFFSET(offset) >= cth->cth_strlen)
611 return "exceeds strlab";
613 if (cth->cth_stroff + CTF_NAME_OFFSET(offset) >= dlen)
616 name = data + cth->cth_stroff + CTF_NAME_OFFSET(offset);
624 decompress(const char *buf, size_t size, off_t len)
637 memset(&stream, 0, sizeof(stream));
638 stream.next_in = (void *)buf;
639 stream.avail_in = size;
640 stream.next_out = data;
641 stream.avail_out = len;
643 if ((error = inflateInit(&stream)) != Z_OK) {
644 warnx("zlib inflateInit failed: %s", zError(error));
648 if ((error = inflate(&stream, Z_FINISH)) != Z_STREAM_END) {
649 warnx("zlib inflate failed: %s", zError(error));
653 if ((error = inflateEnd(&stream)) != Z_OK) {
654 warnx("zlib inflateEnd failed: %s", zError(error));
658 if (stream.total_out != len) {
659 warnx("decompression failed: %llu != %llu",
660 stream.total_out, len);
675 extern char *__progname;
677 fprintf(stderr, "usage: %s [-dfhlst] [file ...]\n",