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