nm.c 26 KB

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