nm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. /* Describe symbol table of a rel file.
  2. Copyright (C) 1986 Richard M. Stallman
  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 Richard M. Stallman"; and include following the
  29. copyright notice a verbatim copy of the above disclaimer of warranty
  30. 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 freely distributed
  39. and licensed to all third parties on terms identical to those
  40. contained in this License Agreement (except that you may choose
  41. to grant more extensive warranty protection to third parties,
  42. at your option).
  43. 3. You may copy and distribute this program or any portion of it in
  44. compiled, executable or object code form under the terms of Paragraphs
  45. 1 and 2 above provided that you do the following:
  46. a) cause each such copy to be accompanied by the
  47. corresponding machine-readable source code, which must
  48. be distributed under the terms of Paragraphs 1 and 2 above; or,
  49. b) cause each such copy to be accompanied by a
  50. written offer, with no time limit, to give any third party
  51. free (except for a nominal shipping charge) a machine readable
  52. copy of the corresponding source code, to be distributed
  53. under the terms of Paragraphs 1 and 2 above; or,
  54. c) in the case of a recipient of this program in compiled, executable
  55. or object code form (without the corresponding source code) you
  56. shall cause copies you distribute to be accompanied by a copy
  57. of the written offer of source code which you received along
  58. with the copy you received.
  59. 4. You may not copy, sublicense, distribute or transfer this program
  60. except as expressly provided under this License Agreement. Any attempt
  61. otherwise to copy, sublicense, distribute or transfer this program is void and
  62. your rights to use the program under this License agreement shall be
  63. automatically terminated. However, parties who have received computer
  64. software programs from you with this License Agreement will not have
  65. their licenses terminated so long as such parties remain in full compliance.
  66. In other words, you are welcome to use, share and improve this program.
  67. You are forbidden to forbid anyone else to use, share and improve
  68. what you give them. Help stamp out software-hoarding! */
  69. /* define this for running on the TI NU machine. */
  70. /* #define numachine 1 */
  71. #ifdef numachine
  72. #define O_RDONLY 0
  73. #endif
  74. #include <a.out.h>
  75. #include <stab.h>
  76. #include <stdio.h>
  77. #include <strings.h>
  78. #include <ar.h>
  79. #ifndef numachine
  80. #include <sys/file.h>
  81. #endif
  82. int number_of_files;
  83. /* current file's name */
  84. char *input_name;
  85. /* current member's name, or 0 if processing a non-library file. */
  86. char *input_member;
  87. /* offset within archive of the current member, if we are processing an archive. */
  88. int member_offset;
  89. /* command switches */
  90. int external_only; /* nonzero means print external symbols only. */
  91. int sort_numerically; /* sort in numerical order rather than alphabetic */
  92. int reverse_sort; /* sort in decreasing (alphabetical or numerical) order. */
  93. int no_sort; /* don't sort; print symbols in the order they appear in the file. */
  94. int undefined_only; /* print undefined symbols only. */
  95. int file_on_each_line; /* print file name on each line. */
  96. int debugger_syms; /* print the debugger-only symbols. */
  97. int print_symdefs; /* describe the __.SYMDEF data in any archive file specified. */
  98. /* The __.SYMDEF member of an archive has the following format:
  99. 1) A longword saying the size of the symdef data that follows
  100. 2) Zero or more struct symdef filling that many bytes
  101. 3) A longword saying how many bytes of strings follow
  102. 4) That many bytes of string data.
  103. */
  104. struct symdef
  105. {
  106. long stringoffset; /* Offset of this symbol's name in the string data */
  107. long offset; /* Offset in the archive of the header-data for the member that
  108. defines this symbol. */
  109. };
  110. void decode_switch ();
  111. int decode_arg ();
  112. void do_one_file (), do_one_rel_file (), do_symdef_member ();
  113. char *concat ();
  114. main(argc, argv)
  115. char **argv;
  116. int argc;
  117. {
  118. int i;
  119. number_of_files = 0;
  120. external_only = 0;
  121. sort_numerically = 0;
  122. reverse_sort = 0;
  123. no_sort = 0;
  124. undefined_only = 0;
  125. file_on_each_line = 0;
  126. debugger_syms = 0;
  127. print_symdefs = 0;
  128. /* First process the switches. */
  129. for (i = 1; i < argc; i++)
  130. {
  131. int code = decode_arg (argv[i]);
  132. if (code)
  133. {
  134. decode_switch (argv[i]);
  135. i += code - 1;
  136. }
  137. else
  138. number_of_files++;
  139. }
  140. if (!number_of_files)
  141. fatal ("no files specified", 0);
  142. /* Now scan again and print the files. */
  143. for (i = 1; i < argc; i++)
  144. {
  145. int code = decode_arg (argv[i]);
  146. if (code)
  147. i += code - 1;
  148. else
  149. do_one_file (argv[i]);
  150. }
  151. }
  152. /* analyze a command line argument.
  153. Returns 0 if the argument is a filename.
  154. Returns 1 if the argument is a switch complete in itself.
  155. Returns 2 if the argument is a switch which gobbles the following argument.
  156. (Except that `nm' doesn't have any switches that need arguments) */
  157. int
  158. decode_arg(arg)
  159. char *arg;
  160. {
  161. if (*arg != '-') return 0;
  162. return 1;
  163. }
  164. void
  165. decode_switch (swt)
  166. char *swt;
  167. {
  168. char *tail = swt + 1;
  169. char c;
  170. while (1)
  171. {
  172. switch ((c = *tail++))
  173. {
  174. case 0:
  175. return;
  176. case 'a':
  177. debugger_syms = 1;
  178. break;
  179. case 'g':
  180. external_only = 1;
  181. break;
  182. case 'n':
  183. sort_numerically = 1;
  184. break;
  185. case 'o':
  186. file_on_each_line = 1;
  187. break;
  188. case 'p':
  189. no_sort = 1;
  190. break;
  191. case 'r':
  192. reverse_sort = 1;
  193. break;
  194. case 's':
  195. print_symdefs = 1;
  196. break;
  197. case 'u':
  198. undefined_only = 1;
  199. break;
  200. default:
  201. error ("invalid command switch character -%c", c);
  202. }
  203. }
  204. }
  205. /* open the file specified by `name', and return a descriptor. */
  206. /* Print the filename of the current file on 'outfile' (a stdio stream). */
  207. print_file_name (outfile)
  208. FILE *outfile;
  209. {
  210. fprintf (outfile, "%s", input_name);
  211. if (input_member)
  212. fprintf (outfile, "(%s)", input_member);
  213. }
  214. /* process one input file */
  215. void scan_library ();
  216. void
  217. do_one_file (name)
  218. char *name;
  219. {
  220. int len, magicnum, desc;
  221. desc = open (name, O_RDONLY, 0);
  222. if (desc < 0)
  223. {
  224. perror_name (name);
  225. return;
  226. }
  227. input_name = name;
  228. input_member = 0;
  229. len = read (desc, &magicnum, sizeof magicnum);
  230. if (len != sizeof magicnum)
  231. error_with_file ("failure reading header of ");
  232. else if (!N_BADMAG(*((struct exec *)&magicnum)))
  233. do_one_rel_file (desc, 0);
  234. else
  235. {
  236. char armag[SARMAG];
  237. lseek (desc, 0, 0);
  238. if (SARMAG != read (desc, armag, SARMAG) || strncmp (armag, ARMAG, SARMAG))
  239. error_with_file ("malformed input file (not rel or archive) ");
  240. else
  241. scan_library (desc);
  242. }
  243. close (desc);
  244. }
  245. /* read in the archive data about one member.
  246. subfile_offset is the address within the archive of the start of that data.
  247. The value returned is the length of the member's contents, which does
  248. not include the archive data about the member.
  249. If there are no more valid members, zero is returned. */
  250. int
  251. decode_library_subfile (desc, subfile_offset, member_name_ptr)
  252. int desc;
  253. int subfile_offset;
  254. char **member_name_ptr;
  255. {
  256. int bytes_read;
  257. int namelen;
  258. int member_length;
  259. char *name;
  260. struct ar_hdr hdr1;
  261. lseek (desc, subfile_offset, 0);
  262. bytes_read = read (desc, &hdr1, sizeof hdr1);
  263. if (!bytes_read)
  264. ; /* end of archive */
  265. else if (sizeof hdr1 != bytes_read)
  266. error_with_file ("malformed library archive ");
  267. else if (sscanf (hdr1.ar_size, "%d", &member_length) != 1)
  268. error_with_file ("malformatted header of archive member in ");
  269. else
  270. {
  271. namelen = index (hdr1.ar_name, ' ') - hdr1.ar_name;
  272. name = (char *) xmalloc (namelen+1);
  273. strncpy (name, hdr1.ar_name, namelen);
  274. *(name + namelen) = 0;
  275. *member_name_ptr = name;
  276. return member_length;
  277. }
  278. return 0; /* tell caller to exit loop */
  279. }
  280. /* Scan a library and describe each member. */
  281. void
  282. scan_library (desc)
  283. int desc;
  284. {
  285. int this_subfile_offset = SARMAG;
  286. int member_length;
  287. if (!file_on_each_line)
  288. printf ("\n%s:\n", input_name);
  289. while (member_length = decode_library_subfile (desc, this_subfile_offset, &input_member))
  290. {
  291. /* describe every member except the ranlib data if any */
  292. if (strcmp (input_member, "__.SYMDEF"))
  293. do_one_rel_file (desc, this_subfile_offset + sizeof (struct ar_hdr));
  294. else if (print_symdefs)
  295. do_symdef_member (desc, this_subfile_offset + sizeof (struct ar_hdr), member_length);
  296. this_subfile_offset += ((member_length + sizeof (struct ar_hdr)) + 1) & -2;
  297. }
  298. }
  299. void print_symbols (), print_one_symbol ();
  300. void read_header ();
  301. int alphacompare (), valuecompare ();
  302. int filter_symbols ();
  303. void
  304. do_one_rel_file (desc, offset)
  305. int desc;
  306. int offset;
  307. {
  308. struct exec header; /* file header read in here */
  309. int string_size;
  310. struct nlist *symbols_and_strings;
  311. int symcount;
  312. int totalsize;
  313. char *strings;
  314. header.a_magic = 0;
  315. read_header (desc, &header, offset);
  316. if (N_BADMAG(header))
  317. {
  318. error_with_file ("bad magic number in ");
  319. return;
  320. }
  321. /* read the string-table-length out of the file */
  322. lseek (desc, N_STROFF(header) + offset, 0);
  323. if (sizeof string_size != read (desc, &string_size, sizeof string_size))
  324. {
  325. error_with_file ("bad string table in ");
  326. return;
  327. }
  328. /* number of symbol entries in the file */
  329. symcount = header.a_syms / sizeof (struct nlist);
  330. totalsize = string_size + header.a_syms;
  331. /* allocate space for symbol entries and string table */
  332. symbols_and_strings = (struct nlist *) xmalloc (totalsize);
  333. strings = (char *) symbols_and_strings + header.a_syms;
  334. /* read them both in all at once */
  335. lseek (desc, N_SYMOFF(header) + offset, 0);
  336. if (totalsize != read (desc, symbols_and_strings, totalsize))
  337. {
  338. error_with_file ("premature end of file in symbols/strings of ");
  339. return;
  340. }
  341. /* identify this file, if desired */
  342. if (!file_on_each_line && (number_of_files > 1 || input_member))
  343. printf ("\n%s:\n", input_member ? input_member : input_name);
  344. /* discard the symbols we don't want to print; compact the rest down */
  345. symcount = filter_symbols (symbols_and_strings, symcount, strings);
  346. /* modify each symbol entry to point directly at the symbol name.
  347. This is so the sort routine does not need to be passed
  348. the value of `strings' separately. */
  349. {
  350. struct nlist *p = symbols_and_strings;
  351. struct nlist *end = symbols_and_strings + symcount;
  352. for (; p < end; p++)
  353. if (p->n_un.n_strx)
  354. p->n_un.n_name = strings + p->n_un.n_strx;
  355. }
  356. /* sort the symbols if desired */
  357. if (!no_sort)
  358. qsort (symbols_and_strings, symcount, sizeof (struct nlist),
  359. sort_numerically ? valuecompare : alphacompare);
  360. /* print the symbols in the order they are now in. */
  361. print_symbols (symbols_and_strings, symcount);
  362. free (symbols_and_strings);
  363. }
  364. /* read a file's header */
  365. void
  366. read_header (desc, loc, offset)
  367. int desc;
  368. struct exec *loc;
  369. int offset;
  370. {
  371. int len;
  372. lseek (desc, offset, 0);
  373. len = read (desc, loc, sizeof (struct exec));
  374. if (len != sizeof (struct exec))
  375. error_with_file ("failure reading header of ");
  376. }
  377. /* Choose which symbol entries to print;
  378. compact them downward to get rid of the rest.
  379. Return the number of symbols to be printed. */
  380. int
  381. filter_symbols (syms, symcount, strings)
  382. struct nlist *syms;
  383. int symcount;
  384. char *strings;
  385. {
  386. struct nlist *from = syms, *to = syms;
  387. struct nlist *end = syms + symcount;
  388. while (from < end)
  389. {
  390. int keep = 0;
  391. /* undefined sym or common sym */
  392. if (from->n_type == N_EXT) keep = !undefined_only || !from->n_value;
  393. /* global defined sym */
  394. else if (from->n_type & N_EXT) keep = !undefined_only;
  395. /* debugger sym: normally don't print */
  396. else if (from->n_type & ~(N_TYPE | N_EXT)) keep = debugger_syms;
  397. /* local sym */
  398. else keep = !external_only && !undefined_only;
  399. if (keep)
  400. *to++ = *from;
  401. from++;
  402. }
  403. return to - syms;
  404. }
  405. /* comparison functions for sorting symbols */
  406. int
  407. alphacompare (sym1, sym2)
  408. struct nlist *sym1, *sym2;
  409. {
  410. if (reverse_sort)
  411. {
  412. if (!sym2)
  413. {
  414. if (sym1) return -1;
  415. else return 0;
  416. }
  417. if (!sym1) return 1;
  418. return strcmp (sym2->n_un.n_name, sym1->n_un.n_name);
  419. }
  420. else
  421. {
  422. if (!sym1)
  423. {
  424. if (sym2) return -1;
  425. else return 0;
  426. }
  427. if (!sym2) return 1;
  428. return strcmp (sym1->n_un.n_name, sym2->n_un.n_name);
  429. }
  430. }
  431. int
  432. valuecompare (sym1, sym2)
  433. struct nlist *sym1, *sym2;
  434. {
  435. if (reverse_sort)
  436. return sym2->n_value - sym1->n_value;
  437. else
  438. return sym1->n_value - sym2->n_value;
  439. }
  440. void
  441. print_symbols (syms, symcount)
  442. struct nlist *syms;
  443. int symcount;
  444. {
  445. int i;
  446. for (i = 0; i < symcount; i++)
  447. print_one_symbol (&syms[i]);
  448. }
  449. void
  450. print_one_symbol (sym)
  451. struct nlist *sym;
  452. {
  453. if (file_on_each_line)
  454. {
  455. print_file_name(stdout);
  456. printf (":");
  457. }
  458. if (undefined_only)
  459. {
  460. if (sym->n_type == N_EXT && !sym->n_value)
  461. printf ("%s\n", sym->n_un.n_name);
  462. return;
  463. }
  464. if (sym->n_type & ~N_EXT || sym->n_value)
  465. printf ("%08x ", sym->n_value);
  466. else printf (" ");
  467. switch (sym->n_type)
  468. {
  469. case N_EXT:
  470. if (sym->n_value) printf ("C");
  471. else printf ("U");
  472. break;
  473. case 0:
  474. if (sym->n_value) printf ("c");
  475. else printf ("u");
  476. break;
  477. case N_ABS | N_EXT:
  478. printf ("A");
  479. break;
  480. case N_ABS:
  481. printf ("a");
  482. break;
  483. case N_TEXT | N_EXT:
  484. printf ("T");
  485. break;
  486. case N_TEXT:
  487. printf ("t");
  488. break;
  489. case N_DATA | N_EXT:
  490. printf ("D");
  491. break;
  492. case N_DATA:
  493. printf ("d");
  494. break;
  495. case N_BSS | N_EXT:
  496. printf ("B");
  497. break;
  498. case N_BSS:
  499. printf ("b");
  500. break;
  501. default:
  502. {
  503. char *s;
  504. switch (sym->n_type)
  505. {
  506. case N_GSYM:
  507. s = "GSYM";
  508. break;
  509. case N_FNAME:
  510. s = "FNAME";
  511. break;
  512. case N_FUN:
  513. s = "FUN";
  514. break;
  515. case N_STSYM:
  516. s = "STSYM";
  517. break;
  518. case N_LCSYM:
  519. s = "LCSYM";
  520. break;
  521. case N_RSYM:
  522. s = "RSYM";
  523. break;
  524. case N_SLINE:
  525. s = "SLINE";
  526. break;
  527. case N_SSYM:
  528. s = "SSYM";
  529. break;
  530. case N_SO:
  531. s = "SO";
  532. break;
  533. case N_LSYM:
  534. s = "LSYM";
  535. break;
  536. case N_SOL:
  537. s = "SOL";
  538. break;
  539. case N_PSYM:
  540. s = "PSYM";
  541. break;
  542. case N_ENTRY:
  543. s = "ENTRY";
  544. break;
  545. case N_LBRAC:
  546. s = "LBRAC";
  547. break;
  548. case N_RBRAC:
  549. s = "RBRAC";
  550. break;
  551. case N_BCOMM:
  552. s = "BCOMM";
  553. break;
  554. case N_ECOMM:
  555. s = "ECOMM";
  556. break;
  557. case N_ECOML:
  558. s = "ECOML";
  559. break;
  560. case N_LENG:
  561. s = "LENG";
  562. break;
  563. default:
  564. s = "";
  565. }
  566. printf ("- %02x %04x %5s", sym->n_other, sym->n_desc, s);
  567. }
  568. }
  569. if (sym->n_un.n_name)
  570. printf (" %s\n", sym->n_un.n_name);
  571. else
  572. printf (" \n");
  573. }
  574. void
  575. do_symdef_member (desc, offset, member_length)
  576. int desc;
  577. int offset;
  578. int member_length;
  579. {
  580. int symdef_size;
  581. int nsymdefs;
  582. struct symdef *symdefs;
  583. int stringsize;
  584. char *strings;
  585. int i;
  586. char *member_name;
  587. int member_offset;
  588. /* read the string-table-length out of the file */
  589. lseek (desc, offset, 0);
  590. if (sizeof symdef_size != read (desc, &symdef_size, sizeof symdef_size))
  591. {
  592. error_with_file ("premature eof in ");
  593. return;
  594. }
  595. if (symdef_size < 0)
  596. {
  597. error_with_file ("invalid size value in ");
  598. return;
  599. }
  600. nsymdefs = symdef_size / sizeof (struct symdef);
  601. symdefs = (struct symdef *) alloca (symdef_size);
  602. if (symdef_size != read (desc, symdefs, symdef_size))
  603. {
  604. error_with_file ("premature eof in ");
  605. return;
  606. }
  607. if (stringsize < 0)
  608. {
  609. error_with_file ("invalid size value in ");
  610. return;
  611. }
  612. if (sizeof stringsize != read (desc, &stringsize, sizeof stringsize))
  613. {
  614. error_with_file ("premature eof in ");
  615. return;
  616. }
  617. strings = (char *) alloca (stringsize);
  618. if (stringsize != read (desc, strings, stringsize))
  619. {
  620. error_with_file ("premature eof in ");
  621. return;
  622. }
  623. if (stringsize + symdef_size + sizeof stringsize + sizeof symdef_size != member_length)
  624. {
  625. error_with_file ("size of data isn't what the data calls for in ");
  626. return;
  627. }
  628. if (!file_on_each_line && (number_of_files > 1 || input_member))
  629. printf ("\n%s:\n", input_member ? input_member : input_name);
  630. member_offset = -1;
  631. for (i = 0; i < nsymdefs; i++)
  632. {
  633. if (symdefs[i].stringoffset < 0 || symdefs[i].stringoffset >= stringsize)
  634. {
  635. error_with_file ("invalid entry in ");
  636. return;
  637. }
  638. if (member_offset != symdefs[i].offset)
  639. {
  640. member_offset = symdefs[i].offset;
  641. decode_library_subfile (desc, member_offset, &member_name);
  642. }
  643. if (file_on_each_line)
  644. {
  645. print_file_name(stdout);
  646. printf (":");
  647. }
  648. printf ("%s in %s\n", symdefs[i].stringoffset + strings, member_name);
  649. }
  650. }
  651. /* Report a fatal error. 'string' is a printf format string and 'arg' is one arg for it. */
  652. fatal (string, arg)
  653. char *string, *arg;
  654. {
  655. fprintf (stderr, "nm: ");
  656. fprintf (stderr, string, arg);
  657. fprintf (stderr, "\n");
  658. exit (1);
  659. }
  660. /* Report a nonfatal error. 'string' is a printf format string and 'arg' is one arg for it. */
  661. error (string, arg)
  662. char *string, *arg;
  663. {
  664. fprintf (stderr, "nm: ");
  665. fprintf (stderr, string, arg);
  666. fprintf (stderr, "\n");
  667. }
  668. /* Report a nonfatal error. 'string' is printed, followed by the current file name. */
  669. error_with_file (string)
  670. char *string;
  671. {
  672. fprintf (stderr, "nm: ");
  673. fprintf (stderr, string);
  674. print_file_name (stderr);
  675. fprintf (stderr, "\n");
  676. }
  677. /* Report a fatal error using the message for the last failed system call,
  678. followed by the string 'name'. */
  679. perror_name (name)
  680. char *name;
  681. {
  682. extern int errno, sys_nerr;
  683. extern char *sys_errlist[];
  684. char *s;
  685. if (errno < sys_nerr)
  686. s = concat ("", sys_errlist[errno], " for %s");
  687. else
  688. s = "cannot open %s";
  689. error (s, name);
  690. }
  691. /* Like malloc but get fatal error if memory is exhausted. */
  692. int
  693. xmalloc (size)
  694. int size;
  695. {
  696. int result = malloc (size);
  697. if (!result)
  698. fatal ("virtual memory exhausted", 0);
  699. return result;
  700. }
  701. /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
  702. char *
  703. concat (s1, s2, s3)
  704. char *s1, *s2, *s3;
  705. {
  706. int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  707. char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  708. strcpy (result, s1);
  709. strcpy (result + len1, s2);
  710. strcpy (result + len1 + len2, s3);
  711. *(result + len1 + len2 + len3) = 0;
  712. return result;
  713. }