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

Annotation of src/usr.bin/nm/nm.c, Revision 1.16

1.16    ! millert     1: /*     $OpenBSD: nm.c,v 1.15 2001/11/19 19:02:15 mpech Exp $   */
1.2       deraadt     2: /*     $NetBSD: nm.c,v 1.7 1996/01/14 23:04:03 pk Exp $        */
1.1       deraadt     3:
                      4: /*
                      5:  * Copyright (c) 1989, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * This code is derived from software contributed to Berkeley by
                      9:  * Hans Huebner.
                     10:  *
                     11:  * Redistribution and use in source and binary forms, with or without
                     12:  * modification, are permitted provided that the following conditions
                     13:  * are met:
                     14:  * 1. Redistributions of source code must retain the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer.
                     16:  * 2. Redistributions in binary form must reproduce the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer in the
                     18:  *    documentation and/or other materials provided with the distribution.
                     19:  * 3. All advertising materials mentioning features or use of this software
                     20:  *    must display the following acknowledgement:
                     21:  *     This product includes software developed by the University of
                     22:  *     California, Berkeley and its contributors.
                     23:  * 4. Neither the name of the University nor the names of its contributors
                     24:  *    may be used to endorse or promote products derived from this software
                     25:  *    without specific prior written permission.
                     26:  *
                     27:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     28:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     29:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     30:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     31:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     32:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     33:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     34:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     35:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     36:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     37:  * SUCH DAMAGE.
                     38:  */
                     39:
                     40: #ifndef lint
                     41: static char copyright[] =
                     42: "@(#) Copyright (c) 1989, 1993\n\
                     43:        The Regents of the University of California.  All rights reserved.\n";
                     44: #endif /* not lint */
                     45:
                     46: #ifndef lint
                     47: #if 0
                     48: static char sccsid[] = "@(#)nm.c       8.1 (Berkeley) 6/6/93";
                     49: #endif
1.16    ! millert    50: static char rcsid[] = "$OpenBSD: nm.c,v 1.15 2001/11/19 19:02:15 mpech Exp $";
1.1       deraadt    51: #endif /* not lint */
                     52:
1.5       deraadt    53: #include <sys/param.h>
1.1       deraadt    54: #include <sys/types.h>
                     55: #include <a.out.h>
                     56: #include <stab.h>
                     57: #include <ar.h>
                     58: #include <ranlib.h>
                     59: #include <unistd.h>
1.2       deraadt    60: #include <err.h>
1.1       deraadt    61: #include <ctype.h>
1.14      espie      62: #include <link.h>
1.1       deraadt    63: #include <stdio.h>
                     64: #include <stdlib.h>
                     65: #include <string.h>
1.8       espie      66: /* XXX get shared code to handle byte-order swaps */
                     67: #include "byte.c"
1.7       niklas     68:
1.1       deraadt    69:
1.10      espie      70: int demangle = 0;
1.1       deraadt    71: int ignore_bad_archive_entries = 1;
                     72: int print_only_external_symbols;
                     73: int print_only_undefined_symbols;
                     74: int print_all_symbols;
                     75: int print_file_each_line;
1.12      espie      76: int show_extensions = 0;
1.1       deraadt    77: int fcount;
                     78:
                     79: int rev;
1.16    ! millert    80: int fname(const void *, const void *);
        !            81: int rname(const void *, const void *);
        !            82: int value(const void *, const void *);
        !            83: int (*sfunc)(const void *, const void *) = fname;
        !            84: char *otherstring(struct nlist *);
        !            85: char *typestring(unsigned int);
        !            86: char typeletter(unsigned int);
1.1       deraadt    87:
1.8       espie      88:
1.1       deraadt    89: /* some macros for symbol type (nlist.n_type) handling */
                     90: #define        IS_DEBUGGER_SYMBOL(x)   ((x) & N_STAB)
                     91: #define        IS_EXTERNAL(x)          ((x) & N_EXT)
                     92: #define        SYMBOL_TYPE(x)          ((x) & (N_TYPE | N_STAB))
                     93:
1.16    ! millert    94: void   *emalloc(size_t);
        !            95: void    pipe2cppfilt(void);
        !            96: void    usage(void);
        !            97: char   *symname(struct nlist *);
        !            98: void   print_symbol(const char *, struct nlist *);
1.10      espie      99:
1.1       deraadt   100: /*
                    101:  * main()
                    102:  *     parse command line, execute process_file() for each file
                    103:  *     specified on the command line.
                    104:  */
                    105: main(argc, argv)
                    106:        int argc;
                    107:        char **argv;
                    108: {
                    109:        extern int optind;
                    110:        int ch, errors;
                    111:
1.12      espie     112:        while ((ch = getopt(argc, argv, "aBCegnopruw")) != -1) {
1.1       deraadt   113:                switch (ch) {
                    114:                case 'a':
                    115:                        print_all_symbols = 1;
                    116:                        break;
1.10      espie     117:                case 'B':
                    118:                        /* no-op, compat with gnu-nm */
                    119:                        break;
                    120:                case 'C':
                    121:                        demangle = 1;
                    122:                        break;
1.12      espie     123:                case 'e':
                    124:                        show_extensions = 1;
                    125:                        break;
1.1       deraadt   126:                case 'g':
                    127:                        print_only_external_symbols = 1;
                    128:                        break;
                    129:                case 'n':
                    130:                        sfunc = value;
                    131:                        break;
                    132:                case 'o':
                    133:                        print_file_each_line = 1;
                    134:                        break;
                    135:                case 'p':
                    136:                        sfunc = NULL;
                    137:                        break;
                    138:                case 'r':
                    139:                        rev = 1;
                    140:                        break;
                    141:                case 'u':
                    142:                        print_only_undefined_symbols = 1;
                    143:                        break;
                    144:                case 'w':
                    145:                        ignore_bad_archive_entries = 0;
                    146:                        break;
                    147:                case '?':
                    148:                default:
                    149:                        usage();
                    150:                }
                    151:        }
1.10      espie     152:
                    153:        if (demangle)
                    154:                pipe2cppfilt();
1.1       deraadt   155:        fcount = argc - optind;
                    156:        argv += optind;
                    157:
                    158:        if (rev && sfunc == fname)
                    159:                sfunc = rname;
                    160:
                    161:        if (!fcount)
                    162:                errors = process_file("a.out");
                    163:        else {
                    164:                errors = 0;
                    165:                do {
                    166:                        errors |= process_file(*argv);
                    167:                } while (*++argv);
                    168:        }
                    169:        exit(errors);
                    170: }
                    171:
                    172: /*
                    173:  * process_file()
                    174:  *     show symbols in the file given as an argument.  Accepts archive and
                    175:  *     object files as input.
                    176:  */
                    177: process_file(fname)
                    178:        char *fname;
                    179: {
                    180:        struct exec exec_head;
                    181:        FILE *fp;
                    182:        int retval;
                    183:        char magic[SARMAG];
                    184:
                    185:        if (!(fp = fopen(fname, "r"))) {
1.6       deraadt   186:                warn("cannot read %s", fname);
1.1       deraadt   187:                return(1);
                    188:        }
                    189:
                    190:        if (fcount > 1)
                    191:                (void)printf("\n%s:\n", fname);
                    192:
                    193:        /*
                    194:         * first check whether this is an object file - read a object
                    195:         * header, and skip back to the beginning
                    196:         */
                    197:        if (fread((char *)&exec_head, sizeof(exec_head), (size_t)1, fp) != 1) {
1.2       deraadt   198:                warnx("%s: bad format", fname);
1.1       deraadt   199:                (void)fclose(fp);
                    200:                return(1);
                    201:        }
                    202:        rewind(fp);
                    203:
1.8       espie     204:        if (BAD_OBJECT(exec_head)) {
1.1       deraadt   205:        /* this could be an archive */
                    206:                if (fread(magic, sizeof(magic), (size_t)1, fp) != 1 ||
                    207:                    strncmp(magic, ARMAG, SARMAG)) {
1.2       deraadt   208:                        warnx("%s: not object file or archive", fname);
1.1       deraadt   209:                        (void)fclose(fp);
                    210:                        return(1);
                    211:                }
                    212:                retval = show_archive(fname, fp);
                    213:        } else
                    214:                retval = show_objfile(fname, fp);
                    215:        (void)fclose(fp);
                    216:        return(retval);
                    217: }
                    218:
                    219: /*
                    220:  * show_archive()
                    221:  *     show symbols in the given archive file
                    222:  */
                    223: show_archive(fname, fp)
                    224:        char *fname;
                    225:        FILE *fp;
                    226: {
                    227:        struct ar_hdr ar_head;
                    228:        struct exec exec_head;
                    229:        int i, rval;
                    230:        long last_ar_off;
                    231:        char *p, *name;
                    232:        int baselen, namelen;
                    233:
                    234:        baselen = strlen(fname) + 3;
                    235:        namelen = sizeof(ar_head.ar_name);
                    236:        name = emalloc(baselen + namelen);
                    237:
                    238:        rval = 0;
                    239:
                    240:        /* while there are more entries in the archive */
                    241:        while (fread((char *)&ar_head, sizeof(ar_head), (size_t)1, fp) == 1) {
                    242:                /* bad archive entry - stop processing this archive */
                    243:                if (strncmp(ar_head.ar_fmag, ARFMAG, sizeof(ar_head.ar_fmag))) {
1.2       deraadt   244:                        warnx("%s: bad format archive header", fname);
1.1       deraadt   245:                        (void)free(name);
                    246:                        return(1);
                    247:                }
                    248:
                    249:                /* remember start position of current archive object */
                    250:                last_ar_off = ftell(fp);
                    251:
                    252:                /* skip ranlib entries */
                    253:                if (!strncmp(ar_head.ar_name, RANLIBMAG, sizeof(RANLIBMAG) - 1))
                    254:                        goto skip;
                    255:
                    256:                /*
                    257:                 * construct a name of the form "archive.a:obj.o:" for the
                    258:                 * current archive entry if the object name is to be printed
                    259:                 * on each output line
                    260:                 */
                    261:                p = name;
                    262:                if (print_file_each_line)
                    263:                        p += sprintf(p, "%s:", fname);
                    264: #ifdef AR_EFMT1
                    265:                /*
                    266:                 * BSD 4.4 extended AR format: #1/<namelen>, with name as the
                    267:                 * first <namelen> bytes of the file
                    268:                 */
                    269:                if (            (ar_head.ar_name[0] == '#') &&
                    270:                                (ar_head.ar_name[1] == '1') &&
                    271:                                (ar_head.ar_name[2] == '/') &&
                    272:                                (isdigit(ar_head.ar_name[3]))) {
                    273:
                    274:                        int len = atoi(&ar_head.ar_name[3]);
                    275:                        if (len > namelen) {
                    276:                                p -= (long)name;
1.11      smart     277:                                if ((name = realloc(name, baselen+len)) == NULL)
                    278:                                        err(1, NULL);
1.1       deraadt   279:                                namelen = len;
                    280:                                p += (long)name;
                    281:                        }
                    282:                        if (fread(p, len, 1, fp) != 1) {
1.2       deraadt   283:                                warnx("%s: premature EOF", name);
1.1       deraadt   284:                                (void)free(name);
                    285:                                return 1;
                    286:                        }
                    287:                        p += len;
                    288:                } else
                    289: #endif
                    290:                for (i = 0; i < sizeof(ar_head.ar_name); ++i)
                    291:                        if (ar_head.ar_name[i] && ar_head.ar_name[i] != ' ')
                    292:                                *p++ = ar_head.ar_name[i];
                    293:                *p++ = '\0';
                    294:
                    295:                /* get and check current object's header */
                    296:                if (fread((char *)&exec_head, sizeof(exec_head),
                    297:                    (size_t)1, fp) != 1) {
1.2       deraadt   298:                        warnx("%s: premature EOF", name);
1.1       deraadt   299:                        (void)free(name);
                    300:                        return(1);
                    301:                }
                    302:
1.8       espie     303:                if (BAD_OBJECT(exec_head)) {
1.1       deraadt   304:                        if (!ignore_bad_archive_entries) {
1.2       deraadt   305:                                 warnx("%s: bad format", name);
1.1       deraadt   306:                                rval = 1;
                    307:                        }
                    308:                } else {
1.2       deraadt   309:                        (void)fseek(fp, (long)-sizeof(exec_head), SEEK_CUR);
1.1       deraadt   310:                        if (!print_file_each_line)
                    311:                                (void)printf("\n%s:\n", name);
                    312:                        rval |= show_objfile(name, fp);
                    313:                }
                    314:
                    315:                /*
                    316:                 * skip to next archive object - it starts at the next
                    317:                 * even byte boundary
                    318:                 */
                    319: #define even(x) (((x) + 1) & ~1)
                    320: skip:          if (fseek(fp, last_ar_off + even(atol(ar_head.ar_size)),
                    321:                    SEEK_SET)) {
1.2       deraadt   322:                        warn("%s", fname);
1.1       deraadt   323:                        (void)free(name);
                    324:                        return(1);
                    325:                }
                    326:        }
                    327:        (void)free(name);
                    328:        return(rval);
                    329: }
                    330:
                    331: /*
                    332:  * show_objfile()
                    333:  *     show symbols from the object file pointed to by fp.  The current
                    334:  *     file pointer for fp is expected to be at the beginning of an a.out
                    335:  *     header.
                    336:  */
                    337: show_objfile(objname, fp)
                    338:        char *objname;
                    339:        FILE *fp;
                    340: {
1.13      espie     341:        struct nlist *names, *np;
                    342:        struct nlist **snames;
1.15      mpech     343:        int i, nnames, nrawnames;
1.1       deraadt   344:        struct exec head;
                    345:        long stabsize;
                    346:        char *stab;
                    347:
                    348:        /* read a.out header */
                    349:        if (fread((char *)&head, sizeof(head), (size_t)1, fp) != 1) {
1.2       deraadt   350:                warnx("%s: cannot read header", objname);
1.1       deraadt   351:                return(1);
                    352:        }
                    353:
                    354:        /*
                    355:         * skip back to the header - the N_-macros return values relative
                    356:         * to the beginning of the a.out header
                    357:         */
                    358:        if (fseek(fp, (long)-sizeof(head), SEEK_CUR)) {
1.2       deraadt   359:                warn("%s", objname);
1.1       deraadt   360:                return(1);
                    361:        }
                    362:
1.8       espie     363:        /* stop if this is no valid object file, or a format we don't dare
                    364:         * playing with
                    365:         */
                    366:        if (BAD_OBJECT(head)) {
1.2       deraadt   367:                warnx("%s: bad format", objname);
1.1       deraadt   368:                return(1);
                    369:        }
                    370:
1.8       espie     371:        fix_header_order(&head);
                    372:
1.1       deraadt   373:        /* stop if the object file contains no symbol table */
                    374:        if (!head.a_syms) {
1.2       deraadt   375:                warnx("%s: no name list", objname);
1.1       deraadt   376:                return(1);
                    377:        }
                    378:
                    379:        if (fseek(fp, (long)N_SYMOFF(head), SEEK_CUR)) {
1.2       deraadt   380:                warn("%s", objname);
1.1       deraadt   381:                return(1);
                    382:        }
                    383:
                    384:        /* get memory for the symbol table */
                    385:        names = emalloc((size_t)head.a_syms);
                    386:        nrawnames = head.a_syms / sizeof(*names);
1.13      espie     387:        snames = emalloc(nrawnames*sizeof(struct nlist *));
1.1       deraadt   388:        if (fread((char *)names, (size_t)head.a_syms, (size_t)1, fp) != 1) {
1.2       deraadt   389:                warnx("%s: cannot read symbol table", objname);
1.1       deraadt   390:                (void)free((char *)names);
                    391:                return(1);
                    392:        }
1.8       espie     393:        fix_nlists_order(names, nrawnames, N_GETMID(head));
1.1       deraadt   394:
                    395:        /*
                    396:         * Following the symbol table comes the string table.  The first
                    397:         * 4-byte-integer gives the total size of the string table
                    398:         * _including_ the size specification itself.
                    399:         */
                    400:        if (fread((char *)&stabsize, sizeof(stabsize), (size_t)1, fp) != 1) {
1.2       deraadt   401:                warnx("%s: cannot read stab size", objname);
1.1       deraadt   402:                (void)free((char *)names);
                    403:                return(1);
                    404:        }
1.8       espie     405:        stabsize = fix_long_order(stabsize, N_GETMID(head));
1.1       deraadt   406:        stab = emalloc((size_t)stabsize);
                    407:
                    408:        /*
                    409:         * read the string table offset by 4 - all indices into the string
                    410:         * table include the size specification.
                    411:         */
                    412:        stabsize -= 4;          /* we already have the size */
                    413:        if (fread(stab + 4, (size_t)stabsize, (size_t)1, fp) != 1) {
1.2       deraadt   414:                warnx("%s: stab truncated..", objname);
1.1       deraadt   415:                (void)free((char *)names);
                    416:                (void)free(stab);
                    417:                return(1);
                    418:        }
                    419:
                    420:        /*
                    421:         * fix up the symbol table and filter out unwanted entries
                    422:         *
                    423:         * common symbols are characterized by a n_type of N_UNDF and a
                    424:         * non-zero n_value -- change n_type to N_COMM for all such
                    425:         * symbols to make life easier later.
                    426:         *
                    427:         * filter out all entries which we don't want to print anyway
                    428:         */
                    429:        for (np = names, i = nnames = 0; i < nrawnames; np++, i++) {
1.13      espie     430:                /*
                    431:                 * make n_un.n_name a character pointer by adding the string
                    432:                 * table's base to n_un.n_strx
                    433:                 *
                    434:                 * don't mess with zero offsets
                    435:                 */
                    436:                if (np->n_un.n_strx)
                    437:                        np->n_un.n_name = stab + np->n_un.n_strx;
                    438:                else
                    439:                        np->n_un.n_name = "";
1.1       deraadt   440:                if (SYMBOL_TYPE(np->n_type) == N_UNDF && np->n_value)
                    441:                        np->n_type = N_COMM | (np->n_type & N_EXT);
                    442:                if (!print_all_symbols && IS_DEBUGGER_SYMBOL(np->n_type))
                    443:                        continue;
                    444:                if (print_only_external_symbols && !IS_EXTERNAL(np->n_type))
                    445:                        continue;
                    446:                if (print_only_undefined_symbols &&
                    447:                    SYMBOL_TYPE(np->n_type) != N_UNDF)
                    448:                        continue;
                    449:
1.13      espie     450:                snames[nnames++] = np;
1.1       deraadt   451:        }
                    452:
                    453:        /* sort the symbol table if applicable */
                    454:        if (sfunc)
1.13      espie     455:                qsort(snames, (size_t)nnames, sizeof(*snames), sfunc);
1.1       deraadt   456:
                    457:        /* print out symbols */
1.13      espie     458:        for (i = 0; i < nnames; i++) {
                    459:                if (show_extensions && snames[i] != names &&
                    460:                    SYMBOL_TYPE((snames[i] -1)->n_type) == N_INDR)
                    461:                        continue;
                    462:                print_symbol(objname, snames[i]);
                    463:        }
1.1       deraadt   464:
1.13      espie     465:        (void)free(snames);
                    466:        (void)free(names);
1.1       deraadt   467:        (void)free(stab);
                    468:        return(0);
                    469: }
                    470:
1.13      espie     471: char *
                    472: symname(sym)
                    473:        struct nlist *sym;
                    474: {
                    475:        if (demangle && sym->n_un.n_name[0] == '_')
                    476:                return sym->n_un.n_name + 1;
                    477:        else
                    478:                return sym->n_un.n_name;
                    479: }
                    480:
1.1       deraadt   481: /*
                    482:  * print_symbol()
                    483:  *     show one symbol
                    484:  */
1.13      espie     485: void
1.1       deraadt   486: print_symbol(objname, sym)
1.13      espie     487:        const char *objname;
                    488:        struct nlist *sym;
1.1       deraadt   489: {
                    490:        if (print_file_each_line)
                    491:                (void)printf("%s:", objname);
                    492:
                    493:        /*
1.10      espie     494:         * handle undefined-only format especially (no space is
1.1       deraadt   495:         * left for symbol values, no type field is printed)
                    496:         */
1.10      espie     497:        if (!print_only_undefined_symbols) {
                    498:                /* print symbol's value */
1.13      espie     499:                if (SYMBOL_TYPE(sym->n_type) == N_UNDF ||
                    500:                    (show_extensions && SYMBOL_TYPE(sym->n_type) == N_INDR &&
                    501:                     sym->n_value == 0))
1.10      espie     502:                        (void)printf("        ");
                    503:                else
                    504:                        (void)printf("%08lx", sym->n_value);
                    505:
                    506:                /* print type information */
                    507:                if (IS_DEBUGGER_SYMBOL(sym->n_type))
                    508:                        (void)printf(" - %02x %04x %5s ", sym->n_other,
                    509:                            sym->n_desc&0xffff, typestring(sym->n_type));
1.12      espie     510:                else if (show_extensions)
                    511:                        (void)printf(" %c%2s ", typeletter(sym->n_type),
1.14      espie     512:                            otherstring(sym));
1.10      espie     513:                else
                    514:                        (void)printf(" %c ", typeletter(sym->n_type));
1.1       deraadt   515:        }
                    516:
1.13      espie     517:        if (SYMBOL_TYPE(sym->n_type) == N_INDR && show_extensions) {
                    518:                printf("%s -> %s\n", symname(sym), symname(sym+1));
                    519:        }
1.1       deraadt   520:        else
1.13      espie     521:                (void)puts(symname(sym));
1.12      espie     522: }
                    523:
                    524: char *
1.14      espie     525: otherstring(sym)
                    526:        struct nlist *sym;
1.12      espie     527: {
                    528:        static char buf[3];
                    529:        char *result;
                    530:
                    531:        result = buf;
                    532:
1.14      espie     533:        if (N_BIND(sym) == BIND_WEAK)
1.12      espie     534:                *result++ = 'w';
1.14      espie     535:        if (N_AUX(sym) == AUX_OBJECT)
1.12      espie     536:                *result++ = 'o';
1.14      espie     537:        else if (N_AUX(sym) == AUX_FUNC)
1.12      espie     538:                *result++ = 'f';
                    539:        *result++ = 0;
                    540:        return buf;
1.1       deraadt   541: }
                    542:
                    543: /*
                    544:  * typestring()
                    545:  *     return the a description string for an STAB entry
                    546:  */
                    547: char *
                    548: typestring(type)
1.14      espie     549:        unsigned int type;
1.1       deraadt   550: {
                    551:        switch(type) {
                    552:        case N_BCOMM:
                    553:                return("BCOMM");
                    554:        case N_ECOML:
                    555:                return("ECOML");
                    556:        case N_ECOMM:
                    557:                return("ECOMM");
                    558:        case N_ENTRY:
                    559:                return("ENTRY");
                    560:        case N_FNAME:
                    561:                return("FNAME");
                    562:        case N_FUN:
                    563:                return("FUN");
                    564:        case N_GSYM:
                    565:                return("GSYM");
                    566:        case N_LBRAC:
                    567:                return("LBRAC");
                    568:        case N_LCSYM:
                    569:                return("LCSYM");
                    570:        case N_LENG:
                    571:                return("LENG");
                    572:        case N_LSYM:
                    573:                return("LSYM");
                    574:        case N_PC:
                    575:                return("PC");
                    576:        case N_PSYM:
                    577:                return("PSYM");
                    578:        case N_RBRAC:
                    579:                return("RBRAC");
                    580:        case N_RSYM:
                    581:                return("RSYM");
                    582:        case N_SLINE:
                    583:                return("SLINE");
                    584:        case N_SO:
                    585:                return("SO");
                    586:        case N_SOL:
                    587:                return("SOL");
                    588:        case N_SSYM:
                    589:                return("SSYM");
                    590:        case N_STSYM:
                    591:                return("STSYM");
                    592:        }
                    593:        return("???");
                    594: }
                    595:
                    596: /*
                    597:  * typeletter()
                    598:  *     return a description letter for the given basic type code of an
                    599:  *     symbol table entry.  The return value will be upper case for
                    600:  *     external, lower case for internal symbols.
                    601:  */
                    602: char
                    603: typeletter(type)
1.14      espie     604:        unsigned int type;
1.1       deraadt   605: {
                    606:        switch(SYMBOL_TYPE(type)) {
                    607:        case N_ABS:
                    608:                return(IS_EXTERNAL(type) ? 'A' : 'a');
                    609:        case N_BSS:
                    610:                return(IS_EXTERNAL(type) ? 'B' : 'b');
                    611:        case N_COMM:
                    612:                return(IS_EXTERNAL(type) ? 'C' : 'c');
                    613:        case N_DATA:
                    614:                return(IS_EXTERNAL(type) ? 'D' : 'd');
                    615:        case N_FN:
                    616:                /* NOTE: N_FN == N_WARNING,
                    617:                 * in this case, the N_EXT bit is to considered as
                    618:                 * part of the symbol's type itself.
                    619:                 */
                    620:                return(IS_EXTERNAL(type) ? 'F' : 'W');
                    621:        case N_TEXT:
                    622:                return(IS_EXTERNAL(type) ? 'T' : 't');
                    623:        case N_INDR:
                    624:                return(IS_EXTERNAL(type) ? 'I' : 'i');
                    625:        case N_SIZE:
                    626:                return(IS_EXTERNAL(type) ? 'S' : 's');
                    627:        case N_UNDF:
                    628:                return(IS_EXTERNAL(type) ? 'U' : 'u');
                    629:        }
                    630:        return('?');
                    631: }
                    632:
1.13      espie     633: int
1.1       deraadt   634: fname(a0, b0)
1.13      espie     635:        const void *a0, *b0;
1.1       deraadt   636: {
1.13      espie     637:        struct nlist * const *a = a0, * const *b = b0;
1.1       deraadt   638:
1.13      espie     639:        return(strcmp((*a)->n_un.n_name, (*b)->n_un.n_name));
1.1       deraadt   640: }
                    641:
1.13      espie     642: int
1.1       deraadt   643: rname(a0, b0)
1.13      espie     644:        const void *a0, *b0;
1.1       deraadt   645: {
1.13      espie     646:        struct nlist * const *a = a0, * const *b = b0;
1.1       deraadt   647:
1.13      espie     648:        return(strcmp((*b)->n_un.n_name, (*a)->n_un.n_name));
1.1       deraadt   649: }
                    650:
1.13      espie     651: int
1.1       deraadt   652: value(a0, b0)
1.13      espie     653:        const void *a0, *b0;
1.1       deraadt   654: {
1.13      espie     655:        struct nlist * const *a = a0, * const *b = b0;
1.1       deraadt   656:
1.13      espie     657:        if (SYMBOL_TYPE((*a)->n_type) == N_UNDF)
                    658:                if (SYMBOL_TYPE((*b)->n_type) == N_UNDF)
1.1       deraadt   659:                        return(0);
                    660:                else
                    661:                        return(-1);
1.13      espie     662:        else if (SYMBOL_TYPE((*b)->n_type) == N_UNDF)
1.1       deraadt   663:                return(1);
                    664:        if (rev) {
1.13      espie     665:                if ((*a)->n_value == (*b)->n_value)
1.1       deraadt   666:                        return(rname(a0, b0));
1.13      espie     667:                return((*b)->n_value > (*a)->n_value ? 1 : -1);
1.1       deraadt   668:        } else {
1.13      espie     669:                if ((*a)->n_value == (*b)->n_value)
1.1       deraadt   670:                        return(fname(a0, b0));
1.13      espie     671:                return((*a)->n_value > (*b)->n_value ? 1 : -1);
1.1       deraadt   672:        }
                    673: }
                    674:
                    675: void *
                    676: emalloc(size)
                    677:        size_t size;
                    678: {
                    679:        char *p;
                    680:
                    681:        /* NOSTRICT */
                    682:        if (p = malloc(size))
                    683:                return(p);
1.2       deraadt   684:        err(1, NULL);
1.1       deraadt   685: }
                    686:
1.10      espie     687: #define CPPFILT        "/usr/bin/c++filt"
                    688:
                    689: void
                    690: pipe2cppfilt()
                    691: {
                    692:        int pip[2];
                    693:        char *argv[2];
                    694:
                    695:        argv[0] = "c++filt";
                    696:        argv[1] = NULL;
                    697:
                    698:        if (pipe(pip) == -1)
                    699:                err(1, "pipe");
                    700:        switch(fork()) {
                    701:        case -1:
                    702:                err(1, "fork");
                    703:        default:
                    704:                dup2(pip[0], 0);
                    705:                close(pip[0]);
                    706:                close(pip[1]);
                    707:                execve(CPPFILT, argv, NULL);
                    708:                err(1, "execve");
                    709:        case 0:
                    710:                dup2(pip[1], 1);
                    711:                close(pip[1]);
                    712:                close(pip[0]);
                    713:        }
                    714: }
                    715:
1.11      smart     716: void
1.1       deraadt   717: usage()
                    718: {
1.10      espie     719:        (void)fprintf(stderr, "usage: nm [-aCgnopruw] [file ...]\n");
1.1       deraadt   720:        exit(1);
                    721: }