* objdump.c (dump_reloc_set): Free malloced memory.
[binutils.git] / binutils / objdump.c
blobc2ce7ae4a4018504c5b9500d3978c90ddb5a5287
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, 2006, 2007, 2008, 2009, 2010, 2011
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 3, 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,
21 MA 02110-1301, USA. */
24 /* Objdump overview.
26 Objdump displays information about one or more object files, either on
27 their own, or inside libraries. It is commonly used as a disassembler,
28 but it can also display information about file headers, symbol tables,
29 relocations, debugging directives and more.
31 The flow of execution is as follows:
33 1. Command line arguments are checked for control switches and the
34 information to be displayed is selected.
36 2. Any remaining arguments are assumed to be object files, and they are
37 processed in order by display_bfd(). If the file is an archive each
38 of its elements is processed in turn.
40 3. The file's target architecture and binary file format are determined
41 by bfd_check_format(). If they are recognised, then dump_bfd() is
42 called.
44 4. dump_bfd() in turn calls separate functions to display the requested
45 item(s) of information(s). For example disassemble_data() is called if
46 a disassembly has been requested.
48 When disassembling the code loops through blocks of instructions bounded
49 by symbols, calling disassemble_bytes() on each block. The actual
50 disassembling is done by the libopcodes library, via a function pointer
51 supplied by the disassembler() function. */
53 #include "sysdep.h"
54 #include "bfd.h"
55 #include "elf-bfd.h"
56 #include "progress.h"
57 #include "bucomm.h"
58 #include "elfcomm.h"
59 #include "dwarf.h"
60 #include "getopt.h"
61 #include "safe-ctype.h"
62 #include "dis-asm.h"
63 #include "libiberty.h"
64 #include "demangle.h"
65 #include "filenames.h"
66 #include "debug.h"
67 #include "budbg.h"
69 #ifdef HAVE_MMAP
70 #include <sys/mman.h>
71 #endif
73 #include <sys/stat.h>
75 /* Internal headers for the ELF .stab-dump code - sorry. */
76 #define BYTES_IN_WORD 32
77 #include "aout/aout64.h"
79 /* Exit status. */
80 static int exit_status = 0;
82 static char *default_target = NULL; /* Default at runtime. */
84 /* The following variables are set based on arguments passed on the
85 command line. */
86 static int show_version = 0; /* Show the version number. */
87 static int dump_section_contents; /* -s */
88 static int dump_section_headers; /* -h */
89 static bfd_boolean dump_file_header; /* -f */
90 static int dump_symtab; /* -t */
91 static int dump_dynamic_symtab; /* -T */
92 static int dump_reloc_info; /* -r */
93 static int dump_dynamic_reloc_info; /* -R */
94 static int dump_ar_hdrs; /* -a */
95 static int dump_private_headers; /* -p */
96 static int prefix_addresses; /* --prefix-addresses */
97 static int with_line_numbers; /* -l */
98 static bfd_boolean with_source_code; /* -S */
99 static int show_raw_insn; /* --show-raw-insn */
100 static int dump_dwarf_section_info; /* --dwarf */
101 static int dump_stab_section_info; /* --stabs */
102 static int do_demangle; /* -C, --demangle */
103 static bfd_boolean disassemble; /* -d */
104 static bfd_boolean disassemble_all; /* -D */
105 static int disassemble_zeroes; /* --disassemble-zeroes */
106 static bfd_boolean formats_info; /* -i */
107 static int wide_output; /* -w */
108 static int insn_width; /* --insn-width */
109 static bfd_vma start_address = (bfd_vma) -1; /* --start-address */
110 static bfd_vma stop_address = (bfd_vma) -1; /* --stop-address */
111 static int dump_debugging; /* --debugging */
112 static int dump_debugging_tags; /* --debugging-tags */
113 static int dump_special_syms = 0; /* --special-syms */
114 static bfd_vma adjust_section_vma = 0; /* --adjust-vma */
115 static int file_start_context = 0; /* --file-start-context */
116 static bfd_boolean display_file_offsets;/* -F */
117 static const char *prefix; /* --prefix */
118 static int prefix_strip; /* --prefix-strip */
119 static size_t prefix_length;
121 /* A structure to record the sections mentioned in -j switches. */
122 struct only
124 const char * name; /* The name of the section. */
125 bfd_boolean seen; /* A flag to indicate that the section has been found in one or more input files. */
126 struct only * next; /* Pointer to the next structure in the list. */
128 /* Pointer to an array of 'only' structures.
129 This pointer is NULL if the -j switch has not been used. */
130 static struct only * only_list = NULL;
132 /* Variables for handling include file path table. */
133 static const char **include_paths;
134 static int include_path_count;
136 /* Extra info to pass to the section disassembler and address printing
137 function. */
138 struct objdump_disasm_info
140 bfd * abfd;
141 asection * sec;
142 bfd_boolean require_sec;
143 arelent ** dynrelbuf;
144 long dynrelcount;
145 disassembler_ftype disassemble_fn;
146 arelent * reloc;
149 /* Architecture to disassemble for, or default if NULL. */
150 static char *machine = NULL;
152 /* Target specific options to the disassembler. */
153 static char *disassembler_options = NULL;
155 /* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */
156 static enum bfd_endian endian = BFD_ENDIAN_UNKNOWN;
158 /* The symbol table. */
159 static asymbol **syms;
161 /* Number of symbols in `syms'. */
162 static long symcount = 0;
164 /* The sorted symbol table. */
165 static asymbol **sorted_syms;
167 /* Number of symbols in `sorted_syms'. */
168 static long sorted_symcount = 0;
170 /* The dynamic symbol table. */
171 static asymbol **dynsyms;
173 /* The synthetic symbol table. */
174 static asymbol *synthsyms;
175 static long synthcount = 0;
177 /* Number of symbols in `dynsyms'. */
178 static long dynsymcount = 0;
180 static bfd_byte *stabs;
181 static bfd_size_type stab_size;
183 static char *strtab;
184 static bfd_size_type stabstr_size;
186 static bfd_boolean is_relocatable = FALSE;
188 static void
189 usage (FILE *stream, int status)
191 fprintf (stream, _("Usage: %s <option(s)> <file(s)>\n"), program_name);
192 fprintf (stream, _(" Display information from object <file(s)>.\n"));
193 fprintf (stream, _(" At least one of the following switches must be given:\n"));
194 fprintf (stream, _("\
195 -a, --archive-headers Display archive header information\n\
196 -f, --file-headers Display the contents of the overall file header\n\
197 -p, --private-headers Display object format specific file header contents\n\
198 -h, --[section-]headers Display the contents of the section headers\n\
199 -x, --all-headers Display the contents of all headers\n\
200 -d, --disassemble Display assembler contents of executable sections\n\
201 -D, --disassemble-all Display assembler contents of all sections\n\
202 -S, --source Intermix source code with disassembly\n\
203 -s, --full-contents Display the full contents of all sections requested\n\
204 -g, --debugging Display debug information in object file\n\
205 -e, --debugging-tags Display debug information using ctags style\n\
206 -G, --stabs Display (in raw form) any STABS info in the file\n\
207 -W[lLiaprmfFsoRt] or\n\
208 --dwarf[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,\n\
209 =frames-interp,=str,=loc,=Ranges,=pubtypes,\n\
210 =gdb_index,=trace_info,=trace_abbrev,=trace_aranges]\n\
211 Display DWARF info in the file\n\
212 -t, --syms Display the contents of the symbol table(s)\n\
213 -T, --dynamic-syms Display the contents of the dynamic symbol table\n\
214 -r, --reloc Display the relocation entries in the file\n\
215 -R, --dynamic-reloc Display the dynamic relocation entries in the file\n\
216 @<file> Read options from <file>\n\
217 -v, --version Display this program's version number\n\
218 -i, --info List object formats and architectures supported\n\
219 -H, --help Display this information\n\
220 "));
221 if (status != 2)
223 fprintf (stream, _("\n The following switches are optional:\n"));
224 fprintf (stream, _("\
225 -b, --target=BFDNAME Specify the target object format as BFDNAME\n\
226 -m, --architecture=MACHINE Specify the target architecture as MACHINE\n\
227 -j, --section=NAME Only display information for section NAME\n\
228 -M, --disassembler-options=OPT Pass text OPT on to the disassembler\n\
229 -EB --endian=big Assume big endian format when disassembling\n\
230 -EL --endian=little Assume little endian format when disassembling\n\
231 --file-start-context Include context from start of file (with -S)\n\
232 -I, --include=DIR Add DIR to search list for source files\n\
233 -l, --line-numbers Include line numbers and filenames in output\n\
234 -F, --file-offsets Include file offsets when displaying information\n\
235 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n\
236 The STYLE, if specified, can be `auto', `gnu',\n\
237 `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
238 or `gnat'\n\
239 -w, --wide Format output for more than 80 columns\n\
240 -z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling\n\
241 --start-address=ADDR Only process data whose address is >= ADDR\n\
242 --stop-address=ADDR Only process data whose address is <= ADDR\n\
243 --prefix-addresses Print complete address alongside disassembly\n\
244 --[no-]show-raw-insn Display hex alongside symbolic disassembly\n\
245 --insn-width=WIDTH Display WIDTH bytes on a single line for -d\n\
246 --adjust-vma=OFFSET Add OFFSET to all displayed section addresses\n\
247 --special-syms Include special symbols in symbol dumps\n\
248 --prefix=PREFIX Add PREFIX to absolute paths for -S\n\
249 --prefix-strip=LEVEL Strip initial directory names for -S\n\
250 \n"));
251 list_supported_targets (program_name, stream);
252 list_supported_architectures (program_name, stream);
254 disassembler_usage (stream);
256 if (REPORT_BUGS_TO[0] && status == 0)
257 fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
258 exit (status);
261 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
262 enum option_values
264 OPTION_ENDIAN=150,
265 OPTION_START_ADDRESS,
266 OPTION_STOP_ADDRESS,
267 OPTION_DWARF,
268 OPTION_PREFIX,
269 OPTION_PREFIX_STRIP,
270 OPTION_INSN_WIDTH,
271 OPTION_ADJUST_VMA
274 static struct option long_options[]=
276 {"adjust-vma", required_argument, NULL, OPTION_ADJUST_VMA},
277 {"all-headers", no_argument, NULL, 'x'},
278 {"private-headers", no_argument, NULL, 'p'},
279 {"architecture", required_argument, NULL, 'm'},
280 {"archive-headers", no_argument, NULL, 'a'},
281 {"debugging", no_argument, NULL, 'g'},
282 {"debugging-tags", no_argument, NULL, 'e'},
283 {"demangle", optional_argument, NULL, 'C'},
284 {"disassemble", no_argument, NULL, 'd'},
285 {"disassemble-all", no_argument, NULL, 'D'},
286 {"disassembler-options", required_argument, NULL, 'M'},
287 {"disassemble-zeroes", no_argument, NULL, 'z'},
288 {"dynamic-reloc", no_argument, NULL, 'R'},
289 {"dynamic-syms", no_argument, NULL, 'T'},
290 {"endian", required_argument, NULL, OPTION_ENDIAN},
291 {"file-headers", no_argument, NULL, 'f'},
292 {"file-offsets", no_argument, NULL, 'F'},
293 {"file-start-context", no_argument, &file_start_context, 1},
294 {"full-contents", no_argument, NULL, 's'},
295 {"headers", no_argument, NULL, 'h'},
296 {"help", no_argument, NULL, 'H'},
297 {"info", no_argument, NULL, 'i'},
298 {"line-numbers", no_argument, NULL, 'l'},
299 {"no-show-raw-insn", no_argument, &show_raw_insn, -1},
300 {"prefix-addresses", no_argument, &prefix_addresses, 1},
301 {"reloc", no_argument, NULL, 'r'},
302 {"section", required_argument, NULL, 'j'},
303 {"section-headers", no_argument, NULL, 'h'},
304 {"show-raw-insn", no_argument, &show_raw_insn, 1},
305 {"source", no_argument, NULL, 'S'},
306 {"special-syms", no_argument, &dump_special_syms, 1},
307 {"include", required_argument, NULL, 'I'},
308 {"dwarf", optional_argument, NULL, OPTION_DWARF},
309 {"stabs", no_argument, NULL, 'G'},
310 {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
311 {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
312 {"syms", no_argument, NULL, 't'},
313 {"target", required_argument, NULL, 'b'},
314 {"version", no_argument, NULL, 'V'},
315 {"wide", no_argument, NULL, 'w'},
316 {"prefix", required_argument, NULL, OPTION_PREFIX},
317 {"prefix-strip", required_argument, NULL, OPTION_PREFIX_STRIP},
318 {"insn-width", required_argument, NULL, OPTION_INSN_WIDTH},
319 {0, no_argument, 0, 0}
322 static void
323 nonfatal (const char *msg)
325 bfd_nonfatal (msg);
326 exit_status = 1;
329 /* Returns TRUE if the specified section should be dumped. */
331 static bfd_boolean
332 process_section_p (asection * section)
334 struct only * only;
336 if (only_list == NULL)
337 return TRUE;
339 for (only = only_list; only; only = only->next)
340 if (strcmp (only->name, section->name) == 0)
342 only->seen = TRUE;
343 return TRUE;
346 return FALSE;
349 /* Add an entry to the 'only' list. */
351 static void
352 add_only (char * name)
354 struct only * only;
356 /* First check to make sure that we do not
357 already have an entry for this name. */
358 for (only = only_list; only; only = only->next)
359 if (strcmp (only->name, name) == 0)
360 return;
362 only = xmalloc (sizeof * only);
363 only->name = name;
364 only->seen = FALSE;
365 only->next = only_list;
366 only_list = only;
369 /* Release the memory used by the 'only' list.
370 PR 11225: Issue a warning message for unseen sections.
371 Only do this if none of the sections were seen. This is mainly to support
372 tools like the GAS testsuite where an object file is dumped with a list of
373 generic section names known to be present in a range of different file
374 formats. */
376 static void
377 free_only_list (void)
379 bfd_boolean at_least_one_seen = FALSE;
380 struct only * only;
381 struct only * next;
383 if (only_list == NULL)
384 return;
386 for (only = only_list; only; only = only->next)
387 if (only->seen)
389 at_least_one_seen = TRUE;
390 break;
393 for (only = only_list; only; only = next)
395 if (! at_least_one_seen)
397 non_fatal (_("section '%s' mentioned in a -j option, "
398 "but not found in any input file"),
399 only->name);
400 exit_status = 1;
402 next = only->next;
403 free (only);
408 static void
409 dump_section_header (bfd *abfd, asection *section,
410 void *ignored ATTRIBUTE_UNUSED)
412 char *comma = "";
413 unsigned int opb = bfd_octets_per_byte (abfd);
415 /* Ignore linker created section. See elfNN_ia64_object_p in
416 bfd/elfxx-ia64.c. */
417 if (section->flags & SEC_LINKER_CREATED)
418 return;
420 /* PR 10413: Skip sections that we are ignoring. */
421 if (! process_section_p (section))
422 return;
424 printf ("%3d %-13s %08lx ", section->index,
425 bfd_get_section_name (abfd, section),
426 (unsigned long) bfd_section_size (abfd, section) / opb);
427 bfd_printf_vma (abfd, bfd_get_section_vma (abfd, section));
428 printf (" ");
429 bfd_printf_vma (abfd, section->lma);
430 printf (" %08lx 2**%u", (unsigned long) section->filepos,
431 bfd_get_section_alignment (abfd, section));
432 if (! wide_output)
433 printf ("\n ");
434 printf (" ");
436 #define PF(x, y) \
437 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
439 PF (SEC_HAS_CONTENTS, "CONTENTS");
440 PF (SEC_ALLOC, "ALLOC");
441 PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
442 PF (SEC_LOAD, "LOAD");
443 PF (SEC_RELOC, "RELOC");
444 PF (SEC_READONLY, "READONLY");
445 PF (SEC_CODE, "CODE");
446 PF (SEC_DATA, "DATA");
447 PF (SEC_ROM, "ROM");
448 PF (SEC_DEBUGGING, "DEBUGGING");
449 PF (SEC_NEVER_LOAD, "NEVER_LOAD");
450 PF (SEC_EXCLUDE, "EXCLUDE");
451 PF (SEC_SORT_ENTRIES, "SORT_ENTRIES");
452 if (bfd_get_arch (abfd) == bfd_arch_tic54x)
454 PF (SEC_TIC54X_BLOCK, "BLOCK");
455 PF (SEC_TIC54X_CLINK, "CLINK");
457 PF (SEC_SMALL_DATA, "SMALL_DATA");
458 if (bfd_get_flavour (abfd) == bfd_target_coff_flavour)
459 PF (SEC_COFF_SHARED, "SHARED");
460 PF (SEC_THREAD_LOCAL, "THREAD_LOCAL");
461 PF (SEC_GROUP, "GROUP");
463 if ((section->flags & SEC_LINK_ONCE) != 0)
465 const char *ls;
466 struct coff_comdat_info *comdat;
468 switch (section->flags & SEC_LINK_DUPLICATES)
470 default:
471 abort ();
472 case SEC_LINK_DUPLICATES_DISCARD:
473 ls = "LINK_ONCE_DISCARD";
474 break;
475 case SEC_LINK_DUPLICATES_ONE_ONLY:
476 ls = "LINK_ONCE_ONE_ONLY";
477 break;
478 case SEC_LINK_DUPLICATES_SAME_SIZE:
479 ls = "LINK_ONCE_SAME_SIZE";
480 break;
481 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
482 ls = "LINK_ONCE_SAME_CONTENTS";
483 break;
485 printf ("%s%s", comma, ls);
487 comdat = bfd_coff_get_comdat_section (abfd, section);
488 if (comdat != NULL)
489 printf (" (COMDAT %s %ld)", comdat->name, comdat->symbol);
491 comma = ", ";
494 printf ("\n");
495 #undef PF
498 static void
499 dump_headers (bfd *abfd)
501 printf (_("Sections:\n"));
503 #ifndef BFD64
504 printf (_("Idx Name Size VMA LMA File off Algn"));
505 #else
506 /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */
507 if (bfd_get_arch_size (abfd) == 32)
508 printf (_("Idx Name Size VMA LMA File off Algn"));
509 else
510 printf (_("Idx Name Size VMA LMA File off Algn"));
511 #endif
513 if (wide_output)
514 printf (_(" Flags"));
515 printf ("\n");
517 bfd_map_over_sections (abfd, dump_section_header, NULL);
520 static asymbol **
521 slurp_symtab (bfd *abfd)
523 asymbol **sy = NULL;
524 long storage;
526 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
528 symcount = 0;
529 return NULL;
532 storage = bfd_get_symtab_upper_bound (abfd);
533 if (storage < 0)
534 bfd_fatal (bfd_get_filename (abfd));
535 if (storage)
536 sy = (asymbol **) xmalloc (storage);
538 symcount = bfd_canonicalize_symtab (abfd, sy);
539 if (symcount < 0)
540 bfd_fatal (bfd_get_filename (abfd));
541 return sy;
544 /* Read in the dynamic symbols. */
546 static asymbol **
547 slurp_dynamic_symtab (bfd *abfd)
549 asymbol **sy = NULL;
550 long storage;
552 storage = bfd_get_dynamic_symtab_upper_bound (abfd);
553 if (storage < 0)
555 if (!(bfd_get_file_flags (abfd) & DYNAMIC))
557 non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd));
558 exit_status = 1;
559 dynsymcount = 0;
560 return NULL;
563 bfd_fatal (bfd_get_filename (abfd));
565 if (storage)
566 sy = (asymbol **) xmalloc (storage);
568 dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
569 if (dynsymcount < 0)
570 bfd_fatal (bfd_get_filename (abfd));
571 return sy;
574 /* Filter out (in place) symbols that are useless for disassembly.
575 COUNT is the number of elements in SYMBOLS.
576 Return the number of useful symbols. */
578 static long
579 remove_useless_symbols (asymbol **symbols, long count)
581 asymbol **in_ptr = symbols, **out_ptr = symbols;
583 while (--count >= 0)
585 asymbol *sym = *in_ptr++;
587 if (sym->name == NULL || sym->name[0] == '\0')
588 continue;
589 if (sym->flags & (BSF_DEBUGGING | BSF_SECTION_SYM))
590 continue;
591 if (bfd_is_und_section (sym->section)
592 || bfd_is_com_section (sym->section))
593 continue;
595 *out_ptr++ = sym;
597 return out_ptr - symbols;
600 /* Sort symbols into value order. */
602 static int
603 compare_symbols (const void *ap, const void *bp)
605 const asymbol *a = * (const asymbol **) ap;
606 const asymbol *b = * (const asymbol **) bp;
607 const char *an;
608 const char *bn;
609 size_t anl;
610 size_t bnl;
611 bfd_boolean af;
612 bfd_boolean bf;
613 flagword aflags;
614 flagword bflags;
616 if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
617 return 1;
618 else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
619 return -1;
621 if (a->section > b->section)
622 return 1;
623 else if (a->section < b->section)
624 return -1;
626 an = bfd_asymbol_name (a);
627 bn = bfd_asymbol_name (b);
628 anl = strlen (an);
629 bnl = strlen (bn);
631 /* The symbols gnu_compiled and gcc2_compiled convey no real
632 information, so put them after other symbols with the same value. */
633 af = (strstr (an, "gnu_compiled") != NULL
634 || strstr (an, "gcc2_compiled") != NULL);
635 bf = (strstr (bn, "gnu_compiled") != NULL
636 || strstr (bn, "gcc2_compiled") != NULL);
638 if (af && ! bf)
639 return 1;
640 if (! af && bf)
641 return -1;
643 /* We use a heuristic for the file name, to try to sort it after
644 more useful symbols. It may not work on non Unix systems, but it
645 doesn't really matter; the only difference is precisely which
646 symbol names get printed. */
648 #define file_symbol(s, sn, snl) \
649 (((s)->flags & BSF_FILE) != 0 \
650 || ((sn)[(snl) - 2] == '.' \
651 && ((sn)[(snl) - 1] == 'o' \
652 || (sn)[(snl) - 1] == 'a')))
654 af = file_symbol (a, an, anl);
655 bf = file_symbol (b, bn, bnl);
657 if (af && ! bf)
658 return 1;
659 if (! af && bf)
660 return -1;
662 /* Try to sort global symbols before local symbols before function
663 symbols before debugging symbols. */
665 aflags = a->flags;
666 bflags = b->flags;
668 if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING))
670 if ((aflags & BSF_DEBUGGING) != 0)
671 return 1;
672 else
673 return -1;
675 if ((aflags & BSF_FUNCTION) != (bflags & BSF_FUNCTION))
677 if ((aflags & BSF_FUNCTION) != 0)
678 return -1;
679 else
680 return 1;
682 if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL))
684 if ((aflags & BSF_LOCAL) != 0)
685 return 1;
686 else
687 return -1;
689 if ((aflags & BSF_GLOBAL) != (bflags & BSF_GLOBAL))
691 if ((aflags & BSF_GLOBAL) != 0)
692 return -1;
693 else
694 return 1;
697 /* Symbols that start with '.' might be section names, so sort them
698 after symbols that don't start with '.'. */
699 if (an[0] == '.' && bn[0] != '.')
700 return 1;
701 if (an[0] != '.' && bn[0] == '.')
702 return -1;
704 /* Finally, if we can't distinguish them in any other way, try to
705 get consistent results by sorting the symbols by name. */
706 return strcmp (an, bn);
709 /* Sort relocs into address order. */
711 static int
712 compare_relocs (const void *ap, const void *bp)
714 const arelent *a = * (const arelent **) ap;
715 const arelent *b = * (const arelent **) bp;
717 if (a->address > b->address)
718 return 1;
719 else if (a->address < b->address)
720 return -1;
722 /* So that associated relocations tied to the same address show up
723 in the correct order, we don't do any further sorting. */
724 if (a > b)
725 return 1;
726 else if (a < b)
727 return -1;
728 else
729 return 0;
732 /* Print an address (VMA) to the output stream in INFO.
733 If SKIP_ZEROES is TRUE, omit leading zeroes. */
735 static void
736 objdump_print_value (bfd_vma vma, struct disassemble_info *inf,
737 bfd_boolean skip_zeroes)
739 char buf[30];
740 char *p;
741 struct objdump_disasm_info *aux;
743 aux = (struct objdump_disasm_info *) inf->application_data;
744 bfd_sprintf_vma (aux->abfd, buf, vma);
745 if (! skip_zeroes)
746 p = buf;
747 else
749 for (p = buf; *p == '0'; ++p)
751 if (*p == '\0')
752 --p;
754 (*inf->fprintf_func) (inf->stream, "%s", p);
757 /* Print the name of a symbol. */
759 static void
760 objdump_print_symname (bfd *abfd, struct disassemble_info *inf,
761 asymbol *sym)
763 char *alloc;
764 const char *name;
766 alloc = NULL;
767 name = bfd_asymbol_name (sym);
768 if (do_demangle && name[0] != '\0')
770 /* Demangle the name. */
771 alloc = bfd_demangle (abfd, name, DMGL_ANSI | DMGL_PARAMS);
772 if (alloc != NULL)
773 name = alloc;
776 if (inf != NULL)
777 (*inf->fprintf_func) (inf->stream, "%s", name);
778 else
779 printf ("%s", name);
781 if (alloc != NULL)
782 free (alloc);
785 /* Locate a symbol given a bfd and a section (from INFO->application_data),
786 and a VMA. If INFO->application_data->require_sec is TRUE, then always
787 require the symbol to be in the section. Returns NULL if there is no
788 suitable symbol. If PLACE is not NULL, then *PLACE is set to the index
789 of the symbol in sorted_syms. */
791 static asymbol *
792 find_symbol_for_address (bfd_vma vma,
793 struct disassemble_info *inf,
794 long *place)
796 /* @@ Would it speed things up to cache the last two symbols returned,
797 and maybe their address ranges? For many processors, only one memory
798 operand can be present at a time, so the 2-entry cache wouldn't be
799 constantly churned by code doing heavy memory accesses. */
801 /* Indices in `sorted_syms'. */
802 long min = 0;
803 long max_count = sorted_symcount;
804 long thisplace;
805 struct objdump_disasm_info *aux;
806 bfd *abfd;
807 asection *sec;
808 unsigned int opb;
809 bfd_boolean want_section;
811 if (sorted_symcount < 1)
812 return NULL;
814 aux = (struct objdump_disasm_info *) inf->application_data;
815 abfd = aux->abfd;
816 sec = aux->sec;
817 opb = inf->octets_per_byte;
819 /* Perform a binary search looking for the closest symbol to the
820 required value. We are searching the range (min, max_count]. */
821 while (min + 1 < max_count)
823 asymbol *sym;
825 thisplace = (max_count + min) / 2;
826 sym = sorted_syms[thisplace];
828 if (bfd_asymbol_value (sym) > vma)
829 max_count = thisplace;
830 else if (bfd_asymbol_value (sym) < vma)
831 min = thisplace;
832 else
834 min = thisplace;
835 break;
839 /* The symbol we want is now in min, the low end of the range we
840 were searching. If there are several symbols with the same
841 value, we want the first one. */
842 thisplace = min;
843 while (thisplace > 0
844 && (bfd_asymbol_value (sorted_syms[thisplace])
845 == bfd_asymbol_value (sorted_syms[thisplace - 1])))
846 --thisplace;
848 /* Prefer a symbol in the current section if we have multple symbols
849 with the same value, as can occur with overlays or zero size
850 sections. */
851 min = thisplace;
852 while (min < max_count
853 && (bfd_asymbol_value (sorted_syms[min])
854 == bfd_asymbol_value (sorted_syms[thisplace])))
856 if (sorted_syms[min]->section == sec
857 && inf->symbol_is_valid (sorted_syms[min], inf))
859 thisplace = min;
861 if (place != NULL)
862 *place = thisplace;
864 return sorted_syms[thisplace];
866 ++min;
869 /* If the file is relocatable, and the symbol could be from this
870 section, prefer a symbol from this section over symbols from
871 others, even if the other symbol's value might be closer.
873 Note that this may be wrong for some symbol references if the
874 sections have overlapping memory ranges, but in that case there's
875 no way to tell what's desired without looking at the relocation
876 table.
878 Also give the target a chance to reject symbols. */
879 want_section = (aux->require_sec
880 || ((abfd->flags & HAS_RELOC) != 0
881 && vma >= bfd_get_section_vma (abfd, sec)
882 && vma < (bfd_get_section_vma (abfd, sec)
883 + bfd_section_size (abfd, sec) / opb)));
884 if ((sorted_syms[thisplace]->section != sec && want_section)
885 || ! inf->symbol_is_valid (sorted_syms[thisplace], inf))
887 long i;
888 long newplace = sorted_symcount;
890 for (i = min - 1; i >= 0; i--)
892 if ((sorted_syms[i]->section == sec || !want_section)
893 && inf->symbol_is_valid (sorted_syms[i], inf))
895 if (newplace == sorted_symcount)
896 newplace = i;
898 if (bfd_asymbol_value (sorted_syms[i])
899 != bfd_asymbol_value (sorted_syms[newplace]))
900 break;
902 /* Remember this symbol and keep searching until we reach
903 an earlier address. */
904 newplace = i;
908 if (newplace != sorted_symcount)
909 thisplace = newplace;
910 else
912 /* We didn't find a good symbol with a smaller value.
913 Look for one with a larger value. */
914 for (i = thisplace + 1; i < sorted_symcount; i++)
916 if ((sorted_syms[i]->section == sec || !want_section)
917 && inf->symbol_is_valid (sorted_syms[i], inf))
919 thisplace = i;
920 break;
925 if ((sorted_syms[thisplace]->section != sec && want_section)
926 || ! inf->symbol_is_valid (sorted_syms[thisplace], inf))
927 /* There is no suitable symbol. */
928 return NULL;
931 if (place != NULL)
932 *place = thisplace;
934 return sorted_syms[thisplace];
937 /* Print an address and the offset to the nearest symbol. */
939 static void
940 objdump_print_addr_with_sym (bfd *abfd, asection *sec, asymbol *sym,
941 bfd_vma vma, struct disassemble_info *inf,
942 bfd_boolean skip_zeroes)
944 objdump_print_value (vma, inf, skip_zeroes);
946 if (sym == NULL)
948 bfd_vma secaddr;
950 (*inf->fprintf_func) (inf->stream, " <%s",
951 bfd_get_section_name (abfd, sec));
952 secaddr = bfd_get_section_vma (abfd, sec);
953 if (vma < secaddr)
955 (*inf->fprintf_func) (inf->stream, "-0x");
956 objdump_print_value (secaddr - vma, inf, TRUE);
958 else if (vma > secaddr)
960 (*inf->fprintf_func) (inf->stream, "+0x");
961 objdump_print_value (vma - secaddr, inf, TRUE);
963 (*inf->fprintf_func) (inf->stream, ">");
965 else
967 (*inf->fprintf_func) (inf->stream, " <");
968 objdump_print_symname (abfd, inf, sym);
969 if (bfd_asymbol_value (sym) > vma)
971 (*inf->fprintf_func) (inf->stream, "-0x");
972 objdump_print_value (bfd_asymbol_value (sym) - vma, inf, TRUE);
974 else if (vma > bfd_asymbol_value (sym))
976 (*inf->fprintf_func) (inf->stream, "+0x");
977 objdump_print_value (vma - bfd_asymbol_value (sym), inf, TRUE);
979 (*inf->fprintf_func) (inf->stream, ">");
982 if (display_file_offsets)
983 inf->fprintf_func (inf->stream, _(" (File Offset: 0x%lx)"),
984 (long int)(sec->filepos + (vma - sec->vma)));
987 /* Print an address (VMA), symbolically if possible.
988 If SKIP_ZEROES is TRUE, don't output leading zeroes. */
990 static void
991 objdump_print_addr (bfd_vma vma,
992 struct disassemble_info *inf,
993 bfd_boolean skip_zeroes)
995 struct objdump_disasm_info *aux;
996 asymbol *sym = NULL;
997 bfd_boolean skip_find = FALSE;
999 aux = (struct objdump_disasm_info *) inf->application_data;
1001 if (sorted_symcount < 1)
1003 (*inf->fprintf_func) (inf->stream, "0x");
1004 objdump_print_value (vma, inf, skip_zeroes);
1006 if (display_file_offsets)
1007 inf->fprintf_func (inf->stream, _(" (File Offset: 0x%lx)"),
1008 (long int)(aux->sec->filepos + (vma - aux->sec->vma)));
1009 return;
1012 if (aux->reloc != NULL
1013 && aux->reloc->sym_ptr_ptr != NULL
1014 && * aux->reloc->sym_ptr_ptr != NULL)
1016 sym = * aux->reloc->sym_ptr_ptr;
1018 /* Adjust the vma to the reloc. */
1019 vma += bfd_asymbol_value (sym);
1021 if (bfd_is_und_section (bfd_get_section (sym)))
1022 skip_find = TRUE;
1025 if (!skip_find)
1026 sym = find_symbol_for_address (vma, inf, NULL);
1028 objdump_print_addr_with_sym (aux->abfd, aux->sec, sym, vma, inf,
1029 skip_zeroes);
1032 /* Print VMA to INFO. This function is passed to the disassembler
1033 routine. */
1035 static void
1036 objdump_print_address (bfd_vma vma, struct disassemble_info *inf)
1038 objdump_print_addr (vma, inf, ! prefix_addresses);
1041 /* Determine if the given address has a symbol associated with it. */
1043 static int
1044 objdump_symbol_at_address (bfd_vma vma, struct disassemble_info * inf)
1046 asymbol * sym;
1048 sym = find_symbol_for_address (vma, inf, NULL);
1050 return (sym != NULL && (bfd_asymbol_value (sym) == vma));
1053 /* Hold the last function name and the last line number we displayed
1054 in a disassembly. */
1056 static char *prev_functionname;
1057 static unsigned int prev_line;
1059 /* We keep a list of all files that we have seen when doing a
1060 disassembly with source, so that we know how much of the file to
1061 display. This can be important for inlined functions. */
1063 struct print_file_list
1065 struct print_file_list *next;
1066 const char *filename;
1067 const char *modname;
1068 const char *map;
1069 size_t mapsize;
1070 const char **linemap;
1071 unsigned maxline;
1072 unsigned last_line;
1073 int first;
1076 static struct print_file_list *print_files;
1078 /* The number of preceding context lines to show when we start
1079 displaying a file for the first time. */
1081 #define SHOW_PRECEDING_CONTEXT_LINES (5)
1083 /* Read a complete file into memory. */
1085 static const char *
1086 slurp_file (const char *fn, size_t *size)
1088 #ifdef HAVE_MMAP
1089 int ps = getpagesize ();
1090 size_t msize;
1091 #endif
1092 const char *map;
1093 struct stat st;
1094 int fd = open (fn, O_RDONLY | O_BINARY);
1096 if (fd < 0)
1097 return NULL;
1098 if (fstat (fd, &st) < 0)
1099 return NULL;
1100 *size = st.st_size;
1101 #ifdef HAVE_MMAP
1102 msize = (*size + ps - 1) & ~(ps - 1);
1103 map = mmap (NULL, msize, PROT_READ, MAP_SHARED, fd, 0);
1104 if (map != (char *)-1L)
1106 close(fd);
1107 return map;
1109 #endif
1110 map = (const char *) malloc (*size);
1111 if (!map || (size_t) read (fd, (char *)map, *size) != *size)
1113 free ((void *)map);
1114 map = NULL;
1116 close (fd);
1117 return map;
1120 #define line_map_decrease 5
1122 /* Precompute array of lines for a mapped file. */
1124 static const char **
1125 index_file (const char *map, size_t size, unsigned int *maxline)
1127 const char *p, *lstart, *end;
1128 int chars_per_line = 45; /* First iteration will use 40. */
1129 unsigned int lineno;
1130 const char **linemap = NULL;
1131 unsigned long line_map_size = 0;
1133 lineno = 0;
1134 lstart = map;
1135 end = map + size;
1137 for (p = map; p < end; p++)
1139 if (*p == '\n')
1141 if (p + 1 < end && p[1] == '\r')
1142 p++;
1144 else if (*p == '\r')
1146 if (p + 1 < end && p[1] == '\n')
1147 p++;
1149 else
1150 continue;
1152 /* End of line found. */
1154 if (linemap == NULL || line_map_size < lineno + 1)
1156 unsigned long newsize;
1158 chars_per_line -= line_map_decrease;
1159 if (chars_per_line <= 1)
1160 chars_per_line = 1;
1161 line_map_size = size / chars_per_line + 1;
1162 if (line_map_size < lineno + 1)
1163 line_map_size = lineno + 1;
1164 newsize = line_map_size * sizeof (char *);
1165 linemap = (const char **) xrealloc (linemap, newsize);
1168 linemap[lineno++] = lstart;
1169 lstart = p + 1;
1172 *maxline = lineno;
1173 return linemap;
1176 /* Tries to open MODNAME, and if successful adds a node to print_files
1177 linked list and returns that node. Returns NULL on failure. */
1179 static struct print_file_list *
1180 try_print_file_open (const char *origname, const char *modname)
1182 struct print_file_list *p;
1184 p = (struct print_file_list *) xmalloc (sizeof (struct print_file_list));
1186 p->map = slurp_file (modname, &p->mapsize);
1187 if (p->map == NULL)
1189 free (p);
1190 return NULL;
1193 p->linemap = index_file (p->map, p->mapsize, &p->maxline);
1194 p->last_line = 0;
1195 p->filename = origname;
1196 p->modname = modname;
1197 p->next = print_files;
1198 p->first = 1;
1199 print_files = p;
1200 return p;
1203 /* If the the source file, as described in the symtab, is not found
1204 try to locate it in one of the paths specified with -I
1205 If found, add location to print_files linked list. */
1207 static struct print_file_list *
1208 update_source_path (const char *filename)
1210 struct print_file_list *p;
1211 const char *fname;
1212 int i;
1214 p = try_print_file_open (filename, filename);
1215 if (p != NULL)
1216 return p;
1218 if (include_path_count == 0)
1219 return NULL;
1221 /* Get the name of the file. */
1222 fname = lbasename (filename);
1224 /* If file exists under a new path, we need to add it to the list
1225 so that show_line knows about it. */
1226 for (i = 0; i < include_path_count; i++)
1228 char *modname = concat (include_paths[i], "/", fname, (const char *) 0);
1230 p = try_print_file_open (filename, modname);
1231 if (p)
1232 return p;
1234 free (modname);
1237 return NULL;
1240 /* Print a source file line. */
1242 static void
1243 print_line (struct print_file_list *p, unsigned int linenum)
1245 const char *l;
1246 size_t len;
1248 --linenum;
1249 if (linenum >= p->maxline)
1250 return;
1251 l = p->linemap [linenum];
1252 /* Test fwrite return value to quiet glibc warning. */
1253 len = strcspn (l, "\n\r");
1254 if (len == 0 || fwrite (l, len, 1, stdout) == 1)
1255 putchar ('\n');
1258 /* Print a range of source code lines. */
1260 static void
1261 dump_lines (struct print_file_list *p, unsigned int start, unsigned int end)
1263 if (p->map == NULL)
1264 return;
1265 while (start <= end)
1267 print_line (p, start);
1268 start++;
1272 /* Show the line number, or the source line, in a disassembly
1273 listing. */
1275 static void
1276 show_line (bfd *abfd, asection *section, bfd_vma addr_offset)
1278 const char *filename;
1279 const char *functionname;
1280 unsigned int linenumber;
1281 bfd_boolean reloc;
1283 if (! with_line_numbers && ! with_source_code)
1284 return;
1286 if (! bfd_find_nearest_line (abfd, section, syms, addr_offset, &filename,
1287 &functionname, &linenumber))
1288 return;
1290 if (filename != NULL && *filename == '\0')
1291 filename = NULL;
1292 if (functionname != NULL && *functionname == '\0')
1293 functionname = NULL;
1295 if (filename
1296 && IS_ABSOLUTE_PATH (filename)
1297 && prefix)
1299 char *path_up;
1300 const char *fname = filename;
1301 char *path = (char *) alloca (prefix_length + PATH_MAX + 1);
1303 if (prefix_length)
1304 memcpy (path, prefix, prefix_length);
1305 path_up = path + prefix_length;
1307 /* Build relocated filename, stripping off leading directories
1308 from the initial filename if requested. */
1309 if (prefix_strip > 0)
1311 int level = 0;
1312 const char *s;
1314 /* Skip selected directory levels. */
1315 for (s = fname + 1; *s != '\0' && level < prefix_strip; s++)
1316 if (IS_DIR_SEPARATOR(*s))
1318 fname = s;
1319 level++;
1323 /* Update complete filename. */
1324 strncpy (path_up, fname, PATH_MAX);
1325 path_up[PATH_MAX] = '\0';
1327 filename = path;
1328 reloc = TRUE;
1330 else
1331 reloc = FALSE;
1333 if (with_line_numbers)
1335 if (functionname != NULL
1336 && (prev_functionname == NULL
1337 || strcmp (functionname, prev_functionname) != 0))
1338 printf ("%s():\n", functionname);
1339 if (linenumber > 0 && linenumber != prev_line)
1340 printf ("%s:%u\n", filename == NULL ? "???" : filename, linenumber);
1343 if (with_source_code
1344 && filename != NULL
1345 && linenumber > 0)
1347 struct print_file_list **pp, *p;
1348 unsigned l;
1350 for (pp = &print_files; *pp != NULL; pp = &(*pp)->next)
1351 if (filename_cmp ((*pp)->filename, filename) == 0)
1352 break;
1353 p = *pp;
1355 if (p == NULL)
1357 if (reloc)
1358 filename = xstrdup (filename);
1359 p = update_source_path (filename);
1362 if (p != NULL && linenumber != p->last_line)
1364 if (file_start_context && p->first)
1365 l = 1;
1366 else
1368 l = linenumber - SHOW_PRECEDING_CONTEXT_LINES;
1369 if (l >= linenumber)
1370 l = 1;
1371 if (p->last_line >= l && p->last_line <= linenumber)
1372 l = p->last_line + 1;
1374 dump_lines (p, l, linenumber);
1375 p->last_line = linenumber;
1376 p->first = 0;
1380 if (functionname != NULL
1381 && (prev_functionname == NULL
1382 || strcmp (functionname, prev_functionname) != 0))
1384 if (prev_functionname != NULL)
1385 free (prev_functionname);
1386 prev_functionname = (char *) xmalloc (strlen (functionname) + 1);
1387 strcpy (prev_functionname, functionname);
1390 if (linenumber > 0 && linenumber != prev_line)
1391 prev_line = linenumber;
1394 /* Pseudo FILE object for strings. */
1395 typedef struct
1397 char *buffer;
1398 size_t pos;
1399 size_t alloc;
1400 } SFILE;
1402 /* sprintf to a "stream". */
1404 static int ATTRIBUTE_PRINTF_2
1405 objdump_sprintf (SFILE *f, const char *format, ...)
1407 size_t n;
1408 va_list args;
1410 while (1)
1412 size_t space = f->alloc - f->pos;
1414 va_start (args, format);
1415 n = vsnprintf (f->buffer + f->pos, space, format, args);
1416 va_end (args);
1418 if (space > n)
1419 break;
1421 f->alloc = (f->alloc + n) * 2;
1422 f->buffer = (char *) xrealloc (f->buffer, f->alloc);
1424 f->pos += n;
1426 return n;
1429 /* The number of zeroes we want to see before we start skipping them.
1430 The number is arbitrarily chosen. */
1432 #define DEFAULT_SKIP_ZEROES 8
1434 /* The number of zeroes to skip at the end of a section. If the
1435 number of zeroes at the end is between SKIP_ZEROES_AT_END and
1436 SKIP_ZEROES, they will be disassembled. If there are fewer than
1437 SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic
1438 attempt to avoid disassembling zeroes inserted by section
1439 alignment. */
1441 #define DEFAULT_SKIP_ZEROES_AT_END 3
1443 /* Disassemble some data in memory between given values. */
1445 static void
1446 disassemble_bytes (struct disassemble_info * inf,
1447 disassembler_ftype disassemble_fn,
1448 bfd_boolean insns,
1449 bfd_byte * data,
1450 bfd_vma start_offset,
1451 bfd_vma stop_offset,
1452 bfd_vma rel_offset,
1453 arelent *** relppp,
1454 arelent ** relppend)
1456 struct objdump_disasm_info *aux;
1457 asection *section;
1458 int octets_per_line;
1459 int skip_addr_chars;
1460 bfd_vma addr_offset;
1461 unsigned int opb = inf->octets_per_byte;
1462 unsigned int skip_zeroes = inf->skip_zeroes;
1463 unsigned int skip_zeroes_at_end = inf->skip_zeroes_at_end;
1464 int octets = opb;
1465 SFILE sfile;
1467 aux = (struct objdump_disasm_info *) inf->application_data;
1468 section = aux->sec;
1470 sfile.alloc = 120;
1471 sfile.buffer = (char *) xmalloc (sfile.alloc);
1472 sfile.pos = 0;
1474 if (insn_width)
1475 octets_per_line = insn_width;
1476 else if (insns)
1477 octets_per_line = 4;
1478 else
1479 octets_per_line = 16;
1481 /* Figure out how many characters to skip at the start of an
1482 address, to make the disassembly look nicer. We discard leading
1483 zeroes in chunks of 4, ensuring that there is always a leading
1484 zero remaining. */
1485 skip_addr_chars = 0;
1486 if (! prefix_addresses)
1488 char buf[30];
1490 bfd_sprintf_vma (aux->abfd, buf, section->vma + section->size / opb);
1492 while (buf[skip_addr_chars] == '0')
1493 ++skip_addr_chars;
1495 /* Don't discard zeros on overflow. */
1496 if (buf[skip_addr_chars] == '\0' && section->vma != 0)
1497 skip_addr_chars = 0;
1499 if (skip_addr_chars != 0)
1500 skip_addr_chars = (skip_addr_chars - 1) & -4;
1503 inf->insn_info_valid = 0;
1505 addr_offset = start_offset;
1506 while (addr_offset < stop_offset)
1508 bfd_vma z;
1509 bfd_boolean need_nl = FALSE;
1510 int previous_octets;
1512 /* Remember the length of the previous instruction. */
1513 previous_octets = octets;
1514 octets = 0;
1516 /* Make sure we don't use relocs from previous instructions. */
1517 aux->reloc = NULL;
1519 /* If we see more than SKIP_ZEROES octets of zeroes, we just
1520 print `...'. */
1521 for (z = addr_offset * opb; z < stop_offset * opb; z++)
1522 if (data[z] != 0)
1523 break;
1524 if (! disassemble_zeroes
1525 && (inf->insn_info_valid == 0
1526 || inf->branch_delay_insns == 0)
1527 && (z - addr_offset * opb >= skip_zeroes
1528 || (z == stop_offset * opb &&
1529 z - addr_offset * opb < skip_zeroes_at_end)))
1531 /* If there are more nonzero octets to follow, we only skip
1532 zeroes in multiples of 4, to try to avoid running over
1533 the start of an instruction which happens to start with
1534 zero. */
1535 if (z != stop_offset * opb)
1536 z = addr_offset * opb + ((z - addr_offset * opb) &~ 3);
1538 octets = z - addr_offset * opb;
1540 /* If we are going to display more data, and we are displaying
1541 file offsets, then tell the user how many zeroes we skip
1542 and the file offset from where we resume dumping. */
1543 if (display_file_offsets && ((addr_offset + (octets / opb)) < stop_offset))
1544 printf ("\t... (skipping %d zeroes, resuming at file offset: 0x%lx)\n",
1545 octets / opb,
1546 (unsigned long) (section->filepos
1547 + (addr_offset + (octets / opb))));
1548 else
1549 printf ("\t...\n");
1551 else
1553 char buf[50];
1554 int bpc = 0;
1555 int pb = 0;
1557 if (with_line_numbers || with_source_code)
1558 show_line (aux->abfd, section, addr_offset);
1560 if (! prefix_addresses)
1562 char *s;
1564 bfd_sprintf_vma (aux->abfd, buf, section->vma + addr_offset);
1565 for (s = buf + skip_addr_chars; *s == '0'; s++)
1566 *s = ' ';
1567 if (*s == '\0')
1568 *--s = '0';
1569 printf ("%s:\t", buf + skip_addr_chars);
1571 else
1573 aux->require_sec = TRUE;
1574 objdump_print_address (section->vma + addr_offset, inf);
1575 aux->require_sec = FALSE;
1576 putchar (' ');
1579 if (insns)
1581 sfile.pos = 0;
1582 inf->fprintf_func = (fprintf_ftype) objdump_sprintf;
1583 inf->stream = &sfile;
1584 inf->bytes_per_line = 0;
1585 inf->bytes_per_chunk = 0;
1586 inf->flags = disassemble_all ? DISASSEMBLE_DATA : 0;
1587 if (machine)
1588 inf->flags |= USER_SPECIFIED_MACHINE_TYPE;
1590 if (inf->disassembler_needs_relocs
1591 && (bfd_get_file_flags (aux->abfd) & EXEC_P) == 0
1592 && (bfd_get_file_flags (aux->abfd) & DYNAMIC) == 0
1593 && *relppp < relppend)
1595 bfd_signed_vma distance_to_rel;
1597 distance_to_rel = (**relppp)->address
1598 - (rel_offset + addr_offset);
1600 /* Check to see if the current reloc is associated with
1601 the instruction that we are about to disassemble. */
1602 if (distance_to_rel == 0
1603 /* FIXME: This is wrong. We are trying to catch
1604 relocs that are addressed part way through the
1605 current instruction, as might happen with a packed
1606 VLIW instruction. Unfortunately we do not know the
1607 length of the current instruction since we have not
1608 disassembled it yet. Instead we take a guess based
1609 upon the length of the previous instruction. The
1610 proper solution is to have a new target-specific
1611 disassembler function which just returns the length
1612 of an instruction at a given address without trying
1613 to display its disassembly. */
1614 || (distance_to_rel > 0
1615 && distance_to_rel < (bfd_signed_vma) (previous_octets/ opb)))
1617 inf->flags |= INSN_HAS_RELOC;
1618 aux->reloc = **relppp;
1622 octets = (*disassemble_fn) (section->vma + addr_offset, inf);
1623 inf->fprintf_func = (fprintf_ftype) fprintf;
1624 inf->stream = stdout;
1625 if (insn_width == 0 && inf->bytes_per_line != 0)
1626 octets_per_line = inf->bytes_per_line;
1627 if (octets < (int) opb)
1629 if (sfile.pos)
1630 printf ("%s\n", sfile.buffer);
1631 if (octets >= 0)
1633 non_fatal (_("disassemble_fn returned length %d"),
1634 octets);
1635 exit_status = 1;
1637 break;
1640 else
1642 bfd_vma j;
1644 octets = octets_per_line;
1645 if (addr_offset + octets / opb > stop_offset)
1646 octets = (stop_offset - addr_offset) * opb;
1648 for (j = addr_offset * opb; j < addr_offset * opb + octets; ++j)
1650 if (ISPRINT (data[j]))
1651 buf[j - addr_offset * opb] = data[j];
1652 else
1653 buf[j - addr_offset * opb] = '.';
1655 buf[j - addr_offset * opb] = '\0';
1658 if (prefix_addresses
1659 ? show_raw_insn > 0
1660 : show_raw_insn >= 0)
1662 bfd_vma j;
1664 /* If ! prefix_addresses and ! wide_output, we print
1665 octets_per_line octets per line. */
1666 pb = octets;
1667 if (pb > octets_per_line && ! prefix_addresses && ! wide_output)
1668 pb = octets_per_line;
1670 if (inf->bytes_per_chunk)
1671 bpc = inf->bytes_per_chunk;
1672 else
1673 bpc = 1;
1675 for (j = addr_offset * opb; j < addr_offset * opb + pb; j += bpc)
1677 int k;
1679 if (bpc > 1 && inf->display_endian == BFD_ENDIAN_LITTLE)
1681 for (k = bpc - 1; k >= 0; k--)
1682 printf ("%02x", (unsigned) data[j + k]);
1683 putchar (' ');
1685 else
1687 for (k = 0; k < bpc; k++)
1688 printf ("%02x", (unsigned) data[j + k]);
1689 putchar (' ');
1693 for (; pb < octets_per_line; pb += bpc)
1695 int k;
1697 for (k = 0; k < bpc; k++)
1698 printf (" ");
1699 putchar (' ');
1702 /* Separate raw data from instruction by extra space. */
1703 if (insns)
1704 putchar ('\t');
1705 else
1706 printf (" ");
1709 if (! insns)
1710 printf ("%s", buf);
1711 else if (sfile.pos)
1712 printf ("%s", sfile.buffer);
1714 if (prefix_addresses
1715 ? show_raw_insn > 0
1716 : show_raw_insn >= 0)
1718 while (pb < octets)
1720 bfd_vma j;
1721 char *s;
1723 putchar ('\n');
1724 j = addr_offset * opb + pb;
1726 bfd_sprintf_vma (aux->abfd, buf, section->vma + j / opb);
1727 for (s = buf + skip_addr_chars; *s == '0'; s++)
1728 *s = ' ';
1729 if (*s == '\0')
1730 *--s = '0';
1731 printf ("%s:\t", buf + skip_addr_chars);
1733 pb += octets_per_line;
1734 if (pb > octets)
1735 pb = octets;
1736 for (; j < addr_offset * opb + pb; j += bpc)
1738 int k;
1740 if (bpc > 1 && inf->display_endian == BFD_ENDIAN_LITTLE)
1742 for (k = bpc - 1; k >= 0; k--)
1743 printf ("%02x", (unsigned) data[j + k]);
1744 putchar (' ');
1746 else
1748 for (k = 0; k < bpc; k++)
1749 printf ("%02x", (unsigned) data[j + k]);
1750 putchar (' ');
1756 if (!wide_output)
1757 putchar ('\n');
1758 else
1759 need_nl = TRUE;
1762 while ((*relppp) < relppend
1763 && (**relppp)->address < rel_offset + addr_offset + octets / opb)
1765 if (dump_reloc_info || dump_dynamic_reloc_info)
1767 arelent *q;
1769 q = **relppp;
1771 if (wide_output)
1772 putchar ('\t');
1773 else
1774 printf ("\t\t\t");
1776 objdump_print_value (section->vma - rel_offset + q->address,
1777 inf, TRUE);
1779 if (q->howto == NULL)
1780 printf (": *unknown*\t");
1781 else if (q->howto->name)
1782 printf (": %s\t", q->howto->name);
1783 else
1784 printf (": %d\t", q->howto->type);
1786 if (q->sym_ptr_ptr == NULL || *q->sym_ptr_ptr == NULL)
1787 printf ("*unknown*");
1788 else
1790 const char *sym_name;
1792 sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
1793 if (sym_name != NULL && *sym_name != '\0')
1794 objdump_print_symname (aux->abfd, inf, *q->sym_ptr_ptr);
1795 else
1797 asection *sym_sec;
1799 sym_sec = bfd_get_section (*q->sym_ptr_ptr);
1800 sym_name = bfd_get_section_name (aux->abfd, sym_sec);
1801 if (sym_name == NULL || *sym_name == '\0')
1802 sym_name = "*unknown*";
1803 printf ("%s", sym_name);
1807 if (q->addend)
1809 printf ("+0x");
1810 objdump_print_value (q->addend, inf, TRUE);
1813 printf ("\n");
1814 need_nl = FALSE;
1816 ++(*relppp);
1819 if (need_nl)
1820 printf ("\n");
1822 addr_offset += octets / opb;
1825 free (sfile.buffer);
1828 static void
1829 disassemble_section (bfd *abfd, asection *section, void *inf)
1831 const struct elf_backend_data * bed;
1832 bfd_vma sign_adjust = 0;
1833 struct disassemble_info * pinfo = (struct disassemble_info *) inf;
1834 struct objdump_disasm_info * paux;
1835 unsigned int opb = pinfo->octets_per_byte;
1836 bfd_byte * data = NULL;
1837 bfd_size_type datasize = 0;
1838 arelent ** rel_pp = NULL;
1839 arelent ** rel_ppstart = NULL;
1840 arelent ** rel_ppend;
1841 unsigned long stop_offset;
1842 asymbol * sym = NULL;
1843 long place = 0;
1844 long rel_count;
1845 bfd_vma rel_offset;
1846 unsigned long addr_offset;
1848 /* Sections that do not contain machine
1849 code are not normally disassembled. */
1850 if (! disassemble_all
1851 && only_list == NULL
1852 && ((section->flags & (SEC_CODE | SEC_HAS_CONTENTS))
1853 != (SEC_CODE | SEC_HAS_CONTENTS)))
1854 return;
1856 if (! process_section_p (section))
1857 return;
1859 datasize = bfd_get_section_size (section);
1860 if (datasize == 0)
1861 return;
1863 /* Decide which set of relocs to use. Load them if necessary. */
1864 paux = (struct objdump_disasm_info *) pinfo->application_data;
1865 if (paux->dynrelbuf)
1867 rel_pp = paux->dynrelbuf;
1868 rel_count = paux->dynrelcount;
1869 /* Dynamic reloc addresses are absolute, non-dynamic are section
1870 relative. REL_OFFSET specifies the reloc address corresponding
1871 to the start of this section. */
1872 rel_offset = section->vma;
1874 else
1876 rel_count = 0;
1877 rel_pp = NULL;
1878 rel_offset = 0;
1880 if ((section->flags & SEC_RELOC) != 0
1881 && (dump_reloc_info || pinfo->disassembler_needs_relocs))
1883 long relsize;
1885 relsize = bfd_get_reloc_upper_bound (abfd, section);
1886 if (relsize < 0)
1887 bfd_fatal (bfd_get_filename (abfd));
1889 if (relsize > 0)
1891 rel_ppstart = rel_pp = (arelent **) xmalloc (relsize);
1892 rel_count = bfd_canonicalize_reloc (abfd, section, rel_pp, syms);
1893 if (rel_count < 0)
1894 bfd_fatal (bfd_get_filename (abfd));
1896 /* Sort the relocs by address. */
1897 qsort (rel_pp, rel_count, sizeof (arelent *), compare_relocs);
1901 rel_ppend = rel_pp + rel_count;
1903 data = (bfd_byte *) xmalloc (datasize);
1905 bfd_get_section_contents (abfd, section, data, 0, datasize);
1907 paux->sec = section;
1908 pinfo->buffer = data;
1909 pinfo->buffer_vma = section->vma;
1910 pinfo->buffer_length = datasize;
1911 pinfo->section = section;
1913 if (start_address == (bfd_vma) -1
1914 || start_address < pinfo->buffer_vma)
1915 addr_offset = 0;
1916 else
1917 addr_offset = start_address - pinfo->buffer_vma;
1919 if (stop_address == (bfd_vma) -1)
1920 stop_offset = datasize / opb;
1921 else
1923 if (stop_address < pinfo->buffer_vma)
1924 stop_offset = 0;
1925 else
1926 stop_offset = stop_address - pinfo->buffer_vma;
1927 if (stop_offset > pinfo->buffer_length / opb)
1928 stop_offset = pinfo->buffer_length / opb;
1931 /* Skip over the relocs belonging to addresses below the
1932 start address. */
1933 while (rel_pp < rel_ppend
1934 && (*rel_pp)->address < rel_offset + addr_offset)
1935 ++rel_pp;
1937 if (addr_offset < stop_offset)
1938 printf (_("\nDisassembly of section %s:\n"), section->name);
1940 /* Find the nearest symbol forwards from our current position. */
1941 paux->require_sec = TRUE;
1942 sym = (asymbol *) find_symbol_for_address (section->vma + addr_offset,
1943 (struct disassemble_info *) inf,
1944 &place);
1945 paux->require_sec = FALSE;
1947 /* PR 9774: If the target used signed addresses then we must make
1948 sure that we sign extend the value that we calculate for 'addr'
1949 in the loop below. */
1950 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1951 && (bed = get_elf_backend_data (abfd)) != NULL
1952 && bed->sign_extend_vma)
1953 sign_adjust = (bfd_vma) 1 << (bed->s->arch_size - 1);
1955 /* Disassemble a block of instructions up to the address associated with
1956 the symbol we have just found. Then print the symbol and find the
1957 next symbol on. Repeat until we have disassembled the entire section
1958 or we have reached the end of the address range we are interested in. */
1959 while (addr_offset < stop_offset)
1961 bfd_vma addr;
1962 asymbol *nextsym;
1963 unsigned long nextstop_offset;
1964 bfd_boolean insns;
1966 addr = section->vma + addr_offset;
1967 addr = ((addr & ((sign_adjust << 1) - 1)) ^ sign_adjust) - sign_adjust;
1969 if (sym != NULL && bfd_asymbol_value (sym) <= addr)
1971 int x;
1973 for (x = place;
1974 (x < sorted_symcount
1975 && (bfd_asymbol_value (sorted_syms[x]) <= addr));
1976 ++x)
1977 continue;
1979 pinfo->symbols = sorted_syms + place;
1980 pinfo->num_symbols = x - place;
1981 pinfo->symtab_pos = place;
1983 else
1985 pinfo->symbols = NULL;
1986 pinfo->num_symbols = 0;
1987 pinfo->symtab_pos = -1;
1990 if (! prefix_addresses)
1992 pinfo->fprintf_func (pinfo->stream, "\n");
1993 objdump_print_addr_with_sym (abfd, section, sym, addr,
1994 pinfo, FALSE);
1995 pinfo->fprintf_func (pinfo->stream, ":\n");
1998 if (sym != NULL && bfd_asymbol_value (sym) > addr)
1999 nextsym = sym;
2000 else if (sym == NULL)
2001 nextsym = NULL;
2002 else
2004 #define is_valid_next_sym(SYM) \
2005 ((SYM)->section == section \
2006 && (bfd_asymbol_value (SYM) > bfd_asymbol_value (sym)) \
2007 && pinfo->symbol_is_valid (SYM, pinfo))
2009 /* Search forward for the next appropriate symbol in
2010 SECTION. Note that all the symbols are sorted
2011 together into one big array, and that some sections
2012 may have overlapping addresses. */
2013 while (place < sorted_symcount
2014 && ! is_valid_next_sym (sorted_syms [place]))
2015 ++place;
2017 if (place >= sorted_symcount)
2018 nextsym = NULL;
2019 else
2020 nextsym = sorted_syms[place];
2023 if (sym != NULL && bfd_asymbol_value (sym) > addr)
2024 nextstop_offset = bfd_asymbol_value (sym) - section->vma;
2025 else if (nextsym == NULL)
2026 nextstop_offset = stop_offset;
2027 else
2028 nextstop_offset = bfd_asymbol_value (nextsym) - section->vma;
2030 if (nextstop_offset > stop_offset
2031 || nextstop_offset <= addr_offset)
2032 nextstop_offset = stop_offset;
2034 /* If a symbol is explicitly marked as being an object
2035 rather than a function, just dump the bytes without
2036 disassembling them. */
2037 if (disassemble_all
2038 || sym == NULL
2039 || sym->section != section
2040 || bfd_asymbol_value (sym) > addr
2041 || ((sym->flags & BSF_OBJECT) == 0
2042 && (strstr (bfd_asymbol_name (sym), "gnu_compiled")
2043 == NULL)
2044 && (strstr (bfd_asymbol_name (sym), "gcc2_compiled")
2045 == NULL))
2046 || (sym->flags & BSF_FUNCTION) != 0)
2047 insns = TRUE;
2048 else
2049 insns = FALSE;
2051 disassemble_bytes (pinfo, paux->disassemble_fn, insns, data,
2052 addr_offset, nextstop_offset,
2053 rel_offset, &rel_pp, rel_ppend);
2055 addr_offset = nextstop_offset;
2056 sym = nextsym;
2059 free (data);
2061 if (rel_ppstart != NULL)
2062 free (rel_ppstart);
2065 /* Disassemble the contents of an object file. */
2067 static void
2068 disassemble_data (bfd *abfd)
2070 struct disassemble_info disasm_info;
2071 struct objdump_disasm_info aux;
2072 long i;
2074 print_files = NULL;
2075 prev_functionname = NULL;
2076 prev_line = -1;
2078 /* We make a copy of syms to sort. We don't want to sort syms
2079 because that will screw up the relocs. */
2080 sorted_symcount = symcount ? symcount : dynsymcount;
2081 sorted_syms = (asymbol **) xmalloc ((sorted_symcount + synthcount)
2082 * sizeof (asymbol *));
2083 memcpy (sorted_syms, symcount ? syms : dynsyms,
2084 sorted_symcount * sizeof (asymbol *));
2086 sorted_symcount = remove_useless_symbols (sorted_syms, sorted_symcount);
2088 for (i = 0; i < synthcount; ++i)
2090 sorted_syms[sorted_symcount] = synthsyms + i;
2091 ++sorted_symcount;
2094 /* Sort the symbols into section and symbol order. */
2095 qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols);
2097 init_disassemble_info (&disasm_info, stdout, (fprintf_ftype) fprintf);
2099 disasm_info.application_data = (void *) &aux;
2100 aux.abfd = abfd;
2101 aux.require_sec = FALSE;
2102 aux.dynrelbuf = NULL;
2103 aux.dynrelcount = 0;
2104 aux.reloc = NULL;
2106 disasm_info.print_address_func = objdump_print_address;
2107 disasm_info.symbol_at_address_func = objdump_symbol_at_address;
2109 if (machine != NULL)
2111 const bfd_arch_info_type *inf = bfd_scan_arch (machine);
2113 if (inf == NULL)
2114 fatal (_("can't use supplied machine %s"), machine);
2116 abfd->arch_info = inf;
2119 if (endian != BFD_ENDIAN_UNKNOWN)
2121 struct bfd_target *xvec;
2123 xvec = (struct bfd_target *) xmalloc (sizeof (struct bfd_target));
2124 memcpy (xvec, abfd->xvec, sizeof (struct bfd_target));
2125 xvec->byteorder = endian;
2126 abfd->xvec = xvec;
2129 /* Use libopcodes to locate a suitable disassembler. */
2130 aux.disassemble_fn = disassembler (abfd);
2131 if (!aux.disassemble_fn)
2133 non_fatal (_("can't disassemble for architecture %s\n"),
2134 bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
2135 exit_status = 1;
2136 return;
2139 disasm_info.flavour = bfd_get_flavour (abfd);
2140 disasm_info.arch = bfd_get_arch (abfd);
2141 disasm_info.mach = bfd_get_mach (abfd);
2142 disasm_info.disassembler_options = disassembler_options;
2143 disasm_info.octets_per_byte = bfd_octets_per_byte (abfd);
2144 disasm_info.skip_zeroes = DEFAULT_SKIP_ZEROES;
2145 disasm_info.skip_zeroes_at_end = DEFAULT_SKIP_ZEROES_AT_END;
2146 disasm_info.disassembler_needs_relocs = FALSE;
2148 if (bfd_big_endian (abfd))
2149 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_BIG;
2150 else if (bfd_little_endian (abfd))
2151 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_LITTLE;
2152 else
2153 /* ??? Aborting here seems too drastic. We could default to big or little
2154 instead. */
2155 disasm_info.endian = BFD_ENDIAN_UNKNOWN;
2157 /* Allow the target to customize the info structure. */
2158 disassemble_init_for_target (& disasm_info);
2160 /* Pre-load the dynamic relocs if we are going
2161 to be dumping them along with the disassembly. */
2162 if (dump_dynamic_reloc_info)
2164 long relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
2166 if (relsize < 0)
2167 bfd_fatal (bfd_get_filename (abfd));
2169 if (relsize > 0)
2171 aux.dynrelbuf = (arelent **) xmalloc (relsize);
2172 aux.dynrelcount = bfd_canonicalize_dynamic_reloc (abfd,
2173 aux.dynrelbuf,
2174 dynsyms);
2175 if (aux.dynrelcount < 0)
2176 bfd_fatal (bfd_get_filename (abfd));
2178 /* Sort the relocs by address. */
2179 qsort (aux.dynrelbuf, aux.dynrelcount, sizeof (arelent *),
2180 compare_relocs);
2183 disasm_info.symtab = sorted_syms;
2184 disasm_info.symtab_size = sorted_symcount;
2186 bfd_map_over_sections (abfd, disassemble_section, & disasm_info);
2188 if (aux.dynrelbuf != NULL)
2189 free (aux.dynrelbuf);
2190 free (sorted_syms);
2193 static int
2194 load_specific_debug_section (enum dwarf_section_display_enum debug,
2195 asection *sec, void *file)
2197 struct dwarf_section *section = &debug_displays [debug].section;
2198 bfd *abfd = (bfd *) file;
2199 bfd_boolean ret;
2201 /* If it is already loaded, do nothing. */
2202 if (section->start != NULL)
2203 return 1;
2205 section->address = 0;
2206 section->size = bfd_get_section_size (sec);
2207 section->start = NULL;
2208 ret = bfd_get_full_section_contents (abfd, sec, &section->start);
2210 if (! ret)
2212 free_debug_section (debug);
2213 printf (_("\nCan't get contents for section '%s'.\n"),
2214 section->name);
2215 return 0;
2218 if (is_relocatable && debug_displays [debug].relocate)
2220 /* We want to relocate the data we've already read (and
2221 decompressed), so we store a pointer to the data in
2222 the bfd_section, and tell it that the contents are
2223 already in memory. */
2224 sec->contents = section->start;
2225 sec->flags |= SEC_IN_MEMORY;
2226 sec->size = section->size;
2228 ret = bfd_simple_get_relocated_section_contents (abfd,
2229 sec,
2230 section->start,
2231 syms) != NULL;
2233 if (! ret)
2235 free_debug_section (debug);
2236 printf (_("\nCan't get contents for section '%s'.\n"),
2237 section->name);
2238 return 0;
2242 return 1;
2246 load_debug_section (enum dwarf_section_display_enum debug, void *file)
2248 struct dwarf_section *section = &debug_displays [debug].section;
2249 bfd *abfd = (bfd *) file;
2250 asection *sec;
2252 /* If it is already loaded, do nothing. */
2253 if (section->start != NULL)
2254 return 1;
2256 /* Locate the debug section. */
2257 sec = bfd_get_section_by_name (abfd, section->uncompressed_name);
2258 if (sec != NULL)
2259 section->name = section->uncompressed_name;
2260 else
2262 sec = bfd_get_section_by_name (abfd, section->compressed_name);
2263 if (sec != NULL)
2264 section->name = section->compressed_name;
2266 if (sec == NULL)
2267 return 0;
2269 return load_specific_debug_section (debug, sec, file);
2272 void
2273 free_debug_section (enum dwarf_section_display_enum debug)
2275 struct dwarf_section *section = &debug_displays [debug].section;
2277 if (section->start == NULL)
2278 return;
2280 free ((char *) section->start);
2281 section->start = NULL;
2282 section->address = 0;
2283 section->size = 0;
2286 static void
2287 dump_dwarf_section (bfd *abfd, asection *section,
2288 void *arg ATTRIBUTE_UNUSED)
2290 const char *name = bfd_get_section_name (abfd, section);
2291 const char *match;
2292 int i;
2294 if (CONST_STRNEQ (name, ".gnu.linkonce.wi."))
2295 match = ".debug_info";
2296 else
2297 match = name;
2299 for (i = 0; i < max; i++)
2300 if ((strcmp (debug_displays [i].section.uncompressed_name, match) == 0
2301 || strcmp (debug_displays [i].section.compressed_name, match) == 0)
2302 && debug_displays [i].enabled != NULL
2303 && *debug_displays [i].enabled)
2305 struct dwarf_section *sec = &debug_displays [i].section;
2307 if (strcmp (sec->uncompressed_name, match) == 0)
2308 sec->name = sec->uncompressed_name;
2309 else
2310 sec->name = sec->compressed_name;
2311 if (load_specific_debug_section ((enum dwarf_section_display_enum) i,
2312 section, abfd))
2314 debug_displays [i].display (sec, abfd);
2316 if (i != info && i != abbrev)
2317 free_debug_section ((enum dwarf_section_display_enum) i);
2319 break;
2323 /* Dump the dwarf debugging information. */
2325 static void
2326 dump_dwarf (bfd *abfd)
2328 is_relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2330 eh_addr_size = bfd_arch_bits_per_address (abfd) / 8;
2332 if (bfd_big_endian (abfd))
2333 byte_get = byte_get_big_endian;
2334 else if (bfd_little_endian (abfd))
2335 byte_get = byte_get_little_endian;
2336 else
2337 abort ();
2339 switch (bfd_get_arch (abfd))
2341 case bfd_arch_i386:
2342 switch (bfd_get_mach (abfd))
2344 case bfd_mach_x86_64:
2345 case bfd_mach_x86_64_intel_syntax:
2346 init_dwarf_regnames_x86_64 ();
2347 break;
2349 default:
2350 init_dwarf_regnames_i386 ();
2351 break;
2353 break;
2355 default:
2356 break;
2359 bfd_map_over_sections (abfd, dump_dwarf_section, NULL);
2361 free_debug_memory ();
2364 /* Read ABFD's stabs section STABSECT_NAME, and return a pointer to
2365 it. Return NULL on failure. */
2367 static char *
2368 read_section_stabs (bfd *abfd, const char *sect_name, bfd_size_type *size_ptr)
2370 asection *stabsect;
2371 bfd_size_type size;
2372 char *contents;
2374 stabsect = bfd_get_section_by_name (abfd, sect_name);
2375 if (stabsect == NULL)
2377 printf (_("No %s section present\n\n"), sect_name);
2378 return FALSE;
2381 size = bfd_section_size (abfd, stabsect);
2382 contents = (char *) xmalloc (size);
2384 if (! bfd_get_section_contents (abfd, stabsect, contents, 0, size))
2386 non_fatal (_("reading %s section of %s failed: %s"),
2387 sect_name, bfd_get_filename (abfd),
2388 bfd_errmsg (bfd_get_error ()));
2389 exit_status = 1;
2390 free (contents);
2391 return NULL;
2394 *size_ptr = size;
2396 return contents;
2399 /* Stabs entries use a 12 byte format:
2400 4 byte string table index
2401 1 byte stab type
2402 1 byte stab other field
2403 2 byte stab desc field
2404 4 byte stab value
2405 FIXME: This will have to change for a 64 bit object format. */
2407 #define STRDXOFF (0)
2408 #define TYPEOFF (4)
2409 #define OTHEROFF (5)
2410 #define DESCOFF (6)
2411 #define VALOFF (8)
2412 #define STABSIZE (12)
2414 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
2415 using string table section STRSECT_NAME (in `strtab'). */
2417 static void
2418 print_section_stabs (bfd *abfd,
2419 const char *stabsect_name,
2420 unsigned *string_offset_ptr)
2422 int i;
2423 unsigned file_string_table_offset = 0;
2424 unsigned next_file_string_table_offset = *string_offset_ptr;
2425 bfd_byte *stabp, *stabs_end;
2427 stabp = stabs;
2428 stabs_end = stabp + stab_size;
2430 printf (_("Contents of %s section:\n\n"), stabsect_name);
2431 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
2433 /* Loop through all symbols and print them.
2435 We start the index at -1 because there is a dummy symbol on
2436 the front of stabs-in-{coff,elf} sections that supplies sizes. */
2437 for (i = -1; stabp < stabs_end; stabp += STABSIZE, i++)
2439 const char *name;
2440 unsigned long strx;
2441 unsigned char type, other;
2442 unsigned short desc;
2443 bfd_vma value;
2445 strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
2446 type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
2447 other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
2448 desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
2449 value = bfd_h_get_32 (abfd, stabp + VALOFF);
2451 printf ("\n%-6d ", i);
2452 /* Either print the stab name, or, if unnamed, print its number
2453 again (makes consistent formatting for tools like awk). */
2454 name = bfd_get_stab_name (type);
2455 if (name != NULL)
2456 printf ("%-6s", name);
2457 else if (type == N_UNDF)
2458 printf ("HdrSym");
2459 else
2460 printf ("%-6d", type);
2461 printf (" %-6d %-6d ", other, desc);
2462 bfd_printf_vma (abfd, value);
2463 printf (" %-6lu", strx);
2465 /* Symbols with type == 0 (N_UNDF) specify the length of the
2466 string table associated with this file. We use that info
2467 to know how to relocate the *next* file's string table indices. */
2468 if (type == N_UNDF)
2470 file_string_table_offset = next_file_string_table_offset;
2471 next_file_string_table_offset += value;
2473 else
2475 /* Using the (possibly updated) string table offset, print the
2476 string (if any) associated with this symbol. */
2477 if ((strx + file_string_table_offset) < stabstr_size)
2478 printf (" %s", &strtab[strx + file_string_table_offset]);
2479 else
2480 printf (" *");
2483 printf ("\n\n");
2484 *string_offset_ptr = next_file_string_table_offset;
2487 typedef struct
2489 const char * section_name;
2490 const char * string_section_name;
2491 unsigned string_offset;
2493 stab_section_names;
2495 static void
2496 find_stabs_section (bfd *abfd, asection *section, void *names)
2498 int len;
2499 stab_section_names * sought = (stab_section_names *) names;
2501 /* Check for section names for which stabsect_name is a prefix, to
2502 handle .stab.N, etc. */
2503 len = strlen (sought->section_name);
2505 /* If the prefix matches, and the files section name ends with a
2506 nul or a digit, then we match. I.e., we want either an exact
2507 match or a section followed by a number. */
2508 if (strncmp (sought->section_name, section->name, len) == 0
2509 && (section->name[len] == 0
2510 || (section->name[len] == '.' && ISDIGIT (section->name[len + 1]))))
2512 if (strtab == NULL)
2513 strtab = read_section_stabs (abfd, sought->string_section_name,
2514 &stabstr_size);
2516 if (strtab)
2518 stabs = (bfd_byte *) read_section_stabs (abfd, section->name,
2519 &stab_size);
2520 if (stabs)
2521 print_section_stabs (abfd, section->name, &sought->string_offset);
2526 static void
2527 dump_stabs_section (bfd *abfd, char *stabsect_name, char *strsect_name)
2529 stab_section_names s;
2531 s.section_name = stabsect_name;
2532 s.string_section_name = strsect_name;
2533 s.string_offset = 0;
2535 bfd_map_over_sections (abfd, find_stabs_section, & s);
2537 free (strtab);
2538 strtab = NULL;
2541 /* Dump the any sections containing stabs debugging information. */
2543 static void
2544 dump_stabs (bfd *abfd)
2546 dump_stabs_section (abfd, ".stab", ".stabstr");
2547 dump_stabs_section (abfd, ".stab.excl", ".stab.exclstr");
2548 dump_stabs_section (abfd, ".stab.index", ".stab.indexstr");
2550 /* For Darwin. */
2551 dump_stabs_section (abfd, "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr");
2553 dump_stabs_section (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
2556 static void
2557 dump_bfd_header (bfd *abfd)
2559 char *comma = "";
2561 printf (_("architecture: %s, "),
2562 bfd_printable_arch_mach (bfd_get_arch (abfd),
2563 bfd_get_mach (abfd)));
2564 printf (_("flags 0x%08x:\n"), abfd->flags & ~BFD_FLAGS_FOR_BFD_USE_MASK);
2566 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
2567 PF (HAS_RELOC, "HAS_RELOC");
2568 PF (EXEC_P, "EXEC_P");
2569 PF (HAS_LINENO, "HAS_LINENO");
2570 PF (HAS_DEBUG, "HAS_DEBUG");
2571 PF (HAS_SYMS, "HAS_SYMS");
2572 PF (HAS_LOCALS, "HAS_LOCALS");
2573 PF (DYNAMIC, "DYNAMIC");
2574 PF (WP_TEXT, "WP_TEXT");
2575 PF (D_PAGED, "D_PAGED");
2576 PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
2577 PF (HAS_LOAD_PAGE, "HAS_LOAD_PAGE");
2578 printf (_("\nstart address 0x"));
2579 bfd_printf_vma (abfd, abfd->start_address);
2580 printf ("\n");
2584 static void
2585 dump_bfd_private_header (bfd *abfd)
2587 bfd_print_private_bfd_data (abfd, stdout);
2591 /* Display a section in hexadecimal format with associated characters.
2592 Each line prefixed by the zero padded address. */
2594 static void
2595 dump_section (bfd *abfd, asection *section, void *dummy ATTRIBUTE_UNUSED)
2597 bfd_byte *data = 0;
2598 bfd_size_type datasize;
2599 bfd_size_type addr_offset;
2600 bfd_size_type start_offset;
2601 bfd_size_type stop_offset;
2602 unsigned int opb = bfd_octets_per_byte (abfd);
2603 /* Bytes per line. */
2604 const int onaline = 16;
2605 char buf[64];
2606 int count;
2607 int width;
2609 if ((section->flags & SEC_HAS_CONTENTS) == 0)
2610 return;
2612 if (! process_section_p (section))
2613 return;
2615 if ((datasize = bfd_section_size (abfd, section)) == 0)
2616 return;
2618 /* Compute the address range to display. */
2619 if (start_address == (bfd_vma) -1
2620 || start_address < section->vma)
2621 start_offset = 0;
2622 else
2623 start_offset = start_address - section->vma;
2625 if (stop_address == (bfd_vma) -1)
2626 stop_offset = datasize / opb;
2627 else
2629 if (stop_address < section->vma)
2630 stop_offset = 0;
2631 else
2632 stop_offset = stop_address - section->vma;
2634 if (stop_offset > datasize / opb)
2635 stop_offset = datasize / opb;
2638 if (start_offset >= stop_offset)
2639 return;
2641 printf (_("Contents of section %s:"), section->name);
2642 if (display_file_offsets)
2643 printf (_(" (Starting at file offset: 0x%lx)"),
2644 (unsigned long) (section->filepos + start_offset));
2645 printf ("\n");
2647 if (!bfd_get_full_section_contents (abfd, section, &data))
2649 non_fatal (_("Reading section failed"));
2650 return;
2653 width = 4;
2655 bfd_sprintf_vma (abfd, buf, start_offset + section->vma);
2656 if (strlen (buf) >= sizeof (buf))
2657 abort ();
2659 count = 0;
2660 while (buf[count] == '0' && buf[count+1] != '\0')
2661 count++;
2662 count = strlen (buf) - count;
2663 if (count > width)
2664 width = count;
2666 bfd_sprintf_vma (abfd, buf, stop_offset + section->vma - 1);
2667 if (strlen (buf) >= sizeof (buf))
2668 abort ();
2670 count = 0;
2671 while (buf[count] == '0' && buf[count+1] != '\0')
2672 count++;
2673 count = strlen (buf) - count;
2674 if (count > width)
2675 width = count;
2677 for (addr_offset = start_offset;
2678 addr_offset < stop_offset; addr_offset += onaline / opb)
2680 bfd_size_type j;
2682 bfd_sprintf_vma (abfd, buf, (addr_offset + section->vma));
2683 count = strlen (buf);
2684 if ((size_t) count >= sizeof (buf))
2685 abort ();
2687 putchar (' ');
2688 while (count < width)
2690 putchar ('0');
2691 count++;
2693 fputs (buf + count - width, stdout);
2694 putchar (' ');
2696 for (j = addr_offset * opb;
2697 j < addr_offset * opb + onaline; j++)
2699 if (j < stop_offset * opb)
2700 printf ("%02x", (unsigned) (data[j]));
2701 else
2702 printf (" ");
2703 if ((j & 3) == 3)
2704 printf (" ");
2707 printf (" ");
2708 for (j = addr_offset * opb;
2709 j < addr_offset * opb + onaline; j++)
2711 if (j >= stop_offset * opb)
2712 printf (" ");
2713 else
2714 printf ("%c", ISPRINT (data[j]) ? data[j] : '.');
2716 putchar ('\n');
2718 free (data);
2721 /* Actually display the various requested regions. */
2723 static void
2724 dump_data (bfd *abfd)
2726 bfd_map_over_sections (abfd, dump_section, NULL);
2729 /* Should perhaps share code and display with nm? */
2731 static void
2732 dump_symbols (bfd *abfd ATTRIBUTE_UNUSED, bfd_boolean dynamic)
2734 asymbol **current;
2735 long max_count;
2736 long count;
2738 if (dynamic)
2740 current = dynsyms;
2741 max_count = dynsymcount;
2742 printf ("DYNAMIC SYMBOL TABLE:\n");
2744 else
2746 current = syms;
2747 max_count = symcount;
2748 printf ("SYMBOL TABLE:\n");
2751 if (max_count == 0)
2752 printf (_("no symbols\n"));
2754 for (count = 0; count < max_count; count++)
2756 bfd *cur_bfd;
2758 if (*current == NULL)
2759 printf (_("no information for symbol number %ld\n"), count);
2761 else if ((cur_bfd = bfd_asymbol_bfd (*current)) == NULL)
2762 printf (_("could not determine the type of symbol number %ld\n"),
2763 count);
2765 else if (process_section_p ((* current)->section)
2766 && (dump_special_syms
2767 || !bfd_is_target_special_symbol (cur_bfd, *current)))
2769 const char *name = (*current)->name;
2771 if (do_demangle && name != NULL && *name != '\0')
2773 char *alloc;
2775 /* If we want to demangle the name, we demangle it
2776 here, and temporarily clobber it while calling
2777 bfd_print_symbol. FIXME: This is a gross hack. */
2778 alloc = bfd_demangle (cur_bfd, name, DMGL_ANSI | DMGL_PARAMS);
2779 if (alloc != NULL)
2780 (*current)->name = alloc;
2781 bfd_print_symbol (cur_bfd, stdout, *current,
2782 bfd_print_symbol_all);
2783 if (alloc != NULL)
2785 (*current)->name = name;
2786 free (alloc);
2789 else
2790 bfd_print_symbol (cur_bfd, stdout, *current,
2791 bfd_print_symbol_all);
2792 printf ("\n");
2795 current++;
2797 printf ("\n\n");
2800 static void
2801 dump_reloc_set (bfd *abfd, asection *sec, arelent **relpp, long relcount)
2803 arelent **p;
2804 char *last_filename, *last_functionname;
2805 unsigned int last_line;
2807 /* Get column headers lined up reasonably. */
2809 static int width;
2811 if (width == 0)
2813 char buf[30];
2815 bfd_sprintf_vma (abfd, buf, (bfd_vma) -1);
2816 width = strlen (buf) - 7;
2818 printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
2821 last_filename = NULL;
2822 last_functionname = NULL;
2823 last_line = 0;
2825 for (p = relpp; relcount && *p != NULL; p++, relcount--)
2827 arelent *q = *p;
2828 const char *filename, *functionname;
2829 unsigned int linenumber;
2830 const char *sym_name;
2831 const char *section_name;
2833 if (start_address != (bfd_vma) -1
2834 && q->address < start_address)
2835 continue;
2836 if (stop_address != (bfd_vma) -1
2837 && q->address > stop_address)
2838 continue;
2840 if (with_line_numbers
2841 && sec != NULL
2842 && bfd_find_nearest_line (abfd, sec, syms, q->address,
2843 &filename, &functionname, &linenumber))
2845 if (functionname != NULL
2846 && (last_functionname == NULL
2847 || strcmp (functionname, last_functionname) != 0))
2849 printf ("%s():\n", functionname);
2850 if (last_functionname != NULL)
2851 free (last_functionname);
2852 last_functionname = xstrdup (functionname);
2855 if (linenumber > 0
2856 && (linenumber != last_line
2857 || (filename != NULL
2858 && last_filename != NULL
2859 && filename_cmp (filename, last_filename) != 0)))
2861 printf ("%s:%u\n", filename == NULL ? "???" : filename, linenumber);
2862 last_line = linenumber;
2863 if (last_filename != NULL)
2864 free (last_filename);
2865 if (filename == NULL)
2866 last_filename = NULL;
2867 else
2868 last_filename = xstrdup (filename);
2872 if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
2874 sym_name = (*(q->sym_ptr_ptr))->name;
2875 section_name = (*(q->sym_ptr_ptr))->section->name;
2877 else
2879 sym_name = NULL;
2880 section_name = NULL;
2883 bfd_printf_vma (abfd, q->address);
2884 if (q->howto == NULL)
2885 printf (" *unknown* ");
2886 else if (q->howto->name)
2887 printf (" %-16s ", q->howto->name);
2888 else
2889 printf (" %-16d ", q->howto->type);
2891 if (sym_name)
2893 objdump_print_symname (abfd, NULL, *q->sym_ptr_ptr);
2895 else
2897 if (section_name == NULL)
2898 section_name = "*unknown*";
2899 printf ("[%s]", section_name);
2902 if (q->addend)
2904 printf ("+0x");
2905 bfd_printf_vma (abfd, q->addend);
2908 printf ("\n");
2911 if (last_filename != NULL)
2912 free (last_filename);
2913 if (last_functionname != NULL)
2914 free (last_functionname);
2917 static void
2918 dump_relocs_in_section (bfd *abfd,
2919 asection *section,
2920 void *dummy ATTRIBUTE_UNUSED)
2922 arelent **relpp;
2923 long relcount;
2924 long relsize;
2926 if ( bfd_is_abs_section (section)
2927 || bfd_is_und_section (section)
2928 || bfd_is_com_section (section)
2929 || (! process_section_p (section))
2930 || ((section->flags & SEC_RELOC) == 0))
2931 return;
2933 relsize = bfd_get_reloc_upper_bound (abfd, section);
2934 if (relsize < 0)
2935 bfd_fatal (bfd_get_filename (abfd));
2937 printf ("RELOCATION RECORDS FOR [%s]:", section->name);
2939 if (relsize == 0)
2941 printf (" (none)\n\n");
2942 return;
2945 relpp = (arelent **) xmalloc (relsize);
2946 relcount = bfd_canonicalize_reloc (abfd, section, relpp, syms);
2948 if (relcount < 0)
2949 bfd_fatal (bfd_get_filename (abfd));
2950 else if (relcount == 0)
2951 printf (" (none)\n\n");
2952 else
2954 printf ("\n");
2955 dump_reloc_set (abfd, section, relpp, relcount);
2956 printf ("\n\n");
2958 free (relpp);
2961 static void
2962 dump_relocs (bfd *abfd)
2964 bfd_map_over_sections (abfd, dump_relocs_in_section, NULL);
2967 static void
2968 dump_dynamic_relocs (bfd *abfd)
2970 long relsize;
2971 arelent **relpp;
2972 long relcount;
2974 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
2975 if (relsize < 0)
2976 bfd_fatal (bfd_get_filename (abfd));
2978 printf ("DYNAMIC RELOCATION RECORDS");
2980 if (relsize == 0)
2981 printf (" (none)\n\n");
2982 else
2984 relpp = (arelent **) xmalloc (relsize);
2985 relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
2987 if (relcount < 0)
2988 bfd_fatal (bfd_get_filename (abfd));
2989 else if (relcount == 0)
2990 printf (" (none)\n\n");
2991 else
2993 printf ("\n");
2994 dump_reloc_set (abfd, NULL, relpp, relcount);
2995 printf ("\n\n");
2997 free (relpp);
3001 /* Creates a table of paths, to search for source files. */
3003 static void
3004 add_include_path (const char *path)
3006 if (path[0] == 0)
3007 return;
3008 include_path_count++;
3009 include_paths = (const char **)
3010 xrealloc (include_paths, include_path_count * sizeof (*include_paths));
3011 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
3012 if (path[1] == ':' && path[2] == 0)
3013 path = concat (path, ".", (const char *) 0);
3014 #endif
3015 include_paths[include_path_count - 1] = path;
3018 static void
3019 adjust_addresses (bfd *abfd ATTRIBUTE_UNUSED,
3020 asection *section,
3021 void *arg)
3023 if ((section->flags & SEC_DEBUGGING) == 0)
3025 bfd_boolean *has_reloc_p = (bfd_boolean *) arg;
3026 section->vma += adjust_section_vma;
3027 if (*has_reloc_p)
3028 section->lma += adjust_section_vma;
3032 /* Dump selected contents of ABFD. */
3034 static void
3035 dump_bfd (bfd *abfd)
3037 /* If we are adjusting section VMA's, change them all now. Changing
3038 the BFD information is a hack. However, we must do it, or
3039 bfd_find_nearest_line will not do the right thing. */
3040 if (adjust_section_vma != 0)
3042 bfd_boolean has_reloc = (abfd->flags & HAS_RELOC);
3043 bfd_map_over_sections (abfd, adjust_addresses, &has_reloc);
3046 if (! dump_debugging_tags)
3047 printf (_("\n%s: file format %s\n"), bfd_get_filename (abfd),
3048 abfd->xvec->name);
3049 if (dump_ar_hdrs)
3050 print_arelt_descr (stdout, abfd, TRUE);
3051 if (dump_file_header)
3052 dump_bfd_header (abfd);
3053 if (dump_private_headers)
3054 dump_bfd_private_header (abfd);
3055 if (! dump_debugging_tags)
3056 putchar ('\n');
3057 if (dump_section_headers)
3058 dump_headers (abfd);
3060 if (dump_symtab
3061 || dump_reloc_info
3062 || disassemble
3063 || dump_debugging
3064 || dump_dwarf_section_info)
3065 syms = slurp_symtab (abfd);
3066 if (dump_dynamic_symtab || dump_dynamic_reloc_info
3067 || (disassemble && bfd_get_dynamic_symtab_upper_bound (abfd) > 0))
3068 dynsyms = slurp_dynamic_symtab (abfd);
3069 if (disassemble)
3071 synthcount = bfd_get_synthetic_symtab (abfd, symcount, syms,
3072 dynsymcount, dynsyms, &synthsyms);
3073 if (synthcount < 0)
3074 synthcount = 0;
3077 if (dump_symtab)
3078 dump_symbols (abfd, FALSE);
3079 if (dump_dynamic_symtab)
3080 dump_symbols (abfd, TRUE);
3081 if (dump_dwarf_section_info)
3082 dump_dwarf (abfd);
3083 if (dump_stab_section_info)
3084 dump_stabs (abfd);
3085 if (dump_reloc_info && ! disassemble)
3086 dump_relocs (abfd);
3087 if (dump_dynamic_reloc_info && ! disassemble)
3088 dump_dynamic_relocs (abfd);
3089 if (dump_section_contents)
3090 dump_data (abfd);
3091 if (disassemble)
3092 disassemble_data (abfd);
3094 if (dump_debugging)
3096 void *dhandle;
3098 dhandle = read_debugging_info (abfd, syms, symcount, TRUE);
3099 if (dhandle != NULL)
3101 if (!print_debugging_info (stdout, dhandle, abfd, syms,
3102 bfd_demangle,
3103 dump_debugging_tags ? TRUE : FALSE))
3105 non_fatal (_("%s: printing debugging information failed"),
3106 bfd_get_filename (abfd));
3107 exit_status = 1;
3110 /* PR 6483: If there was no STABS or IEEE debug
3111 info in the file, try DWARF instead. */
3112 else if (! dump_dwarf_section_info)
3114 dump_dwarf (abfd);
3118 if (syms)
3120 free (syms);
3121 syms = NULL;
3124 if (dynsyms)
3126 free (dynsyms);
3127 dynsyms = NULL;
3130 if (synthsyms)
3132 free (synthsyms);
3133 synthsyms = NULL;
3136 symcount = 0;
3137 dynsymcount = 0;
3138 synthcount = 0;
3141 static void
3142 display_bfd (bfd *abfd)
3144 char **matching;
3146 if (bfd_check_format_matches (abfd, bfd_object, &matching))
3148 dump_bfd (abfd);
3149 return;
3152 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
3154 nonfatal (bfd_get_filename (abfd));
3155 list_matching_formats (matching);
3156 free (matching);
3157 return;
3160 if (bfd_get_error () != bfd_error_file_not_recognized)
3162 nonfatal (bfd_get_filename (abfd));
3163 return;
3166 if (bfd_check_format_matches (abfd, bfd_core, &matching))
3168 dump_bfd (abfd);
3169 return;
3172 nonfatal (bfd_get_filename (abfd));
3174 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
3176 list_matching_formats (matching);
3177 free (matching);
3181 static void
3182 display_file (char *filename, char *target)
3184 bfd *file;
3185 bfd *arfile = NULL;
3187 if (get_file_size (filename) < 1)
3189 exit_status = 1;
3190 return;
3193 file = bfd_openr (filename, target);
3194 if (file == NULL)
3196 nonfatal (filename);
3197 return;
3200 /* Decompress sections unless dumping the section contents. */
3201 if (!dump_section_contents)
3202 file->flags |= BFD_DECOMPRESS;
3204 /* If the file is an archive, process all of its elements. */
3205 if (bfd_check_format (file, bfd_archive))
3207 bfd *last_arfile = NULL;
3209 printf (_("In archive %s:\n"), bfd_get_filename (file));
3210 for (;;)
3212 bfd_set_error (bfd_error_no_error);
3214 arfile = bfd_openr_next_archived_file (file, arfile);
3215 if (arfile == NULL)
3217 if (bfd_get_error () != bfd_error_no_more_archived_files)
3218 nonfatal (bfd_get_filename (file));
3219 break;
3222 display_bfd (arfile);
3224 if (last_arfile != NULL)
3225 bfd_close (last_arfile);
3226 last_arfile = arfile;
3229 if (last_arfile != NULL)
3230 bfd_close (last_arfile);
3232 else
3233 display_bfd (file);
3235 bfd_close (file);
3239 main (int argc, char **argv)
3241 int c;
3242 char *target = default_target;
3243 bfd_boolean seenflag = FALSE;
3245 #if defined (HAVE_SETLOCALE)
3246 #if defined (HAVE_LC_MESSAGES)
3247 setlocale (LC_MESSAGES, "");
3248 #endif
3249 setlocale (LC_CTYPE, "");
3250 #endif
3252 bindtextdomain (PACKAGE, LOCALEDIR);
3253 textdomain (PACKAGE);
3255 program_name = *argv;
3256 xmalloc_set_program_name (program_name);
3258 START_PROGRESS (program_name, 0);
3260 expandargv (&argc, &argv);
3262 bfd_init ();
3263 set_default_bfd_target ();
3265 while ((c = getopt_long (argc, argv,
3266 "pib:m:M:VvCdDlfFaHhrRtTxsSI:j:wE:zgeGW::",
3267 long_options, (int *) 0))
3268 != EOF)
3270 switch (c)
3272 case 0:
3273 break; /* We've been given a long option. */
3274 case 'm':
3275 machine = optarg;
3276 break;
3277 case 'M':
3278 if (disassembler_options)
3279 /* Ignore potential memory leak for now. */
3280 disassembler_options = concat (disassembler_options, ",",
3281 optarg, (const char *) NULL);
3282 else
3283 disassembler_options = optarg;
3284 break;
3285 case 'j':
3286 add_only (optarg);
3287 break;
3288 case 'F':
3289 display_file_offsets = TRUE;
3290 break;
3291 case 'l':
3292 with_line_numbers = TRUE;
3293 break;
3294 case 'b':
3295 target = optarg;
3296 break;
3297 case 'C':
3298 do_demangle = TRUE;
3299 if (optarg != NULL)
3301 enum demangling_styles style;
3303 style = cplus_demangle_name_to_style (optarg);
3304 if (style == unknown_demangling)
3305 fatal (_("unknown demangling style `%s'"),
3306 optarg);
3308 cplus_demangle_set_style (style);
3310 break;
3311 case 'w':
3312 wide_output = TRUE;
3313 break;
3314 case OPTION_ADJUST_VMA:
3315 adjust_section_vma = parse_vma (optarg, "--adjust-vma");
3316 break;
3317 case OPTION_START_ADDRESS:
3318 start_address = parse_vma (optarg, "--start-address");
3319 if ((stop_address != (bfd_vma) -1) && stop_address <= start_address)
3320 fatal (_("error: the start address should be before the end address"));
3321 break;
3322 case OPTION_STOP_ADDRESS:
3323 stop_address = parse_vma (optarg, "--stop-address");
3324 if ((start_address != (bfd_vma) -1) && stop_address <= start_address)
3325 fatal (_("error: the stop address should be after the start address"));
3326 break;
3327 case OPTION_PREFIX:
3328 prefix = optarg;
3329 prefix_length = strlen (prefix);
3330 /* Remove an unnecessary trailing '/' */
3331 while (IS_DIR_SEPARATOR (prefix[prefix_length - 1]))
3332 prefix_length--;
3333 break;
3334 case OPTION_PREFIX_STRIP:
3335 prefix_strip = atoi (optarg);
3336 if (prefix_strip < 0)
3337 fatal (_("error: prefix strip must be non-negative"));
3338 break;
3339 case OPTION_INSN_WIDTH:
3340 insn_width = strtoul (optarg, NULL, 0);
3341 if (insn_width <= 0)
3342 fatal (_("error: instruction width must be positive"));
3343 break;
3344 case 'E':
3345 if (strcmp (optarg, "B") == 0)
3346 endian = BFD_ENDIAN_BIG;
3347 else if (strcmp (optarg, "L") == 0)
3348 endian = BFD_ENDIAN_LITTLE;
3349 else
3351 nonfatal (_("unrecognized -E option"));
3352 usage (stderr, 1);
3354 break;
3355 case OPTION_ENDIAN:
3356 if (strncmp (optarg, "big", strlen (optarg)) == 0)
3357 endian = BFD_ENDIAN_BIG;
3358 else if (strncmp (optarg, "little", strlen (optarg)) == 0)
3359 endian = BFD_ENDIAN_LITTLE;
3360 else
3362 non_fatal (_("unrecognized --endian type `%s'"), optarg);
3363 exit_status = 1;
3364 usage (stderr, 1);
3366 break;
3368 case 'f':
3369 dump_file_header = TRUE;
3370 seenflag = TRUE;
3371 break;
3372 case 'i':
3373 formats_info = TRUE;
3374 seenflag = TRUE;
3375 break;
3376 case 'I':
3377 add_include_path (optarg);
3378 break;
3379 case 'p':
3380 dump_private_headers = TRUE;
3381 seenflag = TRUE;
3382 break;
3383 case 'x':
3384 dump_private_headers = TRUE;
3385 dump_symtab = TRUE;
3386 dump_reloc_info = TRUE;
3387 dump_file_header = TRUE;
3388 dump_ar_hdrs = TRUE;
3389 dump_section_headers = TRUE;
3390 seenflag = TRUE;
3391 break;
3392 case 't':
3393 dump_symtab = TRUE;
3394 seenflag = TRUE;
3395 break;
3396 case 'T':
3397 dump_dynamic_symtab = TRUE;
3398 seenflag = TRUE;
3399 break;
3400 case 'd':
3401 disassemble = TRUE;
3402 seenflag = TRUE;
3403 break;
3404 case 'z':
3405 disassemble_zeroes = TRUE;
3406 break;
3407 case 'D':
3408 disassemble = TRUE;
3409 disassemble_all = TRUE;
3410 seenflag = TRUE;
3411 break;
3412 case 'S':
3413 disassemble = TRUE;
3414 with_source_code = TRUE;
3415 seenflag = TRUE;
3416 break;
3417 case 'g':
3418 dump_debugging = 1;
3419 seenflag = TRUE;
3420 break;
3421 case 'e':
3422 dump_debugging = 1;
3423 dump_debugging_tags = 1;
3424 do_demangle = TRUE;
3425 seenflag = TRUE;
3426 break;
3427 case 'W':
3428 dump_dwarf_section_info = TRUE;
3429 seenflag = TRUE;
3430 if (optarg)
3431 dwarf_select_sections_by_letters (optarg);
3432 else
3433 dwarf_select_sections_all ();
3434 break;
3435 case OPTION_DWARF:
3436 dump_dwarf_section_info = TRUE;
3437 seenflag = TRUE;
3438 if (optarg)
3439 dwarf_select_sections_by_names (optarg);
3440 else
3441 dwarf_select_sections_all ();
3442 break;
3443 case 'G':
3444 dump_stab_section_info = TRUE;
3445 seenflag = TRUE;
3446 break;
3447 case 's':
3448 dump_section_contents = TRUE;
3449 seenflag = TRUE;
3450 break;
3451 case 'r':
3452 dump_reloc_info = TRUE;
3453 seenflag = TRUE;
3454 break;
3455 case 'R':
3456 dump_dynamic_reloc_info = TRUE;
3457 seenflag = TRUE;
3458 break;
3459 case 'a':
3460 dump_ar_hdrs = TRUE;
3461 seenflag = TRUE;
3462 break;
3463 case 'h':
3464 dump_section_headers = TRUE;
3465 seenflag = TRUE;
3466 break;
3467 case 'H':
3468 usage (stdout, 0);
3469 seenflag = TRUE;
3470 case 'v':
3471 case 'V':
3472 show_version = TRUE;
3473 seenflag = TRUE;
3474 break;
3476 default:
3477 usage (stderr, 1);
3481 if (show_version)
3482 print_version ("objdump");
3484 if (!seenflag)
3485 usage (stderr, 2);
3487 if (formats_info)
3488 exit_status = display_info ();
3489 else
3491 if (optind == argc)
3492 display_file ("a.out", target);
3493 else
3494 for (; optind < argc;)
3495 display_file (argv[optind++], target);
3498 free_only_list ();
3500 END_PROGRESS (program_name);
3502 return exit_status;