1 /* nm.c -- Describe symbol table of a rel file.
2 Copyright 1991, 92, 93, 94 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
24 #include "aout/stab_gnu.h"
25 #include "aout/ranlib.h"
29 display_file
PARAMS ((char *filename
));
32 display_rel_file
PARAMS ((bfd
* file
, bfd
* archive
));
35 filter_symbols
PARAMS ((bfd
* file
, asymbol
** syms
, unsigned long symcount
));
38 print_symbols
PARAMS ((bfd
* file
, asymbol
** syms
, unsigned long symcount
,
42 print_symdef_entry
PARAMS ((bfd
* abfd
));
45 /* The output formatting functions. */
48 print_object_filename_bsd
PARAMS ((char *filename
));
51 print_object_filename_sysv
PARAMS ((char *filename
));
54 print_object_filename_posix
PARAMS ((char *filename
));
58 print_archive_filename_bsd
PARAMS ((char *filename
));
61 print_archive_filename_sysv
PARAMS ((char *filename
));
64 print_archive_filename_posix
PARAMS ((char *filename
));
68 print_archive_member_bsd
PARAMS ((char *archive
, CONST
char *filename
));
71 print_archive_member_sysv
PARAMS ((char *archive
, CONST
char *filename
));
74 print_archive_member_posix
PARAMS ((char *archive
, CONST
char *filename
));
78 print_symbol_filename_bsd
PARAMS ((bfd
* archive_bfd
, bfd
* abfd
));
81 print_symbol_filename_sysv
PARAMS ((bfd
* archive_bfd
, bfd
* abfd
));
84 print_symbol_filename_posix
PARAMS ((bfd
* archive_bfd
, bfd
* abfd
));
88 print_symbol_info_bsd
PARAMS ((symbol_info
* info
, bfd
* abfd
));
91 print_symbol_info_sysv
PARAMS ((symbol_info
* info
, bfd
* abfd
));
94 print_symbol_info_posix
PARAMS ((symbol_info
* info
, bfd
* abfd
));
97 /* Support for different output formats. */
100 /* Print the name of an object file given on the command line. */
101 void (*print_object_filename
) PARAMS ((char *filename
));
103 /* Print the name of an archive file given on the command line. */
104 void (*print_archive_filename
) PARAMS ((char *filename
));
106 /* Print the name of an archive member file. */
107 void (*print_archive_member
) PARAMS ((char *archive
, CONST
char *filename
));
109 /* Print the name of the file (and archive, if there is one)
110 containing a symbol. */
111 void (*print_symbol_filename
) PARAMS ((bfd
* archive_bfd
, bfd
* abfd
));
113 /* Print a line of information about a symbol. */
114 void (*print_symbol_info
) PARAMS ((symbol_info
* info
, bfd
* abfd
));
116 static struct output_fns formats
[] =
118 {print_object_filename_bsd
,
119 print_archive_filename_bsd
,
120 print_archive_member_bsd
,
121 print_symbol_filename_bsd
,
122 print_symbol_info_bsd
},
123 {print_object_filename_sysv
,
124 print_archive_filename_sysv
,
125 print_archive_member_sysv
,
126 print_symbol_filename_sysv
,
127 print_symbol_info_sysv
},
128 {print_object_filename_posix
,
129 print_archive_filename_posix
,
130 print_archive_member_posix
,
131 print_symbol_filename_posix
,
132 print_symbol_info_posix
}
135 /* Indices in `formats'. */
137 #define FORMAT_SYSV 1
138 #define FORMAT_POSIX 2
139 #define FORMAT_DEFAULT FORMAT_BSD
141 /* The output format to use. */
142 static struct output_fns
*format
= &formats
[FORMAT_DEFAULT
];
145 /* Command options. */
147 static int do_demangle
= 0; /* Pretty print C++ symbol names. */
148 static int external_only
= 0; /* print external symbols only */
149 static int no_sort
= 0; /* don't sort; print syms in order found */
150 static int print_debug_syms
= 0; /* print debugger-only symbols too */
151 static int print_armap
= 0; /* describe __.SYMDEF data in archive files. */
152 static int reverse_sort
= 0; /* sort in downward(alpha or numeric) order */
153 static int sort_numerically
= 0; /* sort in numeric rather than alpha order */
154 static int undefined_only
= 0; /* print undefined symbols only */
155 static int show_version
= 0; /* show the version number */
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. */
163 static char value_format
[] = "%08x%08x";
165 static char value_format
[] = "%08lx";
167 /* Print formats for printing stab info. */
168 static char other_format
[] = "%02x";
169 static char desc_format
[] = "%04x";
172 extern char *program_name
;
173 extern char *program_version
;
175 extern int print_version
;
177 static struct option long_options
[] =
179 {"debug-syms", no_argument
, &print_debug_syms
, 1},
180 {"demangle", no_argument
, &do_demangle
, 1},
181 {"extern-only", no_argument
, &external_only
, 1},
182 {"format", required_argument
, 0, 'f'},
183 {"help", no_argument
, 0, 'h'},
184 {"no-sort", no_argument
, &no_sort
, 1},
185 {"numeric-sort", no_argument
, &sort_numerically
, 1},
186 {"portability", no_argument
, 0, 'P'},
187 {"print-armap", no_argument
, &print_armap
, 1},
188 {"print-file-name", no_argument
, 0, 'o'},
189 {"radix", required_argument
, 0, 't'},
190 {"reverse-sort", no_argument
, &reverse_sort
, 1},
191 {"target", required_argument
, 0, 200},
192 {"undefined-only", no_argument
, &undefined_only
, 1},
193 {"version", no_argument
, &show_version
, 1},
194 {0, no_argument
, 0, 0}
197 /* Some error-reporting functions */
200 usage (stream
, status
)
205 Usage: %s [-aABCgnopPrsuvV] [-t radix] [--radix=radix] [--target=bfdname]\n\
206 [--debug-syms] [--extern-only] [--print-armap] [--print-file-name]\n\
207 [--numeric-sort] [--no-sort] [--reverse-sort] [--undefined-only]\n\
208 [--portability] [-f {bsd,sysv,posix}] [--format={bsd,sysv,posix}]\n\
209 [--demangle] [--version] [--help] [file...]\n",
214 /* Set the radix for the symbol value and size according to RADIX. */
217 set_print_radix (radix
)
226 value_format
[3] = value_format
[7] = *radix
;
228 value_format
[4] = *radix
;
230 other_format
[3] = desc_format
[3] = *radix
;
233 fprintf (stderr
, "%s: %s: invalid radix\n", program_name
, radix
);
239 set_output_format (f
)
259 fprintf (stderr
, "%s: %s: invalid output format\n", program_name
, f
);
262 format
= &formats
[i
];
273 program_name
= *argv
;
274 xmalloc_set_program_name (program_name
);
278 while ((c
= getopt_long (argc
, argv
, "aABCf:gnopPrst:uvV", long_options
, (int *) 0)) != EOF
)
283 print_debug_syms
= 1;
287 filename_per_symbol
= 1;
289 case 'B': /* For MIPS compatibility. */
290 set_output_format ("bsd");
296 set_output_format (optarg
);
305 sort_numerically
= 1;
311 set_output_format ("posix");
320 set_print_radix (optarg
);
329 case 200: /* --target */
333 case 0: /* A long option that just sets a flag. */
343 printf ("GNU %s version %s\n", program_name
, program_version
);
347 /* OK, all options now parsed. If no filename specified, do a.out. */
349 return !display_file ("a.out");
353 if (argc
- optind
> 1)
354 filename_per_file
= 1;
356 /* We were given several filenames to do. */
357 while (optind
< argc
)
359 if (!display_file (argv
[optind
++]))
368 display_archive (file
)
374 (*format
->print_archive_filename
) (bfd_get_filename (file
));
377 print_symdef_entry (file
);
381 arfile
= bfd_openr_next_archived_file (file
, arfile
);
385 if (bfd_get_error () != bfd_error_no_more_archived_files
)
386 bfd_fatal (bfd_get_filename (file
));
390 if (bfd_check_format_matches (arfile
, bfd_object
, &matching
))
392 (*format
->print_archive_member
) (bfd_get_filename (file
),
393 bfd_get_filename (arfile
));
394 display_rel_file (arfile
, file
);
398 bfd_nonfatal (bfd_get_filename (arfile
));
399 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
401 list_matching_formats (matching
);
411 display_file (filename
)
414 boolean retval
= true;
418 file
= bfd_openr (filename
, target
);
421 bfd_nonfatal (filename
);
425 if (bfd_check_format (file
, bfd_archive
))
427 display_archive (file
);
429 else if (bfd_check_format_matches (file
, bfd_object
, &matching
))
431 (*format
->print_object_filename
) (filename
);
432 display_rel_file (file
, NULL
);
436 bfd_nonfatal (filename
);
437 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
439 list_matching_formats (matching
);
445 if (bfd_close (file
) == false)
446 bfd_fatal (filename
);
451 /* Symbol-sorting predicates */
452 #define valueof(x) ((x)->section->vma + (x)->value)
454 numeric_forward (x
, y
)
458 return (valueof (*(asymbol
**) x
) - valueof (*(asymbol
**) y
));
462 numeric_reverse (x
, y
)
466 return (valueof (*(asymbol
**) y
) - valueof (*(asymbol
**) x
));
470 non_numeric_forward (x
, y
)
474 CONST
char *xn
= (*(asymbol
**) x
)->name
;
475 CONST
char *yn
= (*(asymbol
**) y
)->name
;
477 return ((xn
== NULL
) ? ((yn
== NULL
) ? 0 : -1) :
478 ((yn
== NULL
) ? 1 : strcmp (xn
, yn
)));
482 non_numeric_reverse (x
, y
)
486 return -(non_numeric_forward (x
, y
));
489 static int (*(sorters
[2][2])) PARAMS ((CONST
void *, CONST
void *)) =
491 { non_numeric_forward
, non_numeric_reverse
},
492 { numeric_forward
, numeric_reverse
}
495 /* If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD. */
498 display_rel_file (abfd
, archive_bfd
)
502 unsigned int storage
;
504 unsigned int symcount
= 0;
506 if (!(bfd_get_file_flags (abfd
) & HAS_SYMS
))
508 printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd
));
512 storage
= get_symtab_upper_bound (abfd
);
516 fprintf (stderr
, "%s: Symflags set but there are none?\n",
517 bfd_get_filename (abfd
));
521 syms
= (asymbol
**) xmalloc (storage
);
523 symcount
= bfd_canonicalize_symtab (abfd
, syms
);
530 /* Discard the symbols we don't want to print.
531 It's OK to do this in place; we'll free the storage anyway
534 symcount
= filter_symbols (abfd
, syms
, symcount
);
537 qsort ((char *) syms
, symcount
, sizeof (asymbol
*),
538 sorters
[sort_numerically
][reverse_sort
]);
540 print_symbols (abfd
, syms
, symcount
, archive_bfd
);
544 /* Choose which symbol entries to print;
545 compact them downward to get rid of the rest.
546 Return the number of symbols to be printed. */
549 filter_symbols (abfd
, syms
, symcount
)
550 bfd
*abfd
; /* Unused. */
552 unsigned long symcount
;
554 asymbol
**from
, **to
;
555 unsigned int src_count
;
556 unsigned int dst_count
= 0;
559 for (from
= to
= syms
, src_count
= 0; src_count
< symcount
; src_count
++)
562 flagword flags
= (from
[src_count
])->flags
;
564 sym
= from
[src_count
];
566 keep
= sym
->section
== &bfd_und_section
;
567 else if (external_only
)
568 keep
= ((flags
& BSF_GLOBAL
)
569 || (sym
->section
== &bfd_und_section
)
570 || (bfd_is_com_section (sym
->section
)));
574 if (!print_debug_syms
&& ((flags
& BSF_DEBUGGING
) != 0))
578 to
[dst_count
++] = from
[src_count
];
584 /* Print symbol name NAME, read from ABFD, with printf format FORMAT,
585 demangling it if requested. */
588 print_symname (format
, name
, abfd
)
596 /* In this mode, give a user-level view of the symbol name
597 even if it's not mangled; strip off any leading
599 if (bfd_get_symbol_leading_char (abfd
) == name
[0])
602 res
= cplus_demangle (name
, DMGL_ANSI
| DMGL_PARAMS
);
605 printf (format
, res
);
611 printf (format
, name
);
614 /* If ARCHIVE_BFD is non-NULL, it is the archive containing ABFD. */
617 print_symbols (abfd
, syms
, symcount
, archive_bfd
)
620 unsigned long symcount
;
623 asymbol
**sym
= syms
, **end
= syms
+ symcount
;
626 for (; sym
< end
; ++sym
)
628 (*format
->print_symbol_filename
) (archive_bfd
, abfd
);
632 if ((*sym
)->section
== &bfd_und_section
)
634 print_symname ("%s\n", (*sym
)->name
, abfd
);
642 bfd_get_symbol_info (abfd
, p
, &syminfo
);
643 (*format
->print_symbol_info
) (&syminfo
, abfd
);
650 /* The following 3 groups of functions are called unconditionally,
651 once at the start of processing each file of the appropriate type.
652 They should check `filename_per_file' and `filename_per_symbol',
653 as appropriate for their output format, to determine whether to
656 /* Print the name of an object file given on the command line. */
659 print_object_filename_bsd (filename
)
662 if (filename_per_file
&& !filename_per_symbol
)
663 printf ("\n%s:\n", filename
);
667 print_object_filename_sysv (filename
)
671 printf ("\n\nUndefined symbols from %s:\n\n", filename
);
673 printf ("\n\nSymbols from %s:\n\n", filename
);
675 Name Value Class Type Size Line Section\n\n");
679 print_object_filename_posix (filename
)
682 if (filename_per_file
&& !filename_per_symbol
)
683 printf ("%s:\n", filename
);
686 /* Print the name of an archive file given on the command line. */
689 print_archive_filename_bsd (filename
)
692 if (filename_per_file
)
693 printf ("\n%s:\n", filename
);
697 print_archive_filename_sysv (filename
)
703 print_archive_filename_posix (filename
)
708 /* Print the name of an archive member file. */
711 print_archive_member_bsd (archive
, filename
)
713 CONST
char *filename
;
715 if (!filename_per_symbol
)
716 printf ("\n%s:\n", filename
);
720 print_archive_member_sysv (archive
, filename
)
722 CONST
char *filename
;
725 printf ("\n\nUndefined symbols from %s[%s]:\n\n", archive
, filename
);
727 printf ("\n\nSymbols from %s[%s]:\n\n", archive
, filename
);
729 Name Value Class Type Size Line Section\n\n");
733 print_archive_member_posix (archive
, filename
)
735 CONST
char *filename
;
737 if (!filename_per_symbol
)
738 printf ("%s[%s]:\n", archive
, filename
);
741 /* Print the name of the file (and archive, if there is one)
742 containing a symbol. */
745 print_symbol_filename_bsd (archive_bfd
, abfd
)
746 bfd
*archive_bfd
, *abfd
;
748 if (filename_per_symbol
)
751 printf ("%s:", bfd_get_filename (archive_bfd
));
752 printf ("%s:", bfd_get_filename (abfd
));
757 print_symbol_filename_sysv (archive_bfd
, abfd
)
758 bfd
*archive_bfd
, *abfd
;
760 if (filename_per_symbol
)
763 printf ("%s:", bfd_get_filename (archive_bfd
));
764 printf ("%s:", bfd_get_filename (abfd
));
769 print_symbol_filename_posix (archive_bfd
, abfd
)
770 bfd
*archive_bfd
, *abfd
;
772 if (filename_per_symbol
)
775 printf ("%s[%s]: ", bfd_get_filename (archive_bfd
),
776 bfd_get_filename (abfd
));
778 printf ("%s: ", bfd_get_filename (abfd
));
782 /* Print a line of information about a symbol. */
785 print_symbol_info_bsd (info
, abfd
)
789 if (info
->type
== 'U')
794 printf (value_format
, uint64_typeHIGH (info
->value
),
795 uint64_typeLOW (info
->value
));
797 printf (value_format
, info
->value
);
800 printf (" %c", info
->type
);
801 if (info
->type
== '-')
805 printf (other_format
, info
->stab_other
);
807 printf (desc_format
, info
->stab_desc
);
808 printf (" %5s", info
->stab_name
);
810 print_symname (" %s", info
->name
, abfd
);
814 print_symbol_info_sysv (info
, abfd
)
818 print_symname ("%-20s|", info
->name
, abfd
); /* Name */
819 if (info
->type
== 'U')
820 printf (" "); /* Value */
824 printf (value_format
, uint64_typeHIGH (info
->value
),
825 uint64_typeLOW (info
->value
));
827 printf (value_format
, info
->value
);
830 printf ("| %c |", info
->type
); /* Class */
831 if (info
->type
== '-')
834 printf ("%18s| ", info
->stab_name
); /* (C) Type */
835 printf (desc_format
, info
->stab_desc
); /* Size */
836 printf ("| |"); /* Line, Section */
839 printf (" | | |"); /* Type, Size, Line, Section */
843 print_symbol_info_posix (info
, abfd
)
847 print_symname ("%s ", info
->name
, abfd
);
848 printf ("%c ", info
->type
);
849 if (info
->type
== 'U')
854 printf (value_format
, uint64_typeHIGH (info
->value
),
855 uint64_typeLOW (info
->value
));
857 printf (value_format
, info
->value
);
860 /* POSIX.2 wants the symbol size printed here, when applicable;
861 BFD currently doesn't provide it, so we take the easy way out by
862 considering it to never be applicable. */
866 print_symdef_entry (abfd
)
869 symindex idx
= BFD_NO_MORE_SYMBOLS
;
871 boolean everprinted
= false;
873 for (idx
= bfd_get_next_mapent (abfd
, idx
, &thesym
);
874 idx
!= BFD_NO_MORE_SYMBOLS
;
875 idx
= bfd_get_next_mapent (abfd
, idx
, &thesym
))
880 printf ("\nArchive index:\n");
883 elt
= bfd_get_elt_at_index (abfd
, idx
);
884 if (thesym
->name
!= (char *) NULL
)
886 printf ("%s in %s\n", thesym
->name
, bfd_get_filename (elt
));