[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.13

1.13    ! mpi         1: /*     $OpenBSD: ctfdump.c,v 1.12 2017/10/27 08:33:46 mpi 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/mman.h>
                     22: #include <sys/ctf.h>
                     23:
1.12      mpi        24: #include <elf.h>
1.1       mpi        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 *);
1.9       jsg        69: ssize_t                 elf_getsymtab(const char *, size_t filesize, const char *,
                     70:                     size_t, const Elf_Sym **, size_t *);
                     71: ssize_t                 elf_getsection(char *, size_t, const char *, const char *,
1.1       mpi        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:
1.10      jsg       184:                if (st->st_name >= strtabsz)
                    185:                        break;
                    186:
1.1       mpi       187:                *idx = i;
                    188:                return strtab + st->st_name;
                    189:        }
                    190:
                    191:        return NULL;
                    192: }
                    193:
                    194: int
                    195: elf_dump(char *p, size_t filesize, uint8_t flags)
                    196: {
                    197:        Elf_Ehdr                *eh = (Elf_Ehdr *)p;
                    198:        Elf_Shdr                *sh;
                    199:        const char              *shstab;
                    200:        size_t                   i, shstabsz;
                    201:
                    202:        /* Find section header string table location and size. */
                    203:        if (elf_getshstab(p, filesize, &shstab, &shstabsz))
                    204:                return 1;
                    205:
                    206:        /* Find symbol table location and number of symbols. */
1.9       jsg       207:        if (elf_getsymtab(p, filesize, shstab, shstabsz, &symtab, &nsymb) == -1)
1.1       mpi       208:                warnx("symbol table not found");
                    209:
                    210:        /* Find string table location and size. */
1.9       jsg       211:        if (elf_getsection(p, filesize, ELF_STRTAB, shstab, shstabsz, &strtab,
1.1       mpi       212:            &strtabsz) == -1)
                    213:                warnx("string table not found");
                    214:
                    215:        /* Find CTF section and dump it. */
                    216:        for (i = 0; i < eh->e_shnum; i++) {
                    217:                sh = (Elf_Shdr *)(p + eh->e_shoff + i * eh->e_shentsize);
                    218:
                    219:                if ((sh->sh_link >= eh->e_shnum) ||
                    220:                    (sh->sh_name >= shstabsz))
                    221:                        continue;
                    222:
                    223:                if (strncmp(shstab + sh->sh_name, ELF_CTF, strlen(ELF_CTF)))
1.10      jsg       224:                        continue;
                    225:
                    226:                if ((sh->sh_offset + sh->sh_size) > filesize)
1.1       mpi       227:                        continue;
                    228:
                    229:                if (!isctf(p + sh->sh_offset, sh->sh_size))
                    230:                        break;
                    231:
                    232:                return ctf_dump(p + sh->sh_offset, sh->sh_size, flags);
                    233:        }
                    234:
                    235:        warnx("%s section not found", ELF_CTF);
                    236:        return 1;
                    237: }
                    238:
                    239: int
                    240: isctf(const char *p, size_t filesize)
                    241: {
                    242:        struct ctf_header       *cth = (struct ctf_header *)p;
                    243:        off_t                    dlen;
                    244:
                    245:        if (filesize < sizeof(struct ctf_header)) {
                    246:                warnx("file too small to be CTF");
                    247:                return 0;
                    248:        }
                    249:
                    250:        if (cth->cth_magic != CTF_MAGIC || cth->cth_version != CTF_VERSION)
                    251:                return 0;
                    252:
                    253:        dlen = cth->cth_stroff + cth->cth_strlen;
                    254:        if (dlen > (off_t)filesize && !(cth->cth_flags & CTF_F_COMPRESS)) {
                    255:                warnx("bogus file size");
                    256:                return 0;
                    257:        }
                    258:
                    259:        if ((cth->cth_lbloff & 3) || (cth->cth_objtoff & 1) ||
                    260:            (cth->cth_funcoff & 1) || (cth->cth_typeoff & 3)) {
                    261:                warnx("wrongly aligned offset");
                    262:                return 0;
                    263:        }
                    264:
                    265:        if ((cth->cth_lbloff >= dlen) || (cth->cth_objtoff >= dlen) ||
                    266:            (cth->cth_funcoff >= dlen) || (cth->cth_typeoff >= dlen)) {
                    267:                warnx("truncated file");
                    268:                return 0;
                    269:        }
                    270:
                    271:        if ((cth->cth_lbloff > cth->cth_objtoff) ||
                    272:            (cth->cth_objtoff > cth->cth_funcoff) ||
                    273:            (cth->cth_funcoff > cth->cth_typeoff) ||
                    274:            (cth->cth_typeoff > cth->cth_stroff)) {
                    275:                warnx("corrupted file");
                    276:                return 0;
                    277:        }
                    278:
                    279:        return 1;
                    280: }
                    281:
                    282: int
                    283: ctf_dump(const char *p, size_t size, uint8_t flags)
                    284: {
                    285:        struct ctf_header       *cth = (struct ctf_header *)p;
                    286:        off_t                    dlen = cth->cth_stroff + cth->cth_strlen;
                    287:        char                    *data;
                    288:
                    289:        if (cth->cth_flags & CTF_F_COMPRESS) {
                    290:                data = decompress(p + sizeof(*cth), size - sizeof(*cth), dlen);
                    291:                if (data == NULL)
                    292:                        return 1;
                    293:        } else {
                    294:                data = (char *)p + sizeof(*cth);
                    295:        }
                    296:
                    297:        if (flags & DUMP_HEADER) {
                    298:                printf("  cth_magic    = 0x%04x\n", cth->cth_magic);
                    299:                printf("  cth_version  = %d\n", cth->cth_version);
                    300:                printf("  cth_flags    = 0x%02x\n", cth->cth_flags);
                    301:                printf("  cth_parlabel = %s\n",
1.11      mpi       302:                    ctf_off2name(cth, data, dlen, cth->cth_parlabel));
1.1       mpi       303:                printf("  cth_parname  = %s\n",
                    304:                    ctf_off2name(cth, data, dlen, cth->cth_parname));
                    305:                printf("  cth_lbloff   = %d\n", cth->cth_lbloff);
                    306:                printf("  cth_objtoff  = %d\n", cth->cth_objtoff);
                    307:                printf("  cth_funcoff  = %d\n", cth->cth_funcoff);
                    308:                printf("  cth_typeoff  = %d\n", cth->cth_typeoff);
                    309:                printf("  cth_stroff   = %d\n", cth->cth_stroff);
                    310:                printf("  cth_strlen   = %d\n", cth->cth_strlen);
                    311:                printf("\n");
                    312:        }
                    313:
                    314:        if (flags & DUMP_LABEL) {
                    315:                uint32_t                 lbloff = cth->cth_lbloff;
                    316:                struct ctf_lblent       *ctl;
                    317:
                    318:                while (lbloff < cth->cth_objtoff) {
                    319:                        ctl = (struct ctf_lblent *)(data + lbloff);
                    320:
                    321:                        printf("  %5u %s\n", ctl->ctl_typeidx,
                    322:                            ctf_off2name(cth, data, dlen, ctl->ctl_label));
                    323:
                    324:                        lbloff += sizeof(*ctl);
                    325:                }
                    326:                printf("\n");
                    327:        }
                    328:
                    329:        if (flags & DUMP_OBJECT) {
                    330:                uint32_t                 objtoff = cth->cth_objtoff;
                    331:                size_t                   idx = 0, i = 0;
                    332:                uint16_t                *dsp;
                    333:                const char              *s;
                    334:                int                      l;
                    335:
                    336:                while (objtoff < cth->cth_funcoff) {
                    337:                        dsp = (uint16_t *)(data + objtoff);
                    338:
                    339:                        l = printf("  [%zu] %u", i++, *dsp);
                    340:                        if ((s = elf_idx2sym(&idx, STT_OBJECT)) != NULL)
                    341:                                printf("%*s %s (%zu)\n", (14 - l), "", s, idx);
                    342:                        else
                    343:                                printf("\n");
                    344:
                    345:                        objtoff += sizeof(*dsp);
                    346:                }
                    347:                printf("\n");
                    348:        }
                    349:
                    350:        if (flags & DUMP_FUNCTION) {
                    351:                uint16_t                *fsp, kind, vlen;
1.13    ! mpi       352:                uint16_t                *fstart, *fend;
1.1       mpi       353:                size_t                   idx = 0, i = -1;
                    354:                const char              *s;
                    355:                int                      l;
                    356:
1.13    ! mpi       357:                fstart = (uint16_t *)(data + cth->cth_funcoff);
        !           358:                fend = (uint16_t *)(data + cth->cth_typeoff);
        !           359:
        !           360:                fsp = fstart;
        !           361:                while (fsp < fend) {
1.1       mpi       362:                        kind = CTF_INFO_KIND(*fsp);
                    363:                        vlen = CTF_INFO_VLEN(*fsp);
                    364:                        s = elf_idx2sym(&idx, STT_FUNC);
                    365:                        fsp++;
                    366:                        i++;
                    367:
                    368:                        if (kind == CTF_K_UNKNOWN && vlen == 0)
                    369:                                continue;
                    370:
                    371:                        l = printf("  [%zu] FUNC ", i);
                    372:                        if (s != NULL)
                    373:                                printf("(%s)", s);
                    374:                        printf(" returns: %u args: (", *fsp++);
1.13    ! mpi       375:                        while (vlen-- > 0 && fsp < fend)
1.1       mpi       376:                                printf("%u%s", *fsp++, (vlen > 0) ? ", " : "");
                    377:                        printf(")\n");
                    378:                }
                    379:                printf("\n");
                    380:        }
                    381:
                    382:        if (flags & DUMP_TYPE) {
                    383:                uint32_t                 idx = 1, offset = cth->cth_typeoff;
                    384:
                    385:                while (offset < cth->cth_stroff) {
                    386:                        offset += ctf_dump_type(cth, data, dlen, offset, idx++);
                    387:                }
                    388:                printf("\n");
                    389:        }
                    390:
                    391:        if (flags & DUMP_STRTAB) {
                    392:                uint32_t                 offset = 0;
                    393:                const char              *str;
                    394:
                    395:                while (offset < cth->cth_strlen) {
                    396:                        str = ctf_off2name(cth, data, dlen, offset);
                    397:
                    398:                        printf("  [%u] ", offset);
                    399:                        if (strcmp(str, "(anon)"))
                    400:                                offset += printf("%s\n", str);
                    401:                        else {
                    402:                                printf("\\0\n");
                    403:                                offset++;
                    404:                        }
                    405:                }
                    406:                printf("\n");
                    407:        }
                    408:
                    409:        if (cth->cth_flags & CTF_F_COMPRESS)
                    410:                free(data);
                    411:
                    412:        return 0;
                    413: }
                    414:
                    415: uint32_t
                    416: ctf_dump_type(struct ctf_header *cth, const char *data, off_t dlen,
                    417:     uint32_t offset, uint32_t idx)
                    418: {
                    419:        const char              *p = data + offset;
                    420:        const struct ctf_type   *ctt = (struct ctf_type *)p;
                    421:        const struct ctf_array  *cta;
                    422:        uint16_t                *argp, i, kind, vlen, root;
                    423:        uint32_t                 eob, toff;
                    424:        uint64_t                 size;
                    425:        const char              *name, *kname;
                    426:
                    427:        kind = CTF_INFO_KIND(ctt->ctt_info);
                    428:        vlen = CTF_INFO_VLEN(ctt->ctt_info);
                    429:        root = CTF_INFO_ISROOT(ctt->ctt_info);
                    430:        name = ctf_off2name(cth, data, dlen, ctt->ctt_name);
                    431:
                    432:        if (root)
                    433:                printf("  <%u> ", idx);
                    434:        else
                    435:                printf("  [%u] ", idx);
                    436:
                    437:        if ((kname = ctf_kind2name(kind)) != NULL)
                    438:                printf("%s %s", kname, name);
                    439:
                    440:        if (ctt->ctt_size <= CTF_MAX_SIZE) {
                    441:                size = ctt->ctt_size;
                    442:                toff = sizeof(struct ctf_stype);
                    443:        } else {
                    444:                size = CTF_TYPE_LSIZE(ctt);
                    445:                toff = sizeof(struct ctf_type);
                    446:        }
                    447:
                    448:        switch (kind) {
                    449:        case CTF_K_UNKNOWN:
                    450:        case CTF_K_FORWARD:
                    451:                break;
                    452:        case CTF_K_INTEGER:
                    453:                eob = *((uint32_t *)(p + toff));
                    454:                toff += sizeof(uint32_t);
                    455:                printf(" encoding=%s offset=%u bits=%u",
                    456:                    ctf_enc2name(CTF_INT_ENCODING(eob)), CTF_INT_OFFSET(eob),
                    457:                    CTF_INT_BITS(eob));
                    458:                break;
                    459:        case CTF_K_FLOAT:
                    460:                eob = *((uint32_t *)(p + toff));
                    461:                toff += sizeof(uint32_t);
1.7       uwe       462:                printf(" encoding=%s offset=%u bits=%u",
                    463:                    ctf_fpenc2name(CTF_FP_ENCODING(eob)), CTF_FP_OFFSET(eob),
                    464:                    CTF_FP_BITS(eob));
1.1       mpi       465:                break;
                    466:        case CTF_K_ARRAY:
                    467:                cta = (struct ctf_array *)(p + toff);
                    468:                printf(" content: %u index: %u nelems: %u\n", cta->cta_contents,
                    469:                    cta->cta_index, cta->cta_nelems);
                    470:                toff += sizeof(struct ctf_array);
                    471:                break;
                    472:        case CTF_K_FUNCTION:
                    473:                argp = (uint16_t *)(p + toff);
                    474:                printf(" returns: %u args: (%u", ctt->ctt_type, *argp);
                    475:                for (i = 1; i < vlen; i++) {
                    476:                        argp++;
                    477:                        printf(", %u", *argp);
                    478:                }
                    479:                printf(")");
                    480:                toff += (vlen + (vlen & 1)) * sizeof(uint16_t);
                    481:                break;
                    482:        case CTF_K_STRUCT:
                    483:        case CTF_K_UNION:
                    484:                printf(" (%llu bytes)\n", size);
                    485:
                    486:                if (size < CTF_LSTRUCT_THRESH) {
                    487:                        for (i = 0; i < vlen; i++) {
                    488:                                struct ctf_member       *ctm;
                    489:
                    490:                                ctm = (struct ctf_member *)(p + toff);
                    491:                                toff += sizeof(struct ctf_member);
                    492:
                    493:                                printf("\t%s type=%u off=%u\n",
                    494:                                    ctf_off2name(cth, data, dlen,
                    495:                                        ctm->ctm_name),
                    496:                                    ctm->ctm_type, ctm->ctm_offset);
                    497:                        }
                    498:                } else {
                    499:                        for (i = 0; i < vlen; i++) {
                    500:                                struct ctf_lmember      *ctlm;
                    501:
                    502:                                ctlm = (struct ctf_lmember *)(p + toff);
                    503:                                toff += sizeof(struct ctf_lmember);
                    504:
                    505:                                printf("\t%s type=%u off=%llu\n",
                    506:                                    ctf_off2name(cth, data, dlen,
                    507:                                        ctlm->ctlm_name),
                    508:                                    ctlm->ctlm_type, CTF_LMEM_OFFSET(ctlm));
                    509:                        }
                    510:                }
                    511:                break;
                    512:        case CTF_K_ENUM:
                    513:                printf("\n");
                    514:                for (i = 0; i < vlen; i++) {
                    515:                        struct ctf_enum *cte;
                    516:
                    517:                        cte = (struct ctf_enum *)(p + toff);
                    518:                        toff += sizeof(struct ctf_enum);
                    519:
                    520:                        printf("\t%s = %d\n",
                    521:                            ctf_off2name(cth, data, dlen, cte->cte_name),
                    522:                            cte->cte_value);
                    523:                }
                    524:                break;
                    525:        case CTF_K_POINTER:
                    526:        case CTF_K_TYPEDEF:
                    527:        case CTF_K_VOLATILE:
                    528:        case CTF_K_CONST:
                    529:        case CTF_K_RESTRICT:
                    530:                printf(" refers to %u", ctt->ctt_type);
                    531:                break;
                    532:        default:
                    533:                errx(1, "incorrect type %u at offset %u", kind, offset);
                    534:        }
                    535:
                    536:        printf("\n");
                    537:
                    538:        return toff;
                    539: }
                    540:
                    541: const char *
                    542: ctf_kind2name(uint16_t kind)
                    543: {
                    544:        static const char *kind_name[] = { NULL, "INTEGER", "FLOAT", "POINTER",
                    545:           "ARRAY", "FUNCTION", "STRUCT", "UNION", "ENUM", "FORWARD",
                    546:           "TYPEDEF", "VOLATILE", "CONST", "RESTRICT" };
                    547:
                    548:        if (kind >= nitems(kind_name))
                    549:                return NULL;
                    550:
                    551:        return kind_name[kind];
                    552: }
                    553:
                    554: const char *
                    555: ctf_enc2name(uint16_t enc)
                    556: {
                    557:        static const char *enc_name[] = { "SIGNED", "CHAR", "SIGNED CHAR",
                    558:            "BOOL", "SIGNED BOOL" };
                    559:        static char invalid[7];
                    560:
                    561:        if (enc == CTF_INT_VARARGS)
                    562:                return "VARARGS";
                    563:
1.8       uwe       564:        if (enc > 0 && enc <= nitems(enc_name))
1.7       uwe       565:                return enc_name[enc - 1];
                    566:
                    567:        snprintf(invalid, sizeof(invalid), "0x%x", enc);
                    568:        return invalid;
                    569: }
                    570:
                    571: const char *
                    572: ctf_fpenc2name(uint16_t enc)
                    573: {
                    574:        static const char *enc_name[] = { "SINGLE", "DOUBLE", NULL, NULL,
                    575:            NULL, "LDOUBLE" };
                    576:        static char invalid[7];
                    577:
                    578:        if (enc > 0 && enc <= nitems(enc_name) && enc_name[enc - 1] != NULL)
1.1       mpi       579:                return enc_name[enc - 1];
                    580:
                    581:        snprintf(invalid, sizeof(invalid), "0x%x", enc);
                    582:        return invalid;
                    583: }
                    584:
                    585: const char *
                    586: ctf_off2name(struct ctf_header *cth, const char *data, off_t dlen,
                    587:     uint32_t offset)
                    588: {
                    589:        const char              *name;
                    590:
                    591:        if (CTF_NAME_STID(offset) != CTF_STRTAB_0)
                    592:                return "external";
                    593:
                    594:        if (CTF_NAME_OFFSET(offset) >= cth->cth_strlen)
                    595:                return "exceeds strlab";
                    596:
                    597:        if (cth->cth_stroff + CTF_NAME_OFFSET(offset) >= dlen)
                    598:                return "invalid";
                    599:
                    600:        name = data + cth->cth_stroff + CTF_NAME_OFFSET(offset);
                    601:        if (*name == '\0')
                    602:                return "(anon)";
                    603:
                    604:        return name;
                    605: }
                    606:
                    607: char *
                    608: decompress(const char *buf, size_t size, off_t len)
                    609: {
                    610: #ifdef ZLIB
                    611:        z_stream                 stream;
                    612:        char                    *data;
                    613:        int                      error;
                    614:
                    615:        data = malloc(len);
                    616:        if (data == NULL) {
                    617:                warn(NULL);
                    618:                return NULL;
                    619:        }
                    620:
                    621:        memset(&stream, 0, sizeof(stream));
                    622:        stream.next_in = (void *)buf;
                    623:        stream.avail_in = size;
                    624:        stream.next_out = (uint8_t *)data;
                    625:        stream.avail_out = len;
                    626:
                    627:        if ((error = inflateInit(&stream)) != Z_OK) {
                    628:                warnx("zlib inflateInit failed: %s", zError(error));
                    629:                goto exit;
                    630:        }
                    631:
                    632:        if ((error = inflate(&stream, Z_FINISH)) != Z_STREAM_END) {
                    633:                warnx("zlib inflate failed: %s", zError(error));
                    634:                inflateEnd(&stream);
                    635:                goto exit;
                    636:        }
                    637:
                    638:        if ((error = inflateEnd(&stream)) != Z_OK) {
                    639:                warnx("zlib inflateEnd failed: %s", zError(error));
                    640:                goto exit;
                    641:        }
                    642:
                    643:        if (stream.total_out != len) {
                    644:                warnx("decompression failed: %llu != %llu",
                    645:                    stream.total_out, len);
                    646:                goto exit;
                    647:        }
                    648:
                    649:        return data;
                    650:
                    651: exit:
                    652:        free(data);
                    653: #endif /* ZLIB */
                    654:        return NULL;
                    655: }
                    656:
                    657: __dead void
                    658: usage(void)
                    659: {
                    660:        fprintf(stderr, "usage: %s [-dfhlst] file ...\n",
                    661:            getprogname());
                    662:        exit(1);
                    663: }