nm.c 45 KB

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