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

1.10    ! jsg         1: /*     $OpenBSD: ctfdump.c,v 1.9 2017/10/05 02:59:39 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 *);
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",
                    302:                    ctf_off2name(cth, data, dlen, cth->cth_parname));
                    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;
                    352:                size_t                   idx = 0, i = -1;
                    353:                const char              *s;
                    354:                int                      l;
                    355:
                    356:                fsp = (uint16_t *)(data + cth->cth_funcoff);
                    357:                while (fsp < (uint16_t *)(data + cth->cth_typeoff)) {
                    358:                        kind = CTF_INFO_KIND(*fsp);
                    359:                        vlen = CTF_INFO_VLEN(*fsp);
                    360:                        s = elf_idx2sym(&idx, STT_FUNC);
                    361:                        fsp++;
                    362:                        i++;
                    363:
                    364:                        if (kind == CTF_K_UNKNOWN && vlen == 0)
                    365:                                continue;
                    366:
                    367:                        l = printf("  [%zu] FUNC ", i);
                    368:                        if (s != NULL)
                    369:                                printf("(%s)", s);
                    370:                        printf(" returns: %u args: (", *fsp++);
                    371:                        while (vlen-- > 0)
                    372:                                printf("%u%s", *fsp++, (vlen > 0) ? ", " : "");
                    373:                        printf(")\n");
                    374:                }
                    375:                printf("\n");
                    376:        }
                    377:
                    378:        if (flags & DUMP_TYPE) {
                    379:                uint32_t                 idx = 1, offset = cth->cth_typeoff;
                    380:
                    381:                while (offset < cth->cth_stroff) {
                    382:                        offset += ctf_dump_type(cth, data, dlen, offset, idx++);
                    383:                }
                    384:                printf("\n");
                    385:        }
                    386:
                    387:        if (flags & DUMP_STRTAB) {
                    388:                uint32_t                 offset = 0;
                    389:                const char              *str;
                    390:
                    391:                while (offset < cth->cth_strlen) {
                    392:                        str = ctf_off2name(cth, data, dlen, offset);
                    393:
                    394:                        printf("  [%u] ", offset);
                    395:                        if (strcmp(str, "(anon)"))
                    396:                                offset += printf("%s\n", str);
                    397:                        else {
                    398:                                printf("\\0\n");
                    399:                                offset++;
                    400:                        }
                    401:                }
                    402:                printf("\n");
                    403:        }
                    404:
                    405:        if (cth->cth_flags & CTF_F_COMPRESS)
                    406:                free(data);
                    407:
                    408:        return 0;
                    409: }
                    410:
                    411: uint32_t
                    412: ctf_dump_type(struct ctf_header *cth, const char *data, off_t dlen,
                    413:     uint32_t offset, uint32_t idx)
                    414: {
                    415:        const char              *p = data + offset;
                    416:        const struct ctf_type   *ctt = (struct ctf_type *)p;
                    417:        const struct ctf_array  *cta;
                    418:        uint16_t                *argp, i, kind, vlen, root;
                    419:        uint32_t                 eob, toff;
                    420:        uint64_t                 size;
                    421:        const char              *name, *kname;
                    422:
                    423:        kind = CTF_INFO_KIND(ctt->ctt_info);
                    424:        vlen = CTF_INFO_VLEN(ctt->ctt_info);
                    425:        root = CTF_INFO_ISROOT(ctt->ctt_info);
                    426:        name = ctf_off2name(cth, data, dlen, ctt->ctt_name);
                    427:
                    428:        if (root)
                    429:                printf("  <%u> ", idx);
                    430:        else
                    431:                printf("  [%u] ", idx);
                    432:
                    433:        if ((kname = ctf_kind2name(kind)) != NULL)
                    434:                printf("%s %s", kname, name);
                    435:
                    436:        if (ctt->ctt_size <= CTF_MAX_SIZE) {
                    437:                size = ctt->ctt_size;
                    438:                toff = sizeof(struct ctf_stype);
                    439:        } else {
                    440:                size = CTF_TYPE_LSIZE(ctt);
                    441:                toff = sizeof(struct ctf_type);
                    442:        }
                    443:
                    444:        switch (kind) {
                    445:        case CTF_K_UNKNOWN:
                    446:        case CTF_K_FORWARD:
                    447:                break;
                    448:        case CTF_K_INTEGER:
                    449:                eob = *((uint32_t *)(p + toff));
                    450:                toff += sizeof(uint32_t);
                    451:                printf(" encoding=%s offset=%u bits=%u",
                    452:                    ctf_enc2name(CTF_INT_ENCODING(eob)), CTF_INT_OFFSET(eob),
                    453:                    CTF_INT_BITS(eob));
                    454:                break;
                    455:        case CTF_K_FLOAT:
                    456:                eob = *((uint32_t *)(p + toff));
                    457:                toff += sizeof(uint32_t);
1.7       uwe       458:                printf(" encoding=%s offset=%u bits=%u",
                    459:                    ctf_fpenc2name(CTF_FP_ENCODING(eob)), CTF_FP_OFFSET(eob),
                    460:                    CTF_FP_BITS(eob));
1.1       mpi       461:                break;
                    462:        case CTF_K_ARRAY:
                    463:                cta = (struct ctf_array *)(p + toff);
                    464:                printf(" content: %u index: %u nelems: %u\n", cta->cta_contents,
                    465:                    cta->cta_index, cta->cta_nelems);
                    466:                toff += sizeof(struct ctf_array);
                    467:                break;
                    468:        case CTF_K_FUNCTION:
                    469:                argp = (uint16_t *)(p + toff);
                    470:                printf(" returns: %u args: (%u", ctt->ctt_type, *argp);
                    471:                for (i = 1; i < vlen; i++) {
                    472:                        argp++;
                    473:                        printf(", %u", *argp);
                    474:                }
                    475:                printf(")");
                    476:                toff += (vlen + (vlen & 1)) * sizeof(uint16_t);
                    477:                break;
                    478:        case CTF_K_STRUCT:
                    479:        case CTF_K_UNION:
                    480:                printf(" (%llu bytes)\n", size);
                    481:
                    482:                if (size < CTF_LSTRUCT_THRESH) {
                    483:                        for (i = 0; i < vlen; i++) {
                    484:                                struct ctf_member       *ctm;
                    485:
                    486:                                ctm = (struct ctf_member *)(p + toff);
                    487:                                toff += sizeof(struct ctf_member);
                    488:
                    489:                                printf("\t%s type=%u off=%u\n",
                    490:                                    ctf_off2name(cth, data, dlen,
                    491:                                        ctm->ctm_name),
                    492:                                    ctm->ctm_type, ctm->ctm_offset);
                    493:                        }
                    494:                } else {
                    495:                        for (i = 0; i < vlen; i++) {
                    496:                                struct ctf_lmember      *ctlm;
                    497:
                    498:                                ctlm = (struct ctf_lmember *)(p + toff);
                    499:                                toff += sizeof(struct ctf_lmember);
                    500:
                    501:                                printf("\t%s type=%u off=%llu\n",
                    502:                                    ctf_off2name(cth, data, dlen,
                    503:                                        ctlm->ctlm_name),
                    504:                                    ctlm->ctlm_type, CTF_LMEM_OFFSET(ctlm));
                    505:                        }
                    506:                }
                    507:                break;
                    508:        case CTF_K_ENUM:
                    509:                printf("\n");
                    510:                for (i = 0; i < vlen; i++) {
                    511:                        struct ctf_enum *cte;
                    512:
                    513:                        cte = (struct ctf_enum *)(p + toff);
                    514:                        toff += sizeof(struct ctf_enum);
                    515:
                    516:                        printf("\t%s = %d\n",
                    517:                            ctf_off2name(cth, data, dlen, cte->cte_name),
                    518:                            cte->cte_value);
                    519:                }
                    520:                break;
                    521:        case CTF_K_POINTER:
                    522:        case CTF_K_TYPEDEF:
                    523:        case CTF_K_VOLATILE:
                    524:        case CTF_K_CONST:
                    525:        case CTF_K_RESTRICT:
                    526:                printf(" refers to %u", ctt->ctt_type);
                    527:                break;
                    528:        default:
                    529:                errx(1, "incorrect type %u at offset %u", kind, offset);
                    530:        }
                    531:
                    532:        printf("\n");
                    533:
                    534:        return toff;
                    535: }
                    536:
                    537: const char *
                    538: ctf_kind2name(uint16_t kind)
                    539: {
                    540:        static const char *kind_name[] = { NULL, "INTEGER", "FLOAT", "POINTER",
                    541:           "ARRAY", "FUNCTION", "STRUCT", "UNION", "ENUM", "FORWARD",
                    542:           "TYPEDEF", "VOLATILE", "CONST", "RESTRICT" };
                    543:
                    544:        if (kind >= nitems(kind_name))
                    545:                return NULL;
                    546:
                    547:        return kind_name[kind];
                    548: }
                    549:
                    550: const char *
                    551: ctf_enc2name(uint16_t enc)
                    552: {
                    553:        static const char *enc_name[] = { "SIGNED", "CHAR", "SIGNED CHAR",
                    554:            "BOOL", "SIGNED BOOL" };
                    555:        static char invalid[7];
                    556:
                    557:        if (enc == CTF_INT_VARARGS)
                    558:                return "VARARGS";
                    559:
1.8       uwe       560:        if (enc > 0 && enc <= nitems(enc_name))
1.7       uwe       561:                return enc_name[enc - 1];
                    562:
                    563:        snprintf(invalid, sizeof(invalid), "0x%x", enc);
                    564:        return invalid;
                    565: }
                    566:
                    567: const char *
                    568: ctf_fpenc2name(uint16_t enc)
                    569: {
                    570:        static const char *enc_name[] = { "SINGLE", "DOUBLE", NULL, NULL,
                    571:            NULL, "LDOUBLE" };
                    572:        static char invalid[7];
                    573:
                    574:        if (enc > 0 && enc <= nitems(enc_name) && enc_name[enc - 1] != NULL)
1.1       mpi       575:                return enc_name[enc - 1];
                    576:
                    577:        snprintf(invalid, sizeof(invalid), "0x%x", enc);
                    578:        return invalid;
                    579: }
                    580:
                    581: const char *
                    582: ctf_off2name(struct ctf_header *cth, const char *data, off_t dlen,
                    583:     uint32_t offset)
                    584: {
                    585:        const char              *name;
                    586:
                    587:        if (CTF_NAME_STID(offset) != CTF_STRTAB_0)
                    588:                return "external";
                    589:
                    590:        if (CTF_NAME_OFFSET(offset) >= cth->cth_strlen)
                    591:                return "exceeds strlab";
                    592:
                    593:        if (cth->cth_stroff + CTF_NAME_OFFSET(offset) >= dlen)
                    594:                return "invalid";
                    595:
                    596:        name = data + cth->cth_stroff + CTF_NAME_OFFSET(offset);
                    597:        if (*name == '\0')
                    598:                return "(anon)";
                    599:
                    600:        return name;
                    601: }
                    602:
                    603: char *
                    604: decompress(const char *buf, size_t size, off_t len)
                    605: {
                    606: #ifdef ZLIB
                    607:        z_stream                 stream;
                    608:        char                    *data;
                    609:        int                      error;
                    610:
                    611:        data = malloc(len);
                    612:        if (data == NULL) {
                    613:                warn(NULL);
                    614:                return NULL;
                    615:        }
                    616:
                    617:        memset(&stream, 0, sizeof(stream));
                    618:        stream.next_in = (void *)buf;
                    619:        stream.avail_in = size;
                    620:        stream.next_out = (uint8_t *)data;
                    621:        stream.avail_out = len;
                    622:
                    623:        if ((error = inflateInit(&stream)) != Z_OK) {
                    624:                warnx("zlib inflateInit failed: %s", zError(error));
                    625:                goto exit;
                    626:        }
                    627:
                    628:        if ((error = inflate(&stream, Z_FINISH)) != Z_STREAM_END) {
                    629:                warnx("zlib inflate failed: %s", zError(error));
                    630:                inflateEnd(&stream);
                    631:                goto exit;
                    632:        }
                    633:
                    634:        if ((error = inflateEnd(&stream)) != Z_OK) {
                    635:                warnx("zlib inflateEnd failed: %s", zError(error));
                    636:                goto exit;
                    637:        }
                    638:
                    639:        if (stream.total_out != len) {
                    640:                warnx("decompression failed: %llu != %llu",
                    641:                    stream.total_out, len);
                    642:                goto exit;
                    643:        }
                    644:
                    645:        return data;
                    646:
                    647: exit:
                    648:        free(data);
                    649: #endif /* ZLIB */
                    650:        return NULL;
                    651: }
                    652:
                    653: __dead void
                    654: usage(void)
                    655: {
                    656:        fprintf(stderr, "usage: %s [-dfhlst] file ...\n",
                    657:            getprogname());
                    658:        exit(1);
                    659: }