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