1999-09-12 Donn Terry <donn@interix.com>
[binutils.git] / binutils / objdump.c
blob5b36ae224a886a7d1ae383048024325a324d3e9c
1 /* objdump.c -- dump information about an object file.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 97, 98, 1999
3 Free Software Foundation, Inc.
5 This file is part of GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include "bfd.h"
22 #include "getopt.h"
23 #include "progress.h"
24 #include "bucomm.h"
25 #include <ctype.h>
26 #include "dis-asm.h"
27 #include "libiberty.h"
28 #include "demangle.h"
29 #include "debug.h"
30 #include "budbg.h"
32 #ifdef ANSI_PROTOTYPES
33 #include <stdarg.h>
34 #else
35 #include <varargs.h>
36 #endif
38 /* Internal headers for the ELF .stab-dump code - sorry. */
39 #define BYTES_IN_WORD 32
40 #include "aout/aout64.h"
42 #ifdef NEED_DECLARATION_FPRINTF
43 /* This is needed by INIT_DISASSEMBLE_INFO. */
44 extern int fprintf PARAMS ((FILE *, const char *, ...));
45 #endif
47 /* Exit status. */
48 static int exit_status = 0;
50 static char *default_target = NULL; /* default at runtime */
52 static int show_version = 0; /* show the version number */
53 static int dump_section_contents; /* -s */
54 static int dump_section_headers; /* -h */
55 static boolean dump_file_header; /* -f */
56 static int dump_symtab; /* -t */
57 static int dump_dynamic_symtab; /* -T */
58 static int dump_reloc_info; /* -r */
59 static int dump_dynamic_reloc_info; /* -R */
60 static int dump_ar_hdrs; /* -a */
61 static int dump_private_headers; /* -p */
62 static int prefix_addresses; /* --prefix-addresses */
63 static int with_line_numbers; /* -l */
64 static boolean with_source_code; /* -S */
65 static int show_raw_insn; /* --show-raw-insn */
66 static int dump_stab_section_info; /* --stabs */
67 static int do_demangle; /* -C, --demangle */
68 static boolean disassemble; /* -d */
69 static boolean disassemble_all; /* -D */
70 static int disassemble_zeroes; /* --disassemble-zeroes */
71 static boolean formats_info; /* -i */
72 static char *only; /* -j secname */
73 static int wide_output; /* -w */
74 static bfd_vma start_address = (bfd_vma) -1; /* --start-address */
75 static bfd_vma stop_address = (bfd_vma) -1; /* --stop-address */
76 static int dump_debugging; /* --debugging */
77 static bfd_vma adjust_section_vma = 0; /* --adjust-vma */
79 /* Extra info to pass to the disassembler address printing function. */
80 struct objdump_disasm_info {
81 bfd *abfd;
82 asection *sec;
83 boolean require_sec;
86 /* Architecture to disassemble for, or default if NULL. */
87 static char *machine = (char *) NULL;
89 /* Target specific options to the disassembler. */
90 static char *disassembler_options = (char *) NULL;
92 /* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */
93 static enum bfd_endian endian = BFD_ENDIAN_UNKNOWN;
95 /* The symbol table. */
96 static asymbol **syms;
98 /* Number of symbols in `syms'. */
99 static long symcount = 0;
101 /* The sorted symbol table. */
102 static asymbol **sorted_syms;
104 /* Number of symbols in `sorted_syms'. */
105 static long sorted_symcount = 0;
107 /* The dynamic symbol table. */
108 static asymbol **dynsyms;
110 /* Number of symbols in `dynsyms'. */
111 static long dynsymcount = 0;
113 /* Static declarations. */
115 static void
116 usage PARAMS ((FILE *, int));
118 static void
119 nonfatal PARAMS ((const char *));
121 static void
122 display_file PARAMS ((char *filename, char *target));
124 static void
125 dump_section_header PARAMS ((bfd *, asection *, PTR));
127 static void
128 dump_headers PARAMS ((bfd *));
130 static void
131 dump_data PARAMS ((bfd *abfd));
133 static void
134 dump_relocs PARAMS ((bfd *abfd));
136 static void
137 dump_dynamic_relocs PARAMS ((bfd * abfd));
139 static void
140 dump_reloc_set PARAMS ((bfd *, asection *, arelent **, long));
142 static void
143 dump_symbols PARAMS ((bfd *abfd, boolean dynamic));
145 static void
146 dump_bfd_header PARAMS ((bfd *));
148 static void
149 dump_bfd_private_header PARAMS ((bfd *));
151 static void
152 display_bfd PARAMS ((bfd *abfd));
154 static void
155 display_target_list PARAMS ((void));
157 static void
158 display_info_table PARAMS ((int, int));
160 static void
161 display_target_tables PARAMS ((void));
163 static void
164 display_info PARAMS ((void));
166 static void
167 objdump_print_value PARAMS ((bfd_vma, struct disassemble_info *, boolean));
169 static void
170 objdump_print_symname PARAMS ((bfd *, struct disassemble_info *, asymbol *));
172 static asymbol *
173 find_symbol_for_address PARAMS ((bfd *, asection *, bfd_vma, boolean, long *));
175 static void
176 objdump_print_addr_with_sym PARAMS ((bfd *, asection *, asymbol *, bfd_vma,
177 struct disassemble_info *, boolean));
179 static void
180 objdump_print_addr PARAMS ((bfd_vma, struct disassemble_info *, boolean));
182 static void
183 objdump_print_address PARAMS ((bfd_vma, struct disassemble_info *));
185 static void
186 show_line PARAMS ((bfd *, asection *, bfd_vma));
188 static void
189 disassemble_bytes PARAMS ((struct disassemble_info *, disassembler_ftype,
190 boolean, bfd_byte *, bfd_vma, bfd_vma,
191 arelent ***, arelent **));
193 static void
194 disassemble_data PARAMS ((bfd *));
196 static const char *
197 endian_string PARAMS ((enum bfd_endian));
199 static asymbol **
200 slurp_symtab PARAMS ((bfd *));
202 static asymbol **
203 slurp_dynamic_symtab PARAMS ((bfd *));
205 static long
206 remove_useless_symbols PARAMS ((asymbol **, long));
208 static int
209 compare_symbols PARAMS ((const PTR, const PTR));
211 static int
212 compare_relocs PARAMS ((const PTR, const PTR));
214 static void
215 dump_stabs PARAMS ((bfd *));
217 static boolean
218 read_section_stabs PARAMS ((bfd *, const char *, const char *));
220 static void
221 print_section_stabs PARAMS ((bfd *, const char *, const char *));
223 static void
224 usage (stream, status)
225 FILE *stream;
226 int status;
228 fprintf (stream, _("\
229 Usage: %s [-ahifCdDprRtTxsSlw] [-b bfdname] [-m machine] \n\
230 [-j section-name] [-M disassembler-options]\n\
231 [--archive-headers] [--target=bfdname] [--debugging] [--disassemble]\n\
232 [--disassemble-all] [--disassemble-zeroes] [--file-headers]\n\
233 [--section-headers] [--headers]\n\
234 [--info] [--section=section-name] [--line-numbers] [--source]\n"),
235 program_name);
236 fprintf (stream, _("\
237 [--architecture=machine] [--reloc] [--full-contents] [--stabs]\n\
238 [--syms] [--all-headers] [--dynamic-syms] [--dynamic-reloc]\n\
239 [--wide] [--version] [--help] [--private-headers]\n\
240 [--start-address=addr] [--stop-address=addr]\n\
241 [--prefix-addresses] [--[no-]show-raw-insn] [--demangle]\n\
242 [--adjust-vma=offset] [-EB|-EL] [--endian={big|little}] objfile...\n\
243 at least one option besides -l (--line-numbers) must be given\n"));
244 list_supported_targets (program_name, stream);
245 if (status == 0)
246 fprintf (stream, _("Report bugs to bug-gnu-utils@gnu.org\n"));
247 exit (status);
250 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
252 #define OPTION_ENDIAN (150)
253 #define OPTION_START_ADDRESS (OPTION_ENDIAN + 1)
254 #define OPTION_STOP_ADDRESS (OPTION_START_ADDRESS + 1)
255 #define OPTION_ADJUST_VMA (OPTION_STOP_ADDRESS + 1)
257 static struct option long_options[]=
259 {"adjust-vma", required_argument, NULL, OPTION_ADJUST_VMA},
260 {"all-headers", no_argument, NULL, 'x'},
261 {"private-headers", no_argument, NULL, 'p'},
262 {"architecture", required_argument, NULL, 'm'},
263 {"archive-headers", no_argument, NULL, 'a'},
264 {"debugging", no_argument, &dump_debugging, 1},
265 {"demangle", no_argument, &do_demangle, 1},
266 {"disassemble", no_argument, NULL, 'd'},
267 {"disassemble-all", no_argument, NULL, 'D'},
268 {"disassembler-options", required_argument, NULL, 'M'},
269 {"disassemble-zeroes", no_argument, &disassemble_zeroes, 1},
270 {"dynamic-reloc", no_argument, NULL, 'R'},
271 {"dynamic-syms", no_argument, NULL, 'T'},
272 {"endian", required_argument, NULL, OPTION_ENDIAN},
273 {"file-headers", no_argument, NULL, 'f'},
274 {"full-contents", no_argument, NULL, 's'},
275 {"headers", no_argument, NULL, 'h'},
276 {"help", no_argument, NULL, 'H'},
277 {"info", no_argument, NULL, 'i'},
278 {"line-numbers", no_argument, NULL, 'l'},
279 {"no-show-raw-insn", no_argument, &show_raw_insn, -1},
280 {"prefix-addresses", no_argument, &prefix_addresses, 1},
281 {"reloc", no_argument, NULL, 'r'},
282 {"section", required_argument, NULL, 'j'},
283 {"section-headers", no_argument, NULL, 'h'},
284 {"show-raw-insn", no_argument, &show_raw_insn, 1},
285 {"source", no_argument, NULL, 'S'},
286 {"stabs", no_argument, &dump_stab_section_info, 1},
287 {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
288 {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
289 {"syms", no_argument, NULL, 't'},
290 {"target", required_argument, NULL, 'b'},
291 {"version", no_argument, &show_version, 1},
292 {"wide", no_argument, &wide_output, 'w'},
293 {0, no_argument, 0, 0}
296 static void
297 nonfatal (msg)
298 const char *msg;
300 bfd_nonfatal (msg);
301 exit_status = 1;
304 static void
305 dump_section_header (abfd, section, ignored)
306 bfd *abfd ATTRIBUTE_UNUSED;
307 asection *section;
308 PTR ignored ATTRIBUTE_UNUSED;
310 char *comma = "";
312 printf ("%3d %-13s %08lx ", section->index,
313 bfd_get_section_name (abfd, section),
314 (unsigned long) bfd_section_size (abfd, section));
315 printf_vma (bfd_get_section_vma (abfd, section));
316 printf (" ");
317 printf_vma (section->lma);
318 printf (" %08lx 2**%u", section->filepos,
319 bfd_get_section_alignment (abfd, section));
320 if (! wide_output)
321 printf ("\n ");
322 printf (" ");
324 #define PF(x, y) \
325 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
327 PF (SEC_HAS_CONTENTS, "CONTENTS");
328 PF (SEC_ALLOC, "ALLOC");
329 PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
330 PF (SEC_CONSTRUCTOR_TEXT, "CONSTRUCTOR TEXT");
331 PF (SEC_CONSTRUCTOR_DATA, "CONSTRUCTOR DATA");
332 PF (SEC_CONSTRUCTOR_BSS, "CONSTRUCTOR BSS");
333 PF (SEC_LOAD, "LOAD");
334 PF (SEC_RELOC, "RELOC");
335 #ifdef SEC_BALIGN
336 PF (SEC_BALIGN, "BALIGN");
337 #endif
338 PF (SEC_READONLY, "READONLY");
339 PF (SEC_CODE, "CODE");
340 PF (SEC_DATA, "DATA");
341 PF (SEC_ROM, "ROM");
342 PF (SEC_DEBUGGING, "DEBUGGING");
343 PF (SEC_NEVER_LOAD, "NEVER_LOAD");
344 PF (SEC_EXCLUDE, "EXCLUDE");
345 PF (SEC_SORT_ENTRIES, "SORT_ENTRIES");
346 PF (SEC_SMALL_DATA, "SMALL_DATA");
347 PF (SEC_SHARED, "SHARED");
349 if ((section->flags & SEC_LINK_ONCE) != 0)
351 const char *ls;
353 switch (section->flags & SEC_LINK_DUPLICATES)
355 default:
356 abort ();
357 case SEC_LINK_DUPLICATES_DISCARD:
358 ls = "LINK_ONCE_DISCARD";
359 break;
360 case SEC_LINK_DUPLICATES_ONE_ONLY:
361 ls = "LINK_ONCE_ONE_ONLY";
362 break;
363 case SEC_LINK_DUPLICATES_SAME_SIZE:
364 ls = "LINK_ONCE_SAME_SIZE";
365 break;
366 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
367 ls = "LINK_ONCE_SAME_CONTENTS";
368 break;
370 printf ("%s%s", comma, ls);
371 comma = ", ";
374 printf ("\n");
375 #undef PF
378 static void
379 dump_headers (abfd)
380 bfd *abfd;
382 printf (_("Sections:\n"));
383 #ifndef BFD64
384 printf (_("Idx Name Size VMA LMA File off Algn\n"));
385 #else
386 printf (_("Idx Name Size VMA LMA File off Algn\n"));
387 #endif
388 bfd_map_over_sections (abfd, dump_section_header, (PTR) NULL);
391 static asymbol **
392 slurp_symtab (abfd)
393 bfd *abfd;
395 asymbol **sy = (asymbol **) NULL;
396 long storage;
398 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
400 fprintf (stderr, _("%s: no symbols\n"), bfd_get_filename (abfd));
401 symcount = 0;
402 return NULL;
405 storage = bfd_get_symtab_upper_bound (abfd);
406 if (storage < 0)
407 bfd_fatal (bfd_get_filename (abfd));
409 if (storage)
411 sy = (asymbol **) xmalloc (storage);
413 symcount = bfd_canonicalize_symtab (abfd, sy);
414 if (symcount < 0)
415 bfd_fatal (bfd_get_filename (abfd));
416 if (symcount == 0)
417 fprintf (stderr, _("%s: no symbols\n"), bfd_get_filename (abfd));
418 return sy;
421 /* Read in the dynamic symbols. */
423 static asymbol **
424 slurp_dynamic_symtab (abfd)
425 bfd *abfd;
427 asymbol **sy = (asymbol **) NULL;
428 long storage;
430 storage = bfd_get_dynamic_symtab_upper_bound (abfd);
431 if (storage < 0)
433 if (!(bfd_get_file_flags (abfd) & DYNAMIC))
435 fprintf (stderr, _("%s: %s: not a dynamic object\n"),
436 program_name, bfd_get_filename (abfd));
437 dynsymcount = 0;
438 return NULL;
441 bfd_fatal (bfd_get_filename (abfd));
444 if (storage)
446 sy = (asymbol **) xmalloc (storage);
448 dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
449 if (dynsymcount < 0)
450 bfd_fatal (bfd_get_filename (abfd));
451 if (dynsymcount == 0)
452 fprintf (stderr, _("%s: %s: No dynamic symbols\n"),
453 program_name, bfd_get_filename (abfd));
454 return sy;
457 /* Filter out (in place) symbols that are useless for disassembly.
458 COUNT is the number of elements in SYMBOLS.
459 Return the number of useful symbols. */
461 static long
462 remove_useless_symbols (symbols, count)
463 asymbol **symbols;
464 long count;
466 register asymbol **in_ptr = symbols, **out_ptr = symbols;
468 while (--count >= 0)
470 asymbol *sym = *in_ptr++;
472 if (sym->name == NULL || sym->name[0] == '\0')
473 continue;
474 if (sym->flags & (BSF_DEBUGGING))
475 continue;
476 if (bfd_is_und_section (sym->section)
477 || bfd_is_com_section (sym->section))
478 continue;
480 *out_ptr++ = sym;
482 return out_ptr - symbols;
485 /* Sort symbols into value order. */
487 static int
488 compare_symbols (ap, bp)
489 const PTR ap;
490 const PTR bp;
492 const asymbol *a = *(const asymbol **)ap;
493 const asymbol *b = *(const asymbol **)bp;
494 const char *an, *bn;
495 size_t anl, bnl;
496 boolean af, bf;
497 flagword aflags, bflags;
499 if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
500 return 1;
501 else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
502 return -1;
504 if (a->section > b->section)
505 return 1;
506 else if (a->section < b->section)
507 return -1;
509 an = bfd_asymbol_name (a);
510 bn = bfd_asymbol_name (b);
511 anl = strlen (an);
512 bnl = strlen (bn);
514 /* The symbols gnu_compiled and gcc2_compiled convey no real
515 information, so put them after other symbols with the same value. */
517 af = (strstr (an, "gnu_compiled") != NULL
518 || strstr (an, "gcc2_compiled") != NULL);
519 bf = (strstr (bn, "gnu_compiled") != NULL
520 || strstr (bn, "gcc2_compiled") != NULL);
522 if (af && ! bf)
523 return 1;
524 if (! af && bf)
525 return -1;
527 /* We use a heuristic for the file name, to try to sort it after
528 more useful symbols. It may not work on non Unix systems, but it
529 doesn't really matter; the only difference is precisely which
530 symbol names get printed. */
532 #define file_symbol(s, sn, snl) \
533 (((s)->flags & BSF_FILE) != 0 \
534 || ((sn)[(snl) - 2] == '.' \
535 && ((sn)[(snl) - 1] == 'o' \
536 || (sn)[(snl) - 1] == 'a')))
538 af = file_symbol (a, an, anl);
539 bf = file_symbol (b, bn, bnl);
541 if (af && ! bf)
542 return 1;
543 if (! af && bf)
544 return -1;
546 /* Try to sort global symbols before local symbols before function
547 symbols before debugging symbols. */
549 aflags = a->flags;
550 bflags = b->flags;
552 if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING))
554 if ((aflags & BSF_DEBUGGING) != 0)
555 return 1;
556 else
557 return -1;
559 if ((aflags & BSF_FUNCTION) != (bflags & BSF_FUNCTION))
561 if ((aflags & BSF_FUNCTION) != 0)
562 return -1;
563 else
564 return 1;
566 if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL))
568 if ((aflags & BSF_LOCAL) != 0)
569 return 1;
570 else
571 return -1;
573 if ((aflags & BSF_GLOBAL) != (bflags & BSF_GLOBAL))
575 if ((aflags & BSF_GLOBAL) != 0)
576 return -1;
577 else
578 return 1;
581 /* Symbols that start with '.' might be section names, so sort them
582 after symbols that don't start with '.'. */
583 if (an[0] == '.' && bn[0] != '.')
584 return 1;
585 if (an[0] != '.' && bn[0] == '.')
586 return -1;
588 /* Finally, if we can't distinguish them in any other way, try to
589 get consistent results by sorting the symbols by name. */
590 return strcmp (an, bn);
593 /* Sort relocs into address order. */
595 static int
596 compare_relocs (ap, bp)
597 const PTR ap;
598 const PTR bp;
600 const arelent *a = *(const arelent **)ap;
601 const arelent *b = *(const arelent **)bp;
603 if (a->address > b->address)
604 return 1;
605 else if (a->address < b->address)
606 return -1;
608 /* So that associated relocations tied to the same address show up
609 in the correct order, we don't do any further sorting. */
610 if (a > b)
611 return 1;
612 else if (a < b)
613 return -1;
614 else
615 return 0;
618 /* Print VMA to STREAM. If SKIP_ZEROES is true, omit leading zeroes. */
620 static void
621 objdump_print_value (vma, info, skip_zeroes)
622 bfd_vma vma;
623 struct disassemble_info *info;
624 boolean skip_zeroes;
626 char buf[30];
627 char *p;
629 sprintf_vma (buf, vma);
630 if (! skip_zeroes)
631 p = buf;
632 else
634 for (p = buf; *p == '0'; ++p)
636 if (*p == '\0')
637 --p;
639 (*info->fprintf_func) (info->stream, "%s", p);
642 /* Print the name of a symbol. */
644 static void
645 objdump_print_symname (abfd, info, sym)
646 bfd *abfd;
647 struct disassemble_info *info;
648 asymbol *sym;
650 char *alloc;
651 const char *name;
652 const char *print;
654 alloc = NULL;
655 name = bfd_asymbol_name (sym);
656 if (! do_demangle || name[0] == '\0')
657 print = name;
658 else
660 /* Demangle the name. */
661 if (bfd_get_symbol_leading_char (abfd) == name[0])
662 ++name;
664 alloc = cplus_demangle (name, DMGL_ANSI | DMGL_PARAMS);
665 if (alloc == NULL)
666 print = name;
667 else
668 print = alloc;
671 if (info != NULL)
672 (*info->fprintf_func) (info->stream, "%s", print);
673 else
674 printf ("%s", print);
676 if (alloc != NULL)
677 free (alloc);
680 /* Locate a symbol given a bfd, a section, and a VMA. If REQUIRE_SEC
681 is true, then always require the symbol to be in the section. This
682 returns NULL if there is no suitable symbol. If PLACE is not NULL,
683 then *PLACE is set to the index of the symbol in sorted_syms. */
685 static asymbol *
686 find_symbol_for_address (abfd, sec, vma, require_sec, place)
687 bfd *abfd;
688 asection *sec;
689 bfd_vma vma;
690 boolean require_sec;
691 long *place;
693 /* @@ Would it speed things up to cache the last two symbols returned,
694 and maybe their address ranges? For many processors, only one memory
695 operand can be present at a time, so the 2-entry cache wouldn't be
696 constantly churned by code doing heavy memory accesses. */
698 /* Indices in `sorted_syms'. */
699 long min = 0;
700 long max = sorted_symcount;
701 long thisplace;
703 if (sorted_symcount < 1)
704 return NULL;
706 /* Perform a binary search looking for the closest symbol to the
707 required value. We are searching the range (min, max]. */
708 while (min + 1 < max)
710 asymbol *sym;
712 thisplace = (max + min) / 2;
713 sym = sorted_syms[thisplace];
715 if (bfd_asymbol_value (sym) > vma)
716 max = thisplace;
717 else if (bfd_asymbol_value (sym) < vma)
718 min = thisplace;
719 else
721 min = thisplace;
722 break;
726 /* The symbol we want is now in min, the low end of the range we
727 were searching. If there are several symbols with the same
728 value, we want the first one. */
729 thisplace = min;
730 while (thisplace > 0
731 && (bfd_asymbol_value (sorted_syms[thisplace])
732 == bfd_asymbol_value (sorted_syms[thisplace - 1])))
733 --thisplace;
735 /* If the file is relocateable, and the symbol could be from this
736 section, prefer a symbol from this section over symbols from
737 others, even if the other symbol's value might be closer.
739 Note that this may be wrong for some symbol references if the
740 sections have overlapping memory ranges, but in that case there's
741 no way to tell what's desired without looking at the relocation
742 table. */
744 if (sorted_syms[thisplace]->section != sec
745 && (require_sec
746 || ((abfd->flags & HAS_RELOC) != 0
747 && vma >= bfd_get_section_vma (abfd, sec)
748 && vma < (bfd_get_section_vma (abfd, sec)
749 + bfd_section_size (abfd, sec)))))
751 long i;
753 for (i = thisplace + 1; i < sorted_symcount; i++)
755 if (bfd_asymbol_value (sorted_syms[i])
756 != bfd_asymbol_value (sorted_syms[thisplace]))
757 break;
759 --i;
760 for (; i >= 0; i--)
762 if (sorted_syms[i]->section == sec
763 && (i == 0
764 || sorted_syms[i - 1]->section != sec
765 || (bfd_asymbol_value (sorted_syms[i])
766 != bfd_asymbol_value (sorted_syms[i - 1]))))
768 thisplace = i;
769 break;
773 if (sorted_syms[thisplace]->section != sec)
775 /* We didn't find a good symbol with a smaller value.
776 Look for one with a larger value. */
777 for (i = thisplace + 1; i < sorted_symcount; i++)
779 if (sorted_syms[i]->section == sec)
781 thisplace = i;
782 break;
787 if (sorted_syms[thisplace]->section != sec
788 && (require_sec
789 || ((abfd->flags & HAS_RELOC) != 0
790 && vma >= bfd_get_section_vma (abfd, sec)
791 && vma < (bfd_get_section_vma (abfd, sec)
792 + bfd_section_size (abfd, sec)))))
794 /* There is no suitable symbol. */
795 return NULL;
799 if (place != NULL)
800 *place = thisplace;
802 return sorted_syms[thisplace];
805 /* Print an address to INFO symbolically. */
807 static void
808 objdump_print_addr_with_sym (abfd, sec, sym, vma, info, skip_zeroes)
809 bfd *abfd;
810 asection *sec;
811 asymbol *sym;
812 bfd_vma vma;
813 struct disassemble_info *info;
814 boolean skip_zeroes;
816 objdump_print_value (vma, info, skip_zeroes);
818 if (sym == NULL)
820 bfd_vma secaddr;
822 (*info->fprintf_func) (info->stream, " <%s",
823 bfd_get_section_name (abfd, sec));
824 secaddr = bfd_get_section_vma (abfd, sec);
825 if (vma < secaddr)
827 (*info->fprintf_func) (info->stream, "-0x");
828 objdump_print_value (secaddr - vma, info, true);
830 else if (vma > secaddr)
832 (*info->fprintf_func) (info->stream, "+0x");
833 objdump_print_value (vma - secaddr, info, true);
835 (*info->fprintf_func) (info->stream, ">");
837 else
839 (*info->fprintf_func) (info->stream, " <");
840 objdump_print_symname (abfd, info, sym);
841 if (bfd_asymbol_value (sym) > vma)
843 (*info->fprintf_func) (info->stream, "-0x");
844 objdump_print_value (bfd_asymbol_value (sym) - vma, info, true);
846 else if (vma > bfd_asymbol_value (sym))
848 (*info->fprintf_func) (info->stream, "+0x");
849 objdump_print_value (vma - bfd_asymbol_value (sym), info, true);
851 (*info->fprintf_func) (info->stream, ">");
855 /* Print VMA to INFO, symbolically if possible. If SKIP_ZEROES is
856 true, don't output leading zeroes. */
858 static void
859 objdump_print_addr (vma, info, skip_zeroes)
860 bfd_vma vma;
861 struct disassemble_info *info;
862 boolean skip_zeroes;
864 struct objdump_disasm_info *aux;
865 asymbol *sym;
867 if (sorted_symcount < 1)
869 (*info->fprintf_func) (info->stream, "0x");
870 objdump_print_value (vma, info, skip_zeroes);
871 return;
874 aux = (struct objdump_disasm_info *) info->application_data;
875 sym = find_symbol_for_address (aux->abfd, aux->sec, vma, aux->require_sec,
876 (long *) NULL);
877 objdump_print_addr_with_sym (aux->abfd, aux->sec, sym, vma, info,
878 skip_zeroes);
881 /* Print VMA to INFO. This function is passed to the disassembler
882 routine. */
884 static void
885 objdump_print_address (vma, info)
886 bfd_vma vma;
887 struct disassemble_info *info;
889 objdump_print_addr (vma, info, ! prefix_addresses);
892 /* Determine of the given address has a symbol associated with it. */
894 static int
895 objdump_symbol_at_address (vma, info)
896 bfd_vma vma;
897 struct disassemble_info * info;
899 struct objdump_disasm_info * aux;
900 asymbol * sym;
902 /* No symbols - do not bother checking. */
903 if (sorted_symcount < 1)
904 return 0;
906 aux = (struct objdump_disasm_info *) info->application_data;
907 sym = find_symbol_for_address (aux->abfd, aux->sec, vma, aux->require_sec,
908 (long *) NULL);
910 return (sym != NULL && (bfd_asymbol_value (sym) == vma));
913 /* Hold the last function name and the last line number we displayed
914 in a disassembly. */
916 static char *prev_functionname;
917 static unsigned int prev_line;
919 /* We keep a list of all files that we have seen when doing a
920 dissassembly with source, so that we know how much of the file to
921 display. This can be important for inlined functions. */
923 struct print_file_list
925 struct print_file_list *next;
926 char *filename;
927 unsigned int line;
928 FILE *f;
931 static struct print_file_list *print_files;
933 /* The number of preceding context lines to show when we start
934 displaying a file for the first time. */
936 #define SHOW_PRECEDING_CONTEXT_LINES (5)
938 /* Skip ahead to a given line in a file, optionally printing each
939 line. */
941 static void
942 skip_to_line PARAMS ((struct print_file_list *, unsigned int, boolean));
944 static void
945 skip_to_line (p, line, show)
946 struct print_file_list *p;
947 unsigned int line;
948 boolean show;
950 while (p->line < line)
952 char buf[100];
954 if (fgets (buf, sizeof buf, p->f) == NULL)
956 fclose (p->f);
957 p->f = NULL;
958 break;
961 if (show)
962 printf ("%s", buf);
964 if (strchr (buf, '\n') != NULL)
965 ++p->line;
969 /* Show the line number, or the source line, in a dissassembly
970 listing. */
972 static void
973 show_line (abfd, section, off)
974 bfd *abfd;
975 asection *section;
976 bfd_vma off;
978 CONST char *filename;
979 CONST char *functionname;
980 unsigned int line;
982 if (! with_line_numbers && ! with_source_code)
983 return;
985 if (! bfd_find_nearest_line (abfd, section, syms, off, &filename,
986 &functionname, &line))
987 return;
989 if (filename != NULL && *filename == '\0')
990 filename = NULL;
991 if (functionname != NULL && *functionname == '\0')
992 functionname = NULL;
994 if (with_line_numbers)
996 if (functionname != NULL
997 && (prev_functionname == NULL
998 || strcmp (functionname, prev_functionname) != 0))
999 printf ("%s():\n", functionname);
1000 if (line > 0 && line != prev_line)
1001 printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
1004 if (with_source_code
1005 && filename != NULL
1006 && line > 0)
1008 struct print_file_list **pp, *p;
1010 for (pp = &print_files; *pp != NULL; pp = &(*pp)->next)
1011 if (strcmp ((*pp)->filename, filename) == 0)
1012 break;
1013 p = *pp;
1015 if (p != NULL)
1017 if (p != print_files)
1019 int l;
1021 /* We have reencountered a file name which we saw
1022 earlier. This implies that either we are dumping out
1023 code from an included file, or the same file was
1024 linked in more than once. There are two common cases
1025 of an included file: inline functions in a header
1026 file, and a bison or flex skeleton file. In the
1027 former case we want to just start printing (but we
1028 back up a few lines to give context); in the latter
1029 case we want to continue from where we left off. I
1030 can't think of a good way to distinguish the cases,
1031 so I used a heuristic based on the file name. */
1032 if (strcmp (p->filename + strlen (p->filename) - 2, ".h") != 0)
1033 l = p->line;
1034 else
1036 l = line - SHOW_PRECEDING_CONTEXT_LINES;
1037 if (l <= 0)
1038 l = 1;
1041 if (p->f == NULL)
1043 p->f = fopen (p->filename, "r");
1044 p->line = 0;
1046 if (p->f != NULL)
1047 skip_to_line (p, l, false);
1049 if (print_files->f != NULL)
1051 fclose (print_files->f);
1052 print_files->f = NULL;
1056 if (p->f != NULL)
1058 skip_to_line (p, line, true);
1059 *pp = p->next;
1060 p->next = print_files;
1061 print_files = p;
1064 else
1066 FILE *f;
1068 f = fopen (filename, "r");
1069 if (f != NULL)
1071 int l;
1073 p = ((struct print_file_list *)
1074 xmalloc (sizeof (struct print_file_list)));
1075 p->filename = xmalloc (strlen (filename) + 1);
1076 strcpy (p->filename, filename);
1077 p->line = 0;
1078 p->f = f;
1080 if (print_files != NULL && print_files->f != NULL)
1082 fclose (print_files->f);
1083 print_files->f = NULL;
1085 p->next = print_files;
1086 print_files = p;
1088 l = line - SHOW_PRECEDING_CONTEXT_LINES;
1089 if (l <= 0)
1090 l = 1;
1091 skip_to_line (p, l, false);
1092 if (p->f != NULL)
1093 skip_to_line (p, line, true);
1098 if (functionname != NULL
1099 && (prev_functionname == NULL
1100 || strcmp (functionname, prev_functionname) != 0))
1102 if (prev_functionname != NULL)
1103 free (prev_functionname);
1104 prev_functionname = xmalloc (strlen (functionname) + 1);
1105 strcpy (prev_functionname, functionname);
1108 if (line > 0 && line != prev_line)
1109 prev_line = line;
1112 /* Pseudo FILE object for strings. */
1113 typedef struct
1115 char *buffer;
1116 size_t size;
1117 char *current;
1118 } SFILE;
1120 /* sprintf to a "stream" */
1122 static int
1123 #ifdef ANSI_PROTOTYPES
1124 objdump_sprintf (SFILE *f, const char *format, ...)
1125 #else
1126 objdump_sprintf (va_alist)
1127 va_dcl
1128 #endif
1130 #ifndef ANSI_PROTOTYPES
1131 SFILE *f;
1132 const char *format;
1133 #endif
1134 char *buf;
1135 va_list args;
1136 size_t n;
1138 #ifdef ANSI_PROTOTYPES
1139 va_start (args, format);
1140 #else
1141 va_start (args);
1142 f = va_arg (args, SFILE *);
1143 format = va_arg (args, const char *);
1144 #endif
1146 vasprintf (&buf, format, args);
1148 va_end (args);
1150 if (buf == NULL)
1152 fprintf (stderr, _("Out of virtual memory\n"));
1153 exit (1);
1156 n = strlen (buf);
1158 while ((size_t) ((f->buffer + f->size) - f->current) < n + 1)
1160 size_t curroff;
1162 curroff = f->current - f->buffer;
1163 f->size *= 2;
1164 f->buffer = xrealloc (f->buffer, f->size);
1165 f->current = f->buffer + curroff;
1168 memcpy (f->current, buf, n);
1169 f->current += n;
1170 f->current[0] = '\0';
1172 free (buf);
1174 return n;
1177 /* The number of zeroes we want to see before we start skipping them.
1178 The number is arbitrarily chosen. */
1180 #define SKIP_ZEROES (8)
1182 /* The number of zeroes to skip at the end of a section. If the
1183 number of zeroes at the end is between SKIP_ZEROES_AT_END and
1184 SKIP_ZEROES, they will be disassembled. If there are fewer than
1185 SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic
1186 attempt to avoid disassembling zeroes inserted by section
1187 alignment. */
1189 #define SKIP_ZEROES_AT_END (3)
1191 /* Disassemble some data in memory between given values. */
1193 static void
1194 disassemble_bytes (info, disassemble_fn, insns, data, start, stop, relppp,
1195 relppend)
1196 struct disassemble_info *info;
1197 disassembler_ftype disassemble_fn;
1198 boolean insns;
1199 bfd_byte *data;
1200 bfd_vma start;
1201 bfd_vma stop;
1202 arelent ***relppp;
1203 arelent **relppend;
1205 struct objdump_disasm_info *aux;
1206 asection *section;
1207 int bytes_per_line;
1208 boolean done_dot;
1209 int skip_addr_chars;
1210 bfd_vma i;
1212 aux = (struct objdump_disasm_info *) info->application_data;
1213 section = aux->sec;
1215 if (insns)
1216 bytes_per_line = 4;
1217 else
1218 bytes_per_line = 16;
1220 /* Figure out how many characters to skip at the start of an
1221 address, to make the disassembly look nicer. We discard leading
1222 zeroes in chunks of 4, ensuring that there is always a leading
1223 zero remaining. */
1224 skip_addr_chars = 0;
1225 if (! prefix_addresses)
1227 char buf[30];
1228 char *s;
1230 sprintf_vma (buf,
1231 section->vma + bfd_section_size (section->owner, section));
1232 s = buf;
1233 while (s[0] == '0' && s[1] == '0' && s[2] == '0' && s[3] == '0'
1234 && s[4] == '0')
1236 skip_addr_chars += 4;
1237 s += 4;
1241 info->insn_info_valid = 0;
1243 done_dot = false;
1244 i = start;
1245 while (i < stop)
1247 bfd_vma z;
1248 int bytes = 0;
1249 boolean need_nl = false;
1251 /* If we see more than SKIP_ZEROES bytes of zeroes, we just
1252 print `...'. */
1253 for (z = i; z < stop; z++)
1254 if (data[z] != 0)
1255 break;
1256 if (! disassemble_zeroes
1257 && (info->insn_info_valid == 0
1258 || info->branch_delay_insns == 0)
1259 && (z - i >= SKIP_ZEROES
1260 || (z == stop && z - i < SKIP_ZEROES_AT_END)))
1262 printf ("\t...\n");
1264 /* If there are more nonzero bytes to follow, we only skip
1265 zeroes in multiples of 4, to try to avoid running over
1266 the start of an instruction which happens to start with
1267 zero. */
1268 if (z != stop)
1269 z = i + ((z - i) &~ 3);
1271 bytes = z - i;
1273 else
1275 char buf[50];
1276 SFILE sfile;
1277 int bpc = 0;
1278 int pb = 0;
1280 done_dot = false;
1282 if (with_line_numbers || with_source_code)
1283 show_line (aux->abfd, section, i);
1285 if (! prefix_addresses)
1287 char *s;
1289 sprintf_vma (buf, section->vma + i);
1290 for (s = buf + skip_addr_chars; *s == '0'; s++)
1291 *s = ' ';
1292 if (*s == '\0')
1293 *--s = '0';
1294 printf ("%s:\t", buf + skip_addr_chars);
1296 else
1298 aux->require_sec = true;
1299 objdump_print_address (section->vma + i, info);
1300 aux->require_sec = false;
1301 putchar (' ');
1304 if (insns)
1306 sfile.size = 120;
1307 sfile.buffer = xmalloc (sfile.size);
1308 sfile.current = sfile.buffer;
1309 info->fprintf_func = (fprintf_ftype) objdump_sprintf;
1310 info->stream = (FILE *) &sfile;
1311 info->bytes_per_line = 0;
1312 info->bytes_per_chunk = 0;
1314 /* FIXME: This is wrong. It tests the number of bytes
1315 in the last instruction, not the current one. */
1316 if (*relppp < relppend
1317 && (**relppp)->address >= i
1318 && (**relppp)->address < i + bytes)
1319 info->flags = INSN_HAS_RELOC;
1320 else
1321 info->flags = 0;
1323 bytes = (*disassemble_fn) (section->vma + i, info);
1324 info->fprintf_func = (fprintf_ftype) fprintf;
1325 info->stream = stdout;
1326 if (info->bytes_per_line != 0)
1327 bytes_per_line = info->bytes_per_line;
1328 if (bytes < 0)
1330 if (sfile.current != sfile.buffer)
1331 printf ("%s\n", sfile.buffer);
1332 free (sfile.buffer);
1333 break;
1336 else
1338 bfd_vma j;
1340 bytes = bytes_per_line;
1341 if (i + bytes > stop)
1342 bytes = stop - i;
1344 for (j = i; j < i + bytes; ++j)
1346 if (isprint (data[j]))
1347 buf[j - i] = data[j];
1348 else
1349 buf[j - i] = '.';
1351 buf[j - i] = '\0';
1354 if (prefix_addresses
1355 ? show_raw_insn > 0
1356 : show_raw_insn >= 0)
1358 bfd_vma j;
1360 /* If ! prefix_addresses and ! wide_output, we print
1361 bytes_per_line bytes per line. */
1362 pb = bytes;
1363 if (pb > bytes_per_line && ! prefix_addresses && ! wide_output)
1364 pb = bytes_per_line;
1366 if (info->bytes_per_chunk)
1367 bpc = info->bytes_per_chunk;
1368 else
1369 bpc = 1;
1371 for (j = i; j < i + pb; j += bpc)
1373 int k;
1374 if (bpc > 1 && info->display_endian == BFD_ENDIAN_LITTLE)
1376 for (k = bpc - 1; k >= 0; k--)
1377 printf ("%02x", (unsigned) data[j + k]);
1378 putchar (' ');
1380 else
1382 for (k = 0; k < bpc; k++)
1383 printf ("%02x", (unsigned) data[j + k]);
1384 putchar (' ');
1388 for (; pb < bytes_per_line; pb += bpc)
1390 int k;
1392 for (k = 0; k < bpc; k++)
1393 printf (" ");
1394 putchar (' ');
1397 /* Separate raw data from instruction by extra space. */
1398 if (insns)
1399 putchar ('\t');
1400 else
1401 printf (" ");
1404 if (! insns)
1405 printf ("%s", buf);
1406 else
1408 printf ("%s", sfile.buffer);
1409 free (sfile.buffer);
1412 if (prefix_addresses
1413 ? show_raw_insn > 0
1414 : show_raw_insn >= 0)
1416 while (pb < bytes)
1418 bfd_vma j;
1419 char *s;
1421 putchar ('\n');
1422 j = i + pb;
1424 sprintf_vma (buf, section->vma + j);
1425 for (s = buf + skip_addr_chars; *s == '0'; s++)
1426 *s = ' ';
1427 if (*s == '\0')
1428 *--s = '0';
1429 printf ("%s:\t", buf + skip_addr_chars);
1431 pb += bytes_per_line;
1432 if (pb > bytes)
1433 pb = bytes;
1434 for (; j < i + pb; j += bpc)
1436 int k;
1438 if (bpc > 1 && info->display_endian == BFD_ENDIAN_LITTLE)
1440 for (k = bpc - 1; k >= 0; k--)
1441 printf ("%02x", (unsigned) data[j + k]);
1442 putchar (' ');
1444 else
1446 for (k = 0; k < bpc; k++)
1447 printf ("%02x", (unsigned) data[j + k]);
1448 putchar (' ');
1454 if (!wide_output)
1455 putchar ('\n');
1456 else
1457 need_nl = true;
1460 if (dump_reloc_info
1461 && (section->flags & SEC_RELOC) != 0)
1463 while ((*relppp) < relppend
1464 && ((**relppp)->address >= (bfd_vma) i
1465 && (**relppp)->address < (bfd_vma) i + bytes))
1467 arelent *q;
1469 q = **relppp;
1471 if (wide_output)
1472 putchar ('\t');
1473 else
1474 printf ("\t\t\t");
1476 objdump_print_value (section->vma + q->address, info, true);
1478 printf (": %s\t", q->howto->name);
1480 if (q->sym_ptr_ptr == NULL || *q->sym_ptr_ptr == NULL)
1481 printf ("*unknown*");
1482 else
1484 const char *sym_name;
1486 sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
1487 if (sym_name != NULL && *sym_name != '\0')
1488 objdump_print_symname (aux->abfd, info, *q->sym_ptr_ptr);
1489 else
1491 asection *sym_sec;
1493 sym_sec = bfd_get_section (*q->sym_ptr_ptr);
1494 sym_name = bfd_get_section_name (aux->abfd, sym_sec);
1495 if (sym_name == NULL || *sym_name == '\0')
1496 sym_name = "*unknown*";
1497 printf ("%s", sym_name);
1501 if (q->addend)
1503 printf ("+0x");
1504 objdump_print_value (q->addend, info, true);
1507 printf ("\n");
1508 need_nl = false;
1509 ++(*relppp);
1513 if (need_nl)
1514 printf ("\n");
1516 i += bytes;
1520 /* Disassemble the contents of an object file. */
1522 static void
1523 disassemble_data (abfd)
1524 bfd *abfd;
1526 long i;
1527 disassembler_ftype disassemble_fn;
1528 struct disassemble_info disasm_info;
1529 struct objdump_disasm_info aux;
1530 asection *section;
1532 print_files = NULL;
1533 prev_functionname = NULL;
1534 prev_line = -1;
1536 /* We make a copy of syms to sort. We don't want to sort syms
1537 because that will screw up the relocs. */
1538 sorted_syms = (asymbol **) xmalloc (symcount * sizeof (asymbol *));
1539 memcpy (sorted_syms, syms, symcount * sizeof (asymbol *));
1541 sorted_symcount = remove_useless_symbols (sorted_syms, symcount);
1543 /* Sort the symbols into section and symbol order */
1544 qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols);
1546 INIT_DISASSEMBLE_INFO(disasm_info, stdout, fprintf);
1547 disasm_info.application_data = (PTR) &aux;
1548 aux.abfd = abfd;
1549 aux.require_sec = false;
1550 disasm_info.print_address_func = objdump_print_address;
1551 disasm_info.symbol_at_address_func = objdump_symbol_at_address;
1553 if (machine != (char *) NULL)
1555 const bfd_arch_info_type *info = bfd_scan_arch (machine);
1556 if (info == NULL)
1558 fprintf (stderr, _("%s: Can't use supplied machine %s\n"),
1559 program_name,
1560 machine);
1561 exit (1);
1563 abfd->arch_info = info;
1566 if (endian != BFD_ENDIAN_UNKNOWN)
1568 struct bfd_target *xvec;
1570 xvec = (struct bfd_target *) xmalloc (sizeof (struct bfd_target));
1571 memcpy (xvec, abfd->xvec, sizeof (struct bfd_target));
1572 xvec->byteorder = endian;
1573 abfd->xvec = xvec;
1576 disassemble_fn = disassembler (abfd);
1577 if (!disassemble_fn)
1579 fprintf (stderr, _("%s: Can't disassemble for architecture %s\n"),
1580 program_name,
1581 bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
1582 exit_status = 1;
1583 return;
1586 disasm_info.flavour = bfd_get_flavour (abfd);
1587 disasm_info.arch = bfd_get_arch (abfd);
1588 disasm_info.mach = bfd_get_mach (abfd);
1589 disasm_info.disassembler_options = disassembler_options;
1591 if (bfd_big_endian (abfd))
1592 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_BIG;
1593 else if (bfd_little_endian (abfd))
1594 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_LITTLE;
1595 else
1596 /* ??? Aborting here seems too drastic. We could default to big or little
1597 instead. */
1598 disasm_info.endian = BFD_ENDIAN_UNKNOWN;
1600 for (section = abfd->sections;
1601 section != (asection *) NULL;
1602 section = section->next)
1604 bfd_byte *data = NULL;
1605 bfd_size_type datasize = 0;
1606 arelent **relbuf = NULL;
1607 arelent **relpp = NULL;
1608 arelent **relppend = NULL;
1609 long stop;
1610 asymbol *sym = NULL;
1611 long place = 0;
1613 if ((section->flags & SEC_LOAD) == 0
1614 || (! disassemble_all
1615 && only == NULL
1616 && (section->flags & SEC_CODE) == 0))
1617 continue;
1618 if (only != (char *) NULL && strcmp (only, section->name) != 0)
1619 continue;
1621 if (dump_reloc_info
1622 && (section->flags & SEC_RELOC) != 0)
1624 long relsize;
1626 relsize = bfd_get_reloc_upper_bound (abfd, section);
1627 if (relsize < 0)
1628 bfd_fatal (bfd_get_filename (abfd));
1630 if (relsize > 0)
1632 long relcount;
1634 relbuf = (arelent **) xmalloc (relsize);
1635 relcount = bfd_canonicalize_reloc (abfd, section, relbuf, syms);
1636 if (relcount < 0)
1637 bfd_fatal (bfd_get_filename (abfd));
1639 /* Sort the relocs by address. */
1640 qsort (relbuf, relcount, sizeof (arelent *), compare_relocs);
1642 relpp = relbuf;
1643 relppend = relpp + relcount;
1645 /* Skip over the relocs belonging to addresses below the
1646 start address. */
1647 if (start_address != (bfd_vma) -1)
1649 while (relpp < relppend
1650 && (*relpp)->address < start_address)
1651 ++relpp;
1656 printf (_("Disassembly of section %s:\n"), section->name);
1658 datasize = bfd_get_section_size_before_reloc (section);
1659 if (datasize == 0)
1660 continue;
1662 data = (bfd_byte *) xmalloc ((size_t) datasize);
1664 bfd_get_section_contents (abfd, section, data, 0, datasize);
1666 aux.sec = section;
1667 disasm_info.buffer = data;
1668 disasm_info.buffer_vma = section->vma;
1669 disasm_info.buffer_length = datasize;
1670 if (start_address == (bfd_vma) -1
1671 || start_address < disasm_info.buffer_vma)
1672 i = 0;
1673 else
1674 i = start_address - disasm_info.buffer_vma;
1675 if (stop_address == (bfd_vma) -1)
1676 stop = datasize;
1677 else
1679 if (stop_address < disasm_info.buffer_vma)
1680 stop = 0;
1681 else
1682 stop = stop_address - disasm_info.buffer_vma;
1683 if (stop > disasm_info.buffer_length)
1684 stop = disasm_info.buffer_length;
1687 sym = find_symbol_for_address (abfd, section, section->vma + i,
1688 true, &place);
1690 while (i < stop)
1692 asymbol *nextsym;
1693 long nextstop;
1694 boolean insns;
1696 if (sym != NULL && bfd_asymbol_value (sym) <= section->vma + i)
1698 int x;
1700 for (x = place;
1701 (x < sorted_symcount
1702 && bfd_asymbol_value (sorted_syms[x]) <= section->vma + i);
1703 ++x)
1704 continue;
1705 disasm_info.symbols = & sorted_syms[place];
1706 disasm_info.num_symbols = x - place;
1708 else
1709 disasm_info.symbols = NULL;
1711 if (! prefix_addresses)
1713 printf ("\n");
1714 objdump_print_addr_with_sym (abfd, section, sym,
1715 section->vma + i,
1716 &disasm_info,
1717 false);
1718 printf (":\n");
1721 if (sym != NULL && bfd_asymbol_value (sym) > section->vma + i)
1722 nextsym = sym;
1723 else if (sym == NULL)
1724 nextsym = NULL;
1725 else
1727 while (place < sorted_symcount
1728 /* ??? Why the test for != section? */
1729 && (sorted_syms[place]->section != section
1730 || (bfd_asymbol_value (sorted_syms[place])
1731 <= bfd_asymbol_value (sym))))
1732 ++place;
1733 if (place >= sorted_symcount)
1734 nextsym = NULL;
1735 else
1736 nextsym = sorted_syms[place];
1739 if (sym != NULL && bfd_asymbol_value (sym) > section->vma + i)
1741 nextstop = bfd_asymbol_value (sym) - section->vma;
1742 if (nextstop > stop)
1743 nextstop = stop;
1745 else if (nextsym == NULL)
1746 nextstop = stop;
1747 else
1749 nextstop = bfd_asymbol_value (nextsym) - section->vma;
1750 if (nextstop > stop)
1751 nextstop = stop;
1754 /* If a symbol is explicitly marked as being an object
1755 rather than a function, just dump the bytes without
1756 disassembling them. */
1757 if (disassemble_all
1758 || sym == NULL
1759 || bfd_asymbol_value (sym) > section->vma + i
1760 || ((sym->flags & BSF_OBJECT) == 0
1761 && (strstr (bfd_asymbol_name (sym), "gnu_compiled")
1762 == NULL)
1763 && (strstr (bfd_asymbol_name (sym), "gcc2_compiled")
1764 == NULL))
1765 || (sym->flags & BSF_FUNCTION) != 0)
1766 insns = true;
1767 else
1768 insns = false;
1770 disassemble_bytes (&disasm_info, disassemble_fn, insns, data, i,
1771 nextstop, &relpp, relppend);
1773 i = nextstop;
1774 sym = nextsym;
1777 free (data);
1778 if (relbuf != NULL)
1779 free (relbuf);
1781 free (sorted_syms);
1785 /* Define a table of stab values and print-strings. We wish the initializer
1786 could be a direct-mapped table, but instead we build one the first
1787 time we need it. */
1789 static void dump_section_stabs PARAMS ((bfd *abfd, char *stabsect_name,
1790 char *strsect_name));
1792 /* Dump the stabs sections from an object file that has a section that
1793 uses Sun stabs encoding. */
1795 static void
1796 dump_stabs (abfd)
1797 bfd *abfd;
1799 dump_section_stabs (abfd, ".stab", ".stabstr");
1800 dump_section_stabs (abfd, ".stab.excl", ".stab.exclstr");
1801 dump_section_stabs (abfd, ".stab.index", ".stab.indexstr");
1802 dump_section_stabs (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
1805 static bfd_byte *stabs;
1806 static bfd_size_type stab_size;
1808 static char *strtab;
1809 static bfd_size_type stabstr_size;
1811 /* Read ABFD's stabs section STABSECT_NAME into `stabs'
1812 and string table section STRSECT_NAME into `strtab'.
1813 If the section exists and was read, allocate the space and return true.
1814 Otherwise return false. */
1816 static boolean
1817 read_section_stabs (abfd, stabsect_name, strsect_name)
1818 bfd *abfd;
1819 const char *stabsect_name;
1820 const char *strsect_name;
1822 asection *stabsect, *stabstrsect;
1824 stabsect = bfd_get_section_by_name (abfd, stabsect_name);
1825 if (0 == stabsect)
1827 printf (_("No %s section present\n\n"), stabsect_name);
1828 return false;
1831 stabstrsect = bfd_get_section_by_name (abfd, strsect_name);
1832 if (0 == stabstrsect)
1834 fprintf (stderr, _("%s: %s has no %s section\n"), program_name,
1835 bfd_get_filename (abfd), strsect_name);
1836 exit_status = 1;
1837 return false;
1840 stab_size = bfd_section_size (abfd, stabsect);
1841 stabstr_size = bfd_section_size (abfd, stabstrsect);
1843 stabs = (bfd_byte *) xmalloc (stab_size);
1844 strtab = (char *) xmalloc (stabstr_size);
1846 if (! bfd_get_section_contents (abfd, stabsect, (PTR) stabs, 0, stab_size))
1848 fprintf (stderr, _("%s: Reading %s section of %s failed: %s\n"),
1849 program_name, stabsect_name, bfd_get_filename (abfd),
1850 bfd_errmsg (bfd_get_error ()));
1851 free (stabs);
1852 free (strtab);
1853 exit_status = 1;
1854 return false;
1857 if (! bfd_get_section_contents (abfd, stabstrsect, (PTR) strtab, 0,
1858 stabstr_size))
1860 fprintf (stderr, _("%s: Reading %s section of %s failed: %s\n"),
1861 program_name, strsect_name, bfd_get_filename (abfd),
1862 bfd_errmsg (bfd_get_error ()));
1863 free (stabs);
1864 free (strtab);
1865 exit_status = 1;
1866 return false;
1869 return true;
1872 /* Stabs entries use a 12 byte format:
1873 4 byte string table index
1874 1 byte stab type
1875 1 byte stab other field
1876 2 byte stab desc field
1877 4 byte stab value
1878 FIXME: This will have to change for a 64 bit object format. */
1880 #define STRDXOFF (0)
1881 #define TYPEOFF (4)
1882 #define OTHEROFF (5)
1883 #define DESCOFF (6)
1884 #define VALOFF (8)
1885 #define STABSIZE (12)
1887 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
1888 using string table section STRSECT_NAME (in `strtab'). */
1890 static void
1891 print_section_stabs (abfd, stabsect_name, strsect_name)
1892 bfd *abfd;
1893 const char *stabsect_name;
1894 const char *strsect_name ATTRIBUTE_UNUSED;
1896 int i;
1897 unsigned file_string_table_offset = 0, next_file_string_table_offset = 0;
1898 bfd_byte *stabp, *stabs_end;
1900 stabp = stabs;
1901 stabs_end = stabp + stab_size;
1903 printf (_("Contents of %s section:\n\n"), stabsect_name);
1904 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
1906 /* Loop through all symbols and print them.
1908 We start the index at -1 because there is a dummy symbol on
1909 the front of stabs-in-{coff,elf} sections that supplies sizes. */
1911 for (i = -1; stabp < stabs_end; stabp += STABSIZE, i++)
1913 const char *name;
1914 unsigned long strx;
1915 unsigned char type, other;
1916 unsigned short desc;
1917 bfd_vma value;
1919 strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
1920 type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
1921 other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
1922 desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
1923 value = bfd_h_get_32 (abfd, stabp + VALOFF);
1925 printf ("\n%-6d ", i);
1926 /* Either print the stab name, or, if unnamed, print its number
1927 again (makes consistent formatting for tools like awk). */
1928 name = bfd_get_stab_name (type);
1929 if (name != NULL)
1930 printf ("%-6s", name);
1931 else if (type == N_UNDF)
1932 printf ("HdrSym");
1933 else
1934 printf ("%-6d", type);
1935 printf (" %-6d %-6d ", other, desc);
1936 printf_vma (value);
1937 printf (" %-6lu", strx);
1939 /* Symbols with type == 0 (N_UNDF) specify the length of the
1940 string table associated with this file. We use that info
1941 to know how to relocate the *next* file's string table indices. */
1943 if (type == N_UNDF)
1945 file_string_table_offset = next_file_string_table_offset;
1946 next_file_string_table_offset += value;
1948 else
1950 /* Using the (possibly updated) string table offset, print the
1951 string (if any) associated with this symbol. */
1953 if ((strx + file_string_table_offset) < stabstr_size)
1954 printf (" %s", &strtab[strx + file_string_table_offset]);
1955 else
1956 printf (" *");
1959 printf ("\n\n");
1962 static void
1963 dump_section_stabs (abfd, stabsect_name, strsect_name)
1964 bfd *abfd;
1965 char *stabsect_name;
1966 char *strsect_name;
1968 asection *s;
1970 /* Check for section names for which stabsect_name is a prefix, to
1971 handle .stab0, etc. */
1972 for (s = abfd->sections;
1973 s != NULL;
1974 s = s->next)
1976 int len;
1978 len = strlen (stabsect_name);
1980 /* If the prefix matches, and the files section name ends with a
1981 nul or a digit, then we match. I.e., we want either an exact
1982 match or a section followed by a number. */
1983 if (strncmp (stabsect_name, s->name, len) == 0
1984 && (s->name[len] == '\000'
1985 || isdigit ((unsigned char) s->name[len])))
1987 if (read_section_stabs (abfd, s->name, strsect_name))
1989 print_section_stabs (abfd, s->name, strsect_name);
1990 free (stabs);
1991 free (strtab);
1997 static void
1998 dump_bfd_header (abfd)
1999 bfd *abfd;
2001 char *comma = "";
2003 printf (_("architecture: %s, "),
2004 bfd_printable_arch_mach (bfd_get_arch (abfd),
2005 bfd_get_mach (abfd)));
2006 printf (_("flags 0x%08x:\n"), abfd->flags);
2008 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
2009 PF (HAS_RELOC, "HAS_RELOC");
2010 PF (EXEC_P, "EXEC_P");
2011 PF (HAS_LINENO, "HAS_LINENO");
2012 PF (HAS_DEBUG, "HAS_DEBUG");
2013 PF (HAS_SYMS, "HAS_SYMS");
2014 PF (HAS_LOCALS, "HAS_LOCALS");
2015 PF (DYNAMIC, "DYNAMIC");
2016 PF (WP_TEXT, "WP_TEXT");
2017 PF (D_PAGED, "D_PAGED");
2018 PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
2019 printf (_("\nstart address 0x"));
2020 printf_vma (abfd->start_address);
2021 printf ("\n");
2024 static void
2025 dump_bfd_private_header (abfd)
2026 bfd *abfd;
2028 bfd_print_private_bfd_data (abfd, stdout);
2031 static void
2032 display_bfd (abfd)
2033 bfd *abfd;
2035 char **matching;
2037 if (!bfd_check_format_matches (abfd, bfd_object, &matching))
2039 nonfatal (bfd_get_filename (abfd));
2040 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
2042 list_matching_formats (matching);
2043 free (matching);
2045 return;
2048 /* If we are adjusting section VMA's, change them all now. Changing
2049 the BFD information is a hack. However, we must do it, or
2050 bfd_find_nearest_line will not do the right thing. */
2051 if (adjust_section_vma != 0)
2053 asection *s;
2055 for (s = abfd->sections; s != NULL; s = s->next)
2057 s->vma += adjust_section_vma;
2058 s->lma += adjust_section_vma;
2062 printf (_("\n%s: file format %s\n"), bfd_get_filename (abfd),
2063 abfd->xvec->name);
2064 if (dump_ar_hdrs)
2065 print_arelt_descr (stdout, abfd, true);
2066 if (dump_file_header)
2067 dump_bfd_header (abfd);
2068 if (dump_private_headers)
2069 dump_bfd_private_header (abfd);
2070 putchar ('\n');
2071 if (dump_section_headers)
2072 dump_headers (abfd);
2073 if (dump_symtab || dump_reloc_info || disassemble || dump_debugging)
2075 syms = slurp_symtab (abfd);
2077 if (dump_dynamic_symtab || dump_dynamic_reloc_info)
2079 dynsyms = slurp_dynamic_symtab (abfd);
2081 if (dump_symtab)
2082 dump_symbols (abfd, false);
2083 if (dump_dynamic_symtab)
2084 dump_symbols (abfd, true);
2085 if (dump_stab_section_info)
2086 dump_stabs (abfd);
2087 if (dump_reloc_info && ! disassemble)
2088 dump_relocs (abfd);
2089 if (dump_dynamic_reloc_info)
2090 dump_dynamic_relocs (abfd);
2091 if (dump_section_contents)
2092 dump_data (abfd);
2093 if (disassemble)
2094 disassemble_data (abfd);
2095 if (dump_debugging)
2097 PTR dhandle;
2099 dhandle = read_debugging_info (abfd, syms, symcount);
2100 if (dhandle != NULL)
2102 if (! print_debugging_info (stdout, dhandle))
2104 fprintf (stderr,
2105 _("%s: printing debugging information failed\n"),
2106 bfd_get_filename (abfd));
2107 exit_status = 1;
2111 if (syms)
2113 free (syms);
2114 syms = NULL;
2116 if (dynsyms)
2118 free (dynsyms);
2119 dynsyms = NULL;
2123 static void
2124 display_file (filename, target)
2125 char *filename;
2126 char *target;
2128 bfd *file, *arfile = (bfd *) NULL;
2130 file = bfd_openr (filename, target);
2131 if (file == NULL)
2133 nonfatal (filename);
2134 return;
2137 if (bfd_check_format (file, bfd_archive) == true)
2139 bfd *last_arfile = NULL;
2141 printf (_("In archive %s:\n"), bfd_get_filename (file));
2142 for (;;)
2144 bfd_set_error (bfd_error_no_error);
2146 arfile = bfd_openr_next_archived_file (file, arfile);
2147 if (arfile == NULL)
2149 if (bfd_get_error () != bfd_error_no_more_archived_files)
2150 nonfatal (bfd_get_filename (file));
2151 break;
2154 display_bfd (arfile);
2156 if (last_arfile != NULL)
2157 bfd_close (last_arfile);
2158 last_arfile = arfile;
2161 if (last_arfile != NULL)
2162 bfd_close (last_arfile);
2164 else
2165 display_bfd (file);
2167 bfd_close (file);
2170 /* Actually display the various requested regions */
2172 static void
2173 dump_data (abfd)
2174 bfd *abfd;
2176 asection *section;
2177 bfd_byte *data = 0;
2178 bfd_size_type datasize = 0;
2179 bfd_size_type i;
2180 bfd_size_type start, stop;
2182 for (section = abfd->sections; section != NULL; section =
2183 section->next)
2185 int onaline = 16;
2187 if (only == (char *) NULL ||
2188 strcmp (only, section->name) == 0)
2190 if (section->flags & SEC_HAS_CONTENTS)
2192 printf (_("Contents of section %s:\n"), section->name);
2194 if (bfd_section_size (abfd, section) == 0)
2195 continue;
2196 data = (bfd_byte *) xmalloc ((size_t) bfd_section_size (abfd, section));
2197 datasize = bfd_section_size (abfd, section);
2200 bfd_get_section_contents (abfd, section, (PTR) data, 0, bfd_section_size (abfd, section));
2202 if (start_address == (bfd_vma) -1
2203 || start_address < section->vma)
2204 start = 0;
2205 else
2206 start = start_address - section->vma;
2207 if (stop_address == (bfd_vma) -1)
2208 stop = bfd_section_size (abfd, section);
2209 else
2211 if (stop_address < section->vma)
2212 stop = 0;
2213 else
2214 stop = stop_address - section->vma;
2215 if (stop > bfd_section_size (abfd, section))
2216 stop = bfd_section_size (abfd, section);
2218 for (i = start; i < stop; i += onaline)
2220 bfd_size_type j;
2222 printf (" %04lx ", (unsigned long int) (i + section->vma));
2223 for (j = i; j < i + onaline; j++)
2225 if (j < stop)
2226 printf ("%02x", (unsigned) (data[j]));
2227 else
2228 printf (" ");
2229 if ((j & 3) == 3)
2230 printf (" ");
2233 printf (" ");
2234 for (j = i; j < i + onaline; j++)
2236 if (j >= stop)
2237 printf (" ");
2238 else
2239 printf ("%c", isprint (data[j]) ? data[j] : '.');
2241 putchar ('\n');
2243 free (data);
2249 /* Should perhaps share code and display with nm? */
2250 static void
2251 dump_symbols (abfd, dynamic)
2252 bfd *abfd ATTRIBUTE_UNUSED;
2253 boolean dynamic;
2255 asymbol **current;
2256 long max;
2257 long count;
2259 if (dynamic)
2261 current = dynsyms;
2262 max = dynsymcount;
2263 if (max == 0)
2264 return;
2265 printf ("DYNAMIC SYMBOL TABLE:\n");
2267 else
2269 current = syms;
2270 max = symcount;
2271 if (max == 0)
2272 return;
2273 printf ("SYMBOL TABLE:\n");
2276 for (count = 0; count < max; count++)
2278 if (*current)
2280 bfd *cur_bfd = bfd_asymbol_bfd (*current);
2282 if (cur_bfd != NULL)
2284 const char *name;
2285 char *alloc;
2287 name = bfd_asymbol_name (*current);
2288 alloc = NULL;
2289 if (do_demangle && name != NULL && *name != '\0')
2291 const char *n;
2293 /* If we want to demangle the name, we demangle it
2294 here, and temporarily clobber it while calling
2295 bfd_print_symbol. FIXME: This is a gross hack. */
2297 n = name;
2298 if (bfd_get_symbol_leading_char (cur_bfd) == *n)
2299 ++n;
2300 alloc = cplus_demangle (n, DMGL_ANSI | DMGL_PARAMS);
2301 if (alloc != NULL)
2302 (*current)->name = alloc;
2303 else
2304 (*current)->name = n;
2307 bfd_print_symbol (cur_bfd, stdout, *current,
2308 bfd_print_symbol_all);
2310 (*current)->name = name;
2311 if (alloc != NULL)
2312 free (alloc);
2314 printf ("\n");
2317 current++;
2319 printf ("\n");
2320 printf ("\n");
2323 static void
2324 dump_relocs (abfd)
2325 bfd *abfd;
2327 arelent **relpp;
2328 long relcount;
2329 asection *a;
2331 for (a = abfd->sections; a != (asection *) NULL; a = a->next)
2333 long relsize;
2335 if (bfd_is_abs_section (a))
2336 continue;
2337 if (bfd_is_und_section (a))
2338 continue;
2339 if (bfd_is_com_section (a))
2340 continue;
2342 if (only)
2344 if (strcmp (only, a->name))
2345 continue;
2347 else if ((a->flags & SEC_RELOC) == 0)
2348 continue;
2350 relsize = bfd_get_reloc_upper_bound (abfd, a);
2351 if (relsize < 0)
2352 bfd_fatal (bfd_get_filename (abfd));
2354 printf ("RELOCATION RECORDS FOR [%s]:", a->name);
2356 if (relsize == 0)
2358 printf (" (none)\n\n");
2360 else
2362 relpp = (arelent **) xmalloc (relsize);
2363 relcount = bfd_canonicalize_reloc (abfd, a, relpp, syms);
2364 if (relcount < 0)
2365 bfd_fatal (bfd_get_filename (abfd));
2366 else if (relcount == 0)
2368 printf (" (none)\n\n");
2370 else
2372 printf ("\n");
2373 dump_reloc_set (abfd, a, relpp, relcount);
2374 printf ("\n\n");
2376 free (relpp);
2381 static void
2382 dump_dynamic_relocs (abfd)
2383 bfd *abfd;
2385 long relsize;
2386 arelent **relpp;
2387 long relcount;
2389 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
2390 if (relsize < 0)
2391 bfd_fatal (bfd_get_filename (abfd));
2393 printf ("DYNAMIC RELOCATION RECORDS");
2395 if (relsize == 0)
2397 printf (" (none)\n\n");
2399 else
2401 relpp = (arelent **) xmalloc (relsize);
2402 relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
2403 if (relcount < 0)
2404 bfd_fatal (bfd_get_filename (abfd));
2405 else if (relcount == 0)
2407 printf (" (none)\n\n");
2409 else
2411 printf ("\n");
2412 dump_reloc_set (abfd, (asection *) NULL, relpp, relcount);
2413 printf ("\n\n");
2415 free (relpp);
2419 static void
2420 dump_reloc_set (abfd, sec, relpp, relcount)
2421 bfd *abfd;
2422 asection *sec;
2423 arelent **relpp;
2424 long relcount;
2426 arelent **p;
2427 char *last_filename, *last_functionname;
2428 unsigned int last_line;
2430 /* Get column headers lined up reasonably. */
2432 static int width;
2433 if (width == 0)
2435 char buf[30];
2436 sprintf_vma (buf, (bfd_vma) -1);
2437 width = strlen (buf) - 7;
2439 printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
2442 last_filename = NULL;
2443 last_functionname = NULL;
2444 last_line = 0;
2446 for (p = relpp; relcount && *p != (arelent *) NULL; p++, relcount--)
2448 arelent *q = *p;
2449 const char *filename, *functionname;
2450 unsigned int line;
2451 const char *sym_name;
2452 const char *section_name;
2454 if (start_address != (bfd_vma) -1
2455 && q->address < start_address)
2456 continue;
2457 if (stop_address != (bfd_vma) -1
2458 && q->address > stop_address)
2459 continue;
2461 if (with_line_numbers
2462 && sec != NULL
2463 && bfd_find_nearest_line (abfd, sec, syms, q->address,
2464 &filename, &functionname, &line))
2466 if (functionname != NULL
2467 && (last_functionname == NULL
2468 || strcmp (functionname, last_functionname) != 0))
2470 printf ("%s():\n", functionname);
2471 if (last_functionname != NULL)
2472 free (last_functionname);
2473 last_functionname = xstrdup (functionname);
2475 if (line > 0
2476 && (line != last_line
2477 || (filename != NULL
2478 && last_filename != NULL
2479 && strcmp (filename, last_filename) != 0)))
2481 printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
2482 last_line = line;
2483 if (last_filename != NULL)
2484 free (last_filename);
2485 if (filename == NULL)
2486 last_filename = NULL;
2487 else
2488 last_filename = xstrdup (filename);
2492 if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
2494 sym_name = (*(q->sym_ptr_ptr))->name;
2495 section_name = (*(q->sym_ptr_ptr))->section->name;
2497 else
2499 sym_name = NULL;
2500 section_name = NULL;
2502 if (sym_name)
2504 printf_vma (q->address);
2505 if (q->howto->name)
2506 printf (" %-16s ", q->howto->name);
2507 else
2508 printf (" %-16d ", q->howto->type);
2509 objdump_print_symname (abfd, (struct disassemble_info *) NULL,
2510 *q->sym_ptr_ptr);
2512 else
2514 if (section_name == (CONST char *) NULL)
2515 section_name = "*unknown*";
2516 printf_vma (q->address);
2517 printf (" %-16s [%s]",
2518 q->howto->name,
2519 section_name);
2521 if (q->addend)
2523 printf ("+0x");
2524 printf_vma (q->addend);
2526 printf ("\n");
2530 /* The length of the longest architecture name + 1. */
2531 #define LONGEST_ARCH sizeof("rs6000:6000")
2533 static const char *
2534 endian_string (endian)
2535 enum bfd_endian endian;
2537 if (endian == BFD_ENDIAN_BIG)
2538 return "big endian";
2539 else if (endian == BFD_ENDIAN_LITTLE)
2540 return "little endian";
2541 else
2542 return "endianness unknown";
2545 /* List the targets that BFD is configured to support, each followed
2546 by its endianness and the architectures it supports. */
2548 static void
2549 display_target_list ()
2551 extern bfd_target *bfd_target_vector[];
2552 char *dummy_name;
2553 int t;
2555 dummy_name = choose_temp_base ();
2556 for (t = 0; bfd_target_vector[t]; t++)
2558 bfd_target *p = bfd_target_vector[t];
2559 bfd *abfd = bfd_openw (dummy_name, p->name);
2560 int a;
2562 printf ("%s\n (header %s, data %s)\n", p->name,
2563 endian_string (p->header_byteorder),
2564 endian_string (p->byteorder));
2566 if (abfd == NULL)
2568 nonfatal (dummy_name);
2569 continue;
2572 if (! bfd_set_format (abfd, bfd_object))
2574 if (bfd_get_error () != bfd_error_invalid_operation)
2575 nonfatal (p->name);
2576 continue;
2579 for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
2580 if (bfd_set_arch_mach (abfd, (enum bfd_architecture) a, 0))
2581 printf (" %s\n",
2582 bfd_printable_arch_mach ((enum bfd_architecture) a, 0));
2584 unlink (dummy_name);
2585 free (dummy_name);
2588 /* Print a table showing which architectures are supported for entries
2589 FIRST through LAST-1 of bfd_target_vector (targets across,
2590 architectures down). */
2592 static void
2593 display_info_table (first, last)
2594 int first;
2595 int last;
2597 extern bfd_target *bfd_target_vector[];
2598 int t, a;
2599 char *dummy_name;
2601 /* Print heading of target names. */
2602 printf ("\n%*s", (int) LONGEST_ARCH, " ");
2603 for (t = first; t < last && bfd_target_vector[t]; t++)
2604 printf ("%s ", bfd_target_vector[t]->name);
2605 putchar ('\n');
2607 dummy_name = choose_temp_base ();
2608 for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
2609 if (strcmp (bfd_printable_arch_mach (a, 0), "UNKNOWN!") != 0)
2611 printf ("%*s ", (int) LONGEST_ARCH - 1,
2612 bfd_printable_arch_mach (a, 0));
2613 for (t = first; t < last && bfd_target_vector[t]; t++)
2615 bfd_target *p = bfd_target_vector[t];
2616 boolean ok = true;
2617 bfd *abfd = bfd_openw (dummy_name, p->name);
2619 if (abfd == NULL)
2621 nonfatal (p->name);
2622 ok = false;
2625 if (ok)
2627 if (! bfd_set_format (abfd, bfd_object))
2629 if (bfd_get_error () != bfd_error_invalid_operation)
2630 nonfatal (p->name);
2631 ok = false;
2635 if (ok)
2637 if (! bfd_set_arch_mach (abfd, a, 0))
2638 ok = false;
2641 if (ok)
2642 printf ("%s ", p->name);
2643 else
2645 int l = strlen (p->name);
2646 while (l--)
2647 putchar ('-');
2648 putchar (' ');
2651 putchar ('\n');
2653 unlink (dummy_name);
2654 free (dummy_name);
2657 /* Print tables of all the target-architecture combinations that
2658 BFD has been configured to support. */
2660 static void
2661 display_target_tables ()
2663 int t, columns;
2664 extern bfd_target *bfd_target_vector[];
2665 char *colum;
2667 columns = 0;
2668 colum = getenv ("COLUMNS");
2669 if (colum != NULL)
2670 columns = atoi (colum);
2671 if (columns == 0)
2672 columns = 80;
2674 t = 0;
2675 while (bfd_target_vector[t] != NULL)
2677 int oldt = t, wid;
2679 wid = LONGEST_ARCH + strlen (bfd_target_vector[t]->name) + 1;
2680 ++t;
2681 while (wid < columns && bfd_target_vector[t] != NULL)
2683 int newwid;
2685 newwid = wid + strlen (bfd_target_vector[t]->name) + 1;
2686 if (newwid >= columns)
2687 break;
2688 wid = newwid;
2689 ++t;
2691 display_info_table (oldt, t);
2695 static void
2696 display_info ()
2698 printf (_("BFD header file version %s\n"), BFD_VERSION);
2699 display_target_list ();
2700 display_target_tables ();
2704 main (argc, argv)
2705 int argc;
2706 char **argv;
2708 int c;
2709 char *target = default_target;
2710 boolean seenflag = false;
2712 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2713 setlocale (LC_MESSAGES, "");
2714 #endif
2715 bindtextdomain (PACKAGE, LOCALEDIR);
2716 textdomain (PACKAGE);
2718 program_name = *argv;
2719 xmalloc_set_program_name (program_name);
2721 START_PROGRESS (program_name, 0);
2723 bfd_init ();
2724 set_default_bfd_target ();
2726 while ((c = getopt_long (argc, argv, "pib:m:M:VCdDlfahrRtTxsSj:wE:",
2727 long_options, (int *) 0))
2728 != EOF)
2730 if (c != 'l' && c != OPTION_START_ADDRESS && c != OPTION_STOP_ADDRESS)
2731 seenflag = true;
2732 switch (c)
2734 case 0:
2735 break; /* we've been given a long option */
2736 case 'm':
2737 machine = optarg;
2738 break;
2739 case 'M':
2740 disassembler_options = optarg;
2741 break;
2742 case 'j':
2743 only = optarg;
2744 break;
2745 case 'l':
2746 with_line_numbers = 1;
2747 break;
2748 case 'b':
2749 target = optarg;
2750 break;
2751 case 'f':
2752 dump_file_header = true;
2753 break;
2754 case 'i':
2755 formats_info = true;
2756 break;
2757 case 'p':
2758 dump_private_headers = 1;
2759 break;
2760 case 'x':
2761 dump_private_headers = 1;
2762 dump_symtab = 1;
2763 dump_reloc_info = 1;
2764 dump_file_header = true;
2765 dump_ar_hdrs = 1;
2766 dump_section_headers = 1;
2767 break;
2768 case 't':
2769 dump_symtab = 1;
2770 break;
2771 case 'T':
2772 dump_dynamic_symtab = 1;
2773 break;
2774 case 'C':
2775 do_demangle = 1;
2776 break;
2777 case 'd':
2778 disassemble = true;
2779 break;
2780 case 'D':
2781 disassemble = disassemble_all = true;
2782 break;
2783 case 'S':
2784 disassemble = true;
2785 with_source_code = true;
2786 break;
2787 case 's':
2788 dump_section_contents = 1;
2789 break;
2790 case 'r':
2791 dump_reloc_info = 1;
2792 break;
2793 case 'R':
2794 dump_dynamic_reloc_info = 1;
2795 break;
2796 case 'a':
2797 dump_ar_hdrs = 1;
2798 break;
2799 case 'h':
2800 dump_section_headers = 1;
2801 break;
2802 case 'H':
2803 usage (stdout, 0);
2804 case 'V':
2805 show_version = 1;
2806 break;
2807 case 'w':
2808 wide_output = 1;
2809 break;
2810 case OPTION_ADJUST_VMA:
2811 adjust_section_vma = parse_vma (optarg, "--adjust-vma");
2812 break;
2813 case OPTION_START_ADDRESS:
2814 start_address = parse_vma (optarg, "--start-address");
2815 break;
2816 case OPTION_STOP_ADDRESS:
2817 stop_address = parse_vma (optarg, "--stop-address");
2818 break;
2819 case 'E':
2820 if (strcmp (optarg, "B") == 0)
2821 endian = BFD_ENDIAN_BIG;
2822 else if (strcmp (optarg, "L") == 0)
2823 endian = BFD_ENDIAN_LITTLE;
2824 else
2826 fprintf (stderr, _("%s: unrecognized -E option\n"),
2827 program_name);
2828 usage (stderr, 1);
2830 break;
2831 case OPTION_ENDIAN:
2832 if (strncmp (optarg, "big", strlen (optarg)) == 0)
2833 endian = BFD_ENDIAN_BIG;
2834 else if (strncmp (optarg, "little", strlen (optarg)) == 0)
2835 endian = BFD_ENDIAN_LITTLE;
2836 else
2838 fprintf (stderr, _("%s: unrecognized --endian type `%s'\n"),
2839 program_name, optarg);
2840 usage (stderr, 1);
2842 break;
2843 default:
2844 usage (stderr, 1);
2848 if (show_version)
2849 print_version ("objdump");
2851 if (seenflag == false)
2852 usage (stderr, 1);
2854 if (formats_info)
2856 display_info ();
2858 else
2860 if (optind == argc)
2861 display_file ("a.out", target);
2862 else
2863 for (; optind < argc;)
2864 display_file (argv[optind++], target);
2867 END_PROGRESS (program_name);
2869 return exit_status;