[BACK]Return to ctfdump.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ctfdump

Annotation of src/usr.bin/ctfdump/ctfdump.c, Revision 1.7

1.7     ! uwe         1: /*     $OpenBSD: ctfdump.c,v 1.6 2017/09/19 08:28:57 jsg Exp $ */
1.2       jasper      2:
1.1       mpi         3: /*
                      4:  * Copyright (c) 2016 Martin Pieuchot <mpi@openbsd.org>
                      5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18:
                     19: #include <sys/types.h>
                     20: #include <sys/stat.h>
                     21: #include <sys/exec_elf.h>
                     22: #include <sys/mman.h>
                     23: #include <sys/ctf.h>
                     24:
                     25: #include <err.h>
                     26: #include <fcntl.h>
                     27: #include <locale.h>
                     28: #include <stdio.h>
                     29: #include <stdint.h>
                     30: #include <stdlib.h>
                     31: #include <string.h>
                     32: #include <unistd.h>
                     33:
                     34: #ifdef ZLIB
                     35: #include <zlib.h>
                     36: #endif /* ZLIB */
                     37:
                     38: #ifndef nitems
                     39: #define nitems(_a)     (sizeof((_a)) / sizeof((_a)[0]))
                     40: #endif
                     41:
                     42: #define DUMP_OBJECT    (1 << 0)
                     43: #define DUMP_FUNCTION  (1 << 1)
                     44: #define DUMP_HEADER    (1 << 2)
                     45: #define DUMP_LABEL     (1 << 3)
                     46: #define DUMP_STRTAB    (1 << 4)
                     47: #define DUMP_STATISTIC (1 << 5)
                     48: #define DUMP_TYPE      (1 << 6)
                     49:
                     50: int             dump(const char *, uint8_t);
                     51: int             isctf(const char *, size_t);
                     52: __dead void     usage(void);
                     53:
                     54: int             ctf_dump(const char *, size_t, uint8_t);
                     55: uint32_t        ctf_dump_type(struct ctf_header *, const char *, off_t,
                     56:                     uint32_t, uint32_t);
                     57: const char     *ctf_kind2name(uint16_t);
                     58: const char     *ctf_enc2name(uint16_t);
1.7     ! uwe        59: const char     *ctf_fpenc2name(uint16_t);
1.1       mpi        60: const char     *ctf_off2name(struct ctf_header *, const char *, off_t,
                     61:                     uint32_t);
                     62:
                     63: int             elf_dump(char *, size_t, uint8_t);
                     64: const char     *elf_idx2sym(size_t *, uint8_t);
                     65:
                     66: /* elf.c */
                     67: int             iself(const char *, size_t);
                     68: int             elf_getshstab(const char *, size_t, const char **, size_t *);
                     69: ssize_t                 elf_getsymtab(const char *, const char *, size_t,
                     70:                     const Elf_Sym **, size_t *);
                     71: ssize_t                 elf_getsection(char *, const char *, const char *,
                     72:                     size_t, const char **, size_t *);
                     73:
                     74: char           *decompress(const char *, size_t, off_t);
                     75:
                     76: int
                     77: main(int argc, char *argv[])
                     78: {
                     79:        const char *filename;
                     80:        uint8_t flags = 0;
                     81:        int ch, error = 0;
1.3       jasper     82:
1.4       jasper     83:        setlocale(LC_ALL, "");
                     84:
1.3       jasper     85:        if (pledge("stdio rpath", NULL) == -1)
                     86:                err(1, "pledge");
1.1       mpi        87:
                     88:        while ((ch = getopt(argc, argv, "dfhlst")) != -1) {
                     89:                switch (ch) {
                     90:                case 'd':
                     91:                        flags |= DUMP_OBJECT;
                     92:                        break;
                     93:                case 'f':
                     94:                        flags |= DUMP_FUNCTION;
                     95:                        break;
                     96:                case 'h':
                     97:                        flags |= DUMP_HEADER;
                     98:                        break;
                     99:                case 'l':
                    100:                        flags |= DUMP_LABEL;
                    101:                        break;
                    102:                case 's':
                    103:                        flags |= DUMP_STRTAB;
                    104:                        break;
                    105:                case 't':
                    106:                        flags |= DUMP_TYPE;
                    107:                        break;
                    108:                default:
                    109:                        usage();
                    110:                }
                    111:        }
                    112:
                    113:        argc -= optind;
                    114:        argv += optind;
                    115:
                    116:        if (argc <= 0)
                    117:                usage();
                    118:
                    119:        /* Dump everything by default */
                    120:        if (flags == 0)
                    121:                flags = 0xff;
                    122:
                    123:        while ((filename = *argv++) != NULL)
                    124:                error |= dump(filename, flags);
                    125:
                    126:        return error;
                    127: }
                    128:
                    129: int
                    130: dump(const char *path, uint8_t flags)
                    131: {
                    132:        struct stat              st;
                    133:        int                      fd, error = 1;
                    134:        char                    *p;
                    135:
                    136:        fd = open(path, O_RDONLY);
                    137:        if (fd == -1) {
                    138:                warn("open");
                    139:                return 1;
                    140:        }
                    141:        if (fstat(fd, &st) == -1) {
                    142:                warn("fstat");
1.6       jsg       143:                close(fd);
1.1       mpi       144:                return 1;
                    145:        }
                    146:        if ((uintmax_t)st.st_size > SIZE_MAX) {
                    147:                warnx("file too big to fit memory");
1.6       jsg       148:                close(fd);
1.1       mpi       149:                return 1;
                    150:        }
                    151:
                    152:        p = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
                    153:        if (p == MAP_FAILED)
                    154:                err(1, "mmap");
                    155:
                    156:        if (iself(p, st.st_size)) {
                    157:                error = elf_dump(p, st.st_size, flags);
                    158:        } else if (isctf(p, st.st_size)) {
                    159:                error = ctf_dump(p, st.st_size, flags);
                    160:        }
                    161:
                    162:        munmap(p, st.st_size);
                    163:        close(fd);
                    164:
                    165:        return error;
                    166: }
                    167:
                    168: const char             *strtab;
                    169: const Elf_Sym          *symtab;
                    170: size_t                  strtabsz, nsymb;
                    171:
                    172: const char *
                    173: elf_idx2sym(size_t *idx, uint8_t type)
                    174: {
                    175:        const Elf_Sym   *st;
                    176:        size_t           i;
                    177:
                    178:        for (i = *idx + 1; i < nsymb; i++) {
                    179:                st = &symtab[i];
                    180:
                    181:                if (ELF_ST_TYPE(st->st_info) != type)
                    182:                        continue;
                    183:
                    184:                *idx = i;
                    185:                return strtab + st->st_name;
                    186:        }
                    187:
                    188:        return NULL;
                    189: }
                    190:
                    191: int
                    192: elf_dump(char *p, size_t filesize, uint8_t flags)
                    193: {
                    194:        Elf_Ehdr                *eh = (Elf_Ehdr *)p;
                    195:        Elf_Shdr                *sh;
                    196:        const char              *shstab;
                    197:        size_t                   i, shstabsz;
                    198:
                    199:        /* Find section header string table location and size. */
                    200:        if (elf_getshstab(p, filesize, &shstab, &shstabsz))
                    201:                return 1;
                    202:
                    203:        /* Find symbol table location and number of symbols. */
                    204:        if (elf_getsymtab(p, shstab, shstabsz, &symtab, &nsymb) == -1)
                    205:                warnx("symbol table not found");
                    206:
                    207:        /* Find string table location and size. */
                    208:        if (elf_getsection(p, ELF_STRTAB, shstab, shstabsz, &strtab,
                    209:            &strtabsz) == -1)
                    210:                warnx("string table not found");
                    211:
                    212:        /* Find CTF section and dump it. */
                    213:        for (i = 0; i < eh->e_shnum; i++) {
                    214:                sh = (Elf_Shdr *)(p + eh->e_shoff + i * eh->e_shentsize);
                    215:
                    216:                if ((sh->sh_link >= eh->e_shnum) ||
                    217:                    (sh->sh_name >= shstabsz))
                    218:                        continue;
                    219:
                    220:                if (strncmp(shstab + sh->sh_name, ELF_CTF, strlen(ELF_CTF)))
                    221:                        continue;
                    222:
                    223:                if (!isctf(p + sh->sh_offset, sh->sh_size))
                    224:                        break;
                    225:
                    226:                return ctf_dump(p + sh->sh_offset, sh->sh_size, flags);
                    227:        }
                    228:
                    229:        warnx("%s section not found", ELF_CTF);
                    230:        return 1;
                    231: }
                    232:
                    233: int
                    234: isctf(const char *p, size_t filesize)
                    235: {
                    236:        struct ctf_header       *cth = (struct ctf_header *)p;
                    237:        off_t                    dlen;
                    238:
                    239:        if (filesize < sizeof(struct ctf_header)) {
                    240:                warnx("file too small to be CTF");
                    241:                return 0;
                    242:        }
                    243:
                    244:        if (cth->cth_magic != CTF_MAGIC || cth->cth_version != CTF_VERSION)
                    245:                return 0;
                    246:
                    247:        dlen = cth->cth_stroff + cth->cth_strlen;
                    248:        if (dlen > (off_t)filesize && !(cth->cth_flags & CTF_F_COMPRESS)) {
                    249:                warnx("bogus file size");
                    250:                return 0;
                    251:        }
                    252:
                    253:        if ((cth->cth_lbloff & 3) || (cth->cth_objtoff & 1) ||
                    254:            (cth->cth_funcoff & 1) || (cth->cth_typeoff & 3)) {
                    255:                warnx("wrongly aligned offset");
                    256:                return 0;
                    257:        }
                    258:
                    259:        if ((cth->cth_lbloff >= dlen) || (cth->cth_objtoff >= dlen) ||
                    260:            (cth->cth_funcoff >= dlen) || (cth->cth_typeoff >= dlen)) {
                    261:                warnx("truncated file");
                    262:                return 0;
                    263:        }
                    264:
                    265:        if ((cth->cth_lbloff > cth->cth_objtoff) ||
                    266:            (cth->cth_objtoff > cth->cth_funcoff) ||
                    267:            (cth->cth_funcoff > cth->cth_typeoff) ||
                    268:            (cth->cth_typeoff > cth->cth_stroff)) {
                    269:                warnx("corrupted file");
                    270:                return 0;
                    271:        }
                    272:
                    273:        return 1;
                    274: }
                    275:
                    276: int
                    277: ctf_dump(const char *p, size_t size, uint8_t flags)
                    278: {
                    279:        struct ctf_header       *cth = (struct ctf_header *)p;
                    280:        off_t                    dlen = cth->cth_stroff + cth->cth_strlen;
                    281:        char                    *data;
                    282:
                    283:        if (cth->cth_flags & CTF_F_COMPRESS) {
                    284:                data = decompress(p + sizeof(*cth), size - sizeof(*cth), dlen);
                    285:                if (data == NULL)
                    286:                        return 1;
                    287:        } else {
                    288:                data = (char *)p + sizeof(*cth);
                    289:        }
                    290:
                    291:        if (flags & DUMP_HEADER) {
                    292:                printf("  cth_magic    = 0x%04x\n", cth->cth_magic);
                    293:                printf("  cth_version  = %d\n", cth->cth_version);
                    294:                printf("  cth_flags    = 0x%02x\n", cth->cth_flags);
                    295:                printf("  cth_parlabel = %s\n",
                    296:                    ctf_off2name(cth, data, dlen, cth->cth_parname));
                    297:                printf("  cth_parname  = %s\n",
                    298:                    ctf_off2name(cth, data, dlen, cth->cth_parname));
                    299:                printf("  cth_lbloff   = %d\n", cth->cth_lbloff);
                    300:                printf("  cth_objtoff  = %d\n", cth->cth_objtoff);
                    301:                printf("  cth_funcoff  = %d\n", cth->cth_funcoff);
                    302:                printf("  cth_typeoff  = %d\n", cth->cth_typeoff);
                    303:                printf("  cth_stroff   = %d\n", cth->cth_stroff);
                    304:                printf("  cth_strlen   = %d\n", cth->cth_strlen);
                    305:                printf("\n");
                    306:        }
                    307:
                    308:        if (flags & DUMP_LABEL) {
                    309:                uint32_t                 lbloff = cth->cth_lbloff;
                    310:                struct ctf_lblent       *ctl;
                    311:
                    312:                while (lbloff < cth->cth_objtoff) {
                    313:                        ctl = (struct ctf_lblent *)(data + lbloff);
                    314:
                    315:                        printf("  %5u %s\n", ctl->ctl_typeidx,
                    316:                            ctf_off2name(cth, data, dlen, ctl->ctl_label));
                    317:
                    318:                        lbloff += sizeof(*ctl);
                    319:                }
                    320:                printf("\n");
                    321:        }
                    322:
                    323:        if (flags & DUMP_OBJECT) {
                    324:                uint32_t                 objtoff = cth->cth_objtoff;
                    325:                size_t                   idx = 0, i = 0;
                    326:                uint16_t                *dsp;
                    327:                const char              *s;
                    328:                int                      l;
                    329:
                    330:                while (objtoff < cth->cth_funcoff) {
                    331:                        dsp = (uint16_t *)(data + objtoff);
                    332:
                    333:                        l = printf("  [%zu] %u", i++, *dsp);
                    334:                        if ((s = elf_idx2sym(&idx, STT_OBJECT)) != NULL)
                    335:                                printf("%*s %s (%zu)\n", (14 - l), "", s, idx);
                    336:                        else
                    337:                                printf("\n");
                    338:
                    339:                        objtoff += sizeof(*dsp);
                    340:                }
                    341:                printf("\n");
                    342:        }
                    343:
                    344:        if (flags & DUMP_FUNCTION) {
                    345:                uint16_t                *fsp, kind, vlen;
                    346:                size_t                   idx = 0, i = -1;
                    347:                const char              *s;
                    348:                int                      l;
                    349:
                    350:                fsp = (uint16_t *)(data + cth->cth_funcoff);
                    351:                while (fsp < (uint16_t *)(data + cth->cth_typeoff)) {
                    352:                        kind = CTF_INFO_KIND(*fsp);
                    353:                        vlen = CTF_INFO_VLEN(*fsp);
                    354:                        s = elf_idx2sym(&idx, STT_FUNC);
                    355:                        fsp++;
                    356:                        i++;
                    357:
                    358:                        if (kind == CTF_K_UNKNOWN && vlen == 0)
                    359:                                continue;
                    360:
                    361:                        l = printf("  [%zu] FUNC ", i);
                    362:                        if (s != NULL)
                    363:                                printf("(%s)", s);
                    364:                        printf(" returns: %u args: (", *fsp++);
                    365:                        while (vlen-- > 0)
                    366:                                printf("%u%s", *fsp++, (vlen > 0) ? ", " : "");
                    367:                        printf(")\n");
                    368:                }
                    369:                printf("\n");
                    370:        }
                    371:
                    372:        if (flags & DUMP_TYPE) {
                    373:                uint32_t                 idx = 1, offset = cth->cth_typeoff;
                    374:
                    375:                while (offset < cth->cth_stroff) {
                    376:                        offset += ctf_dump_type(cth, data, dlen, offset, idx++);
                    377:                }
                    378:                printf("\n");
                    379:        }
                    380:
                    381:        if (flags & DUMP_STRTAB) {
                    382:                uint32_t                 offset = 0;
                    383:                const char              *str;
                    384:
                    385:                while (offset < cth->cth_strlen) {
                    386:                        str = ctf_off2name(cth, data, dlen, offset);
                    387:
                    388:                        printf("  [%u] ", offset);
                    389:                        if (strcmp(str, "(anon)"))
                    390:                                offset += printf("%s\n", str);
                    391:                        else {
                    392:                                printf("\\0\n");
                    393:                                offset++;
                    394:                        }
                    395:                }
                    396:                printf("\n");
                    397:        }
                    398:
                    399:        if (cth->cth_flags & CTF_F_COMPRESS)
                    400:                free(data);
                    401:
                    402:        return 0;
                    403: }
                    404:
                    405: uint32_t
                    406: ctf_dump_type(struct ctf_header *cth, const char *data, off_t dlen,
                    407:     uint32_t offset, uint32_t idx)
                    408: {
                    409:        const char              *p = data + offset;
                    410:        const struct ctf_type   *ctt = (struct ctf_type *)p;
                    411:        const struct ctf_array  *cta;
                    412:        uint16_t                *argp, i, kind, vlen, root;
                    413:        uint32_t                 eob, toff;
                    414:        uint64_t                 size;
                    415:        const char              *name, *kname;
                    416:
                    417:        kind = CTF_INFO_KIND(ctt->ctt_info);
                    418:        vlen = CTF_INFO_VLEN(ctt->ctt_info);
                    419:        root = CTF_INFO_ISROOT(ctt->ctt_info);
                    420:        name = ctf_off2name(cth, data, dlen, ctt->ctt_name);
                    421:
                    422:        if (root)
                    423:                printf("  <%u> ", idx);
                    424:        else
                    425:                printf("  [%u] ", idx);
                    426:
                    427:        if ((kname = ctf_kind2name(kind)) != NULL)
                    428:                printf("%s %s", kname, name);
                    429:
                    430:        if (ctt->ctt_size <= CTF_MAX_SIZE) {
                    431:                size = ctt->ctt_size;
                    432:                toff = sizeof(struct ctf_stype);
                    433:        } else {
                    434:                size = CTF_TYPE_LSIZE(ctt);
                    435:                toff = sizeof(struct ctf_type);
                    436:        }
                    437:
                    438:        switch (kind) {
                    439:        case CTF_K_UNKNOWN:
                    440:        case CTF_K_FORWARD:
                    441:                break;
                    442:        case CTF_K_INTEGER:
                    443:                eob = *((uint32_t *)(p + toff));
                    444:                toff += sizeof(uint32_t);
                    445:                printf(" encoding=%s offset=%u bits=%u",
                    446:                    ctf_enc2name(CTF_INT_ENCODING(eob)), CTF_INT_OFFSET(eob),
                    447:                    CTF_INT_BITS(eob));
                    448:                break;
                    449:        case CTF_K_FLOAT:
                    450:                eob = *((uint32_t *)(p + toff));
                    451:                toff += sizeof(uint32_t);
1.7     ! uwe       452:                printf(" encoding=%s offset=%u bits=%u",
        !           453:                    ctf_fpenc2name(CTF_FP_ENCODING(eob)), CTF_FP_OFFSET(eob),
        !           454:                    CTF_FP_BITS(eob));
1.1       mpi       455:                break;
                    456:        case CTF_K_ARRAY:
                    457:                cta = (struct ctf_array *)(p + toff);
                    458:                printf(" content: %u index: %u nelems: %u\n", cta->cta_contents,
                    459:                    cta->cta_index, cta->cta_nelems);
                    460:                toff += sizeof(struct ctf_array);
                    461:                break;
                    462:        case CTF_K_FUNCTION:
                    463:                argp = (uint16_t *)(p + toff);
                    464:                printf(" returns: %u args: (%u", ctt->ctt_type, *argp);
                    465:                for (i = 1; i < vlen; i++) {
                    466:                        argp++;
                    467:                        printf(", %u", *argp);
                    468:                }
                    469:                printf(")");
                    470:                toff += (vlen + (vlen & 1)) * sizeof(uint16_t);
                    471:                break;
                    472:        case CTF_K_STRUCT:
                    473:        case CTF_K_UNION:
                    474:                printf(" (%llu bytes)\n", size);
                    475:
                    476:                if (size < CTF_LSTRUCT_THRESH) {
                    477:                        for (i = 0; i < vlen; i++) {
                    478:                                struct ctf_member       *ctm;
                    479:
                    480:                                ctm = (struct ctf_member *)(p + toff);
                    481:                                toff += sizeof(struct ctf_member);
                    482:
                    483:                                printf("\t%s type=%u off=%u\n",
                    484:                                    ctf_off2name(cth, data, dlen,
                    485:                                        ctm->ctm_name),
                    486:                                    ctm->ctm_type, ctm->ctm_offset);
                    487:                        }
                    488:                } else {
                    489:                        for (i = 0; i < vlen; i++) {
                    490:                                struct ctf_lmember      *ctlm;
                    491:
                    492:                                ctlm = (struct ctf_lmember *)(p + toff);
                    493:                                toff += sizeof(struct ctf_lmember);
                    494:
                    495:                                printf("\t%s type=%u off=%llu\n",
                    496:                                    ctf_off2name(cth, data, dlen,
                    497:                                        ctlm->ctlm_name),
                    498:                                    ctlm->ctlm_type, CTF_LMEM_OFFSET(ctlm));
                    499:                        }
                    500:                }
                    501:                break;
                    502:        case CTF_K_ENUM:
                    503:                printf("\n");
                    504:                for (i = 0; i < vlen; i++) {
                    505:                        struct ctf_enum *cte;
                    506:
                    507:                        cte = (struct ctf_enum *)(p + toff);
                    508:                        toff += sizeof(struct ctf_enum);
                    509:
                    510:                        printf("\t%s = %d\n",
                    511:                            ctf_off2name(cth, data, dlen, cte->cte_name),
                    512:                            cte->cte_value);
                    513:                }
                    514:                break;
                    515:        case CTF_K_POINTER:
                    516:        case CTF_K_TYPEDEF:
                    517:        case CTF_K_VOLATILE:
                    518:        case CTF_K_CONST:
                    519:        case CTF_K_RESTRICT:
                    520:                printf(" refers to %u", ctt->ctt_type);
                    521:                break;
                    522:        default:
                    523:                errx(1, "incorrect type %u at offset %u", kind, offset);
                    524:        }
                    525:
                    526:        printf("\n");
                    527:
                    528:        return toff;
                    529: }
                    530:
                    531: const char *
                    532: ctf_kind2name(uint16_t kind)
                    533: {
                    534:        static const char *kind_name[] = { NULL, "INTEGER", "FLOAT", "POINTER",
                    535:           "ARRAY", "FUNCTION", "STRUCT", "UNION", "ENUM", "FORWARD",
                    536:           "TYPEDEF", "VOLATILE", "CONST", "RESTRICT" };
                    537:
                    538:        if (kind >= nitems(kind_name))
                    539:                return NULL;
                    540:
                    541:        return kind_name[kind];
                    542: }
                    543:
                    544: const char *
                    545: ctf_enc2name(uint16_t enc)
                    546: {
                    547:        static const char *enc_name[] = { "SIGNED", "CHAR", "SIGNED CHAR",
                    548:            "BOOL", "SIGNED BOOL" };
                    549:        static char invalid[7];
                    550:
                    551:        if (enc == CTF_INT_VARARGS)
                    552:                return "VARARGS";
                    553:
                    554:        if (enc > 0 && enc < nitems(enc_name))
1.7     ! uwe       555:                return enc_name[enc - 1];
        !           556:
        !           557:        snprintf(invalid, sizeof(invalid), "0x%x", enc);
        !           558:        return invalid;
        !           559: }
        !           560:
        !           561: const char *
        !           562: ctf_fpenc2name(uint16_t enc)
        !           563: {
        !           564:        static const char *enc_name[] = { "SINGLE", "DOUBLE", NULL, NULL,
        !           565:            NULL, "LDOUBLE" };
        !           566:        static char invalid[7];
        !           567:
        !           568:        if (enc > 0 && enc <= nitems(enc_name) && enc_name[enc - 1] != NULL)
1.1       mpi       569:                return enc_name[enc - 1];
                    570:
                    571:        snprintf(invalid, sizeof(invalid), "0x%x", enc);
                    572:        return invalid;
                    573: }
                    574:
                    575: const char *
                    576: ctf_off2name(struct ctf_header *cth, const char *data, off_t dlen,
                    577:     uint32_t offset)
                    578: {
                    579:        const char              *name;
                    580:
                    581:        if (CTF_NAME_STID(offset) != CTF_STRTAB_0)
                    582:                return "external";
                    583:
                    584:        if (CTF_NAME_OFFSET(offset) >= cth->cth_strlen)
                    585:                return "exceeds strlab";
                    586:
                    587:        if (cth->cth_stroff + CTF_NAME_OFFSET(offset) >= dlen)
                    588:                return "invalid";
                    589:
                    590:        name = data + cth->cth_stroff + CTF_NAME_OFFSET(offset);
                    591:        if (*name == '\0')
                    592:                return "(anon)";
                    593:
                    594:        return name;
                    595: }
                    596:
                    597: char *
                    598: decompress(const char *buf, size_t size, off_t len)
                    599: {
                    600: #ifdef ZLIB
                    601:        z_stream                 stream;
                    602:        char                    *data;
                    603:        int                      error;
                    604:
                    605:        data = malloc(len);
                    606:        if (data == NULL) {
                    607:                warn(NULL);
                    608:                return NULL;
                    609:        }
                    610:
                    611:        memset(&stream, 0, sizeof(stream));
                    612:        stream.next_in = (void *)buf;
                    613:        stream.avail_in = size;
                    614:        stream.next_out = (uint8_t *)data;
                    615:        stream.avail_out = len;
                    616:
                    617:        if ((error = inflateInit(&stream)) != Z_OK) {
                    618:                warnx("zlib inflateInit failed: %s", zError(error));
                    619:                goto exit;
                    620:        }
                    621:
                    622:        if ((error = inflate(&stream, Z_FINISH)) != Z_STREAM_END) {
                    623:                warnx("zlib inflate failed: %s", zError(error));
                    624:                inflateEnd(&stream);
                    625:                goto exit;
                    626:        }
                    627:
                    628:        if ((error = inflateEnd(&stream)) != Z_OK) {
                    629:                warnx("zlib inflateEnd failed: %s", zError(error));
                    630:                goto exit;
                    631:        }
                    632:
                    633:        if (stream.total_out != len) {
                    634:                warnx("decompression failed: %llu != %llu",
                    635:                    stream.total_out, len);
                    636:                goto exit;
                    637:        }
                    638:
                    639:        return data;
                    640:
                    641: exit:
                    642:        free(data);
                    643: #endif /* ZLIB */
                    644:        return NULL;
                    645: }
                    646:
                    647: __dead void
                    648: usage(void)
                    649: {
                    650:        fprintf(stderr, "usage: %s [-dfhlst] file ...\n",
                    651:            getprogname());
                    652:        exit(1);
                    653: }