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