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