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