home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / binutils-1.8.x.tar.gz / binutils-1.8.x.tar / binutils / nm.c < prev    next >
C/C++ Source or Header  |  1990-06-11  |  27KB  |  1,178 lines

  1. /* Describe symbol table of a rel file.
  2.    Copyright (C) 1986, 1988 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <stdio.h>
  19. #include <ar.h>
  20. #include <sys/types.h>
  21. #include <sys/file.h>
  22. #include "getopt.h"
  23.  
  24. #if !defined(A_OUT) && !defined(MACH_O)
  25. #define A_OUT
  26. #endif
  27.  
  28. #ifdef A_OUT
  29. #ifdef COFF_ENCAPSULATE
  30. #include "a.out.encap.h"
  31. #else
  32. /* On native BSD systems, use the system's own a.out.h.  */
  33. #include <a.out.h>
  34. #endif
  35. #endif
  36.  
  37. #ifdef MACH_O
  38. #ifndef A_OUT
  39. #include <nlist.h>
  40. #ifndef N_TEXT
  41. #define N_TEXT 4
  42. #define N_DATA 6
  43. #define N_BSS 8
  44. #endif
  45. #ifndef N_FN
  46. #define N_FN 15
  47. #endif
  48. #endif
  49. #include <sys/loader.h>
  50. #endif
  51.  
  52. /* Always use the GNU version of debugging symbol type codes, if possible.  */
  53. #include "stab.h"
  54.  
  55. /* Struct or union for header of object file.  */
  56.  
  57. #ifdef USG
  58. #include <string.h>
  59. /* You might need to compile with -I/usr/include/sys if your fcntl.h
  60.    isn't in /usr/include (which is where it should be according to POSIX).  */
  61. #include <fcntl.h>
  62. #else
  63. #include <strings.h>
  64. #endif
  65.  
  66. /* Alloca definitions and includes...  */
  67.  
  68. /* If compiled with GNU C, use the built-in alloca */
  69. #ifdef __GNUC__
  70. #define alloca __builtin_alloca
  71. #else
  72. /* If on a sun 4, make sure to include the right definition.  */
  73. #if defined(sun) && defined(sparc)
  74. #include "alloca.h"
  75. #else
  76. char *alloca ();
  77. #endif
  78. #endif
  79.  
  80. char *malloc (), *realloc ();
  81.  
  82. char *xmalloc (), *xrealloc ();
  83.  
  84. /* C++ demangler stuff.  */
  85. char *cplus_demangle ();
  86.  
  87. /* Print NAME on STREAM, demangling if necessary.  */
  88. void
  89. fprint_name (stream, name)
  90.      FILE *stream;
  91.      char *name;
  92. {
  93.   char *demangled = cplus_demangle (name);
  94.   if (demangled == NULL)
  95.     fputs (name, stream);
  96.   else
  97.     {
  98.       fputs (demangled, stream);
  99.       free (demangled);
  100.     }
  101. }
  102.  
  103. /* Special global symbol types understood by GNU LD.  */
  104.  
  105. /* The following type indicates the definition of a symbol as being
  106.    an indirect reference to another symbol.  The other symbol
  107.    appears as an undefined reference, immediately following this symbol.
  108.  
  109.    Indirection is asymmetrical.  The other symbol's value will be used
  110.    to satisfy requests for the indirect symbol, but not vice versa.
  111.    If the other symbol does not have a definition, libraries will
  112.    be searched to find a definition.  */
  113. #ifndef N_INDR
  114. #define N_INDR 0xa
  115. #endif
  116.  
  117. /* Warning message symbol.  The name of this symbol will be printed if
  118.    any symbols from it's file are required by the linker.  */
  119. #ifndef N_WARNING
  120. #define N_WARNING 0x1e
  121. #endif
  122.    
  123.  
  124. /* The following symbols refer to set elements.
  125.    All the N_SET[ATDB] symbols with the same name form one set.
  126.    Space is allocated for the set in the text section, and each set
  127.    element's value is stored into one word of the space.
  128.    The first word of the space is the length of the set (number of elements).
  129.  
  130.    The address of the set is made into an N_SETV symbol
  131.    whose name is the same as the name of the set.
  132.    This symbol acts like a N_DATA global symbol
  133.    in that it can satisfy undefined external references.  */
  134.  
  135. #ifndef N_SETA
  136. #define    N_SETA    0x14        /* Absolute set element symbol */
  137. #endif                /* This is input to LD, in a .o file.  */
  138.  
  139. #ifndef N_SETT
  140. #define    N_SETT    0x16        /* Text set element symbol */
  141. #endif                /* This is input to LD, in a .o file.  */
  142.  
  143. #ifndef N_SETD
  144. #define    N_SETD    0x18        /* Data set element symbol */
  145. #endif                /* This is input to LD, in a .o file.  */
  146.  
  147. #ifndef N_SETB
  148. #define    N_SETB    0x1A        /* Bss set element symbol */
  149. #endif                /* This is input to LD, in a .o file.  */
  150.  
  151. /* Macros dealing with the set element symbols defined in a.out.h */
  152. #define    SET_ELEMENT_P(x)    ((x)>=N_SETA&&(x)<=(N_SETB|N_EXT))
  153. #define TYPE_OF_SET_ELEMENT(x)    ((x)-N_SETA+N_ABS)
  154.  
  155. #ifndef N_SETV
  156. #define N_SETV    0x1C        /* Pointer to set vector in text area.  */
  157. #endif                /* This is output from LD.  */
  158.  
  159. #ifndef __GNU_STAB__
  160.  
  161. /* Line number for the data section.  This is to be used to describe
  162.    the source location of a variable declaration.  */
  163. #ifndef N_DSLINE
  164. #define N_DSLINE (N_SLINE+N_DATA-N_TEXT)
  165. #endif
  166.  
  167. /* Line number for the bss section.  This is to be used to describe
  168.    the source location of a variable declaration.  */
  169. #ifndef N_BSLINE
  170. #define N_BSLINE (N_SLINE+N_BSS-N_TEXT)
  171. #endif
  172.  
  173. #endif /* not __GNU_STAB__ */
  174.  
  175. /* Name of this program.  */
  176.  
  177. char *program_name;
  178.  
  179. /* Number of input files specified.  */
  180.  
  181. int number_of_files;
  182.  
  183. /* Current file's name.  */
  184.  
  185. char *input_name;
  186.  
  187. /* Current member's name, or 0 if processing a non-library file.  */
  188.  
  189. char *input_member;
  190.  
  191. #ifdef MACH_O
  192. /* Section ordinals for N_SECT references.  */
  193. int text_section;
  194. int data_section;
  195. int bss_section;
  196. #endif
  197.  
  198. /* Offset within archive of the current member,
  199.    if we are processing an archive.  */
  200.  
  201. int member_offset;
  202.  
  203. /* Command options.  */
  204.  
  205. int external_only;    /* nonzero means print external symbols only.  */
  206. int sort_numerically;   /* sort in numerical order rather than alphabetic  */
  207. int reverse_sort;    /* sort in downward (alphabetic or numeric) order.  */
  208. int no_sort;        /* don't sort; print symbols in order in the file.  */
  209. int undefined_only;    /* print undefined symbols only.  */
  210. int file_on_each_line;    /* print file name on each line.  */
  211. int debugger_syms;    /* print the debugger-only symbols.  */
  212. int print_symdefs;    /* describe the __.SYMDEF data in any archive file specified.  */
  213.  
  214. /* The __.SYMDEF member of an archive has the following format:
  215.    1) A longword saying the size of the symdef data that follows
  216.    2) Zero or more  struct symdef  filling that many bytes
  217.    3) A longword saying how many bytes of strings follow
  218.    4) That many bytes of string data.
  219. */
  220.  
  221. struct symdef
  222.   {
  223.     long stringoffset;    /* Offset of this symbol's name in the string data */
  224.     long offset;    /* Offset in the archive of the header-data for the member that
  225.                defines this symbol.  */
  226.   };
  227.  
  228. /* Create a table of debugging stab-codes and corresponding names.  */
  229. #ifdef __GNU_STAB__
  230. #define __define_stab(NAME, CODE, STRING) {NAME, STRING},
  231. struct {enum __stab_debug_code code; char *string;} stab_names[]
  232.   = {
  233. #include "stab.def"
  234.     };
  235. #undef __define_stab
  236. #endif
  237.  
  238. void decode_switch ();
  239. int decode_arg ();
  240. void do_one_file (), do_one_rel_file (), do_symdef_member ();
  241. char *concat ();
  242.  
  243. main (argc, argv)
  244.      char **argv;
  245.      int argc;
  246. {
  247.   int i;
  248.   int c;
  249.   extern int optind;
  250.   int ind;
  251.   static struct option long_options[] = 
  252.     {
  253.       {"all",        0, &debugger_syms,    1},
  254.       {"extern-only",    0, &external_only,    1},
  255.       {"numeric-sort",    0, &sort_numerically,    1},
  256.       {"print-file-name", 0, &file_on_each_line,1},
  257.       {"no-sort",    0, &no_sort,        1},
  258.       {"reverse",    0, &reverse_sort,    1},
  259.       {"print-symdefs",    0, &print_symdefs,    1},
  260.       {"undefined-only",0, &undefined_only,    1},
  261.       {NULL, 0, NULL, 0}
  262.     };
  263.  
  264.   program_name = argv[0];
  265.  
  266.   number_of_files = 0;
  267.   external_only = 0;
  268.   sort_numerically = 0;
  269.   reverse_sort = 0;
  270.   no_sort = 0;
  271.   undefined_only = 0;
  272.   file_on_each_line = 0;
  273.   debugger_syms = 0;
  274.   print_symdefs = 0;
  275.  
  276.   while ((c = getopt_long (argc, argv, "agnoprsu", long_options, &ind)) != EOF)
  277.     switch (c)
  278.       {
  279.       case 0:
  280.           break;
  281.     
  282.       case 'a':
  283.     debugger_syms = 1;
  284.     break;
  285.  
  286.       case 'g':
  287.     external_only = 1;
  288.     break;
  289.  
  290.       case 'n':
  291.     sort_numerically = 1;
  292.     break;
  293.  
  294.       case 'o':
  295.     file_on_each_line = 1;
  296.     break;
  297.  
  298.       case 'p':
  299.     no_sort = 1;
  300.     break;
  301.  
  302.       case 'r':
  303.     reverse_sort = 1;
  304.     break;
  305.  
  306.       case 's':
  307.     print_symdefs = 1;
  308.     break;
  309.  
  310.       case 'u':
  311.     undefined_only = 1;
  312.     break;
  313.  
  314.       default:
  315.     fprintf (stderr, "\
  316. Usage: %s [-agnoprsu] [+all] [+extern-only] [+numeric-sort]\n\
  317.        [+print-file-name] [+no-sort] [+reverse] [+print-symdefs]\n\
  318.        [+undefined-only] [file...]\n", program_name);
  319.     exit (1);
  320.     break;
  321.       }
  322.  
  323.   number_of_files = argc - optind;
  324.  
  325.   /* Now scan again and print the files.  */
  326.  
  327.   if (argc == optind)
  328.     do_one_file ("a.out");
  329.   else
  330.     for (i = optind; i < argc; i++)
  331.       do_one_file (argv[i]);
  332.   exit (0);
  333. }
  334.  
  335. /* Print the filename of the current file on 'outfile' (a stdio stream).  */
  336.  
  337. print_file_name (outfile)
  338.      FILE *outfile;
  339. {
  340.   fprintf (outfile, "%s", input_name);
  341.   if (input_member)
  342.     fprintf (outfile, "(%s)", input_member);
  343. }
  344.  
  345. /* process one input file */
  346. void scan_library ();
  347.  
  348. void
  349. do_one_file (name)
  350.      char *name;
  351. {
  352.   int len, desc, nchars;
  353.   char armag[SARMAG];
  354.  
  355.   desc = open (name, O_RDONLY, 0);
  356.  
  357.   if (desc < 0)
  358.     {
  359.       perror_name (name);
  360.       return;
  361.     }
  362.  
  363.   input_name = name;
  364.   input_member = 0;
  365.  
  366.   nchars = read (desc, armag, SARMAG);
  367.   if (nchars == SARMAG && !strncmp(armag, ARMAG, SARMAG))
  368.     scan_library (desc);
  369.   else
  370.     do_one_rel_file (desc, 0);
  371.  
  372.   close (desc);
  373. }
  374.  
  375. /* Read in the archive data about one member.
  376.    SUBFILE_OFFSET is the address within the archive of the start of that data.
  377.    The value returned is the length of the member's contents, which does
  378.    not include the archive data about the member.
  379.    A pointer to the member's name is stored into *MEMBER_NAME_PTR.
  380.  
  381.    If there are no more valid members, zero is returned.  */
  382.  
  383. int
  384. decode_library_subfile (desc, subfile_offset, member_name_ptr)
  385.      int desc;
  386.      int subfile_offset;
  387.      char **member_name_ptr;
  388. {
  389.   int bytes_read;
  390.   int namelen;
  391.   int member_length;
  392.   char *name;
  393.   struct ar_hdr hdr1;
  394.  
  395.   lseek (desc, subfile_offset, 0);
  396.  
  397.   bytes_read = read (desc, &hdr1, sizeof hdr1);
  398.   if (!bytes_read)
  399.     ;        /* end of archive */
  400.  
  401.   else if (sizeof hdr1 != bytes_read)
  402.     error_with_file ("malformed library archive ");
  403.  
  404.   else if (sscanf (hdr1.ar_size, "%d", &member_length) != 1)
  405.     error_with_file ("malformatted header of archive member in ");
  406.  
  407.   else
  408.     {
  409.       for (namelen = 0; ; namelen++)
  410.     if (hdr1.ar_name[namelen] == 0 || hdr1.ar_name[namelen] == ' '
  411.         /* Some systems use a slash?  Strange.  */
  412.         || hdr1.ar_name[namelen] == '/')
  413.       break;
  414.  
  415.       name = (char *) xmalloc (namelen+1);
  416.       strncpy (name, hdr1.ar_name, namelen);
  417.       name[namelen] = 0;
  418.  
  419.       *member_name_ptr = name;
  420.  
  421.       return member_length;
  422.     }
  423.   return 0;   /* tell caller to exit loop */
  424. }
  425.  
  426. /* Scan a library and describe each member.  */
  427.  
  428. void
  429. scan_library (desc)
  430.      int desc;
  431. {
  432.   int this_subfile_offset = SARMAG;
  433.   int member_length;
  434.  
  435.   if (!file_on_each_line)
  436.     printf ("\n%s:\n", input_name);
  437.   
  438.   while (1)
  439.     {
  440.       member_length
  441.     = decode_library_subfile (desc, this_subfile_offset, &input_member);
  442.       if (member_length == 0)
  443.     break;
  444.  
  445.       /* Describe every member except the ranlib data if any.  */
  446.  
  447.       if (strcmp (input_member, "__.SYMDEF"))
  448.     do_one_rel_file (desc, this_subfile_offset + sizeof (struct ar_hdr));
  449.       else if (print_symdefs)
  450.     do_symdef_member (desc, this_subfile_offset + sizeof (struct ar_hdr), member_length);
  451.  
  452.       this_subfile_offset += ((member_length + sizeof (struct ar_hdr)) + 1) & -2;
  453.     }
  454. }
  455.  
  456. /* Read a file's header and fill in various pieces of information.
  457.    Return 0 on failure.  */
  458.  
  459. int
  460. read_header_info (desc, offset, syms_offset, syms_size, strs_offset, strs_size)
  461.      int desc;
  462.      long int offset;
  463.      long int *syms_offset;
  464.      unsigned int *syms_size;
  465.      long int *strs_offset;
  466.      unsigned int *strs_size;
  467. {
  468.   int len;
  469.  
  470. #ifdef A_OUT
  471.   {
  472.     struct exec hdr;
  473.  
  474.     lseek (desc, offset, 0);
  475. #ifdef HEADER_SEEK_FD
  476.     /* Skip the headers that encapsulate our data in some other format
  477.        such as COFF.  */
  478.     HEADER_SEEK_FD (desc);
  479. #endif
  480.     len = read (desc, (char *) &hdr, sizeof (struct exec));
  481.     if (len == sizeof (struct exec) && !N_BADMAG (hdr))
  482.       {
  483.     *syms_offset = N_SYMOFF(hdr);
  484.     *syms_size = hdr.a_syms;
  485.     *strs_offset = N_STROFF(hdr);
  486.     lseek(desc, N_STROFF(hdr) + offset, 0);
  487.     if (read (desc, (char *) strs_size, sizeof *strs_size) != sizeof *strs_size)
  488.       {
  489.         error_with_file ("cannot read string table size in ");
  490.         return 0;
  491.       }
  492.     return 1;
  493.       }
  494.   }
  495. #endif
  496.  
  497. #ifdef MACH_O
  498.   {
  499.     struct mach_header mach_header;
  500.     char *hdrbuf;
  501.     struct load_command *load_command;
  502.     struct segment_command *segment_command;
  503.     struct section *section;
  504.     struct symtab_command *symtab_command;
  505.     int symtab_seen;
  506.     int len, cmd, seg, ordinal;
  507.  
  508.     symtab_seen = 0;
  509.  
  510.     lseek (desc, offset, 0);
  511.     len = read (desc, (char *) &mach_header, sizeof (struct mach_header));
  512.     if (len == sizeof (struct mach_header) && mach_header.magic == MH_MAGIC)
  513.       {
  514.     hdrbuf = xmalloc (mach_header.sizeofcmds);
  515.     len = read (desc, hdrbuf, mach_header.sizeofcmds);
  516.     if (len != mach_header.sizeofcmds)
  517.       {
  518.         error_with_file ("failure reading Mach-O load commands in ");
  519.         return 0;
  520.       }
  521.     load_command = (struct load_command *) hdrbuf;
  522.     ordinal = 1;
  523.     for (cmd = 0; cmd < mach_header.ncmds; ++cmd)
  524.       {
  525.         switch (load_command->cmd)
  526.           {
  527.           case LC_SEGMENT:
  528.         segment_command = (struct segment_command *) load_command;
  529.         section = (struct section *) ((char *) (segment_command + 1));
  530.         for (seg = 0; seg < segment_command->nsects; ++seg, ++section, ++ordinal)
  531.           {
  532.             if (!strncmp(SECT_TEXT, section->sectname, sizeof section->sectname))
  533.               text_section = ordinal;
  534.             else if (!strncmp(SECT_DATA, section->sectname, sizeof section->sectname))
  535.               data_section = ordinal;
  536.             else if (!strncmp(SECT_BSS, section->sectname, sizeof section->sectname))
  537.               bss_section = ordinal;
  538.           }
  539.         break;
  540.           case LC_SYMTAB:
  541.         if (symtab_seen)
  542.           error_with_file ("more than one LC_SYMTAB in ");
  543.         else
  544.           {
  545.             symtab_seen = 1;
  546.             symtab_command = (struct symtab_command *) load_command;
  547.             *syms_offset = symtab_command->symoff;
  548.             *syms_size = symtab_command->nsyms * sizeof (struct nlist);
  549.             *strs_offset = symtab_command->stroff;
  550.             *strs_size = symtab_command->strsize;
  551.           }
  552.         break;
  553.           }
  554.         load_command = (struct load_command *)
  555.           ((char *) load_command + load_command->cmdsize);
  556.       }
  557.     free (hdrbuf);
  558.     return 1;
  559.       }
  560.   }
  561. #endif
  562.  
  563.   return 0;
  564. }
  565.  
  566. void print_symbols (), print_one_symbol ();
  567. void read_header ();
  568. int alphacompare (), valuecompare ();
  569. int filter_symbols ();
  570.  
  571. void
  572. do_one_rel_file (desc, offset)
  573.      int desc;
  574.      int offset;
  575. {
  576.   struct nlist *symbols_and_strings;
  577.   int symcount;
  578.   int totalsize;
  579.   char *strings;
  580.   long int syms_offset, strs_offset;
  581.   unsigned int syms_size, strs_size;
  582.  
  583.   if (!read_header_info (desc, offset, &syms_offset, &syms_size, &strs_offset, &strs_size))
  584.     {
  585.       error_with_file ("malformed input (not a rel file or archive) in ");
  586.       return;
  587.     }
  588.  
  589.   /* Number of symbol entries in the file.  */
  590.   symcount = syms_size / sizeof (struct nlist);
  591.  
  592.   totalsize = strs_size + syms_size;
  593.  
  594.   /* Allocate space for symbol entries and string table.  */
  595.   symbols_and_strings = (struct nlist *) xmalloc (totalsize);
  596.   strings = (char *) symbols_and_strings + syms_size;
  597.  
  598.   /* Read them both in.  */
  599.   lseek (desc, syms_offset + offset, 0);
  600.   if (syms_size != read (desc, (char *) symbols_and_strings, syms_size))
  601.     {
  602.       error_with_file ("premature end of file in symbols of ");
  603.       return;
  604.     }
  605.   lseek (desc, strs_offset + offset, 0);
  606.   if (strs_size != read (desc, (char *) strings, strs_size))
  607.     {
  608.       error_with_file ("premature end of file in strings of ");
  609.       return;
  610.     }
  611.  
  612.   /* Identify this file, if desired.  */
  613.  
  614.   if (!file_on_each_line && (number_of_files > 1 || input_member))
  615.     printf ("\n%s:\n", input_member ? input_member : input_name);
  616.  
  617.   /* Discard the symbols we don't want to print; compact the rest down.  */
  618.  
  619.   symcount = filter_symbols (symbols_and_strings, symcount, strings);
  620.     
  621.   /* Modify each symbol entry to point directly at the symbol name.
  622.      This is so the sort routine does not need to be passed
  623.      the value of `strings' separately.  */
  624.  
  625.   {
  626.     struct nlist *p = symbols_and_strings;
  627.     struct nlist *end = symbols_and_strings + symcount;
  628.  
  629.     for (; p < end; p++)
  630.       {
  631.     /* A zero index means there is no string.  */
  632.     if (p->n_un.n_strx != 0)
  633.       {
  634.         if (p->n_un.n_strx > 0 && p->n_un.n_strx < strs_size)
  635.           p->n_un.n_name = strings + p->n_un.n_strx;
  636.         else
  637.           {
  638.         error_with_file ("bad string table offset in ");
  639.         return;
  640.           }
  641.       }
  642.       }
  643.   }
  644.  
  645.   /* Sort the symbols if desired.  */
  646.  
  647.   if (!no_sort)
  648.     qsort (symbols_and_strings, symcount, sizeof (struct nlist),
  649.        sort_numerically ? valuecompare : alphacompare);
  650.  
  651.   /* Print the symbols in the order they are now in.  */
  652.  
  653.   print_symbols (symbols_and_strings, symcount);
  654.  
  655.   free (symbols_and_strings);
  656. }
  657.  
  658. /* Choose which symbol entries to print;
  659.    compact them downward to get rid of the rest.
  660.    Return the number of symbols to be printed.  */
  661.  
  662. int
  663. filter_symbols (syms, symcount, strings)
  664.      struct nlist *syms;
  665.      int symcount;
  666.      char *strings;
  667. {
  668.   struct nlist *from = syms, *to = syms;
  669.   struct nlist *end = syms + symcount;
  670.  
  671.   while (from < end)
  672.     {
  673.       int keep = 0;
  674.  
  675.       /* undefined sym or common sym */
  676.       if (from->n_type == N_EXT) keep = !undefined_only || !from->n_value;
  677.       /* global defined sym */
  678.       else if (from->n_type & N_EXT) keep = !undefined_only;
  679.       /* debugger sym: normally don't print */
  680.       else if (from->n_type & ~(N_TYPE | N_EXT)) keep = debugger_syms;
  681.       /* local sym */
  682.       else keep = !external_only && !undefined_only;
  683.  
  684.       if (keep)
  685.     *to++ = *from;
  686.       from++;
  687.     }
  688.  
  689.   return to - syms;
  690. }
  691.  
  692. /* Comparison functions for sorting symbols.  */
  693.  
  694. int
  695. alphacompare (sym1, sym2)
  696.      struct nlist *sym1, *sym2;
  697. {
  698.   if (reverse_sort)
  699.     {
  700.       if (!sym2->n_un.n_name)
  701.     {
  702.       if (sym1->n_un.n_name) return -1;
  703.       else return 0;
  704.     }
  705.       if (!sym1->n_un.n_name) return 1;
  706.       return strcmp (sym2->n_un.n_name, sym1->n_un.n_name);
  707.     }
  708.   else
  709.     {
  710.       if (!sym1->n_un.n_name)
  711.     {
  712.       if (sym2->n_un.n_name) return -1;
  713.       else return 0;
  714.     }
  715.       if (!sym2->n_un.n_name) return 1;
  716.       return strcmp (sym1->n_un.n_name, sym2->n_un.n_name);
  717.     }
  718. }
  719.  
  720.  
  721. int
  722. valuecompare (sym1, sym2)
  723.      struct nlist *sym1, *sym2;
  724. {
  725.   if (reverse_sort)
  726.     return sym2->n_value - sym1->n_value;
  727.   else
  728.     return sym1->n_value - sym2->n_value;
  729. }
  730.  
  731. void
  732. print_symbols (syms, symcount)
  733.      struct nlist *syms;
  734.      int symcount;
  735. {
  736.   int i;
  737.  
  738.   for (i = 0; i < symcount; i++)
  739.     print_one_symbol (&syms[i]);
  740. }
  741.  
  742. void
  743. print_one_symbol (sym)
  744.      struct nlist *sym;
  745. {
  746.   if (file_on_each_line)
  747.     {
  748.       print_file_name (stdout);
  749.       printf (":");
  750.     }
  751.  
  752.   if (undefined_only)
  753.     {
  754.       if (sym->n_type == N_EXT && !sym->n_value)
  755.     {
  756.       fprint_name (stdout, sym->n_un.n_name);
  757.       printf ("\n");
  758.     }
  759.       return;
  760.     }
  761.  
  762.   if (sym->n_type & ~N_EXT || sym->n_value)
  763.     printf ("%08x ", sym->n_value);
  764.   else printf ("         ");
  765.  
  766.   switch (sym->n_type)
  767.     {
  768.       case N_EXT:
  769.         if (sym->n_value) printf ("C");
  770.         else printf ("U");
  771.     break;
  772.  
  773.       case 0:
  774.         if (sym->n_value) printf ("c");
  775.         else printf ("u");
  776.     break;
  777.  
  778.       case N_ABS | N_EXT:
  779.     printf ("A");
  780.     break;
  781.  
  782.       case N_ABS:
  783.     printf ("a");
  784.     break;
  785.  
  786.       case N_TEXT | N_EXT:
  787.     printf ("T");
  788.     break;
  789.  
  790.       case N_TEXT:
  791.     printf ("t");
  792.     break;
  793.  
  794.       case N_DATA | N_EXT:
  795.     printf ("D");
  796.     break;
  797.  
  798.       case N_DATA:
  799.     printf ("d");
  800.     break;
  801.  
  802.       case N_BSS | N_EXT:
  803.     printf ("B");
  804.     break;
  805.  
  806.       case N_BSS:
  807.     printf ("b");
  808.     break;
  809.  
  810.       case N_SETV | N_EXT:
  811.     printf ("V");
  812.     break;
  813.  
  814.       case N_SETV:
  815.     printf ("v");
  816.     break;
  817.  
  818.       case N_SETA | N_EXT:
  819.     printf ("L");
  820.     break;
  821.     
  822.       case N_SETA:
  823.     printf ("l");
  824.     break;
  825.     
  826.       case N_SETT | N_EXT:
  827.     printf ("X");
  828.     break;
  829.     
  830.       case N_SETT:
  831.     printf ("x");
  832.     break;
  833.     
  834.       case N_SETD | N_EXT:
  835.     printf ("Z");
  836.     break;
  837.     
  838.       case N_SETD:
  839.     printf ("z");
  840.     break;
  841.     
  842.       case N_SETB | N_EXT:
  843.     printf ("S");
  844.     break;
  845.     
  846.       case N_SETB:
  847.     printf ("s");
  848.     break;
  849.     
  850.       case N_INDR | N_EXT:
  851.     printf ("I");
  852.     break;
  853.     
  854.       case N_INDR:
  855.     printf ("i");
  856.     break;
  857.     
  858.       case N_WARNING | N_EXT:
  859.     printf ("W");
  860.     break;
  861.     
  862.       case N_WARNING:
  863.     printf ("w");
  864.     break;
  865.     
  866. #ifdef N_SECT
  867.       case N_SECT:
  868.     if (sym->n_sect == text_section)
  869.       printf ("t");
  870.     else if (sym->n_sect == data_section)
  871.       printf ("d");
  872.     else if (sym->n_sect == bss_section)
  873.       printf ("b");
  874.     else
  875.       printf ("%d", sym->n_sect);
  876.     break;
  877.  
  878.       case N_SECT | N_EXT:
  879.     if (sym->n_sect == text_section)
  880.       printf ("T");
  881.     else if (sym->n_sect == data_section)
  882.       printf ("D");
  883.     else if (sym->n_sect == bss_section)
  884.       printf ("B");
  885.     else
  886.       printf ("%d", sym->n_sect);
  887.     break;
  888. #endif
  889.  
  890.       default:
  891.     {
  892.       char *s;
  893.       int i;
  894. #ifdef __GNU_STAB__
  895.       s = "";
  896.       for (i = sizeof (stab_names) / sizeof (stab_names[0]) - 1;
  897.            i >= 0; i--)
  898.         {
  899.           if (stab_names[i].code
  900.           == (enum __stab_debug_code) sym->n_type)
  901.         {
  902.           s = stab_names[i].string;
  903.           break;
  904.         }
  905.         }
  906. #else /* not __GNU_STAB__ */
  907.       switch (sym->n_type)
  908.         {
  909.         case N_GSYM:
  910.           s = "GSYM";
  911.           break;
  912.         case N_FNAME:
  913.           s = "FNAME";
  914.           break;
  915.         case N_FUN:
  916.           s = "FUN";
  917.           break;
  918.         case N_STSYM:
  919.           s = "STSYM";
  920.           break;
  921.         case N_LCSYM:
  922.           s = "LCSYM";
  923.           break;
  924.         case N_RSYM:
  925.           s = "RSYM";
  926.           break;
  927.         case N_SLINE:
  928.           s = "SLINE";
  929.           break;
  930.         case N_DSLINE:
  931.           s = "DSLINE";
  932.           break;
  933.         case N_BSLINE:
  934.           s = "BSLINE";
  935.           break;
  936.         case N_SSYM:
  937.           s = "SSYM";
  938.           break;
  939.         case N_SO:
  940.           s = "SO";
  941.           break;
  942.         case N_LSYM:
  943.           s = "LSYM";
  944.           break;
  945.         case N_SOL:
  946.           s = "SOL";
  947.           break;
  948.         case N_PSYM:
  949.           s = "PSYM";
  950.           break;
  951.         case N_ENTRY:
  952.           s = "ENTRY";
  953.           break;
  954.         case N_LBRAC:
  955.           s = "LBRAC";
  956.           break;
  957.         case N_RBRAC:
  958.           s = "RBRAC";
  959.           break;
  960.         case N_BCOMM:
  961.           s = "BCOMM";
  962.           break;
  963.         case N_ECOMM:
  964.           s = "ECOMM";
  965.           break;
  966.         case N_ECOML:
  967.           s = "ECOML";
  968.           break;
  969.         case N_LENG:
  970.           s = "LENG";
  971.           break;
  972.         default:
  973.           s = "";
  974.         }
  975. #endif /* not __GNU_STAB__ */
  976.       /* %x treats them as unsigned anyway, so if we didn't cast
  977.          them we'd get 0xffffffff for all 1's instead of 0xff
  978.          or 0xffff.  */
  979.       printf ("- %02x %04x %5s",
  980. #ifdef N_SECT
  981.           (unsigned char) sym->n_sect,
  982. #else
  983.           (unsigned char) sym->n_other,
  984. #endif
  985.           (unsigned short) sym->n_desc, s);
  986.     }
  987.     }
  988.  
  989.   printf (" ");
  990.   
  991.   if (sym->n_un.n_name)
  992.     fprint_name (stdout, sym->n_un.n_name);
  993.   
  994.   printf ("\n");
  995. }
  996.  
  997. void
  998. do_symdef_member (desc, offset, member_length)
  999.      int desc;
  1000.      int offset;
  1001.      int member_length;
  1002. {
  1003.   int symdef_size;
  1004.   int nsymdefs;
  1005.   struct symdef *symdefs;
  1006.   int stringsize;
  1007.   char *strings;
  1008.   int i;
  1009.   char *member_name;
  1010.   int member_offset;
  1011.  
  1012.   /* read the string-table-length out of the file */
  1013.  
  1014.   lseek (desc, offset, 0);
  1015.   if (sizeof symdef_size != read (desc, &symdef_size, sizeof symdef_size))
  1016.     {
  1017.       error_with_file ("premature eof in ");
  1018.       return;
  1019.     }
  1020.  
  1021.   if (symdef_size < 0)
  1022.     {
  1023.       error_with_file ("invalid size value in ");
  1024.       return;
  1025.     }
  1026.  
  1027.   nsymdefs = symdef_size / sizeof (struct symdef);
  1028.   symdefs = (struct symdef *) alloca (symdef_size);
  1029.   if (symdef_size != read (desc, symdefs, symdef_size))
  1030.     {
  1031.       error_with_file ("premature eof in ");
  1032.       return;
  1033.     }
  1034.  
  1035.   if (stringsize < 0)
  1036.     {
  1037.       error_with_file ("invalid size value in ");
  1038.       return;
  1039.     }
  1040.  
  1041.   if (sizeof stringsize != read (desc, &stringsize, sizeof stringsize))
  1042.     {
  1043.       error_with_file ("premature eof in ");
  1044.       return;
  1045.     }
  1046.  
  1047.   strings = (char *) alloca (stringsize);
  1048.   if (stringsize != read (desc, strings, stringsize))
  1049.     {
  1050.       error_with_file ("premature eof in ");
  1051.       return;
  1052.     }
  1053.  
  1054.   if (stringsize + symdef_size + sizeof stringsize + sizeof symdef_size != member_length)
  1055.     {
  1056.       error_with_file ("size of data isn't what the data calls for in ");
  1057.       return;
  1058.     }
  1059.  
  1060.   if (!file_on_each_line && (number_of_files > 1 || input_member))
  1061.     printf ("\n%s:\n", input_member ? input_member : input_name);
  1062.     
  1063.   member_offset = -1;
  1064.   for (i = 0; i < nsymdefs; i++)
  1065.     {
  1066.       if (symdefs[i].stringoffset < 0 || symdefs[i].stringoffset >= stringsize)
  1067.     {
  1068.       error_with_file ("invalid entry in ");
  1069.       return;
  1070.     }
  1071.       if (member_offset != symdefs[i].offset)
  1072.     {
  1073.       member_offset = symdefs[i].offset;
  1074.       decode_library_subfile (desc, member_offset, &member_name);
  1075.     }
  1076.       if (file_on_each_line)
  1077.     {
  1078.       print_file_name (stdout);
  1079.       printf (":");
  1080.     }
  1081.       printf ("%s in %s\n", symdefs[i].stringoffset + strings, member_name);
  1082.     }
  1083. }
  1084.  
  1085. /* Report a fatal error.
  1086.    STRING is a printf format string and ARG is one arg for it.  */
  1087.  
  1088. fatal (string, arg)
  1089.      char *string, *arg;
  1090. {
  1091.   fprintf (stderr, "%s: ", program_name);
  1092.   fprintf (stderr, string, arg);
  1093.   fprintf (stderr, "\n");
  1094.   exit (1);
  1095. }
  1096.  
  1097. /* Report a nonfatal error.
  1098.    STRING is a printf format string and ARG is one arg for it.  */
  1099.  
  1100. error (string, arg)
  1101.      char *string, *arg;
  1102. {
  1103.   fprintf (stderr, "%s: ", program_name);
  1104.   fprintf (stderr, string, arg);
  1105.   fprintf (stderr, "\n");
  1106. }
  1107.  
  1108. /* Report a nonfatal error.
  1109.    STRING is printed, followed by the current file name.  */
  1110.  
  1111. error_with_file (string)
  1112.      char *string;
  1113. {
  1114.   fprintf (stderr, "%s: ", program_name);
  1115.   fprintf (stderr, string);
  1116.   print_file_name (stderr);
  1117.   fprintf (stderr, "\n");
  1118. }
  1119.  
  1120. /* Report a fatal error using the message for the last failed system call,
  1121.    followed by the string NAME.  */
  1122.  
  1123. perror_name (name)
  1124.      char *name;
  1125. {
  1126.   extern int errno, sys_nerr;
  1127.   extern char *sys_errlist[];
  1128.   char *s;
  1129.  
  1130.   if (errno < sys_nerr)
  1131.     s = concat ("", sys_errlist[errno], " for %s");
  1132.   else
  1133.     s = "cannot open %s";
  1134.   error (s, name);
  1135. }
  1136.  
  1137. /* Like malloc but get fatal error if memory is exhausted.  */
  1138.  
  1139. char *
  1140. xmalloc (size)
  1141.      int size;
  1142. {
  1143.   char *result = malloc (size);
  1144.   if (!result)
  1145.     fatal ("virtual memory exhausted", 0);
  1146.   return result;
  1147. }
  1148.  
  1149. /* Like realloc but get fatal error if out of memory.  */
  1150. char *
  1151. xrealloc (p, size)
  1152.      char *p;
  1153.      int size;
  1154. {
  1155.   char *result = realloc(p, size);
  1156.   if (!result)
  1157.     fatal ("virtual memory exhausted", 0);
  1158.   return result;
  1159. }
  1160.  
  1161. /* Return a newly-allocated string
  1162.    whose contents concatenate those of S1, S2, S3.  */
  1163.  
  1164. char *
  1165. concat (s1, s2, s3)
  1166.      char *s1, *s2, *s3;
  1167. {
  1168.   int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  1169.   char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  1170.  
  1171.   strcpy (result, s1);
  1172.   strcpy (result + len1, s2);
  1173.   strcpy (result + len1 + len2, s3);
  1174.   result[len1 + len2 + len3] = 0;
  1175.  
  1176.   return result;
  1177. }
  1178.