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