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