* bfin-dis.c (print_insn_bfin): Do proper endian transform when
[binutils.git] / binutils / objdump.c
blobf2b5eea01286bb8ee104f551f6bd8578c8dec617
1 /* objdump.c -- dump information about an object file.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005
4 Free Software Foundation, Inc.
6 This file is part of GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
22 /* Objdump overview.
24 Objdump displays information about one or more object files, either on
25 their own, or inside libraries. It is commonly used as a disassembler,
26 but it can also display information about file headers, symbol tables,
27 relocations, debugging directives and more.
29 The flow of execution is as follows:
31 1. Command line arguments are checked for control switches and the
32 information to be displayed is selected.
34 2. Any remaining arguments are assumed to be object files, and they are
35 processed in order by display_bfd(). If the file is an archive each
36 of its elements is processed in turn.
38 3. The file's target architecture and binary file format are determined
39 by bfd_check_format(). If they are recognised, then dump_bfd() is
40 called.
42 4. dump_bfd() in turn calls separate functions to display the requested
43 item(s) of information(s). For example disassemble_data() is called if
44 a disassembly has been requested.
46 When disassembling the code loops through blocks of instructions bounded
47 by symbols, calling disassemble_bytes() on each block. The actual
48 disassembling is done by the libopcodes library, via a function pointer
49 supplied by the disassembler() function. */
51 #include "bfd.h"
52 #include "bfdver.h"
53 #include "progress.h"
54 #include "bucomm.h"
55 #include "dwarf.h"
56 #include "budemang.h"
57 #include "getopt.h"
58 #include "safe-ctype.h"
59 #include "dis-asm.h"
60 #include "libiberty.h"
61 #include "demangle.h"
62 #include "debug.h"
63 #include "budbg.h"
65 /* Internal headers for the ELF .stab-dump code - sorry. */
66 #define BYTES_IN_WORD 32
67 #include "aout/aout64.h"
69 #if !HAVE_DECL_FPRINTF
70 /* This is needed by init_disassemble_info(). */
71 extern int fprintf (FILE *, const char *, ...);
72 #endif
74 /* Exit status. */
75 static int exit_status = 0;
77 static char *default_target = NULL; /* Default at runtime. */
79 /* The following variables are set based on arguments passed on the
80 command line. */
81 static int show_version = 0; /* Show the version number. */
82 static int dump_section_contents; /* -s */
83 static int dump_section_headers; /* -h */
84 static bfd_boolean dump_file_header; /* -f */
85 static int dump_symtab; /* -t */
86 static int dump_dynamic_symtab; /* -T */
87 static int dump_reloc_info; /* -r */
88 static int dump_dynamic_reloc_info; /* -R */
89 static int dump_ar_hdrs; /* -a */
90 static int dump_private_headers; /* -p */
91 static int prefix_addresses; /* --prefix-addresses */
92 static int with_line_numbers; /* -l */
93 static bfd_boolean with_source_code; /* -S */
94 static int show_raw_insn; /* --show-raw-insn */
95 static int dump_dwarf_section_info; /* --dwarf */
96 static int dump_stab_section_info; /* --stabs */
97 static int do_demangle; /* -C, --demangle */
98 static bfd_boolean disassemble; /* -d */
99 static bfd_boolean disassemble_all; /* -D */
100 static int disassemble_zeroes; /* --disassemble-zeroes */
101 static bfd_boolean formats_info; /* -i */
102 static int wide_output; /* -w */
103 static bfd_vma start_address = (bfd_vma) -1; /* --start-address */
104 static bfd_vma stop_address = (bfd_vma) -1; /* --stop-address */
105 static int dump_debugging; /* --debugging */
106 static int dump_debugging_tags; /* --debugging-tags */
107 static int dump_special_syms = 0; /* --special-syms */
108 static bfd_vma adjust_section_vma = 0; /* --adjust-vma */
109 static int file_start_context = 0; /* --file-start-context */
111 /* Pointer to an array of section names provided by
112 one or more "-j secname" command line options. */
113 static char **only;
114 /* The total number of slots in the only[] array. */
115 static size_t only_size = 0;
116 /* The number of occupied slots in the only[] array. */
117 static size_t only_used = 0;
119 /* Variables for handling include file path table. */
120 static const char **include_paths;
121 static int include_path_count;
123 /* Extra info to pass to the section disassembler and address printing
124 function. */
125 struct objdump_disasm_info
127 bfd * abfd;
128 asection * sec;
129 bfd_boolean require_sec;
130 arelent ** dynrelbuf;
131 long dynrelcount;
132 disassembler_ftype disassemble_fn;
133 #ifdef DISASSEMBLER_NEEDS_RELOCS
134 arelent * reloc;
135 #endif
138 /* Architecture to disassemble for, or default if NULL. */
139 static char *machine = NULL;
141 /* Target specific options to the disassembler. */
142 static char *disassembler_options = NULL;
144 /* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */
145 static enum bfd_endian endian = BFD_ENDIAN_UNKNOWN;
147 /* The symbol table. */
148 static asymbol **syms;
150 /* Number of symbols in `syms'. */
151 static long symcount = 0;
153 /* The sorted symbol table. */
154 static asymbol **sorted_syms;
156 /* Number of symbols in `sorted_syms'. */
157 static long sorted_symcount = 0;
159 /* The dynamic symbol table. */
160 static asymbol **dynsyms;
162 /* The synthetic symbol table. */
163 static asymbol *synthsyms;
164 static long synthcount = 0;
166 /* Number of symbols in `dynsyms'. */
167 static long dynsymcount = 0;
169 static bfd_byte *stabs;
170 static bfd_size_type stab_size;
172 static char *strtab;
173 static bfd_size_type stabstr_size;
175 static void
176 usage (FILE *stream, int status)
178 fprintf (stream, _("Usage: %s <option(s)> <file(s)>\n"), program_name);
179 fprintf (stream, _(" Display information from object <file(s)>.\n"));
180 fprintf (stream, _(" At least one of the following switches must be given:\n"));
181 fprintf (stream, _("\
182 -a, --archive-headers Display archive header information\n\
183 -f, --file-headers Display the contents of the overall file header\n\
184 -p, --private-headers Display object format specific file header contents\n\
185 -h, --[section-]headers Display the contents of the section headers\n\
186 -x, --all-headers Display the contents of all headers\n\
187 -d, --disassemble Display assembler contents of executable sections\n\
188 -D, --disassemble-all Display assembler contents of all sections\n\
189 -S, --source Intermix source code with disassembly\n\
190 -s, --full-contents Display the full contents of all sections requested\n\
191 -g, --debugging Display debug information in object file\n\
192 -e, --debugging-tags Display debug information using ctags style\n\
193 -G, --stabs Display (in raw form) any STABS info in the file\n\
194 -W, --dwarf Display DWARF info in the file\n\
195 -t, --syms Display the contents of the symbol table(s)\n\
196 -T, --dynamic-syms Display the contents of the dynamic symbol table\n\
197 -r, --reloc Display the relocation entries in the file\n\
198 -R, --dynamic-reloc Display the dynamic relocation entries in the file\n\
199 @<file> Read options from <file>\n\
200 -v, --version Display this program's version number\n\
201 -i, --info List object formats and architectures supported\n\
202 -H, --help Display this information\n\
203 "));
204 if (status != 2)
206 fprintf (stream, _("\n The following switches are optional:\n"));
207 fprintf (stream, _("\
208 -b, --target=BFDNAME Specify the target object format as BFDNAME\n\
209 -m, --architecture=MACHINE Specify the target architecture as MACHINE\n\
210 -j, --section=NAME Only display information for section NAME\n\
211 -M, --disassembler-options=OPT Pass text OPT on to the disassembler\n\
212 -EB --endian=big Assume big endian format when disassembling\n\
213 -EL --endian=little Assume little endian format when disassembling\n\
214 --file-start-context Include context from start of file (with -S)\n\
215 -I, --include=DIR Add DIR to search list for source files\n\
216 -l, --line-numbers Include line numbers and filenames in output\n\
217 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n\
218 The STYLE, if specified, can be `auto', `gnu',\n\
219 `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
220 or `gnat'\n\
221 -w, --wide Format output for more than 80 columns\n\
222 -z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling\n\
223 --start-address=ADDR Only process data whose address is >= ADDR\n\
224 --stop-address=ADDR Only process data whose address is <= ADDR\n\
225 --prefix-addresses Print complete address alongside disassembly\n\
226 --[no-]show-raw-insn Display hex alongside symbolic disassembly\n\
227 --adjust-vma=OFFSET Add OFFSET to all displayed section addresses\n\
228 --special-syms Include special symbols in symbol dumps\n\
229 \n"));
230 list_supported_targets (program_name, stream);
231 list_supported_architectures (program_name, stream);
233 disassembler_usage (stream);
235 if (status == 0)
236 fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
237 exit (status);
240 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
241 enum option_values
243 OPTION_ENDIAN=150,
244 OPTION_START_ADDRESS,
245 OPTION_STOP_ADDRESS,
246 OPTION_ADJUST_VMA
249 static struct option long_options[]=
251 {"adjust-vma", required_argument, NULL, OPTION_ADJUST_VMA},
252 {"all-headers", no_argument, NULL, 'x'},
253 {"private-headers", no_argument, NULL, 'p'},
254 {"architecture", required_argument, NULL, 'm'},
255 {"archive-headers", no_argument, NULL, 'a'},
256 {"debugging", no_argument, NULL, 'g'},
257 {"debugging-tags", no_argument, NULL, 'e'},
258 {"demangle", optional_argument, NULL, 'C'},
259 {"disassemble", no_argument, NULL, 'd'},
260 {"disassemble-all", no_argument, NULL, 'D'},
261 {"disassembler-options", required_argument, NULL, 'M'},
262 {"disassemble-zeroes", no_argument, NULL, 'z'},
263 {"dynamic-reloc", no_argument, NULL, 'R'},
264 {"dynamic-syms", no_argument, NULL, 'T'},
265 {"endian", required_argument, NULL, OPTION_ENDIAN},
266 {"file-headers", no_argument, NULL, 'f'},
267 {"file-start-context", no_argument, &file_start_context, 1},
268 {"full-contents", no_argument, NULL, 's'},
269 {"headers", no_argument, NULL, 'h'},
270 {"help", no_argument, NULL, 'H'},
271 {"info", no_argument, NULL, 'i'},
272 {"line-numbers", no_argument, NULL, 'l'},
273 {"no-show-raw-insn", no_argument, &show_raw_insn, -1},
274 {"prefix-addresses", no_argument, &prefix_addresses, 1},
275 {"reloc", no_argument, NULL, 'r'},
276 {"section", required_argument, NULL, 'j'},
277 {"section-headers", no_argument, NULL, 'h'},
278 {"show-raw-insn", no_argument, &show_raw_insn, 1},
279 {"source", no_argument, NULL, 'S'},
280 {"special-syms", no_argument, &dump_special_syms, 1},
281 {"include", required_argument, NULL, 'I'},
282 {"dwarf", no_argument, NULL, 'W'},
283 {"stabs", no_argument, NULL, 'G'},
284 {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
285 {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
286 {"syms", no_argument, NULL, 't'},
287 {"target", required_argument, NULL, 'b'},
288 {"version", no_argument, NULL, 'V'},
289 {"wide", no_argument, NULL, 'w'},
290 {0, no_argument, 0, 0}
293 static void
294 nonfatal (const char *msg)
296 bfd_nonfatal (msg);
297 exit_status = 1;
300 static void
301 dump_section_header (bfd *abfd, asection *section,
302 void *ignored ATTRIBUTE_UNUSED)
304 char *comma = "";
305 unsigned int opb = bfd_octets_per_byte (abfd);
307 /* Ignore linker created section. See elfNN_ia64_object_p in
308 bfd/elfxx-ia64.c. */
309 if (section->flags & SEC_LINKER_CREATED)
310 return;
312 printf ("%3d %-13s %08lx ", section->index,
313 bfd_get_section_name (abfd, section),
314 (unsigned long) bfd_section_size (abfd, section) / opb);
315 bfd_printf_vma (abfd, bfd_get_section_vma (abfd, section));
316 printf (" ");
317 bfd_printf_vma (abfd, section->lma);
318 printf (" %08lx 2**%u", (unsigned long) 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_LOAD, "LOAD");
331 PF (SEC_RELOC, "RELOC");
332 PF (SEC_READONLY, "READONLY");
333 PF (SEC_CODE, "CODE");
334 PF (SEC_DATA, "DATA");
335 PF (SEC_ROM, "ROM");
336 PF (SEC_DEBUGGING, "DEBUGGING");
337 PF (SEC_NEVER_LOAD, "NEVER_LOAD");
338 PF (SEC_EXCLUDE, "EXCLUDE");
339 PF (SEC_SORT_ENTRIES, "SORT_ENTRIES");
340 if (bfd_get_arch (abfd) == bfd_arch_tic54x)
342 PF (SEC_TIC54X_BLOCK, "BLOCK");
343 PF (SEC_TIC54X_CLINK, "CLINK");
345 PF (SEC_SMALL_DATA, "SMALL_DATA");
346 if (bfd_get_flavour (abfd) == bfd_target_coff_flavour)
347 PF (SEC_COFF_SHARED, "SHARED");
348 PF (SEC_THREAD_LOCAL, "THREAD_LOCAL");
349 PF (SEC_GROUP, "GROUP");
351 if ((section->flags & SEC_LINK_ONCE) != 0)
353 const char *ls;
354 struct coff_comdat_info *comdat;
356 switch (section->flags & SEC_LINK_DUPLICATES)
358 default:
359 abort ();
360 case SEC_LINK_DUPLICATES_DISCARD:
361 ls = "LINK_ONCE_DISCARD";
362 break;
363 case SEC_LINK_DUPLICATES_ONE_ONLY:
364 ls = "LINK_ONCE_ONE_ONLY";
365 break;
366 case SEC_LINK_DUPLICATES_SAME_SIZE:
367 ls = "LINK_ONCE_SAME_SIZE";
368 break;
369 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
370 ls = "LINK_ONCE_SAME_CONTENTS";
371 break;
373 printf ("%s%s", comma, ls);
375 comdat = bfd_coff_get_comdat_section (abfd, section);
376 if (comdat != NULL)
377 printf (" (COMDAT %s %ld)", comdat->name, comdat->symbol);
379 comma = ", ";
382 printf ("\n");
383 #undef PF
386 static void
387 dump_headers (bfd *abfd)
389 printf (_("Sections:\n"));
391 #ifndef BFD64
392 printf (_("Idx Name Size VMA LMA File off Algn"));
393 #else
394 /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */
395 if (bfd_get_arch_size (abfd) == 32)
396 printf (_("Idx Name Size VMA LMA File off Algn"));
397 else
398 printf (_("Idx Name Size VMA LMA File off Algn"));
399 #endif
401 if (wide_output)
402 printf (_(" Flags"));
403 if (abfd->flags & HAS_LOAD_PAGE)
404 printf (_(" Pg"));
405 printf ("\n");
407 bfd_map_over_sections (abfd, dump_section_header, NULL);
410 static asymbol **
411 slurp_symtab (bfd *abfd)
413 asymbol **sy = NULL;
414 long storage;
416 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
418 symcount = 0;
419 return NULL;
422 storage = bfd_get_symtab_upper_bound (abfd);
423 if (storage < 0)
424 bfd_fatal (bfd_get_filename (abfd));
425 if (storage)
426 sy = xmalloc (storage);
428 symcount = bfd_canonicalize_symtab (abfd, sy);
429 if (symcount < 0)
430 bfd_fatal (bfd_get_filename (abfd));
431 return sy;
434 /* Read in the dynamic symbols. */
436 static asymbol **
437 slurp_dynamic_symtab (bfd *abfd)
439 asymbol **sy = NULL;
440 long storage;
442 storage = bfd_get_dynamic_symtab_upper_bound (abfd);
443 if (storage < 0)
445 if (!(bfd_get_file_flags (abfd) & DYNAMIC))
447 non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd));
448 dynsymcount = 0;
449 return NULL;
452 bfd_fatal (bfd_get_filename (abfd));
454 if (storage)
455 sy = xmalloc (storage);
457 dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
458 if (dynsymcount < 0)
459 bfd_fatal (bfd_get_filename (abfd));
460 return sy;
463 /* Filter out (in place) symbols that are useless for disassembly.
464 COUNT is the number of elements in SYMBOLS.
465 Return the number of useful symbols. */
467 static long
468 remove_useless_symbols (asymbol **symbols, long count)
470 asymbol **in_ptr = symbols, **out_ptr = symbols;
472 while (--count >= 0)
474 asymbol *sym = *in_ptr++;
476 if (sym->name == NULL || sym->name[0] == '\0')
477 continue;
478 if (sym->flags & (BSF_DEBUGGING | BSF_SECTION_SYM))
479 continue;
480 if (bfd_is_und_section (sym->section)
481 || bfd_is_com_section (sym->section))
482 continue;
484 *out_ptr++ = sym;
486 return out_ptr - symbols;
489 /* Sort symbols into value order. */
491 static int
492 compare_symbols (const void *ap, const void *bp)
494 const asymbol *a = * (const asymbol **) ap;
495 const asymbol *b = * (const asymbol **) bp;
496 const char *an;
497 const char *bn;
498 size_t anl;
499 size_t bnl;
500 bfd_boolean af;
501 bfd_boolean bf;
502 flagword aflags;
503 flagword bflags;
505 if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
506 return 1;
507 else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
508 return -1;
510 if (a->section > b->section)
511 return 1;
512 else if (a->section < b->section)
513 return -1;
515 an = bfd_asymbol_name (a);
516 bn = bfd_asymbol_name (b);
517 anl = strlen (an);
518 bnl = strlen (bn);
520 /* The symbols gnu_compiled and gcc2_compiled convey no real
521 information, so put them after other symbols with the same value. */
522 af = (strstr (an, "gnu_compiled") != NULL
523 || strstr (an, "gcc2_compiled") != NULL);
524 bf = (strstr (bn, "gnu_compiled") != NULL
525 || strstr (bn, "gcc2_compiled") != NULL);
527 if (af && ! bf)
528 return 1;
529 if (! af && bf)
530 return -1;
532 /* We use a heuristic for the file name, to try to sort it after
533 more useful symbols. It may not work on non Unix systems, but it
534 doesn't really matter; the only difference is precisely which
535 symbol names get printed. */
537 #define file_symbol(s, sn, snl) \
538 (((s)->flags & BSF_FILE) != 0 \
539 || ((sn)[(snl) - 2] == '.' \
540 && ((sn)[(snl) - 1] == 'o' \
541 || (sn)[(snl) - 1] == 'a')))
543 af = file_symbol (a, an, anl);
544 bf = file_symbol (b, bn, bnl);
546 if (af && ! bf)
547 return 1;
548 if (! af && bf)
549 return -1;
551 /* Try to sort global symbols before local symbols before function
552 symbols before debugging symbols. */
554 aflags = a->flags;
555 bflags = b->flags;
557 if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING))
559 if ((aflags & BSF_DEBUGGING) != 0)
560 return 1;
561 else
562 return -1;
564 if ((aflags & BSF_FUNCTION) != (bflags & BSF_FUNCTION))
566 if ((aflags & BSF_FUNCTION) != 0)
567 return -1;
568 else
569 return 1;
571 if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL))
573 if ((aflags & BSF_LOCAL) != 0)
574 return 1;
575 else
576 return -1;
578 if ((aflags & BSF_GLOBAL) != (bflags & BSF_GLOBAL))
580 if ((aflags & BSF_GLOBAL) != 0)
581 return -1;
582 else
583 return 1;
586 /* Symbols that start with '.' might be section names, so sort them
587 after symbols that don't start with '.'. */
588 if (an[0] == '.' && bn[0] != '.')
589 return 1;
590 if (an[0] != '.' && bn[0] == '.')
591 return -1;
593 /* Finally, if we can't distinguish them in any other way, try to
594 get consistent results by sorting the symbols by name. */
595 return strcmp (an, bn);
598 /* Sort relocs into address order. */
600 static int
601 compare_relocs (const void *ap, const void *bp)
603 const arelent *a = * (const arelent **) ap;
604 const arelent *b = * (const arelent **) bp;
606 if (a->address > b->address)
607 return 1;
608 else if (a->address < b->address)
609 return -1;
611 /* So that associated relocations tied to the same address show up
612 in the correct order, we don't do any further sorting. */
613 if (a > b)
614 return 1;
615 else if (a < b)
616 return -1;
617 else
618 return 0;
621 /* Print an address (VMA) to the output stream in INFO.
622 If SKIP_ZEROES is TRUE, omit leading zeroes. */
624 static void
625 objdump_print_value (bfd_vma vma, struct disassemble_info *info,
626 bfd_boolean skip_zeroes)
628 char buf[30];
629 char *p;
630 struct objdump_disasm_info *aux;
632 aux = (struct objdump_disasm_info *) info->application_data;
633 bfd_sprintf_vma (aux->abfd, buf, vma);
634 if (! skip_zeroes)
635 p = buf;
636 else
638 for (p = buf; *p == '0'; ++p)
640 if (*p == '\0')
641 --p;
643 (*info->fprintf_func) (info->stream, "%s", p);
646 /* Print the name of a symbol. */
648 static void
649 objdump_print_symname (bfd *abfd, struct disassemble_info *info,
650 asymbol *sym)
652 char *alloc;
653 const char *name;
655 alloc = NULL;
656 name = bfd_asymbol_name (sym);
657 if (do_demangle && name[0] != '\0')
659 /* Demangle the name. */
660 alloc = demangle (abfd, name);
661 name = alloc;
664 if (info != NULL)
665 (*info->fprintf_func) (info->stream, "%s", name);
666 else
667 printf ("%s", name);
669 if (alloc != NULL)
670 free (alloc);
673 /* Locate a symbol given a bfd and a section (from INFO->application_data),
674 and a VMA. If INFO->application_data->require_sec is TRUE, then always
675 require the symbol to be in the section. Returns NULL if there is no
676 suitable symbol. If PLACE is not NULL, then *PLACE is set to the index
677 of the symbol in sorted_syms. */
679 static asymbol *
680 find_symbol_for_address (bfd_vma vma,
681 struct disassemble_info *info,
682 long *place)
684 /* @@ Would it speed things up to cache the last two symbols returned,
685 and maybe their address ranges? For many processors, only one memory
686 operand can be present at a time, so the 2-entry cache wouldn't be
687 constantly churned by code doing heavy memory accesses. */
689 /* Indices in `sorted_syms'. */
690 long min = 0;
691 long max = sorted_symcount;
692 long thisplace;
693 struct objdump_disasm_info *aux;
694 bfd *abfd;
695 asection *sec;
696 unsigned int opb;
698 if (sorted_symcount < 1)
699 return NULL;
701 aux = (struct objdump_disasm_info *) info->application_data;
702 abfd = aux->abfd;
703 sec = aux->sec;
704 opb = bfd_octets_per_byte (abfd);
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 relocatable, 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. */
743 if (sorted_syms[thisplace]->section != sec
744 && (aux->require_sec
745 || ((abfd->flags & HAS_RELOC) != 0
746 && vma >= bfd_get_section_vma (abfd, sec)
747 && vma < (bfd_get_section_vma (abfd, sec)
748 + bfd_section_size (abfd, sec) / opb))))
750 long i;
752 for (i = thisplace + 1; i < sorted_symcount; i++)
754 if (bfd_asymbol_value (sorted_syms[i])
755 != bfd_asymbol_value (sorted_syms[thisplace]))
756 break;
759 --i;
761 for (; i >= 0; i--)
763 if (sorted_syms[i]->section == sec
764 && (i == 0
765 || sorted_syms[i - 1]->section != sec
766 || (bfd_asymbol_value (sorted_syms[i])
767 != bfd_asymbol_value (sorted_syms[i - 1]))))
769 thisplace = i;
770 break;
774 if (sorted_syms[thisplace]->section != sec)
776 /* We didn't find a good symbol with a smaller value.
777 Look for one with a larger value. */
778 for (i = thisplace + 1; i < sorted_symcount; i++)
780 if (sorted_syms[i]->section == sec)
782 thisplace = i;
783 break;
788 if (sorted_syms[thisplace]->section != sec
789 && (aux->require_sec
790 || ((abfd->flags & HAS_RELOC) != 0
791 && vma >= bfd_get_section_vma (abfd, sec)
792 && vma < (bfd_get_section_vma (abfd, sec)
793 + bfd_section_size (abfd, sec)))))
794 /* There is no suitable symbol. */
795 return NULL;
798 /* Give the target a chance to reject the symbol. */
799 while (! info->symbol_is_valid (sorted_syms [thisplace], info))
801 ++ thisplace;
802 if (thisplace >= sorted_symcount
803 || bfd_asymbol_value (sorted_syms [thisplace]) > vma)
804 return NULL;
807 if (place != NULL)
808 *place = thisplace;
810 return sorted_syms[thisplace];
813 /* Print an address and the offset to the nearest symbol. */
815 static void
816 objdump_print_addr_with_sym (bfd *abfd, asection *sec, asymbol *sym,
817 bfd_vma vma, struct disassemble_info *info,
818 bfd_boolean skip_zeroes)
820 objdump_print_value (vma, info, skip_zeroes);
822 if (sym == NULL)
824 bfd_vma secaddr;
826 (*info->fprintf_func) (info->stream, " <%s",
827 bfd_get_section_name (abfd, sec));
828 secaddr = bfd_get_section_vma (abfd, sec);
829 if (vma < secaddr)
831 (*info->fprintf_func) (info->stream, "-0x");
832 objdump_print_value (secaddr - vma, info, TRUE);
834 else if (vma > secaddr)
836 (*info->fprintf_func) (info->stream, "+0x");
837 objdump_print_value (vma - secaddr, info, TRUE);
839 (*info->fprintf_func) (info->stream, ">");
841 else
843 (*info->fprintf_func) (info->stream, " <");
844 objdump_print_symname (abfd, info, sym);
845 if (bfd_asymbol_value (sym) > vma)
847 (*info->fprintf_func) (info->stream, "-0x");
848 objdump_print_value (bfd_asymbol_value (sym) - vma, info, TRUE);
850 else if (vma > bfd_asymbol_value (sym))
852 (*info->fprintf_func) (info->stream, "+0x");
853 objdump_print_value (vma - bfd_asymbol_value (sym), info, TRUE);
855 (*info->fprintf_func) (info->stream, ">");
859 /* Print an address (VMA), symbolically if possible.
860 If SKIP_ZEROES is TRUE, don't output leading zeroes. */
862 static void
863 objdump_print_addr (bfd_vma vma,
864 struct disassemble_info *info,
865 bfd_boolean skip_zeroes)
867 struct objdump_disasm_info *aux;
868 asymbol *sym = NULL; /* Initialize to avoid compiler warning. */
869 #ifdef DISASSEMBLER_NEEDS_RELOCS
870 bfd_boolean skip_find = FALSE;
871 #endif
873 if (sorted_symcount < 1)
875 (*info->fprintf_func) (info->stream, "0x");
876 objdump_print_value (vma, info, skip_zeroes);
877 return;
880 aux = (struct objdump_disasm_info *) info->application_data;
882 #ifdef DISASSEMBLER_NEEDS_RELOCS
883 if (aux->reloc != NULL
884 && aux->reloc->sym_ptr_ptr != NULL
885 && * aux->reloc->sym_ptr_ptr != NULL)
887 sym = * aux->reloc->sym_ptr_ptr;
889 /* Adjust the vma to the reloc. */
890 vma += bfd_asymbol_value (sym);
892 if (bfd_is_und_section (bfd_get_section (sym)))
893 skip_find = TRUE;
896 if (!skip_find)
897 #endif
898 sym = find_symbol_for_address (vma, info, NULL);
900 objdump_print_addr_with_sym (aux->abfd, aux->sec, sym, vma, info,
901 skip_zeroes);
904 /* Print VMA to INFO. This function is passed to the disassembler
905 routine. */
907 static void
908 objdump_print_address (bfd_vma vma, struct disassemble_info *info)
910 objdump_print_addr (vma, info, ! prefix_addresses);
913 /* Determine of the given address has a symbol associated with it. */
915 static int
916 objdump_symbol_at_address (bfd_vma vma, struct disassemble_info * info)
918 asymbol * sym;
920 sym = find_symbol_for_address (vma, info, NULL);
922 return (sym != NULL && (bfd_asymbol_value (sym) == vma));
925 /* Hold the last function name and the last line number we displayed
926 in a disassembly. */
928 static char *prev_functionname;
929 static unsigned int prev_line;
931 /* We keep a list of all files that we have seen when doing a
932 disassembly with source, so that we know how much of the file to
933 display. This can be important for inlined functions. */
935 struct print_file_list
937 struct print_file_list *next;
938 const char *filename;
939 const char *modname;
940 unsigned int line;
941 FILE *f;
944 static struct print_file_list *print_files;
946 /* The number of preceding context lines to show when we start
947 displaying a file for the first time. */
949 #define SHOW_PRECEDING_CONTEXT_LINES (5)
951 /* Tries to open MODNAME, and if successful adds a node to print_files
952 linked list and returns that node. Returns NULL on failure. */
954 static struct print_file_list *
955 try_print_file_open (const char *origname, const char *modname)
957 struct print_file_list *p;
958 FILE *f;
960 f = fopen (modname, "r");
961 if (f == NULL)
962 return NULL;
964 if (print_files != NULL && print_files->f != NULL)
966 fclose (print_files->f);
967 print_files->f = NULL;
970 p = xmalloc (sizeof (struct print_file_list));
971 p->filename = origname;
972 p->modname = modname;
973 p->line = 0;
974 p->f = f;
975 p->next = print_files;
976 print_files = p;
977 return p;
980 /* If the the source file, as described in the symtab, is not found
981 try to locate it in one of the paths specified with -I
982 If found, add location to print_files linked list. */
984 static struct print_file_list *
985 update_source_path (const char *filename)
987 struct print_file_list *p;
988 const char *fname;
989 int i;
991 if (filename == NULL)
992 return NULL;
994 p = try_print_file_open (filename, filename);
995 if (p != NULL)
996 return p;
998 if (include_path_count == 0)
999 return NULL;
1001 /* Get the name of the file. */
1002 fname = strrchr (filename, '/');
1003 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
1005 /* We could have a mixed forward/back slash case. */
1006 char *backslash = strrchr (filename, '\\');
1007 if (fname == NULL || (backslash != NULL && backslash > fname))
1008 fname = backslash;
1009 if (fname == NULL && filename[0] != '\0' && filename[1] == ':')
1010 fname = filename + 1;
1012 #endif
1013 if (fname == NULL)
1014 fname = filename;
1015 else
1016 ++fname;
1018 /* If file exists under a new path, we need to add it to the list
1019 so that show_line knows about it. */
1020 for (i = 0; i < include_path_count; i++)
1022 char *modname = concat (include_paths[i], "/", fname, (const char *) 0);
1024 p = try_print_file_open (filename, modname);
1025 if (p)
1026 return p;
1028 free (modname);
1031 return NULL;
1034 /* Skip ahead to a given line in a file, optionally printing each
1035 line. */
1037 static void
1038 skip_to_line (struct print_file_list *p, unsigned int line,
1039 bfd_boolean show)
1041 while (p->line < line)
1043 char buf[100];
1045 if (fgets (buf, sizeof buf, p->f) == NULL)
1047 fclose (p->f);
1048 p->f = NULL;
1049 break;
1052 if (show)
1053 printf ("%s", buf);
1055 if (strchr (buf, '\n') != NULL)
1056 ++p->line;
1060 /* Show the line number, or the source line, in a disassembly
1061 listing. */
1063 static void
1064 show_line (bfd *abfd, asection *section, bfd_vma addr_offset)
1066 const char *filename;
1067 const char *functionname;
1068 unsigned int line;
1070 if (! with_line_numbers && ! with_source_code)
1071 return;
1073 if (! bfd_find_nearest_line (abfd, section, syms, addr_offset, &filename,
1074 &functionname, &line))
1075 return;
1077 if (filename != NULL && *filename == '\0')
1078 filename = NULL;
1079 if (functionname != NULL && *functionname == '\0')
1080 functionname = NULL;
1082 if (with_line_numbers)
1084 if (functionname != NULL
1085 && (prev_functionname == NULL
1086 || strcmp (functionname, prev_functionname) != 0))
1087 printf ("%s():\n", functionname);
1088 if (line > 0 && line != prev_line)
1089 printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
1092 if (with_source_code
1093 && filename != NULL
1094 && line > 0)
1096 struct print_file_list **pp, *p;
1098 for (pp = &print_files; *pp != NULL; pp = &(*pp)->next)
1099 if (strcmp ((*pp)->filename, filename) == 0)
1100 break;
1101 p = *pp;
1103 if (p != NULL)
1105 if (p != print_files)
1107 int l;
1109 /* We have reencountered a file name which we saw
1110 earlier. This implies that either we are dumping out
1111 code from an included file, or the same file was
1112 linked in more than once. There are two common cases
1113 of an included file: inline functions in a header
1114 file, and a bison or flex skeleton file. In the
1115 former case we want to just start printing (but we
1116 back up a few lines to give context); in the latter
1117 case we want to continue from where we left off. I
1118 can't think of a good way to distinguish the cases,
1119 so I used a heuristic based on the file name. */
1120 if (strcmp (p->filename + strlen (p->filename) - 2, ".h") != 0)
1121 l = p->line;
1122 else
1124 l = line - SHOW_PRECEDING_CONTEXT_LINES;
1125 if (l < 0)
1126 l = 0;
1129 if (p->f == NULL)
1131 p->f = fopen (p->modname, "r");
1132 p->line = 0;
1134 if (p->f != NULL)
1135 skip_to_line (p, l, FALSE);
1137 if (print_files->f != NULL)
1139 fclose (print_files->f);
1140 print_files->f = NULL;
1144 if (p->f != NULL)
1146 skip_to_line (p, line, TRUE);
1147 *pp = p->next;
1148 p->next = print_files;
1149 print_files = p;
1152 else
1154 p = update_source_path (filename);
1156 if (p != NULL)
1158 int l;
1160 if (file_start_context)
1161 l = 0;
1162 else
1163 l = line - SHOW_PRECEDING_CONTEXT_LINES;
1164 if (l < 0)
1165 l = 0;
1166 skip_to_line (p, l, FALSE);
1167 if (p->f != NULL)
1168 skip_to_line (p, line, TRUE);
1173 if (functionname != NULL
1174 && (prev_functionname == NULL
1175 || strcmp (functionname, prev_functionname) != 0))
1177 if (prev_functionname != NULL)
1178 free (prev_functionname);
1179 prev_functionname = xmalloc (strlen (functionname) + 1);
1180 strcpy (prev_functionname, functionname);
1183 if (line > 0 && line != prev_line)
1184 prev_line = line;
1187 /* Pseudo FILE object for strings. */
1188 typedef struct
1190 char *buffer;
1191 size_t pos;
1192 size_t alloc;
1193 } SFILE;
1195 /* sprintf to a "stream". */
1197 static int ATTRIBUTE_PRINTF_2
1198 objdump_sprintf (SFILE *f, const char *format, ...)
1200 size_t n;
1201 va_list args;
1203 while (1)
1205 size_t space = f->alloc - f->pos;
1207 va_start (args, format);
1208 n = vsnprintf (f->buffer + f->pos, space, format, args);
1209 va_end (args);
1211 if (space > n)
1212 break;
1214 f->alloc = (f->alloc + n) * 2;
1215 f->buffer = xrealloc (f->buffer, f->alloc);
1217 f->pos += n;
1219 return n;
1222 /* Returns TRUE if the specified section should be dumped. */
1224 static bfd_boolean
1225 process_section_p (asection * section)
1227 size_t i;
1229 if (only == NULL)
1230 return TRUE;
1232 for (i = 0; i < only_used; i++)
1233 if (strcmp (only [i], section->name) == 0)
1234 return TRUE;
1236 return FALSE;
1240 /* The number of zeroes we want to see before we start skipping them.
1241 The number is arbitrarily chosen. */
1243 #define DEFAULT_SKIP_ZEROES 8
1245 /* The number of zeroes to skip at the end of a section. If the
1246 number of zeroes at the end is between SKIP_ZEROES_AT_END and
1247 SKIP_ZEROES, they will be disassembled. If there are fewer than
1248 SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic
1249 attempt to avoid disassembling zeroes inserted by section
1250 alignment. */
1252 #define DEFAULT_SKIP_ZEROES_AT_END 3
1254 /* Disassemble some data in memory between given values. */
1256 static void
1257 disassemble_bytes (struct disassemble_info * info,
1258 disassembler_ftype disassemble_fn,
1259 bfd_boolean insns,
1260 bfd_byte * data,
1261 bfd_vma start_offset,
1262 bfd_vma stop_offset,
1263 bfd_vma rel_offset,
1264 arelent *** relppp,
1265 arelent ** relppend)
1267 struct objdump_disasm_info *aux;
1268 asection *section;
1269 int octets_per_line;
1270 bfd_boolean done_dot;
1271 int skip_addr_chars;
1272 bfd_vma addr_offset;
1273 unsigned int opb = info->octets_per_byte;
1274 unsigned int skip_zeroes = info->skip_zeroes;
1275 unsigned int skip_zeroes_at_end = info->skip_zeroes_at_end;
1276 int octets = opb;
1277 SFILE sfile;
1279 aux = (struct objdump_disasm_info *) info->application_data;
1280 section = aux->sec;
1282 sfile.alloc = 120;
1283 sfile.buffer = xmalloc (sfile.alloc);
1284 sfile.pos = 0;
1286 if (insns)
1287 octets_per_line = 4;
1288 else
1289 octets_per_line = 16;
1291 /* Figure out how many characters to skip at the start of an
1292 address, to make the disassembly look nicer. We discard leading
1293 zeroes in chunks of 4, ensuring that there is always a leading
1294 zero remaining. */
1295 skip_addr_chars = 0;
1296 if (! prefix_addresses)
1298 char buf[30];
1299 char *s;
1301 bfd_sprintf_vma
1302 (aux->abfd, buf,
1303 (section->vma
1304 + bfd_section_size (section->owner, section) / opb));
1305 s = buf;
1306 while (s[0] == '0' && s[1] == '0' && s[2] == '0' && s[3] == '0'
1307 && s[4] == '0')
1309 skip_addr_chars += 4;
1310 s += 4;
1314 info->insn_info_valid = 0;
1316 done_dot = FALSE;
1317 addr_offset = start_offset;
1318 while (addr_offset < stop_offset)
1320 bfd_vma z;
1321 bfd_boolean need_nl = FALSE;
1322 #ifdef DISASSEMBLER_NEEDS_RELOCS
1323 int previous_octets;
1325 /* Remember the length of the previous instruction. */
1326 previous_octets = octets;
1327 #endif
1328 octets = 0;
1330 /* If we see more than SKIP_ZEROES octets of zeroes, we just
1331 print `...'. */
1332 for (z = addr_offset * opb; z < stop_offset * opb; z++)
1333 if (data[z] != 0)
1334 break;
1335 if (! disassemble_zeroes
1336 && (info->insn_info_valid == 0
1337 || info->branch_delay_insns == 0)
1338 && (z - addr_offset * opb >= skip_zeroes
1339 || (z == stop_offset * opb &&
1340 z - addr_offset * opb < skip_zeroes_at_end)))
1342 printf ("\t...\n");
1344 /* If there are more nonzero octets to follow, we only skip
1345 zeroes in multiples of 4, to try to avoid running over
1346 the start of an instruction which happens to start with
1347 zero. */
1348 if (z != stop_offset * opb)
1349 z = addr_offset * opb + ((z - addr_offset * opb) &~ 3);
1351 octets = z - addr_offset * opb;
1353 else
1355 char buf[50];
1356 int bpc = 0;
1357 int pb = 0;
1359 done_dot = FALSE;
1361 if (with_line_numbers || with_source_code)
1362 show_line (aux->abfd, section, addr_offset);
1364 if (! prefix_addresses)
1366 char *s;
1368 bfd_sprintf_vma (aux->abfd, buf, section->vma + addr_offset);
1369 for (s = buf + skip_addr_chars; *s == '0'; s++)
1370 *s = ' ';
1371 if (*s == '\0')
1372 *--s = '0';
1373 printf ("%s:\t", buf + skip_addr_chars);
1375 else
1377 aux->require_sec = TRUE;
1378 objdump_print_address (section->vma + addr_offset, info);
1379 aux->require_sec = FALSE;
1380 putchar (' ');
1383 if (insns)
1385 sfile.pos = 0;
1386 info->fprintf_func = (fprintf_ftype) objdump_sprintf;
1387 info->stream = &sfile;
1388 info->bytes_per_line = 0;
1389 info->bytes_per_chunk = 0;
1390 info->flags = 0;
1392 #ifdef DISASSEMBLER_NEEDS_RELOCS
1393 if (*relppp < relppend)
1395 bfd_signed_vma distance_to_rel;
1397 distance_to_rel = (**relppp)->address
1398 - (rel_offset + addr_offset);
1400 /* Check to see if the current reloc is associated with
1401 the instruction that we are about to disassemble. */
1402 if (distance_to_rel == 0
1403 /* FIXME: This is wrong. We are trying to catch
1404 relocs that are addressed part way through the
1405 current instruction, as might happen with a packed
1406 VLIW instruction. Unfortunately we do not know the
1407 length of the current instruction since we have not
1408 disassembled it yet. Instead we take a guess based
1409 upon the length of the previous instruction. The
1410 proper solution is to have a new target-specific
1411 disassembler function which just returns the length
1412 of an instruction at a given address without trying
1413 to display its disassembly. */
1414 || (distance_to_rel > 0
1415 && distance_to_rel < (bfd_signed_vma) (previous_octets/ opb)))
1417 info->flags = INSN_HAS_RELOC;
1418 aux->reloc = **relppp;
1420 else
1421 aux->reloc = NULL;
1423 #endif
1424 octets = (*disassemble_fn) (section->vma + addr_offset, info);
1425 info->fprintf_func = (fprintf_ftype) fprintf;
1426 info->stream = stdout;
1427 if (info->bytes_per_line != 0)
1428 octets_per_line = info->bytes_per_line;
1429 if (octets < 0)
1431 if (sfile.pos)
1432 printf ("%s\n", sfile.buffer);
1433 break;
1436 else
1438 bfd_vma j;
1440 octets = octets_per_line;
1441 if (addr_offset + octets / opb > stop_offset)
1442 octets = (stop_offset - addr_offset) * opb;
1444 for (j = addr_offset * opb; j < addr_offset * opb + octets; ++j)
1446 if (ISPRINT (data[j]))
1447 buf[j - addr_offset * opb] = data[j];
1448 else
1449 buf[j - addr_offset * opb] = '.';
1451 buf[j - addr_offset * opb] = '\0';
1454 if (prefix_addresses
1455 ? show_raw_insn > 0
1456 : show_raw_insn >= 0)
1458 bfd_vma j;
1460 /* If ! prefix_addresses and ! wide_output, we print
1461 octets_per_line octets per line. */
1462 pb = octets;
1463 if (pb > octets_per_line && ! prefix_addresses && ! wide_output)
1464 pb = octets_per_line;
1466 if (info->bytes_per_chunk)
1467 bpc = info->bytes_per_chunk;
1468 else
1469 bpc = 1;
1471 for (j = addr_offset * opb; j < addr_offset * opb + pb; j += bpc)
1473 int k;
1475 if (bpc > 1 && info->display_endian == BFD_ENDIAN_LITTLE)
1477 for (k = bpc - 1; k >= 0; k--)
1478 printf ("%02x", (unsigned) data[j + k]);
1479 putchar (' ');
1481 else
1483 for (k = 0; k < bpc; k++)
1484 printf ("%02x", (unsigned) data[j + k]);
1485 putchar (' ');
1489 for (; pb < octets_per_line; pb += bpc)
1491 int k;
1493 for (k = 0; k < bpc; k++)
1494 printf (" ");
1495 putchar (' ');
1498 /* Separate raw data from instruction by extra space. */
1499 if (insns)
1500 putchar ('\t');
1501 else
1502 printf (" ");
1505 if (! insns)
1506 printf ("%s", buf);
1507 else if (sfile.pos)
1508 printf ("%s", sfile.buffer);
1510 if (prefix_addresses
1511 ? show_raw_insn > 0
1512 : show_raw_insn >= 0)
1514 while (pb < octets)
1516 bfd_vma j;
1517 char *s;
1519 putchar ('\n');
1520 j = addr_offset * opb + pb;
1522 bfd_sprintf_vma (aux->abfd, buf, section->vma + j / opb);
1523 for (s = buf + skip_addr_chars; *s == '0'; s++)
1524 *s = ' ';
1525 if (*s == '\0')
1526 *--s = '0';
1527 printf ("%s:\t", buf + skip_addr_chars);
1529 pb += octets_per_line;
1530 if (pb > octets)
1531 pb = octets;
1532 for (; j < addr_offset * opb + pb; j += bpc)
1534 int k;
1536 if (bpc > 1 && info->display_endian == BFD_ENDIAN_LITTLE)
1538 for (k = bpc - 1; k >= 0; k--)
1539 printf ("%02x", (unsigned) data[j + k]);
1540 putchar (' ');
1542 else
1544 for (k = 0; k < bpc; k++)
1545 printf ("%02x", (unsigned) data[j + k]);
1546 putchar (' ');
1552 if (!wide_output)
1553 putchar ('\n');
1554 else
1555 need_nl = TRUE;
1558 while ((*relppp) < relppend
1559 && (**relppp)->address < rel_offset + addr_offset + octets / opb)
1561 if (dump_reloc_info || dump_dynamic_reloc_info)
1563 arelent *q;
1565 q = **relppp;
1567 if (wide_output)
1568 putchar ('\t');
1569 else
1570 printf ("\t\t\t");
1572 objdump_print_value (section->vma - rel_offset + q->address,
1573 info, TRUE);
1575 if (q->howto == NULL)
1576 printf (": *unknown*\t");
1577 else if (q->howto->name)
1578 printf (": %s\t", q->howto->name);
1579 else
1580 printf (": %d\t", q->howto->type);
1582 if (q->sym_ptr_ptr == NULL || *q->sym_ptr_ptr == NULL)
1583 printf ("*unknown*");
1584 else
1586 const char *sym_name;
1588 sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
1589 if (sym_name != NULL && *sym_name != '\0')
1590 objdump_print_symname (aux->abfd, info, *q->sym_ptr_ptr);
1591 else
1593 asection *sym_sec;
1595 sym_sec = bfd_get_section (*q->sym_ptr_ptr);
1596 sym_name = bfd_get_section_name (aux->abfd, sym_sec);
1597 if (sym_name == NULL || *sym_name == '\0')
1598 sym_name = "*unknown*";
1599 printf ("%s", sym_name);
1603 if (q->addend)
1605 printf ("+0x");
1606 objdump_print_value (q->addend, info, TRUE);
1609 printf ("\n");
1610 need_nl = FALSE;
1612 ++(*relppp);
1615 if (need_nl)
1616 printf ("\n");
1618 addr_offset += octets / opb;
1621 free (sfile.buffer);
1624 static void
1625 disassemble_section (bfd *abfd, asection *section, void *info)
1627 struct disassemble_info * pinfo = (struct disassemble_info *) info;
1628 struct objdump_disasm_info * paux;
1629 unsigned int opb = pinfo->octets_per_byte;
1630 bfd_byte * data = NULL;
1631 bfd_size_type datasize = 0;
1632 arelent ** rel_pp = NULL;
1633 arelent ** rel_ppstart = NULL;
1634 arelent ** rel_ppend;
1635 unsigned long stop_offset;
1636 asymbol * sym = NULL;
1637 long place = 0;
1638 long rel_count;
1639 bfd_vma rel_offset;
1640 unsigned long addr_offset;
1642 /* Sections that do not contain machine
1643 code are not normally disassembled. */
1644 if (! disassemble_all
1645 && only == NULL
1646 && ((section->flags & (SEC_CODE | SEC_HAS_CONTENTS))
1647 != (SEC_CODE | SEC_HAS_CONTENTS)))
1648 return;
1650 if (! process_section_p (section))
1651 return;
1653 datasize = bfd_get_section_size (section);
1654 if (datasize == 0)
1655 return;
1657 /* Decide which set of relocs to use. Load them if necessary. */
1658 paux = (struct objdump_disasm_info *) pinfo->application_data;
1659 if (paux->dynrelbuf)
1661 rel_pp = paux->dynrelbuf;
1662 rel_count = paux->dynrelcount;
1663 /* Dynamic reloc addresses are absolute, non-dynamic are section
1664 relative. REL_OFFSET specifies the reloc address corresponding
1665 to the start of this section. */
1666 rel_offset = section->vma;
1668 else
1670 rel_count = 0;
1671 rel_pp = NULL;
1672 rel_offset = 0;
1674 if ((section->flags & SEC_RELOC) != 0
1675 #ifndef DISASSEMBLER_NEEDS_RELOCS
1676 && dump_reloc_info
1677 #endif
1680 long relsize;
1682 relsize = bfd_get_reloc_upper_bound (abfd, section);
1683 if (relsize < 0)
1684 bfd_fatal (bfd_get_filename (abfd));
1686 if (relsize > 0)
1688 rel_ppstart = rel_pp = xmalloc (relsize);
1689 rel_count = bfd_canonicalize_reloc (abfd, section, rel_pp, syms);
1690 if (rel_count < 0)
1691 bfd_fatal (bfd_get_filename (abfd));
1693 /* Sort the relocs by address. */
1694 qsort (rel_pp, rel_count, sizeof (arelent *), compare_relocs);
1699 rel_ppend = rel_pp + rel_count;
1701 data = xmalloc (datasize);
1703 bfd_get_section_contents (abfd, section, data, 0, datasize);
1705 paux->sec = section;
1706 pinfo->buffer = data;
1707 pinfo->buffer_vma = section->vma;
1708 pinfo->buffer_length = datasize;
1709 pinfo->section = section;
1711 if (start_address == (bfd_vma) -1
1712 || start_address < pinfo->buffer_vma)
1713 addr_offset = 0;
1714 else
1715 addr_offset = start_address - pinfo->buffer_vma;
1717 if (stop_address == (bfd_vma) -1)
1718 stop_offset = datasize / opb;
1719 else
1721 if (stop_address < pinfo->buffer_vma)
1722 stop_offset = 0;
1723 else
1724 stop_offset = stop_address - pinfo->buffer_vma;
1725 if (stop_offset > pinfo->buffer_length / opb)
1726 stop_offset = pinfo->buffer_length / opb;
1729 /* Skip over the relocs belonging to addresses below the
1730 start address. */
1731 while (rel_pp < rel_ppend
1732 && (*rel_pp)->address < rel_offset + addr_offset)
1733 ++rel_pp;
1735 printf (_("Disassembly of section %s:\n"), section->name);
1737 /* Find the nearest symbol forwards from our current position. */
1738 paux->require_sec = TRUE;
1739 sym = find_symbol_for_address (section->vma + addr_offset, info, &place);
1740 paux->require_sec = FALSE;
1742 /* Disassemble a block of instructions up to the address associated with
1743 the symbol we have just found. Then print the symbol and find the
1744 next symbol on. Repeat until we have disassembled the entire section
1745 or we have reached the end of the address range we are interested in. */
1746 while (addr_offset < stop_offset)
1748 bfd_vma addr;
1749 asymbol *nextsym;
1750 unsigned long nextstop_offset;
1751 bfd_boolean insns;
1753 addr = section->vma + addr_offset;
1755 if (sym != NULL && bfd_asymbol_value (sym) <= addr)
1757 int x;
1759 for (x = place;
1760 (x < sorted_symcount
1761 && (bfd_asymbol_value (sorted_syms[x]) <= addr));
1762 ++x)
1763 continue;
1765 pinfo->symbols = sorted_syms + place;
1766 pinfo->num_symbols = x - place;
1768 else
1770 pinfo->symbols = NULL;
1771 pinfo->num_symbols = 0;
1774 if (! prefix_addresses)
1776 pinfo->fprintf_func (pinfo->stream, "\n");
1777 objdump_print_addr_with_sym (abfd, section, sym, addr,
1778 pinfo, FALSE);
1779 pinfo->fprintf_func (pinfo->stream, ":\n");
1782 if (sym != NULL && bfd_asymbol_value (sym) > addr)
1783 nextsym = sym;
1784 else if (sym == NULL)
1785 nextsym = NULL;
1786 else
1788 #define is_valid_next_sym(SYM) \
1789 ((SYM)->section == section \
1790 && (bfd_asymbol_value (SYM) > bfd_asymbol_value (sym)) \
1791 && pinfo->symbol_is_valid (SYM, pinfo))
1793 /* Search forward for the next appropriate symbol in
1794 SECTION. Note that all the symbols are sorted
1795 together into one big array, and that some sections
1796 may have overlapping addresses. */
1797 while (place < sorted_symcount
1798 && ! is_valid_next_sym (sorted_syms [place]))
1799 ++place;
1801 if (place >= sorted_symcount)
1802 nextsym = NULL;
1803 else
1804 nextsym = sorted_syms[place];
1807 if (sym != NULL && bfd_asymbol_value (sym) > addr)
1808 nextstop_offset = bfd_asymbol_value (sym) - section->vma;
1809 else if (nextsym == NULL)
1810 nextstop_offset = stop_offset;
1811 else
1812 nextstop_offset = bfd_asymbol_value (nextsym) - section->vma;
1814 if (nextstop_offset > stop_offset)
1815 nextstop_offset = stop_offset;
1817 /* If a symbol is explicitly marked as being an object
1818 rather than a function, just dump the bytes without
1819 disassembling them. */
1820 if (disassemble_all
1821 || sym == NULL
1822 || bfd_asymbol_value (sym) > addr
1823 || ((sym->flags & BSF_OBJECT) == 0
1824 && (strstr (bfd_asymbol_name (sym), "gnu_compiled")
1825 == NULL)
1826 && (strstr (bfd_asymbol_name (sym), "gcc2_compiled")
1827 == NULL))
1828 || (sym->flags & BSF_FUNCTION) != 0)
1829 insns = TRUE;
1830 else
1831 insns = FALSE;
1833 disassemble_bytes (pinfo, paux->disassemble_fn, insns, data,
1834 addr_offset, nextstop_offset,
1835 rel_offset, &rel_pp, rel_ppend);
1837 addr_offset = nextstop_offset;
1838 sym = nextsym;
1841 free (data);
1843 if (rel_ppstart != NULL)
1844 free (rel_ppstart);
1847 /* Disassemble the contents of an object file. */
1849 static void
1850 disassemble_data (bfd *abfd)
1852 struct disassemble_info disasm_info;
1853 struct objdump_disasm_info aux;
1854 long i;
1856 print_files = NULL;
1857 prev_functionname = NULL;
1858 prev_line = -1;
1860 /* We make a copy of syms to sort. We don't want to sort syms
1861 because that will screw up the relocs. */
1862 sorted_symcount = symcount ? symcount : dynsymcount;
1863 sorted_syms = xmalloc ((sorted_symcount + synthcount) * sizeof (asymbol *));
1864 memcpy (sorted_syms, symcount ? syms : dynsyms,
1865 sorted_symcount * sizeof (asymbol *));
1867 sorted_symcount = remove_useless_symbols (sorted_syms, sorted_symcount);
1869 for (i = 0; i < synthcount; ++i)
1871 sorted_syms[sorted_symcount] = synthsyms + i;
1872 ++sorted_symcount;
1875 /* Sort the symbols into section and symbol order. */
1876 qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols);
1878 init_disassemble_info (&disasm_info, stdout, (fprintf_ftype) fprintf);
1880 disasm_info.application_data = (void *) &aux;
1881 aux.abfd = abfd;
1882 aux.require_sec = FALSE;
1883 aux.dynrelbuf = NULL;
1884 aux.dynrelcount = 0;
1885 #ifdef DISASSEMBLER_NEEDS_RELOCS
1886 aux.reloc = NULL;
1887 #endif
1889 disasm_info.print_address_func = objdump_print_address;
1890 disasm_info.symbol_at_address_func = objdump_symbol_at_address;
1892 if (machine != NULL)
1894 const bfd_arch_info_type *info = bfd_scan_arch (machine);
1896 if (info == NULL)
1897 fatal (_("Can't use supplied machine %s"), machine);
1899 abfd->arch_info = info;
1902 if (endian != BFD_ENDIAN_UNKNOWN)
1904 struct bfd_target *xvec;
1906 xvec = xmalloc (sizeof (struct bfd_target));
1907 memcpy (xvec, abfd->xvec, sizeof (struct bfd_target));
1908 xvec->byteorder = endian;
1909 abfd->xvec = xvec;
1912 /* Use libopcodes to locate a suitable disassembler. */
1913 aux.disassemble_fn = disassembler (abfd);
1914 if (!aux.disassemble_fn)
1916 non_fatal (_("Can't disassemble for architecture %s\n"),
1917 bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
1918 exit_status = 1;
1919 return;
1922 disasm_info.flavour = bfd_get_flavour (abfd);
1923 disasm_info.arch = bfd_get_arch (abfd);
1924 disasm_info.mach = bfd_get_mach (abfd);
1925 disasm_info.disassembler_options = disassembler_options;
1926 disasm_info.octets_per_byte = bfd_octets_per_byte (abfd);
1927 disasm_info.skip_zeroes = DEFAULT_SKIP_ZEROES;
1928 disasm_info.skip_zeroes_at_end = DEFAULT_SKIP_ZEROES_AT_END;
1930 if (bfd_big_endian (abfd))
1931 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_BIG;
1932 else if (bfd_little_endian (abfd))
1933 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_LITTLE;
1934 else
1935 /* ??? Aborting here seems too drastic. We could default to big or little
1936 instead. */
1937 disasm_info.endian = BFD_ENDIAN_UNKNOWN;
1939 /* Allow the target to customize the info structure. */
1940 disassemble_init_for_target (& disasm_info);
1942 /* Pre-load the dynamic relocs if we are going
1943 to be dumping them along with the disassembly. */
1944 if (dump_dynamic_reloc_info)
1946 long relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
1948 if (relsize < 0)
1949 bfd_fatal (bfd_get_filename (abfd));
1951 if (relsize > 0)
1953 aux.dynrelbuf = xmalloc (relsize);
1954 aux.dynrelcount = bfd_canonicalize_dynamic_reloc (abfd,
1955 aux.dynrelbuf,
1956 dynsyms);
1957 if (aux.dynrelcount < 0)
1958 bfd_fatal (bfd_get_filename (abfd));
1960 /* Sort the relocs by address. */
1961 qsort (aux.dynrelbuf, aux.dynrelcount, sizeof (arelent *),
1962 compare_relocs);
1966 bfd_map_over_sections (abfd, disassemble_section, & disasm_info);
1968 if (aux.dynrelbuf != NULL)
1969 free (aux.dynrelbuf);
1970 free (sorted_syms);
1974 load_debug_section (enum dwarf_section_display_enum debug, void *file)
1976 struct dwarf_section *section = &debug_displays [debug].section;
1977 bfd *abfd = file;
1978 asection *sec;
1979 bfd_boolean ret;
1981 /* If it is already loaded, do nothing. */
1982 if (section->start != NULL)
1983 return 1;
1985 /* Locate the debug section. */
1986 sec = bfd_get_section_by_name (abfd, section->name);
1987 if (sec == NULL)
1988 return 0;
1990 section->address = bfd_get_section_vma (abfd, sec);
1991 section->size = bfd_get_section_size (sec);
1992 section->start = xmalloc (section->size);
1994 if (is_relocatable && debug_displays [debug].relocate)
1995 ret = bfd_simple_get_relocated_section_contents (abfd,
1996 sec,
1997 section->start,
1998 syms) != NULL;
1999 else
2000 ret = bfd_get_section_contents (abfd, sec, section->start, 0,
2001 section->size);
2003 if (!ret)
2005 free_debug_section (debug);
2006 printf (_("\nCan't get contents for section '%s'.\n"),
2007 section->name);
2010 return ret;
2013 void
2014 free_debug_section (enum dwarf_section_display_enum debug)
2016 struct dwarf_section *section = &debug_displays [debug].section;
2018 if (section->start == NULL)
2019 return;
2021 free ((char *) section->start);
2022 section->start = NULL;
2023 section->address = 0;
2024 section->size = 0;
2027 static void
2028 dump_dwarf_section (bfd *abfd, asection *section,
2029 void *arg ATTRIBUTE_UNUSED)
2031 const char *name = bfd_get_section_name (abfd, section);
2032 const char *match;
2033 enum dwarf_section_display_enum i;
2035 if (strncmp (name, ".gnu.linkonce.wi.", 17) == 0)
2036 match = ".debug_info";
2037 else
2038 match = name;
2040 for (i = 0; i < max; i++)
2041 if (strcmp (debug_displays[i].section.name, match) == 0)
2043 if (!debug_displays[i].eh_frame)
2045 struct dwarf_section *sec = &debug_displays [i].section;
2047 if (load_debug_section (i, abfd))
2049 debug_displays[i].display (sec, abfd);
2051 if (i != info && i != abbrev)
2052 free_debug_section (i);
2055 break;
2059 static const char *mach_o_dwarf_sections [] = {
2060 "LC_SEGMENT.__DWARFA.__debug_abbrev", /* .debug_abbrev */
2061 "LC_SEGMENT.__DWARFA.__debug_aranges", /* .debug_aranges */
2062 "LC_SEGMENT.__DWARFA.__debug_frame", /* .debug_frame */
2063 "LC_SEGMENT.__DWARFA.__debug_info", /* .debug_info */
2064 "LC_SEGMENT.__DWARFA.__debug_line", /* .debug_line */
2065 "LC_SEGMENT.__DWARFA.__debug_pubnames", /* .debug_pubnames */
2066 ".eh_frame", /* .eh_frame */
2067 "LC_SEGMENT.__DWARFA.__debug_macinfo", /* .debug_macinfo */
2068 "LC_SEGMENT.__DWARFA.__debug_str", /* .debug_str */
2069 "LC_SEGMENT.__DWARFA.__debug_loc", /* .debug_loc */
2070 "LC_SEGMENT.__DWARFA.__debug_pubtypes", /* .debug_pubtypes */
2071 "LC_SEGMENT.__DWARFA.__debug_ranges", /* .debug_ranges */
2072 "LC_SEGMENT.__DWARFA.__debug_static_func", /* .debug_static_func */
2073 "LC_SEGMENT.__DWARFA.__debug_static_vars", /* .debug_static_vars */
2074 "LC_SEGMENT.__DWARFA.__debug_types", /* .debug_types */
2075 "LC_SEGMENT.__DWARFA.__debug_weaknames" /* .debug_weaknames */
2078 static const char *generic_dwarf_sections [max];
2080 static void
2081 check_mach_o_dwarf (bfd *abfd)
2083 static enum bfd_flavour old_flavour = bfd_target_unknown_flavour;
2084 enum bfd_flavour current_flavour = bfd_get_flavour (abfd);
2085 enum dwarf_section_display_enum i;
2087 if (generic_dwarf_sections [0] == NULL)
2088 for (i = 0; i < max; i++)
2089 generic_dwarf_sections [i] = debug_displays[i].section.name;
2091 if (old_flavour != current_flavour)
2093 if (current_flavour == bfd_target_mach_o_flavour)
2094 for (i = 0; i < max; i++)
2095 debug_displays[i].section.name = mach_o_dwarf_sections [i];
2096 else if (old_flavour == bfd_target_mach_o_flavour)
2097 for (i = 0; i < max; i++)
2098 debug_displays[i].section.name = generic_dwarf_sections [i];
2100 old_flavour = current_flavour;
2104 /* Dump the dwarf debugging information. */
2106 static void
2107 dump_dwarf (bfd *abfd)
2109 is_relocatable = ((abfd->flags & (HAS_RELOC | EXEC_P | DYNAMIC))
2110 == HAS_RELOC);
2112 /* FIXME: bfd_get_arch_size may return -1. We assume that 64bit
2113 targets will return 64. */
2114 eh_addr_size = bfd_get_arch_size (abfd) == 64 ? 8 : 4;
2116 if (bfd_big_endian (abfd))
2117 byte_get = byte_get_big_endian;
2118 else if (bfd_little_endian (abfd))
2119 byte_get = byte_get_little_endian;
2120 else
2121 abort ();
2123 check_mach_o_dwarf (abfd);
2125 bfd_map_over_sections (abfd, dump_dwarf_section, NULL);
2127 free_debug_memory ();
2130 /* Read ABFD's stabs section STABSECT_NAME, and return a pointer to
2131 it. Return NULL on failure. */
2133 static char *
2134 read_section_stabs (bfd *abfd, const char *sect_name, bfd_size_type *size_ptr)
2136 asection *stabsect;
2137 bfd_size_type size;
2138 char *contents;
2140 stabsect = bfd_get_section_by_name (abfd, sect_name);
2141 if (stabsect == NULL)
2143 printf (_("No %s section present\n\n"), sect_name);
2144 return FALSE;
2147 size = bfd_section_size (abfd, stabsect);
2148 contents = xmalloc (size);
2150 if (! bfd_get_section_contents (abfd, stabsect, contents, 0, size))
2152 non_fatal (_("Reading %s section of %s failed: %s"),
2153 sect_name, bfd_get_filename (abfd),
2154 bfd_errmsg (bfd_get_error ()));
2155 free (contents);
2156 exit_status = 1;
2157 return NULL;
2160 *size_ptr = size;
2162 return contents;
2165 /* Stabs entries use a 12 byte format:
2166 4 byte string table index
2167 1 byte stab type
2168 1 byte stab other field
2169 2 byte stab desc field
2170 4 byte stab value
2171 FIXME: This will have to change for a 64 bit object format. */
2173 #define STRDXOFF (0)
2174 #define TYPEOFF (4)
2175 #define OTHEROFF (5)
2176 #define DESCOFF (6)
2177 #define VALOFF (8)
2178 #define STABSIZE (12)
2180 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
2181 using string table section STRSECT_NAME (in `strtab'). */
2183 static void
2184 print_section_stabs (bfd *abfd,
2185 const char *stabsect_name,
2186 unsigned *string_offset_ptr)
2188 int i;
2189 unsigned file_string_table_offset = 0;
2190 unsigned next_file_string_table_offset = *string_offset_ptr;
2191 bfd_byte *stabp, *stabs_end;
2193 stabp = stabs;
2194 stabs_end = stabp + stab_size;
2196 printf (_("Contents of %s section:\n\n"), stabsect_name);
2197 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
2199 /* Loop through all symbols and print them.
2201 We start the index at -1 because there is a dummy symbol on
2202 the front of stabs-in-{coff,elf} sections that supplies sizes. */
2203 for (i = -1; stabp < stabs_end; stabp += STABSIZE, i++)
2205 const char *name;
2206 unsigned long strx;
2207 unsigned char type, other;
2208 unsigned short desc;
2209 bfd_vma value;
2211 strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
2212 type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
2213 other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
2214 desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
2215 value = bfd_h_get_32 (abfd, stabp + VALOFF);
2217 printf ("\n%-6d ", i);
2218 /* Either print the stab name, or, if unnamed, print its number
2219 again (makes consistent formatting for tools like awk). */
2220 name = bfd_get_stab_name (type);
2221 if (name != NULL)
2222 printf ("%-6s", name);
2223 else if (type == N_UNDF)
2224 printf ("HdrSym");
2225 else
2226 printf ("%-6d", type);
2227 printf (" %-6d %-6d ", other, desc);
2228 bfd_printf_vma (abfd, value);
2229 printf (" %-6lu", strx);
2231 /* Symbols with type == 0 (N_UNDF) specify the length of the
2232 string table associated with this file. We use that info
2233 to know how to relocate the *next* file's string table indices. */
2234 if (type == N_UNDF)
2236 file_string_table_offset = next_file_string_table_offset;
2237 next_file_string_table_offset += value;
2239 else
2241 /* Using the (possibly updated) string table offset, print the
2242 string (if any) associated with this symbol. */
2243 if ((strx + file_string_table_offset) < stabstr_size)
2244 printf (" %s", &strtab[strx + file_string_table_offset]);
2245 else
2246 printf (" *");
2249 printf ("\n\n");
2250 *string_offset_ptr = next_file_string_table_offset;
2253 typedef struct
2255 const char * section_name;
2256 const char * string_section_name;
2257 unsigned string_offset;
2259 stab_section_names;
2261 static void
2262 find_stabs_section (bfd *abfd, asection *section, void *names)
2264 int len;
2265 stab_section_names * sought = (stab_section_names *) names;
2267 /* Check for section names for which stabsect_name is a prefix, to
2268 handle .stab.N, etc. */
2269 len = strlen (sought->section_name);
2271 /* If the prefix matches, and the files section name ends with a
2272 nul or a digit, then we match. I.e., we want either an exact
2273 match or a section followed by a number. */
2274 if (strncmp (sought->section_name, section->name, len) == 0
2275 && (section->name[len] == 0
2276 || (section->name[len] == '.' && ISDIGIT (section->name[len + 1]))))
2278 if (strtab == NULL)
2279 strtab = read_section_stabs (abfd, sought->string_section_name,
2280 &stabstr_size);
2282 if (strtab)
2284 stabs = (bfd_byte *) read_section_stabs (abfd, section->name,
2285 &stab_size);
2286 if (stabs)
2287 print_section_stabs (abfd, section->name, &sought->string_offset);
2292 static void
2293 dump_stabs_section (bfd *abfd, char *stabsect_name, char *strsect_name)
2295 stab_section_names s;
2297 s.section_name = stabsect_name;
2298 s.string_section_name = strsect_name;
2299 s.string_offset = 0;
2301 bfd_map_over_sections (abfd, find_stabs_section, & s);
2303 free (strtab);
2304 strtab = NULL;
2307 /* Dump the any sections containing stabs debugging information. */
2309 static void
2310 dump_stabs (bfd *abfd)
2312 dump_stabs_section (abfd, ".stab", ".stabstr");
2313 dump_stabs_section (abfd, ".stab.excl", ".stab.exclstr");
2314 dump_stabs_section (abfd, ".stab.index", ".stab.indexstr");
2315 dump_stabs_section (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
2318 static void
2319 dump_bfd_header (bfd *abfd)
2321 char *comma = "";
2323 printf (_("architecture: %s, "),
2324 bfd_printable_arch_mach (bfd_get_arch (abfd),
2325 bfd_get_mach (abfd)));
2326 printf (_("flags 0x%08x:\n"), abfd->flags);
2328 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
2329 PF (HAS_RELOC, "HAS_RELOC");
2330 PF (EXEC_P, "EXEC_P");
2331 PF (HAS_LINENO, "HAS_LINENO");
2332 PF (HAS_DEBUG, "HAS_DEBUG");
2333 PF (HAS_SYMS, "HAS_SYMS");
2334 PF (HAS_LOCALS, "HAS_LOCALS");
2335 PF (DYNAMIC, "DYNAMIC");
2336 PF (WP_TEXT, "WP_TEXT");
2337 PF (D_PAGED, "D_PAGED");
2338 PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
2339 PF (HAS_LOAD_PAGE, "HAS_LOAD_PAGE");
2340 printf (_("\nstart address 0x"));
2341 bfd_printf_vma (abfd, abfd->start_address);
2342 printf ("\n");
2346 static void
2347 dump_bfd_private_header (bfd *abfd)
2349 bfd_print_private_bfd_data (abfd, stdout);
2353 /* Display a section in hexadecimal format with associated characters.
2354 Each line prefixed by the zero padded address. */
2356 static void
2357 dump_section (bfd *abfd, asection *section, void *dummy ATTRIBUTE_UNUSED)
2359 bfd_byte *data = 0;
2360 bfd_size_type datasize;
2361 bfd_size_type addr_offset;
2362 bfd_size_type start_offset;
2363 bfd_size_type stop_offset;
2364 unsigned int opb = bfd_octets_per_byte (abfd);
2365 /* Bytes per line. */
2366 const int onaline = 16;
2367 char buf[64];
2368 int count;
2369 int width;
2371 if ((section->flags & SEC_HAS_CONTENTS) == 0)
2372 return;
2374 if (! process_section_p (section))
2375 return;
2377 if ((datasize = bfd_section_size (abfd, section)) == 0)
2378 return;
2380 printf (_("Contents of section %s:\n"), section->name);
2382 data = xmalloc (datasize);
2384 bfd_get_section_contents (abfd, section, data, 0, datasize);
2386 /* Compute the address range to display. */
2387 if (start_address == (bfd_vma) -1
2388 || start_address < section->vma)
2389 start_offset = 0;
2390 else
2391 start_offset = start_address - section->vma;
2393 if (stop_address == (bfd_vma) -1)
2394 stop_offset = datasize / opb;
2395 else
2397 if (stop_address < section->vma)
2398 stop_offset = 0;
2399 else
2400 stop_offset = stop_address - section->vma;
2402 if (stop_offset > datasize / opb)
2403 stop_offset = datasize / opb;
2406 width = 4;
2408 bfd_sprintf_vma (abfd, buf, start_offset + section->vma);
2409 if (strlen (buf) >= sizeof (buf))
2410 abort ();
2412 count = 0;
2413 while (buf[count] == '0' && buf[count+1] != '\0')
2414 count++;
2415 count = strlen (buf) - count;
2416 if (count > width)
2417 width = count;
2419 bfd_sprintf_vma (abfd, buf, stop_offset + section->vma - 1);
2420 if (strlen (buf) >= sizeof (buf))
2421 abort ();
2423 count = 0;
2424 while (buf[count] == '0' && buf[count+1] != '\0')
2425 count++;
2426 count = strlen (buf) - count;
2427 if (count > width)
2428 width = count;
2430 for (addr_offset = start_offset;
2431 addr_offset < stop_offset; addr_offset += onaline / opb)
2433 bfd_size_type j;
2435 bfd_sprintf_vma (abfd, buf, (addr_offset + section->vma));
2436 count = strlen (buf);
2437 if ((size_t) count >= sizeof (buf))
2438 abort ();
2440 putchar (' ');
2441 while (count < width)
2443 putchar ('0');
2444 count++;
2446 fputs (buf + count - width, stdout);
2447 putchar (' ');
2449 for (j = addr_offset * opb;
2450 j < addr_offset * opb + onaline; j++)
2452 if (j < stop_offset * opb)
2453 printf ("%02x", (unsigned) (data[j]));
2454 else
2455 printf (" ");
2456 if ((j & 3) == 3)
2457 printf (" ");
2460 printf (" ");
2461 for (j = addr_offset * opb;
2462 j < addr_offset * opb + onaline; j++)
2464 if (j >= stop_offset * opb)
2465 printf (" ");
2466 else
2467 printf ("%c", ISPRINT (data[j]) ? data[j] : '.');
2469 putchar ('\n');
2471 free (data);
2474 /* Actually display the various requested regions. */
2476 static void
2477 dump_data (bfd *abfd)
2479 bfd_map_over_sections (abfd, dump_section, NULL);
2482 /* Should perhaps share code and display with nm? */
2484 static void
2485 dump_symbols (bfd *abfd ATTRIBUTE_UNUSED, bfd_boolean dynamic)
2487 asymbol **current;
2488 long max;
2489 long count;
2491 if (dynamic)
2493 current = dynsyms;
2494 max = dynsymcount;
2495 printf ("DYNAMIC SYMBOL TABLE:\n");
2497 else
2499 current = syms;
2500 max = symcount;
2501 printf ("SYMBOL TABLE:\n");
2504 if (max == 0)
2505 printf (_("no symbols\n"));
2507 for (count = 0; count < max; count++)
2509 bfd *cur_bfd;
2511 if (*current == NULL)
2512 printf (_("no information for symbol number %ld\n"), count);
2514 else if ((cur_bfd = bfd_asymbol_bfd (*current)) == NULL)
2515 printf (_("could not determine the type of symbol number %ld\n"),
2516 count);
2518 else if (process_section_p ((* current)->section)
2519 && (dump_special_syms
2520 || !bfd_is_target_special_symbol (cur_bfd, *current)))
2522 const char *name = (*current)->name;
2524 if (do_demangle && name != NULL && *name != '\0')
2526 char *alloc;
2528 /* If we want to demangle the name, we demangle it
2529 here, and temporarily clobber it while calling
2530 bfd_print_symbol. FIXME: This is a gross hack. */
2531 alloc = demangle (cur_bfd, name);
2532 (*current)->name = alloc;
2533 bfd_print_symbol (cur_bfd, stdout, *current,
2534 bfd_print_symbol_all);
2535 (*current)->name = name;
2536 free (alloc);
2538 else
2539 bfd_print_symbol (cur_bfd, stdout, *current,
2540 bfd_print_symbol_all);
2541 printf ("\n");
2544 current++;
2546 printf ("\n\n");
2549 static void
2550 dump_reloc_set (bfd *abfd, asection *sec, arelent **relpp, long relcount)
2552 arelent **p;
2553 char *last_filename, *last_functionname;
2554 unsigned int last_line;
2556 /* Get column headers lined up reasonably. */
2558 static int width;
2560 if (width == 0)
2562 char buf[30];
2564 bfd_sprintf_vma (abfd, buf, (bfd_vma) -1);
2565 width = strlen (buf) - 7;
2567 printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
2570 last_filename = NULL;
2571 last_functionname = NULL;
2572 last_line = 0;
2574 for (p = relpp; relcount && *p != NULL; p++, relcount--)
2576 arelent *q = *p;
2577 const char *filename, *functionname;
2578 unsigned int line;
2579 const char *sym_name;
2580 const char *section_name;
2582 if (start_address != (bfd_vma) -1
2583 && q->address < start_address)
2584 continue;
2585 if (stop_address != (bfd_vma) -1
2586 && q->address > stop_address)
2587 continue;
2589 if (with_line_numbers
2590 && sec != NULL
2591 && bfd_find_nearest_line (abfd, sec, syms, q->address,
2592 &filename, &functionname, &line))
2594 if (functionname != NULL
2595 && (last_functionname == NULL
2596 || strcmp (functionname, last_functionname) != 0))
2598 printf ("%s():\n", functionname);
2599 if (last_functionname != NULL)
2600 free (last_functionname);
2601 last_functionname = xstrdup (functionname);
2604 if (line > 0
2605 && (line != last_line
2606 || (filename != NULL
2607 && last_filename != NULL
2608 && strcmp (filename, last_filename) != 0)))
2610 printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
2611 last_line = line;
2612 if (last_filename != NULL)
2613 free (last_filename);
2614 if (filename == NULL)
2615 last_filename = NULL;
2616 else
2617 last_filename = xstrdup (filename);
2621 if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
2623 sym_name = (*(q->sym_ptr_ptr))->name;
2624 section_name = (*(q->sym_ptr_ptr))->section->name;
2626 else
2628 sym_name = NULL;
2629 section_name = NULL;
2632 bfd_printf_vma (abfd, q->address);
2633 if (q->howto == NULL)
2634 printf (" *unknown* ");
2635 else if (q->howto->name)
2636 printf (" %-16s ", q->howto->name);
2637 else
2638 printf (" %-16d ", q->howto->type);
2639 if (sym_name)
2640 objdump_print_symname (abfd, NULL, *q->sym_ptr_ptr);
2641 else
2643 if (section_name == NULL)
2644 section_name = "*unknown*";
2645 printf ("[%s]", section_name);
2648 if (q->addend)
2650 printf ("+0x");
2651 bfd_printf_vma (abfd, q->addend);
2654 printf ("\n");
2658 static void
2659 dump_relocs_in_section (bfd *abfd,
2660 asection *section,
2661 void *dummy ATTRIBUTE_UNUSED)
2663 arelent **relpp;
2664 long relcount;
2665 long relsize;
2667 if ( bfd_is_abs_section (section)
2668 || bfd_is_und_section (section)
2669 || bfd_is_com_section (section)
2670 || (! process_section_p (section))
2671 || ((section->flags & SEC_RELOC) == 0))
2672 return;
2674 relsize = bfd_get_reloc_upper_bound (abfd, section);
2675 if (relsize < 0)
2676 bfd_fatal (bfd_get_filename (abfd));
2678 printf ("RELOCATION RECORDS FOR [%s]:", section->name);
2680 if (relsize == 0)
2682 printf (" (none)\n\n");
2683 return;
2686 relpp = xmalloc (relsize);
2687 relcount = bfd_canonicalize_reloc (abfd, section, relpp, syms);
2689 if (relcount < 0)
2690 bfd_fatal (bfd_get_filename (abfd));
2691 else if (relcount == 0)
2692 printf (" (none)\n\n");
2693 else
2695 printf ("\n");
2696 dump_reloc_set (abfd, section, relpp, relcount);
2697 printf ("\n\n");
2699 free (relpp);
2702 static void
2703 dump_relocs (bfd *abfd)
2705 bfd_map_over_sections (abfd, dump_relocs_in_section, NULL);
2708 static void
2709 dump_dynamic_relocs (bfd *abfd)
2711 long relsize;
2712 arelent **relpp;
2713 long relcount;
2715 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
2716 if (relsize < 0)
2717 bfd_fatal (bfd_get_filename (abfd));
2719 printf ("DYNAMIC RELOCATION RECORDS");
2721 if (relsize == 0)
2722 printf (" (none)\n\n");
2723 else
2725 relpp = xmalloc (relsize);
2726 relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
2728 if (relcount < 0)
2729 bfd_fatal (bfd_get_filename (abfd));
2730 else if (relcount == 0)
2731 printf (" (none)\n\n");
2732 else
2734 printf ("\n");
2735 dump_reloc_set (abfd, NULL, relpp, relcount);
2736 printf ("\n\n");
2738 free (relpp);
2742 /* Creates a table of paths, to search for source files. */
2744 static void
2745 add_include_path (const char *path)
2747 if (path[0] == 0)
2748 return;
2749 include_path_count++;
2750 include_paths = xrealloc (include_paths,
2751 include_path_count * sizeof (*include_paths));
2752 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2753 if (path[1] == ':' && path[2] == 0)
2754 path = concat (path, ".", (const char *) 0);
2755 #endif
2756 include_paths[include_path_count - 1] = path;
2759 static void
2760 adjust_addresses (bfd *abfd ATTRIBUTE_UNUSED,
2761 asection *section,
2762 void *arg)
2764 if ((section->flags & SEC_DEBUGGING) == 0)
2766 bfd_boolean *has_reloc_p = (bfd_boolean *) arg;
2767 section->vma += adjust_section_vma;
2768 if (*has_reloc_p)
2769 section->lma += adjust_section_vma;
2773 /* Dump selected contents of ABFD. */
2775 static void
2776 dump_bfd (bfd *abfd)
2778 /* If we are adjusting section VMA's, change them all now. Changing
2779 the BFD information is a hack. However, we must do it, or
2780 bfd_find_nearest_line will not do the right thing. */
2781 if (adjust_section_vma != 0)
2783 bfd_boolean has_reloc = (abfd->flags & HAS_RELOC);
2784 bfd_map_over_sections (abfd, adjust_addresses, &has_reloc);
2787 if (! dump_debugging_tags)
2788 printf (_("\n%s: file format %s\n"), bfd_get_filename (abfd),
2789 abfd->xvec->name);
2790 if (dump_ar_hdrs)
2791 print_arelt_descr (stdout, abfd, TRUE);
2792 if (dump_file_header)
2793 dump_bfd_header (abfd);
2794 if (dump_private_headers)
2795 dump_bfd_private_header (abfd);
2796 if (! dump_debugging_tags)
2797 putchar ('\n');
2798 if (dump_section_headers)
2799 dump_headers (abfd);
2801 if (dump_symtab
2802 || dump_reloc_info
2803 || disassemble
2804 || dump_debugging
2805 || dump_dwarf_section_info)
2806 syms = slurp_symtab (abfd);
2807 if (dump_dynamic_symtab || dump_dynamic_reloc_info
2808 || (disassemble && bfd_get_dynamic_symtab_upper_bound (abfd) > 0))
2809 dynsyms = slurp_dynamic_symtab (abfd);
2810 if (disassemble)
2812 synthcount = bfd_get_synthetic_symtab (abfd, symcount, syms,
2813 dynsymcount, dynsyms, &synthsyms);
2814 if (synthcount < 0)
2815 synthcount = 0;
2818 if (dump_symtab)
2819 dump_symbols (abfd, FALSE);
2820 if (dump_dynamic_symtab)
2821 dump_symbols (abfd, TRUE);
2822 if (dump_dwarf_section_info)
2823 dump_dwarf (abfd);
2824 if (dump_stab_section_info)
2825 dump_stabs (abfd);
2826 if (dump_reloc_info && ! disassemble)
2827 dump_relocs (abfd);
2828 if (dump_dynamic_reloc_info && ! disassemble)
2829 dump_dynamic_relocs (abfd);
2830 if (dump_section_contents)
2831 dump_data (abfd);
2832 if (disassemble)
2833 disassemble_data (abfd);
2835 if (dump_debugging)
2837 void *dhandle;
2839 dhandle = read_debugging_info (abfd, syms, symcount);
2840 if (dhandle != NULL)
2842 if (! print_debugging_info (stdout, dhandle, abfd, syms, demangle,
2843 dump_debugging_tags ? TRUE : FALSE))
2845 non_fatal (_("%s: printing debugging information failed"),
2846 bfd_get_filename (abfd));
2847 exit_status = 1;
2852 if (syms)
2854 free (syms);
2855 syms = NULL;
2858 if (dynsyms)
2860 free (dynsyms);
2861 dynsyms = NULL;
2864 if (synthsyms)
2866 free (synthsyms);
2867 synthsyms = NULL;
2870 symcount = 0;
2871 dynsymcount = 0;
2872 synthcount = 0;
2875 static void
2876 display_bfd (bfd *abfd)
2878 char **matching;
2880 if (bfd_check_format_matches (abfd, bfd_object, &matching))
2882 dump_bfd (abfd);
2883 return;
2886 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
2888 nonfatal (bfd_get_filename (abfd));
2889 list_matching_formats (matching);
2890 free (matching);
2891 return;
2894 if (bfd_get_error () != bfd_error_file_not_recognized)
2896 nonfatal (bfd_get_filename (abfd));
2897 return;
2900 if (bfd_check_format_matches (abfd, bfd_core, &matching))
2902 dump_bfd (abfd);
2903 return;
2906 nonfatal (bfd_get_filename (abfd));
2908 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
2910 list_matching_formats (matching);
2911 free (matching);
2915 static void
2916 display_file (char *filename, char *target)
2918 bfd *file;
2919 bfd *arfile = NULL;
2921 if (get_file_size (filename) < 1)
2922 return;
2924 file = bfd_openr (filename, target);
2925 if (file == NULL)
2927 nonfatal (filename);
2928 return;
2931 /* If the file is an archive, process all of its elements. */
2932 if (bfd_check_format (file, bfd_archive))
2934 bfd *last_arfile = NULL;
2936 printf (_("In archive %s:\n"), bfd_get_filename (file));
2937 for (;;)
2939 bfd_set_error (bfd_error_no_error);
2941 arfile = bfd_openr_next_archived_file (file, arfile);
2942 if (arfile == NULL)
2944 if (bfd_get_error () != bfd_error_no_more_archived_files)
2945 nonfatal (bfd_get_filename (file));
2946 break;
2949 display_bfd (arfile);
2951 if (last_arfile != NULL)
2952 bfd_close (last_arfile);
2953 last_arfile = arfile;
2956 if (last_arfile != NULL)
2957 bfd_close (last_arfile);
2959 else
2960 display_bfd (file);
2962 bfd_close (file);
2966 main (int argc, char **argv)
2968 int c;
2969 char *target = default_target;
2970 bfd_boolean seenflag = FALSE;
2972 #if defined (HAVE_SETLOCALE)
2973 #if defined (HAVE_LC_MESSAGES)
2974 setlocale (LC_MESSAGES, "");
2975 #endif
2976 setlocale (LC_CTYPE, "");
2977 #endif
2979 bindtextdomain (PACKAGE, LOCALEDIR);
2980 textdomain (PACKAGE);
2982 program_name = *argv;
2983 xmalloc_set_program_name (program_name);
2985 START_PROGRESS (program_name, 0);
2987 expandargv (&argc, &argv);
2989 bfd_init ();
2990 set_default_bfd_target ();
2992 while ((c = getopt_long (argc, argv, "pib:m:M:VvCdDlfaHhrRtTxsSI:j:wE:zgeGW",
2993 long_options, (int *) 0))
2994 != EOF)
2996 switch (c)
2998 case 0:
2999 break; /* We've been given a long option. */
3000 case 'm':
3001 machine = optarg;
3002 break;
3003 case 'M':
3004 if (disassembler_options)
3005 /* Ignore potential memory leak for now. */
3006 disassembler_options = concat (disassembler_options, ",",
3007 optarg, NULL);
3008 else
3009 disassembler_options = optarg;
3010 break;
3011 case 'j':
3012 if (only_used == only_size)
3014 only_size += 8;
3015 only = xrealloc (only, only_size * sizeof (char *));
3017 only [only_used++] = optarg;
3018 break;
3019 case 'l':
3020 with_line_numbers = TRUE;
3021 break;
3022 case 'b':
3023 target = optarg;
3024 break;
3025 case 'C':
3026 do_demangle = TRUE;
3027 if (optarg != NULL)
3029 enum demangling_styles style;
3031 style = cplus_demangle_name_to_style (optarg);
3032 if (style == unknown_demangling)
3033 fatal (_("unknown demangling style `%s'"),
3034 optarg);
3036 cplus_demangle_set_style (style);
3038 break;
3039 case 'w':
3040 wide_output = TRUE;
3041 break;
3042 case OPTION_ADJUST_VMA:
3043 adjust_section_vma = parse_vma (optarg, "--adjust-vma");
3044 break;
3045 case OPTION_START_ADDRESS:
3046 start_address = parse_vma (optarg, "--start-address");
3047 break;
3048 case OPTION_STOP_ADDRESS:
3049 stop_address = parse_vma (optarg, "--stop-address");
3050 break;
3051 case 'E':
3052 if (strcmp (optarg, "B") == 0)
3053 endian = BFD_ENDIAN_BIG;
3054 else if (strcmp (optarg, "L") == 0)
3055 endian = BFD_ENDIAN_LITTLE;
3056 else
3058 non_fatal (_("unrecognized -E option"));
3059 usage (stderr, 1);
3061 break;
3062 case OPTION_ENDIAN:
3063 if (strncmp (optarg, "big", strlen (optarg)) == 0)
3064 endian = BFD_ENDIAN_BIG;
3065 else if (strncmp (optarg, "little", strlen (optarg)) == 0)
3066 endian = BFD_ENDIAN_LITTLE;
3067 else
3069 non_fatal (_("unrecognized --endian type `%s'"), optarg);
3070 usage (stderr, 1);
3072 break;
3074 case 'f':
3075 dump_file_header = TRUE;
3076 seenflag = TRUE;
3077 break;
3078 case 'i':
3079 formats_info = TRUE;
3080 seenflag = TRUE;
3081 break;
3082 case 'I':
3083 add_include_path (optarg);
3084 break;
3085 case 'p':
3086 dump_private_headers = TRUE;
3087 seenflag = TRUE;
3088 break;
3089 case 'x':
3090 dump_private_headers = TRUE;
3091 dump_symtab = TRUE;
3092 dump_reloc_info = TRUE;
3093 dump_file_header = TRUE;
3094 dump_ar_hdrs = TRUE;
3095 dump_section_headers = TRUE;
3096 seenflag = TRUE;
3097 break;
3098 case 't':
3099 dump_symtab = TRUE;
3100 seenflag = TRUE;
3101 break;
3102 case 'T':
3103 dump_dynamic_symtab = TRUE;
3104 seenflag = TRUE;
3105 break;
3106 case 'd':
3107 disassemble = TRUE;
3108 seenflag = TRUE;
3109 break;
3110 case 'z':
3111 disassemble_zeroes = TRUE;
3112 break;
3113 case 'D':
3114 disassemble = TRUE;
3115 disassemble_all = TRUE;
3116 seenflag = TRUE;
3117 break;
3118 case 'S':
3119 disassemble = TRUE;
3120 with_source_code = TRUE;
3121 seenflag = TRUE;
3122 break;
3123 case 'g':
3124 dump_debugging = 1;
3125 seenflag = TRUE;
3126 break;
3127 case 'e':
3128 dump_debugging = 1;
3129 dump_debugging_tags = 1;
3130 do_demangle = TRUE;
3131 seenflag = TRUE;
3132 break;
3133 case 'W':
3134 dump_dwarf_section_info = TRUE;
3135 seenflag = TRUE;
3136 do_debug_info = 1;
3137 do_debug_abbrevs = 1;
3138 do_debug_lines = 1;
3139 do_debug_pubnames = 1;
3140 do_debug_aranges = 1;
3141 do_debug_ranges = 1;
3142 do_debug_frames = 1;
3143 do_debug_macinfo = 1;
3144 do_debug_str = 1;
3145 do_debug_loc = 1;
3146 break;
3147 case 'G':
3148 dump_stab_section_info = TRUE;
3149 seenflag = TRUE;
3150 break;
3151 case 's':
3152 dump_section_contents = TRUE;
3153 seenflag = TRUE;
3154 break;
3155 case 'r':
3156 dump_reloc_info = TRUE;
3157 seenflag = TRUE;
3158 break;
3159 case 'R':
3160 dump_dynamic_reloc_info = TRUE;
3161 seenflag = TRUE;
3162 break;
3163 case 'a':
3164 dump_ar_hdrs = TRUE;
3165 seenflag = TRUE;
3166 break;
3167 case 'h':
3168 dump_section_headers = TRUE;
3169 seenflag = TRUE;
3170 break;
3171 case 'H':
3172 usage (stdout, 0);
3173 seenflag = TRUE;
3174 case 'v':
3175 case 'V':
3176 show_version = TRUE;
3177 seenflag = TRUE;
3178 break;
3180 default:
3181 usage (stderr, 1);
3185 if (show_version)
3186 print_version ("objdump");
3188 if (!seenflag)
3189 usage (stderr, 2);
3191 if (formats_info)
3192 exit_status = display_info ();
3193 else
3195 if (optind == argc)
3196 display_file ("a.out", target);
3197 else
3198 for (; optind < argc;)
3199 display_file (argv[optind++], target);
3202 END_PROGRESS (program_name);
3204 return exit_status;