home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / gnu / utlsrc33.lzh / UTLSRC33 / NM.C < prev    next >
C/C++ Source or Header  |  1993-07-30  |  25KB  |  1,003 lines

  1. /* Describe symbol table of a rel file.
  2.    Copyright (C) 1986 Free Software Foundation, Inc.
  3.  
  4.                NO WARRANTY
  5.  
  6.   BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  7. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
  8. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  9. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  10. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  11. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  13. AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
  14. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  15. CORRECTION.
  16.  
  17.  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  18. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  19. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  20. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  21. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  22. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  23. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  24. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  25. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  26. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  27.  
  28.         GENERAL PUBLIC LICENSE TO COPY
  29.  
  30.   1. You may copy and distribute verbatim copies of this source file
  31. as you receive it, in any medium, provided that you conspicuously
  32. and appropriately publish on each copy a valid copyright notice
  33. "Copyright (C) 1986 Free Software Foundation, Inc.", and include
  34. following the copyright notice a verbatim copy of the above disclaimer
  35. of warranty and of this License.
  36.  
  37.   2. You may modify your copy or copies of this source file or
  38. any portion of it, and copy and distribute such modifications under
  39. the terms of Paragraph 1 above, provided that you also do the following:
  40.  
  41.     a) cause the modified files to carry prominent notices stating
  42.     that you changed the files and the date of any change; and
  43.  
  44.     b) cause the whole of any work that you distribute or publish,
  45.     that in whole or in part contains or is a derivative of this
  46.     program or any part thereof, to be licensed at no charge to all
  47.     third parties on terms identical to those contained in this
  48.     License Agreement (except that you may choose to grant more extensive
  49.     warranty protection to some or all third parties, at your option).
  50.  
  51.     c) You may charge a distribution fee for the physical act of
  52.     transferring a copy, and you may at your option offer warranty
  53.     protection in exchange for a fee.
  54.  
  55. Mere aggregation of another unrelated program with this program (or its
  56. derivative) on a volume of a storage or distribution medium does not bring
  57. the other program under the scope of these terms.
  58.  
  59.   3. You may copy and distribute this program (or a portion or derivative
  60. of it, under Paragraph 2) in object code or executable form under the terms
  61. of Paragraphs 1 and 2 above provided that you also do one of the following:
  62.  
  63.     a) accompany it with the complete corresponding machine-readable
  64.     source code, which must be distributed under the terms of
  65.     Paragraphs 1 and 2 above; or,
  66.  
  67.     b) accompany it with a written offer, valid for at least three
  68.     years, to give any third party free (except for a nominal
  69.     shipping charge) a complete machine-readable copy of the
  70.     corresponding source code, to be distributed under the terms of
  71.     Paragraphs 1 and 2 above; or,
  72.  
  73.     c) accompany it with the information you received as to where the
  74.     corresponding source code may be obtained.  (This alternative is
  75.     allowed only for noncommercial distribution and only if you
  76.     received the program in object code or executable form alone.)
  77.  
  78. For an executable file, complete source code means all the source code for
  79. all modules it contains; but, as a special exception, it need not include
  80. source code for modules which are standard libraries that accompany the
  81. operating system on which the executable file runs.
  82.  
  83.   4. You may not copy, sublicense, distribute or transfer this program
  84. except as expressly provided under this License Agreement.  Any attempt
  85. otherwise to copy, sublicense, distribute or transfer this program is void and
  86. your rights to use the program under this License agreement shall be
  87. automatically terminated.  However, parties who have received computer
  88. software programs from you with this License Agreement will not have
  89. their licenses terminated so long as such parties remain in full compliance.
  90.  
  91.   5. If you wish to incorporate parts of this program into other free
  92. programs whose distribution conditions are different, write to the Free
  93. Software Foundation at 675 Mass Ave, Cambridge, MA 02139.  We have not yet
  94. worked out a simple rule that can be stated here, but we will often permit
  95. this.  We will be guided by the two goals of preserving the free status of
  96. all derivatives of our free software and of promoting the sharing and reuse of
  97. software.
  98.  
  99.  In other words, you are welcome to use, share and improve this program.
  100.  You are forbidden to forbid anyone else to use, share and improve
  101.  what you give them.   Help stamp out software-hoarding!  */
  102.  
  103.  
  104. #if (CROSSATARI || atarist || atariminix)
  105. #include "gnu-out.h"
  106. #else
  107. #include <a.out.h>
  108. #endif
  109.  
  110. #ifdef CROSSATARI
  111. #include "stab.h"
  112. #include "/usr/include/stdio.h"
  113. #ifdef USG
  114. #include "/usr/include/string.h"
  115. #include "/usr/include/fcntl.h"
  116. #include "/usr/include/sys/types.h"
  117. #else
  118. #include "/usr/include/strings.h"
  119. #endif
  120. #include "gnu-ar.h"
  121. #else
  122. #include <stab.h>
  123. #include <stdio.h>
  124. #include <string.h>
  125. #include "gnu-ar.h"
  126. #endif
  127.  
  128. #ifdef atarist
  129. #include <file.h>
  130. #else
  131. #ifdef CROSSATARI
  132. #include "/usr/include/sys/file.h"
  133. #else
  134. #ifdef atariminix
  135. #if 0
  136. #define EMUL_OPEN3
  137. #include <open3.h>
  138. #else
  139. #include <fcntl.h>
  140. #endif
  141. #else
  142. #include <sys/file.h>
  143. #endif
  144. #endif
  145. #endif
  146.  
  147. /* If compiled with GNU C, use the built-in alloca */
  148. #ifdef __GNUC__
  149. #    define alloca __builtin_alloca
  150. #endif
  151.  
  152. /*
  153.  * Alloca include.
  154.  */
  155. #if defined(sun) && defined(sparc)
  156. #ifdef alloca
  157. #undef alloca
  158. #endif
  159. #include "alloca.h"
  160. #endif
  161.  
  162. /* Name of this program.  */
  163.  
  164. char *program_name;
  165.  
  166. /* Number of input files specified.  */
  167.  
  168. int number_of_files;
  169.  
  170. /* Current file's name.  */
  171.  
  172. char *input_name;
  173.  
  174. /* Current member's name, or 0 if processing a non-library file.  */
  175.  
  176. char *input_member;
  177.  
  178. /* Offset within archive of the current member,
  179.    if we are processing an archive.  */
  180.  
  181. int member_offset;
  182.  
  183. /* Command options.  */
  184.  
  185. int external_only;    /* nonzero means print external symbols only.  */
  186. int sort_numerically;   /* sort in numerical order rather than alphabetic  */
  187. int reverse_sort;    /* sort in downward (alphabetic or numeric) order.  */
  188. int no_sort;        /* don't sort; print symbols in order in the file.  */
  189. int undefined_only;    /* print undefined symbols only.  */
  190. int file_on_each_line;    /* print file name on each line.  */
  191. int debugger_syms;    /* print the debugger-only symbols.  */
  192. int print_symdefs;    /* describe the __.SYMDEF data in any archive file specified.  */
  193. int demangle_symbols;   /* Demangle G++ symbols.  */
  194.  
  195. /* The __.SYMDEF member of an archive has the following format:
  196.    1) A longword saying the size of the symdef data that follows
  197.    2) Zero or more  struct symdef  filling that many bytes
  198.    3) A longword saying how many bytes of strings follow
  199.    4) That many bytes of string data.
  200. */
  201.  
  202. struct symdef
  203.   {
  204.     long stringoffset;    /* Offset of this symbol's name in the string data */
  205.     long offset;    /* Offset in the archive of the header-data for the member that
  206.                defines this symbol.  */
  207.   };
  208.  
  209. void decode_switch ();
  210. int decode_arg ();
  211. void do_one_file (), do_one_rel_file (), do_symdef_member ();
  212. char *concat ();
  213.  
  214. main (argc, argv)
  215.      char **argv;
  216.      int argc;
  217. {
  218.   int i;
  219.   int c;
  220.   extern int optind;
  221.  
  222.   program_name = argv[0];
  223.  
  224.   number_of_files = 0;
  225.   external_only = 0;
  226.   sort_numerically = 0;
  227.   reverse_sort = 0;
  228.   no_sort = 0;
  229.   undefined_only = 0;
  230.   file_on_each_line = 0;
  231.   debugger_syms = 0;
  232.   print_symdefs = 0;
  233.   demangle_symbols = 1;
  234.  
  235.   while ((c = getopt (argc, argv, "agnoprsux")) != EOF)
  236.     switch (c)
  237.       {
  238.       case 'a':
  239.     debugger_syms = 1;
  240.     break;
  241.  
  242.       case 'g':
  243.     external_only = 1;
  244.     break;
  245.  
  246.       case 'n':
  247.     sort_numerically = 1;
  248.     break;
  249.  
  250.       case 'o':
  251.     file_on_each_line = 1;
  252.     break;
  253.  
  254.       case 'p':
  255.     no_sort = 1;
  256.     break;
  257.  
  258.       case 'r':
  259.     reverse_sort = 1;
  260.     break;
  261.  
  262.       case 's':
  263.     print_symdefs = 1;
  264.     break;
  265.  
  266.       case 'u':
  267.     undefined_only = 1;
  268.     break;
  269.  
  270.       case 'x':
  271.     demangle_symbols = 0;
  272.     break;
  273.       }
  274.  
  275.   number_of_files = argc - optind;
  276.  
  277.   /* Now scan again and print the files.  */
  278.  
  279.   if (argc == optind)
  280.     do_one_file ("a.out");
  281.   else
  282.     for (i = optind; i < argc; i++)
  283.       do_one_file (argv[i]);
  284. }
  285.  
  286. /* Print the filename of the current file on 'outfile' (a stdio stream).  */
  287.  
  288. print_file_name (outfile)
  289.      FILE *outfile;
  290. {
  291.   fprintf (outfile, "%s", input_name);
  292.   if (input_member)
  293.     fprintf (outfile, "(%s)", input_member);
  294. }
  295.  
  296. /* process one input file */
  297. void scan_library ();
  298.  
  299. void
  300. do_one_file (name)
  301.      char *name;
  302. {
  303.   int len, magicnum, desc;
  304.  
  305.   desc = open (name, O_RDONLY, 0);
  306.  
  307.   if (desc < 0)
  308.     {
  309.       perror_name (name);
  310.       return;
  311.     }
  312.  
  313.   input_name = name;
  314.   input_member = 0;
  315.  
  316.   len = read (desc, &magicnum, sizeof magicnum);
  317.   if (len != sizeof magicnum)
  318.     error_with_file ("failure reading header of ");
  319.   else if (!N_BADMAG (*((struct exec *)&magicnum)))
  320.     do_one_rel_file (desc, 0);
  321.   else
  322.     {
  323.       char armag[SARMAG];
  324.       int nchars;
  325.  
  326.       lseek (desc, 0, 0);
  327.       nchars = read (desc, armag, SARMAG);
  328.  
  329.       if (SARMAG != nchars || strncmp (armag, ARMAG, SARMAG))
  330.     error_with_file ("malformed input file (not rel or archive) ");
  331.       else
  332.         scan_library (desc);
  333.     }
  334.  
  335.   close (desc);
  336. }
  337.  
  338. /* Read in the archive data about one member.
  339.    SUBFILE_OFFSET is the address within the archive of the start of that data.
  340.    The value returned is the length of the member's contents, which does
  341.    not include the archive data about the member.
  342.    A pointer to the member's name is stored into *MEMBER_NAME_PTR.
  343.  
  344.    If there are no more valid members, zero is returned.  */
  345.  
  346. int
  347. decode_library_subfile (desc, subfile_offset, member_name_ptr)
  348.      int desc;
  349.      int subfile_offset;
  350.      char **member_name_ptr;
  351. {
  352.   int bytes_read;
  353.   int namelen;
  354.   int member_length;
  355.   char *name;
  356.   struct ar_hdr hdr1;
  357.  
  358.   lseek (desc, subfile_offset, 0);
  359.  
  360. #if (defined(WORD_ALIGNED) && defined(CROSSATARI))
  361.   bytes_read = read (desc, &hdr1.ar_name, 
  362.              sizeof (hdr1.ar_name));
  363.   bytes_read += read (desc, &hdr1.ar_size, 
  364.               sizeof (hdr1.ar_size));
  365.   bytes_read += read (desc, &hdr1.ar_date, 
  366.               sizeof (hdr1.ar_date));
  367.   bytes_read += read (desc, &hdr1.ar_mode, 
  368.               sizeof (hdr1.ar_mode));
  369.   bytes_read += read (desc, &hdr1.ar_uid, 
  370.               sizeof (hdr1.ar_uid));
  371.   bytes_read += read (desc, &hdr1.ar_gid, 
  372.               sizeof (hdr1.ar_gid));
  373.   bytes_read += read (desc, &hdr1.ar_fmag, 
  374.               sizeof (hdr1.ar_fmag));
  375. #else
  376.   bytes_read = read (desc, &hdr1, sizeof hdr1);
  377. #endif
  378.   if (!bytes_read)
  379.     return 0;        /* end of archive */
  380.  
  381. #if (defined(WORD_ALIGNED) && defined(CROSSATARI))
  382.   else if (bytes_read != (sizeof (hdr1.ar_name) +
  383.               sizeof (hdr1.ar_size) +
  384.               sizeof (hdr1.ar_date) +
  385.               sizeof (hdr1.ar_mode) +
  386.               sizeof (hdr1.ar_uid) +
  387.               sizeof (hdr1.ar_gid) +
  388.               sizeof (hdr1.ar_fmag)))
  389.     error_with_file ("malformed library archive ");
  390. #else
  391.   else if (sizeof hdr1 != bytes_read)
  392.     error_with_file ("malformed library archive ");
  393. #endif 
  394.  
  395.   else if (sscanf (hdr1.ar_size, "%d", &member_length) != 1)
  396.     error_with_file ("malformatted header of archive member in ");
  397.  
  398.   else
  399.     {
  400.       for (namelen = 0; ; namelen++)
  401.     if (hdr1.ar_name[namelen] == 0 || hdr1.ar_name[namelen] == ' ')
  402.       break;
  403.  
  404.       name = (char *) xmalloc (namelen+1);
  405.       strncpy (name, hdr1.ar_name, namelen);
  406.       name[namelen] = 0;
  407.  
  408.       *member_name_ptr = name;
  409.  
  410.       return member_length;
  411.     }
  412.   return 0;   /* tell caller to exit loop */
  413. }
  414.  
  415. /* Scan a library and describe each member.  */
  416.  
  417. void
  418. scan_library (desc)
  419.      int desc;
  420. {
  421.   int this_subfile_offset = SARMAG;
  422.   int member_length;
  423. #if (defined(WORD_ALIGNED) && defined(CROSSATARI))
  424.   struct ar_hdr hdr1;
  425. #endif 
  426.  
  427.   if (!file_on_each_line)
  428.     printf ("\n%s:\n", input_name);
  429.   
  430.   while (1)
  431.     {
  432.       member_length
  433.     = decode_library_subfile (desc, this_subfile_offset, &input_member);
  434.       if (member_length == 0)
  435.     break;
  436.  
  437.       /* Describe every member except the ranlib data if any.  */
  438.  
  439. #if (defined(WORD_ALIGNED) && defined(CROSSATARI))
  440.       if (strcmp (input_member, "__.SYMDEF"))
  441.     do_one_rel_file (desc, this_subfile_offset + (sizeof (hdr1.ar_name) +
  442.                               sizeof (hdr1.ar_size) +
  443.                               sizeof (hdr1.ar_date) +
  444.                               sizeof (hdr1.ar_mode) +
  445.                               sizeof (hdr1.ar_uid) +
  446.                               sizeof (hdr1.ar_gid) +
  447.                               sizeof (hdr1.ar_fmag)));
  448.       else if (print_symdefs)
  449.     do_symdef_member (desc, this_subfile_offset + (sizeof (hdr1.ar_name) +
  450.                                sizeof (hdr1.ar_size) +
  451.                                sizeof (hdr1.ar_date) +
  452.                                sizeof (hdr1.ar_mode) +
  453.                                sizeof (hdr1.ar_uid) +
  454.                                sizeof (hdr1.ar_gid) +
  455.                                sizeof (hdr1.ar_fmag)),
  456.               member_length);
  457.  
  458.       this_subfile_offset += ((member_length + (sizeof (hdr1.ar_name) +
  459.                         sizeof (hdr1.ar_size) +
  460.                         sizeof (hdr1.ar_date) +
  461.                         sizeof (hdr1.ar_mode) +
  462.                         sizeof (hdr1.ar_uid) +
  463.                         sizeof (hdr1.ar_gid) +
  464.                         sizeof (hdr1.ar_fmag))) + 1) & -2;
  465. #else
  466.       if (strcmp (input_member, "__.SYMDEF"))
  467.     do_one_rel_file (desc, this_subfile_offset + sizeof (struct ar_hdr));
  468.       else if (print_symdefs)
  469.     do_symdef_member (desc, this_subfile_offset + sizeof (struct ar_hdr), member_length);
  470.  
  471.       this_subfile_offset += ((member_length + sizeof (struct ar_hdr)) + 1) & -2;
  472. #endif 
  473.     }
  474. }
  475.  
  476. void print_symbols (), print_one_symbol ();
  477. void read_header ();
  478. int alphacompare (), valuecompare ();
  479. int filter_symbols ();
  480. char *cplus_demangle();
  481.  
  482. void
  483. do_one_rel_file (desc, offset)
  484.      int desc;
  485.      int offset;
  486. {
  487.   struct exec header;   /* file header read in here */
  488.   int string_size;
  489.   struct nlist *symbols_and_strings;
  490.   int symcount;
  491.   int totalsize;
  492.   char *strings;
  493.  
  494. #if 0
  495.    header.a_magic = 0;
  496. #else
  497.   N_SET_MAGIC(header, 0);
  498. #endif
  499.  
  500.   read_header (desc, &header, offset);
  501.  
  502.   if (N_BADMAG (header))
  503.     {
  504.       error_with_file ("bad magic number in ");
  505.       return;
  506.     }
  507.  
  508.   /* Read the string-table-length out of the file.  */
  509.  
  510.   lseek (desc, N_STROFF (header) + offset, 0);
  511.   if (sizeof string_size != read (desc, &string_size, sizeof string_size))
  512.     {
  513.       error_with_file ("bad string table in ");
  514.       return;
  515.     }
  516.  
  517.   /* Number of symbol entries in the file.  */
  518.   symcount = header.a_syms / sizeof (struct nlist);
  519.  
  520.   totalsize = string_size + header.a_syms;
  521.   /* Allocate space for symbol entries and string table.  */
  522.   symbols_and_strings = (struct nlist *) xmalloc (totalsize);
  523.   strings = (char *) symbols_and_strings + header.a_syms;
  524.  
  525.   /* Read them both in all at once.  */
  526.   lseek (desc, N_SYMOFF (header) + offset, 0);
  527.   if (totalsize != read (desc, symbols_and_strings, totalsize))
  528.     {
  529.       error_with_file ("premature end of file in symbols/strings of ");
  530.       return;
  531.     }
  532.  
  533.   /* Identify this file, if desired.  */
  534.  
  535.   if (!file_on_each_line && (number_of_files > 1 || input_member))
  536.     printf ("\n%s:\n", input_member ? input_member : input_name);
  537.  
  538.   /* Discard the symbols we don't want to print; compact the rest down.  */
  539.  
  540.   symcount = filter_symbols (symbols_and_strings, symcount, strings);
  541.     
  542.   /* Modify each symbol entry to point directly at the symbol name.
  543.      This is so the sort routine does not need to be passed
  544.      the value of `strings' separately.  */
  545.  
  546.   {
  547.     struct nlist *p = symbols_and_strings;
  548.     struct nlist *end = symbols_and_strings + symcount;
  549.  
  550.     for (; p < end; p++)
  551.       if (p->n_un.n_strx)
  552.         p->n_un.n_name = strings + p->n_un.n_strx;
  553.   }
  554.  
  555.   /* Sort the symbols if desired.  */
  556.  
  557.   if (!no_sort)
  558.     qsort (symbols_and_strings, symcount, sizeof (struct nlist),
  559.        sort_numerically ? valuecompare : alphacompare);
  560.  
  561.   /* Print the symbols in the order they are now in.  */
  562.  
  563.   print_symbols (symbols_and_strings, symcount);
  564.  
  565.   free (symbols_and_strings);
  566. }
  567.  
  568.  
  569. /* Read a file's header.  */
  570.  
  571. void
  572. read_header (desc, loc, offset)
  573.      int desc;
  574.      struct exec *loc;
  575.      int offset;
  576. {
  577.   int len;
  578.   lseek (desc, offset, 0);
  579.   len = read (desc, loc, sizeof (struct exec));
  580.   if (len != sizeof (struct exec))
  581.     error_with_file ("failure reading header of ");
  582. }
  583.  
  584. /* Choose which symbol entries to print;
  585.    compact them downward to get rid of the rest.
  586.    Return the number of symbols to be printed.  */
  587.  
  588. int
  589. filter_symbols (syms, symcount, strings)
  590.      struct nlist *syms;
  591.      int symcount;
  592.      char *strings;
  593. {
  594.   struct nlist *from = syms, *to = syms;
  595.   struct nlist *end = syms + symcount;
  596.  
  597.   while (from < end)
  598.     {
  599.       int keep = 0;
  600.  
  601.       /* undefined sym or common sym */
  602.       if (from->n_type == N_EXT) keep = !undefined_only || !from->n_value;
  603.       /* global defined sym */
  604.       else if (from->n_type & N_EXT) keep = !undefined_only;
  605.       /* debugger sym: normally don't print */
  606.       else if (from->n_type & ~(N_TYPE | N_EXT)) keep = debugger_syms;
  607.       /* local sym */
  608.       else keep = !external_only && !undefined_only;
  609.  
  610.       if (keep)
  611.     *to++ = *from;
  612.       from++;
  613.     }
  614.  
  615.   return to - syms;
  616. }
  617.  
  618. /* Comparison functions for sorting symbols.  */
  619.  
  620. int
  621. alphacompare (sym1, sym2)
  622.      struct nlist *sym1, *sym2;
  623. {
  624.   if (reverse_sort)
  625.     {
  626.       if (!sym2->n_un.n_name)
  627.     {
  628.       if (sym1->n_un.n_name) return -1;
  629.       else return 0;
  630.     }
  631.       if (!sym1->n_un.n_name) return 1;
  632.       return strcmp (sym2->n_un.n_name, sym1->n_un.n_name);
  633.     }
  634.   else
  635.     {
  636.       if (!sym1->n_un.n_name)
  637.     {
  638.       if (sym2->n_un.n_name) return -1;
  639.       else return 0;
  640.     }
  641.       if (!sym2->n_un.n_name) return 1;
  642.       return strcmp (sym1->n_un.n_name, sym2->n_un.n_name);
  643.     }
  644. }
  645.  
  646.  
  647. int
  648. valuecompare (sym1, sym2)
  649.      struct nlist *sym1, *sym2;
  650. {
  651.   if (reverse_sort)
  652.     return sym2->n_value - sym1->n_value;
  653.   else
  654.     return sym1->n_value - sym2->n_value;
  655. }
  656.  
  657. void
  658. print_symbols (syms, symcount)
  659.      struct nlist *syms;
  660.      int symcount;
  661. {
  662.   int i;
  663.  
  664.   for (i = 0; i < symcount; i++)
  665.     print_one_symbol (&syms[i]);
  666. }
  667.  
  668. void
  669. print_one_symbol (sym)
  670.      struct nlist *sym;
  671. {
  672.   if (file_on_each_line)
  673.     {
  674.       print_file_name (stdout);
  675.       printf (":");
  676.     }
  677.  
  678.   if (undefined_only)
  679.     {
  680.       if (sym->n_type == N_EXT && !sym->n_value)
  681.         printf ("%s\n", sym->n_un.n_name);
  682.       return;
  683.     }
  684.  
  685.   if (sym->n_type & ~N_EXT || sym->n_value)
  686.     printf ("%08x ", sym->n_value);
  687.   else printf ("         ");
  688.  
  689.   switch (sym->n_type)
  690.     {
  691.       case N_EXT:
  692.         if (sym->n_value) printf ("C");
  693.         else printf ("U");
  694.     break;
  695.  
  696.       case 0:
  697.         if (sym->n_value) printf ("c");
  698.         else printf ("u");
  699.     break;
  700.  
  701.       case N_ABS | N_EXT:
  702.     printf ("A");
  703.     break;
  704.  
  705.       case N_ABS:
  706.     printf ("a");
  707.     break;
  708.  
  709.       case N_TEXT | N_EXT:
  710.     printf ("T");
  711.     break;
  712.  
  713.       case N_TEXT:
  714.     printf ("t");
  715.     break;
  716.  
  717.       case N_DATA | N_EXT:
  718.     printf ("D");
  719.     break;
  720.  
  721.       case N_DATA:
  722.     printf ("d");
  723.     break;
  724.  
  725.       case N_BSS | N_EXT:
  726.     printf ("B");
  727.     break;
  728.  
  729.       case N_BSS:
  730.     printf ("b");
  731.     break;
  732.  
  733.       default:
  734.     {
  735.       char *s;
  736.       switch (sym->n_type)
  737.         {
  738.         case N_GSYM:
  739.           s = "GSYM";
  740.           break;
  741.         case N_FNAME:
  742.           s = "FNAME";
  743.           break;
  744.         case N_FUN:
  745.           s = "FUN";
  746.           break;
  747.         case N_STSYM:
  748.           s = "STSYM";
  749.           break;
  750.         case N_LCSYM:
  751.           s = "LCSYM";
  752.           break;
  753.         case N_RSYM:
  754.           s = "RSYM";
  755.           break;
  756.         case N_SLINE:
  757.           s = "SLINE";
  758.           break;
  759.         case N_SSYM:
  760.           s = "SSYM";
  761.           break;
  762.         case N_SO:
  763.           s = "SO";
  764.           break;
  765.         case N_LSYM:
  766.           s = "LSYM";
  767.           break;
  768.         case N_SOL:
  769.           s = "SOL";
  770.           break;
  771.         case N_PSYM:
  772.           s = "PSYM";
  773.           break;
  774.         case N_ENTRY:
  775.           s = "ENTRY";
  776.           break;
  777.         case N_LBRAC:
  778.           s = "LBRAC";
  779.           break;
  780.         case N_RBRAC:
  781.           s = "RBRAC";
  782.           break;
  783.         case N_BCOMM:
  784.           s = "BCOMM";
  785.           break;
  786.         case N_ECOMM:
  787.           s = "ECOMM";
  788.           break;
  789.         case N_ECOML:
  790.           s = "ECOML";
  791.           break;
  792.         case N_LENG:
  793.           s = "LENG";
  794.           break;
  795.         default:
  796.           s = "";
  797.         }
  798.       printf ("- %02x %04x %5s", sym->n_other, sym->n_desc, s);
  799.     }
  800.     }
  801.  
  802.   if (sym->n_un.n_name) {
  803.     if (demangle_symbols) {
  804.       char *nm;
  805.       
  806.       if ((nm = cplus_demangle(sym->n_un.n_name)) == NULL)
  807.         printf (" %s\n", sym->n_un.n_name);
  808.       else
  809.       {
  810.         printf (" %s\n", nm);
  811.     free(nm);
  812.       }
  813.     } else {
  814.       printf (" %s\n", sym->n_un.n_name);
  815.     }
  816.   } else {
  817.     printf (" \n");
  818.   }
  819. }
  820.  
  821. void
  822. do_symdef_member (desc, offset, member_length)
  823.      int desc;
  824.      int offset;
  825.      int member_length;
  826. {
  827.   int symdef_size;
  828.   int nsymdefs;
  829.   struct symdef *symdefs;
  830.   int stringsize;
  831.   char *strings;
  832.   int i;
  833.   char *member_name;
  834.   int member_offset;
  835.  
  836.   /* read the string-table-length out of the file */
  837.  
  838.   lseek (desc, offset, 0);
  839.   if (sizeof symdef_size != read (desc, &symdef_size, sizeof symdef_size))
  840.     {
  841.       error_with_file ("premature eof in ");
  842.       return;
  843.     }
  844.  
  845.   if (symdef_size < 0)
  846.     {
  847.       error_with_file ("invalid size value in ");
  848.       return;
  849.     }
  850.  
  851.   nsymdefs = symdef_size / sizeof (struct symdef);
  852.   symdefs = (struct symdef *) alloca (symdef_size);
  853.   if (symdef_size != read (desc, symdefs, symdef_size))
  854.     {
  855.       error_with_file ("premature eof in ");
  856.       return;
  857.     }
  858.  
  859.   if (stringsize < 0)
  860.     {
  861.       error_with_file ("invalid size value in ");
  862.       return;
  863.     }
  864.  
  865.   if (sizeof stringsize != read (desc, &stringsize, sizeof stringsize))
  866.     {
  867.       error_with_file ("premature eof in ");
  868.       return;
  869.     }
  870.  
  871.   strings = (char *) alloca (stringsize);
  872.   if (stringsize != read (desc, strings, stringsize))
  873.     {
  874.       error_with_file ("premature eof in ");
  875.       return;
  876.     }
  877.  
  878.   if (stringsize + symdef_size + sizeof stringsize + sizeof symdef_size != member_length)
  879.     {
  880.       error_with_file ("size of data isn't what the data calls for in ");
  881.       return;
  882.     }
  883.  
  884.   if (!file_on_each_line && (number_of_files > 1 || input_member))
  885.     printf ("\n%s:\n", input_member ? input_member : input_name);
  886.     
  887.   member_offset = -1;
  888.   for (i = 0; i < nsymdefs; i++)
  889.     {
  890.       if (symdefs[i].stringoffset < 0 || symdefs[i].stringoffset >= stringsize)
  891.     {
  892.       error_with_file ("invalid entry in ");
  893.       return;
  894.     }
  895.       if (member_offset != symdefs[i].offset)
  896.     {
  897.       member_offset = symdefs[i].offset;
  898.       decode_library_subfile (desc, member_offset, &member_name);
  899.     }
  900.       if (file_on_each_line)
  901.     {
  902.       print_file_name (stdout);
  903.       printf (":");
  904.     }
  905.       printf ("%s in %s\n", symdefs[i].stringoffset + strings, member_name);
  906.     }
  907. }
  908.  
  909. /* Report a fatal error.
  910.    STRING is a printf format string and ARG is one arg for it.  */
  911.  
  912. fatal (string, arg)
  913.      char *string, *arg;
  914. {
  915.   fprintf (stderr, "%s: ", program_name);
  916.   fprintf (stderr, string, arg);
  917.   fprintf (stderr, "\n");
  918.   exit (1);
  919. }
  920.  
  921. /* Report a nonfatal error.
  922.    STRING is a printf format string and ARG is one arg for it.  */
  923.  
  924. error (string, arg)
  925.      char *string, *arg;
  926. {
  927.   fprintf (stderr, "%s: ", program_name);
  928.   fprintf (stderr, string, arg);
  929.   fprintf (stderr, "\n");
  930. }
  931.  
  932. /* Report a nonfatal error.
  933.    STRING is printed, followed by the current file name.  */
  934.  
  935. error_with_file (string)
  936.      char *string;
  937. {
  938.   fprintf (stderr, "%s: ", program_name);
  939.   fprintf (stderr, string);
  940.   print_file_name (stderr);
  941.   fprintf (stderr, "\n");
  942. }
  943.  
  944. /* Report a fatal error using the message for the last failed system call,
  945.    followed by the string NAME.  */
  946.  
  947. perror_name (name)
  948.      char *name;
  949. {
  950.   extern int errno, sys_nerr;
  951.   extern char *sys_errlist[];
  952.   char *s;
  953.  
  954.   if (errno < sys_nerr)
  955.     s = concat ("", sys_errlist[errno], " for %s");
  956.   else
  957.     s = "cannot open %s";
  958.   error (s, name);
  959. }
  960.  
  961. /* Like malloc but get fatal error if memory is exhausted.  */
  962.  
  963. int
  964. xmalloc (size)
  965.      int size;
  966. {
  967.   int result = malloc (size);
  968.   if (!result)
  969.     fatal ("virtual memory exhausted", 0);
  970.   return result;
  971. }
  972.  
  973. /* Like realloc but get fatal error if memory is exhausted.  */
  974.  
  975. int
  976. xrealloc (ptr, size)
  977.      char *ptr;
  978.      int size;
  979. {
  980.   register int result = realloc (ptr, size);
  981.   if (!result)
  982.     fatal ("virtual memory exhausted", 0);
  983.   return result;
  984. }
  985.  
  986. /* Return a newly-allocated string
  987.    whose contents concatenate those of S1, S2, S3.  */
  988.  
  989. char *
  990. concat (s1, s2, s3)
  991.      char *s1, *s2, *s3;
  992. {
  993.   int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  994.   char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  995.  
  996.   strcpy (result, s1);
  997.   strcpy (result + len1, s2);
  998.   strcpy (result + len1 + len2, s3);
  999.   *(result + len1 + len2 + len3) = 0;
  1000.  
  1001.   return result;
  1002. }
  1003.