nm.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811
  1. /* nm.c -- Describe symbol table of a rel file.
  2. Copyright (C) 1991-2017 Free Software Foundation, Inc.
  3. This file is part of GNU Binutils.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  15. 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include "bfd.h"
  18. #include "progress.h"
  19. #include "getopt.h"
  20. #include "aout/stab_gnu.h"
  21. #include "aout/ranlib.h"
  22. #include "demangle.h"
  23. #include "libiberty.h"
  24. #include "elf-bfd.h"
  25. #include "elf/common.h"
  26. #define DO_NOT_DEFINE_AOUTHDR
  27. #define DO_NOT_DEFINE_FILHDR
  28. #define DO_NOT_DEFINE_LINENO
  29. #define DO_NOT_DEFINE_SCNHDR
  30. #include "coff/external.h"
  31. #include "coff/internal.h"
  32. #include "libcoff.h"
  33. #include "bucomm.h"
  34. #include "plugin-api.h"
  35. #include "plugin.h"
  36. /* When sorting by size, we use this structure to hold the size and a
  37. pointer to the minisymbol. */
  38. struct size_sym
  39. {
  40. const void *minisym;
  41. bfd_vma size;
  42. };
  43. /* When fetching relocs, we use this structure to pass information to
  44. get_relocs. */
  45. struct get_relocs_info
  46. {
  47. asection **secs;
  48. arelent ***relocs;
  49. long *relcount;
  50. asymbol **syms;
  51. };
  52. struct extended_symbol_info
  53. {
  54. symbol_info *sinfo;
  55. bfd_vma ssize;
  56. elf_symbol_type *elfinfo;
  57. coff_symbol_type *coffinfo;
  58. /* FIXME: We should add more fields for Type, Line, Section. */
  59. };
  60. #define SYM_NAME(sym) (sym->sinfo->name)
  61. #define SYM_VALUE(sym) (sym->sinfo->value)
  62. #define SYM_TYPE(sym) (sym->sinfo->type)
  63. #define SYM_STAB_NAME(sym) (sym->sinfo->stab_name)
  64. #define SYM_STAB_DESC(sym) (sym->sinfo->stab_desc)
  65. #define SYM_STAB_OTHER(sym) (sym->sinfo->stab_other)
  66. #define SYM_SIZE(sym) \
  67. (sym->elfinfo ? sym->elfinfo->internal_elf_sym.st_size: sym->ssize)
  68. /* The output formatting functions. */
  69. static void print_object_filename_bsd (char *);
  70. static void print_object_filename_sysv (char *);
  71. static void print_object_filename_posix (char *);
  72. static void print_archive_filename_bsd (char *);
  73. static void print_archive_filename_sysv (char *);
  74. static void print_archive_filename_posix (char *);
  75. static void print_archive_member_bsd (char *, const char *);
  76. static void print_archive_member_sysv (char *, const char *);
  77. static void print_archive_member_posix (char *, const char *);
  78. static void print_symbol_filename_bsd (bfd *, bfd *);
  79. static void print_symbol_filename_sysv (bfd *, bfd *);
  80. static void print_symbol_filename_posix (bfd *, bfd *);
  81. static void print_value (bfd *, bfd_vma);
  82. static void print_symbol_info_bsd (struct extended_symbol_info *, bfd *);
  83. static void print_symbol_info_sysv (struct extended_symbol_info *, bfd *);
  84. static void print_symbol_info_posix (struct extended_symbol_info *, bfd *);
  85. /* Support for different output formats. */
  86. struct output_fns
  87. {
  88. /* Print the name of an object file given on the command line. */
  89. void (*print_object_filename) (char *);
  90. /* Print the name of an archive file given on the command line. */
  91. void (*print_archive_filename) (char *);
  92. /* Print the name of an archive member file. */
  93. void (*print_archive_member) (char *, const char *);
  94. /* Print the name of the file (and archive, if there is one)
  95. containing a symbol. */
  96. void (*print_symbol_filename) (bfd *, bfd *);
  97. /* Print a line of information about a symbol. */
  98. void (*print_symbol_info) (struct extended_symbol_info *, bfd *);
  99. };
  100. static struct output_fns formats[] =
  101. {
  102. {print_object_filename_bsd,
  103. print_archive_filename_bsd,
  104. print_archive_member_bsd,
  105. print_symbol_filename_bsd,
  106. print_symbol_info_bsd},
  107. {print_object_filename_sysv,
  108. print_archive_filename_sysv,
  109. print_archive_member_sysv,
  110. print_symbol_filename_sysv,
  111. print_symbol_info_sysv},
  112. {print_object_filename_posix,
  113. print_archive_filename_posix,
  114. print_archive_member_posix,
  115. print_symbol_filename_posix,
  116. print_symbol_info_posix}
  117. };
  118. /* Indices in `formats'. */
  119. #define FORMAT_BSD 0
  120. #define FORMAT_SYSV 1
  121. #define FORMAT_POSIX 2
  122. #define FORMAT_DEFAULT FORMAT_BSD
  123. /* The output format to use. */
  124. static struct output_fns *format = &formats[FORMAT_DEFAULT];
  125. /* Command options. */
  126. static int do_demangle = 0; /* Pretty print C++ symbol names. */
  127. static int external_only = 0; /* Print external symbols only. */
  128. static int defined_only = 0; /* Print defined symbols only. */
  129. static int no_sort = 0; /* Don't sort; print syms in order found. */
  130. static int print_debug_syms = 0;/* Print debugger-only symbols too. */
  131. static int print_armap = 0; /* Describe __.SYMDEF data in archive files. */
  132. static int print_size = 0; /* Print size of defined symbols. */
  133. static int reverse_sort = 0; /* Sort in downward(alpha or numeric) order. */
  134. static int sort_numerically = 0;/* Sort in numeric rather than alpha order. */
  135. static int sort_by_size = 0; /* Sort by size of symbol. */
  136. static int undefined_only = 0; /* Print undefined symbols only. */
  137. static int dynamic = 0; /* Print dynamic symbols. */
  138. static int show_version = 0; /* Show the version number. */
  139. static int show_stats = 0; /* Show statistics. */
  140. static int show_synthetic = 0; /* Display synthesized symbols too. */
  141. static int line_numbers = 0; /* Print line numbers for symbols. */
  142. static int allow_special_symbols = 0; /* Allow special symbols. */
  143. static int with_symbol_versions = 0; /* Include symbol version information in the output. */
  144. /* When to print the names of files. Not mutually exclusive in SYSV format. */
  145. static int filename_per_file = 0; /* Once per file, on its own line. */
  146. static int filename_per_symbol = 0; /* Once per symbol, at start of line. */
  147. /* Print formats for printing a symbol value. */
  148. static char value_format_32bit[] = "%08lx";
  149. #if BFD_HOST_64BIT_LONG
  150. static char value_format_64bit[] = "%016lx";
  151. #elif BFD_HOST_64BIT_LONG_LONG
  152. #ifndef __MSVCRT__
  153. static char value_format_64bit[] = "%016llx";
  154. #else
  155. static char value_format_64bit[] = "%016I64x";
  156. #endif
  157. #endif
  158. static int print_width = 0;
  159. static int print_radix = 16;
  160. /* Print formats for printing stab info. */
  161. static char other_format[] = "%02x";
  162. static char desc_format[] = "%04x";
  163. static char *target = NULL;
  164. #if BFD_SUPPORTS_PLUGINS
  165. static const char *plugin_target = "plugin";
  166. #else
  167. static const char *plugin_target = NULL;
  168. #endif
  169. /* Used to cache the line numbers for a BFD. */
  170. static bfd *lineno_cache_bfd;
  171. static bfd *lineno_cache_rel_bfd;
  172. #define OPTION_TARGET 200
  173. #define OPTION_PLUGIN (OPTION_TARGET + 1)
  174. #define OPTION_SIZE_SORT (OPTION_PLUGIN + 1)
  175. static struct option long_options[] =
  176. {
  177. {"debug-syms", no_argument, &print_debug_syms, 1},
  178. {"demangle", optional_argument, 0, 'C'},
  179. {"dynamic", no_argument, &dynamic, 1},
  180. {"extern-only", no_argument, &external_only, 1},
  181. {"format", required_argument, 0, 'f'},
  182. {"help", no_argument, 0, 'h'},
  183. {"line-numbers", no_argument, 0, 'l'},
  184. {"no-cplus", no_argument, &do_demangle, 0}, /* Linux compatibility. */
  185. {"no-demangle", no_argument, &do_demangle, 0},
  186. {"no-sort", no_argument, 0, 'p'},
  187. {"numeric-sort", no_argument, 0, 'n'},
  188. {"plugin", required_argument, 0, OPTION_PLUGIN},
  189. {"portability", no_argument, 0, 'P'},
  190. {"print-armap", no_argument, &print_armap, 1},
  191. {"print-file-name", no_argument, 0, 'o'},
  192. {"print-size", no_argument, 0, 'S'},
  193. {"radix", required_argument, 0, 't'},
  194. {"reverse-sort", no_argument, &reverse_sort, 1},
  195. {"size-sort", no_argument, 0, OPTION_SIZE_SORT},
  196. {"special-syms", no_argument, &allow_special_symbols, 1},
  197. {"stats", no_argument, &show_stats, 1},
  198. {"synthetic", no_argument, &show_synthetic, 1},
  199. {"target", required_argument, 0, OPTION_TARGET},
  200. {"defined-only", no_argument, &defined_only, 1},
  201. {"undefined-only", no_argument, &undefined_only, 1},
  202. {"version", no_argument, &show_version, 1},
  203. {"with-symbol-versions", no_argument, &with_symbol_versions, 1},
  204. {0, no_argument, 0, 0}
  205. };
  206. /* Some error-reporting functions. */
  207. ATTRIBUTE_NORETURN static void
  208. usage (FILE *stream, int status)
  209. {
  210. fprintf (stream, _("Usage: %s [option(s)] [file(s)]\n"), program_name);
  211. fprintf (stream, _(" List symbols in [file(s)] (a.out by default).\n"));
  212. fprintf (stream, _(" The options are:\n\
  213. -a, --debug-syms Display debugger-only symbols\n\
  214. -A, --print-file-name Print name of the input file before every symbol\n\
  215. -B Same as --format=bsd\n\
  216. -C, --demangle[=STYLE] Decode low-level symbol names into user-level names\n\
  217. The STYLE, if specified, can be `auto' (the default),\n\
  218. `gnu', `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
  219. or `gnat'\n\
  220. --no-demangle Do not demangle low-level symbol names\n\
  221. -D, --dynamic Display dynamic symbols instead of normal symbols\n\
  222. --defined-only Display only defined symbols\n\
  223. -e (ignored)\n\
  224. -f, --format=FORMAT Use the output format FORMAT. FORMAT can be `bsd',\n\
  225. `sysv' or `posix'. The default is `bsd'\n\
  226. -g, --extern-only Display only external symbols\n\
  227. -l, --line-numbers Use debugging information to find a filename and\n\
  228. line number for each symbol\n\
  229. -n, --numeric-sort Sort symbols numerically by address\n\
  230. -o Same as -A\n\
  231. -p, --no-sort Do not sort the symbols\n\
  232. -P, --portability Same as --format=posix\n\
  233. -r, --reverse-sort Reverse the sense of the sort\n"));
  234. #if BFD_SUPPORTS_PLUGINS
  235. fprintf (stream, _("\
  236. --plugin NAME Load the specified plugin\n"));
  237. #endif
  238. fprintf (stream, _("\
  239. -S, --print-size Print size of defined symbols\n\
  240. -s, --print-armap Include index for symbols from archive members\n\
  241. --size-sort Sort symbols by size\n\
  242. --special-syms Include special symbols in the output\n\
  243. --synthetic Display synthetic symbols as well\n\
  244. -t, --radix=RADIX Use RADIX for printing symbol values\n\
  245. --target=BFDNAME Specify the target object format as BFDNAME\n\
  246. -u, --undefined-only Display only undefined symbols\n\
  247. --with-symbol-versions Display version strings after symbol names\n\
  248. -X 32_64 (ignored)\n\
  249. @FILE Read options from FILE\n\
  250. -h, --help Display this information\n\
  251. -V, --version Display this program's version number\n\
  252. \n"));
  253. list_supported_targets (program_name, stream);
  254. if (REPORT_BUGS_TO[0] && status == 0)
  255. fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
  256. exit (status);
  257. }
  258. /* Set the radix for the symbol value and size according to RADIX. */
  259. static void
  260. set_print_radix (char *radix)
  261. {
  262. switch (*radix)
  263. {
  264. case 'x':
  265. break;
  266. case 'd':
  267. case 'o':
  268. if (*radix == 'd')
  269. print_radix = 10;
  270. else
  271. print_radix = 8;
  272. value_format_32bit[4] = *radix;
  273. #if BFD_HOST_64BIT_LONG
  274. value_format_64bit[5] = *radix;
  275. #elif BFD_HOST_64BIT_LONG_LONG
  276. #ifndef __MSVCRT__
  277. value_format_64bit[6] = *radix;
  278. #else
  279. value_format_64bit[7] = *radix;
  280. #endif
  281. #endif
  282. other_format[3] = desc_format[3] = *radix;
  283. break;
  284. default:
  285. fatal (_("%s: invalid radix"), radix);
  286. }
  287. }
  288. static void
  289. set_output_format (char *f)
  290. {
  291. int i;
  292. switch (*f)
  293. {
  294. case 'b':
  295. case 'B':
  296. i = FORMAT_BSD;
  297. break;
  298. case 'p':
  299. case 'P':
  300. i = FORMAT_POSIX;
  301. break;
  302. case 's':
  303. case 'S':
  304. i = FORMAT_SYSV;
  305. break;
  306. default:
  307. fatal (_("%s: invalid output format"), f);
  308. }
  309. format = &formats[i];
  310. }
  311. static const char *
  312. get_elf_symbol_type (unsigned int type)
  313. {
  314. static char *bufp;
  315. int n;
  316. switch (type)
  317. {
  318. case STT_NOTYPE: return "NOTYPE";
  319. case STT_OBJECT: return "OBJECT";
  320. case STT_FUNC: return "FUNC";
  321. case STT_SECTION: return "SECTION";
  322. case STT_FILE: return "FILE";
  323. case STT_COMMON: return "COMMON";
  324. case STT_TLS: return "TLS";
  325. }
  326. free (bufp);
  327. if (type >= STT_LOPROC && type <= STT_HIPROC)
  328. n = asprintf (&bufp, _("<processor specific>: %d"), type);
  329. else if (type >= STT_LOOS && type <= STT_HIOS)
  330. n = asprintf (&bufp, _("<OS specific>: %d"), type);
  331. else
  332. n = asprintf (&bufp, _("<unknown>: %d"), type);
  333. if (n < 0)
  334. fatal ("%s", xstrerror (errno));
  335. return bufp;
  336. }
  337. static const char *
  338. get_coff_symbol_type (const struct internal_syment *sym)
  339. {
  340. static char *bufp;
  341. int n;
  342. switch (sym->n_sclass)
  343. {
  344. case C_BLOCK: return "Block";
  345. case C_FILE: return "File";
  346. case C_LINE: return "Line";
  347. }
  348. if (!sym->n_type)
  349. return "None";
  350. switch (DTYPE(sym->n_type))
  351. {
  352. case DT_FCN: return "Function";
  353. case DT_PTR: return "Pointer";
  354. case DT_ARY: return "Array";
  355. }
  356. free (bufp);
  357. n = asprintf (&bufp, _("<unknown>: %d/%d"), sym->n_sclass, sym->n_type);
  358. if (n < 0)
  359. fatal ("%s", xstrerror (errno));
  360. return bufp;
  361. }
  362. /* Print symbol name NAME, read from ABFD, with printf format FORM,
  363. demangling it if requested. */
  364. static void
  365. print_symname (const char *form, const char *name, bfd *abfd)
  366. {
  367. if (do_demangle && *name)
  368. {
  369. char *res = bfd_demangle (abfd, name, DMGL_ANSI | DMGL_PARAMS);
  370. if (res != NULL)
  371. {
  372. printf (form, res);
  373. free (res);
  374. return;
  375. }
  376. }
  377. printf (form, name);
  378. }
  379. static void
  380. print_symdef_entry (bfd *abfd)
  381. {
  382. symindex idx = BFD_NO_MORE_SYMBOLS;
  383. carsym *thesym;
  384. bfd_boolean everprinted = FALSE;
  385. for (idx = bfd_get_next_mapent (abfd, idx, &thesym);
  386. idx != BFD_NO_MORE_SYMBOLS;
  387. idx = bfd_get_next_mapent (abfd, idx, &thesym))
  388. {
  389. bfd *elt;
  390. if (!everprinted)
  391. {
  392. printf (_("\nArchive index:\n"));
  393. everprinted = TRUE;
  394. }
  395. elt = bfd_get_elt_at_index (abfd, idx);
  396. if (elt == NULL)
  397. bfd_fatal ("bfd_get_elt_at_index");
  398. if (thesym->name != (char *) NULL)
  399. {
  400. print_symname ("%s", thesym->name, abfd);
  401. printf (" in %s\n", bfd_get_filename (elt));
  402. }
  403. }
  404. }
  405. /* Choose which symbol entries to print;
  406. compact them downward to get rid of the rest.
  407. Return the number of symbols to be printed. */
  408. static long
  409. filter_symbols (bfd *abfd, bfd_boolean is_dynamic, void *minisyms,
  410. long symcount, unsigned int size)
  411. {
  412. bfd_byte *from, *fromend, *to;
  413. asymbol *store;
  414. store = bfd_make_empty_symbol (abfd);
  415. if (store == NULL)
  416. bfd_fatal (bfd_get_filename (abfd));
  417. from = (bfd_byte *) minisyms;
  418. fromend = from + symcount * size;
  419. to = (bfd_byte *) minisyms;
  420. for (; from < fromend; from += size)
  421. {
  422. int keep = 0;
  423. asymbol *sym;
  424. PROGRESS (1);
  425. sym = bfd_minisymbol_to_symbol (abfd, is_dynamic, (const void *) from, store);
  426. if (sym == NULL)
  427. bfd_fatal (bfd_get_filename (abfd));
  428. if (strcmp (sym->name, "__gnu_lto_slim") == 0)
  429. non_fatal (_("%s: plugin needed to handle lto object"),
  430. bfd_get_filename (abfd));
  431. if (undefined_only)
  432. keep = bfd_is_und_section (sym->section);
  433. else if (external_only)
  434. /* PR binutls/12753: Unique symbols are global too. */
  435. keep = ((sym->flags & (BSF_GLOBAL
  436. | BSF_WEAK
  437. | BSF_GNU_UNIQUE)) != 0
  438. || bfd_is_und_section (sym->section)
  439. || bfd_is_com_section (sym->section));
  440. else
  441. keep = 1;
  442. if (keep
  443. && ! print_debug_syms
  444. && (sym->flags & BSF_DEBUGGING) != 0)
  445. keep = 0;
  446. if (keep
  447. && sort_by_size
  448. && (bfd_is_abs_section (sym->section)
  449. || bfd_is_und_section (sym->section)))
  450. keep = 0;
  451. if (keep
  452. && defined_only)
  453. {
  454. if (bfd_is_und_section (sym->section))
  455. keep = 0;
  456. }
  457. if (keep
  458. && bfd_is_target_special_symbol (abfd, sym)
  459. && ! allow_special_symbols)
  460. keep = 0;
  461. if (keep)
  462. {
  463. if (to != from)
  464. memcpy (to, from, size);
  465. to += size;
  466. }
  467. }
  468. return (to - (bfd_byte *) minisyms) / size;
  469. }
  470. /* These globals are used to pass information into the sorting
  471. routines. */
  472. static bfd *sort_bfd;
  473. static bfd_boolean sort_dynamic;
  474. static asymbol *sort_x;
  475. static asymbol *sort_y;
  476. /* Symbol-sorting predicates */
  477. #define valueof(x) ((x)->section->vma + (x)->value)
  478. /* Numeric sorts. Undefined symbols are always considered "less than"
  479. defined symbols with zero values. Common symbols are not treated
  480. specially -- i.e., their sizes are used as their "values". */
  481. static int
  482. non_numeric_forward (const void *P_x, const void *P_y)
  483. {
  484. asymbol *x, *y;
  485. const char *xn, *yn;
  486. x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
  487. y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
  488. if (x == NULL || y == NULL)
  489. bfd_fatal (bfd_get_filename (sort_bfd));
  490. xn = bfd_asymbol_name (x);
  491. yn = bfd_asymbol_name (y);
  492. if (yn == NULL)
  493. return xn != NULL;
  494. if (xn == NULL)
  495. return -1;
  496. #ifdef HAVE_STRCOLL
  497. /* Solaris 2.5 has a bug in strcoll.
  498. strcoll returns invalid values when confronted with empty strings. */
  499. if (*yn == '\0')
  500. return *xn != '\0';
  501. if (*xn == '\0')
  502. return -1;
  503. return strcoll (xn, yn);
  504. #else
  505. return strcmp (xn, yn);
  506. #endif
  507. }
  508. static int
  509. non_numeric_reverse (const void *x, const void *y)
  510. {
  511. return - non_numeric_forward (x, y);
  512. }
  513. static int
  514. numeric_forward (const void *P_x, const void *P_y)
  515. {
  516. asymbol *x, *y;
  517. asection *xs, *ys;
  518. x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
  519. y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
  520. if (x == NULL || y == NULL)
  521. bfd_fatal (bfd_get_filename (sort_bfd));
  522. xs = bfd_get_section (x);
  523. ys = bfd_get_section (y);
  524. if (bfd_is_und_section (xs))
  525. {
  526. if (! bfd_is_und_section (ys))
  527. return -1;
  528. }
  529. else if (bfd_is_und_section (ys))
  530. return 1;
  531. else if (valueof (x) != valueof (y))
  532. return valueof (x) < valueof (y) ? -1 : 1;
  533. return non_numeric_forward (P_x, P_y);
  534. }
  535. static int
  536. numeric_reverse (const void *x, const void *y)
  537. {
  538. return - numeric_forward (x, y);
  539. }
  540. static int (*(sorters[2][2])) (const void *, const void *) =
  541. {
  542. { non_numeric_forward, non_numeric_reverse },
  543. { numeric_forward, numeric_reverse }
  544. };
  545. /* This sort routine is used by sort_symbols_by_size. It is similar
  546. to numeric_forward, but when symbols have the same value it sorts
  547. by section VMA. This simplifies the sort_symbols_by_size code
  548. which handles symbols at the end of sections. Also, this routine
  549. tries to sort file names before other symbols with the same value.
  550. That will make the file name have a zero size, which will make
  551. sort_symbols_by_size choose the non file name symbol, leading to
  552. more meaningful output. For similar reasons, this code sorts
  553. gnu_compiled_* and gcc2_compiled before other symbols with the same
  554. value. */
  555. static int
  556. size_forward1 (const void *P_x, const void *P_y)
  557. {
  558. asymbol *x, *y;
  559. asection *xs, *ys;
  560. const char *xn, *yn;
  561. size_t xnl, ynl;
  562. int xf, yf;
  563. x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
  564. y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
  565. if (x == NULL || y == NULL)
  566. bfd_fatal (bfd_get_filename (sort_bfd));
  567. xs = bfd_get_section (x);
  568. ys = bfd_get_section (y);
  569. if (bfd_is_und_section (xs))
  570. abort ();
  571. if (bfd_is_und_section (ys))
  572. abort ();
  573. if (valueof (x) != valueof (y))
  574. return valueof (x) < valueof (y) ? -1 : 1;
  575. if (xs->vma != ys->vma)
  576. return xs->vma < ys->vma ? -1 : 1;
  577. xn = bfd_asymbol_name (x);
  578. yn = bfd_asymbol_name (y);
  579. xnl = strlen (xn);
  580. ynl = strlen (yn);
  581. /* The symbols gnu_compiled and gcc2_compiled convey even less
  582. information than the file name, so sort them out first. */
  583. xf = (strstr (xn, "gnu_compiled") != NULL
  584. || strstr (xn, "gcc2_compiled") != NULL);
  585. yf = (strstr (yn, "gnu_compiled") != NULL
  586. || strstr (yn, "gcc2_compiled") != NULL);
  587. if (xf && ! yf)
  588. return -1;
  589. if (! xf && yf)
  590. return 1;
  591. /* We use a heuristic for the file name. It may not work on non
  592. Unix systems, but it doesn't really matter; the only difference
  593. is precisely which symbol names get printed. */
  594. #define file_symbol(s, sn, snl) \
  595. (((s)->flags & BSF_FILE) != 0 \
  596. || ((snl) > 2 \
  597. && (sn)[(snl) - 2] == '.' \
  598. && ((sn)[(snl) - 1] == 'o' \
  599. || (sn)[(snl) - 1] == 'a')))
  600. xf = file_symbol (x, xn, xnl);
  601. yf = file_symbol (y, yn, ynl);
  602. if (xf && ! yf)
  603. return -1;
  604. if (! xf && yf)
  605. return 1;
  606. return non_numeric_forward (P_x, P_y);
  607. }
  608. /* This sort routine is used by sort_symbols_by_size. It is sorting
  609. an array of size_sym structures into size order. */
  610. static int
  611. size_forward2 (const void *P_x, const void *P_y)
  612. {
  613. const struct size_sym *x = (const struct size_sym *) P_x;
  614. const struct size_sym *y = (const struct size_sym *) P_y;
  615. if (x->size < y->size)
  616. return reverse_sort ? 1 : -1;
  617. else if (x->size > y->size)
  618. return reverse_sort ? -1 : 1;
  619. else
  620. return sorters[0][reverse_sort] (x->minisym, y->minisym);
  621. }
  622. /* Sort the symbols by size. ELF provides a size but for other formats
  623. we have to make a guess by assuming that the difference between the
  624. address of a symbol and the address of the next higher symbol is the
  625. size. */
  626. static long
  627. sort_symbols_by_size (bfd *abfd, bfd_boolean is_dynamic, void *minisyms,
  628. long symcount, unsigned int size,
  629. struct size_sym **symsizesp)
  630. {
  631. struct size_sym *symsizes;
  632. bfd_byte *from, *fromend;
  633. asymbol *sym = NULL;
  634. asymbol *store_sym, *store_next;
  635. qsort (minisyms, symcount, size, size_forward1);
  636. /* We are going to return a special set of symbols and sizes to
  637. print. */
  638. symsizes = (struct size_sym *) xmalloc (symcount * sizeof (struct size_sym));
  639. *symsizesp = symsizes;
  640. /* Note that filter_symbols has already removed all absolute and
  641. undefined symbols. Here we remove all symbols whose size winds
  642. up as zero. */
  643. from = (bfd_byte *) minisyms;
  644. fromend = from + symcount * size;
  645. store_sym = sort_x;
  646. store_next = sort_y;
  647. if (from < fromend)
  648. {
  649. sym = bfd_minisymbol_to_symbol (abfd, is_dynamic, (const void *) from,
  650. store_sym);
  651. if (sym == NULL)
  652. bfd_fatal (bfd_get_filename (abfd));
  653. }
  654. for (; from < fromend; from += size)
  655. {
  656. asymbol *next;
  657. asection *sec;
  658. bfd_vma sz;
  659. asymbol *temp;
  660. int synthetic = (sym->flags & BSF_SYNTHETIC);
  661. if (from + size < fromend)
  662. {
  663. next = bfd_minisymbol_to_symbol (abfd,
  664. is_dynamic,
  665. (const void *) (from + size),
  666. store_next);
  667. if (next == NULL)
  668. bfd_fatal (bfd_get_filename (abfd));
  669. }
  670. else
  671. next = NULL;
  672. sec = bfd_get_section (sym);
  673. /* Synthetic symbols don't have a full type set of data available, thus
  674. we can't rely on that information for the symbol size. */
  675. if (!synthetic && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
  676. sz = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
  677. else if (!synthetic && bfd_is_com_section (sec))
  678. sz = sym->value;
  679. else
  680. {
  681. if (from + size < fromend
  682. && sec == bfd_get_section (next))
  683. sz = valueof (next) - valueof (sym);
  684. else
  685. sz = (bfd_get_section_vma (abfd, sec)
  686. + bfd_section_size (abfd, sec)
  687. - valueof (sym));
  688. }
  689. if (sz != 0)
  690. {
  691. symsizes->minisym = (const void *) from;
  692. symsizes->size = sz;
  693. ++symsizes;
  694. }
  695. sym = next;
  696. temp = store_sym;
  697. store_sym = store_next;
  698. store_next = temp;
  699. }
  700. symcount = symsizes - *symsizesp;
  701. /* We must now sort again by size. */
  702. qsort ((void *) *symsizesp, symcount, sizeof (struct size_sym), size_forward2);
  703. return symcount;
  704. }
  705. /* This function is used to get the relocs for a particular section.
  706. It is called via bfd_map_over_sections. */
  707. static void
  708. get_relocs (bfd *abfd, asection *sec, void *dataarg)
  709. {
  710. struct get_relocs_info *data = (struct get_relocs_info *) dataarg;
  711. *data->secs = sec;
  712. if ((sec->flags & SEC_RELOC) == 0)
  713. {
  714. *data->relocs = NULL;
  715. *data->relcount = 0;
  716. }
  717. else
  718. {
  719. long relsize;
  720. relsize = bfd_get_reloc_upper_bound (abfd, sec);
  721. if (relsize < 0)
  722. bfd_fatal (bfd_get_filename (abfd));
  723. *data->relocs = (arelent **) xmalloc (relsize);
  724. *data->relcount = bfd_canonicalize_reloc (abfd, sec, *data->relocs,
  725. data->syms);
  726. if (*data->relcount < 0)
  727. bfd_fatal (bfd_get_filename (abfd));
  728. }
  729. ++data->secs;
  730. ++data->relocs;
  731. ++data->relcount;
  732. }
  733. /* Print a single symbol. */
  734. static void
  735. print_symbol (bfd * abfd,
  736. asymbol * sym,
  737. bfd_vma ssize,
  738. bfd * archive_bfd)
  739. {
  740. symbol_info syminfo;
  741. struct extended_symbol_info info;
  742. PROGRESS (1);
  743. format->print_symbol_filename (archive_bfd, abfd);
  744. bfd_get_symbol_info (abfd, sym, &syminfo);
  745. info.sinfo = &syminfo;
  746. info.ssize = ssize;
  747. /* Synthetic symbols do not have a full symbol type set of data available. */
  748. if ((sym->flags & BSF_SYNTHETIC) != 0)
  749. {
  750. info.elfinfo = NULL;
  751. info.coffinfo = NULL;
  752. }
  753. else
  754. {
  755. info.elfinfo = elf_symbol_from (abfd, sym);
  756. info.coffinfo = coff_symbol_from (sym);
  757. }
  758. format->print_symbol_info (&info, abfd);
  759. if (with_symbol_versions)
  760. {
  761. const char * version_string = NULL;
  762. bfd_boolean hidden = FALSE;
  763. if ((sym->flags & BSF_SYNTHETIC) == 0)
  764. version_string = bfd_get_symbol_version_string (abfd, sym, &hidden);
  765. if (bfd_is_und_section (bfd_get_section (sym)))
  766. hidden = TRUE;
  767. if (version_string && *version_string != '\0')
  768. printf (hidden ? "@%s" : "@@%s", version_string);
  769. }
  770. if (line_numbers)
  771. {
  772. static asymbol **syms;
  773. static long symcount;
  774. const char *filename, *functionname;
  775. unsigned int lineno;
  776. /* We need to get the canonical symbols in order to call
  777. bfd_find_nearest_line. This is inefficient, but, then, you
  778. don't have to use --line-numbers. */
  779. if (abfd != lineno_cache_bfd && syms != NULL)
  780. {
  781. free (syms);
  782. syms = NULL;
  783. }
  784. if (syms == NULL)
  785. {
  786. long symsize;
  787. symsize = bfd_get_symtab_upper_bound (abfd);
  788. if (symsize < 0)
  789. bfd_fatal (bfd_get_filename (abfd));
  790. syms = (asymbol **) xmalloc (symsize);
  791. symcount = bfd_canonicalize_symtab (abfd, syms);
  792. if (symcount < 0)
  793. bfd_fatal (bfd_get_filename (abfd));
  794. lineno_cache_bfd = abfd;
  795. }
  796. if (bfd_is_und_section (bfd_get_section (sym)))
  797. {
  798. static asection **secs;
  799. static arelent ***relocs;
  800. static long *relcount;
  801. static unsigned int seccount;
  802. unsigned int i;
  803. const char *symname;
  804. /* For an undefined symbol, we try to find a reloc for the
  805. symbol, and print the line number of the reloc. */
  806. if (abfd != lineno_cache_rel_bfd && relocs != NULL)
  807. {
  808. for (i = 0; i < seccount; i++)
  809. if (relocs[i] != NULL)
  810. free (relocs[i]);
  811. free (secs);
  812. free (relocs);
  813. free (relcount);
  814. secs = NULL;
  815. relocs = NULL;
  816. relcount = NULL;
  817. }
  818. if (relocs == NULL)
  819. {
  820. struct get_relocs_info rinfo;
  821. seccount = bfd_count_sections (abfd);
  822. secs = (asection **) xmalloc (seccount * sizeof *secs);
  823. relocs = (arelent ***) xmalloc (seccount * sizeof *relocs);
  824. relcount = (long *) xmalloc (seccount * sizeof *relcount);
  825. rinfo.secs = secs;
  826. rinfo.relocs = relocs;
  827. rinfo.relcount = relcount;
  828. rinfo.syms = syms;
  829. bfd_map_over_sections (abfd, get_relocs, (void *) &rinfo);
  830. lineno_cache_rel_bfd = abfd;
  831. }
  832. symname = bfd_asymbol_name (sym);
  833. for (i = 0; i < seccount; i++)
  834. {
  835. long j;
  836. for (j = 0; j < relcount[i]; j++)
  837. {
  838. arelent *r;
  839. r = relocs[i][j];
  840. if (r->sym_ptr_ptr != NULL
  841. && (*r->sym_ptr_ptr)->section == sym->section
  842. && (*r->sym_ptr_ptr)->value == sym->value
  843. && strcmp (symname,
  844. bfd_asymbol_name (*r->sym_ptr_ptr)) == 0
  845. && bfd_find_nearest_line (abfd, secs[i], syms,
  846. r->address, &filename,
  847. &functionname, &lineno)
  848. && filename != NULL)
  849. {
  850. /* We only print the first one we find. */
  851. printf ("\t%s:%u", filename, lineno);
  852. i = seccount;
  853. break;
  854. }
  855. }
  856. }
  857. }
  858. else if (bfd_get_section (sym)->owner == abfd)
  859. {
  860. if ((bfd_find_line (abfd, syms, sym, &filename, &lineno)
  861. || bfd_find_nearest_line (abfd, bfd_get_section (sym),
  862. syms, sym->value, &filename,
  863. &functionname, &lineno))
  864. && filename != NULL
  865. && lineno != 0)
  866. printf ("\t%s:%u", filename, lineno);
  867. }
  868. }
  869. putchar ('\n');
  870. }
  871. /* Print the symbols when sorting by size. */
  872. static void
  873. print_size_symbols (bfd * abfd,
  874. bfd_boolean is_dynamic,
  875. struct size_sym * symsizes,
  876. long symcount,
  877. bfd * archive_bfd)
  878. {
  879. asymbol *store;
  880. struct size_sym *from;
  881. struct size_sym *fromend;
  882. store = bfd_make_empty_symbol (abfd);
  883. if (store == NULL)
  884. bfd_fatal (bfd_get_filename (abfd));
  885. from = symsizes;
  886. fromend = from + symcount;
  887. for (; from < fromend; from++)
  888. {
  889. asymbol *sym;
  890. sym = bfd_minisymbol_to_symbol (abfd, is_dynamic, from->minisym, store);
  891. if (sym == NULL)
  892. bfd_fatal (bfd_get_filename (abfd));
  893. print_symbol (abfd, sym, from->size, archive_bfd);
  894. }
  895. }
  896. /* Print the symbols of ABFD that are held in MINISYMS.
  897. If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD.
  898. SYMCOUNT is the number of symbols in MINISYMS.
  899. SIZE is the size of a symbol in MINISYMS. */
  900. static void
  901. print_symbols (bfd * abfd,
  902. bfd_boolean is_dynamic,
  903. void * minisyms,
  904. long symcount,
  905. unsigned int size,
  906. bfd * archive_bfd)
  907. {
  908. asymbol *store;
  909. bfd_byte *from;
  910. bfd_byte *fromend;
  911. store = bfd_make_empty_symbol (abfd);
  912. if (store == NULL)
  913. bfd_fatal (bfd_get_filename (abfd));
  914. from = (bfd_byte *) minisyms;
  915. fromend = from + symcount * size;
  916. for (; from < fromend; from += size)
  917. {
  918. asymbol *sym;
  919. sym = bfd_minisymbol_to_symbol (abfd, is_dynamic, from, store);
  920. if (sym == NULL)
  921. bfd_fatal (bfd_get_filename (abfd));
  922. print_symbol (abfd, sym, (bfd_vma) 0, archive_bfd);
  923. }
  924. }
  925. /* If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD. */
  926. static void
  927. display_rel_file (bfd *abfd, bfd *archive_bfd)
  928. {
  929. long symcount;
  930. void *minisyms;
  931. unsigned int size;
  932. struct size_sym *symsizes;
  933. if (! dynamic)
  934. {
  935. if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
  936. {
  937. non_fatal (_("%s: no symbols"), bfd_get_filename (abfd));
  938. return;
  939. }
  940. }
  941. symcount = bfd_read_minisymbols (abfd, dynamic, &minisyms, &size);
  942. if (symcount < 0)
  943. {
  944. if (dynamic && bfd_get_error () == bfd_error_no_symbols)
  945. {
  946. non_fatal (_("%s: no symbols"), bfd_get_filename (abfd));
  947. return;
  948. }
  949. bfd_fatal (bfd_get_filename (abfd));
  950. }
  951. if (symcount == 0)
  952. {
  953. non_fatal (_("%s: no symbols"), bfd_get_filename (abfd));
  954. return;
  955. }
  956. if (show_synthetic && size == sizeof (asymbol *))
  957. {
  958. asymbol *synthsyms;
  959. asymbol **static_syms = NULL;
  960. asymbol **dyn_syms = NULL;
  961. long static_count = 0;
  962. long dyn_count = 0;
  963. long synth_count;
  964. if (dynamic)
  965. {
  966. dyn_count = symcount;
  967. dyn_syms = (asymbol **) minisyms;
  968. }
  969. else
  970. {
  971. long storage = bfd_get_dynamic_symtab_upper_bound (abfd);
  972. static_count = symcount;
  973. static_syms = (asymbol **) minisyms;
  974. if (storage > 0)
  975. {
  976. dyn_syms = (asymbol **) xmalloc (storage);
  977. dyn_count = bfd_canonicalize_dynamic_symtab (abfd, dyn_syms);
  978. if (dyn_count < 0)
  979. bfd_fatal (bfd_get_filename (abfd));
  980. }
  981. }
  982. synth_count = bfd_get_synthetic_symtab (abfd, static_count, static_syms,
  983. dyn_count, dyn_syms, &synthsyms);
  984. if (synth_count > 0)
  985. {
  986. asymbol **symp;
  987. void *new_mini;
  988. long i;
  989. new_mini = xmalloc ((symcount + synth_count + 1) * sizeof (*symp));
  990. symp = (asymbol **) new_mini;
  991. memcpy (symp, minisyms, symcount * sizeof (*symp));
  992. symp += symcount;
  993. for (i = 0; i < synth_count; i++)
  994. *symp++ = synthsyms + i;
  995. *symp = 0;
  996. minisyms = new_mini;
  997. symcount += synth_count;
  998. }
  999. }
  1000. /* Discard the symbols we don't want to print.
  1001. It's OK to do this in place; we'll free the storage anyway
  1002. (after printing). */
  1003. symcount = filter_symbols (abfd, dynamic, minisyms, symcount, size);
  1004. symsizes = NULL;
  1005. if (! no_sort)
  1006. {
  1007. sort_bfd = abfd;
  1008. sort_dynamic = dynamic;
  1009. sort_x = bfd_make_empty_symbol (abfd);
  1010. sort_y = bfd_make_empty_symbol (abfd);
  1011. if (sort_x == NULL || sort_y == NULL)
  1012. bfd_fatal (bfd_get_filename (abfd));
  1013. if (! sort_by_size)
  1014. qsort (minisyms, symcount, size,
  1015. sorters[sort_numerically][reverse_sort]);
  1016. else
  1017. symcount = sort_symbols_by_size (abfd, dynamic, minisyms, symcount,
  1018. size, &symsizes);
  1019. }
  1020. if (! sort_by_size)
  1021. print_symbols (abfd, dynamic, minisyms, symcount, size, archive_bfd);
  1022. else
  1023. print_size_symbols (abfd, dynamic, symsizes, symcount, archive_bfd);
  1024. free (minisyms);
  1025. free (symsizes);
  1026. }
  1027. static void
  1028. set_print_width (bfd *file)
  1029. {
  1030. print_width = bfd_get_arch_size (file);
  1031. if (print_width == -1)
  1032. {
  1033. /* PR binutils/4292
  1034. Guess the target's bitsize based on its name.
  1035. We assume here than any 64-bit format will include
  1036. "64" somewhere in its name. The only known exception
  1037. is the MMO object file format. */
  1038. if (strstr (bfd_get_target (file), "64") != NULL
  1039. || strcmp (bfd_get_target (file), "mmo") == 0)
  1040. print_width = 64;
  1041. else
  1042. print_width = 32;
  1043. }
  1044. }
  1045. static void
  1046. display_archive (bfd *file)
  1047. {
  1048. bfd *arfile = NULL;
  1049. bfd *last_arfile = NULL;
  1050. char **matching;
  1051. format->print_archive_filename (bfd_get_filename (file));
  1052. if (print_armap)
  1053. print_symdef_entry (file);
  1054. for (;;)
  1055. {
  1056. PROGRESS (1);
  1057. arfile = bfd_openr_next_archived_file (file, arfile);
  1058. if (arfile == NULL)
  1059. {
  1060. if (bfd_get_error () != bfd_error_no_more_archived_files)
  1061. bfd_fatal (bfd_get_filename (file));
  1062. break;
  1063. }
  1064. if (bfd_check_format_matches (arfile, bfd_object, &matching))
  1065. {
  1066. set_print_width (arfile);
  1067. format->print_archive_member (bfd_get_filename (file),
  1068. bfd_get_filename (arfile));
  1069. display_rel_file (arfile, file);
  1070. }
  1071. else
  1072. {
  1073. bfd_nonfatal (bfd_get_filename (arfile));
  1074. if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
  1075. {
  1076. list_matching_formats (matching);
  1077. free (matching);
  1078. }
  1079. }
  1080. if (last_arfile != NULL)
  1081. {
  1082. bfd_close (last_arfile);
  1083. lineno_cache_bfd = NULL;
  1084. lineno_cache_rel_bfd = NULL;
  1085. if (arfile == last_arfile)
  1086. return;
  1087. }
  1088. last_arfile = arfile;
  1089. }
  1090. if (last_arfile != NULL)
  1091. {
  1092. bfd_close (last_arfile);
  1093. lineno_cache_bfd = NULL;
  1094. lineno_cache_rel_bfd = NULL;
  1095. }
  1096. }
  1097. static bfd_boolean
  1098. display_file (char *filename)
  1099. {
  1100. bfd_boolean retval = TRUE;
  1101. bfd *file;
  1102. char **matching;
  1103. if (get_file_size (filename) < 1)
  1104. return FALSE;
  1105. file = bfd_openr (filename, target ? target : plugin_target);
  1106. if (file == NULL)
  1107. {
  1108. bfd_nonfatal (filename);
  1109. return FALSE;
  1110. }
  1111. /* If printing line numbers, decompress the debug sections. */
  1112. if (line_numbers)
  1113. file->flags |= BFD_DECOMPRESS;
  1114. if (bfd_check_format (file, bfd_archive))
  1115. {
  1116. display_archive (file);
  1117. }
  1118. else if (bfd_check_format_matches (file, bfd_object, &matching))
  1119. {
  1120. set_print_width (file);
  1121. format->print_object_filename (filename);
  1122. display_rel_file (file, NULL);
  1123. }
  1124. else
  1125. {
  1126. bfd_nonfatal (filename);
  1127. if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
  1128. {
  1129. list_matching_formats (matching);
  1130. free (matching);
  1131. }
  1132. retval = FALSE;
  1133. }
  1134. if (!bfd_close (file))
  1135. bfd_fatal (filename);
  1136. lineno_cache_bfd = NULL;
  1137. lineno_cache_rel_bfd = NULL;
  1138. return retval;
  1139. }
  1140. /* The following 3 groups of functions are called unconditionally,
  1141. once at the start of processing each file of the appropriate type.
  1142. They should check `filename_per_file' and `filename_per_symbol',
  1143. as appropriate for their output format, to determine whether to
  1144. print anything. */
  1145. /* Print the name of an object file given on the command line. */
  1146. static void
  1147. print_object_filename_bsd (char *filename)
  1148. {
  1149. if (filename_per_file && !filename_per_symbol)
  1150. printf ("\n%s:\n", filename);
  1151. }
  1152. static void
  1153. print_object_filename_sysv (char *filename)
  1154. {
  1155. if (undefined_only)
  1156. printf (_("\n\nUndefined symbols from %s:\n\n"), filename);
  1157. else
  1158. printf (_("\n\nSymbols from %s:\n\n"), filename);
  1159. if (print_width == 32)
  1160. printf (_("\
  1161. Name Value Class Type Size Line Section\n\n"));
  1162. else
  1163. printf (_("\
  1164. Name Value Class Type Size Line Section\n\n"));
  1165. }
  1166. static void
  1167. print_object_filename_posix (char *filename)
  1168. {
  1169. if (filename_per_file && !filename_per_symbol)
  1170. printf ("%s:\n", filename);
  1171. }
  1172. /* Print the name of an archive file given on the command line. */
  1173. static void
  1174. print_archive_filename_bsd (char *filename)
  1175. {
  1176. if (filename_per_file)
  1177. printf ("\n%s:\n", filename);
  1178. }
  1179. static void
  1180. print_archive_filename_sysv (char *filename ATTRIBUTE_UNUSED)
  1181. {
  1182. }
  1183. static void
  1184. print_archive_filename_posix (char *filename ATTRIBUTE_UNUSED)
  1185. {
  1186. }
  1187. /* Print the name of an archive member file. */
  1188. static void
  1189. print_archive_member_bsd (char *archive ATTRIBUTE_UNUSED,
  1190. const char *filename)
  1191. {
  1192. if (!filename_per_symbol)
  1193. printf ("\n%s:\n", filename);
  1194. }
  1195. static void
  1196. print_archive_member_sysv (char *archive, const char *filename)
  1197. {
  1198. if (undefined_only)
  1199. printf (_("\n\nUndefined symbols from %s[%s]:\n\n"), archive, filename);
  1200. else
  1201. printf (_("\n\nSymbols from %s[%s]:\n\n"), archive, filename);
  1202. if (print_width == 32)
  1203. printf (_("\
  1204. Name Value Class Type Size Line Section\n\n"));
  1205. else
  1206. printf (_("\
  1207. Name Value Class Type Size Line Section\n\n"));
  1208. }
  1209. static void
  1210. print_archive_member_posix (char *archive, const char *filename)
  1211. {
  1212. if (!filename_per_symbol)
  1213. printf ("%s[%s]:\n", archive, filename);
  1214. }
  1215. /* Print the name of the file (and archive, if there is one)
  1216. containing a symbol. */
  1217. static void
  1218. print_symbol_filename_bsd (bfd *archive_bfd, bfd *abfd)
  1219. {
  1220. if (filename_per_symbol)
  1221. {
  1222. if (archive_bfd)
  1223. printf ("%s:", bfd_get_filename (archive_bfd));
  1224. printf ("%s:", bfd_get_filename (abfd));
  1225. }
  1226. }
  1227. static void
  1228. print_symbol_filename_sysv (bfd *archive_bfd, bfd *abfd)
  1229. {
  1230. if (filename_per_symbol)
  1231. {
  1232. if (archive_bfd)
  1233. printf ("%s:", bfd_get_filename (archive_bfd));
  1234. printf ("%s:", bfd_get_filename (abfd));
  1235. }
  1236. }
  1237. static void
  1238. print_symbol_filename_posix (bfd *archive_bfd, bfd *abfd)
  1239. {
  1240. if (filename_per_symbol)
  1241. {
  1242. if (archive_bfd)
  1243. printf ("%s[%s]: ", bfd_get_filename (archive_bfd),
  1244. bfd_get_filename (abfd));
  1245. else
  1246. printf ("%s: ", bfd_get_filename (abfd));
  1247. }
  1248. }
  1249. /* Print a symbol value. */
  1250. static void
  1251. print_value (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma val)
  1252. {
  1253. switch (print_width)
  1254. {
  1255. case 32:
  1256. printf (value_format_32bit, (unsigned long) val);
  1257. break;
  1258. case 64:
  1259. #if BFD_HOST_64BIT_LONG || BFD_HOST_64BIT_LONG_LONG
  1260. printf (value_format_64bit, val);
  1261. #else
  1262. /* We have a 64 bit value to print, but the host is only 32 bit. */
  1263. if (print_radix == 16)
  1264. bfd_fprintf_vma (abfd, stdout, val);
  1265. else
  1266. {
  1267. char buf[30];
  1268. char *s;
  1269. s = buf + sizeof buf;
  1270. *--s = '\0';
  1271. while (val > 0)
  1272. {
  1273. *--s = (val % print_radix) + '0';
  1274. val /= print_radix;
  1275. }
  1276. while ((buf + sizeof buf - 1) - s < 16)
  1277. *--s = '0';
  1278. printf ("%s", s);
  1279. }
  1280. #endif
  1281. break;
  1282. default:
  1283. fatal (_("Print width has not been initialized (%d)"), print_width);
  1284. break;
  1285. }
  1286. }
  1287. /* Print a line of information about a symbol. */
  1288. static void
  1289. print_symbol_info_bsd (struct extended_symbol_info *info, bfd *abfd)
  1290. {
  1291. if (bfd_is_undefined_symclass (SYM_TYPE (info)))
  1292. {
  1293. if (print_width == 64)
  1294. printf (" ");
  1295. printf (" ");
  1296. }
  1297. else
  1298. {
  1299. /* Normally we print the value of the symbol. If we are printing the
  1300. size or sorting by size then we print its size, except for the
  1301. (weird) special case where both flags are defined, in which case we
  1302. print both values. This conforms to documented behaviour. */
  1303. if (sort_by_size && !print_size)
  1304. print_value (abfd, SYM_SIZE (info));
  1305. else
  1306. print_value (abfd, SYM_VALUE (info));
  1307. if (print_size && SYM_SIZE (info))
  1308. {
  1309. printf (" ");
  1310. print_value (abfd, SYM_SIZE (info));
  1311. }
  1312. }
  1313. printf (" %c", SYM_TYPE (info));
  1314. if (SYM_TYPE (info) == '-')
  1315. {
  1316. /* A stab. */
  1317. printf (" ");
  1318. printf (other_format, SYM_STAB_OTHER (info));
  1319. printf (" ");
  1320. printf (desc_format, SYM_STAB_DESC (info));
  1321. printf (" %5s", SYM_STAB_NAME (info));
  1322. }
  1323. print_symname (" %s", SYM_NAME (info), abfd);
  1324. }
  1325. static void
  1326. print_symbol_info_sysv (struct extended_symbol_info *info, bfd *abfd)
  1327. {
  1328. print_symname ("%-20s|", SYM_NAME (info), abfd);
  1329. if (bfd_is_undefined_symclass (SYM_TYPE (info)))
  1330. {
  1331. if (print_width == 32)
  1332. printf (" ");
  1333. else
  1334. printf (" ");
  1335. }
  1336. else
  1337. print_value (abfd, SYM_VALUE (info));
  1338. printf ("| %c |", SYM_TYPE (info));
  1339. if (SYM_TYPE (info) == '-')
  1340. {
  1341. /* A stab. */
  1342. printf ("%18s| ", SYM_STAB_NAME (info)); /* (C) Type. */
  1343. printf (desc_format, SYM_STAB_DESC (info)); /* Size. */
  1344. printf ("| |"); /* Line, Section. */
  1345. }
  1346. else
  1347. {
  1348. /* Type, Size, Line, Section */
  1349. if (info->elfinfo)
  1350. printf ("%18s|",
  1351. get_elf_symbol_type (ELF_ST_TYPE (info->elfinfo->internal_elf_sym.st_info)));
  1352. else if (info->coffinfo)
  1353. printf ("%18s|",
  1354. get_coff_symbol_type (&info->coffinfo->native->u.syment));
  1355. else
  1356. printf (" |");
  1357. if (SYM_SIZE (info))
  1358. print_value (abfd, SYM_SIZE (info));
  1359. else
  1360. {
  1361. if (print_width == 32)
  1362. printf (" ");
  1363. else
  1364. printf (" ");
  1365. }
  1366. if (info->elfinfo)
  1367. printf("| |%s", info->elfinfo->symbol.section->name);
  1368. else if (info->coffinfo)
  1369. printf("| |%s", info->coffinfo->symbol.section->name);
  1370. else
  1371. printf("| |");
  1372. }
  1373. }
  1374. static void
  1375. print_symbol_info_posix (struct extended_symbol_info *info, bfd *abfd)
  1376. {
  1377. print_symname ("%s ", SYM_NAME (info), abfd);
  1378. printf ("%c ", SYM_TYPE (info));
  1379. if (bfd_is_undefined_symclass (SYM_TYPE (info)))
  1380. printf (" ");
  1381. else
  1382. {
  1383. print_value (abfd, SYM_VALUE (info));
  1384. printf (" ");
  1385. if (SYM_SIZE (info))
  1386. print_value (abfd, SYM_SIZE (info));
  1387. }
  1388. }
  1389. int
  1390. main (int argc, char **argv)
  1391. {
  1392. int c;
  1393. int retval;
  1394. #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
  1395. setlocale (LC_MESSAGES, "");
  1396. #endif
  1397. #if defined (HAVE_SETLOCALE)
  1398. setlocale (LC_CTYPE, "");
  1399. setlocale (LC_COLLATE, "");
  1400. #endif
  1401. bindtextdomain (PACKAGE, LOCALEDIR);
  1402. textdomain (PACKAGE);
  1403. program_name = *argv;
  1404. xmalloc_set_program_name (program_name);
  1405. bfd_set_error_program_name (program_name);
  1406. #if BFD_SUPPORTS_PLUGINS
  1407. bfd_plugin_set_program_name (program_name);
  1408. #endif
  1409. START_PROGRESS (program_name, 0);
  1410. expandargv (&argc, &argv);
  1411. bfd_init ();
  1412. set_default_bfd_target ();
  1413. while ((c = getopt_long (argc, argv, "aABCDef:gHhlnopPrSst:uvVvX:",
  1414. long_options, (int *) 0)) != EOF)
  1415. {
  1416. switch (c)
  1417. {
  1418. case 'a':
  1419. print_debug_syms = 1;
  1420. break;
  1421. case 'A':
  1422. case 'o':
  1423. filename_per_symbol = 1;
  1424. break;
  1425. case 'B': /* For MIPS compatibility. */
  1426. set_output_format ("bsd");
  1427. break;
  1428. case 'C':
  1429. do_demangle = 1;
  1430. if (optarg != NULL)
  1431. {
  1432. enum demangling_styles style;
  1433. style = cplus_demangle_name_to_style (optarg);
  1434. if (style == unknown_demangling)
  1435. fatal (_("unknown demangling style `%s'"),
  1436. optarg);
  1437. cplus_demangle_set_style (style);
  1438. }
  1439. break;
  1440. case 'D':
  1441. dynamic = 1;
  1442. break;
  1443. case 'e':
  1444. /* Ignored for HP/UX compatibility. */
  1445. break;
  1446. case 'f':
  1447. set_output_format (optarg);
  1448. break;
  1449. case 'g':
  1450. external_only = 1;
  1451. break;
  1452. case 'H':
  1453. case 'h':
  1454. usage (stdout, 0);
  1455. case 'l':
  1456. line_numbers = 1;
  1457. break;
  1458. case 'n':
  1459. case 'v':
  1460. no_sort = 0;
  1461. sort_numerically = 1;
  1462. sort_by_size = 0;
  1463. break;
  1464. case 'p':
  1465. no_sort = 1;
  1466. sort_numerically = 0;
  1467. sort_by_size = 0;
  1468. break;
  1469. case OPTION_SIZE_SORT:
  1470. no_sort = 0;
  1471. sort_numerically = 0;
  1472. sort_by_size = 1;
  1473. break;
  1474. case 'P':
  1475. set_output_format ("posix");
  1476. break;
  1477. case 'r':
  1478. reverse_sort = 1;
  1479. break;
  1480. case 's':
  1481. print_armap = 1;
  1482. break;
  1483. case 'S':
  1484. print_size = 1;
  1485. break;
  1486. case 't':
  1487. set_print_radix (optarg);
  1488. break;
  1489. case 'u':
  1490. undefined_only = 1;
  1491. break;
  1492. case 'V':
  1493. show_version = 1;
  1494. break;
  1495. case 'X':
  1496. /* Ignored for (partial) AIX compatibility. On AIX, the
  1497. argument has values 32, 64, or 32_64, and specifies that
  1498. only 32-bit, only 64-bit, or both kinds of objects should
  1499. be examined. The default is 32. So plain AIX nm on a
  1500. library archive with both kinds of objects will ignore
  1501. the 64-bit ones. For GNU nm, the default is and always
  1502. has been -X 32_64, and other options are not supported. */
  1503. if (strcmp (optarg, "32_64") != 0)
  1504. fatal (_("Only -X 32_64 is supported"));
  1505. break;
  1506. case OPTION_TARGET: /* --target */
  1507. target = optarg;
  1508. break;
  1509. case OPTION_PLUGIN: /* --plugin */
  1510. #if BFD_SUPPORTS_PLUGINS
  1511. bfd_plugin_set_plugin (optarg);
  1512. #else
  1513. fatal (_("sorry - this program has been built without plugin support\n"));
  1514. #endif
  1515. break;
  1516. case 0: /* A long option that just sets a flag. */
  1517. break;
  1518. default:
  1519. usage (stderr, 1);
  1520. }
  1521. }
  1522. if (show_version)
  1523. print_version ("nm");
  1524. if (sort_by_size && undefined_only)
  1525. {
  1526. non_fatal (_("Using the --size-sort and --undefined-only options together"));
  1527. non_fatal (_("will produce no output, since undefined symbols have no size."));
  1528. return 0;
  1529. }
  1530. /* OK, all options now parsed. If no filename specified, do a.out. */
  1531. if (optind == argc)
  1532. return !display_file ("a.out");
  1533. retval = 0;
  1534. if (argc - optind > 1)
  1535. filename_per_file = 1;
  1536. /* We were given several filenames to do. */
  1537. while (optind < argc)
  1538. {
  1539. PROGRESS (1);
  1540. if (!display_file (argv[optind++]))
  1541. retval++;
  1542. }
  1543. END_PROGRESS (program_name);
  1544. #ifdef HAVE_SBRK
  1545. if (show_stats)
  1546. {
  1547. char *lim = (char *) sbrk (0);
  1548. non_fatal (_("data size %ld"), (long) (lim - (char *) &environ));
  1549. }
  1550. #endif
  1551. exit (retval);
  1552. return retval;
  1553. }