]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - binutils/objdump.c
* dwarf2read.c (dwarf2_build_psymtabs_hard): Move lowpc and highpc
[thirdparty/binutils-gdb.git] / binutils / objdump.c
CommitLineData
252b5132 1/* objdump.c -- dump information about an object file.
8c2bc687 2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
1b0adfe0 3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
252b5132
RH
4 Free Software Foundation, Inc.
5
b5e2a4f3 6 This file is part of GNU Binutils.
252b5132 7
b5e2a4f3
NC
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
32866df7 10 the Free Software Foundation; either version 3, or (at your option)
b5e2a4f3 11 any later version.
252b5132 12
b5e2a4f3
NC
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.
252b5132 17
b5e2a4f3
NC
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
32866df7
NC
20 Foundation, 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
22
252b5132 23
155e0d23
NC
24/* Objdump overview.
25
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.
30
31 The flow of execution is as follows:
32
33 1. Command line arguments are checked for control switches and the
34 information to be displayed is selected.
35
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.
39
50c2245b 40 3. The file's target architecture and binary file format are determined
155e0d23
NC
41 by bfd_check_format(). If they are recognised, then dump_bfd() is
42 called.
43
50c2245b
KH
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
aaad4cf3 46 a disassembly has been requested.
155e0d23
NC
47
48 When disassembling the code loops through blocks of instructions bounded
50c2245b 49 by symbols, calling disassemble_bytes() on each block. The actual
155e0d23
NC
50 disassembling is done by the libopcodes library, via a function pointer
51 supplied by the disassembler() function. */
52
3db64b00 53#include "sysdep.h"
252b5132 54#include "bfd.h"
2dc4cec1 55#include "elf-bfd.h"
252b5132
RH
56#include "progress.h"
57#include "bucomm.h"
365544c3 58#include "dwarf.h"
d7a283d4 59#include "getopt.h"
3882b010 60#include "safe-ctype.h"
252b5132
RH
61#include "dis-asm.h"
62#include "libiberty.h"
63#include "demangle.h"
0dafdf3f 64#include "filenames.h"
252b5132
RH
65#include "debug.h"
66#include "budbg.h"
67
e8f5eee4
NC
68#ifdef HAVE_MMAP
69#include <sys/mman.h>
70#endif
71
b1364e8f
DS
72#include <sys/stat.h>
73
252b5132
RH
74/* Internal headers for the ELF .stab-dump code - sorry. */
75#define BYTES_IN_WORD 32
76#include "aout/aout64.h"
77
75cd796a
ILT
78/* Exit status. */
79static int exit_status = 0;
80
98a91d6a 81static char *default_target = NULL; /* Default at runtime. */
252b5132 82
3b9ad1cc
AM
83/* The following variables are set based on arguments passed on the
84 command line. */
98a91d6a 85static int show_version = 0; /* Show the version number. */
252b5132
RH
86static int dump_section_contents; /* -s */
87static int dump_section_headers; /* -h */
b34976b6 88static bfd_boolean dump_file_header; /* -f */
252b5132
RH
89static int dump_symtab; /* -t */
90static int dump_dynamic_symtab; /* -T */
91static int dump_reloc_info; /* -r */
92static int dump_dynamic_reloc_info; /* -R */
93static int dump_ar_hdrs; /* -a */
94static int dump_private_headers; /* -p */
95static int prefix_addresses; /* --prefix-addresses */
96static int with_line_numbers; /* -l */
b34976b6 97static bfd_boolean with_source_code; /* -S */
252b5132 98static int show_raw_insn; /* --show-raw-insn */
365544c3 99static int dump_dwarf_section_info; /* --dwarf */
252b5132
RH
100static int dump_stab_section_info; /* --stabs */
101static int do_demangle; /* -C, --demangle */
b34976b6
AM
102static bfd_boolean disassemble; /* -d */
103static bfd_boolean disassemble_all; /* -D */
252b5132 104static int disassemble_zeroes; /* --disassemble-zeroes */
b34976b6 105static bfd_boolean formats_info; /* -i */
252b5132
RH
106static int wide_output; /* -w */
107static bfd_vma start_address = (bfd_vma) -1; /* --start-address */
108static bfd_vma stop_address = (bfd_vma) -1; /* --stop-address */
109static int dump_debugging; /* --debugging */
51cdc6e0 110static int dump_debugging_tags; /* --debugging-tags */
3c9458e9 111static int dump_special_syms = 0; /* --special-syms */
252b5132 112static bfd_vma adjust_section_vma = 0; /* --adjust-vma */
f1563258 113static int file_start_context = 0; /* --file-start-context */
98ec6e72 114static bfd_boolean display_file_offsets;/* -F */
0dafdf3f
L
115static const char *prefix; /* --prefix */
116static int prefix_strip; /* --prefix-strip */
117static size_t prefix_length;
252b5132 118
155e0d23
NC
119/* Pointer to an array of section names provided by
120 one or more "-j secname" command line options. */
121static char **only;
122/* The total number of slots in the only[] array. */
123static size_t only_size = 0;
124/* The number of occupied slots in the only[] array. */
125static size_t only_used = 0;
126
43ac9881
AM
127/* Variables for handling include file path table. */
128static const char **include_paths;
129static int include_path_count;
130
3b9ad1cc
AM
131/* Extra info to pass to the section disassembler and address printing
132 function. */
026df7c5
NC
133struct objdump_disasm_info
134{
155e0d23
NC
135 bfd * abfd;
136 asection * sec;
137 bfd_boolean require_sec;
138 arelent ** dynrelbuf;
139 long dynrelcount;
140 disassembler_ftype disassemble_fn;
ce04548a 141 arelent * reloc;
252b5132
RH
142};
143
144/* Architecture to disassemble for, or default if NULL. */
d3ba0551 145static char *machine = NULL;
252b5132 146
dd92f639 147/* Target specific options to the disassembler. */
d3ba0551 148static char *disassembler_options = NULL;
dd92f639 149
252b5132
RH
150/* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */
151static enum bfd_endian endian = BFD_ENDIAN_UNKNOWN;
152
153/* The symbol table. */
154static asymbol **syms;
155
156/* Number of symbols in `syms'. */
157static long symcount = 0;
158
159/* The sorted symbol table. */
160static asymbol **sorted_syms;
161
162/* Number of symbols in `sorted_syms'. */
163static long sorted_symcount = 0;
164
165/* The dynamic symbol table. */
166static asymbol **dynsyms;
167
4c45e5c9
JJ
168/* The synthetic symbol table. */
169static asymbol *synthsyms;
170static long synthcount = 0;
171
252b5132
RH
172/* Number of symbols in `dynsyms'. */
173static long dynsymcount = 0;
174
98a91d6a
NC
175static bfd_byte *stabs;
176static bfd_size_type stab_size;
177
178static char *strtab;
179static bfd_size_type stabstr_size;
41e92641
NC
180
181static bfd_boolean is_relocatable = FALSE;
252b5132
RH
182\f
183static void
46dca2e0 184usage (FILE *stream, int status)
252b5132 185{
8b53311e
NC
186 fprintf (stream, _("Usage: %s <option(s)> <file(s)>\n"), program_name);
187 fprintf (stream, _(" Display information from object <file(s)>.\n"));
188 fprintf (stream, _(" At least one of the following switches must be given:\n"));
252b5132 189 fprintf (stream, _("\
86d65c94
MK
190 -a, --archive-headers Display archive header information\n\
191 -f, --file-headers Display the contents of the overall file header\n\
192 -p, --private-headers Display object format specific file header contents\n\
193 -h, --[section-]headers Display the contents of the section headers\n\
194 -x, --all-headers Display the contents of all headers\n\
195 -d, --disassemble Display assembler contents of executable sections\n\
196 -D, --disassemble-all Display assembler contents of all sections\n\
197 -S, --source Intermix source code with disassembly\n\
198 -s, --full-contents Display the full contents of all sections requested\n\
199 -g, --debugging Display debug information in object file\n\
51cdc6e0 200 -e, --debugging-tags Display debug information using ctags style\n\
86d65c94 201 -G, --stabs Display (in raw form) any STABS info in the file\n\
365544c3 202 -W, --dwarf Display DWARF info in the file\n\
86d65c94
MK
203 -t, --syms Display the contents of the symbol table(s)\n\
204 -T, --dynamic-syms Display the contents of the dynamic symbol table\n\
205 -r, --reloc Display the relocation entries in the file\n\
206 -R, --dynamic-reloc Display the dynamic relocation entries in the file\n\
07012eee 207 @<file> Read options from <file>\n\
8b53311e 208 -v, --version Display this program's version number\n\
86d65c94
MK
209 -i, --info List object formats and architectures supported\n\
210 -H, --help Display this information\n\
1dada9c5
NC
211"));
212 if (status != 2)
213 {
214 fprintf (stream, _("\n The following switches are optional:\n"));
215 fprintf (stream, _("\
86d65c94
MK
216 -b, --target=BFDNAME Specify the target object format as BFDNAME\n\
217 -m, --architecture=MACHINE Specify the target architecture as MACHINE\n\
218 -j, --section=NAME Only display information for section NAME\n\
219 -M, --disassembler-options=OPT Pass text OPT on to the disassembler\n\
1dada9c5
NC
220 -EB --endian=big Assume big endian format when disassembling\n\
221 -EL --endian=little Assume little endian format when disassembling\n\
f1563258 222 --file-start-context Include context from start of file (with -S)\n\
43ac9881 223 -I, --include=DIR Add DIR to search list for source files\n\
86d65c94 224 -l, --line-numbers Include line numbers and filenames in output\n\
98ec6e72 225 -F, --file-offsets Include file offsets when displaying information\n\
28c309a2 226 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n\
f0c8c24a
NC
227 The STYLE, if specified, can be `auto', `gnu',\n\
228 `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
229 or `gnat'\n\
86d65c94
MK
230 -w, --wide Format output for more than 80 columns\n\
231 -z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling\n\
f0c8c24a
NC
232 --start-address=ADDR Only process data whose address is >= ADDR\n\
233 --stop-address=ADDR Only process data whose address is <= ADDR\n\
1dada9c5
NC
234 --prefix-addresses Print complete address alongside disassembly\n\
235 --[no-]show-raw-insn Display hex alongside symbolic disassembly\n\
86d65c94 236 --adjust-vma=OFFSET Add OFFSET to all displayed section addresses\n\
3c9458e9 237 --special-syms Include special symbols in symbol dumps\n\
0dafdf3f
L
238 --prefix=PREFIX Add PREFIX to absolute paths for -S\n\
239 --prefix-strip=LEVEL Strip initial directory names for -S\n\
1dada9c5
NC
240\n"));
241 list_supported_targets (program_name, stream);
2f83960e 242 list_supported_architectures (program_name, stream);
86d65c94 243
94470b23 244 disassembler_usage (stream);
1dada9c5 245 }
92f01d61 246 if (REPORT_BUGS_TO[0] && status == 0)
86d65c94 247 fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
252b5132
RH
248 exit (status);
249}
250
251/* 150 isn't special; it's just an arbitrary non-ASCII char value. */
46dca2e0
NC
252enum option_values
253 {
254 OPTION_ENDIAN=150,
255 OPTION_START_ADDRESS,
256 OPTION_STOP_ADDRESS,
0dafdf3f
L
257 OPTION_PREFIX,
258 OPTION_PREFIX_STRIP,
46dca2e0
NC
259 OPTION_ADJUST_VMA
260 };
252b5132
RH
261
262static struct option long_options[]=
263{
264 {"adjust-vma", required_argument, NULL, OPTION_ADJUST_VMA},
265 {"all-headers", no_argument, NULL, 'x'},
266 {"private-headers", no_argument, NULL, 'p'},
267 {"architecture", required_argument, NULL, 'm'},
268 {"archive-headers", no_argument, NULL, 'a'},
1dada9c5 269 {"debugging", no_argument, NULL, 'g'},
51cdc6e0 270 {"debugging-tags", no_argument, NULL, 'e'},
28c309a2 271 {"demangle", optional_argument, NULL, 'C'},
252b5132
RH
272 {"disassemble", no_argument, NULL, 'd'},
273 {"disassemble-all", no_argument, NULL, 'D'},
dd92f639 274 {"disassembler-options", required_argument, NULL, 'M'},
1dada9c5 275 {"disassemble-zeroes", no_argument, NULL, 'z'},
252b5132
RH
276 {"dynamic-reloc", no_argument, NULL, 'R'},
277 {"dynamic-syms", no_argument, NULL, 'T'},
278 {"endian", required_argument, NULL, OPTION_ENDIAN},
279 {"file-headers", no_argument, NULL, 'f'},
98ec6e72 280 {"file-offsets", no_argument, NULL, 'F'},
f1563258 281 {"file-start-context", no_argument, &file_start_context, 1},
252b5132
RH
282 {"full-contents", no_argument, NULL, 's'},
283 {"headers", no_argument, NULL, 'h'},
284 {"help", no_argument, NULL, 'H'},
285 {"info", no_argument, NULL, 'i'},
286 {"line-numbers", no_argument, NULL, 'l'},
287 {"no-show-raw-insn", no_argument, &show_raw_insn, -1},
288 {"prefix-addresses", no_argument, &prefix_addresses, 1},
289 {"reloc", no_argument, NULL, 'r'},
290 {"section", required_argument, NULL, 'j'},
291 {"section-headers", no_argument, NULL, 'h'},
292 {"show-raw-insn", no_argument, &show_raw_insn, 1},
293 {"source", no_argument, NULL, 'S'},
3c9458e9 294 {"special-syms", no_argument, &dump_special_syms, 1},
43ac9881 295 {"include", required_argument, NULL, 'I'},
365544c3 296 {"dwarf", no_argument, NULL, 'W'},
1dada9c5 297 {"stabs", no_argument, NULL, 'G'},
252b5132
RH
298 {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
299 {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
300 {"syms", no_argument, NULL, 't'},
301 {"target", required_argument, NULL, 'b'},
1dada9c5
NC
302 {"version", no_argument, NULL, 'V'},
303 {"wide", no_argument, NULL, 'w'},
0dafdf3f
L
304 {"prefix", required_argument, NULL, OPTION_PREFIX},
305 {"prefix-strip", required_argument, NULL, OPTION_PREFIX_STRIP},
252b5132
RH
306 {0, no_argument, 0, 0}
307};
308\f
309static void
46dca2e0 310nonfatal (const char *msg)
75cd796a
ILT
311{
312 bfd_nonfatal (msg);
313 exit_status = 1;
314}
315\f
316static void
ebe372c1 317dump_section_header (bfd *abfd, asection *section,
46dca2e0 318 void *ignored ATTRIBUTE_UNUSED)
252b5132
RH
319{
320 char *comma = "";
f6af82bd 321 unsigned int opb = bfd_octets_per_byte (abfd);
252b5132 322
3bee8bcd
L
323 /* Ignore linker created section. See elfNN_ia64_object_p in
324 bfd/elfxx-ia64.c. */
325 if (section->flags & SEC_LINKER_CREATED)
326 return;
327
252b5132
RH
328 printf ("%3d %-13s %08lx ", section->index,
329 bfd_get_section_name (abfd, section),
940b2b78 330 (unsigned long) bfd_section_size (abfd, section) / opb);
d8180c76 331 bfd_printf_vma (abfd, bfd_get_section_vma (abfd, section));
252b5132 332 printf (" ");
d8180c76 333 bfd_printf_vma (abfd, section->lma);
e59b4dfb 334 printf (" %08lx 2**%u", (unsigned long) section->filepos,
252b5132
RH
335 bfd_get_section_alignment (abfd, section));
336 if (! wide_output)
337 printf ("\n ");
338 printf (" ");
339
340#define PF(x, y) \
341 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
342
343 PF (SEC_HAS_CONTENTS, "CONTENTS");
344 PF (SEC_ALLOC, "ALLOC");
345 PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
252b5132
RH
346 PF (SEC_LOAD, "LOAD");
347 PF (SEC_RELOC, "RELOC");
252b5132
RH
348 PF (SEC_READONLY, "READONLY");
349 PF (SEC_CODE, "CODE");
350 PF (SEC_DATA, "DATA");
351 PF (SEC_ROM, "ROM");
352 PF (SEC_DEBUGGING, "DEBUGGING");
353 PF (SEC_NEVER_LOAD, "NEVER_LOAD");
354 PF (SEC_EXCLUDE, "EXCLUDE");
355 PF (SEC_SORT_ENTRIES, "SORT_ENTRIES");
ebe372c1
L
356 if (bfd_get_arch (abfd) == bfd_arch_tic54x)
357 {
358 PF (SEC_TIC54X_BLOCK, "BLOCK");
359 PF (SEC_TIC54X_CLINK, "CLINK");
360 }
24c411ed 361 PF (SEC_SMALL_DATA, "SMALL_DATA");
ebe372c1
L
362 if (bfd_get_flavour (abfd) == bfd_target_coff_flavour)
363 PF (SEC_COFF_SHARED, "SHARED");
13ae64f3 364 PF (SEC_THREAD_LOCAL, "THREAD_LOCAL");
64c1196b 365 PF (SEC_GROUP, "GROUP");
252b5132
RH
366
367 if ((section->flags & SEC_LINK_ONCE) != 0)
368 {
369 const char *ls;
082b7297 370 struct coff_comdat_info *comdat;
252b5132
RH
371
372 switch (section->flags & SEC_LINK_DUPLICATES)
373 {
374 default:
375 abort ();
376 case SEC_LINK_DUPLICATES_DISCARD:
377 ls = "LINK_ONCE_DISCARD";
378 break;
379 case SEC_LINK_DUPLICATES_ONE_ONLY:
380 ls = "LINK_ONCE_ONE_ONLY";
381 break;
382 case SEC_LINK_DUPLICATES_SAME_SIZE:
383 ls = "LINK_ONCE_SAME_SIZE";
384 break;
385 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
386 ls = "LINK_ONCE_SAME_CONTENTS";
387 break;
388 }
389 printf ("%s%s", comma, ls);
deecf979 390
082b7297
L
391 comdat = bfd_coff_get_comdat_section (abfd, section);
392 if (comdat != NULL)
393 printf (" (COMDAT %s %ld)", comdat->name, comdat->symbol);
deecf979 394
252b5132
RH
395 comma = ", ";
396 }
397
398 printf ("\n");
399#undef PF
400}
401
402static void
46dca2e0 403dump_headers (bfd *abfd)
252b5132
RH
404{
405 printf (_("Sections:\n"));
8bea4d5c 406
252b5132 407#ifndef BFD64
8bea4d5c 408 printf (_("Idx Name Size VMA LMA File off Algn"));
252b5132 409#else
21611032
TS
410 /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */
411 if (bfd_get_arch_size (abfd) == 32)
412 printf (_("Idx Name Size VMA LMA File off Algn"));
413 else
414 printf (_("Idx Name Size VMA LMA File off Algn"));
252b5132 415#endif
8bea4d5c
ILT
416
417 if (wide_output)
418 printf (_(" Flags"));
026df7c5
NC
419 if (abfd->flags & HAS_LOAD_PAGE)
420 printf (_(" Pg"));
8bea4d5c
ILT
421 printf ("\n");
422
46dca2e0 423 bfd_map_over_sections (abfd, dump_section_header, NULL);
252b5132
RH
424}
425\f
426static asymbol **
46dca2e0 427slurp_symtab (bfd *abfd)
252b5132 428{
d3ba0551 429 asymbol **sy = NULL;
252b5132
RH
430 long storage;
431
432 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
433 {
252b5132
RH
434 symcount = 0;
435 return NULL;
436 }
437
438 storage = bfd_get_symtab_upper_bound (abfd);
439 if (storage < 0)
440 bfd_fatal (bfd_get_filename (abfd));
252b5132 441 if (storage)
d3ba0551 442 sy = xmalloc (storage);
28b18af1 443
252b5132
RH
444 symcount = bfd_canonicalize_symtab (abfd, sy);
445 if (symcount < 0)
446 bfd_fatal (bfd_get_filename (abfd));
252b5132
RH
447 return sy;
448}
449
450/* Read in the dynamic symbols. */
451
452static asymbol **
46dca2e0 453slurp_dynamic_symtab (bfd *abfd)
252b5132 454{
d3ba0551 455 asymbol **sy = NULL;
252b5132
RH
456 long storage;
457
458 storage = bfd_get_dynamic_symtab_upper_bound (abfd);
459 if (storage < 0)
460 {
461 if (!(bfd_get_file_flags (abfd) & DYNAMIC))
462 {
37cc8ec1 463 non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd));
252b5132
RH
464 dynsymcount = 0;
465 return NULL;
466 }
467
468 bfd_fatal (bfd_get_filename (abfd));
469 }
252b5132 470 if (storage)
d3ba0551 471 sy = xmalloc (storage);
28b18af1 472
252b5132
RH
473 dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
474 if (dynsymcount < 0)
475 bfd_fatal (bfd_get_filename (abfd));
252b5132
RH
476 return sy;
477}
478
479/* Filter out (in place) symbols that are useless for disassembly.
480 COUNT is the number of elements in SYMBOLS.
0af11b59 481 Return the number of useful symbols. */
252b5132
RH
482
483static long
46dca2e0 484remove_useless_symbols (asymbol **symbols, long count)
252b5132 485{
46dca2e0 486 asymbol **in_ptr = symbols, **out_ptr = symbols;
252b5132
RH
487
488 while (--count >= 0)
489 {
490 asymbol *sym = *in_ptr++;
491
492 if (sym->name == NULL || sym->name[0] == '\0')
493 continue;
180e47e2 494 if (sym->flags & (BSF_DEBUGGING | BSF_SECTION_SYM))
252b5132
RH
495 continue;
496 if (bfd_is_und_section (sym->section)
497 || bfd_is_com_section (sym->section))
498 continue;
499
500 *out_ptr++ = sym;
501 }
502 return out_ptr - symbols;
503}
504
505/* Sort symbols into value order. */
506
0af11b59 507static int
46dca2e0 508compare_symbols (const void *ap, const void *bp)
252b5132 509{
46dca2e0
NC
510 const asymbol *a = * (const asymbol **) ap;
511 const asymbol *b = * (const asymbol **) bp;
512 const char *an;
513 const char *bn;
514 size_t anl;
515 size_t bnl;
516 bfd_boolean af;
517 bfd_boolean bf;
518 flagword aflags;
519 flagword bflags;
252b5132
RH
520
521 if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
522 return 1;
523 else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
524 return -1;
525
526 if (a->section > b->section)
527 return 1;
528 else if (a->section < b->section)
529 return -1;
530
531 an = bfd_asymbol_name (a);
532 bn = bfd_asymbol_name (b);
533 anl = strlen (an);
534 bnl = strlen (bn);
535
536 /* The symbols gnu_compiled and gcc2_compiled convey no real
537 information, so put them after other symbols with the same value. */
252b5132
RH
538 af = (strstr (an, "gnu_compiled") != NULL
539 || strstr (an, "gcc2_compiled") != NULL);
540 bf = (strstr (bn, "gnu_compiled") != NULL
541 || strstr (bn, "gcc2_compiled") != NULL);
542
543 if (af && ! bf)
544 return 1;
545 if (! af && bf)
546 return -1;
547
548 /* We use a heuristic for the file name, to try to sort it after
549 more useful symbols. It may not work on non Unix systems, but it
550 doesn't really matter; the only difference is precisely which
551 symbol names get printed. */
552
553#define file_symbol(s, sn, snl) \
554 (((s)->flags & BSF_FILE) != 0 \
555 || ((sn)[(snl) - 2] == '.' \
556 && ((sn)[(snl) - 1] == 'o' \
557 || (sn)[(snl) - 1] == 'a')))
558
559 af = file_symbol (a, an, anl);
560 bf = file_symbol (b, bn, bnl);
561
562 if (af && ! bf)
563 return 1;
564 if (! af && bf)
565 return -1;
566
567 /* Try to sort global symbols before local symbols before function
568 symbols before debugging symbols. */
569
570 aflags = a->flags;
571 bflags = b->flags;
572
573 if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING))
574 {
575 if ((aflags & BSF_DEBUGGING) != 0)
576 return 1;
577 else
578 return -1;
579 }
580 if ((aflags & BSF_FUNCTION) != (bflags & BSF_FUNCTION))
581 {
582 if ((aflags & BSF_FUNCTION) != 0)
583 return -1;
584 else
585 return 1;
586 }
587 if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL))
588 {
589 if ((aflags & BSF_LOCAL) != 0)
590 return 1;
591 else
592 return -1;
593 }
594 if ((aflags & BSF_GLOBAL) != (bflags & BSF_GLOBAL))
595 {
596 if ((aflags & BSF_GLOBAL) != 0)
597 return -1;
598 else
599 return 1;
600 }
601
602 /* Symbols that start with '.' might be section names, so sort them
603 after symbols that don't start with '.'. */
604 if (an[0] == '.' && bn[0] != '.')
605 return 1;
606 if (an[0] != '.' && bn[0] == '.')
607 return -1;
608
609 /* Finally, if we can't distinguish them in any other way, try to
610 get consistent results by sorting the symbols by name. */
611 return strcmp (an, bn);
612}
613
614/* Sort relocs into address order. */
615
616static int
46dca2e0 617compare_relocs (const void *ap, const void *bp)
252b5132 618{
46dca2e0
NC
619 const arelent *a = * (const arelent **) ap;
620 const arelent *b = * (const arelent **) bp;
252b5132
RH
621
622 if (a->address > b->address)
623 return 1;
624 else if (a->address < b->address)
625 return -1;
626
627 /* So that associated relocations tied to the same address show up
628 in the correct order, we don't do any further sorting. */
629 if (a > b)
630 return 1;
631 else if (a < b)
632 return -1;
633 else
634 return 0;
635}
636
155e0d23
NC
637/* Print an address (VMA) to the output stream in INFO.
638 If SKIP_ZEROES is TRUE, omit leading zeroes. */
252b5132
RH
639
640static void
46dca2e0
NC
641objdump_print_value (bfd_vma vma, struct disassemble_info *info,
642 bfd_boolean skip_zeroes)
252b5132
RH
643{
644 char buf[30];
645 char *p;
3b9ad1cc 646 struct objdump_disasm_info *aux;
252b5132 647
3b9ad1cc 648 aux = (struct objdump_disasm_info *) info->application_data;
d8180c76 649 bfd_sprintf_vma (aux->abfd, buf, vma);
252b5132
RH
650 if (! skip_zeroes)
651 p = buf;
652 else
653 {
654 for (p = buf; *p == '0'; ++p)
655 ;
656 if (*p == '\0')
657 --p;
658 }
659 (*info->fprintf_func) (info->stream, "%s", p);
660}
661
662/* Print the name of a symbol. */
663
664static void
46dca2e0
NC
665objdump_print_symname (bfd *abfd, struct disassemble_info *info,
666 asymbol *sym)
252b5132
RH
667{
668 char *alloc;
669 const char *name;
252b5132
RH
670
671 alloc = NULL;
672 name = bfd_asymbol_name (sym);
a6637ec0 673 if (do_demangle && name[0] != '\0')
252b5132
RH
674 {
675 /* Demangle the name. */
ed180cc5
AM
676 alloc = bfd_demangle (abfd, name, DMGL_ANSI | DMGL_PARAMS);
677 if (alloc != NULL)
678 name = alloc;
252b5132
RH
679 }
680
681 if (info != NULL)
a6637ec0 682 (*info->fprintf_func) (info->stream, "%s", name);
252b5132 683 else
a6637ec0 684 printf ("%s", name);
252b5132
RH
685
686 if (alloc != NULL)
687 free (alloc);
688}
689
22a398e1
NC
690/* Locate a symbol given a bfd and a section (from INFO->application_data),
691 and a VMA. If INFO->application_data->require_sec is TRUE, then always
692 require the symbol to be in the section. Returns NULL if there is no
693 suitable symbol. If PLACE is not NULL, then *PLACE is set to the index
694 of the symbol in sorted_syms. */
252b5132
RH
695
696static asymbol *
3b9ad1cc
AM
697find_symbol_for_address (bfd_vma vma,
698 struct disassemble_info *info,
699 long *place)
252b5132
RH
700{
701 /* @@ Would it speed things up to cache the last two symbols returned,
702 and maybe their address ranges? For many processors, only one memory
703 operand can be present at a time, so the 2-entry cache wouldn't be
704 constantly churned by code doing heavy memory accesses. */
705
706 /* Indices in `sorted_syms'. */
707 long min = 0;
708 long max = sorted_symcount;
709 long thisplace;
3b9ad1cc
AM
710 struct objdump_disasm_info *aux;
711 bfd *abfd;
712 asection *sec;
713 unsigned int opb;
e39ff52a 714 bfd_boolean want_section;
252b5132
RH
715
716 if (sorted_symcount < 1)
717 return NULL;
718
3b9ad1cc
AM
719 aux = (struct objdump_disasm_info *) info->application_data;
720 abfd = aux->abfd;
721 sec = aux->sec;
722 opb = bfd_octets_per_byte (abfd);
723
252b5132
RH
724 /* Perform a binary search looking for the closest symbol to the
725 required value. We are searching the range (min, max]. */
726 while (min + 1 < max)
727 {
728 asymbol *sym;
729
730 thisplace = (max + min) / 2;
731 sym = sorted_syms[thisplace];
732
733 if (bfd_asymbol_value (sym) > vma)
734 max = thisplace;
735 else if (bfd_asymbol_value (sym) < vma)
736 min = thisplace;
737 else
738 {
739 min = thisplace;
740 break;
741 }
742 }
743
744 /* The symbol we want is now in min, the low end of the range we
745 were searching. If there are several symbols with the same
746 value, we want the first one. */
747 thisplace = min;
748 while (thisplace > 0
749 && (bfd_asymbol_value (sorted_syms[thisplace])
750 == bfd_asymbol_value (sorted_syms[thisplace - 1])))
751 --thisplace;
752
2b4590fb
AM
753 /* Prefer a symbol in the current section if we have multple symbols
754 with the same value, as can occur with overlays or zero size
755 sections. */
756 min = thisplace;
757 while (min < max
758 && (bfd_asymbol_value (sorted_syms[min])
759 == bfd_asymbol_value (sorted_syms[thisplace])))
760 {
761 if (sorted_syms[min]->section == sec
762 && info->symbol_is_valid (sorted_syms[min], info))
763 {
764 thisplace = min;
765
766 if (place != NULL)
767 *place = thisplace;
768
769 return sorted_syms[thisplace];
770 }
771 ++min;
772 }
773
1049f94e 774 /* If the file is relocatable, and the symbol could be from this
252b5132
RH
775 section, prefer a symbol from this section over symbols from
776 others, even if the other symbol's value might be closer.
0af11b59 777
252b5132
RH
778 Note that this may be wrong for some symbol references if the
779 sections have overlapping memory ranges, but in that case there's
780 no way to tell what's desired without looking at the relocation
e39ff52a
PB
781 table.
782
783 Also give the target a chance to reject symbols. */
784 want_section = (aux->require_sec
785 || ((abfd->flags & HAS_RELOC) != 0
786 && vma >= bfd_get_section_vma (abfd, sec)
787 && vma < (bfd_get_section_vma (abfd, sec)
788 + bfd_section_size (abfd, sec) / opb)));
789 if ((sorted_syms[thisplace]->section != sec && want_section)
790 || !info->symbol_is_valid (sorted_syms[thisplace], info))
252b5132
RH
791 {
792 long i;
2b4590fb 793 long newplace = sorted_symcount;
98a91d6a 794
2b4590fb 795 for (i = min - 1; i >= 0; i--)
252b5132 796 {
e39ff52a
PB
797 if ((sorted_syms[i]->section == sec || !want_section)
798 && info->symbol_is_valid (sorted_syms[i], info))
252b5132 799 {
e39ff52a
PB
800 if (newplace == sorted_symcount)
801 newplace = i;
802
803 if (bfd_asymbol_value (sorted_syms[i])
804 != bfd_asymbol_value (sorted_syms[newplace]))
805 break;
806
807 /* Remember this symbol and keep searching until we reach
808 an earlier address. */
809 newplace = i;
252b5132
RH
810 }
811 }
812
e39ff52a
PB
813 if (newplace != sorted_symcount)
814 thisplace = newplace;
815 else
252b5132
RH
816 {
817 /* We didn't find a good symbol with a smaller value.
818 Look for one with a larger value. */
819 for (i = thisplace + 1; i < sorted_symcount; i++)
820 {
e39ff52a
PB
821 if ((sorted_syms[i]->section == sec || !want_section)
822 && info->symbol_is_valid (sorted_syms[i], info))
252b5132
RH
823 {
824 thisplace = i;
825 break;
826 }
827 }
828 }
829
e39ff52a
PB
830 if ((sorted_syms[thisplace]->section != sec && want_section)
831 || !info->symbol_is_valid (sorted_syms[thisplace], info))
22a398e1
NC
832 /* There is no suitable symbol. */
833 return NULL;
834 }
835
252b5132
RH
836 if (place != NULL)
837 *place = thisplace;
838
839 return sorted_syms[thisplace];
840}
841
155e0d23 842/* Print an address and the offset to the nearest symbol. */
252b5132
RH
843
844static void
46dca2e0
NC
845objdump_print_addr_with_sym (bfd *abfd, asection *sec, asymbol *sym,
846 bfd_vma vma, struct disassemble_info *info,
847 bfd_boolean skip_zeroes)
252b5132
RH
848{
849 objdump_print_value (vma, info, skip_zeroes);
850
851 if (sym == NULL)
852 {
853 bfd_vma secaddr;
854
855 (*info->fprintf_func) (info->stream, " <%s",
856 bfd_get_section_name (abfd, sec));
857 secaddr = bfd_get_section_vma (abfd, sec);
858 if (vma < secaddr)
859 {
860 (*info->fprintf_func) (info->stream, "-0x");
b34976b6 861 objdump_print_value (secaddr - vma, info, TRUE);
252b5132
RH
862 }
863 else if (vma > secaddr)
864 {
865 (*info->fprintf_func) (info->stream, "+0x");
b34976b6 866 objdump_print_value (vma - secaddr, info, TRUE);
252b5132
RH
867 }
868 (*info->fprintf_func) (info->stream, ">");
869 }
870 else
871 {
872 (*info->fprintf_func) (info->stream, " <");
873 objdump_print_symname (abfd, info, sym);
874 if (bfd_asymbol_value (sym) > vma)
875 {
876 (*info->fprintf_func) (info->stream, "-0x");
b34976b6 877 objdump_print_value (bfd_asymbol_value (sym) - vma, info, TRUE);
252b5132
RH
878 }
879 else if (vma > bfd_asymbol_value (sym))
880 {
881 (*info->fprintf_func) (info->stream, "+0x");
b34976b6 882 objdump_print_value (vma - bfd_asymbol_value (sym), info, TRUE);
252b5132
RH
883 }
884 (*info->fprintf_func) (info->stream, ">");
885 }
98ec6e72
NC
886
887 if (display_file_offsets)
32760852 888 info->fprintf_func (info->stream, _(" (File Offset: 0x%lx)"),
98ec6e72 889 (long int)(sec->filepos + (vma - sec->vma)));
252b5132
RH
890}
891
155e0d23
NC
892/* Print an address (VMA), symbolically if possible.
893 If SKIP_ZEROES is TRUE, don't output leading zeroes. */
252b5132
RH
894
895static void
3b9ad1cc
AM
896objdump_print_addr (bfd_vma vma,
897 struct disassemble_info *info,
46dca2e0 898 bfd_boolean skip_zeroes)
252b5132 899{
3b9ad1cc 900 struct objdump_disasm_info *aux;
d253b654 901 asymbol *sym = NULL;
ce04548a 902 bfd_boolean skip_find = FALSE;
252b5132 903
32760852
NC
904 aux = (struct objdump_disasm_info *) info->application_data;
905
252b5132
RH
906 if (sorted_symcount < 1)
907 {
908 (*info->fprintf_func) (info->stream, "0x");
909 objdump_print_value (vma, info, skip_zeroes);
32760852
NC
910
911 if (display_file_offsets)
912 info->fprintf_func (info->stream, _(" (File Offset: 0x%lx)"),
913 (long int)(aux->sec->filepos + (vma - aux->sec->vma)));
252b5132
RH
914 return;
915 }
916
ce04548a
NC
917 if (aux->reloc != NULL
918 && aux->reloc->sym_ptr_ptr != NULL
919 && * aux->reloc->sym_ptr_ptr != NULL)
920 {
921 sym = * aux->reloc->sym_ptr_ptr;
922
923 /* Adjust the vma to the reloc. */
924 vma += bfd_asymbol_value (sym);
925
926 if (bfd_is_und_section (bfd_get_section (sym)))
927 skip_find = TRUE;
928 }
929
930 if (!skip_find)
ce04548a
NC
931 sym = find_symbol_for_address (vma, info, NULL);
932
252b5132
RH
933 objdump_print_addr_with_sym (aux->abfd, aux->sec, sym, vma, info,
934 skip_zeroes);
935}
936
937/* Print VMA to INFO. This function is passed to the disassembler
938 routine. */
939
940static void
46dca2e0 941objdump_print_address (bfd_vma vma, struct disassemble_info *info)
252b5132
RH
942{
943 objdump_print_addr (vma, info, ! prefix_addresses);
944}
945
2ae86dfc 946/* Determine if the given address has a symbol associated with it. */
252b5132
RH
947
948static int
46dca2e0 949objdump_symbol_at_address (bfd_vma vma, struct disassemble_info * info)
252b5132 950{
252b5132
RH
951 asymbol * sym;
952
22a398e1 953 sym = find_symbol_for_address (vma, info, NULL);
252b5132
RH
954
955 return (sym != NULL && (bfd_asymbol_value (sym) == vma));
956}
957
958/* Hold the last function name and the last line number we displayed
959 in a disassembly. */
960
961static char *prev_functionname;
962static unsigned int prev_line;
963
964/* We keep a list of all files that we have seen when doing a
50c2245b 965 disassembly with source, so that we know how much of the file to
252b5132
RH
966 display. This can be important for inlined functions. */
967
968struct print_file_list
969{
970 struct print_file_list *next;
43ac9881
AM
971 const char *filename;
972 const char *modname;
e8f5eee4
NC
973 const char *map;
974 size_t mapsize;
975 const char **linemap;
976 unsigned maxline;
977 unsigned last_line;
978 int first;
252b5132
RH
979};
980
981static struct print_file_list *print_files;
982
983/* The number of preceding context lines to show when we start
984 displaying a file for the first time. */
985
986#define SHOW_PRECEDING_CONTEXT_LINES (5)
987
417ed8af 988/* Read a complete file into memory. */
e8f5eee4
NC
989
990static const char *
991slurp_file (const char *fn, size_t *size)
992{
993#ifdef HAVE_MMAP
994 int ps = getpagesize ();
995 size_t msize;
996#endif
997 const char *map;
998 struct stat st;
417ed8af 999 int fd = open (fn, O_RDONLY | O_BINARY);
e8f5eee4
NC
1000
1001 if (fd < 0)
1002 return NULL;
1003 if (fstat (fd, &st) < 0)
1004 return NULL;
1005 *size = st.st_size;
1006#ifdef HAVE_MMAP
1007 msize = (*size + ps - 1) & ~(ps - 1);
1008 map = mmap (NULL, msize, PROT_READ, MAP_SHARED, fd, 0);
1009 if (map != (char *)-1L)
1010 {
1011 close(fd);
1012 return map;
1013 }
1014#endif
1015 map = malloc (*size);
1016 if (!map || (size_t) read (fd, (char *)map, *size) != *size)
1017 {
1018 free ((void *)map);
1019 map = NULL;
1020 }
1021 close (fd);
1022 return map;
1023}
1024
1025#define line_map_decrease 5
1026
1027/* Precompute array of lines for a mapped file. */
1028
1029static const char **
1030index_file (const char *map, size_t size, unsigned int *maxline)
1031{
1032 const char *p, *lstart, *end;
1033 int chars_per_line = 45; /* First iteration will use 40. */
1034 unsigned int lineno;
1035 const char **linemap = NULL;
1036 unsigned long line_map_size = 0;
1037
1038 lineno = 0;
1039 lstart = map;
1040 end = map + size;
1041
1042 for (p = map; p < end; p++)
1043 {
1044 if (*p == '\n')
1045 {
1046 if (p + 1 < end && p[1] == '\r')
1047 p++;
1048 }
1049 else if (*p == '\r')
1050 {
1051 if (p + 1 < end && p[1] == '\n')
1052 p++;
1053 }
1054 else
1055 continue;
1056
1057 /* End of line found. */
1058
1059 if (linemap == NULL || line_map_size < lineno + 1)
1060 {
1061 unsigned long newsize;
1062
1063 chars_per_line -= line_map_decrease;
1064 if (chars_per_line <= 1)
1065 chars_per_line = 1;
1066 line_map_size = size / chars_per_line + 1;
1067 if (line_map_size < lineno + 1)
1068 line_map_size = lineno + 1;
1069 newsize = line_map_size * sizeof (char *);
1070 linemap = xrealloc (linemap, newsize);
1071 }
1072
1073 linemap[lineno++] = lstart;
1074 lstart = p + 1;
1075 }
1076
1077 *maxline = lineno;
1078 return linemap;
1079}
1080
43ac9881
AM
1081/* Tries to open MODNAME, and if successful adds a node to print_files
1082 linked list and returns that node. Returns NULL on failure. */
1083
1084static struct print_file_list *
1085try_print_file_open (const char *origname, const char *modname)
1086{
1087 struct print_file_list *p;
43ac9881 1088
e8f5eee4 1089 p = xmalloc (sizeof (struct print_file_list));
43ac9881 1090
e8f5eee4
NC
1091 p->map = slurp_file (modname, &p->mapsize);
1092 if (p->map == NULL)
43ac9881 1093 {
e8f5eee4
NC
1094 free (p);
1095 return NULL;
43ac9881 1096 }
e8f5eee4
NC
1097
1098 p->linemap = index_file (p->map, p->mapsize, &p->maxline);
1099 p->last_line = 0;
43ac9881
AM
1100 p->filename = origname;
1101 p->modname = modname;
43ac9881 1102 p->next = print_files;
e8f5eee4 1103 p->first = 1;
43ac9881
AM
1104 print_files = p;
1105 return p;
1106}
1107
1108/* If the the source file, as described in the symtab, is not found
1109 try to locate it in one of the paths specified with -I
1110 If found, add location to print_files linked list. */
1111
1112static struct print_file_list *
1113update_source_path (const char *filename)
1114{
1115 struct print_file_list *p;
1116 const char *fname;
1117 int i;
1118
1119 if (filename == NULL)
1120 return NULL;
1121
1122 p = try_print_file_open (filename, filename);
1123 if (p != NULL)
1124 return p;
1125
1126 if (include_path_count == 0)
1127 return NULL;
1128
1129 /* Get the name of the file. */
1130 fname = strrchr (filename, '/');
1131#ifdef HAVE_DOS_BASED_FILE_SYSTEM
1132 {
1133 /* We could have a mixed forward/back slash case. */
1134 char *backslash = strrchr (filename, '\\');
1135 if (fname == NULL || (backslash != NULL && backslash > fname))
1136 fname = backslash;
1137 if (fname == NULL && filename[0] != '\0' && filename[1] == ':')
1138 fname = filename + 1;
1139 }
1140#endif
1141 if (fname == NULL)
1142 fname = filename;
1143 else
1144 ++fname;
1145
1146 /* If file exists under a new path, we need to add it to the list
1147 so that show_line knows about it. */
1148 for (i = 0; i < include_path_count; i++)
1149 {
1150 char *modname = concat (include_paths[i], "/", fname, (const char *) 0);
1151
1152 p = try_print_file_open (filename, modname);
1153 if (p)
1154 return p;
1155
1156 free (modname);
1157 }
1158
1159 return NULL;
1160}
1161
e8f5eee4 1162/* Print a source file line. */
252b5132 1163
e8f5eee4
NC
1164static void
1165print_line (struct print_file_list *p, unsigned int line)
252b5132 1166{
e8f5eee4 1167 const char *l;
615f3149 1168 size_t len;
e8f5eee4
NC
1169
1170 --line;
1171 if (line >= p->maxline)
1172 return;
1173 l = p->linemap [line];
615f3149
AM
1174 /* Test fwrite return value to quiet glibc warning. */
1175 len = strcspn (l, "\n\r");
1176 if (len == 0 || fwrite (l, len, 1, stdout) == 1)
1177 putchar ('\n');
1178}
252b5132 1179
e8f5eee4 1180/* Print a range of source code lines. */
252b5132 1181
e8f5eee4
NC
1182static void
1183dump_lines (struct print_file_list *p, unsigned int start, unsigned int end)
1184{
1185 if (p->map == NULL)
1186 return;
1187 while (start <= end)
1188 {
1189 print_line (p, start);
1190 start++;
252b5132 1191 }
0af11b59 1192}
252b5132 1193
50c2245b 1194/* Show the line number, or the source line, in a disassembly
252b5132
RH
1195 listing. */
1196
1197static void
46dca2e0 1198show_line (bfd *abfd, asection *section, bfd_vma addr_offset)
252b5132 1199{
b1f88ebe
AM
1200 const char *filename;
1201 const char *functionname;
252b5132 1202 unsigned int line;
0dafdf3f 1203 bfd_boolean reloc;
252b5132
RH
1204
1205 if (! with_line_numbers && ! with_source_code)
1206 return;
1207
940b2b78 1208 if (! bfd_find_nearest_line (abfd, section, syms, addr_offset, &filename,
252b5132
RH
1209 &functionname, &line))
1210 return;
1211
1212 if (filename != NULL && *filename == '\0')
1213 filename = NULL;
1214 if (functionname != NULL && *functionname == '\0')
1215 functionname = NULL;
1216
0dafdf3f
L
1217 if (filename
1218 && IS_ABSOLUTE_PATH (filename)
1219 && prefix)
1220 {
1221 char *path_up;
1222 const char *fname = filename;
1223 char *path = (char *) alloca (prefix_length + PATH_MAX + 1);
1224
1225 if (prefix_length)
1226 memcpy (path, prefix, prefix_length);
1227 path_up = path + prefix_length;
1228
1229 /* Build relocated filename, stripping off leading directories
1230 from the initial filename if requested. */
1231 if (prefix_strip > 0)
1232 {
1233 int level = 0;
1234 const char *s;
1235
1236 /* Skip selected directory levels. */
1237 for (s = fname + 1; *s != '\0' && level < prefix_strip; s++)
1238 if (IS_DIR_SEPARATOR(*s))
1239 {
1240 fname = s;
1241 level++;
1242 }
1243 }
1244
1245 /* Update complete filename. */
1246 strncpy (path_up, fname, PATH_MAX);
1247 path_up[PATH_MAX] = '\0';
1248
1249 filename = path;
1250 reloc = TRUE;
1251 }
1252 else
1253 reloc = FALSE;
1254
252b5132
RH
1255 if (with_line_numbers)
1256 {
1257 if (functionname != NULL
1258 && (prev_functionname == NULL
1259 || strcmp (functionname, prev_functionname) != 0))
1260 printf ("%s():\n", functionname);
1261 if (line > 0 && line != prev_line)
1262 printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
1263 }
1264
1265 if (with_source_code
1266 && filename != NULL
1267 && line > 0)
1268 {
1269 struct print_file_list **pp, *p;
e8f5eee4 1270 unsigned l;
252b5132
RH
1271
1272 for (pp = &print_files; *pp != NULL; pp = &(*pp)->next)
1273 if (strcmp ((*pp)->filename, filename) == 0)
1274 break;
1275 p = *pp;
1276
e8f5eee4 1277 if (p == NULL)
0dafdf3f
L
1278 {
1279 if (reloc)
1280 filename = xstrdup (filename);
43ac9881 1281 p = update_source_path (filename);
0dafdf3f 1282 }
252b5132 1283
e8f5eee4
NC
1284 if (p != NULL && line != p->last_line)
1285 {
1286 if (file_start_context && p->first)
1287 l = 1;
1288 else
252b5132 1289 {
e8f5eee4
NC
1290 l = line - SHOW_PRECEDING_CONTEXT_LINES;
1291 if (l >= line)
1292 l = 1;
1293 if (p->last_line >= l && p->last_line <= line)
1294 l = p->last_line + 1;
252b5132 1295 }
e8f5eee4
NC
1296 dump_lines (p, l, line);
1297 p->last_line = line;
1298 p->first = 0;
252b5132
RH
1299 }
1300 }
1301
1302 if (functionname != NULL
1303 && (prev_functionname == NULL
1304 || strcmp (functionname, prev_functionname) != 0))
1305 {
1306 if (prev_functionname != NULL)
1307 free (prev_functionname);
1308 prev_functionname = xmalloc (strlen (functionname) + 1);
1309 strcpy (prev_functionname, functionname);
1310 }
1311
1312 if (line > 0 && line != prev_line)
1313 prev_line = line;
1314}
1315
1316/* Pseudo FILE object for strings. */
1317typedef struct
1318{
1319 char *buffer;
6f104306
NS
1320 size_t pos;
1321 size_t alloc;
252b5132
RH
1322} SFILE;
1323
46dca2e0 1324/* sprintf to a "stream". */
252b5132 1325
0fd3a477 1326static int ATTRIBUTE_PRINTF_2
46dca2e0 1327objdump_sprintf (SFILE *f, const char *format, ...)
252b5132 1328{
252b5132 1329 size_t n;
46dca2e0 1330 va_list args;
252b5132 1331
6f104306 1332 while (1)
252b5132 1333 {
6f104306
NS
1334 size_t space = f->alloc - f->pos;
1335
1336 va_start (args, format);
1337 n = vsnprintf (f->buffer + f->pos, space, format, args);
451dad9c 1338 va_end (args);
252b5132 1339
6f104306
NS
1340 if (space > n)
1341 break;
1342
1343 f->alloc = (f->alloc + n) * 2;
1344 f->buffer = xrealloc (f->buffer, f->alloc);
252b5132 1345 }
6f104306
NS
1346 f->pos += n;
1347
252b5132
RH
1348 return n;
1349}
1350
155e0d23
NC
1351/* Returns TRUE if the specified section should be dumped. */
1352
1353static bfd_boolean
1354process_section_p (asection * section)
1355{
1356 size_t i;
1357
1358 if (only == NULL)
1359 return TRUE;
1360
1361 for (i = 0; i < only_used; i++)
1362 if (strcmp (only [i], section->name) == 0)
1363 return TRUE;
1364
1365 return FALSE;
1366}
1367
1368
252b5132
RH
1369/* The number of zeroes we want to see before we start skipping them.
1370 The number is arbitrarily chosen. */
1371
0bcb06d2 1372#define DEFAULT_SKIP_ZEROES 8
252b5132
RH
1373
1374/* The number of zeroes to skip at the end of a section. If the
1375 number of zeroes at the end is between SKIP_ZEROES_AT_END and
1376 SKIP_ZEROES, they will be disassembled. If there are fewer than
1377 SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic
1378 attempt to avoid disassembling zeroes inserted by section
1379 alignment. */
1380
0bcb06d2 1381#define DEFAULT_SKIP_ZEROES_AT_END 3
252b5132
RH
1382
1383/* Disassemble some data in memory between given values. */
1384
1385static void
46dca2e0
NC
1386disassemble_bytes (struct disassemble_info * info,
1387 disassembler_ftype disassemble_fn,
1388 bfd_boolean insns,
1389 bfd_byte * data,
1390 bfd_vma start_offset,
1391 bfd_vma stop_offset,
fd7bb956 1392 bfd_vma rel_offset,
46dca2e0
NC
1393 arelent *** relppp,
1394 arelent ** relppend)
252b5132
RH
1395{
1396 struct objdump_disasm_info *aux;
1397 asection *section;
940b2b78 1398 int octets_per_line;
b34976b6 1399 bfd_boolean done_dot;
252b5132 1400 int skip_addr_chars;
940b2b78 1401 bfd_vma addr_offset;
0bcb06d2
AS
1402 unsigned int opb = info->octets_per_byte;
1403 unsigned int skip_zeroes = info->skip_zeroes;
1404 unsigned int skip_zeroes_at_end = info->skip_zeroes_at_end;
ce04548a 1405 int octets = opb;
6f104306 1406 SFILE sfile;
252b5132
RH
1407
1408 aux = (struct objdump_disasm_info *) info->application_data;
1409 section = aux->sec;
1410
6f104306
NS
1411 sfile.alloc = 120;
1412 sfile.buffer = xmalloc (sfile.alloc);
1413 sfile.pos = 0;
1414
252b5132 1415 if (insns)
940b2b78 1416 octets_per_line = 4;
252b5132 1417 else
940b2b78 1418 octets_per_line = 16;
252b5132
RH
1419
1420 /* Figure out how many characters to skip at the start of an
1421 address, to make the disassembly look nicer. We discard leading
1422 zeroes in chunks of 4, ensuring that there is always a leading
1423 zero remaining. */
1424 skip_addr_chars = 0;
1425 if (! prefix_addresses)
1426 {
1427 char buf[30];
17ceb936
AM
1428
1429 bfd_sprintf_vma (aux->abfd, buf, section->vma + section->size / opb);
1430
1431 while (buf[skip_addr_chars] == '0')
1432 ++skip_addr_chars;
1433
1434 /* Don't discard zeros on overflow. */
1435 if (buf[skip_addr_chars] == '\0' && section->vma != 0)
1436 skip_addr_chars = 0;
1437
1438 if (skip_addr_chars != 0)
1439 skip_addr_chars = (skip_addr_chars - 1) & -4;
252b5132
RH
1440 }
1441
1442 info->insn_info_valid = 0;
1443
b34976b6 1444 done_dot = FALSE;
940b2b78
TW
1445 addr_offset = start_offset;
1446 while (addr_offset < stop_offset)
252b5132
RH
1447 {
1448 bfd_vma z;
b34976b6 1449 bfd_boolean need_nl = FALSE;
ce04548a
NC
1450 int previous_octets;
1451
1452 /* Remember the length of the previous instruction. */
1453 previous_octets = octets;
ce04548a 1454 octets = 0;
252b5132 1455
940b2b78 1456 /* If we see more than SKIP_ZEROES octets of zeroes, we just
43ac9881 1457 print `...'. */
940b2b78 1458 for (z = addr_offset * opb; z < stop_offset * opb; z++)
252b5132
RH
1459 if (data[z] != 0)
1460 break;
1461 if (! disassemble_zeroes
1462 && (info->insn_info_valid == 0
1463 || info->branch_delay_insns == 0)
0bcb06d2 1464 && (z - addr_offset * opb >= skip_zeroes
0af11b59 1465 || (z == stop_offset * opb &&
0bcb06d2 1466 z - addr_offset * opb < skip_zeroes_at_end)))
252b5132 1467 {
940b2b78 1468 /* If there are more nonzero octets to follow, we only skip
43ac9881
AM
1469 zeroes in multiples of 4, to try to avoid running over
1470 the start of an instruction which happens to start with
1471 zero. */
940b2b78
TW
1472 if (z != stop_offset * opb)
1473 z = addr_offset * opb + ((z - addr_offset * opb) &~ 3);
252b5132 1474
940b2b78 1475 octets = z - addr_offset * opb;
98ec6e72
NC
1476
1477 /* If we are going to display more data, and we are displaying
1478 file offsets, then tell the user how many zeroes we skip
1479 and the file offset from where we resume dumping. */
1480 if (display_file_offsets && ((addr_offset + (octets / opb)) < stop_offset))
1481 printf ("\t... (skipping %d zeroes, resuming at file offset: 0x%lx)\n",
1482 octets / opb,
0af1713e
AM
1483 (unsigned long) (section->filepos
1484 + (addr_offset + (octets / opb))));
98ec6e72
NC
1485 else
1486 printf ("\t...\n");
252b5132
RH
1487 }
1488 else
1489 {
1490 char buf[50];
252b5132
RH
1491 int bpc = 0;
1492 int pb = 0;
1493
b34976b6 1494 done_dot = FALSE;
252b5132
RH
1495
1496 if (with_line_numbers || with_source_code)
bc79cded 1497 show_line (aux->abfd, section, addr_offset);
252b5132
RH
1498
1499 if (! prefix_addresses)
1500 {
1501 char *s;
1502
d8180c76 1503 bfd_sprintf_vma (aux->abfd, buf, section->vma + addr_offset);
252b5132
RH
1504 for (s = buf + skip_addr_chars; *s == '0'; s++)
1505 *s = ' ';
1506 if (*s == '\0')
1507 *--s = '0';
1508 printf ("%s:\t", buf + skip_addr_chars);
1509 }
1510 else
1511 {
b34976b6 1512 aux->require_sec = TRUE;
940b2b78 1513 objdump_print_address (section->vma + addr_offset, info);
b34976b6 1514 aux->require_sec = FALSE;
252b5132
RH
1515 putchar (' ');
1516 }
1517
1518 if (insns)
1519 {
6f104306 1520 sfile.pos = 0;
252b5132 1521 info->fprintf_func = (fprintf_ftype) objdump_sprintf;
5810296b 1522 info->stream = &sfile;
252b5132
RH
1523 info->bytes_per_line = 0;
1524 info->bytes_per_chunk = 0;
ce04548a 1525 info->flags = 0;
252b5132 1526
d99b6465 1527 if (info->disassembler_needs_relocs
7df428b1
RS
1528 && (bfd_get_file_flags (aux->abfd) & EXEC_P) == 0
1529 && (bfd_get_file_flags (aux->abfd) & DYNAMIC) == 0
d99b6465 1530 && *relppp < relppend)
ce04548a
NC
1531 {
1532 bfd_signed_vma distance_to_rel;
1533
1534 distance_to_rel = (**relppp)->address
1535 - (rel_offset + addr_offset);
1536
1537 /* Check to see if the current reloc is associated with
1538 the instruction that we are about to disassemble. */
1539 if (distance_to_rel == 0
1540 /* FIXME: This is wrong. We are trying to catch
1541 relocs that are addressed part way through the
1542 current instruction, as might happen with a packed
1543 VLIW instruction. Unfortunately we do not know the
1544 length of the current instruction since we have not
1545 disassembled it yet. Instead we take a guess based
1546 upon the length of the previous instruction. The
1547 proper solution is to have a new target-specific
1548 disassembler function which just returns the length
1549 of an instruction at a given address without trying
1550 to display its disassembly. */
1551 || (distance_to_rel > 0
1552 && distance_to_rel < (bfd_signed_vma) (previous_octets/ opb)))
1553 {
1554 info->flags = INSN_HAS_RELOC;
1555 aux->reloc = **relppp;
1556 }
1557 else
1558 aux->reloc = NULL;
1559 }
d99b6465 1560
940b2b78 1561 octets = (*disassemble_fn) (section->vma + addr_offset, info);
252b5132
RH
1562 info->fprintf_func = (fprintf_ftype) fprintf;
1563 info->stream = stdout;
1564 if (info->bytes_per_line != 0)
940b2b78
TW
1565 octets_per_line = info->bytes_per_line;
1566 if (octets < 0)
e07bf1ac 1567 {
6f104306 1568 if (sfile.pos)
e07bf1ac 1569 printf ("%s\n", sfile.buffer);
e07bf1ac
ILT
1570 break;
1571 }
252b5132
RH
1572 }
1573 else
1574 {
b4c96d0d 1575 bfd_vma j;
252b5132 1576
940b2b78
TW
1577 octets = octets_per_line;
1578 if (addr_offset + octets / opb > stop_offset)
1579 octets = (stop_offset - addr_offset) * opb;
252b5132 1580
940b2b78 1581 for (j = addr_offset * opb; j < addr_offset * opb + octets; ++j)
252b5132 1582 {
3882b010 1583 if (ISPRINT (data[j]))
940b2b78 1584 buf[j - addr_offset * opb] = data[j];
252b5132 1585 else
940b2b78 1586 buf[j - addr_offset * opb] = '.';
252b5132 1587 }
940b2b78 1588 buf[j - addr_offset * opb] = '\0';
252b5132
RH
1589 }
1590
1591 if (prefix_addresses
1592 ? show_raw_insn > 0
1593 : show_raw_insn >= 0)
1594 {
b4c96d0d 1595 bfd_vma j;
252b5132
RH
1596
1597 /* If ! prefix_addresses and ! wide_output, we print
43ac9881 1598 octets_per_line octets per line. */
940b2b78
TW
1599 pb = octets;
1600 if (pb > octets_per_line && ! prefix_addresses && ! wide_output)
1601 pb = octets_per_line;
252b5132
RH
1602
1603 if (info->bytes_per_chunk)
1604 bpc = info->bytes_per_chunk;
1605 else
1606 bpc = 1;
1607
940b2b78 1608 for (j = addr_offset * opb; j < addr_offset * opb + pb; j += bpc)
252b5132
RH
1609 {
1610 int k;
ed049af3 1611
252b5132
RH
1612 if (bpc > 1 && info->display_endian == BFD_ENDIAN_LITTLE)
1613 {
1614 for (k = bpc - 1; k >= 0; k--)
1615 printf ("%02x", (unsigned) data[j + k]);
1616 putchar (' ');
1617 }
1618 else
1619 {
1620 for (k = 0; k < bpc; k++)
1621 printf ("%02x", (unsigned) data[j + k]);
1622 putchar (' ');
1623 }
1624 }
1625
940b2b78 1626 for (; pb < octets_per_line; pb += bpc)
252b5132
RH
1627 {
1628 int k;
1629
1630 for (k = 0; k < bpc; k++)
1631 printf (" ");
1632 putchar (' ');
1633 }
1634
1635 /* Separate raw data from instruction by extra space. */
1636 if (insns)
1637 putchar ('\t');
1638 else
1639 printf (" ");
1640 }
1641
1642 if (! insns)
1643 printf ("%s", buf);
6f104306
NS
1644 else if (sfile.pos)
1645 printf ("%s", sfile.buffer);
252b5132
RH
1646
1647 if (prefix_addresses
1648 ? show_raw_insn > 0
1649 : show_raw_insn >= 0)
1650 {
940b2b78 1651 while (pb < octets)
252b5132 1652 {
b4c96d0d 1653 bfd_vma j;
252b5132
RH
1654 char *s;
1655
1656 putchar ('\n');
940b2b78 1657 j = addr_offset * opb + pb;
252b5132 1658
d8180c76 1659 bfd_sprintf_vma (aux->abfd, buf, section->vma + j / opb);
252b5132
RH
1660 for (s = buf + skip_addr_chars; *s == '0'; s++)
1661 *s = ' ';
1662 if (*s == '\0')
1663 *--s = '0';
1664 printf ("%s:\t", buf + skip_addr_chars);
1665
940b2b78
TW
1666 pb += octets_per_line;
1667 if (pb > octets)
1668 pb = octets;
1669 for (; j < addr_offset * opb + pb; j += bpc)
252b5132
RH
1670 {
1671 int k;
1672
1673 if (bpc > 1 && info->display_endian == BFD_ENDIAN_LITTLE)
1674 {
1675 for (k = bpc - 1; k >= 0; k--)
1676 printf ("%02x", (unsigned) data[j + k]);
1677 putchar (' ');
1678 }
1679 else
1680 {
1681 for (k = 0; k < bpc; k++)
1682 printf ("%02x", (unsigned) data[j + k]);
1683 putchar (' ');
1684 }
1685 }
1686 }
1687 }
1688
1689 if (!wide_output)
1690 putchar ('\n');
1691 else
b34976b6 1692 need_nl = TRUE;
252b5132
RH
1693 }
1694
fd7bb956
AM
1695 while ((*relppp) < relppend
1696 && (**relppp)->address < rel_offset + addr_offset + octets / opb)
252b5132 1697 {
fd7bb956 1698 if (dump_reloc_info || dump_dynamic_reloc_info)
252b5132
RH
1699 {
1700 arelent *q;
1701
1702 q = **relppp;
1703
1704 if (wide_output)
1705 putchar ('\t');
1706 else
1707 printf ("\t\t\t");
1708
68b3b8dc
JJ
1709 objdump_print_value (section->vma - rel_offset + q->address,
1710 info, TRUE);
252b5132 1711
f9ecb0a4
JJ
1712 if (q->howto == NULL)
1713 printf (": *unknown*\t");
1714 else if (q->howto->name)
1715 printf (": %s\t", q->howto->name);
1716 else
1717 printf (": %d\t", q->howto->type);
252b5132
RH
1718
1719 if (q->sym_ptr_ptr == NULL || *q->sym_ptr_ptr == NULL)
1720 printf ("*unknown*");
1721 else
1722 {
1723 const char *sym_name;
1724
1725 sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
1726 if (sym_name != NULL && *sym_name != '\0')
1727 objdump_print_symname (aux->abfd, info, *q->sym_ptr_ptr);
1728 else
1729 {
1730 asection *sym_sec;
1731
1732 sym_sec = bfd_get_section (*q->sym_ptr_ptr);
1733 sym_name = bfd_get_section_name (aux->abfd, sym_sec);
1734 if (sym_name == NULL || *sym_name == '\0')
1735 sym_name = "*unknown*";
1736 printf ("%s", sym_name);
1737 }
1738 }
1739
1740 if (q->addend)
1741 {
1742 printf ("+0x");
b34976b6 1743 objdump_print_value (q->addend, info, TRUE);
252b5132
RH
1744 }
1745
1746 printf ("\n");
b34976b6 1747 need_nl = FALSE;
252b5132 1748 }
fd7bb956 1749 ++(*relppp);
252b5132
RH
1750 }
1751
1752 if (need_nl)
1753 printf ("\n");
1754
940b2b78 1755 addr_offset += octets / opb;
252b5132 1756 }
6f104306
NS
1757
1758 free (sfile.buffer);
252b5132
RH
1759}
1760
155e0d23
NC
1761static void
1762disassemble_section (bfd *abfd, asection *section, void *info)
1763{
1b0adfe0
NC
1764 const struct elf_backend_data * bed;
1765 bfd_vma sign_adjust = 0;
155e0d23 1766 struct disassemble_info * pinfo = (struct disassemble_info *) info;
3b9ad1cc 1767 struct objdump_disasm_info * paux;
155e0d23
NC
1768 unsigned int opb = pinfo->octets_per_byte;
1769 bfd_byte * data = NULL;
1770 bfd_size_type datasize = 0;
1771 arelent ** rel_pp = NULL;
1772 arelent ** rel_ppstart = NULL;
1773 arelent ** rel_ppend;
1774 unsigned long stop_offset;
1775 asymbol * sym = NULL;
1776 long place = 0;
1777 long rel_count;
1778 bfd_vma rel_offset;
1779 unsigned long addr_offset;
1780
1781 /* Sections that do not contain machine
1782 code are not normally disassembled. */
1783 if (! disassemble_all
1784 && only == NULL
46212538
AM
1785 && ((section->flags & (SEC_CODE | SEC_HAS_CONTENTS))
1786 != (SEC_CODE | SEC_HAS_CONTENTS)))
155e0d23
NC
1787 return;
1788
1789 if (! process_section_p (section))
1790 return;
1791
135dfb4a 1792 datasize = bfd_get_section_size (section);
155e0d23
NC
1793 if (datasize == 0)
1794 return;
1795
1796 /* Decide which set of relocs to use. Load them if necessary. */
3b9ad1cc 1797 paux = (struct objdump_disasm_info *) pinfo->application_data;
155e0d23
NC
1798 if (paux->dynrelbuf)
1799 {
1800 rel_pp = paux->dynrelbuf;
1801 rel_count = paux->dynrelcount;
1802 /* Dynamic reloc addresses are absolute, non-dynamic are section
50c2245b 1803 relative. REL_OFFSET specifies the reloc address corresponding
155e0d23 1804 to the start of this section. */
68b3b8dc 1805 rel_offset = section->vma;
155e0d23
NC
1806 }
1807 else
1808 {
1809 rel_count = 0;
1810 rel_pp = NULL;
1811 rel_offset = 0;
1812
1813 if ((section->flags & SEC_RELOC) != 0
d99b6465 1814 && (dump_reloc_info || pinfo->disassembler_needs_relocs))
155e0d23
NC
1815 {
1816 long relsize;
1817
1818 relsize = bfd_get_reloc_upper_bound (abfd, section);
1819 if (relsize < 0)
1820 bfd_fatal (bfd_get_filename (abfd));
1821
1822 if (relsize > 0)
1823 {
1824 rel_ppstart = rel_pp = xmalloc (relsize);
1825 rel_count = bfd_canonicalize_reloc (abfd, section, rel_pp, syms);
1826 if (rel_count < 0)
1827 bfd_fatal (bfd_get_filename (abfd));
1828
1829 /* Sort the relocs by address. */
1830 qsort (rel_pp, rel_count, sizeof (arelent *), compare_relocs);
1831 }
1832 }
155e0d23
NC
1833 }
1834 rel_ppend = rel_pp + rel_count;
1835
1836 data = xmalloc (datasize);
1837
1838 bfd_get_section_contents (abfd, section, data, 0, datasize);
1839
1840 paux->sec = section;
1841 pinfo->buffer = data;
1842 pinfo->buffer_vma = section->vma;
1843 pinfo->buffer_length = datasize;
1844 pinfo->section = section;
1845
1846 if (start_address == (bfd_vma) -1
1847 || start_address < pinfo->buffer_vma)
1848 addr_offset = 0;
1849 else
1850 addr_offset = start_address - pinfo->buffer_vma;
1851
1852 if (stop_address == (bfd_vma) -1)
1853 stop_offset = datasize / opb;
1854 else
1855 {
1856 if (stop_address < pinfo->buffer_vma)
1857 stop_offset = 0;
1858 else
1859 stop_offset = stop_address - pinfo->buffer_vma;
1860 if (stop_offset > pinfo->buffer_length / opb)
1861 stop_offset = pinfo->buffer_length / opb;
1862 }
1863
1864 /* Skip over the relocs belonging to addresses below the
1865 start address. */
1866 while (rel_pp < rel_ppend
1867 && (*rel_pp)->address < rel_offset + addr_offset)
1868 ++rel_pp;
1869
98ec6e72
NC
1870 if (addr_offset < stop_offset)
1871 printf (_("\nDisassembly of section %s:\n"), section->name);
155e0d23
NC
1872
1873 /* Find the nearest symbol forwards from our current position. */
3b9ad1cc 1874 paux->require_sec = TRUE;
22a398e1 1875 sym = find_symbol_for_address (section->vma + addr_offset, info, &place);
3b9ad1cc 1876 paux->require_sec = FALSE;
155e0d23 1877
1b0adfe0
NC
1878 /* PR 9774: If the target used signed 32-bit addresses then we must make
1879 sure that we sign extend the value that we calculate for 'addr' in the
1880 loop below. */
1881 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1882 && (bed = get_elf_backend_data (abfd)) != NULL
1883 && bed->sign_extend_vma)
1884 sign_adjust = 0x80000000;
1885
155e0d23
NC
1886 /* Disassemble a block of instructions up to the address associated with
1887 the symbol we have just found. Then print the symbol and find the
1888 next symbol on. Repeat until we have disassembled the entire section
1889 or we have reached the end of the address range we are interested in. */
1890 while (addr_offset < stop_offset)
1891 {
22a398e1 1892 bfd_vma addr;
155e0d23
NC
1893 asymbol *nextsym;
1894 unsigned long nextstop_offset;
1895 bfd_boolean insns;
1896
22a398e1 1897 addr = section->vma + addr_offset;
1b0adfe0 1898 addr = (addr ^ sign_adjust) - sign_adjust;
22a398e1
NC
1899
1900 if (sym != NULL && bfd_asymbol_value (sym) <= addr)
155e0d23
NC
1901 {
1902 int x;
1903
1904 for (x = place;
1905 (x < sorted_symcount
22a398e1 1906 && (bfd_asymbol_value (sorted_syms[x]) <= addr));
155e0d23
NC
1907 ++x)
1908 continue;
1909
22a398e1 1910 pinfo->symbols = sorted_syms + place;
155e0d23 1911 pinfo->num_symbols = x - place;
2087ad84 1912 pinfo->symtab_pos = place;
155e0d23
NC
1913 }
1914 else
22a398e1
NC
1915 {
1916 pinfo->symbols = NULL;
1917 pinfo->num_symbols = 0;
2087ad84 1918 pinfo->symtab_pos = -1;
22a398e1 1919 }
155e0d23
NC
1920
1921 if (! prefix_addresses)
1922 {
1923 pinfo->fprintf_func (pinfo->stream, "\n");
22a398e1 1924 objdump_print_addr_with_sym (abfd, section, sym, addr,
155e0d23
NC
1925 pinfo, FALSE);
1926 pinfo->fprintf_func (pinfo->stream, ":\n");
1927 }
1928
22a398e1 1929 if (sym != NULL && bfd_asymbol_value (sym) > addr)
155e0d23
NC
1930 nextsym = sym;
1931 else if (sym == NULL)
1932 nextsym = NULL;
1933 else
1934 {
22a398e1
NC
1935#define is_valid_next_sym(SYM) \
1936 ((SYM)->section == section \
1937 && (bfd_asymbol_value (SYM) > bfd_asymbol_value (sym)) \
1938 && pinfo->symbol_is_valid (SYM, pinfo))
1939
155e0d23
NC
1940 /* Search forward for the next appropriate symbol in
1941 SECTION. Note that all the symbols are sorted
1942 together into one big array, and that some sections
1943 may have overlapping addresses. */
1944 while (place < sorted_symcount
22a398e1 1945 && ! is_valid_next_sym (sorted_syms [place]))
155e0d23 1946 ++place;
22a398e1 1947
155e0d23
NC
1948 if (place >= sorted_symcount)
1949 nextsym = NULL;
1950 else
1951 nextsym = sorted_syms[place];
1952 }
1953
22a398e1
NC
1954 if (sym != NULL && bfd_asymbol_value (sym) > addr)
1955 nextstop_offset = bfd_asymbol_value (sym) - section->vma;
155e0d23
NC
1956 else if (nextsym == NULL)
1957 nextstop_offset = stop_offset;
1958 else
22a398e1
NC
1959 nextstop_offset = bfd_asymbol_value (nextsym) - section->vma;
1960
1961 if (nextstop_offset > stop_offset)
1962 nextstop_offset = stop_offset;
155e0d23
NC
1963
1964 /* If a symbol is explicitly marked as being an object
1965 rather than a function, just dump the bytes without
1966 disassembling them. */
1967 if (disassemble_all
1968 || sym == NULL
abf71725 1969 || sym->section != section
22a398e1 1970 || bfd_asymbol_value (sym) > addr
155e0d23
NC
1971 || ((sym->flags & BSF_OBJECT) == 0
1972 && (strstr (bfd_asymbol_name (sym), "gnu_compiled")
1973 == NULL)
1974 && (strstr (bfd_asymbol_name (sym), "gcc2_compiled")
1975 == NULL))
1976 || (sym->flags & BSF_FUNCTION) != 0)
1977 insns = TRUE;
1978 else
1979 insns = FALSE;
1980
1981 disassemble_bytes (pinfo, paux->disassemble_fn, insns, data,
1982 addr_offset, nextstop_offset,
1983 rel_offset, &rel_pp, rel_ppend);
1984
1985 addr_offset = nextstop_offset;
1986 sym = nextsym;
1987 }
1988
1989 free (data);
1990
1991 if (rel_ppstart != NULL)
1992 free (rel_ppstart);
1993}
1994
252b5132
RH
1995/* Disassemble the contents of an object file. */
1996
1997static void
46dca2e0 1998disassemble_data (bfd *abfd)
252b5132 1999{
252b5132
RH
2000 struct disassemble_info disasm_info;
2001 struct objdump_disasm_info aux;
4c45e5c9 2002 long i;
252b5132
RH
2003
2004 print_files = NULL;
2005 prev_functionname = NULL;
2006 prev_line = -1;
2007
2008 /* We make a copy of syms to sort. We don't want to sort syms
2009 because that will screw up the relocs. */
4c45e5c9
JJ
2010 sorted_symcount = symcount ? symcount : dynsymcount;
2011 sorted_syms = xmalloc ((sorted_symcount + synthcount) * sizeof (asymbol *));
2012 memcpy (sorted_syms, symcount ? syms : dynsyms,
2013 sorted_symcount * sizeof (asymbol *));
252b5132 2014
4c45e5c9
JJ
2015 sorted_symcount = remove_useless_symbols (sorted_syms, sorted_symcount);
2016
2017 for (i = 0; i < synthcount; ++i)
2018 {
2019 sorted_syms[sorted_symcount] = synthsyms + i;
2020 ++sorted_symcount;
2021 }
252b5132 2022
98a91d6a 2023 /* Sort the symbols into section and symbol order. */
252b5132
RH
2024 qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols);
2025
22a398e1 2026 init_disassemble_info (&disasm_info, stdout, (fprintf_ftype) fprintf);
98a91d6a 2027
46dca2e0 2028 disasm_info.application_data = (void *) &aux;
252b5132 2029 aux.abfd = abfd;
b34976b6 2030 aux.require_sec = FALSE;
155e0d23
NC
2031 aux.dynrelbuf = NULL;
2032 aux.dynrelcount = 0;
ce04548a 2033 aux.reloc = NULL;
155e0d23 2034
252b5132
RH
2035 disasm_info.print_address_func = objdump_print_address;
2036 disasm_info.symbol_at_address_func = objdump_symbol_at_address;
2037
d3ba0551 2038 if (machine != NULL)
252b5132
RH
2039 {
2040 const bfd_arch_info_type *info = bfd_scan_arch (machine);
98a91d6a 2041
252b5132 2042 if (info == NULL)
98a91d6a
NC
2043 fatal (_("Can't use supplied machine %s"), machine);
2044
252b5132
RH
2045 abfd->arch_info = info;
2046 }
2047
2048 if (endian != BFD_ENDIAN_UNKNOWN)
2049 {
2050 struct bfd_target *xvec;
2051
d3ba0551 2052 xvec = xmalloc (sizeof (struct bfd_target));
252b5132
RH
2053 memcpy (xvec, abfd->xvec, sizeof (struct bfd_target));
2054 xvec->byteorder = endian;
2055 abfd->xvec = xvec;
2056 }
2057
155e0d23
NC
2058 /* Use libopcodes to locate a suitable disassembler. */
2059 aux.disassemble_fn = disassembler (abfd);
2060 if (!aux.disassemble_fn)
252b5132 2061 {
37cc8ec1
AM
2062 non_fatal (_("Can't disassemble for architecture %s\n"),
2063 bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
75cd796a 2064 exit_status = 1;
252b5132
RH
2065 return;
2066 }
2067
2068 disasm_info.flavour = bfd_get_flavour (abfd);
2069 disasm_info.arch = bfd_get_arch (abfd);
2070 disasm_info.mach = bfd_get_mach (abfd);
dd92f639 2071 disasm_info.disassembler_options = disassembler_options;
155e0d23 2072 disasm_info.octets_per_byte = bfd_octets_per_byte (abfd);
0bcb06d2
AS
2073 disasm_info.skip_zeroes = DEFAULT_SKIP_ZEROES;
2074 disasm_info.skip_zeroes_at_end = DEFAULT_SKIP_ZEROES_AT_END;
d99b6465 2075 disasm_info.disassembler_needs_relocs = FALSE;
0af11b59 2076
252b5132 2077 if (bfd_big_endian (abfd))
a8a9050d 2078 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_BIG;
252b5132 2079 else if (bfd_little_endian (abfd))
a8a9050d 2080 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_LITTLE;
252b5132
RH
2081 else
2082 /* ??? Aborting here seems too drastic. We could default to big or little
2083 instead. */
2084 disasm_info.endian = BFD_ENDIAN_UNKNOWN;
2085
22a398e1
NC
2086 /* Allow the target to customize the info structure. */
2087 disassemble_init_for_target (& disasm_info);
2088
155e0d23
NC
2089 /* Pre-load the dynamic relocs if we are going
2090 to be dumping them along with the disassembly. */
fd7bb956
AM
2091 if (dump_dynamic_reloc_info)
2092 {
2093 long relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
155e0d23 2094
fd7bb956
AM
2095 if (relsize < 0)
2096 bfd_fatal (bfd_get_filename (abfd));
2097
2098 if (relsize > 0)
2099 {
155e0d23 2100 aux.dynrelbuf = xmalloc (relsize);
3b9ad1cc
AM
2101 aux.dynrelcount = bfd_canonicalize_dynamic_reloc (abfd,
2102 aux.dynrelbuf,
2103 dynsyms);
155e0d23 2104 if (aux.dynrelcount < 0)
fd7bb956
AM
2105 bfd_fatal (bfd_get_filename (abfd));
2106
2107 /* Sort the relocs by address. */
3b9ad1cc
AM
2108 qsort (aux.dynrelbuf, aux.dynrelcount, sizeof (arelent *),
2109 compare_relocs);
fd7bb956
AM
2110 }
2111 }
2087ad84
PB
2112 disasm_info.symtab = sorted_syms;
2113 disasm_info.symtab_size = sorted_symcount;
fd7bb956 2114
155e0d23 2115 bfd_map_over_sections (abfd, disassemble_section, & disasm_info);
98a91d6a 2116
155e0d23
NC
2117 if (aux.dynrelbuf != NULL)
2118 free (aux.dynrelbuf);
252b5132
RH
2119 free (sorted_syms);
2120}
2121\f
365544c3
L
2122int
2123load_debug_section (enum dwarf_section_display_enum debug, void *file)
2124{
2125 struct dwarf_section *section = &debug_displays [debug].section;
2126 bfd *abfd = file;
2127 asection *sec;
2128 bfd_boolean ret;
1b315056 2129 int section_is_compressed;
365544c3
L
2130
2131 /* If it is already loaded, do nothing. */
2132 if (section->start != NULL)
2133 return 1;
2134
2135 /* Locate the debug section. */
1b315056
CS
2136 sec = bfd_get_section_by_name (abfd, section->uncompressed_name);
2137 if (sec != NULL)
a71cc8e0 2138 section->name = section->uncompressed_name;
1b315056
CS
2139 else
2140 {
2141 sec = bfd_get_section_by_name (abfd, section->compressed_name);
2142 if (sec != NULL)
a71cc8e0 2143 section->name = section->compressed_name;
1b315056 2144 }
365544c3
L
2145 if (sec == NULL)
2146 return 0;
a71cc8e0 2147 section_is_compressed = section->name == section->compressed_name;
365544c3 2148
f5913efe 2149 section->address = 0;
365544c3
L
2150 section->size = bfd_get_section_size (sec);
2151 section->start = xmalloc (section->size);
2152
2153 if (is_relocatable && debug_displays [debug].relocate)
2154 ret = bfd_simple_get_relocated_section_contents (abfd,
2155 sec,
2156 section->start,
2157 syms) != NULL;
2158 else
2159 ret = bfd_get_section_contents (abfd, sec, section->start, 0,
2160 section->size);
2161
1b315056 2162 if (! ret)
365544c3
L
2163 {
2164 free_debug_section (debug);
2165 printf (_("\nCan't get contents for section '%s'.\n"),
2166 section->name);
1b315056
CS
2167 return 0;
2168 }
2169
2170 if (section_is_compressed)
2171 {
2172 bfd_size_type size = section->size;
2173 if (! bfd_uncompress_section_contents (&section->start, &size))
2174 {
2175 free_debug_section (debug);
2176 printf (_("\nCan't uncompress section '%s'.\n"), section->name);
2177 return 0;
2178 }
2179 section->size = size;
365544c3
L
2180 }
2181
2182 return ret;
2183}
2184
2185void
2186free_debug_section (enum dwarf_section_display_enum debug)
2187{
2188 struct dwarf_section *section = &debug_displays [debug].section;
2189
2190 if (section->start == NULL)
2191 return;
2192
2193 free ((char *) section->start);
2194 section->start = NULL;
2195 section->address = 0;
2196 section->size = 0;
2197}
2198
2199static void
2200dump_dwarf_section (bfd *abfd, asection *section,
2201 void *arg ATTRIBUTE_UNUSED)
2202{
2203 const char *name = bfd_get_section_name (abfd, section);
2204 const char *match;
2205 enum dwarf_section_display_enum i;
2206
0112cd26 2207 if (CONST_STRNEQ (name, ".gnu.linkonce.wi."))
365544c3
L
2208 match = ".debug_info";
2209 else
2210 match = name;
2211
2212 for (i = 0; i < max; i++)
1b315056
CS
2213 if (strcmp (debug_displays [i].section.uncompressed_name, match) == 0
2214 || strcmp (debug_displays [i].section.compressed_name, match) == 0)
365544c3 2215 {
1b315056 2216 if (!debug_displays [i].eh_frame)
365544c3
L
2217 {
2218 struct dwarf_section *sec = &debug_displays [i].section;
2219
2220 if (load_debug_section (i, abfd))
2221 {
1b315056 2222 debug_displays [i].display (sec, abfd);
365544c3
L
2223
2224 if (i != info && i != abbrev)
2225 free_debug_section (i);
2226 }
2227 }
2228 break;
2229 }
2230}
2231
2232/* Dump the dwarf debugging information. */
2233
2234static void
2235dump_dwarf (bfd *abfd)
2236{
5184c2ae 2237 is_relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
365544c3
L
2238
2239 /* FIXME: bfd_get_arch_size may return -1. We assume that 64bit
2240 targets will return 64. */
2241 eh_addr_size = bfd_get_arch_size (abfd) == 64 ? 8 : 4;
2242
2243 if (bfd_big_endian (abfd))
2244 byte_get = byte_get_big_endian;
2245 else if (bfd_little_endian (abfd))
2246 byte_get = byte_get_little_endian;
2247 else
2248 abort ();
2249
2dc4cec1
L
2250 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
2251 {
2252 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2253 init_dwarf_regnames (bed->elf_machine_code);
2254 }
2255
365544c3
L
2256 bfd_map_over_sections (abfd, dump_dwarf_section, NULL);
2257
2258 free_debug_memory ();
2259}
2260\f
29ca8dc5
NS
2261/* Read ABFD's stabs section STABSECT_NAME, and return a pointer to
2262 it. Return NULL on failure. */
252b5132 2263
29ca8dc5
NS
2264static char *
2265read_section_stabs (bfd *abfd, const char *sect_name, bfd_size_type *size_ptr)
252b5132 2266{
29ca8dc5
NS
2267 asection *stabsect;
2268 bfd_size_type size;
2269 char *contents;
252b5132 2270
29ca8dc5 2271 stabsect = bfd_get_section_by_name (abfd, sect_name);
155e0d23 2272 if (stabsect == NULL)
252b5132 2273 {
29ca8dc5 2274 printf (_("No %s section present\n\n"), sect_name);
b34976b6 2275 return FALSE;
252b5132
RH
2276 }
2277
29ca8dc5
NS
2278 size = bfd_section_size (abfd, stabsect);
2279 contents = xmalloc (size);
0af11b59 2280
29ca8dc5 2281 if (! bfd_get_section_contents (abfd, stabsect, contents, 0, size))
252b5132 2282 {
37cc8ec1 2283 non_fatal (_("Reading %s section of %s failed: %s"),
29ca8dc5 2284 sect_name, bfd_get_filename (abfd),
37cc8ec1 2285 bfd_errmsg (bfd_get_error ()));
29ca8dc5 2286 free (contents);
75cd796a 2287 exit_status = 1;
29ca8dc5 2288 return NULL;
252b5132
RH
2289 }
2290
29ca8dc5 2291 *size_ptr = size;
252b5132 2292
29ca8dc5 2293 return contents;
252b5132
RH
2294}
2295
2296/* Stabs entries use a 12 byte format:
2297 4 byte string table index
2298 1 byte stab type
2299 1 byte stab other field
2300 2 byte stab desc field
2301 4 byte stab value
2302 FIXME: This will have to change for a 64 bit object format. */
2303
46dca2e0
NC
2304#define STRDXOFF (0)
2305#define TYPEOFF (4)
2306#define OTHEROFF (5)
2307#define DESCOFF (6)
2308#define VALOFF (8)
252b5132
RH
2309#define STABSIZE (12)
2310
2311/* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
2312 using string table section STRSECT_NAME (in `strtab'). */
2313
2314static void
3b9ad1cc
AM
2315print_section_stabs (bfd *abfd,
2316 const char *stabsect_name,
2317 unsigned *string_offset_ptr)
252b5132
RH
2318{
2319 int i;
46dca2e0 2320 unsigned file_string_table_offset = 0;
29ca8dc5 2321 unsigned next_file_string_table_offset = *string_offset_ptr;
252b5132
RH
2322 bfd_byte *stabp, *stabs_end;
2323
2324 stabp = stabs;
2325 stabs_end = stabp + stab_size;
2326
2327 printf (_("Contents of %s section:\n\n"), stabsect_name);
2328 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
2329
2330 /* Loop through all symbols and print them.
2331
2332 We start the index at -1 because there is a dummy symbol on
2333 the front of stabs-in-{coff,elf} sections that supplies sizes. */
252b5132
RH
2334 for (i = -1; stabp < stabs_end; stabp += STABSIZE, i++)
2335 {
2336 const char *name;
2337 unsigned long strx;
2338 unsigned char type, other;
2339 unsigned short desc;
2340 bfd_vma value;
2341
2342 strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
2343 type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
2344 other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
2345 desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
2346 value = bfd_h_get_32 (abfd, stabp + VALOFF);
2347
2348 printf ("\n%-6d ", i);
2349 /* Either print the stab name, or, if unnamed, print its number
0af11b59 2350 again (makes consistent formatting for tools like awk). */
252b5132
RH
2351 name = bfd_get_stab_name (type);
2352 if (name != NULL)
2353 printf ("%-6s", name);
2354 else if (type == N_UNDF)
2355 printf ("HdrSym");
2356 else
2357 printf ("%-6d", type);
2358 printf (" %-6d %-6d ", other, desc);
d8180c76 2359 bfd_printf_vma (abfd, value);
252b5132
RH
2360 printf (" %-6lu", strx);
2361
2362 /* Symbols with type == 0 (N_UNDF) specify the length of the
2363 string table associated with this file. We use that info
2364 to know how to relocate the *next* file's string table indices. */
252b5132
RH
2365 if (type == N_UNDF)
2366 {
2367 file_string_table_offset = next_file_string_table_offset;
2368 next_file_string_table_offset += value;
2369 }
2370 else
2371 {
2372 /* Using the (possibly updated) string table offset, print the
2373 string (if any) associated with this symbol. */
252b5132
RH
2374 if ((strx + file_string_table_offset) < stabstr_size)
2375 printf (" %s", &strtab[strx + file_string_table_offset]);
2376 else
2377 printf (" *");
2378 }
2379 }
2380 printf ("\n\n");
29ca8dc5 2381 *string_offset_ptr = next_file_string_table_offset;
252b5132
RH
2382}
2383
155e0d23
NC
2384typedef struct
2385{
2386 const char * section_name;
2387 const char * string_section_name;
29ca8dc5 2388 unsigned string_offset;
155e0d23
NC
2389}
2390stab_section_names;
2391
252b5132 2392static void
155e0d23 2393find_stabs_section (bfd *abfd, asection *section, void *names)
252b5132 2394{
155e0d23
NC
2395 int len;
2396 stab_section_names * sought = (stab_section_names *) names;
252b5132
RH
2397
2398 /* Check for section names for which stabsect_name is a prefix, to
29ca8dc5 2399 handle .stab.N, etc. */
155e0d23
NC
2400 len = strlen (sought->section_name);
2401
2402 /* If the prefix matches, and the files section name ends with a
2403 nul or a digit, then we match. I.e., we want either an exact
2404 match or a section followed by a number. */
2405 if (strncmp (sought->section_name, section->name, len) == 0
2406 && (section->name[len] == 0
29ca8dc5 2407 || (section->name[len] == '.' && ISDIGIT (section->name[len + 1]))))
252b5132 2408 {
29ca8dc5
NS
2409 if (strtab == NULL)
2410 strtab = read_section_stabs (abfd, sought->string_section_name,
2411 &stabstr_size);
2412
2413 if (strtab)
252b5132 2414 {
9210d879
AM
2415 stabs = (bfd_byte *) read_section_stabs (abfd, section->name,
2416 &stab_size);
29ca8dc5
NS
2417 if (stabs)
2418 print_section_stabs (abfd, section->name, &sought->string_offset);
252b5132
RH
2419 }
2420 }
2421}
98a91d6a 2422
155e0d23
NC
2423static void
2424dump_stabs_section (bfd *abfd, char *stabsect_name, char *strsect_name)
2425{
2426 stab_section_names s;
2427
2428 s.section_name = stabsect_name;
2429 s.string_section_name = strsect_name;
29ca8dc5
NS
2430 s.string_offset = 0;
2431
155e0d23 2432 bfd_map_over_sections (abfd, find_stabs_section, & s);
29ca8dc5
NS
2433
2434 free (strtab);
2435 strtab = NULL;
155e0d23
NC
2436}
2437
2438/* Dump the any sections containing stabs debugging information. */
2439
2440static void
2441dump_stabs (bfd *abfd)
2442{
2443 dump_stabs_section (abfd, ".stab", ".stabstr");
2444 dump_stabs_section (abfd, ".stab.excl", ".stab.exclstr");
2445 dump_stabs_section (abfd, ".stab.index", ".stab.indexstr");
62bb81b8
TG
2446
2447 /* For Darwin. */
2448 dump_stabs_section (abfd, "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr");
2449
155e0d23
NC
2450 dump_stabs_section (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
2451}
252b5132
RH
2452\f
2453static void
46dca2e0 2454dump_bfd_header (bfd *abfd)
252b5132
RH
2455{
2456 char *comma = "";
2457
2458 printf (_("architecture: %s, "),
2459 bfd_printable_arch_mach (bfd_get_arch (abfd),
2460 bfd_get_mach (abfd)));
2461 printf (_("flags 0x%08x:\n"), abfd->flags);
2462
2463#define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
2464 PF (HAS_RELOC, "HAS_RELOC");
2465 PF (EXEC_P, "EXEC_P");
2466 PF (HAS_LINENO, "HAS_LINENO");
2467 PF (HAS_DEBUG, "HAS_DEBUG");
2468 PF (HAS_SYMS, "HAS_SYMS");
2469 PF (HAS_LOCALS, "HAS_LOCALS");
2470 PF (DYNAMIC, "DYNAMIC");
2471 PF (WP_TEXT, "WP_TEXT");
2472 PF (D_PAGED, "D_PAGED");
2473 PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
026df7c5 2474 PF (HAS_LOAD_PAGE, "HAS_LOAD_PAGE");
252b5132 2475 printf (_("\nstart address 0x"));
d8180c76 2476 bfd_printf_vma (abfd, abfd->start_address);
252b5132
RH
2477 printf ("\n");
2478}
98a91d6a 2479
252b5132
RH
2480\f
2481static void
46dca2e0 2482dump_bfd_private_header (bfd *abfd)
252b5132
RH
2483{
2484 bfd_print_private_bfd_data (abfd, stdout);
2485}
2486
155e0d23
NC
2487\f
2488/* Display a section in hexadecimal format with associated characters.
2489 Each line prefixed by the zero padded address. */
d24de309 2490
252b5132 2491static void
155e0d23 2492dump_section (bfd *abfd, asection *section, void *dummy ATTRIBUTE_UNUSED)
252b5132 2493{
155e0d23
NC
2494 bfd_byte *data = 0;
2495 bfd_size_type datasize;
2496 bfd_size_type addr_offset;
2497 bfd_size_type start_offset;
2498 bfd_size_type stop_offset;
2499 unsigned int opb = bfd_octets_per_byte (abfd);
2500 /* Bytes per line. */
2501 const int onaline = 16;
2502 char buf[64];
2503 int count;
2504 int width;
2505
2506 if ((section->flags & SEC_HAS_CONTENTS) == 0)
2507 return;
2508
2509 if (! process_section_p (section))
2510 return;
2511
2512 if ((datasize = bfd_section_size (abfd, section)) == 0)
2513 return;
2514
155e0d23
NC
2515 /* Compute the address range to display. */
2516 if (start_address == (bfd_vma) -1
2517 || start_address < section->vma)
2518 start_offset = 0;
2519 else
2520 start_offset = start_address - section->vma;
2521
2522 if (stop_address == (bfd_vma) -1)
2523 stop_offset = datasize / opb;
2524 else
252b5132 2525 {
155e0d23
NC
2526 if (stop_address < section->vma)
2527 stop_offset = 0;
2528 else
2529 stop_offset = stop_address - section->vma;
252b5132 2530
155e0d23
NC
2531 if (stop_offset > datasize / opb)
2532 stop_offset = datasize / opb;
252b5132
RH
2533 }
2534
32760852
NC
2535 if (start_offset >= stop_offset)
2536 return;
2537
2538 printf (_("Contents of section %s:"), section->name);
2539 if (display_file_offsets)
0af1713e
AM
2540 printf (_(" (Starting at file offset: 0x%lx)"),
2541 (unsigned long) (section->filepos + start_offset));
32760852
NC
2542 printf ("\n");
2543
2544 data = xmalloc (datasize);
2545
2546 bfd_get_section_contents (abfd, section, data, 0, datasize);
2547
155e0d23 2548 width = 4;
026df7c5 2549
155e0d23
NC
2550 bfd_sprintf_vma (abfd, buf, start_offset + section->vma);
2551 if (strlen (buf) >= sizeof (buf))
2552 abort ();
026df7c5 2553
155e0d23
NC
2554 count = 0;
2555 while (buf[count] == '0' && buf[count+1] != '\0')
2556 count++;
2557 count = strlen (buf) - count;
2558 if (count > width)
2559 width = count;
252b5132 2560
155e0d23
NC
2561 bfd_sprintf_vma (abfd, buf, stop_offset + section->vma - 1);
2562 if (strlen (buf) >= sizeof (buf))
2563 abort ();
026df7c5 2564
155e0d23
NC
2565 count = 0;
2566 while (buf[count] == '0' && buf[count+1] != '\0')
2567 count++;
2568 count = strlen (buf) - count;
2569 if (count > width)
2570 width = count;
026df7c5 2571
155e0d23
NC
2572 for (addr_offset = start_offset;
2573 addr_offset < stop_offset; addr_offset += onaline / opb)
d24de309 2574 {
155e0d23 2575 bfd_size_type j;
d24de309 2576
155e0d23
NC
2577 bfd_sprintf_vma (abfd, buf, (addr_offset + section->vma));
2578 count = strlen (buf);
2579 if ((size_t) count >= sizeof (buf))
2580 abort ();
d24de309 2581
155e0d23
NC
2582 putchar (' ');
2583 while (count < width)
252b5132 2584 {
155e0d23
NC
2585 putchar ('0');
2586 count++;
2587 }
2588 fputs (buf + count - width, stdout);
2589 putchar (' ');
252b5132 2590
155e0d23
NC
2591 for (j = addr_offset * opb;
2592 j < addr_offset * opb + onaline; j++)
2593 {
2594 if (j < stop_offset * opb)
2595 printf ("%02x", (unsigned) (data[j]));
2596 else
2597 printf (" ");
2598 if ((j & 3) == 3)
2599 printf (" ");
252b5132
RH
2600 }
2601
155e0d23
NC
2602 printf (" ");
2603 for (j = addr_offset * opb;
2604 j < addr_offset * opb + onaline; j++)
2605 {
2606 if (j >= stop_offset * opb)
2607 printf (" ");
2608 else
2609 printf ("%c", ISPRINT (data[j]) ? data[j] : '.');
2610 }
2611 putchar ('\n');
252b5132 2612 }
155e0d23 2613 free (data);
252b5132 2614}
155e0d23 2615
98a91d6a 2616/* Actually display the various requested regions. */
252b5132
RH
2617
2618static void
46dca2e0 2619dump_data (bfd *abfd)
252b5132 2620{
155e0d23 2621 bfd_map_over_sections (abfd, dump_section, NULL);
252b5132
RH
2622}
2623
98a91d6a
NC
2624/* Should perhaps share code and display with nm? */
2625
252b5132 2626static void
46dca2e0 2627dump_symbols (bfd *abfd ATTRIBUTE_UNUSED, bfd_boolean dynamic)
252b5132
RH
2628{
2629 asymbol **current;
2630 long max;
2631 long count;
2632
2633 if (dynamic)
2634 {
2635 current = dynsyms;
2636 max = dynsymcount;
252b5132
RH
2637 printf ("DYNAMIC SYMBOL TABLE:\n");
2638 }
2639 else
2640 {
2641 current = syms;
2642 max = symcount;
252b5132
RH
2643 printf ("SYMBOL TABLE:\n");
2644 }
2645
a1df01d1
AM
2646 if (max == 0)
2647 printf (_("no symbols\n"));
2648
252b5132
RH
2649 for (count = 0; count < max; count++)
2650 {
155e0d23
NC
2651 bfd *cur_bfd;
2652
2653 if (*current == NULL)
83ef0798 2654 printf (_("no information for symbol number %ld\n"), count);
155e0d23
NC
2655
2656 else if ((cur_bfd = bfd_asymbol_bfd (*current)) == NULL)
83ef0798 2657 printf (_("could not determine the type of symbol number %ld\n"),
155e0d23
NC
2658 count);
2659
661f7c35
NC
2660 else if (process_section_p ((* current)->section)
2661 && (dump_special_syms
2662 || !bfd_is_target_special_symbol (cur_bfd, *current)))
252b5132 2663 {
155e0d23 2664 const char *name = (*current)->name;
252b5132 2665
155e0d23 2666 if (do_demangle && name != NULL && *name != '\0')
252b5132 2667 {
252b5132
RH
2668 char *alloc;
2669
155e0d23
NC
2670 /* If we want to demangle the name, we demangle it
2671 here, and temporarily clobber it while calling
2672 bfd_print_symbol. FIXME: This is a gross hack. */
ed180cc5
AM
2673 alloc = bfd_demangle (cur_bfd, name, DMGL_ANSI | DMGL_PARAMS);
2674 if (alloc != NULL)
2675 (*current)->name = alloc;
252b5132
RH
2676 bfd_print_symbol (cur_bfd, stdout, *current,
2677 bfd_print_symbol_all);
ed180cc5
AM
2678 if (alloc != NULL)
2679 {
2680 (*current)->name = name;
2681 free (alloc);
2682 }
252b5132 2683 }
252b5132 2684 else
155e0d23
NC
2685 bfd_print_symbol (cur_bfd, stdout, *current,
2686 bfd_print_symbol_all);
83ef0798 2687 printf ("\n");
252b5132 2688 }
661f7c35 2689
155e0d23 2690 current++;
252b5132 2691 }
155e0d23 2692 printf ("\n\n");
252b5132 2693}
155e0d23 2694\f
252b5132 2695static void
46dca2e0 2696dump_reloc_set (bfd *abfd, asection *sec, arelent **relpp, long relcount)
252b5132
RH
2697{
2698 arelent **p;
2699 char *last_filename, *last_functionname;
2700 unsigned int last_line;
2701
2702 /* Get column headers lined up reasonably. */
2703 {
2704 static int width;
98a91d6a 2705
252b5132
RH
2706 if (width == 0)
2707 {
2708 char buf[30];
155e0d23 2709
d8180c76 2710 bfd_sprintf_vma (abfd, buf, (bfd_vma) -1);
252b5132
RH
2711 width = strlen (buf) - 7;
2712 }
2713 printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
2714 }
2715
2716 last_filename = NULL;
2717 last_functionname = NULL;
2718 last_line = 0;
2719
d3ba0551 2720 for (p = relpp; relcount && *p != NULL; p++, relcount--)
252b5132
RH
2721 {
2722 arelent *q = *p;
2723 const char *filename, *functionname;
2724 unsigned int line;
2725 const char *sym_name;
2726 const char *section_name;
2727
2728 if (start_address != (bfd_vma) -1
2729 && q->address < start_address)
2730 continue;
2731 if (stop_address != (bfd_vma) -1
2732 && q->address > stop_address)
2733 continue;
2734
2735 if (with_line_numbers
2736 && sec != NULL
2737 && bfd_find_nearest_line (abfd, sec, syms, q->address,
2738 &filename, &functionname, &line))
2739 {
2740 if (functionname != NULL
2741 && (last_functionname == NULL
2742 || strcmp (functionname, last_functionname) != 0))
2743 {
2744 printf ("%s():\n", functionname);
2745 if (last_functionname != NULL)
2746 free (last_functionname);
2747 last_functionname = xstrdup (functionname);
2748 }
98a91d6a 2749
252b5132
RH
2750 if (line > 0
2751 && (line != last_line
2752 || (filename != NULL
2753 && last_filename != NULL
2754 && strcmp (filename, last_filename) != 0)))
2755 {
2756 printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
2757 last_line = line;
2758 if (last_filename != NULL)
2759 free (last_filename);
2760 if (filename == NULL)
2761 last_filename = NULL;
2762 else
2763 last_filename = xstrdup (filename);
2764 }
2765 }
2766
2767 if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
2768 {
2769 sym_name = (*(q->sym_ptr_ptr))->name;
2770 section_name = (*(q->sym_ptr_ptr))->section->name;
2771 }
2772 else
2773 {
2774 sym_name = NULL;
2775 section_name = NULL;
2776 }
98a91d6a 2777
f9ecb0a4
JJ
2778 bfd_printf_vma (abfd, q->address);
2779 if (q->howto == NULL)
2780 printf (" *unknown* ");
2781 else if (q->howto->name)
2782 printf (" %-16s ", q->howto->name);
2783 else
2784 printf (" %-16d ", q->howto->type);
171191ba 2785
252b5132 2786 if (sym_name)
171191ba
NC
2787 {
2788 objdump_print_symname (abfd, NULL, *q->sym_ptr_ptr);
171191ba 2789 }
252b5132
RH
2790 else
2791 {
d3ba0551 2792 if (section_name == NULL)
252b5132 2793 section_name = "*unknown*";
f9ecb0a4 2794 printf ("[%s]", section_name);
252b5132 2795 }
98a91d6a 2796
252b5132
RH
2797 if (q->addend)
2798 {
2799 printf ("+0x");
d8180c76 2800 bfd_printf_vma (abfd, q->addend);
252b5132 2801 }
98a91d6a 2802
252b5132
RH
2803 printf ("\n");
2804 }
2805}
43ac9881 2806
155e0d23 2807static void
3b9ad1cc
AM
2808dump_relocs_in_section (bfd *abfd,
2809 asection *section,
2810 void *dummy ATTRIBUTE_UNUSED)
155e0d23
NC
2811{
2812 arelent **relpp;
2813 long relcount;
2814 long relsize;
2815
2816 if ( bfd_is_abs_section (section)
2817 || bfd_is_und_section (section)
2818 || bfd_is_com_section (section)
2819 || (! process_section_p (section))
2820 || ((section->flags & SEC_RELOC) == 0))
2821 return;
2822
2823 relsize = bfd_get_reloc_upper_bound (abfd, section);
2824 if (relsize < 0)
2825 bfd_fatal (bfd_get_filename (abfd));
2826
2827 printf ("RELOCATION RECORDS FOR [%s]:", section->name);
2828
2829 if (relsize == 0)
2830 {
2831 printf (" (none)\n\n");
2832 return;
2833 }
2834
2835 relpp = xmalloc (relsize);
2836 relcount = bfd_canonicalize_reloc (abfd, section, relpp, syms);
2837
2838 if (relcount < 0)
2839 bfd_fatal (bfd_get_filename (abfd));
2840 else if (relcount == 0)
2841 printf (" (none)\n\n");
2842 else
2843 {
2844 printf ("\n");
2845 dump_reloc_set (abfd, section, relpp, relcount);
2846 printf ("\n\n");
2847 }
2848 free (relpp);
2849}
2850
2851static void
2852dump_relocs (bfd *abfd)
2853{
2854 bfd_map_over_sections (abfd, dump_relocs_in_section, NULL);
2855}
2856
2857static void
2858dump_dynamic_relocs (bfd *abfd)
2859{
2860 long relsize;
2861 arelent **relpp;
2862 long relcount;
2863
2864 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
2865 if (relsize < 0)
2866 bfd_fatal (bfd_get_filename (abfd));
2867
2868 printf ("DYNAMIC RELOCATION RECORDS");
2869
2870 if (relsize == 0)
2871 printf (" (none)\n\n");
2872 else
2873 {
2874 relpp = xmalloc (relsize);
2875 relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
2876
2877 if (relcount < 0)
2878 bfd_fatal (bfd_get_filename (abfd));
2879 else if (relcount == 0)
2880 printf (" (none)\n\n");
2881 else
2882 {
2883 printf ("\n");
2884 dump_reloc_set (abfd, NULL, relpp, relcount);
2885 printf ("\n\n");
2886 }
2887 free (relpp);
2888 }
2889}
2890
43ac9881
AM
2891/* Creates a table of paths, to search for source files. */
2892
2893static void
2894add_include_path (const char *path)
2895{
2896 if (path[0] == 0)
2897 return;
2898 include_path_count++;
2899 include_paths = xrealloc (include_paths,
2900 include_path_count * sizeof (*include_paths));
2901#ifdef HAVE_DOS_BASED_FILE_SYSTEM
2902 if (path[1] == ':' && path[2] == 0)
2903 path = concat (path, ".", (const char *) 0);
2904#endif
2905 include_paths[include_path_count - 1] = path;
2906}
155e0d23
NC
2907
2908static void
3b9ad1cc
AM
2909adjust_addresses (bfd *abfd ATTRIBUTE_UNUSED,
2910 asection *section,
bc79cded 2911 void *arg)
155e0d23 2912{
bc79cded
L
2913 if ((section->flags & SEC_DEBUGGING) == 0)
2914 {
2915 bfd_boolean *has_reloc_p = (bfd_boolean *) arg;
2916 section->vma += adjust_section_vma;
2917 if (*has_reloc_p)
2918 section->lma += adjust_section_vma;
2919 }
155e0d23
NC
2920}
2921
2922/* Dump selected contents of ABFD. */
2923
2924static void
2925dump_bfd (bfd *abfd)
2926{
2927 /* If we are adjusting section VMA's, change them all now. Changing
2928 the BFD information is a hack. However, we must do it, or
2929 bfd_find_nearest_line will not do the right thing. */
2930 if (adjust_section_vma != 0)
bc79cded
L
2931 {
2932 bfd_boolean has_reloc = (abfd->flags & HAS_RELOC);
2933 bfd_map_over_sections (abfd, adjust_addresses, &has_reloc);
2934 }
155e0d23
NC
2935
2936 if (! dump_debugging_tags)
2937 printf (_("\n%s: file format %s\n"), bfd_get_filename (abfd),
2938 abfd->xvec->name);
2939 if (dump_ar_hdrs)
2940 print_arelt_descr (stdout, abfd, TRUE);
2941 if (dump_file_header)
2942 dump_bfd_header (abfd);
2943 if (dump_private_headers)
2944 dump_bfd_private_header (abfd);
2945 if (! dump_debugging_tags)
2946 putchar ('\n');
2947 if (dump_section_headers)
2948 dump_headers (abfd);
2949
365544c3
L
2950 if (dump_symtab
2951 || dump_reloc_info
2952 || disassemble
2953 || dump_debugging
2954 || dump_dwarf_section_info)
155e0d23 2955 syms = slurp_symtab (abfd);
4c45e5c9
JJ
2956 if (dump_dynamic_symtab || dump_dynamic_reloc_info
2957 || (disassemble && bfd_get_dynamic_symtab_upper_bound (abfd) > 0))
155e0d23 2958 dynsyms = slurp_dynamic_symtab (abfd);
90e3cdf2 2959 if (disassemble)
4c45e5c9 2960 {
c9727e01
AM
2961 synthcount = bfd_get_synthetic_symtab (abfd, symcount, syms,
2962 dynsymcount, dynsyms, &synthsyms);
2963 if (synthcount < 0)
2964 synthcount = 0;
4c45e5c9 2965 }
155e0d23
NC
2966
2967 if (dump_symtab)
2968 dump_symbols (abfd, FALSE);
2969 if (dump_dynamic_symtab)
2970 dump_symbols (abfd, TRUE);
365544c3
L
2971 if (dump_dwarf_section_info)
2972 dump_dwarf (abfd);
155e0d23
NC
2973 if (dump_stab_section_info)
2974 dump_stabs (abfd);
2975 if (dump_reloc_info && ! disassemble)
2976 dump_relocs (abfd);
2977 if (dump_dynamic_reloc_info && ! disassemble)
2978 dump_dynamic_relocs (abfd);
2979 if (dump_section_contents)
2980 dump_data (abfd);
2981 if (disassemble)
2982 disassemble_data (abfd);
2983
2984 if (dump_debugging)
2985 {
2986 void *dhandle;
2987
b922d590 2988 dhandle = read_debugging_info (abfd, syms, symcount, TRUE);
155e0d23
NC
2989 if (dhandle != NULL)
2990 {
ed180cc5
AM
2991 if (!print_debugging_info (stdout, dhandle, abfd, syms,
2992 bfd_demangle,
2993 dump_debugging_tags ? TRUE : FALSE))
155e0d23
NC
2994 {
2995 non_fatal (_("%s: printing debugging information failed"),
2996 bfd_get_filename (abfd));
2997 exit_status = 1;
2998 }
2999 }
b922d590
NC
3000 /* PR 6483: If there was no STABS or IEEE debug
3001 info in the file, try DWARF instead. */
3002 else if (! dump_dwarf_section_info)
3003 {
3004 dump_dwarf (abfd);
3005 }
155e0d23
NC
3006 }
3007
3008 if (syms)
3009 {
3010 free (syms);
3011 syms = NULL;
3012 }
3013
3014 if (dynsyms)
3015 {
3016 free (dynsyms);
3017 dynsyms = NULL;
3018 }
4c45e5c9
JJ
3019
3020 if (synthsyms)
3021 {
3022 free (synthsyms);
3023 synthsyms = NULL;
3024 }
3025
3026 symcount = 0;
3027 dynsymcount = 0;
3028 synthcount = 0;
155e0d23
NC
3029}
3030
3031static void
3032display_bfd (bfd *abfd)
3033{
3034 char **matching;
3035
3036 if (bfd_check_format_matches (abfd, bfd_object, &matching))
3037 {
3038 dump_bfd (abfd);
3039 return;
3040 }
3041
3042 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
3043 {
3044 nonfatal (bfd_get_filename (abfd));
3045 list_matching_formats (matching);
3046 free (matching);
3047 return;
3048 }
3049
3050 if (bfd_get_error () != bfd_error_file_not_recognized)
3051 {
3052 nonfatal (bfd_get_filename (abfd));
3053 return;
3054 }
3055
3056 if (bfd_check_format_matches (abfd, bfd_core, &matching))
3057 {
3058 dump_bfd (abfd);
3059 return;
3060 }
3061
3062 nonfatal (bfd_get_filename (abfd));
3063
3064 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
3065 {
3066 list_matching_formats (matching);
3067 free (matching);
3068 }
3069}
3070
3071static void
3072display_file (char *filename, char *target)
3073{
f24ddbdd
NC
3074 bfd *file;
3075 bfd *arfile = NULL;
3076
3077 if (get_file_size (filename) < 1)
d68c385b
NC
3078 {
3079 exit_status = 1;
3080 return;
3081 }
155e0d23
NC
3082
3083 file = bfd_openr (filename, target);
3084 if (file == NULL)
3085 {
3086 nonfatal (filename);
3087 return;
3088 }
3089
3090 /* If the file is an archive, process all of its elements. */
3091 if (bfd_check_format (file, bfd_archive))
3092 {
3093 bfd *last_arfile = NULL;
3094
3095 printf (_("In archive %s:\n"), bfd_get_filename (file));
3096 for (;;)
3097 {
3098 bfd_set_error (bfd_error_no_error);
3099
3100 arfile = bfd_openr_next_archived_file (file, arfile);
3101 if (arfile == NULL)
3102 {
3103 if (bfd_get_error () != bfd_error_no_more_archived_files)
3104 nonfatal (bfd_get_filename (file));
3105 break;
3106 }
3107
3108 display_bfd (arfile);
3109
3110 if (last_arfile != NULL)
3111 bfd_close (last_arfile);
3112 last_arfile = arfile;
3113 }
3114
3115 if (last_arfile != NULL)
3116 bfd_close (last_arfile);
3117 }
3118 else
3119 display_bfd (file);
3120
3121 bfd_close (file);
3122}
252b5132 3123\f
252b5132 3124int
46dca2e0 3125main (int argc, char **argv)
252b5132
RH
3126{
3127 int c;
3128 char *target = default_target;
b34976b6 3129 bfd_boolean seenflag = FALSE;
252b5132 3130
155e0d23
NC
3131#if defined (HAVE_SETLOCALE)
3132#if defined (HAVE_LC_MESSAGES)
252b5132 3133 setlocale (LC_MESSAGES, "");
3882b010 3134#endif
3882b010 3135 setlocale (LC_CTYPE, "");
252b5132 3136#endif
155e0d23 3137
252b5132
RH
3138 bindtextdomain (PACKAGE, LOCALEDIR);
3139 textdomain (PACKAGE);
3140
3141 program_name = *argv;
3142 xmalloc_set_program_name (program_name);
3143
3144 START_PROGRESS (program_name, 0);
3145
869b9d07
MM
3146 expandargv (&argc, &argv);
3147
252b5132
RH
3148 bfd_init ();
3149 set_default_bfd_target ();
3150
98ec6e72 3151 while ((c = getopt_long (argc, argv, "pib:m:M:VvCdDlfFaHhrRtTxsSI:j:wE:zgeGW",
252b5132
RH
3152 long_options, (int *) 0))
3153 != EOF)
3154 {
252b5132
RH
3155 switch (c)
3156 {
3157 case 0:
8b53311e 3158 break; /* We've been given a long option. */
252b5132
RH
3159 case 'm':
3160 machine = optarg;
3161 break;
dd92f639 3162 case 'M':
073fbac6 3163 if (disassembler_options)
31e0f3cd 3164 /* Ignore potential memory leak for now. */
46dca2e0 3165 disassembler_options = concat (disassembler_options, ",",
ee832e18 3166 optarg, (const char *) NULL);
31e0f3cd
NC
3167 else
3168 disassembler_options = optarg;
dd92f639 3169 break;
252b5132 3170 case 'j':
43ac9881 3171 if (only_used == only_size)
6e50c90c
L
3172 {
3173 only_size += 8;
43ac9881 3174 only = xrealloc (only, only_size * sizeof (char *));
6e50c90c
L
3175 }
3176 only [only_used++] = optarg;
252b5132 3177 break;
98ec6e72
NC
3178 case 'F':
3179 display_file_offsets = TRUE;
3180 break;
252b5132 3181 case 'l':
b34976b6 3182 with_line_numbers = TRUE;
252b5132
RH
3183 break;
3184 case 'b':
3185 target = optarg;
3186 break;
1dada9c5 3187 case 'C':
b34976b6 3188 do_demangle = TRUE;
28c309a2
NC
3189 if (optarg != NULL)
3190 {
3191 enum demangling_styles style;
8b53311e 3192
28c309a2 3193 style = cplus_demangle_name_to_style (optarg);
0af11b59 3194 if (style == unknown_demangling)
28c309a2
NC
3195 fatal (_("unknown demangling style `%s'"),
3196 optarg);
8b53311e 3197
28c309a2 3198 cplus_demangle_set_style (style);
0af11b59 3199 }
1dada9c5
NC
3200 break;
3201 case 'w':
b34976b6 3202 wide_output = TRUE;
1dada9c5
NC
3203 break;
3204 case OPTION_ADJUST_VMA:
3205 adjust_section_vma = parse_vma (optarg, "--adjust-vma");
3206 break;
3207 case OPTION_START_ADDRESS:
3208 start_address = parse_vma (optarg, "--start-address");
98ec6e72
NC
3209 if ((stop_address != (bfd_vma) -1) && stop_address <= start_address)
3210 fatal (_("error: the start address should be before the end address"));
1dada9c5
NC
3211 break;
3212 case OPTION_STOP_ADDRESS:
3213 stop_address = parse_vma (optarg, "--stop-address");
98ec6e72
NC
3214 if ((start_address != (bfd_vma) -1) && stop_address <= start_address)
3215 fatal (_("error: the stop address should be after the start address"));
1dada9c5 3216 break;
0dafdf3f
L
3217 case OPTION_PREFIX:
3218 prefix = optarg;
3219 prefix_length = strlen (prefix);
3220 /* Remove an unnecessary trailing '/' */
3221 while (IS_DIR_SEPARATOR (prefix[prefix_length - 1]))
3222 prefix_length--;
3223 break;
3224 case OPTION_PREFIX_STRIP:
3225 prefix_strip = atoi (optarg);
3226 if (prefix_strip < 0)
3227 fatal (_("error: prefix strip must be non-negative"));
3228 break;
1dada9c5
NC
3229 case 'E':
3230 if (strcmp (optarg, "B") == 0)
3231 endian = BFD_ENDIAN_BIG;
3232 else if (strcmp (optarg, "L") == 0)
3233 endian = BFD_ENDIAN_LITTLE;
3234 else
3235 {
37cc8ec1 3236 non_fatal (_("unrecognized -E option"));
1dada9c5
NC
3237 usage (stderr, 1);
3238 }
3239 break;
3240 case OPTION_ENDIAN:
3241 if (strncmp (optarg, "big", strlen (optarg)) == 0)
3242 endian = BFD_ENDIAN_BIG;
3243 else if (strncmp (optarg, "little", strlen (optarg)) == 0)
3244 endian = BFD_ENDIAN_LITTLE;
3245 else
3246 {
37cc8ec1 3247 non_fatal (_("unrecognized --endian type `%s'"), optarg);
1dada9c5
NC
3248 usage (stderr, 1);
3249 }
3250 break;
8b53311e 3251
252b5132 3252 case 'f':
b34976b6
AM
3253 dump_file_header = TRUE;
3254 seenflag = TRUE;
252b5132
RH
3255 break;
3256 case 'i':
b34976b6
AM
3257 formats_info = TRUE;
3258 seenflag = TRUE;
252b5132 3259 break;
43ac9881
AM
3260 case 'I':
3261 add_include_path (optarg);
3262 break;
252b5132 3263 case 'p':
b34976b6
AM
3264 dump_private_headers = TRUE;
3265 seenflag = TRUE;
252b5132
RH
3266 break;
3267 case 'x':
b34976b6
AM
3268 dump_private_headers = TRUE;
3269 dump_symtab = TRUE;
3270 dump_reloc_info = TRUE;
3271 dump_file_header = TRUE;
3272 dump_ar_hdrs = TRUE;
3273 dump_section_headers = TRUE;
3274 seenflag = TRUE;
252b5132
RH
3275 break;
3276 case 't':
b34976b6
AM
3277 dump_symtab = TRUE;
3278 seenflag = TRUE;
252b5132
RH
3279 break;
3280 case 'T':
b34976b6
AM
3281 dump_dynamic_symtab = TRUE;
3282 seenflag = TRUE;
252b5132
RH
3283 break;
3284 case 'd':
b34976b6
AM
3285 disassemble = TRUE;
3286 seenflag = TRUE;
1dada9c5
NC
3287 break;
3288 case 'z':
b34976b6 3289 disassemble_zeroes = TRUE;
252b5132
RH
3290 break;
3291 case 'D':
b34976b6
AM
3292 disassemble = TRUE;
3293 disassemble_all = TRUE;
3294 seenflag = TRUE;
252b5132
RH
3295 break;
3296 case 'S':
b34976b6
AM
3297 disassemble = TRUE;
3298 with_source_code = TRUE;
3299 seenflag = TRUE;
1dada9c5
NC
3300 break;
3301 case 'g':
3302 dump_debugging = 1;
b34976b6 3303 seenflag = TRUE;
1dada9c5 3304 break;
51cdc6e0
NC
3305 case 'e':
3306 dump_debugging = 1;
3307 dump_debugging_tags = 1;
3308 do_demangle = TRUE;
3309 seenflag = TRUE;
3310 break;
365544c3
L
3311 case 'W':
3312 dump_dwarf_section_info = TRUE;
3313 seenflag = TRUE;
3314 do_debug_info = 1;
3315 do_debug_abbrevs = 1;
3316 do_debug_lines = 1;
3317 do_debug_pubnames = 1;
3318 do_debug_aranges = 1;
3319 do_debug_ranges = 1;
3320 do_debug_frames = 1;
3321 do_debug_macinfo = 1;
3322 do_debug_str = 1;
3323 do_debug_loc = 1;
3324 break;
1dada9c5 3325 case 'G':
b34976b6
AM
3326 dump_stab_section_info = TRUE;
3327 seenflag = TRUE;
252b5132
RH
3328 break;
3329 case 's':
b34976b6
AM
3330 dump_section_contents = TRUE;
3331 seenflag = TRUE;
252b5132
RH
3332 break;
3333 case 'r':
b34976b6
AM
3334 dump_reloc_info = TRUE;
3335 seenflag = TRUE;
252b5132
RH
3336 break;
3337 case 'R':
b34976b6
AM
3338 dump_dynamic_reloc_info = TRUE;
3339 seenflag = TRUE;
252b5132
RH
3340 break;
3341 case 'a':
b34976b6
AM
3342 dump_ar_hdrs = TRUE;
3343 seenflag = TRUE;
252b5132
RH
3344 break;
3345 case 'h':
b34976b6
AM
3346 dump_section_headers = TRUE;
3347 seenflag = TRUE;
252b5132
RH
3348 break;
3349 case 'H':
3350 usage (stdout, 0);
b34976b6 3351 seenflag = TRUE;
8b53311e 3352 case 'v':
252b5132 3353 case 'V':
b34976b6
AM
3354 show_version = TRUE;
3355 seenflag = TRUE;
252b5132 3356 break;
0af11b59 3357
252b5132
RH
3358 default:
3359 usage (stderr, 1);
3360 }
3361 }
3362
3363 if (show_version)
3364 print_version ("objdump");
3365
b34976b6 3366 if (!seenflag)
1dada9c5 3367 usage (stderr, 2);
252b5132
RH
3368
3369 if (formats_info)
06d86cf7 3370 exit_status = display_info ();
252b5132
RH
3371 else
3372 {
3373 if (optind == argc)
3374 display_file ("a.out", target);
3375 else
3376 for (; optind < argc;)
3377 display_file (argv[optind++], target);
3378 }
3379
3380 END_PROGRESS (program_name);
3381
75cd796a 3382 return exit_status;
252b5132 3383}