2005-01-03 Paolo Bonzini <bonzini@gnu.org>
[binutils.git] / binutils / nm.c
blob3668d6b75e811e6f9ceab1a225b8a782b0cc6053
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, 2003, 2004
4 Free Software Foundation, Inc.
6 This file is part of GNU Binutils.
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.
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.
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. */
23 #include "bfd.h"
24 #include "progress.h"
25 #include "bucomm.h"
26 #include "budemang.h"
27 #include "getopt.h"
28 #include "aout/stab_gnu.h"
29 #include "aout/ranlib.h"
30 #include "demangle.h"
31 #include "libiberty.h"
32 #include "elf-bfd.h"
33 #include "elf/common.h"
35 /* When sorting by size, we use this structure to hold the size and a
36 pointer to the minisymbol. */
38 struct size_sym
40 const void *minisym;
41 bfd_vma size;
44 /* When fetching relocs, we use this structure to pass information to
45 get_relocs. */
47 struct get_relocs_info
49 asection **secs;
50 arelent ***relocs;
51 long *relcount;
52 asymbol **syms;
55 struct extended_symbol_info
57 symbol_info *sinfo;
58 bfd_vma ssize;
59 elf_symbol_type *elfinfo;
60 /* FIXME: We should add more fields for Type, Line, Section. */
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)
68 #define SYM_SIZE(sym) \
69 (sym->elfinfo ? sym->elfinfo->internal_elf_sym.st_size: sym->ssize)
71 /* The output formatting functions. */
72 static void print_object_filename_bsd (char *);
73 static void print_object_filename_sysv (char *);
74 static void print_object_filename_posix (char *);
75 static void print_archive_filename_bsd (char *);
76 static void print_archive_filename_sysv (char *);
77 static void print_archive_filename_posix (char *);
78 static void print_archive_member_bsd (char *, const char *);
79 static void print_archive_member_sysv (char *, const char *);
80 static void print_archive_member_posix (char *, const char *);
81 static void print_symbol_filename_bsd (bfd *, bfd *);
82 static void print_symbol_filename_sysv (bfd *, bfd *);
83 static void print_symbol_filename_posix (bfd *, bfd *);
84 static void print_value (bfd *, bfd_vma);
85 static void print_symbol_info_bsd (struct extended_symbol_info *, bfd *);
86 static void print_symbol_info_sysv (struct extended_symbol_info *, bfd *);
87 static void print_symbol_info_posix (struct extended_symbol_info *, bfd *);
89 /* Support for different output formats. */
90 struct output_fns
92 /* Print the name of an object file given on the command line. */
93 void (*print_object_filename) (char *);
95 /* Print the name of an archive file given on the command line. */
96 void (*print_archive_filename) (char *);
98 /* Print the name of an archive member file. */
99 void (*print_archive_member) (char *, const char *);
101 /* Print the name of the file (and archive, if there is one)
102 containing a symbol. */
103 void (*print_symbol_filename) (bfd *, bfd *);
105 /* Print a line of information about a symbol. */
106 void (*print_symbol_info) (struct extended_symbol_info *, bfd *);
109 static struct output_fns formats[] =
111 {print_object_filename_bsd,
112 print_archive_filename_bsd,
113 print_archive_member_bsd,
114 print_symbol_filename_bsd,
115 print_symbol_info_bsd},
116 {print_object_filename_sysv,
117 print_archive_filename_sysv,
118 print_archive_member_sysv,
119 print_symbol_filename_sysv,
120 print_symbol_info_sysv},
121 {print_object_filename_posix,
122 print_archive_filename_posix,
123 print_archive_member_posix,
124 print_symbol_filename_posix,
125 print_symbol_info_posix}
128 /* Indices in `formats'. */
129 #define FORMAT_BSD 0
130 #define FORMAT_SYSV 1
131 #define FORMAT_POSIX 2
132 #define FORMAT_DEFAULT FORMAT_BSD
134 /* The output format to use. */
135 static struct output_fns *format = &formats[FORMAT_DEFAULT];
137 /* Command options. */
139 static int do_demangle = 0; /* Pretty print C++ symbol names. */
140 static int external_only = 0; /* Print external symbols only. */
141 static int defined_only = 0; /* Print defined symbols only. */
142 static int no_sort = 0; /* Don't sort; print syms in order found. */
143 static int print_debug_syms = 0;/* Print debugger-only symbols too. */
144 static int print_armap = 0; /* Describe __.SYMDEF data in archive files. */
145 static int print_size = 0; /* Print size of defined symbols. */
146 static int reverse_sort = 0; /* Sort in downward(alpha or numeric) order. */
147 static int sort_numerically = 0;/* Sort in numeric rather than alpha order. */
148 static int sort_by_size = 0; /* Sort by size of symbol. */
149 static int undefined_only = 0; /* Print undefined symbols only. */
150 static int dynamic = 0; /* Print dynamic symbols. */
151 static int show_version = 0; /* Show the version number. */
152 static int show_stats = 0; /* Show statistics. */
153 static int show_synthetic = 0; /* Display synthesized symbols too. */
154 static int line_numbers = 0; /* Print line numbers for symbols. */
155 static int allow_special_symbols = 0; /* Allow special symbols. */
157 /* When to print the names of files. Not mutually exclusive in SYSV format. */
158 static int filename_per_file = 0; /* Once per file, on its own line. */
159 static int filename_per_symbol = 0; /* Once per symbol, at start of line. */
161 /* Print formats for printing a symbol value. */
162 #ifndef BFD64
163 static char value_format[] = "%08lx";
164 #else
165 #if BFD_HOST_64BIT_LONG
166 static char value_format[] = "%016lx";
167 #else
168 /* We don't use value_format for this case. */
169 #endif
170 #endif
171 #ifdef BFD64
172 static int print_width = 16;
173 #else
174 static int print_width = 8;
175 #endif
176 static int print_radix = 16;
177 /* Print formats for printing stab info. */
178 static char other_format[] = "%02x";
179 static char desc_format[] = "%04x";
181 static char *target = NULL;
183 /* Used to cache the line numbers for a BFD. */
184 static bfd *lineno_cache_bfd;
185 static bfd *lineno_cache_rel_bfd;
187 #define OPTION_TARGET 200
189 static struct option long_options[] =
191 {"debug-syms", no_argument, &print_debug_syms, 1},
192 {"demangle", optional_argument, 0, 'C'},
193 {"dynamic", no_argument, &dynamic, 1},
194 {"extern-only", no_argument, &external_only, 1},
195 {"format", required_argument, 0, 'f'},
196 {"help", no_argument, 0, 'h'},
197 {"line-numbers", no_argument, 0, 'l'},
198 {"no-cplus", no_argument, &do_demangle, 0}, /* Linux compatibility. */
199 {"no-demangle", no_argument, &do_demangle, 0},
200 {"no-sort", no_argument, &no_sort, 1},
201 {"numeric-sort", no_argument, &sort_numerically, 1},
202 {"portability", no_argument, 0, 'P'},
203 {"print-armap", no_argument, &print_armap, 1},
204 {"print-file-name", no_argument, 0, 'o'},
205 {"print-size", no_argument, 0, 'S'},
206 {"radix", required_argument, 0, 't'},
207 {"reverse-sort", no_argument, &reverse_sort, 1},
208 {"size-sort", no_argument, &sort_by_size, 1},
209 {"special-syms", no_argument, &allow_special_symbols, 1},
210 {"stats", no_argument, &show_stats, 1},
211 {"synthetic", no_argument, &show_synthetic, 1},
212 {"target", required_argument, 0, OPTION_TARGET},
213 {"defined-only", no_argument, &defined_only, 1},
214 {"undefined-only", no_argument, &undefined_only, 1},
215 {"version", no_argument, &show_version, 1},
216 {0, no_argument, 0, 0}
219 /* Some error-reporting functions. */
221 static void
222 usage (FILE *stream, int status)
224 fprintf (stream, _("Usage: %s [option(s)] [file(s)]\n"), program_name);
225 fprintf (stream, _(" List symbols in [file(s)] (a.out by default).\n"));
226 fprintf (stream, _(" The options are:\n\
227 -a, --debug-syms Display debugger-only symbols\n\
228 -A, --print-file-name Print name of the input file before every symbol\n\
229 -B Same as --format=bsd\n\
230 -C, --demangle[=STYLE] Decode low-level symbol names into user-level names\n\
231 The STYLE, if specified, can be `auto' (the default),\n\
232 `gnu', `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
233 or `gnat'\n\
234 --no-demangle Do not demangle low-level symbol names\n\
235 -D, --dynamic Display dynamic symbols instead of normal symbols\n\
236 --defined-only Display only defined symbols\n\
237 -e (ignored)\n\
238 -f, --format=FORMAT Use the output format FORMAT. FORMAT can be `bsd',\n\
239 `sysv' or `posix'. The default is `bsd'\n\
240 -g, --extern-only Display only external symbols\n\
241 -l, --line-numbers Use debugging information to find a filename and\n\
242 line number for each symbol\n\
243 -n, --numeric-sort Sort symbols numerically by address\n\
244 -o Same as -A\n\
245 -p, --no-sort Do not sort the symbols\n\
246 -P, --portability Same as --format=posix\n\
247 -r, --reverse-sort Reverse the sense of the sort\n\
248 -S, --print-size Print size of defined symbols\n\
249 -s, --print-armap Include index for symbols from archive members\n\
250 --size-sort Sort symbols by size\n\
251 --special-syms Include special symbols in the output\n\
252 --synthetic Display synthetic symbols as well\n\
253 -t, --radix=RADIX Use RADIX for printing symbol values\n\
254 --target=BFDNAME Specify the target object format as BFDNAME\n\
255 -u, --undefined-only Display only undefined symbols\n\
256 -X 32_64 (ignored)\n\
257 -h, --help Display this information\n\
258 -V, --version Display this program's version number\n\
259 \n"));
260 list_supported_targets (program_name, stream);
261 if (status == 0)
262 fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
263 exit (status);
266 /* Set the radix for the symbol value and size according to RADIX. */
268 static void
269 set_print_radix (char *radix)
271 switch (*radix)
273 case 'x':
274 break;
275 case 'd':
276 case 'o':
277 if (*radix == 'd')
278 print_radix = 10;
279 else
280 print_radix = 8;
281 #ifndef BFD64
282 value_format[4] = *radix;
283 #else
284 #if BFD_HOST_64BIT_LONG
285 value_format[5] = *radix;
286 #else
287 /* This case requires special handling for octal and decimal
288 printing. */
289 #endif
290 #endif
291 other_format[3] = desc_format[3] = *radix;
292 break;
293 default:
294 fatal (_("%s: invalid radix"), radix);
298 static void
299 set_output_format (char *f)
301 int i;
303 switch (*f)
305 case 'b':
306 case 'B':
307 i = FORMAT_BSD;
308 break;
309 case 'p':
310 case 'P':
311 i = FORMAT_POSIX;
312 break;
313 case 's':
314 case 'S':
315 i = FORMAT_SYSV;
316 break;
317 default:
318 fatal (_("%s: invalid output format"), f);
320 format = &formats[i];
323 static const char *
324 get_symbol_type (unsigned int type)
326 static char buff [32];
328 switch (type)
330 case STT_NOTYPE: return "NOTYPE";
331 case STT_OBJECT: return "OBJECT";
332 case STT_FUNC: return "FUNC";
333 case STT_SECTION: return "SECTION";
334 case STT_FILE: return "FILE";
335 case STT_COMMON: return "COMMON";
336 case STT_TLS: return "TLS";
337 default:
338 if (type >= STT_LOPROC && type <= STT_HIPROC)
339 sprintf (buff, _("<processor specific>: %d"), type);
340 else if (type >= STT_LOOS && type <= STT_HIOS)
341 sprintf (buff, _("<OS specific>: %d"), type);
342 else
343 sprintf (buff, _("<unknown>: %d"), type);
344 return buff;
348 /* Print symbol name NAME, read from ABFD, with printf format FORMAT,
349 demangling it if requested. */
351 static void
352 print_symname (const char *format, const char *name, bfd *abfd)
354 if (do_demangle && *name)
356 char *res = demangle (abfd, name);
358 printf (format, res);
359 free (res);
360 return;
363 printf (format, name);
366 static void
367 print_symdef_entry (bfd *abfd)
369 symindex idx = BFD_NO_MORE_SYMBOLS;
370 carsym *thesym;
371 bfd_boolean everprinted = FALSE;
373 for (idx = bfd_get_next_mapent (abfd, idx, &thesym);
374 idx != BFD_NO_MORE_SYMBOLS;
375 idx = bfd_get_next_mapent (abfd, idx, &thesym))
377 bfd *elt;
378 if (!everprinted)
380 printf (_("\nArchive index:\n"));
381 everprinted = TRUE;
383 elt = bfd_get_elt_at_index (abfd, idx);
384 if (elt == NULL)
385 bfd_fatal ("bfd_get_elt_at_index");
386 if (thesym->name != (char *) NULL)
388 print_symname ("%s", thesym->name, abfd);
389 printf (" in %s\n", bfd_get_filename (elt));
394 /* Choose which symbol entries to print;
395 compact them downward to get rid of the rest.
396 Return the number of symbols to be printed. */
398 static long
399 filter_symbols (bfd *abfd, bfd_boolean dynamic, void *minisyms,
400 long symcount, unsigned int size)
402 bfd_byte *from, *fromend, *to;
403 asymbol *store;
405 store = bfd_make_empty_symbol (abfd);
406 if (store == NULL)
407 bfd_fatal (bfd_get_filename (abfd));
409 from = (bfd_byte *) minisyms;
410 fromend = from + symcount * size;
411 to = (bfd_byte *) minisyms;
413 for (; from < fromend; from += size)
415 int keep = 0;
416 asymbol *sym;
418 PROGRESS (1);
420 sym = bfd_minisymbol_to_symbol (abfd, dynamic, (const void *) from, store);
421 if (sym == NULL)
422 bfd_fatal (bfd_get_filename (abfd));
424 if (undefined_only)
425 keep = bfd_is_und_section (sym->section);
426 else if (external_only)
427 keep = ((sym->flags & BSF_GLOBAL) != 0
428 || (sym->flags & BSF_WEAK) != 0
429 || bfd_is_und_section (sym->section)
430 || bfd_is_com_section (sym->section));
431 else
432 keep = 1;
434 if (keep
435 && ! print_debug_syms
436 && (sym->flags & BSF_DEBUGGING) != 0)
437 keep = 0;
439 if (keep
440 && sort_by_size
441 && (bfd_is_abs_section (sym->section)
442 || bfd_is_und_section (sym->section)))
443 keep = 0;
445 if (keep
446 && defined_only)
448 if (bfd_is_und_section (sym->section))
449 keep = 0;
452 if (keep
453 && bfd_is_target_special_symbol (abfd, sym)
454 && ! allow_special_symbols)
455 keep = 0;
457 if (keep)
459 memcpy (to, from, size);
460 to += size;
464 return (to - (bfd_byte *) minisyms) / size;
467 /* These globals are used to pass information into the sorting
468 routines. */
469 static bfd *sort_bfd;
470 static bfd_boolean sort_dynamic;
471 static asymbol *sort_x;
472 static asymbol *sort_y;
474 /* Symbol-sorting predicates */
475 #define valueof(x) ((x)->section->vma + (x)->value)
477 /* Numeric sorts. Undefined symbols are always considered "less than"
478 defined symbols with zero values. Common symbols are not treated
479 specially -- i.e., their sizes are used as their "values". */
481 static int
482 non_numeric_forward (const void *P_x, const void *P_y)
484 asymbol *x, *y;
485 const char *xn, *yn;
487 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
488 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
489 if (x == NULL || y == NULL)
490 bfd_fatal (bfd_get_filename (sort_bfd));
492 xn = bfd_asymbol_name (x);
493 yn = bfd_asymbol_name (y);
495 if (yn == NULL)
496 return xn != NULL;
497 if (xn == NULL)
498 return -1;
500 #ifdef HAVE_STRCOLL
501 /* Solaris 2.5 has a bug in strcoll.
502 strcoll returns invalid values when confronted with empty strings. */
503 if (*yn == '\0')
504 return *xn != '\0';
505 if (*xn == '\0')
506 return -1;
508 return strcoll (xn, yn);
509 #else
510 return strcmp (xn, yn);
511 #endif
514 static int
515 non_numeric_reverse (const void *x, const void *y)
517 return - non_numeric_forward (x, y);
520 static int
521 numeric_forward (const void *P_x, const void *P_y)
523 asymbol *x, *y;
524 asection *xs, *ys;
526 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
527 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
528 if (x == NULL || y == NULL)
529 bfd_fatal (bfd_get_filename (sort_bfd));
531 xs = bfd_get_section (x);
532 ys = bfd_get_section (y);
534 if (bfd_is_und_section (xs))
536 if (! bfd_is_und_section (ys))
537 return -1;
539 else if (bfd_is_und_section (ys))
540 return 1;
541 else if (valueof (x) != valueof (y))
542 return valueof (x) < valueof (y) ? -1 : 1;
544 return non_numeric_forward (P_x, P_y);
547 static int
548 numeric_reverse (const void *x, const void *y)
550 return - numeric_forward (x, y);
553 static int (*(sorters[2][2])) (const void *, const void *) =
555 { non_numeric_forward, non_numeric_reverse },
556 { numeric_forward, numeric_reverse }
559 /* This sort routine is used by sort_symbols_by_size. It is similar
560 to numeric_forward, but when symbols have the same value it sorts
561 by section VMA. This simplifies the sort_symbols_by_size code
562 which handles symbols at the end of sections. Also, this routine
563 tries to sort file names before other symbols with the same value.
564 That will make the file name have a zero size, which will make
565 sort_symbols_by_size choose the non file name symbol, leading to
566 more meaningful output. For similar reasons, this code sorts
567 gnu_compiled_* and gcc2_compiled before other symbols with the same
568 value. */
570 static int
571 size_forward1 (const void *P_x, const void *P_y)
573 asymbol *x, *y;
574 asection *xs, *ys;
575 const char *xn, *yn;
576 size_t xnl, ynl;
577 int xf, yf;
579 x = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_x, sort_x);
580 y = bfd_minisymbol_to_symbol (sort_bfd, sort_dynamic, P_y, sort_y);
581 if (x == NULL || y == NULL)
582 bfd_fatal (bfd_get_filename (sort_bfd));
584 xs = bfd_get_section (x);
585 ys = bfd_get_section (y);
587 if (bfd_is_und_section (xs))
588 abort ();
589 if (bfd_is_und_section (ys))
590 abort ();
592 if (valueof (x) != valueof (y))
593 return valueof (x) < valueof (y) ? -1 : 1;
595 if (xs->vma != ys->vma)
596 return xs->vma < ys->vma ? -1 : 1;
598 xn = bfd_asymbol_name (x);
599 yn = bfd_asymbol_name (y);
600 xnl = strlen (xn);
601 ynl = strlen (yn);
603 /* The symbols gnu_compiled and gcc2_compiled convey even less
604 information than the file name, so sort them out first. */
606 xf = (strstr (xn, "gnu_compiled") != NULL
607 || strstr (xn, "gcc2_compiled") != NULL);
608 yf = (strstr (yn, "gnu_compiled") != NULL
609 || strstr (yn, "gcc2_compiled") != NULL);
611 if (xf && ! yf)
612 return -1;
613 if (! xf && yf)
614 return 1;
616 /* We use a heuristic for the file name. It may not work on non
617 Unix systems, but it doesn't really matter; the only difference
618 is precisely which symbol names get printed. */
620 #define file_symbol(s, sn, snl) \
621 (((s)->flags & BSF_FILE) != 0 \
622 || ((sn)[(snl) - 2] == '.' \
623 && ((sn)[(snl) - 1] == 'o' \
624 || (sn)[(snl) - 1] == 'a')))
626 xf = file_symbol (x, xn, xnl);
627 yf = file_symbol (y, yn, ynl);
629 if (xf && ! yf)
630 return -1;
631 if (! xf && yf)
632 return 1;
634 return non_numeric_forward (P_x, P_y);
637 /* This sort routine is used by sort_symbols_by_size. It is sorting
638 an array of size_sym structures into size order. */
640 static int
641 size_forward2 (const void *P_x, const void *P_y)
643 const struct size_sym *x = (const struct size_sym *) P_x;
644 const struct size_sym *y = (const struct size_sym *) P_y;
646 if (x->size < y->size)
647 return reverse_sort ? 1 : -1;
648 else if (x->size > y->size)
649 return reverse_sort ? -1 : 1;
650 else
651 return sorters[0][reverse_sort] (x->minisym, y->minisym);
654 /* Sort the symbols by size. ELF provides a size but for other formats
655 we have to make a guess by assuming that the difference between the
656 address of a symbol and the address of the next higher symbol is the
657 size. */
659 static long
660 sort_symbols_by_size (bfd *abfd, bfd_boolean dynamic, void *minisyms,
661 long symcount, unsigned int size,
662 struct size_sym **symsizesp)
664 struct size_sym *symsizes;
665 bfd_byte *from, *fromend;
666 asymbol *sym = NULL;
667 asymbol *store_sym, *store_next;
669 qsort (minisyms, symcount, size, size_forward1);
671 /* We are going to return a special set of symbols and sizes to
672 print. */
673 symsizes = xmalloc (symcount * sizeof (struct size_sym));
674 *symsizesp = symsizes;
676 /* Note that filter_symbols has already removed all absolute and
677 undefined symbols. Here we remove all symbols whose size winds
678 up as zero. */
679 from = (bfd_byte *) minisyms;
680 fromend = from + symcount * size;
682 store_sym = sort_x;
683 store_next = sort_y;
685 if (from < fromend)
687 sym = bfd_minisymbol_to_symbol (abfd, dynamic, (const void *) from,
688 store_sym);
689 if (sym == NULL)
690 bfd_fatal (bfd_get_filename (abfd));
693 for (; from < fromend; from += size)
695 asymbol *next;
696 asection *sec;
697 bfd_vma sz;
698 asymbol *temp;
700 if (from + size < fromend)
702 next = bfd_minisymbol_to_symbol (abfd,
703 dynamic,
704 (const void *) (from + size),
705 store_next);
706 if (next == NULL)
707 bfd_fatal (bfd_get_filename (abfd));
709 else
710 next = NULL;
712 sec = bfd_get_section (sym);
714 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
715 sz = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
716 else if (bfd_is_com_section (sec))
717 sz = sym->value;
718 else
720 if (from + size < fromend
721 && sec == bfd_get_section (next))
722 sz = valueof (next) - valueof (sym);
723 else
724 sz = (bfd_get_section_vma (abfd, sec)
725 + bfd_section_size (abfd, sec)
726 - valueof (sym));
729 if (sz != 0)
731 symsizes->minisym = (const void *) from;
732 symsizes->size = sz;
733 ++symsizes;
736 sym = next;
738 temp = store_sym;
739 store_sym = store_next;
740 store_next = temp;
743 symcount = symsizes - *symsizesp;
745 /* We must now sort again by size. */
746 qsort ((void *) *symsizesp, symcount, sizeof (struct size_sym), size_forward2);
748 return symcount;
751 /* This function is used to get the relocs for a particular section.
752 It is called via bfd_map_over_sections. */
754 static void
755 get_relocs (bfd *abfd, asection *sec, void *dataarg)
757 struct get_relocs_info *data = (struct get_relocs_info *) dataarg;
759 *data->secs = sec;
761 if ((sec->flags & SEC_RELOC) == 0)
763 *data->relocs = NULL;
764 *data->relcount = 0;
766 else
768 long relsize;
770 relsize = bfd_get_reloc_upper_bound (abfd, sec);
771 if (relsize < 0)
772 bfd_fatal (bfd_get_filename (abfd));
774 *data->relocs = xmalloc (relsize);
775 *data->relcount = bfd_canonicalize_reloc (abfd, sec, *data->relocs,
776 data->syms);
777 if (*data->relcount < 0)
778 bfd_fatal (bfd_get_filename (abfd));
781 ++data->secs;
782 ++data->relocs;
783 ++data->relcount;
786 /* Print a single symbol. */
788 static void
789 print_symbol (bfd *abfd, asymbol *sym, bfd_vma ssize, bfd *archive_bfd)
791 symbol_info syminfo;
792 struct extended_symbol_info info;
794 PROGRESS (1);
796 format->print_symbol_filename (archive_bfd, abfd);
798 bfd_get_symbol_info (abfd, sym, &syminfo);
799 info.sinfo = &syminfo;
800 info.ssize = ssize;
801 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
802 info.elfinfo = (elf_symbol_type *) sym;
803 else
804 info.elfinfo = NULL;
805 format->print_symbol_info (&info, abfd);
807 if (line_numbers)
809 static asymbol **syms;
810 static long symcount;
811 const char *filename, *functionname;
812 unsigned int lineno;
814 /* We need to get the canonical symbols in order to call
815 bfd_find_nearest_line. This is inefficient, but, then, you
816 don't have to use --line-numbers. */
817 if (abfd != lineno_cache_bfd && syms != NULL)
819 free (syms);
820 syms = NULL;
822 if (syms == NULL)
824 long symsize;
826 symsize = bfd_get_symtab_upper_bound (abfd);
827 if (symsize < 0)
828 bfd_fatal (bfd_get_filename (abfd));
829 syms = xmalloc (symsize);
830 symcount = bfd_canonicalize_symtab (abfd, syms);
831 if (symcount < 0)
832 bfd_fatal (bfd_get_filename (abfd));
833 lineno_cache_bfd = abfd;
836 if (bfd_is_und_section (bfd_get_section (sym)))
838 static asection **secs;
839 static arelent ***relocs;
840 static long *relcount;
841 static unsigned int seccount;
842 unsigned int i;
843 const char *symname;
845 /* For an undefined symbol, we try to find a reloc for the
846 symbol, and print the line number of the reloc. */
847 if (abfd != lineno_cache_rel_bfd && relocs != NULL)
849 for (i = 0; i < seccount; i++)
850 if (relocs[i] != NULL)
851 free (relocs[i]);
852 free (secs);
853 free (relocs);
854 free (relcount);
855 secs = NULL;
856 relocs = NULL;
857 relcount = NULL;
860 if (relocs == NULL)
862 struct get_relocs_info info;
864 seccount = bfd_count_sections (abfd);
866 secs = xmalloc (seccount * sizeof *secs);
867 relocs = xmalloc (seccount * sizeof *relocs);
868 relcount = xmalloc (seccount * sizeof *relcount);
870 info.secs = secs;
871 info.relocs = relocs;
872 info.relcount = relcount;
873 info.syms = syms;
874 bfd_map_over_sections (abfd, get_relocs, (void *) &info);
875 lineno_cache_rel_bfd = abfd;
878 symname = bfd_asymbol_name (sym);
879 for (i = 0; i < seccount; i++)
881 long j;
883 for (j = 0; j < relcount[i]; j++)
885 arelent *r;
887 r = relocs[i][j];
888 if (r->sym_ptr_ptr != NULL
889 && (*r->sym_ptr_ptr)->section == sym->section
890 && (*r->sym_ptr_ptr)->value == sym->value
891 && strcmp (symname,
892 bfd_asymbol_name (*r->sym_ptr_ptr)) == 0
893 && bfd_find_nearest_line (abfd, secs[i], syms,
894 r->address, &filename,
895 &functionname, &lineno)
896 && filename != NULL)
898 /* We only print the first one we find. */
899 printf ("\t%s:%u", filename, lineno);
900 i = seccount;
901 break;
906 else if (bfd_get_section (sym)->owner == abfd)
908 if (bfd_find_nearest_line (abfd, bfd_get_section (sym), syms,
909 sym->value, &filename, &functionname,
910 &lineno)
911 && filename != NULL
912 && lineno != 0)
913 printf ("\t%s:%u", filename, lineno);
917 putchar ('\n');
920 /* Print the symbols when sorting by size. */
922 static void
923 print_size_symbols (bfd *abfd, bfd_boolean dynamic,
924 struct size_sym *symsizes, long symcount,
925 bfd *archive_bfd)
927 asymbol *store;
928 struct size_sym *from, *fromend;
930 store = bfd_make_empty_symbol (abfd);
931 if (store == NULL)
932 bfd_fatal (bfd_get_filename (abfd));
934 from = symsizes;
935 fromend = from + symcount;
936 for (; from < fromend; from++)
938 asymbol *sym;
939 bfd_vma ssize;
941 sym = bfd_minisymbol_to_symbol (abfd, dynamic, from->minisym, store);
942 if (sym == NULL)
943 bfd_fatal (bfd_get_filename (abfd));
945 /* For elf we have already computed the correct symbol size. */
946 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
947 ssize = from->size;
948 else
949 ssize = from->size - bfd_section_vma (abfd, bfd_get_section (sym));
951 print_symbol (abfd, sym, ssize, archive_bfd);
956 /* Print the symbols. If ARCHIVE_BFD is non-NULL, it is the archive
957 containing ABFD. */
959 static void
960 print_symbols (bfd *abfd, bfd_boolean dynamic, void *minisyms, long symcount,
961 unsigned int size, bfd *archive_bfd)
963 asymbol *store;
964 bfd_byte *from, *fromend;
966 store = bfd_make_empty_symbol (abfd);
967 if (store == NULL)
968 bfd_fatal (bfd_get_filename (abfd));
970 from = (bfd_byte *) minisyms;
971 fromend = from + symcount * size;
972 for (; from < fromend; from += size)
974 asymbol *sym;
976 sym = bfd_minisymbol_to_symbol (abfd, dynamic, from, store);
977 if (sym == NULL)
978 bfd_fatal (bfd_get_filename (abfd));
980 print_symbol (abfd, sym, (bfd_vma) 0, archive_bfd);
984 /* If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD. */
986 static void
987 display_rel_file (bfd *abfd, bfd *archive_bfd)
989 long symcount;
990 void *minisyms;
991 unsigned int size;
992 struct size_sym *symsizes;
994 if (! dynamic)
996 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
998 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd));
999 return;
1003 symcount = bfd_read_minisymbols (abfd, dynamic, &minisyms, &size);
1004 if (symcount < 0)
1005 bfd_fatal (bfd_get_filename (abfd));
1007 if (symcount == 0)
1009 non_fatal (_("%s: no symbols"), bfd_get_filename (abfd));
1010 return;
1013 if (show_synthetic && size == sizeof (asymbol *))
1015 asymbol *synthsyms;
1016 long synth_count;
1017 asymbol **static_syms = NULL;
1018 asymbol **dyn_syms = NULL;
1019 long static_count = 0;
1020 long dyn_count = 0;
1022 if (dynamic)
1024 dyn_count = symcount;
1025 dyn_syms = minisyms;
1027 else
1029 static_count = symcount;
1030 static_syms = minisyms;
1032 synth_count = bfd_get_synthetic_symtab (abfd, static_count, static_syms,
1033 dyn_count, dyn_syms, &synthsyms);
1034 if (synth_count > 0)
1036 asymbol **symp;
1037 void *new_mini;
1038 long i;
1040 new_mini = xmalloc ((symcount + synth_count + 1) * sizeof (*symp));
1041 symp = new_mini;
1042 memcpy (symp, minisyms, symcount * sizeof (*symp));
1043 symp += symcount;
1044 for (i = 0; i < synth_count; i++)
1045 *symp++ = synthsyms + i;
1046 *symp = 0;
1047 minisyms = new_mini;
1048 symcount += synth_count;
1052 /* Discard the symbols we don't want to print.
1053 It's OK to do this in place; we'll free the storage anyway
1054 (after printing). */
1056 symcount = filter_symbols (abfd, dynamic, minisyms, symcount, size);
1058 symsizes = NULL;
1059 if (! no_sort)
1061 sort_bfd = abfd;
1062 sort_dynamic = dynamic;
1063 sort_x = bfd_make_empty_symbol (abfd);
1064 sort_y = bfd_make_empty_symbol (abfd);
1065 if (sort_x == NULL || sort_y == NULL)
1066 bfd_fatal (bfd_get_filename (abfd));
1068 if (! sort_by_size)
1069 qsort (minisyms, symcount, size,
1070 sorters[sort_numerically][reverse_sort]);
1071 else
1072 symcount = sort_symbols_by_size (abfd, dynamic, minisyms, symcount,
1073 size, &symsizes);
1076 if (! sort_by_size)
1077 print_symbols (abfd, dynamic, minisyms, symcount, size, archive_bfd);
1078 else
1079 print_size_symbols (abfd, dynamic, symsizes, symcount, archive_bfd);
1081 free (minisyms);
1084 static void
1085 display_archive (bfd *file)
1087 bfd *arfile = NULL;
1088 bfd *last_arfile = NULL;
1089 char **matching;
1091 format->print_archive_filename (bfd_get_filename (file));
1093 if (print_armap)
1094 print_symdef_entry (file);
1096 for (;;)
1098 PROGRESS (1);
1100 arfile = bfd_openr_next_archived_file (file, arfile);
1102 if (arfile == NULL)
1104 if (bfd_get_error () != bfd_error_no_more_archived_files)
1105 bfd_fatal (bfd_get_filename (file));
1106 break;
1109 if (bfd_check_format_matches (arfile, bfd_object, &matching))
1111 char buf[30];
1113 bfd_sprintf_vma (arfile, buf, (bfd_vma) -1);
1114 print_width = strlen (buf);
1115 format->print_archive_member (bfd_get_filename (file),
1116 bfd_get_filename (arfile));
1117 display_rel_file (arfile, file);
1119 else
1121 bfd_nonfatal (bfd_get_filename (arfile));
1122 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1124 list_matching_formats (matching);
1125 free (matching);
1129 if (last_arfile != NULL)
1131 bfd_close (last_arfile);
1132 lineno_cache_bfd = NULL;
1133 lineno_cache_rel_bfd = NULL;
1135 last_arfile = arfile;
1138 if (last_arfile != NULL)
1140 bfd_close (last_arfile);
1141 lineno_cache_bfd = NULL;
1142 lineno_cache_rel_bfd = NULL;
1146 static bfd_boolean
1147 display_file (char *filename)
1149 bfd_boolean retval = TRUE;
1150 bfd *file;
1151 char **matching;
1153 if (get_file_size (filename) < 1)
1154 return FALSE;
1156 file = bfd_openr (filename, target);
1157 if (file == NULL)
1159 bfd_nonfatal (filename);
1160 return FALSE;
1163 if (bfd_check_format (file, bfd_archive))
1165 display_archive (file);
1167 else if (bfd_check_format_matches (file, bfd_object, &matching))
1169 char buf[30];
1171 bfd_sprintf_vma (file, buf, (bfd_vma) -1);
1172 print_width = strlen (buf);
1173 format->print_object_filename (filename);
1174 display_rel_file (file, NULL);
1176 else
1178 bfd_nonfatal (filename);
1179 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1181 list_matching_formats (matching);
1182 free (matching);
1184 retval = FALSE;
1187 if (!bfd_close (file))
1188 bfd_fatal (filename);
1190 lineno_cache_bfd = NULL;
1191 lineno_cache_rel_bfd = NULL;
1193 return retval;
1196 /* The following 3 groups of functions are called unconditionally,
1197 once at the start of processing each file of the appropriate type.
1198 They should check `filename_per_file' and `filename_per_symbol',
1199 as appropriate for their output format, to determine whether to
1200 print anything. */
1202 /* Print the name of an object file given on the command line. */
1204 static void
1205 print_object_filename_bsd (char *filename)
1207 if (filename_per_file && !filename_per_symbol)
1208 printf ("\n%s:\n", filename);
1211 static void
1212 print_object_filename_sysv (char *filename)
1214 if (undefined_only)
1215 printf (_("\n\nUndefined symbols from %s:\n\n"), filename);
1216 else
1217 printf (_("\n\nSymbols from %s:\n\n"), filename);
1218 if (print_width == 8)
1219 printf (_("\
1220 Name Value Class Type Size Line Section\n\n"));
1221 else
1222 printf (_("\
1223 Name Value Class Type Size Line Section\n\n"));
1226 static void
1227 print_object_filename_posix (char *filename)
1229 if (filename_per_file && !filename_per_symbol)
1230 printf ("%s:\n", filename);
1233 /* Print the name of an archive file given on the command line. */
1235 static void
1236 print_archive_filename_bsd (char *filename)
1238 if (filename_per_file)
1239 printf ("\n%s:\n", filename);
1242 static void
1243 print_archive_filename_sysv (char *filename ATTRIBUTE_UNUSED)
1247 static void
1248 print_archive_filename_posix (char *filename ATTRIBUTE_UNUSED)
1252 /* Print the name of an archive member file. */
1254 static void
1255 print_archive_member_bsd (char *archive ATTRIBUTE_UNUSED,
1256 const char *filename)
1258 if (!filename_per_symbol)
1259 printf ("\n%s:\n", filename);
1262 static void
1263 print_archive_member_sysv (char *archive, const char *filename)
1265 if (undefined_only)
1266 printf (_("\n\nUndefined symbols from %s[%s]:\n\n"), archive, filename);
1267 else
1268 printf (_("\n\nSymbols from %s[%s]:\n\n"), archive, filename);
1269 if (print_width == 8)
1270 printf (_("\
1271 Name Value Class Type Size Line Section\n\n"));
1272 else
1273 printf (_("\
1274 Name Value Class Type Size Line Section\n\n"));
1277 static void
1278 print_archive_member_posix (char *archive, const char *filename)
1280 if (!filename_per_symbol)
1281 printf ("%s[%s]:\n", archive, filename);
1284 /* Print the name of the file (and archive, if there is one)
1285 containing a symbol. */
1287 static void
1288 print_symbol_filename_bsd (bfd *archive_bfd, bfd *abfd)
1290 if (filename_per_symbol)
1292 if (archive_bfd)
1293 printf ("%s:", bfd_get_filename (archive_bfd));
1294 printf ("%s:", bfd_get_filename (abfd));
1298 static void
1299 print_symbol_filename_sysv (bfd *archive_bfd, bfd *abfd)
1301 if (filename_per_symbol)
1303 if (archive_bfd)
1304 printf ("%s:", bfd_get_filename (archive_bfd));
1305 printf ("%s:", bfd_get_filename (abfd));
1309 static void
1310 print_symbol_filename_posix (bfd *archive_bfd, bfd *abfd)
1312 if (filename_per_symbol)
1314 if (archive_bfd)
1315 printf ("%s[%s]: ", bfd_get_filename (archive_bfd),
1316 bfd_get_filename (abfd));
1317 else
1318 printf ("%s: ", bfd_get_filename (abfd));
1322 /* Print a symbol value. */
1324 static void
1325 print_value (bfd *abfd ATTRIBUTE_UNUSED, bfd_vma val)
1327 #if ! defined (BFD64) || BFD_HOST_64BIT_LONG
1328 printf (value_format, val);
1329 #else
1330 /* We have a 64 bit value to print, but the host is only 32 bit. */
1331 if (print_radix == 16)
1332 bfd_fprintf_vma (abfd, stdout, val);
1333 else
1335 char buf[30];
1336 char *s;
1338 s = buf + sizeof buf;
1339 *--s = '\0';
1340 while (val > 0)
1342 *--s = (val % print_radix) + '0';
1343 val /= print_radix;
1345 while ((buf + sizeof buf - 1) - s < 16)
1346 *--s = '0';
1347 printf ("%s", s);
1349 #endif
1352 /* Print a line of information about a symbol. */
1354 static void
1355 print_symbol_info_bsd (struct extended_symbol_info *info, bfd *abfd)
1357 if (bfd_is_undefined_symclass (SYM_TYPE (info)))
1359 if (print_width == 16)
1360 printf (" ");
1361 printf (" ");
1363 else
1365 /* Normally we print the value of the symbol. If we are printing the
1366 size or sorting by size then we print its size, except for the
1367 (weird) special case where both flags are defined, in which case we
1368 print both values. This conforms to documented behaviour. */
1369 if (sort_by_size && !print_size)
1370 print_value (abfd, SYM_SIZE (info));
1371 else
1372 print_value (abfd, SYM_VALUE (info));
1374 if (print_size && SYM_SIZE (info))
1376 printf (" ");
1377 print_value (abfd, SYM_SIZE (info));
1381 printf (" %c", SYM_TYPE (info));
1383 if (SYM_TYPE (info) == '-')
1385 /* A stab. */
1386 printf (" ");
1387 printf (other_format, SYM_STAB_OTHER (info));
1388 printf (" ");
1389 printf (desc_format, SYM_STAB_DESC (info));
1390 printf (" %5s", SYM_STAB_NAME (info));
1392 print_symname (" %s", SYM_NAME (info), abfd);
1395 static void
1396 print_symbol_info_sysv (struct extended_symbol_info *info, bfd *abfd)
1398 print_symname ("%-20s|", SYM_NAME (info), abfd);
1400 if (bfd_is_undefined_symclass (SYM_TYPE (info)))
1402 if (print_width == 8)
1403 printf (" ");
1404 else
1405 printf (" ");
1407 else
1408 print_value (abfd, SYM_VALUE (info));
1410 printf ("| %c |", SYM_TYPE (info));
1412 if (SYM_TYPE (info) == '-')
1414 /* A stab. */
1415 printf ("%18s| ", SYM_STAB_NAME (info)); /* (C) Type. */
1416 printf (desc_format, SYM_STAB_DESC (info)); /* Size. */
1417 printf ("| |"); /* Line, Section. */
1419 else
1421 /* Type, Size, Line, Section */
1422 if (info->elfinfo)
1423 printf ("%18s|",
1424 get_symbol_type (ELF_ST_TYPE (info->elfinfo->internal_elf_sym.st_info)));
1425 else
1426 printf (" |");
1428 if (SYM_SIZE (info))
1429 print_value (abfd, SYM_SIZE (info));
1430 else
1432 if (print_width == 8)
1433 printf (" ");
1434 else
1435 printf (" ");
1438 if (info->elfinfo)
1439 printf("| |%s", info->elfinfo->symbol.section->name);
1440 else
1441 printf("| |");
1445 static void
1446 print_symbol_info_posix (struct extended_symbol_info *info, bfd *abfd)
1448 print_symname ("%s ", SYM_NAME (info), abfd);
1449 printf ("%c ", SYM_TYPE (info));
1451 if (bfd_is_undefined_symclass (SYM_TYPE (info)))
1452 printf (" ");
1453 else
1455 print_value (abfd, SYM_VALUE (info));
1456 printf (" ");
1457 if (SYM_SIZE (info))
1458 print_value (abfd, SYM_SIZE (info));
1463 main (int argc, char **argv)
1465 int c;
1466 int retval;
1468 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
1469 setlocale (LC_MESSAGES, "");
1470 #endif
1471 #if defined (HAVE_SETLOCALE)
1472 setlocale (LC_CTYPE, "");
1473 setlocale (LC_COLLATE, "");
1474 #endif
1475 bindtextdomain (PACKAGE, LOCALEDIR);
1476 textdomain (PACKAGE);
1478 program_name = *argv;
1479 xmalloc_set_program_name (program_name);
1481 START_PROGRESS (program_name, 0);
1483 bfd_init ();
1484 set_default_bfd_target ();
1486 while ((c = getopt_long (argc, argv, "aABCDef:gHhlnopPrSst:uvVvX:",
1487 long_options, (int *) 0)) != EOF)
1489 switch (c)
1491 case 'a':
1492 print_debug_syms = 1;
1493 break;
1494 case 'A':
1495 case 'o':
1496 filename_per_symbol = 1;
1497 break;
1498 case 'B': /* For MIPS compatibility. */
1499 set_output_format ("bsd");
1500 break;
1501 case 'C':
1502 do_demangle = 1;
1503 if (optarg != NULL)
1505 enum demangling_styles style;
1507 style = cplus_demangle_name_to_style (optarg);
1508 if (style == unknown_demangling)
1509 fatal (_("unknown demangling style `%s'"),
1510 optarg);
1512 cplus_demangle_set_style (style);
1514 break;
1515 case 'D':
1516 dynamic = 1;
1517 break;
1518 case 'e':
1519 /* Ignored for HP/UX compatibility. */
1520 break;
1521 case 'f':
1522 set_output_format (optarg);
1523 break;
1524 case 'g':
1525 external_only = 1;
1526 break;
1527 case 'H':
1528 case 'h':
1529 usage (stdout, 0);
1530 case 'l':
1531 line_numbers = 1;
1532 break;
1533 case 'n':
1534 case 'v':
1535 sort_numerically = 1;
1536 break;
1537 case 'p':
1538 no_sort = 1;
1539 break;
1540 case 'P':
1541 set_output_format ("posix");
1542 break;
1543 case 'r':
1544 reverse_sort = 1;
1545 break;
1546 case 's':
1547 print_armap = 1;
1548 break;
1549 case 'S':
1550 print_size = 1;
1551 break;
1552 case 't':
1553 set_print_radix (optarg);
1554 break;
1555 case 'u':
1556 undefined_only = 1;
1557 break;
1558 case 'V':
1559 show_version = 1;
1560 break;
1561 case 'X':
1562 /* Ignored for (partial) AIX compatibility. On AIX, the
1563 argument has values 32, 64, or 32_64, and specifies that
1564 only 32-bit, only 64-bit, or both kinds of objects should
1565 be examined. The default is 32. So plain AIX nm on a
1566 library archive with both kinds of objects will ignore
1567 the 64-bit ones. For GNU nm, the default is and always
1568 has been -X 32_64, and other options are not supported. */
1569 if (strcmp (optarg, "32_64") != 0)
1570 fatal (_("Only -X 32_64 is supported"));
1571 break;
1573 case OPTION_TARGET: /* --target */
1574 target = optarg;
1575 break;
1577 case 0: /* A long option that just sets a flag. */
1578 break;
1580 default:
1581 usage (stderr, 1);
1585 if (show_version)
1586 print_version ("nm");
1588 if (sort_by_size && undefined_only)
1590 non_fatal (_("Using the --size-sort and --undefined-only options together"));
1591 non_fatal (_("will produce no output, since undefined symbols have no size."));
1592 return 0;
1595 /* OK, all options now parsed. If no filename specified, do a.out. */
1596 if (optind == argc)
1597 return !display_file ("a.out");
1599 retval = 0;
1601 if (argc - optind > 1)
1602 filename_per_file = 1;
1604 /* We were given several filenames to do. */
1605 while (optind < argc)
1607 PROGRESS (1);
1608 if (!display_file (argv[optind++]))
1609 retval++;
1612 END_PROGRESS (program_name);
1614 #ifdef HAVE_SBRK
1615 if (show_stats)
1617 char *lim = (char *) sbrk (0);
1619 non_fatal (_("data size %ld"), (long) (lim - (char *) &environ));
1621 #endif
1623 exit (retval);
1624 return retval;