home *** CD-ROM | disk | FTP | other *** search
/ M.u.C.S. Disc 2000 / MUCS2000.iso / gnuc / util-41s.lzh / util-41 / nm.c < prev    next >
C/C++ Source or Header  |  1998-10-05  |  27KB  |  1,092 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. #ifdef BYTE_SWAP
  104.  
  105. #define SWAP4(y) (((unsigned)(y)>>24) + (((unsigned)(y)>>8)&0xff00) + \
  106.                  (((unsigned)(y)<<8)&0xff0000) + ((unsigned)(y)<<24))
  107. #define SWAP2(y) ((((unsigned)(y)&0xff00)>>8) + (((unsigned)(y)&0x00ff)<<8))
  108. #endif /* BYTE_SWAP */
  109.  
  110. #ifdef CROSSATARI
  111. #include "cross-inc/gnu-out.h"
  112. #else
  113. #ifdef atarist
  114. #include <gnu-out.h>
  115. #else
  116. #include <a.out.h>
  117. #endif
  118. #endif
  119.  
  120. #ifdef CROSSATARI
  121. #include "cross-inc/stab.h"
  122. #include <stdio.h>
  123. #ifdef USG
  124. #include "/usr/include/string.h"
  125. #include "/usr/include/fcntl.h"
  126. #include "/usr/include/sys/types.h"
  127. #else
  128. #include <strings.h>
  129. #endif
  130. #include "cross-inc/gnu-ar.h"
  131. #else
  132. #include <stab.h>
  133. #include <stdio.h>
  134. #include <string.h>
  135. #include <gnu-ar.h>
  136. #endif
  137.  
  138. #ifdef atarist
  139. #include <file.h>
  140. #else
  141. #ifdef CROSSATARI
  142.  
  143. #include <sys/file.h>
  144. #else
  145. #ifdef atariminix
  146. #if 0
  147. #define EMUL_OPEN3
  148. #include <open3.h>
  149. #else
  150. #include <fcntl.h>
  151. #endif
  152. #else
  153. #include <sys/file.h>
  154. #endif
  155. #endif
  156. #endif
  157.  
  158. /* If compiled with GNU C, use the built-in alloca */
  159. #ifdef __GNUC__
  160. #ifndef alloca
  161. #    define alloca __builtin_alloca
  162. #endif
  163.  
  164. #else
  165. /*
  166.  * Alloca include.
  167.  */
  168. #if defined(sun) && defined(sparc)
  169. #ifdef alloca
  170. #undef alloca
  171. #endif
  172. #include "alloca.h"
  173. #endif
  174. #endif /* __GNUC__ */
  175.  
  176. /* Name of this program.  */
  177.  
  178. extern void dump_version(char *prog);
  179. char *progname = "nm";
  180.  
  181. /* Number of input files specified.  */
  182.  
  183. int number_of_files;
  184.  
  185. /* Current file's name.  */
  186.  
  187. char *input_name;
  188.  
  189. /* Current member's name, or 0 if processing a non-library file.  */
  190.  
  191. char *input_member;
  192.  
  193. /* Offset within archive of the current member,
  194.    if we are processing an archive.  */
  195.  
  196. int member_offset;
  197.  
  198. /* Command options.  */
  199.  
  200. int external_only;    /* nonzero means print external symbols only.  */
  201. int sort_numerically;   /* sort in numerical order rather than alphabetic  */
  202. int reverse_sort;    /* sort in downward (alphabetic or numeric) order.  */
  203. int no_sort;        /* don't sort; print symbols in order in the file.  */
  204. int undefined_only;    /* print undefined symbols only.  */
  205. int file_on_each_line;    /* print file name on each line.  */
  206. int debugger_syms;    /* print the debugger-only symbols.  */
  207. int print_symdefs;    /* describe the __.SYMDEF data in any archive file specified.  */
  208. int demangle_symbols;   /* Demangle G++ symbols.  */
  209.  
  210. /* The __.SYMDEF member of an archive has the following format:
  211.    1) A longword saying the size of the symdef data that follows
  212.    2) Zero or more  struct symdef  filling that many bytes
  213.    3) A longword saying how many bytes of strings follow
  214.    4) That many bytes of string data.
  215. */
  216.  
  217. struct symdef
  218.   {
  219.     long stringoffset;    /* Offset of this symbol's name in the string data */
  220.     long offset;    /* Offset in the archive of the header-data for the member that
  221.                defines this symbol.  */
  222.   };
  223.  
  224. void decode_switch ();
  225. int decode_arg ();
  226. void do_one_file (), do_one_rel_file (), do_symdef_member ();
  227. char *concat ();
  228.  
  229. main (argc, argv)
  230.      char **argv;
  231.      int argc;
  232. {
  233.   int i;
  234.   int c;
  235.   extern int optind;
  236.  
  237.   if (argv[0][0] != '\0')
  238.     progname = argv[0];
  239.  
  240.   number_of_files = 0;
  241.   external_only = 0;
  242.   sort_numerically = 0;
  243.   reverse_sort = 0;
  244.   no_sort = 0;
  245.   undefined_only = 0;
  246.   file_on_each_line = 0;
  247.   debugger_syms = 0;
  248.   print_symdefs = 0;
  249.   demangle_symbols = 1;
  250.  
  251.   while ((c = getopt (argc, argv, "agnoprsuvx")) != EOF)
  252.     switch (c)
  253.       {
  254.       case 'a':
  255.     debugger_syms = 1;
  256.     break;
  257.  
  258.       case 'g':
  259.     external_only = 1;
  260.     break;
  261.  
  262.       case 'n':
  263.     sort_numerically = 1;
  264.     break;
  265.  
  266.       case 'o':
  267.     file_on_each_line = 1;
  268.     break;
  269.  
  270.       case 'p':
  271.     no_sort = 1;
  272.     break;
  273.  
  274.       case 'r':
  275.     reverse_sort = 1;
  276.     break;
  277.  
  278.       case 's':
  279.     print_symdefs = 1;
  280.     break;
  281.  
  282.       case 'u':
  283.     undefined_only = 1;
  284.     break;
  285.  
  286.       case 'v':
  287.    dump_version(progname);
  288.    exit(0);
  289.    break;
  290.  
  291.       case 'x':
  292.     demangle_symbols = 0;
  293.     break;
  294.       }
  295.  
  296.   number_of_files = argc - optind;
  297.  
  298.   /* Now scan again and print the files.  */
  299.  
  300.   if (argc == optind)
  301.     do_one_file ("a.out");
  302.   else
  303.     for (i = optind; i < argc; i++)
  304.       do_one_file (argv[i]);
  305. }
  306.  
  307. /* Print the filename of the current file on 'outfile' (a stdio stream).  */
  308.  
  309. print_file_name (outfile)
  310.      FILE *outfile;
  311. {
  312.   fprintf (outfile, "%s", input_name);
  313.   if (input_member)
  314.     fprintf (outfile, "(%s)", input_member);
  315. }
  316.  
  317. /* process one input file */
  318. void scan_library ();
  319.  
  320. void
  321. do_one_file (name)
  322.      char *name;
  323. {
  324.   int len, magicnum, desc;
  325.  
  326.   desc = open (name, O_RDONLY, 0);
  327.  
  328.   if (desc < 0)
  329.     {
  330.       perror_name (name);
  331.       return;
  332.     }
  333.  
  334.   input_name = name;
  335.   input_member = 0;
  336.  
  337.   len = read (desc, &magicnum, sizeof magicnum);
  338. #ifdef BYTE_SWAP
  339.   magicnum = SWAP4(magicnum);
  340. #endif
  341.   if (len != sizeof magicnum)
  342.     error_with_file ("failure reading header of ");
  343.   else if (!N_BADMAG (*((struct exec *)&magicnum)))
  344.     do_one_rel_file (desc, 0);
  345.   else
  346.     {
  347.       char armag[SARMAG];
  348.       int nchars;
  349.  
  350.       lseek (desc, 0, 0);
  351.       nchars = read (desc, armag, SARMAG);
  352.  
  353.       if (SARMAG != nchars || strncmp (armag, ARMAG, SARMAG)){
  354.     fprintf(stderr,"Magix is %lx, expected %lx\n",magicnum,OMAGIC);
  355.     error_with_file ("malformed input file (not rel or archive) ");
  356.       }
  357.       else
  358.         scan_library (desc);
  359.     }
  360.  
  361.   close (desc);
  362. }
  363.  
  364. /* Read in the archive data about one member.
  365.    SUBFILE_OFFSET is the address within the archive of the start of that data.
  366.    The value returned is the length of the member's contents, which does
  367.    not include the archive data about the member.
  368.    A pointer to the member's name is stored into *MEMBER_NAME_PTR.
  369.  
  370.    If there are no more valid members, zero is returned.  */
  371.  
  372. int
  373. decode_library_subfile (desc, subfile_offset, member_name_ptr)
  374.      int desc;
  375.      int subfile_offset;
  376.      char **member_name_ptr;
  377. {
  378.   int bytes_read;
  379.   int namelen;
  380.   int member_length;
  381.   char *name;
  382.   struct ar_hdr hdr1;
  383.  
  384.   lseek (desc, subfile_offset, 0);
  385.  
  386. #if (defined(WORD_ALIGNED) && defined(CROSSATARI))
  387.   bytes_read = read (desc, &hdr1.ar_name, 
  388.              sizeof (hdr1.ar_name));
  389.   bytes_read += read (desc, &hdr1.ar_size, 
  390.               sizeof (hdr1.ar_size));
  391.   bytes_read += read (desc, &hdr1.ar_date, 
  392.               sizeof (hdr1.ar_date));
  393.   bytes_read += read (desc, &hdr1.ar_mode, 
  394.               sizeof (hdr1.ar_mode));
  395.   bytes_read += read (desc, &hdr1.ar_uid, 
  396.               sizeof (hdr1.ar_uid));
  397.   bytes_read += read (desc, &hdr1.ar_gid, 
  398.               sizeof (hdr1.ar_gid));
  399.   bytes_read += read (desc, &hdr1.ar_fmag, 
  400.               sizeof (hdr1.ar_fmag));
  401. #else
  402.   bytes_read = read (desc, &hdr1, sizeof hdr1);
  403. #endif
  404.   if (!bytes_read)
  405.     return 0;        /* end of archive */
  406.  
  407. #if (defined(WORD_ALIGNED) && defined(CROSSATARI))
  408.   else if (bytes_read != (sizeof (hdr1.ar_name) +
  409.               sizeof (hdr1.ar_size) +
  410.               sizeof (hdr1.ar_date) +
  411.               sizeof (hdr1.ar_mode) +
  412.               sizeof (hdr1.ar_uid) +
  413.               sizeof (hdr1.ar_gid) +
  414.               sizeof (hdr1.ar_fmag)))
  415.     error_with_file ("malformed library archive ");
  416. #else
  417.   else if (sizeof hdr1 != bytes_read)
  418.     error_with_file ("malformed library archive ");
  419. #endif 
  420.  
  421.   else if (sscanf (hdr1.ar_size, "%d", &member_length) != 1)
  422.     error_with_file ("malformatted header of archive member in ");
  423.  
  424.   else
  425.     {
  426.       for (namelen = 0; ; namelen++)
  427.     if (hdr1.ar_name[namelen] == 0 || hdr1.ar_name[namelen] == ' ')
  428.       break;
  429.  
  430.       name = (char *) xmalloc (namelen+1);
  431.       strncpy (name, hdr1.ar_name, namelen);
  432.       name[namelen] = 0;
  433.  
  434.       *member_name_ptr = name;
  435.  
  436.       return member_length;
  437.     }
  438.   return 0;   /* tell caller to exit loop */
  439. }
  440.  
  441. /* Scan a library and describe each member.  */
  442.  
  443. void
  444. scan_library (desc)
  445.      int desc;
  446. {
  447.   int this_subfile_offset = SARMAG;
  448.   int member_length;
  449. #if (defined(WORD_ALIGNED) && defined(CROSSATARI))
  450.   struct ar_hdr hdr1;
  451. #endif 
  452.  
  453.   if (!file_on_each_line)
  454.     printf ("\n%s:\n", input_name);
  455.   
  456.   while (1)
  457.     {
  458.       member_length
  459.     = decode_library_subfile (desc, this_subfile_offset, &input_member);
  460.       if (member_length == 0)
  461.     break;
  462.  
  463.       /* Describe every member except the ranlib data if any.  */
  464.  
  465. #if (defined(WORD_ALIGNED) && defined(CROSSATARI))
  466.       if (strcmp (input_member, "__.SYMDEF"))
  467.     do_one_rel_file (desc, this_subfile_offset + (sizeof (hdr1.ar_name) +
  468.                               sizeof (hdr1.ar_size) +
  469.                               sizeof (hdr1.ar_date) +
  470.                               sizeof (hdr1.ar_mode) +
  471.                               sizeof (hdr1.ar_uid) +
  472.                               sizeof (hdr1.ar_gid) +
  473.                               sizeof (hdr1.ar_fmag)));
  474.       else if (print_symdefs)
  475.     do_symdef_member (desc, this_subfile_offset + (sizeof (hdr1.ar_name) +
  476.                                sizeof (hdr1.ar_size) +
  477.                                sizeof (hdr1.ar_date) +
  478.                                sizeof (hdr1.ar_mode) +
  479.                                sizeof (hdr1.ar_uid) +
  480.                                sizeof (hdr1.ar_gid) +
  481.                                sizeof (hdr1.ar_fmag)),
  482.               member_length);
  483.  
  484.       this_subfile_offset += ((member_length + (sizeof (hdr1.ar_name) +
  485.                         sizeof (hdr1.ar_size) +
  486.                         sizeof (hdr1.ar_date) +
  487.                         sizeof (hdr1.ar_mode) +
  488.                         sizeof (hdr1.ar_uid) +
  489.                         sizeof (hdr1.ar_gid) +
  490.                         sizeof (hdr1.ar_fmag))) + 1) & -2;
  491. #else
  492.       if (strcmp (input_member, "__.SYMDEF"))
  493.     do_one_rel_file (desc, this_subfile_offset + sizeof (struct ar_hdr));
  494.       else if (print_symdefs)
  495.     do_symdef_member (desc, this_subfile_offset + sizeof (struct ar_hdr), member_length);
  496.  
  497.       this_subfile_offset += ((member_length + sizeof (struct ar_hdr)) + 1) & -2;
  498. #endif 
  499.     }
  500. }
  501.  
  502. void print_symbols (), print_one_symbol ();
  503. void read_header ();
  504. int alphacompare (), valuecompare ();
  505. int filter_symbols ();
  506. char *cplus_demangle();
  507.  
  508. void
  509. do_one_rel_file (desc, offset)
  510.      int desc;
  511.      int offset;
  512. {
  513.   struct exec header;   /* file header read in here */
  514.   int string_size;
  515.   struct nlist *symbols_and_strings;
  516.   int symcount;
  517.   int totalsize;
  518.   char *strings;
  519.  
  520. #if 0
  521.    header.a_magic = 0;
  522. #else
  523.   N_SET_MAGIC(header, 0);
  524. #endif
  525.  
  526.   read_header (desc, &header, offset);
  527.  
  528.   if (N_BADMAG (header))
  529.     {
  530.       error_with_file ("bad magic number in ");
  531.       return;
  532.     }
  533.  
  534.   /* Read the string-table-length out of the file.  */
  535.  
  536.   lseek (desc, N_STROFF (header) + offset, 0);
  537.   if (sizeof string_size != read (desc, &string_size, sizeof string_size))
  538.     {
  539.       error_with_file ("bad string table in ");
  540.       return;
  541.     }
  542. #ifdef BYTE_SWAP
  543.   string_size = SWAP4(string_size);
  544. #endif
  545.  
  546.   /* Number of symbol entries in the file.  */
  547.   symcount = header.a_syms / sizeof (struct nlist);
  548.  
  549.   totalsize = string_size + header.a_syms;
  550.   /* Allocate space for symbol entries and string table.  */
  551.   symbols_and_strings = (struct nlist *) xmalloc (totalsize);
  552.   strings = (char *) symbols_and_strings + header.a_syms;
  553.  
  554.   /* Read them both in all at once.  */
  555.   lseek (desc, N_SYMOFF (header) + offset, 0);
  556.   if (totalsize != read (desc, symbols_and_strings, totalsize))
  557.     {
  558.       error_with_file ("premature end of file in symbols/strings of ");
  559.       return;
  560.     }
  561.  
  562.   /* Identify this file, if desired.  */
  563.  
  564.   if (!file_on_each_line && (number_of_files > 1 || input_member))
  565.     printf ("\n%s:\n", input_member ? input_member : input_name);
  566.  
  567.   /* Discard the symbols we don't want to print; compact the rest down.  */
  568.  
  569.   symcount = filter_symbols (symbols_and_strings, symcount, strings);
  570.     
  571.   /* Modify each symbol entry to point directly at the symbol name.
  572.      This is so the sort routine does not need to be passed
  573.      the value of `strings' separately.  */
  574.  
  575.   {
  576.     struct nlist *p = symbols_and_strings;
  577.     struct nlist *end = symbols_and_strings + symcount;
  578.  
  579.     for (; p < end; p++)
  580.       if (p->n_un.n_strx){
  581. #ifdef BYTE_SWAP
  582.     p->n_un.n_strx = SWAP4(p->n_un.n_strx);
  583. #endif
  584.         p->n_un.n_name = strings + p->n_un.n_strx;
  585.       }
  586.   }
  587.  
  588.   /* Sort the symbols if desired.  */
  589.  
  590.   if (!no_sort)
  591.     qsort (symbols_and_strings, symcount, sizeof (struct nlist),
  592.        sort_numerically ? valuecompare : alphacompare);
  593.  
  594.   /* Print the symbols in the order they are now in.  */
  595.  
  596.   print_symbols (symbols_and_strings, symcount);
  597.  
  598.   free (symbols_and_strings);
  599. }
  600.  
  601.  
  602. /* Read a file's header.  */
  603.  
  604. void
  605. read_header (desc, loc, offset)
  606.      int desc;
  607.      struct exec *loc;
  608.      int offset;
  609. {
  610.   int len;
  611.   lseek (desc, offset, 0);
  612.   len = read (desc, loc, sizeof (struct exec));
  613.   if (len != sizeof (struct exec))
  614.     error_with_file ("failure reading header of ");
  615. #ifdef BYTE_SWAP
  616. loc->a_info = SWAP4(loc->a_info);       /* always four (4) bytes */
  617. if (sizeof(loc->a_text)==4)                     /* does this need to be ? */
  618. {  loc->a_text   = SWAP4(loc->a_text);
  619.    loc->a_data   = SWAP4(loc->a_data);
  620.    loc->a_bss    = SWAP4(loc->a_bss);
  621.    loc->a_syms   = SWAP4(loc->a_syms);
  622.    loc->a_entry  = SWAP4(loc->a_entry);
  623.    loc->a_trsize = SWAP4(loc->a_trsize);
  624.    loc->a_drsize = SWAP4(loc->a_drsize); }
  625. else
  626. {  loc->a_text   = SWAP4(loc->a_text);
  627.    loc->a_data   = SWAP4(loc->a_data);
  628.    loc->a_bss    = SWAP4(loc->a_bss);
  629.    loc->a_syms   = SWAP4(loc->a_syms);
  630.    loc->a_entry  = SWAP4(loc->a_entry);
  631.    loc->a_trsize = SWAP4(loc->a_trsize);
  632.    loc->a_drsize = SWAP4(loc->a_drsize); }
  633. #endif /* BYTE_SWAP */
  634. }
  635.  
  636. /* Choose which symbol entries to print;
  637.    compact them downward to get rid of the rest.
  638.    Return the number of symbols to be printed.  */
  639.  
  640. int
  641. filter_symbols (syms, symcount, strings)
  642.      struct nlist *syms;
  643.      int symcount;
  644.      char *strings;
  645. {
  646.   struct nlist *from = syms, *to = syms;
  647.   struct nlist *end = syms + symcount;
  648.  
  649.   while (from < end)
  650.     {
  651.       int keep = 0;
  652.  
  653.       /* undefined sym or common sym */
  654.       if (from->n_type == N_EXT) keep = !undefined_only || !from->n_value;
  655.       /* global defined sym */
  656.       else if (from->n_type & N_EXT) keep = !undefined_only;
  657.       /* debugger sym: normally don't print */
  658.       else if (from->n_type & ~(N_TYPE | N_EXT)) keep = debugger_syms;
  659.       /* local sym */
  660.       else keep = !external_only && !undefined_only;
  661.  
  662.       if (keep)
  663.     *to++ = *from;
  664.       from++;
  665.     }
  666.  
  667.   return to - syms;
  668. }
  669.  
  670. /* Comparison functions for sorting symbols.  */
  671.  
  672. int
  673. alphacompare (sym1, sym2)
  674.      struct nlist *sym1, *sym2;
  675. {
  676.   if (reverse_sort)
  677.     {
  678.       if (!sym2->n_un.n_name)
  679.     {
  680.       if (sym1->n_un.n_name) return -1;
  681.       else return 0;
  682.     }
  683.       if (!sym1->n_un.n_name) return 1;
  684.       return strcmp (sym2->n_un.n_name, sym1->n_un.n_name);
  685.     }
  686.   else
  687.     {
  688.       if (!sym1->n_un.n_name)
  689.     {
  690.       if (sym2->n_un.n_name) return -1;
  691.       else return 0;
  692.     }
  693.       if (!sym2->n_un.n_name) return 1;
  694.       return strcmp (sym1->n_un.n_name, sym2->n_un.n_name);
  695.     }
  696. }
  697.  
  698.  
  699. int
  700. valuecompare (sym1, sym2)
  701.      struct nlist *sym1, *sym2;
  702. {
  703.   if (reverse_sort)
  704.     return sym2->n_value - sym1->n_value;
  705.   else
  706.     return sym1->n_value - sym2->n_value;
  707. }
  708.  
  709. void
  710. print_symbols (syms, symcount)
  711.      struct nlist *syms;
  712.      int symcount;
  713. {
  714.   int i;
  715.  
  716.   for (i = 0; i < symcount; i++)
  717.     print_one_symbol (&syms[i]);
  718. }
  719.  
  720. void
  721. print_one_symbol (sym)
  722.      struct nlist *sym;
  723. {
  724.   if (file_on_each_line)
  725.     {
  726.       print_file_name (stdout);
  727.       printf (":");
  728.     }
  729.  
  730.   if (undefined_only)
  731.     {
  732.       if (sym->n_type == N_EXT && !sym->n_value)
  733.         printf ("%s\n", sym->n_un.n_name);
  734.       return;
  735.     }
  736.  
  737.   if (sym->n_type & ~N_EXT || sym->n_value)
  738. #ifdef BYTE_SWAP
  739.     printf ("%08x ", SWAP4(sym->n_value));
  740. #else
  741.     printf ("%08x ", sym->n_value);
  742. #endif
  743.   else printf ("         ");
  744.  
  745.   switch (sym->n_type)
  746.     {
  747.       case N_EXT:
  748.         if (sym->n_value) printf ("C");
  749.         else printf ("U");
  750.     break;
  751.  
  752.       case 0:
  753.         if (sym->n_value) printf ("c");
  754.         else printf ("u");
  755.     break;
  756.  
  757.       case N_ABS | N_EXT:
  758.     printf ("A");
  759.     break;
  760.  
  761.       case N_ABS:
  762.     printf ("a");
  763.     break;
  764.  
  765.       case N_TEXT | N_EXT:
  766.     printf ("T");
  767.     break;
  768.  
  769.       case N_TEXT:
  770.     printf ("t");
  771.     break;
  772.  
  773.       case N_DATA | N_EXT:
  774.     printf ("D");
  775.     break;
  776.  
  777.       case N_DATA:
  778.     printf ("d");
  779.     break;
  780.  
  781.       case N_BSS | N_EXT:
  782.     printf ("B");
  783.     break;
  784.  
  785.       case N_BSS:
  786.     printf ("b");
  787.     break;
  788.  
  789.       default:
  790.     {
  791.       char *s;
  792.       switch (sym->n_type)
  793.         {
  794.         case N_GSYM:
  795.           s = "GSYM";
  796.           break;
  797.         case N_FNAME:
  798.           s = "FNAME";
  799.           break;
  800.         case N_FUN:
  801.           s = "FUN";
  802.           break;
  803.         case N_STSYM:
  804.           s = "STSYM";
  805.           break;
  806.         case N_LCSYM:
  807.           s = "LCSYM";
  808.           break;
  809.         case N_RSYM:
  810.           s = "RSYM";
  811.           break;
  812.         case N_SLINE:
  813.           s = "SLINE";
  814.           break;
  815.         case N_SSYM:
  816.           s = "SSYM";
  817.           break;
  818.         case N_SO:
  819.           s = "SO";
  820.           break;
  821.         case N_LSYM:
  822.           s = "LSYM";
  823.           break;
  824.         case N_SOL:
  825.           s = "SOL";
  826.           break;
  827.         case N_PSYM:
  828.           s = "PSYM";
  829.           break;
  830.         case N_ENTRY:
  831.           s = "ENTRY";
  832.           break;
  833.         case N_LBRAC:
  834.           s = "LBRAC";
  835.           break;
  836.         case N_RBRAC:
  837.           s = "RBRAC";
  838.           break;
  839.         case N_BCOMM:
  840.           s = "BCOMM";
  841.           break;
  842.         case N_ECOMM:
  843.           s = "ECOMM";
  844.           break;
  845.         case N_ECOML:
  846.           s = "ECOML";
  847.           break;
  848.         case N_LENG:
  849.           s = "LENG";
  850.           break;
  851.         case N_BINCL:
  852.           s = "BINCL";
  853.           break;
  854.         case N_EINCL:
  855.           s = "EINCL";
  856.           break;
  857.         default:
  858.           s = "";
  859.         }
  860. #ifdef BYTE_SWAP
  861.       printf ("- %02x %04x %5s", sym->n_other, SWAP2(sym->n_desc), s);
  862. #else
  863.       printf ("- %02x %04x %5s", sym->n_other, sym->n_desc, s);
  864. #endif
  865.     }
  866.     }
  867.  
  868.   if (sym->n_un.n_name) {
  869.     if (demangle_symbols) {
  870.       char *nm;
  871.       
  872.       if ((nm = cplus_demangle(sym->n_un.n_name)) == NULL)
  873.         printf (" %s\n", sym->n_un.n_name);
  874.       else
  875.       {
  876.         printf (" %s\n", nm);
  877.     free(nm);
  878.       }
  879.     } else {
  880.       printf (" %s\n", sym->n_un.n_name);
  881.     }
  882.   } else {
  883.     printf (" \n");
  884.   }
  885. }
  886.  
  887. void
  888. do_symdef_member (desc, offset, member_length)
  889.      int desc;
  890.      int offset;
  891.      int member_length;
  892. {
  893.   int symdef_size;
  894.   int nsymdefs;
  895.   struct symdef *symdefs;
  896.   int stringsize;
  897.   char *strings;
  898.   int i;
  899.   char *member_name;
  900.   int member_offset;
  901.  
  902.   /* read the string-table-length out of the file */
  903.  
  904.   lseek (desc, offset, 0);
  905.   if (sizeof symdef_size != read (desc, &symdef_size, sizeof symdef_size))
  906.     {
  907.       error_with_file ("premature eof in ");
  908.       return;
  909.     }
  910. #ifdef BYTE_SWAP
  911.   symdef_size = SWAP4(symdef_size);
  912. #endif
  913.  
  914.   if (symdef_size < 0)
  915.     {
  916.       error_with_file ("invalid size value in ");
  917.       return;
  918.     }
  919.  
  920.   nsymdefs = symdef_size / sizeof (struct symdef);
  921.   symdefs = (struct symdef *) xmalloc (symdef_size);
  922.   if (symdef_size != read (desc, symdefs, symdef_size))
  923.     {
  924.       error_with_file ("premature eof in ");
  925.       free (symdefs);
  926.       return;
  927.     }
  928.  
  929.   if (sizeof stringsize != read (desc, &stringsize, sizeof stringsize))
  930.     {
  931.       error_with_file ("premature eof in ");
  932.       free (symdefs);
  933.       return;
  934.     }
  935. #ifdef BYTE_SWAP
  936.   stringsize = SWAP4(stringsize);
  937. #endif
  938.  
  939.   if (stringsize < 0)
  940.     {
  941.       error_with_file ("invalid size value in ");
  942.       free (symdefs);
  943.       return;
  944.     }
  945.  
  946.   strings = (char *) xmalloc (stringsize);
  947.   if (stringsize != read (desc, strings, stringsize))
  948.     {
  949.       error_with_file ("premature eof in ");
  950.       free (symdefs);
  951.       free (strings);
  952.       return;
  953.     }
  954.  
  955.   if (stringsize + symdef_size + sizeof stringsize + sizeof symdef_size != member_length)
  956.     {
  957.       error_with_file ("size of data isn't what the data calls for in ");
  958.       free (symdefs);
  959.       free (strings);
  960.       return;
  961.     }
  962.  
  963.   if (!file_on_each_line && (number_of_files > 1 || input_member))
  964.     printf ("\n%s:\n", input_member ? input_member : input_name);
  965.     
  966.   member_offset = -1;
  967.   for (i = 0; i < nsymdefs; i++)
  968.     {
  969. #ifdef BYTE_SWAP
  970.   symdefs[i].stringoffset = SWAP4(symdefs[i].stringoffset);
  971. #endif
  972.   if (symdefs[i].stringoffset < 0 || symdefs[i].stringoffset >= stringsize)
  973.     {
  974.       error_with_file ("invalid entry in ");
  975.       free (symdefs);
  976.       free (strings);
  977.       return;
  978.     }
  979. #ifdef BYTE_SWAP
  980.   symdefs[i].offset = SWAP4(symdefs[i].offset);
  981. #endif
  982.       if (member_offset != symdefs[i].offset)
  983.     {
  984.       member_offset = symdefs[i].offset;
  985.       decode_library_subfile (desc, member_offset, &member_name);
  986.     }
  987.       if (file_on_each_line)
  988.     {
  989.       print_file_name (stdout);
  990.       printf (":");
  991.     }
  992.       printf ("%s in %s\n", symdefs[i].stringoffset + strings, member_name);
  993.     }
  994.   free (symdefs);
  995.   free (strings);
  996. }
  997.  
  998. /* Report a fatal error.
  999.    STRING is a printf format string and ARG is one arg for it.  */
  1000.  
  1001. fatal (string, arg)
  1002.      char *string, *arg;
  1003. {
  1004.   fprintf (stderr, "%s: ", progname);
  1005.   fprintf (stderr, string, arg);
  1006.   fprintf (stderr, "\n");
  1007.   exit (1);
  1008. }
  1009.  
  1010. /* Report a nonfatal error.
  1011.    STRING is a printf format string and ARG is one arg for it.  */
  1012.  
  1013. error (string, arg)
  1014.      char *string, *arg;
  1015. {
  1016.   fprintf (stderr, "%s: ", progname);
  1017.   fprintf (stderr, string, arg);
  1018.   fprintf (stderr, "\n");
  1019. }
  1020.  
  1021. /* Report a nonfatal error.
  1022.    STRING is printed, followed by the current file name.  */
  1023.  
  1024. error_with_file (string)
  1025.      char *string;
  1026. {
  1027.   fprintf (stderr, "%s: ", progname);
  1028.   fprintf (stderr, string);
  1029.   print_file_name (stderr);
  1030.   fprintf (stderr, "\n");
  1031. }
  1032.  
  1033. /* Report a fatal error using the message for the last failed system call,
  1034.    followed by the string NAME.  */
  1035.  
  1036. perror_name (name)
  1037.      char *name;
  1038. {
  1039.   extern int errno, sys_nerr;
  1040.   extern char *sys_errlist[];
  1041.   char *s;
  1042.  
  1043.   if (errno < sys_nerr)
  1044.     s = concat ("", sys_errlist[errno], " for %s");
  1045.   else
  1046.     s = "cannot open %s";
  1047.   error (s, name);
  1048. }
  1049.  
  1050. /* Like malloc but get fatal error if memory is exhausted.  */
  1051.  
  1052. int
  1053. xmalloc (size)
  1054.      int size;
  1055. {
  1056.   int result = malloc (size);
  1057.   if (!result)
  1058.     fatal ("virtual memory exhausted", 0);
  1059.   return result;
  1060. }
  1061.  
  1062. /* Like realloc but get fatal error if memory is exhausted.  */
  1063.  
  1064. int
  1065. xrealloc (ptr, size)
  1066.      char *ptr;
  1067.      int size;
  1068. {
  1069.   register int result = realloc (ptr, size);
  1070.   if (!result)
  1071.     fatal ("virtual memory exhausted", 0);
  1072.   return result;
  1073. }
  1074.  
  1075. /* Return a newly-allocated string
  1076.    whose contents concatenate those of S1, S2, S3.  */
  1077.  
  1078. char *
  1079. concat (s1, s2, s3)
  1080.      char *s1, *s2, *s3;
  1081. {
  1082.   int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  1083.   char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  1084.  
  1085.   strcpy (result, s1);
  1086.   strcpy (result + len1, s2);
  1087.   strcpy (result + len1 + len2, s3);
  1088.   *(result + len1 + len2 + len3) = 0;
  1089.  
  1090.   return result;
  1091. }
  1092.