Fix objdump's display of indirect strings in object files, which was broken by the...
[deliverable/binutils-gdb.git] / binutils / objdump.c
CommitLineData
252b5132 1/* objdump.c -- dump information about an object file.
82704155 2 Copyright (C) 1990-2019 Free Software Foundation, Inc.
252b5132 3
b5e2a4f3 4 This file is part of GNU Binutils.
252b5132 5
b5e2a4f3
NC
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
32866df7 8 the Free Software Foundation; either version 3, or (at your option)
b5e2a4f3 9 any later version.
252b5132 10
b5e2a4f3
NC
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
252b5132 15
b5e2a4f3
NC
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
32866df7
NC
18 Foundation, 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
252b5132 21
155e0d23
NC
22/* Objdump overview.
23
24 Objdump displays information about one or more object files, either on
25 their own, or inside libraries. It is commonly used as a disassembler,
26 but it can also display information about file headers, symbol tables,
27 relocations, debugging directives and more.
28
29 The flow of execution is as follows:
3aade688 30
155e0d23
NC
31 1. Command line arguments are checked for control switches and the
32 information to be displayed is selected.
3aade688 33
155e0d23
NC
34 2. Any remaining arguments are assumed to be object files, and they are
35 processed in order by display_bfd(). If the file is an archive each
36 of its elements is processed in turn.
3aade688 37
50c2245b 38 3. The file's target architecture and binary file format are determined
155e0d23
NC
39 by bfd_check_format(). If they are recognised, then dump_bfd() is
40 called.
41
50c2245b
KH
42 4. dump_bfd() in turn calls separate functions to display the requested
43 item(s) of information(s). For example disassemble_data() is called if
aaad4cf3 44 a disassembly has been requested.
155e0d23
NC
45
46 When disassembling the code loops through blocks of instructions bounded
50c2245b 47 by symbols, calling disassemble_bytes() on each block. The actual
155e0d23
NC
48 disassembling is done by the libopcodes library, via a function pointer
49 supplied by the disassembler() function. */
50
3db64b00 51#include "sysdep.h"
252b5132 52#include "bfd.h"
2dc4cec1 53#include "elf-bfd.h"
f4943d82 54#include "coff-bfd.h"
252b5132
RH
55#include "progress.h"
56#include "bucomm.h"
3284fe0c 57#include "elfcomm.h"
365544c3 58#include "dwarf.h"
7d9813f1 59#include "ctf-api.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"
6abcee90 68#include "objdump.h"
252b5132 69
e8f5eee4
NC
70#ifdef HAVE_MMAP
71#include <sys/mman.h>
72#endif
73
252b5132
RH
74/* Internal headers for the ELF .stab-dump code - sorry. */
75#define BYTES_IN_WORD 32
76#include "aout/aout64.h"
77
75cd796a
ILT
78/* Exit status. */
79static int exit_status = 0;
80
98a91d6a 81static char *default_target = NULL; /* Default at runtime. */
252b5132 82
3b9ad1cc
AM
83/* The following variables are set based on arguments passed on the
84 command line. */
98a91d6a 85static int show_version = 0; /* Show the version number. */
252b5132
RH
86static int dump_section_contents; /* -s */
87static int dump_section_headers; /* -h */
b34976b6 88static bfd_boolean dump_file_header; /* -f */
252b5132
RH
89static int dump_symtab; /* -t */
90static int dump_dynamic_symtab; /* -T */
91static int dump_reloc_info; /* -r */
92static int dump_dynamic_reloc_info; /* -R */
93static int dump_ar_hdrs; /* -a */
94static int dump_private_headers; /* -p */
6abcee90 95static char *dump_private_options; /* -P */
252b5132
RH
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 101static int dump_stab_section_info; /* --stabs */
7d9813f1
NA
102static int dump_ctf_section_info; /* --ctf */
103static char *dump_ctf_section_name;
104static char *dump_ctf_parent_name; /* --ctf-parent */
252b5132 105static int do_demangle; /* -C, --demangle */
b34976b6
AM
106static bfd_boolean disassemble; /* -d */
107static bfd_boolean disassemble_all; /* -D */
252b5132 108static int disassemble_zeroes; /* --disassemble-zeroes */
b34976b6 109static bfd_boolean formats_info; /* -i */
252b5132 110static int wide_output; /* -w */
3dcb3fcb 111static int insn_width; /* --insn-width */
252b5132
RH
112static bfd_vma start_address = (bfd_vma) -1; /* --start-address */
113static bfd_vma stop_address = (bfd_vma) -1; /* --stop-address */
114static int dump_debugging; /* --debugging */
51cdc6e0 115static int dump_debugging_tags; /* --debugging-tags */
fd2f0033 116static int suppress_bfd_header;
3c9458e9 117static int dump_special_syms = 0; /* --special-syms */
252b5132 118static bfd_vma adjust_section_vma = 0; /* --adjust-vma */
f1563258 119static int file_start_context = 0; /* --file-start-context */
98ec6e72 120static bfd_boolean display_file_offsets;/* -F */
0dafdf3f
L
121static const char *prefix; /* --prefix */
122static int prefix_strip; /* --prefix-strip */
123static size_t prefix_length;
4a14e306 124static bfd_boolean unwind_inlines; /* --inlines. */
d3def5d7 125static const char * disasm_sym; /* Disassembly start symbol. */
252b5132 126
af03af8f
NC
127static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
128
70ecb384
NC
129/* A structure to record the sections mentioned in -j switches. */
130struct only
131{
132 const char * name; /* The name of the section. */
133 bfd_boolean seen; /* A flag to indicate that the section has been found in one or more input files. */
134 struct only * next; /* Pointer to the next structure in the list. */
135};
136/* Pointer to an array of 'only' structures.
137 This pointer is NULL if the -j switch has not been used. */
138static struct only * only_list = NULL;
155e0d23 139
43ac9881
AM
140/* Variables for handling include file path table. */
141static const char **include_paths;
142static int include_path_count;
143
3b9ad1cc
AM
144/* Extra info to pass to the section disassembler and address printing
145 function. */
026df7c5
NC
146struct objdump_disasm_info
147{
155e0d23
NC
148 bfd * abfd;
149 asection * sec;
150 bfd_boolean require_sec;
151 arelent ** dynrelbuf;
152 long dynrelcount;
153 disassembler_ftype disassemble_fn;
ce04548a 154 arelent * reloc;
d3def5d7 155 const char * symbol;
252b5132
RH
156};
157
158/* Architecture to disassemble for, or default if NULL. */
d3ba0551 159static char *machine = NULL;
252b5132 160
dd92f639 161/* Target specific options to the disassembler. */
d3ba0551 162static char *disassembler_options = NULL;
dd92f639 163
252b5132
RH
164/* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */
165static enum bfd_endian endian = BFD_ENDIAN_UNKNOWN;
166
167/* The symbol table. */
168static asymbol **syms;
169
170/* Number of symbols in `syms'. */
171static long symcount = 0;
172
173/* The sorted symbol table. */
174static asymbol **sorted_syms;
175
176/* Number of symbols in `sorted_syms'. */
177static long sorted_symcount = 0;
178
179/* The dynamic symbol table. */
180static asymbol **dynsyms;
181
4c45e5c9
JJ
182/* The synthetic symbol table. */
183static asymbol *synthsyms;
184static long synthcount = 0;
185
252b5132
RH
186/* Number of symbols in `dynsyms'. */
187static long dynsymcount = 0;
188
98a91d6a
NC
189static bfd_byte *stabs;
190static bfd_size_type stab_size;
191
bae7501e 192static bfd_byte *strtab;
98a91d6a 193static bfd_size_type stabstr_size;
41e92641 194
6abcee90
TG
195/* Handlers for -P/--private. */
196static const struct objdump_private_desc * const objdump_private_vectors[] =
197 {
198 OBJDUMP_PRIVATE_VECTORS
199 NULL
200 };
252b5132 201\f
aebcf7b7 202static void usage (FILE *, int) ATTRIBUTE_NORETURN;
252b5132 203static void
46dca2e0 204usage (FILE *stream, int status)
252b5132 205{
8b53311e
NC
206 fprintf (stream, _("Usage: %s <option(s)> <file(s)>\n"), program_name);
207 fprintf (stream, _(" Display information from object <file(s)>.\n"));
208 fprintf (stream, _(" At least one of the following switches must be given:\n"));
252b5132 209 fprintf (stream, _("\
86d65c94
MK
210 -a, --archive-headers Display archive header information\n\
211 -f, --file-headers Display the contents of the overall file header\n\
212 -p, --private-headers Display object format specific file header contents\n\
6abcee90 213 -P, --private=OPT,OPT... Display object format specific contents\n\
86d65c94
MK
214 -h, --[section-]headers Display the contents of the section headers\n\
215 -x, --all-headers Display the contents of all headers\n\
216 -d, --disassemble Display assembler contents of executable sections\n\
217 -D, --disassemble-all Display assembler contents of all sections\n\
d3def5d7 218 --disassemble=<sym> Display assembler contents from <sym>\n\
86d65c94
MK
219 -S, --source Intermix source code with disassembly\n\
220 -s, --full-contents Display the full contents of all sections requested\n\
221 -g, --debugging Display debug information in object file\n\
51cdc6e0 222 -e, --debugging-tags Display debug information using ctags style\n\
86d65c94 223 -G, --stabs Display (in raw form) any STABS info in the file\n\
dda8d76d 224 -W[lLiaprmfFsoRtUuTgAckK] or\n\
1ed06042 225 --dwarf[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,\n\
6f875884 226 =frames-interp,=str,=loc,=Ranges,=pubtypes,\n\
657d0d47 227 =gdb_index,=trace_info,=trace_abbrev,=trace_aranges,\n\
dda8d76d 228 =addr,=cu_index,=links,=follow-links]\n\
79b377b3
NC
229 Display DWARF info in the file\n\
230 --ctf=SECTION Display CTF info from SECTION\n\
86d65c94
MK
231 -t, --syms Display the contents of the symbol table(s)\n\
232 -T, --dynamic-syms Display the contents of the dynamic symbol table\n\
233 -r, --reloc Display the relocation entries in the file\n\
234 -R, --dynamic-reloc Display the dynamic relocation entries in the file\n\
07012eee 235 @<file> Read options from <file>\n\
8b53311e 236 -v, --version Display this program's version number\n\
86d65c94
MK
237 -i, --info List object formats and architectures supported\n\
238 -H, --help Display this information\n\
1dada9c5
NC
239"));
240 if (status != 2)
241 {
6abcee90
TG
242 const struct objdump_private_desc * const *desc;
243
1dada9c5
NC
244 fprintf (stream, _("\n The following switches are optional:\n"));
245 fprintf (stream, _("\
86d65c94
MK
246 -b, --target=BFDNAME Specify the target object format as BFDNAME\n\
247 -m, --architecture=MACHINE Specify the target architecture as MACHINE\n\
248 -j, --section=NAME Only display information for section NAME\n\
249 -M, --disassembler-options=OPT Pass text OPT on to the disassembler\n\
1dada9c5
NC
250 -EB --endian=big Assume big endian format when disassembling\n\
251 -EL --endian=little Assume little endian format when disassembling\n\
f1563258 252 --file-start-context Include context from start of file (with -S)\n\
43ac9881 253 -I, --include=DIR Add DIR to search list for source files\n\
86d65c94 254 -l, --line-numbers Include line numbers and filenames in output\n\
98ec6e72 255 -F, --file-offsets Include file offsets when displaying information\n\
28c309a2 256 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n\
f0c8c24a
NC
257 The STYLE, if specified, can be `auto', `gnu',\n\
258 `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
259 or `gnat'\n\
af03af8f
NC
260 --recurse-limit Enable a limit on recursion whilst demangling. [Default]\n\
261 --no-recurse-limit Disable a limit on recursion whilst demangling\n\
86d65c94
MK
262 -w, --wide Format output for more than 80 columns\n\
263 -z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling\n\
f0c8c24a
NC
264 --start-address=ADDR Only process data whose address is >= ADDR\n\
265 --stop-address=ADDR Only process data whose address is <= ADDR\n\
1dada9c5
NC
266 --prefix-addresses Print complete address alongside disassembly\n\
267 --[no-]show-raw-insn Display hex alongside symbolic disassembly\n\
505f1412 268 --insn-width=WIDTH Display WIDTH bytes on a single line for -d\n\
86d65c94 269 --adjust-vma=OFFSET Add OFFSET to all displayed section addresses\n\
3c9458e9 270 --special-syms Include special symbols in symbol dumps\n\
4a14e306 271 --inlines Print all inlines for source line (with -l)\n\
0dafdf3f 272 --prefix=PREFIX Add PREFIX to absolute paths for -S\n\
fd2f0033
TT
273 --prefix-strip=LEVEL Strip initial directory names for -S\n"));
274 fprintf (stream, _("\
275 --dwarf-depth=N Do not display DIEs at depth N or greater\n\
276 --dwarf-start=N Display DIEs starting with N, at the same depth\n\
4723351a
CC
277 or deeper\n\
278 --dwarf-check Make additional dwarf internal consistency checks.\
79b377b3 279 \n\
7d9813f1 280 --ctf-parent=SECTION Use SECTION as the CTF parent\n\n"));
1dada9c5 281 list_supported_targets (program_name, stream);
2f83960e 282 list_supported_architectures (program_name, stream);
86d65c94 283
94470b23 284 disassembler_usage (stream);
6abcee90
TG
285
286 if (objdump_private_vectors[0] != NULL)
287 {
288 fprintf (stream,
289 _("\nOptions supported for -P/--private switch:\n"));
290 for (desc = objdump_private_vectors; *desc != NULL; desc++)
291 (*desc)->help (stream);
292 }
1dada9c5 293 }
92f01d61 294 if (REPORT_BUGS_TO[0] && status == 0)
86d65c94 295 fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
252b5132
RH
296 exit (status);
297}
298
299/* 150 isn't special; it's just an arbitrary non-ASCII char value. */
46dca2e0
NC
300enum option_values
301 {
302 OPTION_ENDIAN=150,
303 OPTION_START_ADDRESS,
304 OPTION_STOP_ADDRESS,
4cb93e3b 305 OPTION_DWARF,
0dafdf3f
L
306 OPTION_PREFIX,
307 OPTION_PREFIX_STRIP,
3dcb3fcb 308 OPTION_INSN_WIDTH,
fd2f0033
TT
309 OPTION_ADJUST_VMA,
310 OPTION_DWARF_DEPTH,
4723351a 311 OPTION_DWARF_CHECK,
4a14e306 312 OPTION_DWARF_START,
af03af8f
NC
313 OPTION_RECURSE_LIMIT,
314 OPTION_NO_RECURSE_LIMIT,
79b377b3 315 OPTION_INLINES,
7d9813f1 316 OPTION_CTF,
79b377b3 317 OPTION_CTF_PARENT
46dca2e0 318 };
252b5132
RH
319
320static struct option long_options[]=
321{
322 {"adjust-vma", required_argument, NULL, OPTION_ADJUST_VMA},
323 {"all-headers", no_argument, NULL, 'x'},
324 {"private-headers", no_argument, NULL, 'p'},
6abcee90 325 {"private", required_argument, NULL, 'P'},
252b5132
RH
326 {"architecture", required_argument, NULL, 'm'},
327 {"archive-headers", no_argument, NULL, 'a'},
1dada9c5 328 {"debugging", no_argument, NULL, 'g'},
51cdc6e0 329 {"debugging-tags", no_argument, NULL, 'e'},
28c309a2 330 {"demangle", optional_argument, NULL, 'C'},
d3def5d7 331 {"disassemble", optional_argument, NULL, 'd'},
252b5132 332 {"disassemble-all", no_argument, NULL, 'D'},
dd92f639 333 {"disassembler-options", required_argument, NULL, 'M'},
1dada9c5 334 {"disassemble-zeroes", no_argument, NULL, 'z'},
252b5132
RH
335 {"dynamic-reloc", no_argument, NULL, 'R'},
336 {"dynamic-syms", no_argument, NULL, 'T'},
337 {"endian", required_argument, NULL, OPTION_ENDIAN},
338 {"file-headers", no_argument, NULL, 'f'},
98ec6e72 339 {"file-offsets", no_argument, NULL, 'F'},
f1563258 340 {"file-start-context", no_argument, &file_start_context, 1},
252b5132
RH
341 {"full-contents", no_argument, NULL, 's'},
342 {"headers", no_argument, NULL, 'h'},
343 {"help", no_argument, NULL, 'H'},
344 {"info", no_argument, NULL, 'i'},
345 {"line-numbers", no_argument, NULL, 'l'},
346 {"no-show-raw-insn", no_argument, &show_raw_insn, -1},
347 {"prefix-addresses", no_argument, &prefix_addresses, 1},
af03af8f
NC
348 {"recurse-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
349 {"recursion-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
350 {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
351 {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
252b5132
RH
352 {"reloc", no_argument, NULL, 'r'},
353 {"section", required_argument, NULL, 'j'},
354 {"section-headers", no_argument, NULL, 'h'},
355 {"show-raw-insn", no_argument, &show_raw_insn, 1},
356 {"source", no_argument, NULL, 'S'},
3c9458e9 357 {"special-syms", no_argument, &dump_special_syms, 1},
43ac9881 358 {"include", required_argument, NULL, 'I'},
4cb93e3b 359 {"dwarf", optional_argument, NULL, OPTION_DWARF},
7d9813f1
NA
360 {"ctf", required_argument, NULL, OPTION_CTF},
361 {"ctf-parent", required_argument, NULL, OPTION_CTF_PARENT},
1dada9c5 362 {"stabs", no_argument, NULL, 'G'},
252b5132
RH
363 {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
364 {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
365 {"syms", no_argument, NULL, 't'},
366 {"target", required_argument, NULL, 'b'},
1dada9c5
NC
367 {"version", no_argument, NULL, 'V'},
368 {"wide", no_argument, NULL, 'w'},
0dafdf3f
L
369 {"prefix", required_argument, NULL, OPTION_PREFIX},
370 {"prefix-strip", required_argument, NULL, OPTION_PREFIX_STRIP},
3dcb3fcb 371 {"insn-width", required_argument, NULL, OPTION_INSN_WIDTH},
dda8d76d
NC
372 {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
373 {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
374 {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
375 {"inlines", no_argument, 0, OPTION_INLINES},
252b5132
RH
376 {0, no_argument, 0, 0}
377};
378\f
379static void
46dca2e0 380nonfatal (const char *msg)
75cd796a
ILT
381{
382 bfd_nonfatal (msg);
383 exit_status = 1;
384}
12add40e
NC
385
386/* Returns a version of IN with any control characters
387 replaced by escape sequences. Uses a static buffer
388 if necessary. */
389
390static const char *
391sanitize_string (const char * in)
392{
63455780
NC
393 static char * buffer = NULL;
394 static size_t buffer_len = 0;
395 const char * original = in;
396 char * out;
12add40e
NC
397
398 /* Paranoia. */
399 if (in == NULL)
400 return "";
401
402 /* See if any conversion is necessary. In the majority
403 of cases it will not be needed. */
404 do
405 {
406 char c = *in++;
407
408 if (c == 0)
409 return original;
410
411 if (ISCNTRL (c))
412 break;
413 }
414 while (1);
415
416 /* Copy the input, translating as needed. */
417 in = original;
418 if (buffer_len < (strlen (in) * 2))
419 {
420 free ((void *) buffer);
421 buffer_len = strlen (in) * 2;
422 buffer = xmalloc (buffer_len + 1);
423 }
424
425 out = buffer;
426 do
427 {
428 char c = *in++;
429
430 if (c == 0)
431 break;
432
433 if (!ISCNTRL (c))
434 *out++ = c;
435 else
436 {
437 *out++ = '^';
438 *out++ = c + 0x40;
439 }
440 }
441 while (1);
442
443 *out = 0;
444 return buffer;
445}
446
75cd796a 447\f
d2fcac5c
NC
448/* Returns TRUE if the specified section should be dumped. */
449
450static bfd_boolean
451process_section_p (asection * section)
452{
70ecb384 453 struct only * only;
d2fcac5c 454
70ecb384 455 if (only_list == NULL)
d2fcac5c
NC
456 return TRUE;
457
70ecb384
NC
458 for (only = only_list; only; only = only->next)
459 if (strcmp (only->name, section->name) == 0)
460 {
461 only->seen = TRUE;
462 return TRUE;
463 }
d2fcac5c
NC
464
465 return FALSE;
466}
70ecb384
NC
467
468/* Add an entry to the 'only' list. */
469
470static void
471add_only (char * name)
472{
473 struct only * only;
474
475 /* First check to make sure that we do not
476 already have an entry for this name. */
477 for (only = only_list; only; only = only->next)
478 if (strcmp (only->name, name) == 0)
479 return;
480
481 only = xmalloc (sizeof * only);
482 only->name = name;
483 only->seen = FALSE;
484 only->next = only_list;
485 only_list = only;
486}
487
488/* Release the memory used by the 'only' list.
489 PR 11225: Issue a warning message for unseen sections.
490 Only do this if none of the sections were seen. This is mainly to support
491 tools like the GAS testsuite where an object file is dumped with a list of
492 generic section names known to be present in a range of different file
493 formats. */
494
495static void
496free_only_list (void)
497{
498 bfd_boolean at_least_one_seen = FALSE;
499 struct only * only;
500 struct only * next;
501
502 if (only_list == NULL)
503 return;
504
505 for (only = only_list; only; only = only->next)
506 if (only->seen)
507 {
508 at_least_one_seen = TRUE;
509 break;
510 }
511
512 for (only = only_list; only; only = next)
513 {
514 if (! at_least_one_seen)
515 {
a8c62f1c
AM
516 non_fatal (_("section '%s' mentioned in a -j option, "
517 "but not found in any input file"),
70ecb384
NC
518 only->name);
519 exit_status = 1;
520 }
521 next = only->next;
522 free (only);
523 }
524}
525
d2fcac5c 526\f
75cd796a 527static void
1737c640 528dump_section_header (bfd *abfd, asection *section, void *data)
252b5132
RH
529{
530 char *comma = "";
f6af82bd 531 unsigned int opb = bfd_octets_per_byte (abfd);
1737c640 532 int longest_section_name = *((int *) data);
252b5132 533
3bee8bcd
L
534 /* Ignore linker created section. See elfNN_ia64_object_p in
535 bfd/elfxx-ia64.c. */
536 if (section->flags & SEC_LINKER_CREATED)
537 return;
538
d2fcac5c
NC
539 /* PR 10413: Skip sections that we are ignoring. */
540 if (! process_section_p (section))
541 return;
542
1737c640 543 printf ("%3d %-*s %08lx ", section->index, longest_section_name,
12add40e 544 sanitize_string (bfd_get_section_name (abfd, section)),
940b2b78 545 (unsigned long) bfd_section_size (abfd, section) / opb);
d8180c76 546 bfd_printf_vma (abfd, bfd_get_section_vma (abfd, section));
252b5132 547 printf (" ");
d8180c76 548 bfd_printf_vma (abfd, section->lma);
e59b4dfb 549 printf (" %08lx 2**%u", (unsigned long) section->filepos,
252b5132
RH
550 bfd_get_section_alignment (abfd, section));
551 if (! wide_output)
552 printf ("\n ");
553 printf (" ");
554
555#define PF(x, y) \
556 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
557
558 PF (SEC_HAS_CONTENTS, "CONTENTS");
559 PF (SEC_ALLOC, "ALLOC");
560 PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
252b5132
RH
561 PF (SEC_LOAD, "LOAD");
562 PF (SEC_RELOC, "RELOC");
252b5132
RH
563 PF (SEC_READONLY, "READONLY");
564 PF (SEC_CODE, "CODE");
565 PF (SEC_DATA, "DATA");
566 PF (SEC_ROM, "ROM");
567 PF (SEC_DEBUGGING, "DEBUGGING");
568 PF (SEC_NEVER_LOAD, "NEVER_LOAD");
569 PF (SEC_EXCLUDE, "EXCLUDE");
570 PF (SEC_SORT_ENTRIES, "SORT_ENTRIES");
ebe372c1
L
571 if (bfd_get_arch (abfd) == bfd_arch_tic54x)
572 {
573 PF (SEC_TIC54X_BLOCK, "BLOCK");
574 PF (SEC_TIC54X_CLINK, "CLINK");
575 }
24c411ed 576 PF (SEC_SMALL_DATA, "SMALL_DATA");
ebe372c1 577 if (bfd_get_flavour (abfd) == bfd_target_coff_flavour)
91f68a68
MG
578 {
579 PF (SEC_COFF_SHARED, "SHARED");
580 PF (SEC_COFF_NOREAD, "NOREAD");
581 }
582 else if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
f0728ee3 583 PF (SEC_ELF_PURECODE, "PURECODE");
13ae64f3 584 PF (SEC_THREAD_LOCAL, "THREAD_LOCAL");
64c1196b 585 PF (SEC_GROUP, "GROUP");
91f68a68
MG
586 if (bfd_get_arch (abfd) == bfd_arch_mep)
587 {
588 PF (SEC_MEP_VLIW, "VLIW");
589 }
252b5132
RH
590
591 if ((section->flags & SEC_LINK_ONCE) != 0)
592 {
593 const char *ls;
082b7297 594 struct coff_comdat_info *comdat;
252b5132
RH
595
596 switch (section->flags & SEC_LINK_DUPLICATES)
597 {
598 default:
599 abort ();
600 case SEC_LINK_DUPLICATES_DISCARD:
601 ls = "LINK_ONCE_DISCARD";
602 break;
603 case SEC_LINK_DUPLICATES_ONE_ONLY:
604 ls = "LINK_ONCE_ONE_ONLY";
605 break;
606 case SEC_LINK_DUPLICATES_SAME_SIZE:
607 ls = "LINK_ONCE_SAME_SIZE";
608 break;
609 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
610 ls = "LINK_ONCE_SAME_CONTENTS";
611 break;
612 }
613 printf ("%s%s", comma, ls);
deecf979 614
082b7297
L
615 comdat = bfd_coff_get_comdat_section (abfd, section);
616 if (comdat != NULL)
617 printf (" (COMDAT %s %ld)", comdat->name, comdat->symbol);
deecf979 618
252b5132
RH
619 comma = ", ";
620 }
621
622 printf ("\n");
623#undef PF
624}
625
1737c640
AB
626/* Called on each SECTION in ABFD, update the int variable pointed to by
627 DATA which contains the string length of the longest section name. */
628
629static void
630find_longest_section_name (bfd *abfd, asection *section, void *data)
631{
632 int *longest_so_far = (int *) data;
633 const char *name;
634 int len;
635
636 /* Ignore linker created section. */
637 if (section->flags & SEC_LINKER_CREATED)
638 return;
639
640 /* Skip sections that we are ignoring. */
641 if (! process_section_p (section))
642 return;
643
644 name = bfd_get_section_name (abfd, section);
645 len = (int) strlen (name);
646 if (len > *longest_so_far)
647 *longest_so_far = len;
648}
649
252b5132 650static void
46dca2e0 651dump_headers (bfd *abfd)
252b5132 652{
1737c640
AB
653 /* The default width of 13 is just an arbitrary choice. */
654 int max_section_name_length = 13;
655 int bfd_vma_width;
8bea4d5c 656
252b5132 657#ifndef BFD64
1737c640 658 bfd_vma_width = 10;
252b5132 659#else
21611032
TS
660 /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */
661 if (bfd_get_arch_size (abfd) == 32)
1737c640 662 bfd_vma_width = 10;
21611032 663 else
1737c640 664 bfd_vma_width = 18;
252b5132 665#endif
8bea4d5c 666
1737c640
AB
667 printf (_("Sections:\n"));
668
669 if (wide_output)
670 bfd_map_over_sections (abfd, find_longest_section_name,
671 &max_section_name_length);
672
673 printf (_("Idx %-*s Size %-*s%-*sFile off Algn"),
674 max_section_name_length, "Name",
675 bfd_vma_width, "VMA",
676 bfd_vma_width, "LMA");
677
8bea4d5c
ILT
678 if (wide_output)
679 printf (_(" Flags"));
680 printf ("\n");
681
1737c640
AB
682 bfd_map_over_sections (abfd, dump_section_header,
683 &max_section_name_length);
252b5132
RH
684}
685\f
686static asymbol **
46dca2e0 687slurp_symtab (bfd *abfd)
252b5132 688{
d3ba0551 689 asymbol **sy = NULL;
252b5132
RH
690 long storage;
691
692 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
693 {
252b5132
RH
694 symcount = 0;
695 return NULL;
696 }
697
698 storage = bfd_get_symtab_upper_bound (abfd);
699 if (storage < 0)
5a3f568b
NC
700 {
701 non_fatal (_("failed to read symbol table from: %s"), bfd_get_filename (abfd));
702 bfd_fatal (_("error message was"));
703 }
252b5132 704 if (storage)
781152ec
NC
705 {
706 off_t filesize = bfd_get_file_size (abfd);
707
708 /* qv PR 24707. */
7e56c51c
NC
709 if (filesize > 0
710 && filesize < storage
711 /* The MMO file format supports its own special compression
712 technique, so its sections can be larger than the file size. */
713 && bfd_get_flavour (abfd) != bfd_target_mmo_flavour)
781152ec
NC
714 {
715 bfd_nonfatal_message (bfd_get_filename (abfd), abfd, NULL,
716 _("error: symbol table size (%#lx) is larger than filesize (%#lx)"),
717 storage, (long) filesize);
718 exit_status = 1;
719 symcount = 0;
720 return NULL;
721 }
722
723 sy = (asymbol **) xmalloc (storage);
724 }
28b18af1 725
252b5132
RH
726 symcount = bfd_canonicalize_symtab (abfd, sy);
727 if (symcount < 0)
728 bfd_fatal (bfd_get_filename (abfd));
252b5132
RH
729 return sy;
730}
731
732/* Read in the dynamic symbols. */
733
734static asymbol **
46dca2e0 735slurp_dynamic_symtab (bfd *abfd)
252b5132 736{
d3ba0551 737 asymbol **sy = NULL;
252b5132
RH
738 long storage;
739
740 storage = bfd_get_dynamic_symtab_upper_bound (abfd);
741 if (storage < 0)
742 {
743 if (!(bfd_get_file_flags (abfd) & DYNAMIC))
744 {
37cc8ec1 745 non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd));
a8c62f1c 746 exit_status = 1;
252b5132
RH
747 dynsymcount = 0;
748 return NULL;
749 }
750
751 bfd_fatal (bfd_get_filename (abfd));
752 }
252b5132 753 if (storage)
3f5e193b 754 sy = (asymbol **) xmalloc (storage);
28b18af1 755
252b5132
RH
756 dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
757 if (dynsymcount < 0)
758 bfd_fatal (bfd_get_filename (abfd));
252b5132
RH
759 return sy;
760}
761
a24bb4f0
NC
762/* Some symbol names are significant and should be kept in the
763 table of sorted symbol names, even if they are marked as
764 debugging/section symbols. */
765
766static bfd_boolean
767is_significant_symbol_name (const char * name)
768{
0e70b27b 769 return strncmp (name, ".plt", 4) == 0 || strcmp (name, ".got") == 0;
a24bb4f0
NC
770}
771
252b5132
RH
772/* Filter out (in place) symbols that are useless for disassembly.
773 COUNT is the number of elements in SYMBOLS.
0af11b59 774 Return the number of useful symbols. */
252b5132
RH
775
776static long
46dca2e0 777remove_useless_symbols (asymbol **symbols, long count)
252b5132 778{
46dca2e0 779 asymbol **in_ptr = symbols, **out_ptr = symbols;
252b5132
RH
780
781 while (--count >= 0)
782 {
783 asymbol *sym = *in_ptr++;
784
785 if (sym->name == NULL || sym->name[0] == '\0')
786 continue;
a24bb4f0
NC
787 if ((sym->flags & (BSF_DEBUGGING | BSF_SECTION_SYM))
788 && ! is_significant_symbol_name (sym->name))
252b5132
RH
789 continue;
790 if (bfd_is_und_section (sym->section)
791 || bfd_is_com_section (sym->section))
792 continue;
793
794 *out_ptr++ = sym;
795 }
796 return out_ptr - symbols;
797}
798
799/* Sort symbols into value order. */
800
0af11b59 801static int
46dca2e0 802compare_symbols (const void *ap, const void *bp)
252b5132 803{
46dca2e0
NC
804 const asymbol *a = * (const asymbol **) ap;
805 const asymbol *b = * (const asymbol **) bp;
806 const char *an;
807 const char *bn;
808 size_t anl;
809 size_t bnl;
810 bfd_boolean af;
811 bfd_boolean bf;
812 flagword aflags;
813 flagword bflags;
252b5132
RH
814
815 if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
816 return 1;
817 else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
818 return -1;
819
820 if (a->section > b->section)
821 return 1;
822 else if (a->section < b->section)
823 return -1;
824
825 an = bfd_asymbol_name (a);
826 bn = bfd_asymbol_name (b);
827 anl = strlen (an);
828 bnl = strlen (bn);
829
830 /* The symbols gnu_compiled and gcc2_compiled convey no real
831 information, so put them after other symbols with the same value. */
252b5132
RH
832 af = (strstr (an, "gnu_compiled") != NULL
833 || strstr (an, "gcc2_compiled") != NULL);
834 bf = (strstr (bn, "gnu_compiled") != NULL
835 || strstr (bn, "gcc2_compiled") != NULL);
836
837 if (af && ! bf)
838 return 1;
839 if (! af && bf)
840 return -1;
841
842 /* We use a heuristic for the file name, to try to sort it after
843 more useful symbols. It may not work on non Unix systems, but it
844 doesn't really matter; the only difference is precisely which
845 symbol names get printed. */
846
847#define file_symbol(s, sn, snl) \
848 (((s)->flags & BSF_FILE) != 0 \
849 || ((sn)[(snl) - 2] == '.' \
850 && ((sn)[(snl) - 1] == 'o' \
851 || (sn)[(snl) - 1] == 'a')))
852
853 af = file_symbol (a, an, anl);
854 bf = file_symbol (b, bn, bnl);
855
856 if (af && ! bf)
857 return 1;
858 if (! af && bf)
859 return -1;
860
861 /* Try to sort global symbols before local symbols before function
862 symbols before debugging symbols. */
863
864 aflags = a->flags;
865 bflags = b->flags;
866
867 if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING))
868 {
869 if ((aflags & BSF_DEBUGGING) != 0)
870 return 1;
871 else
872 return -1;
873 }
874 if ((aflags & BSF_FUNCTION) != (bflags & BSF_FUNCTION))
875 {
876 if ((aflags & BSF_FUNCTION) != 0)
877 return -1;
878 else
879 return 1;
880 }
881 if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL))
882 {
883 if ((aflags & BSF_LOCAL) != 0)
884 return 1;
885 else
886 return -1;
887 }
888 if ((aflags & BSF_GLOBAL) != (bflags & BSF_GLOBAL))
889 {
890 if ((aflags & BSF_GLOBAL) != 0)
891 return -1;
892 else
893 return 1;
894 }
895
32a0481f
AM
896 if (bfd_get_flavour (bfd_asymbol_bfd (a)) == bfd_target_elf_flavour
897 && bfd_get_flavour (bfd_asymbol_bfd (b)) == bfd_target_elf_flavour)
898 {
899 bfd_vma asz, bsz;
900
901 asz = 0;
160b1a61 902 if ((a->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0)
32a0481f
AM
903 asz = ((elf_symbol_type *) a)->internal_elf_sym.st_size;
904 bsz = 0;
160b1a61 905 if ((b->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0)
32a0481f
AM
906 bsz = ((elf_symbol_type *) b)->internal_elf_sym.st_size;
907 if (asz != bsz)
908 return asz > bsz ? -1 : 1;
909 }
910
252b5132
RH
911 /* Symbols that start with '.' might be section names, so sort them
912 after symbols that don't start with '.'. */
913 if (an[0] == '.' && bn[0] != '.')
914 return 1;
915 if (an[0] != '.' && bn[0] == '.')
916 return -1;
917
918 /* Finally, if we can't distinguish them in any other way, try to
919 get consistent results by sorting the symbols by name. */
920 return strcmp (an, bn);
921}
922
923/* Sort relocs into address order. */
924
925static int
46dca2e0 926compare_relocs (const void *ap, const void *bp)
252b5132 927{
46dca2e0
NC
928 const arelent *a = * (const arelent **) ap;
929 const arelent *b = * (const arelent **) bp;
252b5132
RH
930
931 if (a->address > b->address)
932 return 1;
933 else if (a->address < b->address)
934 return -1;
935
936 /* So that associated relocations tied to the same address show up
937 in the correct order, we don't do any further sorting. */
938 if (a > b)
939 return 1;
940 else if (a < b)
941 return -1;
942 else
943 return 0;
944}
945
155e0d23
NC
946/* Print an address (VMA) to the output stream in INFO.
947 If SKIP_ZEROES is TRUE, omit leading zeroes. */
252b5132
RH
948
949static void
91d6fa6a 950objdump_print_value (bfd_vma vma, struct disassemble_info *inf,
46dca2e0 951 bfd_boolean skip_zeroes)
252b5132
RH
952{
953 char buf[30];
954 char *p;
3b9ad1cc 955 struct objdump_disasm_info *aux;
252b5132 956
91d6fa6a 957 aux = (struct objdump_disasm_info *) inf->application_data;
d8180c76 958 bfd_sprintf_vma (aux->abfd, buf, vma);
252b5132
RH
959 if (! skip_zeroes)
960 p = buf;
961 else
962 {
963 for (p = buf; *p == '0'; ++p)
964 ;
965 if (*p == '\0')
966 --p;
967 }
91d6fa6a 968 (*inf->fprintf_func) (inf->stream, "%s", p);
252b5132
RH
969}
970
971/* Print the name of a symbol. */
972
973static void
91d6fa6a 974objdump_print_symname (bfd *abfd, struct disassemble_info *inf,
46dca2e0 975 asymbol *sym)
252b5132
RH
976{
977 char *alloc;
bb4d2ac2
L
978 const char *name, *version_string = NULL;
979 bfd_boolean hidden = FALSE;
252b5132
RH
980
981 alloc = NULL;
982 name = bfd_asymbol_name (sym);
a6637ec0 983 if (do_demangle && name[0] != '\0')
252b5132
RH
984 {
985 /* Demangle the name. */
af03af8f 986 alloc = bfd_demangle (abfd, name, demangle_flags);
ed180cc5
AM
987 if (alloc != NULL)
988 name = alloc;
252b5132
RH
989 }
990
160b1a61 991 if ((sym->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0)
f2b2af2c 992 version_string = bfd_get_symbol_version_string (abfd, sym, &hidden);
bb4d2ac2
L
993
994 if (bfd_is_und_section (bfd_get_section (sym)))
995 hidden = TRUE;
996
12add40e
NC
997 name = sanitize_string (name);
998
91d6fa6a 999 if (inf != NULL)
bb4d2ac2
L
1000 {
1001 (*inf->fprintf_func) (inf->stream, "%s", name);
1002 if (version_string && *version_string != '\0')
1003 (*inf->fprintf_func) (inf->stream, hidden ? "@%s" : "@@%s",
1004 version_string);
1005 }
252b5132 1006 else
bb4d2ac2
L
1007 {
1008 printf ("%s", name);
1009 if (version_string && *version_string != '\0')
1010 printf (hidden ? "@%s" : "@@%s", version_string);
1011 }
252b5132
RH
1012
1013 if (alloc != NULL)
1014 free (alloc);
1015}
1016
39f0547e
NC
1017static inline bfd_boolean
1018sym_ok (bfd_boolean want_section,
1019 bfd * abfd ATTRIBUTE_UNUSED,
1020 long place,
1021 asection * sec,
1022 struct disassemble_info * inf)
1023{
1024 if (want_section)
1025 {
1026 /* Note - we cannot just compare section pointers because they could
1027 be different, but the same... Ie the symbol that we are trying to
1028 find could have come from a separate debug info file. Under such
1029 circumstances the symbol will be associated with a section in the
1030 debug info file, whilst the section we want is in a normal file.
1031 So the section pointers will be different, but the section names
1032 will be the same. */
1033 if (strcmp (bfd_section_name (abfd, sorted_syms[place]->section),
1034 bfd_section_name (abfd, sec)) != 0)
1035 return FALSE;
1036 }
1037
1038 return inf->symbol_is_valid (sorted_syms[place], inf);
1039}
1040
22a398e1
NC
1041/* Locate a symbol given a bfd and a section (from INFO->application_data),
1042 and a VMA. If INFO->application_data->require_sec is TRUE, then always
1043 require the symbol to be in the section. Returns NULL if there is no
1044 suitable symbol. If PLACE is not NULL, then *PLACE is set to the index
1045 of the symbol in sorted_syms. */
252b5132
RH
1046
1047static asymbol *
3b9ad1cc 1048find_symbol_for_address (bfd_vma vma,
91d6fa6a 1049 struct disassemble_info *inf,
3b9ad1cc 1050 long *place)
252b5132
RH
1051{
1052 /* @@ Would it speed things up to cache the last two symbols returned,
1053 and maybe their address ranges? For many processors, only one memory
1054 operand can be present at a time, so the 2-entry cache wouldn't be
1055 constantly churned by code doing heavy memory accesses. */
1056
1057 /* Indices in `sorted_syms'. */
1058 long min = 0;
91d6fa6a 1059 long max_count = sorted_symcount;
252b5132 1060 long thisplace;
3b9ad1cc
AM
1061 struct objdump_disasm_info *aux;
1062 bfd *abfd;
1063 asection *sec;
1064 unsigned int opb;
e39ff52a 1065 bfd_boolean want_section;
0e70b27b 1066 long rel_count;
252b5132
RH
1067
1068 if (sorted_symcount < 1)
1069 return NULL;
1070
91d6fa6a 1071 aux = (struct objdump_disasm_info *) inf->application_data;
3b9ad1cc
AM
1072 abfd = aux->abfd;
1073 sec = aux->sec;
91d6fa6a 1074 opb = inf->octets_per_byte;
3b9ad1cc 1075
252b5132 1076 /* Perform a binary search looking for the closest symbol to the
91d6fa6a
NC
1077 required value. We are searching the range (min, max_count]. */
1078 while (min + 1 < max_count)
252b5132
RH
1079 {
1080 asymbol *sym;
1081
91d6fa6a 1082 thisplace = (max_count + min) / 2;
252b5132
RH
1083 sym = sorted_syms[thisplace];
1084
1085 if (bfd_asymbol_value (sym) > vma)
91d6fa6a 1086 max_count = thisplace;
252b5132
RH
1087 else if (bfd_asymbol_value (sym) < vma)
1088 min = thisplace;
1089 else
1090 {
1091 min = thisplace;
1092 break;
1093 }
1094 }
1095
1096 /* The symbol we want is now in min, the low end of the range we
1097 were searching. If there are several symbols with the same
a24bb4f0 1098 value, we want the first (non-section/non-debugging) one. */
252b5132
RH
1099 thisplace = min;
1100 while (thisplace > 0
1101 && (bfd_asymbol_value (sorted_syms[thisplace])
a24bb4f0
NC
1102 == bfd_asymbol_value (sorted_syms[thisplace - 1]))
1103 && ((sorted_syms[thisplace - 1]->flags
1104 & (BSF_SECTION_SYM | BSF_DEBUGGING)) == 0)
1105 )
252b5132
RH
1106 --thisplace;
1107
2b4590fb
AM
1108 /* Prefer a symbol in the current section if we have multple symbols
1109 with the same value, as can occur with overlays or zero size
1110 sections. */
1111 min = thisplace;
91d6fa6a 1112 while (min < max_count
2b4590fb
AM
1113 && (bfd_asymbol_value (sorted_syms[min])
1114 == bfd_asymbol_value (sorted_syms[thisplace])))
1115 {
39f0547e 1116 if (sym_ok (TRUE, abfd, min, sec, inf))
2b4590fb
AM
1117 {
1118 thisplace = min;
1119
1120 if (place != NULL)
1121 *place = thisplace;
1122
1123 return sorted_syms[thisplace];
1124 }
1125 ++min;
1126 }
1127
1049f94e 1128 /* If the file is relocatable, and the symbol could be from this
252b5132
RH
1129 section, prefer a symbol from this section over symbols from
1130 others, even if the other symbol's value might be closer.
0af11b59 1131
252b5132
RH
1132 Note that this may be wrong for some symbol references if the
1133 sections have overlapping memory ranges, but in that case there's
1134 no way to tell what's desired without looking at the relocation
e39ff52a 1135 table.
3aade688 1136
e39ff52a
PB
1137 Also give the target a chance to reject symbols. */
1138 want_section = (aux->require_sec
1139 || ((abfd->flags & HAS_RELOC) != 0
1140 && vma >= bfd_get_section_vma (abfd, sec)
1141 && vma < (bfd_get_section_vma (abfd, sec)
1142 + bfd_section_size (abfd, sec) / opb)));
39f0547e
NC
1143
1144 if (! sym_ok (want_section, abfd, thisplace, sec, inf))
252b5132
RH
1145 {
1146 long i;
2b4590fb 1147 long newplace = sorted_symcount;
98a91d6a 1148
2b4590fb 1149 for (i = min - 1; i >= 0; i--)
252b5132 1150 {
39f0547e 1151 if (sym_ok (want_section, abfd, i, sec, inf))
252b5132 1152 {
e39ff52a
PB
1153 if (newplace == sorted_symcount)
1154 newplace = i;
1155
1156 if (bfd_asymbol_value (sorted_syms[i])
1157 != bfd_asymbol_value (sorted_syms[newplace]))
1158 break;
1159
1160 /* Remember this symbol and keep searching until we reach
1161 an earlier address. */
1162 newplace = i;
252b5132
RH
1163 }
1164 }
1165
e39ff52a
PB
1166 if (newplace != sorted_symcount)
1167 thisplace = newplace;
1168 else
252b5132
RH
1169 {
1170 /* We didn't find a good symbol with a smaller value.
1171 Look for one with a larger value. */
1172 for (i = thisplace + 1; i < sorted_symcount; i++)
1173 {
39f0547e 1174 if (sym_ok (want_section, abfd, i, sec, inf))
252b5132
RH
1175 {
1176 thisplace = i;
1177 break;
1178 }
1179 }
1180 }
1181
39f0547e 1182 if (! sym_ok (want_section, abfd, thisplace, sec, inf))
22a398e1
NC
1183 /* There is no suitable symbol. */
1184 return NULL;
1185 }
1186
a24bb4f0
NC
1187 /* If we have not found an exact match for the specified address
1188 and we have dynamic relocations available, then we can produce
1189 a better result by matching a relocation to the address and
1190 using the symbol associated with that relocation. */
0e70b27b 1191 rel_count = aux->dynrelcount;
a24bb4f0 1192 if (!want_section
a24bb4f0 1193 && sorted_syms[thisplace]->value != vma
0e70b27b
L
1194 && rel_count > 0
1195 && aux->dynrelbuf != NULL
1196 && aux->dynrelbuf[0]->address <= vma
1197 && aux->dynrelbuf[rel_count - 1]->address >= vma
a24bb4f0
NC
1198 /* If we have matched a synthetic symbol, then stick with that. */
1199 && (sorted_syms[thisplace]->flags & BSF_SYNTHETIC) == 0)
1200 {
0e70b27b
L
1201 arelent ** rel_low;
1202 arelent ** rel_high;
a24bb4f0 1203
0e70b27b
L
1204 rel_low = aux->dynrelbuf;
1205 rel_high = rel_low + rel_count - 1;
1206 while (rel_low <= rel_high)
a24bb4f0 1207 {
0e70b27b
L
1208 arelent **rel_mid = &rel_low[(rel_high - rel_low) / 2];
1209 arelent * rel = *rel_mid;
a24bb4f0 1210
0e70b27b 1211 if (rel->address == vma)
a24bb4f0 1212 {
0e70b27b
L
1213 /* Absolute relocations do not provide a more helpful
1214 symbolic address. Find a non-absolute relocation
1215 with the same address. */
1216 arelent **rel_vma = rel_mid;
1217 for (rel_mid--;
1218 rel_mid >= rel_low && rel_mid[0]->address == vma;
1219 rel_mid--)
1220 rel_vma = rel_mid;
1221
1222 for (; rel_vma <= rel_high && rel_vma[0]->address == vma;
1223 rel_vma++)
1224 {
1225 rel = *rel_vma;
1226 if (rel->sym_ptr_ptr != NULL
1227 && ! bfd_is_abs_section ((* rel->sym_ptr_ptr)->section))
1228 {
1229 if (place != NULL)
1230 * place = thisplace;
1231 return * rel->sym_ptr_ptr;
1232 }
1233 }
1234 break;
a24bb4f0
NC
1235 }
1236
0e70b27b
L
1237 if (vma < rel->address)
1238 rel_high = rel_mid;
1239 else if (vma >= rel_mid[1]->address)
1240 rel_low = rel_mid + 1;
1241 else
a24bb4f0
NC
1242 break;
1243 }
1244 }
1245
252b5132
RH
1246 if (place != NULL)
1247 *place = thisplace;
1248
1249 return sorted_syms[thisplace];
1250}
1251
155e0d23 1252/* Print an address and the offset to the nearest symbol. */
252b5132
RH
1253
1254static void
46dca2e0 1255objdump_print_addr_with_sym (bfd *abfd, asection *sec, asymbol *sym,
91d6fa6a 1256 bfd_vma vma, struct disassemble_info *inf,
46dca2e0 1257 bfd_boolean skip_zeroes)
252b5132 1258{
91d6fa6a 1259 objdump_print_value (vma, inf, skip_zeroes);
252b5132
RH
1260
1261 if (sym == NULL)
1262 {
1263 bfd_vma secaddr;
1264
91d6fa6a 1265 (*inf->fprintf_func) (inf->stream, " <%s",
12add40e 1266 sanitize_string (bfd_get_section_name (abfd, sec)));
252b5132
RH
1267 secaddr = bfd_get_section_vma (abfd, sec);
1268 if (vma < secaddr)
1269 {
91d6fa6a
NC
1270 (*inf->fprintf_func) (inf->stream, "-0x");
1271 objdump_print_value (secaddr - vma, inf, TRUE);
252b5132
RH
1272 }
1273 else if (vma > secaddr)
1274 {
91d6fa6a
NC
1275 (*inf->fprintf_func) (inf->stream, "+0x");
1276 objdump_print_value (vma - secaddr, inf, TRUE);
252b5132 1277 }
91d6fa6a 1278 (*inf->fprintf_func) (inf->stream, ">");
252b5132
RH
1279 }
1280 else
1281 {
91d6fa6a 1282 (*inf->fprintf_func) (inf->stream, " <");
a24bb4f0 1283
91d6fa6a 1284 objdump_print_symname (abfd, inf, sym);
a24bb4f0
NC
1285
1286 if (bfd_asymbol_value (sym) == vma)
1287 ;
1288 /* Undefined symbols in an executables and dynamic objects do not have
1289 a value associated with them, so it does not make sense to display
1290 an offset relative to them. Normally we would not be provided with
1291 this kind of symbol, but the target backend might choose to do so,
1292 and the code in find_symbol_for_address might return an as yet
1293 unresolved symbol associated with a dynamic reloc. */
1294 else if ((bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC))
1295 && bfd_is_und_section (sym->section))
1296 ;
1297 else if (bfd_asymbol_value (sym) > vma)
252b5132 1298 {
91d6fa6a
NC
1299 (*inf->fprintf_func) (inf->stream, "-0x");
1300 objdump_print_value (bfd_asymbol_value (sym) - vma, inf, TRUE);
252b5132
RH
1301 }
1302 else if (vma > bfd_asymbol_value (sym))
1303 {
91d6fa6a
NC
1304 (*inf->fprintf_func) (inf->stream, "+0x");
1305 objdump_print_value (vma - bfd_asymbol_value (sym), inf, TRUE);
252b5132 1306 }
a24bb4f0 1307
91d6fa6a 1308 (*inf->fprintf_func) (inf->stream, ">");
252b5132 1309 }
98ec6e72
NC
1310
1311 if (display_file_offsets)
91d6fa6a 1312 inf->fprintf_func (inf->stream, _(" (File Offset: 0x%lx)"),
98ec6e72 1313 (long int)(sec->filepos + (vma - sec->vma)));
252b5132
RH
1314}
1315
155e0d23
NC
1316/* Print an address (VMA), symbolically if possible.
1317 If SKIP_ZEROES is TRUE, don't output leading zeroes. */
252b5132
RH
1318
1319static void
3b9ad1cc 1320objdump_print_addr (bfd_vma vma,
91d6fa6a 1321 struct disassemble_info *inf,
46dca2e0 1322 bfd_boolean skip_zeroes)
252b5132 1323{
3b9ad1cc 1324 struct objdump_disasm_info *aux;
d253b654 1325 asymbol *sym = NULL;
ce04548a 1326 bfd_boolean skip_find = FALSE;
252b5132 1327
91d6fa6a 1328 aux = (struct objdump_disasm_info *) inf->application_data;
32760852 1329
252b5132
RH
1330 if (sorted_symcount < 1)
1331 {
91d6fa6a
NC
1332 (*inf->fprintf_func) (inf->stream, "0x");
1333 objdump_print_value (vma, inf, skip_zeroes);
32760852
NC
1334
1335 if (display_file_offsets)
91d6fa6a
NC
1336 inf->fprintf_func (inf->stream, _(" (File Offset: 0x%lx)"),
1337 (long int)(aux->sec->filepos + (vma - aux->sec->vma)));
252b5132
RH
1338 return;
1339 }
1340
ce04548a
NC
1341 if (aux->reloc != NULL
1342 && aux->reloc->sym_ptr_ptr != NULL
1343 && * aux->reloc->sym_ptr_ptr != NULL)
1344 {
1345 sym = * aux->reloc->sym_ptr_ptr;
1346
1347 /* Adjust the vma to the reloc. */
1348 vma += bfd_asymbol_value (sym);
1349
1350 if (bfd_is_und_section (bfd_get_section (sym)))
1351 skip_find = TRUE;
1352 }
1353
1354 if (!skip_find)
91d6fa6a 1355 sym = find_symbol_for_address (vma, inf, NULL);
ce04548a 1356
91d6fa6a 1357 objdump_print_addr_with_sym (aux->abfd, aux->sec, sym, vma, inf,
252b5132
RH
1358 skip_zeroes);
1359}
1360
1361/* Print VMA to INFO. This function is passed to the disassembler
1362 routine. */
1363
1364static void
91d6fa6a 1365objdump_print_address (bfd_vma vma, struct disassemble_info *inf)
252b5132 1366{
91d6fa6a 1367 objdump_print_addr (vma, inf, ! prefix_addresses);
252b5132
RH
1368}
1369
2ae86dfc 1370/* Determine if the given address has a symbol associated with it. */
252b5132
RH
1371
1372static int
91d6fa6a 1373objdump_symbol_at_address (bfd_vma vma, struct disassemble_info * inf)
252b5132 1374{
252b5132
RH
1375 asymbol * sym;
1376
91d6fa6a 1377 sym = find_symbol_for_address (vma, inf, NULL);
252b5132
RH
1378
1379 return (sym != NULL && (bfd_asymbol_value (sym) == vma));
1380}
1381
1382/* Hold the last function name and the last line number we displayed
1383 in a disassembly. */
1384
1385static char *prev_functionname;
1386static unsigned int prev_line;
9b8d1a36 1387static unsigned int prev_discriminator;
252b5132
RH
1388
1389/* We keep a list of all files that we have seen when doing a
50c2245b 1390 disassembly with source, so that we know how much of the file to
252b5132
RH
1391 display. This can be important for inlined functions. */
1392
1393struct print_file_list
1394{
1395 struct print_file_list *next;
43ac9881
AM
1396 const char *filename;
1397 const char *modname;
3aade688 1398 const char *map;
e8f5eee4 1399 size_t mapsize;
3aade688 1400 const char **linemap;
e8f5eee4
NC
1401 unsigned maxline;
1402 unsigned last_line;
43339b1d 1403 unsigned max_printed;
e8f5eee4 1404 int first;
252b5132
RH
1405};
1406
1407static struct print_file_list *print_files;
1408
1409/* The number of preceding context lines to show when we start
1410 displaying a file for the first time. */
1411
1412#define SHOW_PRECEDING_CONTEXT_LINES (5)
1413
417ed8af 1414/* Read a complete file into memory. */
e8f5eee4
NC
1415
1416static const char *
5ef2d51b 1417slurp_file (const char *fn, size_t *size, struct stat *fst)
e8f5eee4
NC
1418{
1419#ifdef HAVE_MMAP
1420 int ps = getpagesize ();
1421 size_t msize;
1422#endif
1423 const char *map;
417ed8af 1424 int fd = open (fn, O_RDONLY | O_BINARY);
e8f5eee4
NC
1425
1426 if (fd < 0)
1427 return NULL;
5ef2d51b 1428 if (fstat (fd, fst) < 0)
6c713012
AM
1429 {
1430 close (fd);
1431 return NULL;
1432 }
5ef2d51b 1433 *size = fst->st_size;
e8f5eee4
NC
1434#ifdef HAVE_MMAP
1435 msize = (*size + ps - 1) & ~(ps - 1);
1436 map = mmap (NULL, msize, PROT_READ, MAP_SHARED, fd, 0);
6c713012 1437 if (map != (char *) -1L)
e8f5eee4 1438 {
6c713012
AM
1439 close (fd);
1440 return map;
e8f5eee4
NC
1441 }
1442#endif
3f5e193b 1443 map = (const char *) malloc (*size);
6c713012
AM
1444 if (!map || (size_t) read (fd, (char *) map, *size) != *size)
1445 {
1446 free ((void *) map);
e8f5eee4
NC
1447 map = NULL;
1448 }
1449 close (fd);
6c713012 1450 return map;
e8f5eee4
NC
1451}
1452
1453#define line_map_decrease 5
1454
1455/* Precompute array of lines for a mapped file. */
1456
3aade688
L
1457static const char **
1458index_file (const char *map, size_t size, unsigned int *maxline)
e8f5eee4
NC
1459{
1460 const char *p, *lstart, *end;
1461 int chars_per_line = 45; /* First iteration will use 40. */
1462 unsigned int lineno;
3aade688 1463 const char **linemap = NULL;
e8f5eee4 1464 unsigned long line_map_size = 0;
3aade688 1465
e8f5eee4
NC
1466 lineno = 0;
1467 lstart = map;
1468 end = map + size;
1469
3aade688
L
1470 for (p = map; p < end; p++)
1471 {
1472 if (*p == '\n')
1473 {
1474 if (p + 1 < end && p[1] == '\r')
1475 p++;
1476 }
1477 else if (*p == '\r')
1478 {
e8f5eee4
NC
1479 if (p + 1 < end && p[1] == '\n')
1480 p++;
1481 }
1482 else
1483 continue;
3aade688 1484
e8f5eee4
NC
1485 /* End of line found. */
1486
3aade688
L
1487 if (linemap == NULL || line_map_size < lineno + 1)
1488 {
e8f5eee4
NC
1489 unsigned long newsize;
1490
1491 chars_per_line -= line_map_decrease;
1492 if (chars_per_line <= 1)
1493 chars_per_line = 1;
1494 line_map_size = size / chars_per_line + 1;
1495 if (line_map_size < lineno + 1)
1496 line_map_size = lineno + 1;
1497 newsize = line_map_size * sizeof (char *);
3f5e193b 1498 linemap = (const char **) xrealloc (linemap, newsize);
e8f5eee4
NC
1499 }
1500
3aade688
L
1501 linemap[lineno++] = lstart;
1502 lstart = p + 1;
e8f5eee4 1503 }
3aade688
L
1504
1505 *maxline = lineno;
e8f5eee4
NC
1506 return linemap;
1507}
1508
43ac9881
AM
1509/* Tries to open MODNAME, and if successful adds a node to print_files
1510 linked list and returns that node. Returns NULL on failure. */
1511
1512static struct print_file_list *
5ef2d51b 1513try_print_file_open (const char *origname, const char *modname, struct stat *fst)
43ac9881
AM
1514{
1515 struct print_file_list *p;
43ac9881 1516
3f5e193b 1517 p = (struct print_file_list *) xmalloc (sizeof (struct print_file_list));
43ac9881 1518
5ef2d51b 1519 p->map = slurp_file (modname, &p->mapsize, fst);
e8f5eee4 1520 if (p->map == NULL)
43ac9881 1521 {
e8f5eee4
NC
1522 free (p);
1523 return NULL;
43ac9881 1524 }
3aade688 1525
e8f5eee4
NC
1526 p->linemap = index_file (p->map, p->mapsize, &p->maxline);
1527 p->last_line = 0;
43339b1d 1528 p->max_printed = 0;
43ac9881
AM
1529 p->filename = origname;
1530 p->modname = modname;
43ac9881 1531 p->next = print_files;
e8f5eee4 1532 p->first = 1;
43ac9881
AM
1533 print_files = p;
1534 return p;
1535}
1536
a8685210 1537/* If the source file, as described in the symtab, is not found
43ac9881
AM
1538 try to locate it in one of the paths specified with -I
1539 If found, add location to print_files linked list. */
1540
1541static struct print_file_list *
5ef2d51b 1542update_source_path (const char *filename, bfd *abfd)
43ac9881
AM
1543{
1544 struct print_file_list *p;
1545 const char *fname;
5ef2d51b 1546 struct stat fst;
43ac9881
AM
1547 int i;
1548
5ef2d51b
AM
1549 p = try_print_file_open (filename, filename, &fst);
1550 if (p == NULL)
1551 {
1552 if (include_path_count == 0)
1553 return NULL;
43ac9881 1554
5ef2d51b
AM
1555 /* Get the name of the file. */
1556 fname = lbasename (filename);
43ac9881 1557
5ef2d51b
AM
1558 /* If file exists under a new path, we need to add it to the list
1559 so that show_line knows about it. */
1560 for (i = 0; i < include_path_count; i++)
1561 {
1562 char *modname = concat (include_paths[i], "/", fname,
1563 (const char *) 0);
43ac9881 1564
5ef2d51b
AM
1565 p = try_print_file_open (filename, modname, &fst);
1566 if (p)
1567 break;
43ac9881 1568
5ef2d51b
AM
1569 free (modname);
1570 }
1571 }
1572
1573 if (p != NULL)
1574 {
1575 long mtime = bfd_get_mtime (abfd);
43ac9881 1576
5ef2d51b
AM
1577 if (fst.st_mtime > mtime)
1578 warn (_("source file %s is more recent than object file\n"),
1579 filename);
43ac9881
AM
1580 }
1581
5ef2d51b 1582 return p;
43ac9881
AM
1583}
1584
e8f5eee4 1585/* Print a source file line. */
252b5132 1586
3aade688 1587static void
91d6fa6a 1588print_line (struct print_file_list *p, unsigned int linenum)
252b5132 1589{
e8f5eee4 1590 const char *l;
615f3149 1591 size_t len;
3aade688
L
1592
1593 --linenum;
91d6fa6a 1594 if (linenum >= p->maxline)
e8f5eee4 1595 return;
91d6fa6a 1596 l = p->linemap [linenum];
615f3149
AM
1597 /* Test fwrite return value to quiet glibc warning. */
1598 len = strcspn (l, "\n\r");
1599 if (len == 0 || fwrite (l, len, 1, stdout) == 1)
1600 putchar ('\n');
1601}
252b5132 1602
e8f5eee4 1603/* Print a range of source code lines. */
252b5132 1604
e8f5eee4
NC
1605static void
1606dump_lines (struct print_file_list *p, unsigned int start, unsigned int end)
1607{
1608 if (p->map == NULL)
1609 return;
3aade688 1610 while (start <= end)
e8f5eee4
NC
1611 {
1612 print_line (p, start);
1613 start++;
252b5132 1614 }
0af11b59 1615}
252b5132 1616
50c2245b 1617/* Show the line number, or the source line, in a disassembly
252b5132
RH
1618 listing. */
1619
1620static void
46dca2e0 1621show_line (bfd *abfd, asection *section, bfd_vma addr_offset)
252b5132 1622{
b1f88ebe
AM
1623 const char *filename;
1624 const char *functionname;
91d6fa6a 1625 unsigned int linenumber;
9b8d1a36 1626 unsigned int discriminator;
0dafdf3f 1627 bfd_boolean reloc;
e1fa0163 1628 char *path = NULL;
252b5132
RH
1629
1630 if (! with_line_numbers && ! with_source_code)
1631 return;
1632
9b8d1a36 1633 if (! bfd_find_nearest_line_discriminator (abfd, section, syms, addr_offset,
8b5b2529
AM
1634 &filename, &functionname,
1635 &linenumber, &discriminator))
252b5132
RH
1636 return;
1637
1638 if (filename != NULL && *filename == '\0')
1639 filename = NULL;
1640 if (functionname != NULL && *functionname == '\0')
1641 functionname = NULL;
1642
0dafdf3f
L
1643 if (filename
1644 && IS_ABSOLUTE_PATH (filename)
1645 && prefix)
1646 {
1647 char *path_up;
1648 const char *fname = filename;
e1fa0163
NC
1649
1650 path = xmalloc (prefix_length + PATH_MAX + 1);
0dafdf3f
L
1651
1652 if (prefix_length)
1653 memcpy (path, prefix, prefix_length);
1654 path_up = path + prefix_length;
1655
1656 /* Build relocated filename, stripping off leading directories
e1fa0163 1657 from the initial filename if requested. */
0dafdf3f
L
1658 if (prefix_strip > 0)
1659 {
1660 int level = 0;
1661 const char *s;
1662
e1fa0163 1663 /* Skip selected directory levels. */
0dafdf3f
L
1664 for (s = fname + 1; *s != '\0' && level < prefix_strip; s++)
1665 if (IS_DIR_SEPARATOR(*s))
1666 {
1667 fname = s;
1668 level++;
1669 }
1670 }
1671
e1fa0163 1672 /* Update complete filename. */
0dafdf3f
L
1673 strncpy (path_up, fname, PATH_MAX);
1674 path_up[PATH_MAX] = '\0';
1675
1676 filename = path;
1677 reloc = TRUE;
1678 }
1679 else
1680 reloc = FALSE;
1681
252b5132
RH
1682 if (with_line_numbers)
1683 {
1684 if (functionname != NULL
1685 && (prev_functionname == NULL
1686 || strcmp (functionname, prev_functionname) != 0))
8b5b2529 1687 {
12add40e 1688 printf ("%s():\n", sanitize_string (functionname));
8b5b2529
AM
1689 prev_line = -1;
1690 }
1691 if (linenumber > 0
1692 && (linenumber != prev_line
1693 || discriminator != prev_discriminator))
1694 {
1695 if (discriminator > 0)
1696 printf ("%s:%u (discriminator %u)\n",
12add40e 1697 filename == NULL ? "???" : sanitize_string (filename),
8b5b2529
AM
1698 linenumber, discriminator);
1699 else
12add40e
NC
1700 printf ("%s:%u\n", filename == NULL
1701 ? "???" : sanitize_string (filename),
8b5b2529
AM
1702 linenumber);
1703 }
4a14e306
AK
1704 if (unwind_inlines)
1705 {
1706 const char *filename2;
1707 const char *functionname2;
1708 unsigned line2;
1709
1710 while (bfd_find_inliner_info (abfd, &filename2, &functionname2,
1711 &line2))
12add40e
NC
1712 {
1713 printf ("inlined by %s:%u",
1714 sanitize_string (filename2), line2);
1715 printf (" (%s)\n", sanitize_string (functionname2));
1716 }
4a14e306 1717 }
252b5132
RH
1718 }
1719
1720 if (with_source_code
1721 && filename != NULL
91d6fa6a 1722 && linenumber > 0)
252b5132
RH
1723 {
1724 struct print_file_list **pp, *p;
e8f5eee4 1725 unsigned l;
252b5132
RH
1726
1727 for (pp = &print_files; *pp != NULL; pp = &(*pp)->next)
8b6efd89 1728 if (filename_cmp ((*pp)->filename, filename) == 0)
252b5132
RH
1729 break;
1730 p = *pp;
1731
e8f5eee4 1732 if (p == NULL)
0dafdf3f
L
1733 {
1734 if (reloc)
1735 filename = xstrdup (filename);
5ef2d51b 1736 p = update_source_path (filename, abfd);
0dafdf3f 1737 }
252b5132 1738
91d6fa6a 1739 if (p != NULL && linenumber != p->last_line)
e8f5eee4 1740 {
3aade688 1741 if (file_start_context && p->first)
e8f5eee4 1742 l = 1;
3aade688 1743 else
252b5132 1744 {
91d6fa6a 1745 l = linenumber - SHOW_PRECEDING_CONTEXT_LINES;
3aade688 1746 if (l >= linenumber)
e8f5eee4 1747 l = 1;
43339b1d
AM
1748 if (p->max_printed >= l)
1749 {
1750 if (p->max_printed < linenumber)
1751 l = p->max_printed + 1;
1752 else
1753 l = linenumber;
1754 }
252b5132 1755 }
91d6fa6a 1756 dump_lines (p, l, linenumber);
43339b1d
AM
1757 if (p->max_printed < linenumber)
1758 p->max_printed = linenumber;
91d6fa6a 1759 p->last_line = linenumber;
e8f5eee4 1760 p->first = 0;
252b5132
RH
1761 }
1762 }
1763
1764 if (functionname != NULL
1765 && (prev_functionname == NULL
1766 || strcmp (functionname, prev_functionname) != 0))
1767 {
1768 if (prev_functionname != NULL)
1769 free (prev_functionname);
3f5e193b 1770 prev_functionname = (char *) xmalloc (strlen (functionname) + 1);
252b5132
RH
1771 strcpy (prev_functionname, functionname);
1772 }
1773
91d6fa6a
NC
1774 if (linenumber > 0 && linenumber != prev_line)
1775 prev_line = linenumber;
9b8d1a36
CC
1776
1777 if (discriminator != prev_discriminator)
1778 prev_discriminator = discriminator;
e1fa0163
NC
1779
1780 if (path)
1781 free (path);
252b5132
RH
1782}
1783
1784/* Pseudo FILE object for strings. */
1785typedef struct
1786{
1787 char *buffer;
6f104306
NS
1788 size_t pos;
1789 size_t alloc;
252b5132
RH
1790} SFILE;
1791
46dca2e0 1792/* sprintf to a "stream". */
252b5132 1793
0fd3a477 1794static int ATTRIBUTE_PRINTF_2
46dca2e0 1795objdump_sprintf (SFILE *f, const char *format, ...)
252b5132 1796{
252b5132 1797 size_t n;
46dca2e0 1798 va_list args;
252b5132 1799
6f104306 1800 while (1)
252b5132 1801 {
6f104306 1802 size_t space = f->alloc - f->pos;
3aade688 1803
6f104306
NS
1804 va_start (args, format);
1805 n = vsnprintf (f->buffer + f->pos, space, format, args);
451dad9c 1806 va_end (args);
252b5132 1807
6f104306
NS
1808 if (space > n)
1809 break;
3aade688 1810
6f104306 1811 f->alloc = (f->alloc + n) * 2;
3f5e193b 1812 f->buffer = (char *) xrealloc (f->buffer, f->alloc);
252b5132 1813 }
6f104306 1814 f->pos += n;
3aade688 1815
252b5132
RH
1816 return n;
1817}
1818
1819/* The number of zeroes we want to see before we start skipping them.
1820 The number is arbitrarily chosen. */
1821
0bcb06d2 1822#define DEFAULT_SKIP_ZEROES 8
252b5132
RH
1823
1824/* The number of zeroes to skip at the end of a section. If the
1825 number of zeroes at the end is between SKIP_ZEROES_AT_END and
1826 SKIP_ZEROES, they will be disassembled. If there are fewer than
1827 SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic
1828 attempt to avoid disassembling zeroes inserted by section
1829 alignment. */
1830
0bcb06d2 1831#define DEFAULT_SKIP_ZEROES_AT_END 3
252b5132
RH
1832
1833/* Disassemble some data in memory between given values. */
1834
1835static void
91d6fa6a 1836disassemble_bytes (struct disassemble_info * inf,
46dca2e0
NC
1837 disassembler_ftype disassemble_fn,
1838 bfd_boolean insns,
1839 bfd_byte * data,
1840 bfd_vma start_offset,
1841 bfd_vma stop_offset,
fd7bb956 1842 bfd_vma rel_offset,
46dca2e0
NC
1843 arelent *** relppp,
1844 arelent ** relppend)
252b5132
RH
1845{
1846 struct objdump_disasm_info *aux;
1847 asection *section;
940b2b78 1848 int octets_per_line;
252b5132 1849 int skip_addr_chars;
940b2b78 1850 bfd_vma addr_offset;
91d6fa6a
NC
1851 unsigned int opb = inf->octets_per_byte;
1852 unsigned int skip_zeroes = inf->skip_zeroes;
1853 unsigned int skip_zeroes_at_end = inf->skip_zeroes_at_end;
ce04548a 1854 int octets = opb;
6f104306 1855 SFILE sfile;
252b5132 1856
91d6fa6a 1857 aux = (struct objdump_disasm_info *) inf->application_data;
252b5132
RH
1858 section = aux->sec;
1859
6f104306 1860 sfile.alloc = 120;
3f5e193b 1861 sfile.buffer = (char *) xmalloc (sfile.alloc);
6f104306 1862 sfile.pos = 0;
3aade688 1863
3dcb3fcb
L
1864 if (insn_width)
1865 octets_per_line = insn_width;
1866 else if (insns)
940b2b78 1867 octets_per_line = 4;
252b5132 1868 else
940b2b78 1869 octets_per_line = 16;
252b5132
RH
1870
1871 /* Figure out how many characters to skip at the start of an
1872 address, to make the disassembly look nicer. We discard leading
1873 zeroes in chunks of 4, ensuring that there is always a leading
1874 zero remaining. */
1875 skip_addr_chars = 0;
1876 if (! prefix_addresses)
1877 {
1878 char buf[30];
17ceb936
AM
1879
1880 bfd_sprintf_vma (aux->abfd, buf, section->vma + section->size / opb);
1881
1882 while (buf[skip_addr_chars] == '0')
1883 ++skip_addr_chars;
1884
1885 /* Don't discard zeros on overflow. */
1886 if (buf[skip_addr_chars] == '\0' && section->vma != 0)
1887 skip_addr_chars = 0;
1888
1889 if (skip_addr_chars != 0)
1890 skip_addr_chars = (skip_addr_chars - 1) & -4;
252b5132
RH
1891 }
1892
91d6fa6a 1893 inf->insn_info_valid = 0;
252b5132 1894
940b2b78
TW
1895 addr_offset = start_offset;
1896 while (addr_offset < stop_offset)
252b5132
RH
1897 {
1898 bfd_vma z;
b34976b6 1899 bfd_boolean need_nl = FALSE;
ce04548a
NC
1900 int previous_octets;
1901
1902 /* Remember the length of the previous instruction. */
1903 previous_octets = octets;
ce04548a 1904 octets = 0;
252b5132 1905
bb7c70ed
NC
1906 /* Make sure we don't use relocs from previous instructions. */
1907 aux->reloc = NULL;
1908
940b2b78 1909 /* If we see more than SKIP_ZEROES octets of zeroes, we just
43ac9881 1910 print `...'. */
940b2b78 1911 for (z = addr_offset * opb; z < stop_offset * opb; z++)
252b5132
RH
1912 if (data[z] != 0)
1913 break;
1914 if (! disassemble_zeroes
91d6fa6a
NC
1915 && (inf->insn_info_valid == 0
1916 || inf->branch_delay_insns == 0)
0bcb06d2 1917 && (z - addr_offset * opb >= skip_zeroes
0af11b59 1918 || (z == stop_offset * opb &&
0bcb06d2 1919 z - addr_offset * opb < skip_zeroes_at_end)))
252b5132 1920 {
940b2b78 1921 /* If there are more nonzero octets to follow, we only skip
43ac9881
AM
1922 zeroes in multiples of 4, to try to avoid running over
1923 the start of an instruction which happens to start with
1924 zero. */
940b2b78
TW
1925 if (z != stop_offset * opb)
1926 z = addr_offset * opb + ((z - addr_offset * opb) &~ 3);
252b5132 1927
940b2b78 1928 octets = z - addr_offset * opb;
98ec6e72
NC
1929
1930 /* If we are going to display more data, and we are displaying
1931 file offsets, then tell the user how many zeroes we skip
1932 and the file offset from where we resume dumping. */
1933 if (display_file_offsets && ((addr_offset + (octets / opb)) < stop_offset))
1934 printf ("\t... (skipping %d zeroes, resuming at file offset: 0x%lx)\n",
1935 octets / opb,
0af1713e
AM
1936 (unsigned long) (section->filepos
1937 + (addr_offset + (octets / opb))));
98ec6e72
NC
1938 else
1939 printf ("\t...\n");
252b5132
RH
1940 }
1941 else
1942 {
1943 char buf[50];
252b5132
RH
1944 int bpc = 0;
1945 int pb = 0;
1946
252b5132 1947 if (with_line_numbers || with_source_code)
bc79cded 1948 show_line (aux->abfd, section, addr_offset);
252b5132
RH
1949
1950 if (! prefix_addresses)
1951 {
1952 char *s;
1953
d8180c76 1954 bfd_sprintf_vma (aux->abfd, buf, section->vma + addr_offset);
252b5132
RH
1955 for (s = buf + skip_addr_chars; *s == '0'; s++)
1956 *s = ' ';
1957 if (*s == '\0')
1958 *--s = '0';
1959 printf ("%s:\t", buf + skip_addr_chars);
1960 }
1961 else
1962 {
b34976b6 1963 aux->require_sec = TRUE;
91d6fa6a 1964 objdump_print_address (section->vma + addr_offset, inf);
b34976b6 1965 aux->require_sec = FALSE;
252b5132
RH
1966 putchar (' ');
1967 }
1968
1969 if (insns)
1970 {
6f104306 1971 sfile.pos = 0;
91d6fa6a
NC
1972 inf->fprintf_func = (fprintf_ftype) objdump_sprintf;
1973 inf->stream = &sfile;
1974 inf->bytes_per_line = 0;
1975 inf->bytes_per_chunk = 0;
dd7efa79
PB
1976 inf->flags = ((disassemble_all ? DISASSEMBLE_DATA : 0)
1977 | (wide_output ? WIDE_OUTPUT : 0));
0313a2b8 1978 if (machine)
91d6fa6a 1979 inf->flags |= USER_SPECIFIED_MACHINE_TYPE;
252b5132 1980
91d6fa6a 1981 if (inf->disassembler_needs_relocs
7df428b1
RS
1982 && (bfd_get_file_flags (aux->abfd) & EXEC_P) == 0
1983 && (bfd_get_file_flags (aux->abfd) & DYNAMIC) == 0
d99b6465 1984 && *relppp < relppend)
ce04548a
NC
1985 {
1986 bfd_signed_vma distance_to_rel;
1987
1988 distance_to_rel = (**relppp)->address
1989 - (rel_offset + addr_offset);
1990
1991 /* Check to see if the current reloc is associated with
1992 the instruction that we are about to disassemble. */
1993 if (distance_to_rel == 0
1994 /* FIXME: This is wrong. We are trying to catch
1995 relocs that are addressed part way through the
1996 current instruction, as might happen with a packed
1997 VLIW instruction. Unfortunately we do not know the
1998 length of the current instruction since we have not
1999 disassembled it yet. Instead we take a guess based
2000 upon the length of the previous instruction. The
2001 proper solution is to have a new target-specific
2002 disassembler function which just returns the length
2003 of an instruction at a given address without trying
2004 to display its disassembly. */
2005 || (distance_to_rel > 0
2006 && distance_to_rel < (bfd_signed_vma) (previous_octets/ opb)))
2007 {
91d6fa6a 2008 inf->flags |= INSN_HAS_RELOC;
ce04548a
NC
2009 aux->reloc = **relppp;
2010 }
ce04548a 2011 }
d99b6465 2012
bdc4de1b
NC
2013 if (! disassemble_all
2014 && (section->flags & (SEC_CODE | SEC_HAS_CONTENTS))
2015 == (SEC_CODE | SEC_HAS_CONTENTS))
2016 /* Set a stop_vma so that the disassembler will not read
2017 beyond the next symbol. We assume that symbols appear on
2018 the boundaries between instructions. We only do this when
2019 disassembling code of course, and when -D is in effect. */
2020 inf->stop_vma = section->vma + stop_offset;
3aade688 2021
53b2f36b 2022 inf->stop_offset = stop_offset;
91d6fa6a 2023 octets = (*disassemble_fn) (section->vma + addr_offset, inf);
bdc4de1b
NC
2024
2025 inf->stop_vma = 0;
91d6fa6a
NC
2026 inf->fprintf_func = (fprintf_ftype) fprintf;
2027 inf->stream = stdout;
2028 if (insn_width == 0 && inf->bytes_per_line != 0)
2029 octets_per_line = inf->bytes_per_line;
a8c62f1c 2030 if (octets < (int) opb)
e07bf1ac 2031 {
6f104306 2032 if (sfile.pos)
e07bf1ac 2033 printf ("%s\n", sfile.buffer);
a8c62f1c
AM
2034 if (octets >= 0)
2035 {
2036 non_fatal (_("disassemble_fn returned length %d"),
2037 octets);
2038 exit_status = 1;
2039 }
e07bf1ac
ILT
2040 break;
2041 }
252b5132
RH
2042 }
2043 else
2044 {
b4c96d0d 2045 bfd_vma j;
252b5132 2046
940b2b78
TW
2047 octets = octets_per_line;
2048 if (addr_offset + octets / opb > stop_offset)
2049 octets = (stop_offset - addr_offset) * opb;
252b5132 2050
940b2b78 2051 for (j = addr_offset * opb; j < addr_offset * opb + octets; ++j)
252b5132 2052 {
3882b010 2053 if (ISPRINT (data[j]))
940b2b78 2054 buf[j - addr_offset * opb] = data[j];
252b5132 2055 else
940b2b78 2056 buf[j - addr_offset * opb] = '.';
252b5132 2057 }
940b2b78 2058 buf[j - addr_offset * opb] = '\0';
252b5132
RH
2059 }
2060
2061 if (prefix_addresses
2062 ? show_raw_insn > 0
2063 : show_raw_insn >= 0)
2064 {
b4c96d0d 2065 bfd_vma j;
252b5132
RH
2066
2067 /* If ! prefix_addresses and ! wide_output, we print
43ac9881 2068 octets_per_line octets per line. */
940b2b78
TW
2069 pb = octets;
2070 if (pb > octets_per_line && ! prefix_addresses && ! wide_output)
2071 pb = octets_per_line;
252b5132 2072
91d6fa6a
NC
2073 if (inf->bytes_per_chunk)
2074 bpc = inf->bytes_per_chunk;
252b5132
RH
2075 else
2076 bpc = 1;
2077
940b2b78 2078 for (j = addr_offset * opb; j < addr_offset * opb + pb; j += bpc)
252b5132 2079 {
ae87f7e7
NC
2080 /* PR 21580: Check for a buffer ending early. */
2081 if (j + bpc <= stop_offset * opb)
252b5132 2082 {
ae87f7e7
NC
2083 int k;
2084
2085 if (inf->display_endian == BFD_ENDIAN_LITTLE)
2086 {
2087 for (k = bpc - 1; k >= 0; k--)
2088 printf ("%02x", (unsigned) data[j + k]);
2089 }
2090 else
2091 {
2092 for (k = 0; k < bpc; k++)
2093 printf ("%02x", (unsigned) data[j + k]);
2094 }
252b5132 2095 }
ae87f7e7 2096 putchar (' ');
252b5132
RH
2097 }
2098
940b2b78 2099 for (; pb < octets_per_line; pb += bpc)
252b5132
RH
2100 {
2101 int k;
2102
2103 for (k = 0; k < bpc; k++)
2104 printf (" ");
2105 putchar (' ');
2106 }
2107
2108 /* Separate raw data from instruction by extra space. */
2109 if (insns)
2110 putchar ('\t');
2111 else
2112 printf (" ");
2113 }
2114
2115 if (! insns)
2116 printf ("%s", buf);
6f104306
NS
2117 else if (sfile.pos)
2118 printf ("%s", sfile.buffer);
252b5132
RH
2119
2120 if (prefix_addresses
2121 ? show_raw_insn > 0
2122 : show_raw_insn >= 0)
2123 {
940b2b78 2124 while (pb < octets)
252b5132 2125 {
b4c96d0d 2126 bfd_vma j;
252b5132
RH
2127 char *s;
2128
2129 putchar ('\n');
940b2b78 2130 j = addr_offset * opb + pb;
252b5132 2131
d8180c76 2132 bfd_sprintf_vma (aux->abfd, buf, section->vma + j / opb);
252b5132
RH
2133 for (s = buf + skip_addr_chars; *s == '0'; s++)
2134 *s = ' ';
2135 if (*s == '\0')
2136 *--s = '0';
2137 printf ("%s:\t", buf + skip_addr_chars);
2138
940b2b78
TW
2139 pb += octets_per_line;
2140 if (pb > octets)
2141 pb = octets;
2142 for (; j < addr_offset * opb + pb; j += bpc)
252b5132 2143 {
d16fdddb
NC
2144 /* PR 21619: Check for a buffer ending early. */
2145 if (j + bpc <= stop_offset * opb)
252b5132 2146 {
d16fdddb
NC
2147 int k;
2148
2149 if (inf->display_endian == BFD_ENDIAN_LITTLE)
2150 {
2151 for (k = bpc - 1; k >= 0; k--)
2152 printf ("%02x", (unsigned) data[j + k]);
2153 }
2154 else
2155 {
2156 for (k = 0; k < bpc; k++)
2157 printf ("%02x", (unsigned) data[j + k]);
2158 }
252b5132 2159 }
d16fdddb 2160 putchar (' ');
252b5132
RH
2161 }
2162 }
2163 }
2164
2165 if (!wide_output)
2166 putchar ('\n');
2167 else
b34976b6 2168 need_nl = TRUE;
252b5132
RH
2169 }
2170
fd7bb956
AM
2171 while ((*relppp) < relppend
2172 && (**relppp)->address < rel_offset + addr_offset + octets / opb)
252b5132 2173 {
fd7bb956 2174 if (dump_reloc_info || dump_dynamic_reloc_info)
252b5132
RH
2175 {
2176 arelent *q;
2177
2178 q = **relppp;
2179
2180 if (wide_output)
2181 putchar ('\t');
2182 else
2183 printf ("\t\t\t");
2184
68b3b8dc 2185 objdump_print_value (section->vma - rel_offset + q->address,
91d6fa6a 2186 inf, TRUE);
252b5132 2187
f9ecb0a4
JJ
2188 if (q->howto == NULL)
2189 printf (": *unknown*\t");
2190 else if (q->howto->name)
2191 printf (": %s\t", q->howto->name);
2192 else
2193 printf (": %d\t", q->howto->type);
252b5132
RH
2194
2195 if (q->sym_ptr_ptr == NULL || *q->sym_ptr_ptr == NULL)
2196 printf ("*unknown*");
2197 else
2198 {
2199 const char *sym_name;
2200
2201 sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
2202 if (sym_name != NULL && *sym_name != '\0')
91d6fa6a 2203 objdump_print_symname (aux->abfd, inf, *q->sym_ptr_ptr);
252b5132
RH
2204 else
2205 {
2206 asection *sym_sec;
2207
2208 sym_sec = bfd_get_section (*q->sym_ptr_ptr);
2209 sym_name = bfd_get_section_name (aux->abfd, sym_sec);
2210 if (sym_name == NULL || *sym_name == '\0')
2211 sym_name = "*unknown*";
12add40e 2212 printf ("%s", sanitize_string (sym_name));
252b5132
RH
2213 }
2214 }
2215
2216 if (q->addend)
2217 {
343dbc36
L
2218 bfd_signed_vma addend = q->addend;
2219 if (addend < 0)
2220 {
2221 printf ("-0x");
2222 addend = -addend;
2223 }
2224 else
2225 printf ("+0x");
2226 objdump_print_value (addend, inf, TRUE);
252b5132
RH
2227 }
2228
2229 printf ("\n");
b34976b6 2230 need_nl = FALSE;
252b5132 2231 }
fd7bb956 2232 ++(*relppp);
252b5132
RH
2233 }
2234
2235 if (need_nl)
2236 printf ("\n");
2237
940b2b78 2238 addr_offset += octets / opb;
252b5132 2239 }
6f104306
NS
2240
2241 free (sfile.buffer);
252b5132
RH
2242}
2243
155e0d23 2244static void
91d6fa6a 2245disassemble_section (bfd *abfd, asection *section, void *inf)
155e0d23 2246{
1b0adfe0
NC
2247 const struct elf_backend_data * bed;
2248 bfd_vma sign_adjust = 0;
91d6fa6a 2249 struct disassemble_info * pinfo = (struct disassemble_info *) inf;
3b9ad1cc 2250 struct objdump_disasm_info * paux;
155e0d23
NC
2251 unsigned int opb = pinfo->octets_per_byte;
2252 bfd_byte * data = NULL;
2253 bfd_size_type datasize = 0;
2254 arelent ** rel_pp = NULL;
2255 arelent ** rel_ppstart = NULL;
2256 arelent ** rel_ppend;
bdc4de1b 2257 bfd_vma stop_offset;
155e0d23
NC
2258 asymbol * sym = NULL;
2259 long place = 0;
2260 long rel_count;
2261 bfd_vma rel_offset;
2262 unsigned long addr_offset;
baae986a
NC
2263 bfd_boolean do_print;
2264 enum loop_control
2265 {
2266 stop_offset_reached,
2267 function_sym,
2268 next_sym
2269 } loop_until;
155e0d23
NC
2270
2271 /* Sections that do not contain machine
2272 code are not normally disassembled. */
2273 if (! disassemble_all
70ecb384 2274 && only_list == NULL
46212538
AM
2275 && ((section->flags & (SEC_CODE | SEC_HAS_CONTENTS))
2276 != (SEC_CODE | SEC_HAS_CONTENTS)))
155e0d23
NC
2277 return;
2278
2279 if (! process_section_p (section))
2280 return;
2281
135dfb4a 2282 datasize = bfd_get_section_size (section);
60a02042 2283 if (datasize == 0)
155e0d23
NC
2284 return;
2285
643902a4
AS
2286 if (start_address == (bfd_vma) -1
2287 || start_address < section->vma)
2288 addr_offset = 0;
2289 else
2290 addr_offset = start_address - section->vma;
2291
2292 if (stop_address == (bfd_vma) -1)
2293 stop_offset = datasize / opb;
2294 else
2295 {
2296 if (stop_address < section->vma)
2297 stop_offset = 0;
2298 else
2299 stop_offset = stop_address - section->vma;
2300 if (stop_offset > datasize / opb)
2301 stop_offset = datasize / opb;
2302 }
2303
2304 if (addr_offset >= stop_offset)
2305 return;
2306
155e0d23 2307 /* Decide which set of relocs to use. Load them if necessary. */
3b9ad1cc 2308 paux = (struct objdump_disasm_info *) pinfo->application_data;
a24bb4f0 2309 if (paux->dynrelbuf && dump_dynamic_reloc_info)
155e0d23
NC
2310 {
2311 rel_pp = paux->dynrelbuf;
2312 rel_count = paux->dynrelcount;
2313 /* Dynamic reloc addresses are absolute, non-dynamic are section
50c2245b 2314 relative. REL_OFFSET specifies the reloc address corresponding
155e0d23 2315 to the start of this section. */
68b3b8dc 2316 rel_offset = section->vma;
155e0d23
NC
2317 }
2318 else
2319 {
2320 rel_count = 0;
2321 rel_pp = NULL;
2322 rel_offset = 0;
2323
2324 if ((section->flags & SEC_RELOC) != 0
d99b6465 2325 && (dump_reloc_info || pinfo->disassembler_needs_relocs))
155e0d23
NC
2326 {
2327 long relsize;
2328
2329 relsize = bfd_get_reloc_upper_bound (abfd, section);
2330 if (relsize < 0)
2331 bfd_fatal (bfd_get_filename (abfd));
2332
2333 if (relsize > 0)
2334 {
3f5e193b 2335 rel_ppstart = rel_pp = (arelent **) xmalloc (relsize);
155e0d23
NC
2336 rel_count = bfd_canonicalize_reloc (abfd, section, rel_pp, syms);
2337 if (rel_count < 0)
2338 bfd_fatal (bfd_get_filename (abfd));
2339
2340 /* Sort the relocs by address. */
2341 qsort (rel_pp, rel_count, sizeof (arelent *), compare_relocs);
2342 }
2343 }
155e0d23
NC
2344 }
2345 rel_ppend = rel_pp + rel_count;
2346
bae7501e 2347 if (!bfd_malloc_and_get_section (abfd, section, &data))
b02cd3e9
AM
2348 {
2349 non_fatal (_("Reading section %s failed because: %s"),
2350 section->name, bfd_errmsg (bfd_get_error ()));
2351 return;
2352 }
155e0d23
NC
2353
2354 paux->sec = section;
2355 pinfo->buffer = data;
2356 pinfo->buffer_vma = section->vma;
2357 pinfo->buffer_length = datasize;
2358 pinfo->section = section;
2359
155e0d23
NC
2360 /* Skip over the relocs belonging to addresses below the
2361 start address. */
2362 while (rel_pp < rel_ppend
2363 && (*rel_pp)->address < rel_offset + addr_offset)
2364 ++rel_pp;
2365
12add40e 2366 printf (_("\nDisassembly of section %s:\n"), sanitize_string (section->name));
155e0d23
NC
2367
2368 /* Find the nearest symbol forwards from our current position. */
3b9ad1cc 2369 paux->require_sec = TRUE;
3f5e193b 2370 sym = (asymbol *) find_symbol_for_address (section->vma + addr_offset,
91d6fa6a 2371 (struct disassemble_info *) inf,
3f5e193b 2372 &place);
3b9ad1cc 2373 paux->require_sec = FALSE;
155e0d23 2374
46bc35a9
RS
2375 /* PR 9774: If the target used signed addresses then we must make
2376 sure that we sign extend the value that we calculate for 'addr'
2377 in the loop below. */
1b0adfe0
NC
2378 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
2379 && (bed = get_elf_backend_data (abfd)) != NULL
2380 && bed->sign_extend_vma)
46bc35a9 2381 sign_adjust = (bfd_vma) 1 << (bed->s->arch_size - 1);
1b0adfe0 2382
155e0d23
NC
2383 /* Disassemble a block of instructions up to the address associated with
2384 the symbol we have just found. Then print the symbol and find the
2385 next symbol on. Repeat until we have disassembled the entire section
2386 or we have reached the end of the address range we are interested in. */
baae986a
NC
2387 do_print = paux->symbol == NULL;
2388 loop_until = stop_offset_reached;
2389
155e0d23
NC
2390 while (addr_offset < stop_offset)
2391 {
22a398e1 2392 bfd_vma addr;
155e0d23 2393 asymbol *nextsym;
bdc4de1b 2394 bfd_vma nextstop_offset;
155e0d23
NC
2395 bfd_boolean insns;
2396
22a398e1 2397 addr = section->vma + addr_offset;
095ad3b8 2398 addr = ((addr & ((sign_adjust << 1) - 1)) ^ sign_adjust) - sign_adjust;
22a398e1
NC
2399
2400 if (sym != NULL && bfd_asymbol_value (sym) <= addr)
155e0d23
NC
2401 {
2402 int x;
2403
2404 for (x = place;
2405 (x < sorted_symcount
22a398e1 2406 && (bfd_asymbol_value (sorted_syms[x]) <= addr));
155e0d23
NC
2407 ++x)
2408 continue;
2409
22a398e1 2410 pinfo->symbols = sorted_syms + place;
155e0d23 2411 pinfo->num_symbols = x - place;
2087ad84 2412 pinfo->symtab_pos = place;
155e0d23
NC
2413 }
2414 else
22a398e1
NC
2415 {
2416 pinfo->symbols = NULL;
2417 pinfo->num_symbols = 0;
2087ad84 2418 pinfo->symtab_pos = -1;
22a398e1 2419 }
155e0d23 2420
baae986a
NC
2421 /* If we are only disassembling from a specific symbol,
2422 check to see if we should start or stop displaying. */
d3def5d7
MY
2423 if (sym && paux->symbol)
2424 {
baae986a
NC
2425 if (do_print)
2426 {
2427 /* See if we should stop printing. */
2428 switch (loop_until)
2429 {
2430 case function_sym:
2431 if (sym->flags & BSF_FUNCTION)
2432 do_print = FALSE;
2433 break;
2434
2435 case stop_offset_reached:
2436 /* Handled by the while loop. */
2437 break;
d3def5d7 2438
baae986a
NC
2439 case next_sym:
2440 /* FIXME: There is an implicit assumption here
2441 that the name of sym is different from
2442 paux->symbol. */
2443 if (! bfd_is_local_label (abfd, sym))
2444 do_print = FALSE;
2445 break;
2446 }
2447 }
2448 else
d3def5d7 2449 {
baae986a
NC
2450 const char * name = bfd_asymbol_name (sym);
2451 char * alloc = NULL;
2452
2453 if (do_demangle && name[0] != '\0')
2454 {
2455 /* Demangle the name. */
2456 alloc = bfd_demangle (abfd, name, demangle_flags);
2457 if (alloc != NULL)
2458 name = alloc;
2459 }
2460
2461 /* We are not currently printing. Check to see
2462 if the current symbol matches the requested symbol. */
2463 if (streq (name, paux->symbol))
2464 {
2465 do_print = TRUE;
2466
2467 if (sym->flags & BSF_FUNCTION)
2468 {
2469 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
2470 && ((elf_symbol_type *) sym)->internal_elf_sym.st_size > 0)
2471 {
2472 /* Sym is a function symbol with a size associated
2473 with it. Turn on automatic disassembly for the
2474 next VALUE bytes. */
2475 stop_offset = addr_offset
2476 + ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
2477 loop_until = stop_offset_reached;
2478 }
2479 else
2480 {
2481 /* Otherwise we need to tell the loop heuristic to
2482 loop until the next function symbol is encountered. */
2483 loop_until = function_sym;
2484 }
2485 }
2486 else
2487 {
2488 /* Otherwise loop until the next symbol is encountered. */
2489 loop_until = next_sym;
2490 }
2491 }
2492
2493 free (alloc);
d3def5d7 2494 }
d3def5d7
MY
2495 }
2496
2497 if (! prefix_addresses && do_print)
155e0d23
NC
2498 {
2499 pinfo->fprintf_func (pinfo->stream, "\n");
22a398e1 2500 objdump_print_addr_with_sym (abfd, section, sym, addr,
155e0d23
NC
2501 pinfo, FALSE);
2502 pinfo->fprintf_func (pinfo->stream, ":\n");
2503 }
2504
22a398e1 2505 if (sym != NULL && bfd_asymbol_value (sym) > addr)
155e0d23
NC
2506 nextsym = sym;
2507 else if (sym == NULL)
2508 nextsym = NULL;
2509 else
2510 {
22a398e1 2511#define is_valid_next_sym(SYM) \
39f0547e 2512 (strcmp (bfd_section_name (abfd, (SYM)->section), bfd_section_name (abfd, section)) == 0 \
22a398e1
NC
2513 && (bfd_asymbol_value (SYM) > bfd_asymbol_value (sym)) \
2514 && pinfo->symbol_is_valid (SYM, pinfo))
3aade688 2515
155e0d23
NC
2516 /* Search forward for the next appropriate symbol in
2517 SECTION. Note that all the symbols are sorted
2518 together into one big array, and that some sections
2519 may have overlapping addresses. */
2520 while (place < sorted_symcount
22a398e1 2521 && ! is_valid_next_sym (sorted_syms [place]))
155e0d23 2522 ++place;
22a398e1 2523
155e0d23
NC
2524 if (place >= sorted_symcount)
2525 nextsym = NULL;
2526 else
2527 nextsym = sorted_syms[place];
2528 }
2529
22a398e1
NC
2530 if (sym != NULL && bfd_asymbol_value (sym) > addr)
2531 nextstop_offset = bfd_asymbol_value (sym) - section->vma;
155e0d23
NC
2532 else if (nextsym == NULL)
2533 nextstop_offset = stop_offset;
2534 else
22a398e1
NC
2535 nextstop_offset = bfd_asymbol_value (nextsym) - section->vma;
2536
84d7b001
NC
2537 if (nextstop_offset > stop_offset
2538 || nextstop_offset <= addr_offset)
22a398e1 2539 nextstop_offset = stop_offset;
155e0d23
NC
2540
2541 /* If a symbol is explicitly marked as being an object
2542 rather than a function, just dump the bytes without
2543 disassembling them. */
2544 if (disassemble_all
2545 || sym == NULL
abf71725 2546 || sym->section != section
22a398e1 2547 || bfd_asymbol_value (sym) > addr
155e0d23
NC
2548 || ((sym->flags & BSF_OBJECT) == 0
2549 && (strstr (bfd_asymbol_name (sym), "gnu_compiled")
2550 == NULL)
2551 && (strstr (bfd_asymbol_name (sym), "gcc2_compiled")
2552 == NULL))
2553 || (sym->flags & BSF_FUNCTION) != 0)
2554 insns = TRUE;
2555 else
2556 insns = FALSE;
2557
d3def5d7 2558 if (do_print)
baae986a
NC
2559 disassemble_bytes (pinfo, paux->disassemble_fn, insns, data,
2560 addr_offset, nextstop_offset,
2561 rel_offset, &rel_pp, rel_ppend);
3aade688 2562
155e0d23
NC
2563 addr_offset = nextstop_offset;
2564 sym = nextsym;
2565 }
2566
2567 free (data);
2568
2569 if (rel_ppstart != NULL)
2570 free (rel_ppstart);
2571}
2572
252b5132
RH
2573/* Disassemble the contents of an object file. */
2574
2575static void
46dca2e0 2576disassemble_data (bfd *abfd)
252b5132 2577{
252b5132
RH
2578 struct disassemble_info disasm_info;
2579 struct objdump_disasm_info aux;
4c45e5c9 2580 long i;
252b5132
RH
2581
2582 print_files = NULL;
2583 prev_functionname = NULL;
2584 prev_line = -1;
9b8d1a36 2585 prev_discriminator = 0;
252b5132
RH
2586
2587 /* We make a copy of syms to sort. We don't want to sort syms
2588 because that will screw up the relocs. */
4c45e5c9 2589 sorted_symcount = symcount ? symcount : dynsymcount;
3f5e193b
NC
2590 sorted_syms = (asymbol **) xmalloc ((sorted_symcount + synthcount)
2591 * sizeof (asymbol *));
4c45e5c9
JJ
2592 memcpy (sorted_syms, symcount ? syms : dynsyms,
2593 sorted_symcount * sizeof (asymbol *));
252b5132 2594
4c45e5c9
JJ
2595 sorted_symcount = remove_useless_symbols (sorted_syms, sorted_symcount);
2596
2597 for (i = 0; i < synthcount; ++i)
2598 {
2599 sorted_syms[sorted_symcount] = synthsyms + i;
2600 ++sorted_symcount;
2601 }
252b5132 2602
98a91d6a 2603 /* Sort the symbols into section and symbol order. */
252b5132
RH
2604 qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols);
2605
22a398e1 2606 init_disassemble_info (&disasm_info, stdout, (fprintf_ftype) fprintf);
98a91d6a 2607
46dca2e0 2608 disasm_info.application_data = (void *) &aux;
252b5132 2609 aux.abfd = abfd;
b34976b6 2610 aux.require_sec = FALSE;
155e0d23
NC
2611 aux.dynrelbuf = NULL;
2612 aux.dynrelcount = 0;
ce04548a 2613 aux.reloc = NULL;
d3def5d7 2614 aux.symbol = disasm_sym;
155e0d23 2615
252b5132
RH
2616 disasm_info.print_address_func = objdump_print_address;
2617 disasm_info.symbol_at_address_func = objdump_symbol_at_address;
2618
d3ba0551 2619 if (machine != NULL)
252b5132 2620 {
91d6fa6a 2621 const bfd_arch_info_type *inf = bfd_scan_arch (machine);
98a91d6a 2622
91d6fa6a 2623 if (inf == NULL)
a8c62f1c 2624 fatal (_("can't use supplied machine %s"), machine);
98a91d6a 2625
91d6fa6a 2626 abfd->arch_info = inf;
252b5132
RH
2627 }
2628
2629 if (endian != BFD_ENDIAN_UNKNOWN)
2630 {
2631 struct bfd_target *xvec;
2632
3f5e193b 2633 xvec = (struct bfd_target *) xmalloc (sizeof (struct bfd_target));
252b5132
RH
2634 memcpy (xvec, abfd->xvec, sizeof (struct bfd_target));
2635 xvec->byteorder = endian;
2636 abfd->xvec = xvec;
2637 }
2638
155e0d23 2639 /* Use libopcodes to locate a suitable disassembler. */
003ca0fd
YQ
2640 aux.disassemble_fn = disassembler (bfd_get_arch (abfd),
2641 bfd_big_endian (abfd),
2642 bfd_get_mach (abfd), abfd);
155e0d23 2643 if (!aux.disassemble_fn)
252b5132 2644 {
a8c62f1c 2645 non_fatal (_("can't disassemble for architecture %s\n"),
37cc8ec1 2646 bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
75cd796a 2647 exit_status = 1;
252b5132
RH
2648 return;
2649 }
2650
2651 disasm_info.flavour = bfd_get_flavour (abfd);
2652 disasm_info.arch = bfd_get_arch (abfd);
2653 disasm_info.mach = bfd_get_mach (abfd);
dd92f639 2654 disasm_info.disassembler_options = disassembler_options;
155e0d23 2655 disasm_info.octets_per_byte = bfd_octets_per_byte (abfd);
0bcb06d2
AS
2656 disasm_info.skip_zeroes = DEFAULT_SKIP_ZEROES;
2657 disasm_info.skip_zeroes_at_end = DEFAULT_SKIP_ZEROES_AT_END;
d99b6465 2658 disasm_info.disassembler_needs_relocs = FALSE;
0af11b59 2659
252b5132 2660 if (bfd_big_endian (abfd))
a8a9050d 2661 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_BIG;
252b5132 2662 else if (bfd_little_endian (abfd))
a8a9050d 2663 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_LITTLE;
252b5132
RH
2664 else
2665 /* ??? Aborting here seems too drastic. We could default to big or little
2666 instead. */
2667 disasm_info.endian = BFD_ENDIAN_UNKNOWN;
2668
22a398e1
NC
2669 /* Allow the target to customize the info structure. */
2670 disassemble_init_for_target (& disasm_info);
2671
a24bb4f0 2672 /* Pre-load the dynamic relocs as we may need them during the disassembly. */
fd7bb956
AM
2673 {
2674 long relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
3aade688 2675
a24bb4f0 2676 if (relsize < 0 && dump_dynamic_reloc_info)
fd7bb956
AM
2677 bfd_fatal (bfd_get_filename (abfd));
2678
2679 if (relsize > 0)
2680 {
3f5e193b 2681 aux.dynrelbuf = (arelent **) xmalloc (relsize);
3b9ad1cc
AM
2682 aux.dynrelcount = bfd_canonicalize_dynamic_reloc (abfd,
2683 aux.dynrelbuf,
2684 dynsyms);
155e0d23 2685 if (aux.dynrelcount < 0)
fd7bb956
AM
2686 bfd_fatal (bfd_get_filename (abfd));
2687
2688 /* Sort the relocs by address. */
3b9ad1cc
AM
2689 qsort (aux.dynrelbuf, aux.dynrelcount, sizeof (arelent *),
2690 compare_relocs);
fd7bb956
AM
2691 }
2692 }
2087ad84
PB
2693 disasm_info.symtab = sorted_syms;
2694 disasm_info.symtab_size = sorted_symcount;
fd7bb956 2695
155e0d23 2696 bfd_map_over_sections (abfd, disassemble_section, & disasm_info);
98a91d6a 2697
155e0d23
NC
2698 if (aux.dynrelbuf != NULL)
2699 free (aux.dynrelbuf);
252b5132
RH
2700 free (sorted_syms);
2701}
2702\f
dda8d76d 2703static bfd_boolean
7bcbeb0f
CC
2704load_specific_debug_section (enum dwarf_section_display_enum debug,
2705 asection *sec, void *file)
365544c3
L
2706{
2707 struct dwarf_section *section = &debug_displays [debug].section;
3f5e193b 2708 bfd *abfd = (bfd *) file;
bfec0f11 2709 bfd_byte *contents;
f2023ce7 2710 bfd_size_type amt;
63455780 2711 size_t alloced;
365544c3 2712
365544c3 2713 if (section->start != NULL)
dda8d76d
NC
2714 {
2715 /* If it is already loaded, do nothing. */
2716 if (streq (section->filename, bfd_get_filename (abfd)))
2717 return TRUE;
2718 free (section->start);
2719 }
365544c3 2720
dda8d76d 2721 section->filename = bfd_get_filename (abfd);
d1c4b12b 2722 section->reloc_info = NULL;
3aade688 2723 section->num_relocs = 0;
595330b7 2724 section->address = bfd_get_section_vma (abfd, sec);
11fa9f13 2725 section->user_data = sec;
365544c3 2726 section->size = bfd_get_section_size (sec);
63455780
NC
2727 /* PR 24360: On 32-bit hosts sizeof (size_t) < sizeof (bfd_size_type). */
2728 alloced = amt = section->size + 1;
2729 if (alloced != amt || alloced == 0)
11fa9f13
NC
2730 {
2731 section->start = NULL;
2732 free_debug_section (debug);
2733 printf (_("\nSection '%s' has an invalid size: %#llx.\n"),
12add40e
NC
2734 sanitize_string (section->name),
2735 (unsigned long long) section->size);
11fa9f13
NC
2736 return FALSE;
2737 }
63455780 2738 section->start = contents = malloc (alloced);
11fa9f13 2739 if (section->start == NULL
bfec0f11 2740 || !bfd_get_full_section_contents (abfd, sec, &contents))
365544c3
L
2741 {
2742 free_debug_section (debug);
2743 printf (_("\nCan't get contents for section '%s'.\n"),
12add40e 2744 sanitize_string (section->name));
dda8d76d 2745 return FALSE;
1b315056 2746 }
4f1881b9
AM
2747 /* Ensure any string section has a terminating NUL. */
2748 section->start[section->size] = 0;
1b315056 2749
2e8136f9
NC
2750 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
2751 && debug_displays [debug].relocate)
0acf065b 2752 {
dda8d76d
NC
2753 long reloc_size;
2754 bfd_boolean ret;
2755
8a72cc6e 2756 bfd_cache_section_contents (sec, section->start);
0acf065b
CC
2757
2758 ret = bfd_simple_get_relocated_section_contents (abfd,
2759 sec,
2760 section->start,
2761 syms) != NULL;
2762
2763 if (! ret)
2764 {
2765 free_debug_section (debug);
2766 printf (_("\nCan't get contents for section '%s'.\n"),
12add40e 2767 sanitize_string (section->name));
dda8d76d 2768 return FALSE;
0acf065b 2769 }
d1c4b12b 2770
d1c4b12b
NC
2771 reloc_size = bfd_get_reloc_upper_bound (abfd, sec);
2772 if (reloc_size > 0)
2773 {
2774 unsigned long reloc_count;
2775 arelent **relocs;
2776
2777 relocs = (arelent **) xmalloc (reloc_size);
2778
2779 reloc_count = bfd_canonicalize_reloc (abfd, sec, relocs, NULL);
2780 if (reloc_count == 0)
2781 free (relocs);
2782 else
2783 {
2784 section->reloc_info = relocs;
2785 section->num_relocs = reloc_count;
2786 }
2787 }
bdc4de1b 2788 }
0acf065b 2789
dda8d76d 2790 return TRUE;
7bcbeb0f
CC
2791}
2792
d1c4b12b
NC
2793bfd_boolean
2794reloc_at (struct dwarf_section * dsec, dwarf_vma offset)
2795{
2796 arelent ** relocs;
2797 arelent * rp;
2798
2799 if (dsec == NULL || dsec->reloc_info == NULL)
2800 return FALSE;
2801
2802 relocs = (arelent **) dsec->reloc_info;
2803
2804 for (; (rp = * relocs) != NULL; ++ relocs)
2805 if (rp->address == offset)
2806 return TRUE;
2807
2808 return FALSE;
2809}
2810
dda8d76d 2811bfd_boolean
7bcbeb0f
CC
2812load_debug_section (enum dwarf_section_display_enum debug, void *file)
2813{
2814 struct dwarf_section *section = &debug_displays [debug].section;
3f5e193b 2815 bfd *abfd = (bfd *) file;
7bcbeb0f
CC
2816 asection *sec;
2817
2818 /* If it is already loaded, do nothing. */
2819 if (section->start != NULL)
dda8d76d
NC
2820 {
2821 if (streq (section->filename, bfd_get_filename (abfd)))
2822 return TRUE;
2823 }
7bcbeb0f
CC
2824
2825 /* Locate the debug section. */
2826 sec = bfd_get_section_by_name (abfd, section->uncompressed_name);
2827 if (sec != NULL)
2828 section->name = section->uncompressed_name;
2829 else
2830 {
2831 sec = bfd_get_section_by_name (abfd, section->compressed_name);
2832 if (sec != NULL)
2833 section->name = section->compressed_name;
2834 }
2835 if (sec == NULL)
dda8d76d 2836 return FALSE;
7bcbeb0f
CC
2837
2838 return load_specific_debug_section (debug, sec, file);
365544c3
L
2839}
2840
2841void
2842free_debug_section (enum dwarf_section_display_enum debug)
2843{
2844 struct dwarf_section *section = &debug_displays [debug].section;
2845
2846 if (section->start == NULL)
2847 return;
2848
06614111
NC
2849 /* PR 17512: file: 0f67f69d. */
2850 if (section->user_data != NULL)
2851 {
2852 asection * sec = (asection *) section->user_data;
2853
2854 /* If we are freeing contents that are also pointed to by the BFD
2855 library's section structure then make sure to update those pointers
2856 too. Otherwise, the next time we try to load data for this section
2857 we can end up using a stale pointer. */
2858 if (section->start == sec->contents)
2859 {
2860 sec->contents = NULL;
2861 sec->flags &= ~ SEC_IN_MEMORY;
db6b071a 2862 sec->compress_status = COMPRESS_SECTION_NONE;
06614111
NC
2863 }
2864 }
2865
365544c3
L
2866 free ((char *) section->start);
2867 section->start = NULL;
2868 section->address = 0;
2869 section->size = 0;
2870}
2871
dda8d76d
NC
2872void
2873close_debug_file (void * file)
2874{
2875 bfd * abfd = (bfd *) file;
2876
2877 bfd_close (abfd);
2878}
2879
2880void *
2881open_debug_file (const char * pathname)
2882{
2883 bfd * data;
2884
2885 data = bfd_openr (pathname, NULL);
2886 if (data == NULL)
2887 return NULL;
2888
2889 if (! bfd_check_format (data, bfd_object))
2890 return NULL;
2891
2892 return data;
2893}
2894
365544c3
L
2895static void
2896dump_dwarf_section (bfd *abfd, asection *section,
2897 void *arg ATTRIBUTE_UNUSED)
2898{
2899 const char *name = bfd_get_section_name (abfd, section);
2900 const char *match;
3f5e193b 2901 int i;
365544c3 2902
0112cd26 2903 if (CONST_STRNEQ (name, ".gnu.linkonce.wi."))
365544c3
L
2904 match = ".debug_info";
2905 else
2906 match = name;
2907
2908 for (i = 0; i < max; i++)
4cb93e3b
TG
2909 if ((strcmp (debug_displays [i].section.uncompressed_name, match) == 0
2910 || strcmp (debug_displays [i].section.compressed_name, match) == 0)
2911 && debug_displays [i].enabled != NULL
2912 && *debug_displays [i].enabled)
365544c3 2913 {
c8450da8 2914 struct dwarf_section *sec = &debug_displays [i].section;
365544c3 2915
c8450da8
TG
2916 if (strcmp (sec->uncompressed_name, match) == 0)
2917 sec->name = sec->uncompressed_name;
2918 else
2919 sec->name = sec->compressed_name;
3f5e193b
NC
2920 if (load_specific_debug_section ((enum dwarf_section_display_enum) i,
2921 section, abfd))
c8450da8
TG
2922 {
2923 debug_displays [i].display (sec, abfd);
3aade688 2924
c8450da8 2925 if (i != info && i != abbrev)
3f5e193b 2926 free_debug_section ((enum dwarf_section_display_enum) i);
365544c3
L
2927 }
2928 break;
2929 }
2930}
2931
2932/* Dump the dwarf debugging information. */
2933
2934static void
2935dump_dwarf (bfd *abfd)
2936{
39f0547e
NC
2937 /* The byte_get pointer should have been set at the start of dump_bfd(). */
2938 if (byte_get == NULL)
f41e4712
NC
2939 {
2940 warn (_("File %s does not contain any dwarf debug information\n"),
2941 bfd_get_filename (abfd));
2942 return;
2943 }
365544c3 2944
39f0547e
NC
2945 eh_addr_size = bfd_arch_bits_per_address (abfd) / 8;
2946
b129eb0e 2947 switch (bfd_get_arch (abfd))
2dc4cec1 2948 {
b129eb0e
RH
2949 case bfd_arch_i386:
2950 switch (bfd_get_mach (abfd))
2951 {
2952 case bfd_mach_x86_64:
2953 case bfd_mach_x86_64_intel_syntax:
64b384e1 2954 case bfd_mach_x86_64_nacl:
f24e5a8a
L
2955 case bfd_mach_x64_32:
2956 case bfd_mach_x64_32_intel_syntax:
64b384e1 2957 case bfd_mach_x64_32_nacl:
b129eb0e
RH
2958 init_dwarf_regnames_x86_64 ();
2959 break;
2960
2961 default:
2962 init_dwarf_regnames_i386 ();
2963 break;
2964 }
2965 break;
2966
3d875af5
L
2967 case bfd_arch_iamcu:
2968 init_dwarf_regnames_iamcu ();
2969 break;
2970
4ee22035
RH
2971 case bfd_arch_aarch64:
2972 init_dwarf_regnames_aarch64();
2973 break;
2974
d6bb17b0
AA
2975 case bfd_arch_s390:
2976 init_dwarf_regnames_s390 ();
2977 break;
2978
5bb0830d
AB
2979 case bfd_arch_riscv:
2980 init_dwarf_regnames_riscv ();
2981 break;
2982
8ab159a9
AM
2983 case bfd_arch_s12z:
2984 /* S12Z has a 24 bit address space. But the only known
2985 producer of dwarf_info encodes addresses into 32 bits. */
2986 eh_addr_size = 4;
2987 break;
2988
b129eb0e
RH
2989 default:
2990 break;
2dc4cec1
L
2991 }
2992
365544c3 2993 bfd_map_over_sections (abfd, dump_dwarf_section, NULL);
365544c3
L
2994}
2995\f
29ca8dc5
NS
2996/* Read ABFD's stabs section STABSECT_NAME, and return a pointer to
2997 it. Return NULL on failure. */
252b5132 2998
bae7501e 2999static bfd_byte *
7d9813f1
NA
3000read_section_stabs (bfd *abfd, const char *sect_name, bfd_size_type *size_ptr,
3001 bfd_size_type *entsize_ptr)
252b5132 3002{
29ca8dc5 3003 asection *stabsect;
bae7501e 3004 bfd_byte *contents;
252b5132 3005
29ca8dc5 3006 stabsect = bfd_get_section_by_name (abfd, sect_name);
155e0d23 3007 if (stabsect == NULL)
252b5132 3008 {
12add40e
NC
3009 printf (_("No %s section present\n\n"),
3010 sanitize_string (sect_name));
b34976b6 3011 return FALSE;
252b5132
RH
3012 }
3013
bae7501e 3014 if (!bfd_malloc_and_get_section (abfd, stabsect, &contents))
252b5132 3015 {
a8c62f1c 3016 non_fatal (_("reading %s section of %s failed: %s"),
29ca8dc5 3017 sect_name, bfd_get_filename (abfd),
37cc8ec1 3018 bfd_errmsg (bfd_get_error ()));
75cd796a 3019 exit_status = 1;
a8c62f1c 3020 free (contents);
29ca8dc5 3021 return NULL;
252b5132
RH
3022 }
3023
bae7501e 3024 *size_ptr = bfd_section_size (abfd, stabsect);
7d9813f1
NA
3025 if (entsize_ptr)
3026 *entsize_ptr = stabsect->entsize;
252b5132 3027
29ca8dc5 3028 return contents;
252b5132
RH
3029}
3030
3031/* Stabs entries use a 12 byte format:
3032 4 byte string table index
3033 1 byte stab type
3034 1 byte stab other field
3035 2 byte stab desc field
3036 4 byte stab value
3037 FIXME: This will have to change for a 64 bit object format. */
3038
46dca2e0
NC
3039#define STRDXOFF (0)
3040#define TYPEOFF (4)
3041#define OTHEROFF (5)
3042#define DESCOFF (6)
3043#define VALOFF (8)
252b5132
RH
3044#define STABSIZE (12)
3045
3046/* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
3047 using string table section STRSECT_NAME (in `strtab'). */
3048
3049static void
3b9ad1cc
AM
3050print_section_stabs (bfd *abfd,
3051 const char *stabsect_name,
3052 unsigned *string_offset_ptr)
252b5132
RH
3053{
3054 int i;
46dca2e0 3055 unsigned file_string_table_offset = 0;
29ca8dc5 3056 unsigned next_file_string_table_offset = *string_offset_ptr;
252b5132
RH
3057 bfd_byte *stabp, *stabs_end;
3058
3059 stabp = stabs;
3060 stabs_end = stabp + stab_size;
3061
12add40e 3062 printf (_("Contents of %s section:\n\n"), sanitize_string (stabsect_name));
252b5132
RH
3063 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
3064
3065 /* Loop through all symbols and print them.
3066
3067 We start the index at -1 because there is a dummy symbol on
3068 the front of stabs-in-{coff,elf} sections that supplies sizes. */
f41e4712 3069 for (i = -1; stabp <= stabs_end - STABSIZE; stabp += STABSIZE, i++)
252b5132
RH
3070 {
3071 const char *name;
3072 unsigned long strx;
3073 unsigned char type, other;
3074 unsigned short desc;
3075 bfd_vma value;
3076
3077 strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
3078 type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
3079 other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
3080 desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
3081 value = bfd_h_get_32 (abfd, stabp + VALOFF);
3082
3083 printf ("\n%-6d ", i);
3084 /* Either print the stab name, or, if unnamed, print its number
0af11b59 3085 again (makes consistent formatting for tools like awk). */
252b5132
RH
3086 name = bfd_get_stab_name (type);
3087 if (name != NULL)
12add40e 3088 printf ("%-6s", sanitize_string (name));
252b5132
RH
3089 else if (type == N_UNDF)
3090 printf ("HdrSym");
3091 else
3092 printf ("%-6d", type);
3093 printf (" %-6d %-6d ", other, desc);
d8180c76 3094 bfd_printf_vma (abfd, value);
252b5132
RH
3095 printf (" %-6lu", strx);
3096
3097 /* Symbols with type == 0 (N_UNDF) specify the length of the
3098 string table associated with this file. We use that info
3099 to know how to relocate the *next* file's string table indices. */
252b5132
RH
3100 if (type == N_UNDF)
3101 {
3102 file_string_table_offset = next_file_string_table_offset;
3103 next_file_string_table_offset += value;
3104 }
3105 else
3106 {
f41e4712
NC
3107 bfd_size_type amt = strx + file_string_table_offset;
3108
252b5132
RH
3109 /* Using the (possibly updated) string table offset, print the
3110 string (if any) associated with this symbol. */
f41e4712 3111 if (amt < stabstr_size)
12add40e
NC
3112 /* PR 17512: file: 079-79389-0.001:0.1.
3113 FIXME: May need to sanitize this string before displaying. */
f41e4712 3114 printf (" %.*s", (int)(stabstr_size - amt), strtab + amt);
252b5132
RH
3115 else
3116 printf (" *");
3117 }
3118 }
3119 printf ("\n\n");
29ca8dc5 3120 *string_offset_ptr = next_file_string_table_offset;
252b5132
RH
3121}
3122
155e0d23
NC
3123typedef struct
3124{
3125 const char * section_name;
3126 const char * string_section_name;
29ca8dc5 3127 unsigned string_offset;
155e0d23
NC
3128}
3129stab_section_names;
3130
252b5132 3131static void
155e0d23 3132find_stabs_section (bfd *abfd, asection *section, void *names)
252b5132 3133{
155e0d23
NC
3134 int len;
3135 stab_section_names * sought = (stab_section_names *) names;
252b5132
RH
3136
3137 /* Check for section names for which stabsect_name is a prefix, to
29ca8dc5 3138 handle .stab.N, etc. */
155e0d23
NC
3139 len = strlen (sought->section_name);
3140
3141 /* If the prefix matches, and the files section name ends with a
3142 nul or a digit, then we match. I.e., we want either an exact
3143 match or a section followed by a number. */
3144 if (strncmp (sought->section_name, section->name, len) == 0
3145 && (section->name[len] == 0
29ca8dc5 3146 || (section->name[len] == '.' && ISDIGIT (section->name[len + 1]))))
252b5132 3147 {
29ca8dc5
NS
3148 if (strtab == NULL)
3149 strtab = read_section_stabs (abfd, sought->string_section_name,
7d9813f1 3150 &stabstr_size, NULL);
3aade688 3151
29ca8dc5 3152 if (strtab)
252b5132 3153 {
7d9813f1 3154 stabs = read_section_stabs (abfd, section->name, &stab_size, NULL);
29ca8dc5
NS
3155 if (stabs)
3156 print_section_stabs (abfd, section->name, &sought->string_offset);
252b5132
RH
3157 }
3158 }
3159}
98a91d6a 3160
155e0d23
NC
3161static void
3162dump_stabs_section (bfd *abfd, char *stabsect_name, char *strsect_name)
3163{
3164 stab_section_names s;
3165
3166 s.section_name = stabsect_name;
3167 s.string_section_name = strsect_name;
29ca8dc5
NS
3168 s.string_offset = 0;
3169
155e0d23 3170 bfd_map_over_sections (abfd, find_stabs_section, & s);
29ca8dc5
NS
3171
3172 free (strtab);
3173 strtab = NULL;
155e0d23
NC
3174}
3175
3176/* Dump the any sections containing stabs debugging information. */
3177
3178static void
3179dump_stabs (bfd *abfd)
3180{
3181 dump_stabs_section (abfd, ".stab", ".stabstr");
3182 dump_stabs_section (abfd, ".stab.excl", ".stab.exclstr");
3183 dump_stabs_section (abfd, ".stab.index", ".stab.indexstr");
62bb81b8
TG
3184
3185 /* For Darwin. */
3186 dump_stabs_section (abfd, "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr");
3187
155e0d23
NC
3188 dump_stabs_section (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
3189}
252b5132
RH
3190\f
3191static void
46dca2e0 3192dump_bfd_header (bfd *abfd)
252b5132
RH
3193{
3194 char *comma = "";
3195
3196 printf (_("architecture: %s, "),
3197 bfd_printable_arch_mach (bfd_get_arch (abfd),
3198 bfd_get_mach (abfd)));
6b6bc957 3199 printf (_("flags 0x%08x:\n"), abfd->flags & ~BFD_FLAGS_FOR_BFD_USE_MASK);
252b5132
RH
3200
3201#define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
3202 PF (HAS_RELOC, "HAS_RELOC");
3203 PF (EXEC_P, "EXEC_P");
3204 PF (HAS_LINENO, "HAS_LINENO");
3205 PF (HAS_DEBUG, "HAS_DEBUG");
3206 PF (HAS_SYMS, "HAS_SYMS");
3207 PF (HAS_LOCALS, "HAS_LOCALS");
3208 PF (DYNAMIC, "DYNAMIC");
3209 PF (WP_TEXT, "WP_TEXT");
3210 PF (D_PAGED, "D_PAGED");
3211 PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
3212 printf (_("\nstart address 0x"));
d8180c76 3213 bfd_printf_vma (abfd, abfd->start_address);
252b5132
RH
3214 printf ("\n");
3215}
7d9813f1
NA
3216\f
3217
3218/* Formatting callback function passed to ctf_dump. Returns either the pointer
3219 it is passed, or a pointer to newly-allocated storage, in which case
3220 dump_ctf() will free it when it no longer needs it. */
3221
3222static char *
3223dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
3224 char *s, void *arg)
3225{
63160fc9 3226 const char *blanks = arg;
7d9813f1
NA
3227 char *new_s;
3228
63160fc9 3229 if (asprintf (&new_s, "%s%s", blanks, s) < 0)
7d9813f1
NA
3230 return s;
3231 return new_s;
3232}
3233
3234/* Make a ctfsect suitable for ctf_bfdopen_ctfsect(). */
3235static ctf_sect_t
3236make_ctfsect (const char *name, bfd_byte *data,
3237 bfd_size_type size)
3238{
3239 ctf_sect_t ctfsect;
3240
3241 ctfsect.cts_name = name;
7d9813f1 3242 ctfsect.cts_entsize = 1;
7d9813f1
NA
3243 ctfsect.cts_size = size;
3244 ctfsect.cts_data = data;
3245
3246 return ctfsect;
3247}
3248
3249/* Dump one CTF archive member. */
3250
3251static int
3252dump_ctf_archive_member (ctf_file_t *ctf, const char *name, void *arg)
3253{
3254 ctf_file_t *parent = (ctf_file_t *) arg;
3255 const char *things[] = {"Labels", "Data objects", "Function objects",
3256 "Variables", "Types", "Strings", ""};
3257 const char **thing;
3258 size_t i;
3259
3260 /* Only print out the name of non-default-named archive members.
3261 The name .ctf appears everywhere, even for things that aren't
3262 really archives, so printing it out is liable to be confusing. */
3263 if (strcmp (name, ".ctf") != 0)
3264 printf (_("\nCTF archive member: %s:\n"), sanitize_string (name));
3265
3266 ctf_import (ctf, parent);
3267 for (i = 1, thing = things; *thing[0]; thing++, i++)
3268 {
3269 ctf_dump_state_t *s = NULL;
3270 char *item;
3271
3272 printf ("\n %s:\n", *thing);
3273 while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
3274 (void *) " ")) != NULL)
3275 {
3276 printf ("%s\n", item);
3277 free (item);
3278 }
3279
3280 if (ctf_errno (ctf))
3281 {
3282 non_fatal (_("Iteration failed: %s, %s\n"), *thing,
3283 ctf_errmsg (ctf_errno (ctf)));
3284 break;
3285 }
3286 }
3287 return 0;
3288}
3289
3290/* Dump the CTF debugging information. */
3291
3292static void
3293dump_ctf (bfd *abfd, const char *sect_name, const char *parent_name)
3294{
3295 ctf_archive_t *ctfa, *parenta = NULL;
3296 bfd_byte *ctfdata, *parentdata = NULL;
3297 bfd_size_type ctfsize, parentsize;
3298 ctf_sect_t ctfsect;
3299 ctf_file_t *parent = NULL;
3300 int err;
3301
3302 if ((ctfdata = read_section_stabs (abfd, sect_name, &ctfsize, NULL)) == NULL)
3303 bfd_fatal (bfd_get_filename (abfd));
3304
3305 if (parent_name
3306 && (parentdata = read_section_stabs (abfd, parent_name, &parentsize,
3307 NULL)) == NULL)
3308 bfd_fatal (bfd_get_filename (abfd));
3309
3310 /* Load the CTF file and dump it. */
3311
3312 ctfsect = make_ctfsect (sect_name, ctfdata, ctfsize);
3313 if ((ctfa = ctf_bfdopen_ctfsect (abfd, &ctfsect, &err)) == NULL)
3314 {
3315 non_fatal (_("CTF open failure: %s\n"), ctf_errmsg (err));
3316 bfd_fatal (bfd_get_filename (abfd));
3317 }
3318
3319 if (parentdata)
3320 {
3321 ctfsect = make_ctfsect (parent_name, parentdata, parentsize);
3322 if ((parenta = ctf_bfdopen_ctfsect (abfd, &ctfsect, &err)) == NULL)
3323 {
3324 non_fatal (_("CTF open failure: %s\n"), ctf_errmsg (err));
3325 bfd_fatal (bfd_get_filename (abfd));
3326 }
3327
3328 /* Assume that the applicable parent archive member is the default one.
3329 (This is what all known implementations are expected to do, if they
3330 put CTFs and their parents in archives together.) */
3331 if ((parent = ctf_arc_open_by_name (parenta, NULL, &err)) == NULL)
3332 {
3333 non_fatal (_("CTF open failure: %s\n"), ctf_errmsg (err));
3334 bfd_fatal (bfd_get_filename (abfd));
3335 }
3336 }
3337
3338 printf (_("Contents of CTF section %s:\n"), sanitize_string (sect_name));
3339
3340 ctf_archive_iter (ctfa, dump_ctf_archive_member, parent);
3341 ctf_file_close (parent);
3342 ctf_close (ctfa);
3343 ctf_close (parenta);
3344 free (parentdata);
3345 free (ctfdata);
3346}
98a91d6a 3347
79b377b3 3348\f
252b5132 3349static void
46dca2e0 3350dump_bfd_private_header (bfd *abfd)
252b5132 3351{
7d272a55
AM
3352 if (!bfd_print_private_bfd_data (abfd, stdout))
3353 non_fatal (_("warning: private headers incomplete: %s"),
3354 bfd_errmsg (bfd_get_error ()));
252b5132
RH
3355}
3356
6abcee90
TG
3357static void
3358dump_target_specific (bfd *abfd)
3359{
3360 const struct objdump_private_desc * const *desc;
3361 struct objdump_private_option *opt;
3362 char *e, *b;
3363
3364 /* Find the desc. */
3365 for (desc = objdump_private_vectors; *desc != NULL; desc++)
3366 if ((*desc)->filter (abfd))
3367 break;
3368
c32d6f7b 3369 if (*desc == NULL)
6abcee90
TG
3370 {
3371 non_fatal (_("option -P/--private not supported by this file"));
3372 return;
3373 }
3374
3375 /* Clear all options. */
3376 for (opt = (*desc)->options; opt->name; opt++)
3377 opt->selected = FALSE;
3378
3379 /* Decode options. */
3380 b = dump_private_options;
3381 do
3382 {
3383 e = strchr (b, ',');
3384
3385 if (e)
3386 *e = 0;
3387
3388 for (opt = (*desc)->options; opt->name; opt++)
3389 if (strcmp (opt->name, b) == 0)
3390 {
3391 opt->selected = TRUE;
3392 break;
3393 }
3394 if (opt->name == NULL)
3395 non_fatal (_("target specific dump '%s' not supported"), b);
3396
3397 if (e)
3398 {
3399 *e = ',';
3400 b = e + 1;
3401 }
3402 }
3403 while (e != NULL);
3404
3405 /* Dump. */
3406 (*desc)->dump (abfd);
3407}
155e0d23
NC
3408\f
3409/* Display a section in hexadecimal format with associated characters.
3410 Each line prefixed by the zero padded address. */
d24de309 3411
252b5132 3412static void
155e0d23 3413dump_section (bfd *abfd, asection *section, void *dummy ATTRIBUTE_UNUSED)
252b5132 3414{
cfd14a50 3415 bfd_byte *data = NULL;
155e0d23 3416 bfd_size_type datasize;
bdc4de1b
NC
3417 bfd_vma addr_offset;
3418 bfd_vma start_offset;
3419 bfd_vma stop_offset;
155e0d23
NC
3420 unsigned int opb = bfd_octets_per_byte (abfd);
3421 /* Bytes per line. */
3422 const int onaline = 16;
3423 char buf[64];
3424 int count;
3425 int width;
3426
3427 if ((section->flags & SEC_HAS_CONTENTS) == 0)
3428 return;
3429
3430 if (! process_section_p (section))
3431 return;
3aade688 3432
155e0d23
NC
3433 if ((datasize = bfd_section_size (abfd, section)) == 0)
3434 return;
3435
155e0d23
NC
3436 /* Compute the address range to display. */
3437 if (start_address == (bfd_vma) -1
3438 || start_address < section->vma)
3439 start_offset = 0;
3440 else
3441 start_offset = start_address - section->vma;
3442
3443 if (stop_address == (bfd_vma) -1)
3444 stop_offset = datasize / opb;
3445 else
252b5132 3446 {
155e0d23
NC
3447 if (stop_address < section->vma)
3448 stop_offset = 0;
3449 else
3450 stop_offset = stop_address - section->vma;
252b5132 3451
155e0d23
NC
3452 if (stop_offset > datasize / opb)
3453 stop_offset = datasize / opb;
252b5132
RH
3454 }
3455
32760852
NC
3456 if (start_offset >= stop_offset)
3457 return;
3aade688 3458
12add40e 3459 printf (_("Contents of section %s:"), sanitize_string (section->name));
32760852 3460 if (display_file_offsets)
0af1713e
AM
3461 printf (_(" (Starting at file offset: 0x%lx)"),
3462 (unsigned long) (section->filepos + start_offset));
32760852
NC
3463 printf ("\n");
3464
4a114e3e
L
3465 if (!bfd_get_full_section_contents (abfd, section, &data))
3466 {
0821d5b1
NC
3467 non_fatal (_("Reading section %s failed because: %s"),
3468 section->name, bfd_errmsg (bfd_get_error ()));
4a114e3e
L
3469 return;
3470 }
32760852 3471
155e0d23 3472 width = 4;
026df7c5 3473
155e0d23
NC
3474 bfd_sprintf_vma (abfd, buf, start_offset + section->vma);
3475 if (strlen (buf) >= sizeof (buf))
3476 abort ();
026df7c5 3477
155e0d23
NC
3478 count = 0;
3479 while (buf[count] == '0' && buf[count+1] != '\0')
3480 count++;
3481 count = strlen (buf) - count;
3482 if (count > width)
3483 width = count;
252b5132 3484
155e0d23
NC
3485 bfd_sprintf_vma (abfd, buf, stop_offset + section->vma - 1);
3486 if (strlen (buf) >= sizeof (buf))
3487 abort ();
026df7c5 3488
155e0d23
NC
3489 count = 0;
3490 while (buf[count] == '0' && buf[count+1] != '\0')
3491 count++;
3492 count = strlen (buf) - count;
3493 if (count > width)
3494 width = count;
026df7c5 3495
155e0d23
NC
3496 for (addr_offset = start_offset;
3497 addr_offset < stop_offset; addr_offset += onaline / opb)
d24de309 3498 {
155e0d23 3499 bfd_size_type j;
d24de309 3500
155e0d23
NC
3501 bfd_sprintf_vma (abfd, buf, (addr_offset + section->vma));
3502 count = strlen (buf);
3503 if ((size_t) count >= sizeof (buf))
3504 abort ();
d24de309 3505
155e0d23
NC
3506 putchar (' ');
3507 while (count < width)
252b5132 3508 {
155e0d23
NC
3509 putchar ('0');
3510 count++;
3511 }
3512 fputs (buf + count - width, stdout);
3513 putchar (' ');
252b5132 3514
155e0d23
NC
3515 for (j = addr_offset * opb;
3516 j < addr_offset * opb + onaline; j++)
3517 {
3518 if (j < stop_offset * opb)
3519 printf ("%02x", (unsigned) (data[j]));
3520 else
3521 printf (" ");
3522 if ((j & 3) == 3)
3523 printf (" ");
252b5132
RH
3524 }
3525
155e0d23
NC
3526 printf (" ");
3527 for (j = addr_offset * opb;
3528 j < addr_offset * opb + onaline; j++)
3529 {
3530 if (j >= stop_offset * opb)
3531 printf (" ");
3532 else
3533 printf ("%c", ISPRINT (data[j]) ? data[j] : '.');
3534 }
3535 putchar ('\n');
252b5132 3536 }
155e0d23 3537 free (data);
252b5132 3538}
155e0d23 3539
98a91d6a 3540/* Actually display the various requested regions. */
252b5132
RH
3541
3542static void
46dca2e0 3543dump_data (bfd *abfd)
252b5132 3544{
155e0d23 3545 bfd_map_over_sections (abfd, dump_section, NULL);
252b5132
RH
3546}
3547
98a91d6a
NC
3548/* Should perhaps share code and display with nm? */
3549
252b5132 3550static void
46dca2e0 3551dump_symbols (bfd *abfd ATTRIBUTE_UNUSED, bfd_boolean dynamic)
252b5132
RH
3552{
3553 asymbol **current;
91d6fa6a 3554 long max_count;
252b5132
RH
3555 long count;
3556
3557 if (dynamic)
3558 {
3559 current = dynsyms;
91d6fa6a 3560 max_count = dynsymcount;
252b5132
RH
3561 printf ("DYNAMIC SYMBOL TABLE:\n");
3562 }
3563 else
3564 {
3565 current = syms;
91d6fa6a 3566 max_count = symcount;
252b5132
RH
3567 printf ("SYMBOL TABLE:\n");
3568 }
3569
91d6fa6a 3570 if (max_count == 0)
a1df01d1
AM
3571 printf (_("no symbols\n"));
3572
91d6fa6a 3573 for (count = 0; count < max_count; count++)
252b5132 3574 {
155e0d23
NC
3575 bfd *cur_bfd;
3576
3577 if (*current == NULL)
83ef0798 3578 printf (_("no information for symbol number %ld\n"), count);
155e0d23
NC
3579
3580 else if ((cur_bfd = bfd_asymbol_bfd (*current)) == NULL)
83ef0798 3581 printf (_("could not determine the type of symbol number %ld\n"),
155e0d23
NC
3582 count);
3583
661f7c35
NC
3584 else if (process_section_p ((* current)->section)
3585 && (dump_special_syms
3586 || !bfd_is_target_special_symbol (cur_bfd, *current)))
252b5132 3587 {
155e0d23 3588 const char *name = (*current)->name;
252b5132 3589
155e0d23 3590 if (do_demangle && name != NULL && *name != '\0')
252b5132 3591 {
252b5132
RH
3592 char *alloc;
3593
155e0d23
NC
3594 /* If we want to demangle the name, we demangle it
3595 here, and temporarily clobber it while calling
3596 bfd_print_symbol. FIXME: This is a gross hack. */
af03af8f 3597 alloc = bfd_demangle (cur_bfd, name, demangle_flags);
ed180cc5
AM
3598 if (alloc != NULL)
3599 (*current)->name = alloc;
252b5132
RH
3600 bfd_print_symbol (cur_bfd, stdout, *current,
3601 bfd_print_symbol_all);
ed180cc5
AM
3602 if (alloc != NULL)
3603 {
3604 (*current)->name = name;
3605 free (alloc);
3606 }
252b5132 3607 }
252b5132 3608 else
155e0d23
NC
3609 bfd_print_symbol (cur_bfd, stdout, *current,
3610 bfd_print_symbol_all);
83ef0798 3611 printf ("\n");
252b5132 3612 }
661f7c35 3613
155e0d23 3614 current++;
252b5132 3615 }
155e0d23 3616 printf ("\n\n");
252b5132 3617}
155e0d23 3618\f
252b5132 3619static void
46dca2e0 3620dump_reloc_set (bfd *abfd, asection *sec, arelent **relpp, long relcount)
252b5132
RH
3621{
3622 arelent **p;
3623 char *last_filename, *last_functionname;
3624 unsigned int last_line;
9b8d1a36 3625 unsigned int last_discriminator;
252b5132
RH
3626
3627 /* Get column headers lined up reasonably. */
3628 {
3629 static int width;
98a91d6a 3630
252b5132
RH
3631 if (width == 0)
3632 {
3633 char buf[30];
155e0d23 3634
d8180c76 3635 bfd_sprintf_vma (abfd, buf, (bfd_vma) -1);
252b5132
RH
3636 width = strlen (buf) - 7;
3637 }
3638 printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
3639 }
3640
3641 last_filename = NULL;
3642 last_functionname = NULL;
3643 last_line = 0;
9b8d1a36 3644 last_discriminator = 0;
252b5132 3645
d3ba0551 3646 for (p = relpp; relcount && *p != NULL; p++, relcount--)
252b5132
RH
3647 {
3648 arelent *q = *p;
3649 const char *filename, *functionname;
91d6fa6a 3650 unsigned int linenumber;
9b8d1a36 3651 unsigned int discriminator;
252b5132
RH
3652 const char *sym_name;
3653 const char *section_name;
bf03e632 3654 bfd_vma addend2 = 0;
252b5132
RH
3655
3656 if (start_address != (bfd_vma) -1
3657 && q->address < start_address)
3658 continue;
3659 if (stop_address != (bfd_vma) -1
3660 && q->address > stop_address)
3661 continue;
3662
3663 if (with_line_numbers
3664 && sec != NULL
9b8d1a36
CC
3665 && bfd_find_nearest_line_discriminator (abfd, sec, syms, q->address,
3666 &filename, &functionname,
3667 &linenumber, &discriminator))
252b5132
RH
3668 {
3669 if (functionname != NULL
3670 && (last_functionname == NULL
3671 || strcmp (functionname, last_functionname) != 0))
3672 {
12add40e 3673 printf ("%s():\n", sanitize_string (functionname));
252b5132
RH
3674 if (last_functionname != NULL)
3675 free (last_functionname);
3676 last_functionname = xstrdup (functionname);
3677 }
98a91d6a 3678
91d6fa6a
NC
3679 if (linenumber > 0
3680 && (linenumber != last_line
252b5132
RH
3681 || (filename != NULL
3682 && last_filename != NULL
9b8d1a36
CC
3683 && filename_cmp (filename, last_filename) != 0)
3684 || (discriminator != last_discriminator)))
252b5132 3685 {
9b8d1a36 3686 if (discriminator > 0)
12add40e
NC
3687 printf ("%s:%u\n", filename == NULL ? "???" :
3688 sanitize_string (filename), linenumber);
9b8d1a36 3689 else
12add40e
NC
3690 printf ("%s:%u (discriminator %u)\n",
3691 filename == NULL ? "???" : sanitize_string (filename),
9b8d1a36 3692 linenumber, discriminator);
91d6fa6a 3693 last_line = linenumber;
9b8d1a36 3694 last_discriminator = discriminator;
252b5132
RH
3695 if (last_filename != NULL)
3696 free (last_filename);
3697 if (filename == NULL)
3698 last_filename = NULL;
3699 else
3700 last_filename = xstrdup (filename);
3701 }
3702 }
3703
3704 if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
3705 {
3706 sym_name = (*(q->sym_ptr_ptr))->name;
3707 section_name = (*(q->sym_ptr_ptr))->section->name;
3708 }
3709 else
3710 {
3711 sym_name = NULL;
3712 section_name = NULL;
3713 }
98a91d6a 3714
f9ecb0a4
JJ
3715 bfd_printf_vma (abfd, q->address);
3716 if (q->howto == NULL)
3717 printf (" *unknown* ");
3718 else if (q->howto->name)
bf03e632
DM
3719 {
3720 const char *name = q->howto->name;
3721
3722 /* R_SPARC_OLO10 relocations contain two addends.
3723 But because 'arelent' lacks enough storage to
3724 store them both, the 64-bit ELF Sparc backend
3725 records this as two relocations. One R_SPARC_LO10
3726 and one R_SPARC_13, both pointing to the same
3727 address. This is merely so that we have some
3728 place to store both addend fields.
3729
3730 Undo this transformation, otherwise the output
3731 will be confusing. */
3732 if (abfd->xvec->flavour == bfd_target_elf_flavour
3733 && elf_tdata(abfd)->elf_header->e_machine == EM_SPARCV9
3734 && relcount > 1
3735 && !strcmp (q->howto->name, "R_SPARC_LO10"))
3736 {
3737 arelent *q2 = *(p + 1);
3738 if (q2 != NULL
3739 && q2->howto
3740 && q->address == q2->address
3741 && !strcmp (q2->howto->name, "R_SPARC_13"))
3742 {
3743 name = "R_SPARC_OLO10";
3744 addend2 = q2->addend;
3745 p++;
3746 }
3747 }
3748 printf (" %-16s ", name);
3749 }
f9ecb0a4
JJ
3750 else
3751 printf (" %-16d ", q->howto->type);
171191ba 3752
252b5132 3753 if (sym_name)
171191ba
NC
3754 {
3755 objdump_print_symname (abfd, NULL, *q->sym_ptr_ptr);
171191ba 3756 }
252b5132
RH
3757 else
3758 {
d3ba0551 3759 if (section_name == NULL)
252b5132 3760 section_name = "*unknown*";
12add40e 3761 printf ("[%s]", sanitize_string (section_name));
252b5132 3762 }
98a91d6a 3763
252b5132
RH
3764 if (q->addend)
3765 {
343dbc36
L
3766 bfd_signed_vma addend = q->addend;
3767 if (addend < 0)
3768 {
3769 printf ("-0x");
3770 addend = -addend;
3771 }
3772 else
3773 printf ("+0x");
3774 bfd_printf_vma (abfd, addend);
252b5132 3775 }
bf03e632
DM
3776 if (addend2)
3777 {
3778 printf ("+0x");
3779 bfd_printf_vma (abfd, addend2);
3780 }
98a91d6a 3781
252b5132
RH
3782 printf ("\n");
3783 }
4b41844b
NC
3784
3785 if (last_filename != NULL)
3786 free (last_filename);
3787 if (last_functionname != NULL)
3788 free (last_functionname);
252b5132 3789}
43ac9881 3790
155e0d23 3791static void
3b9ad1cc
AM
3792dump_relocs_in_section (bfd *abfd,
3793 asection *section,
3794 void *dummy ATTRIBUTE_UNUSED)
155e0d23 3795{
e8fba7f6 3796 arelent **relpp = NULL;
155e0d23
NC
3797 long relcount;
3798 long relsize;
3799
3800 if ( bfd_is_abs_section (section)
3801 || bfd_is_und_section (section)
3802 || bfd_is_com_section (section)
3803 || (! process_section_p (section))
3804 || ((section->flags & SEC_RELOC) == 0))
3805 return;
3806
12add40e 3807 printf ("RELOCATION RECORDS FOR [%s]:", sanitize_string (section->name));
155e0d23 3808
7a6e0d89 3809 relsize = bfd_get_reloc_upper_bound (abfd, section);
155e0d23
NC
3810 if (relsize == 0)
3811 {
3812 printf (" (none)\n\n");
3813 return;
3814 }
3815
7a6e0d89
AM
3816 if (relsize < 0)
3817 relcount = relsize;
3818 else
3819 {
3820 relpp = (arelent **) xmalloc (relsize);
3821 relcount = bfd_canonicalize_reloc (abfd, section, relpp, syms);
39ff1b79
NC
3822 }
3823
155e0d23 3824 if (relcount < 0)
5a3f568b
NC
3825 {
3826 printf ("\n");
7a6e0d89
AM
3827 non_fatal (_("failed to read relocs in: %s"),
3828 sanitize_string (bfd_get_filename (abfd)));
5a3f568b
NC
3829 bfd_fatal (_("error message was"));
3830 }
155e0d23
NC
3831 else if (relcount == 0)
3832 printf (" (none)\n\n");
3833 else
3834 {
3835 printf ("\n");
3836 dump_reloc_set (abfd, section, relpp, relcount);
3837 printf ("\n\n");
3838 }
3839 free (relpp);
3840}
3841
3842static void
3843dump_relocs (bfd *abfd)
3844{
3845 bfd_map_over_sections (abfd, dump_relocs_in_section, NULL);
3846}
3847
3848static void
3849dump_dynamic_relocs (bfd *abfd)
3850{
3851 long relsize;
3852 arelent **relpp;
3853 long relcount;
3854
3855 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
3856 if (relsize < 0)
3857 bfd_fatal (bfd_get_filename (abfd));
3858
3859 printf ("DYNAMIC RELOCATION RECORDS");
3860
3861 if (relsize == 0)
3862 printf (" (none)\n\n");
3863 else
3864 {
3f5e193b 3865 relpp = (arelent **) xmalloc (relsize);
155e0d23
NC
3866 relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
3867
3868 if (relcount < 0)
3869 bfd_fatal (bfd_get_filename (abfd));
3870 else if (relcount == 0)
3871 printf (" (none)\n\n");
3872 else
3873 {
3874 printf ("\n");
3875 dump_reloc_set (abfd, NULL, relpp, relcount);
3876 printf ("\n\n");
3877 }
3878 free (relpp);
3879 }
3880}
3881
43ac9881
AM
3882/* Creates a table of paths, to search for source files. */
3883
3884static void
3885add_include_path (const char *path)
3886{
3887 if (path[0] == 0)
3888 return;
3889 include_path_count++;
3f5e193b
NC
3890 include_paths = (const char **)
3891 xrealloc (include_paths, include_path_count * sizeof (*include_paths));
43ac9881
AM
3892#ifdef HAVE_DOS_BASED_FILE_SYSTEM
3893 if (path[1] == ':' && path[2] == 0)
3894 path = concat (path, ".", (const char *) 0);
3895#endif
3896 include_paths[include_path_count - 1] = path;
3897}
155e0d23
NC
3898
3899static void
3b9ad1cc
AM
3900adjust_addresses (bfd *abfd ATTRIBUTE_UNUSED,
3901 asection *section,
bc79cded 3902 void *arg)
155e0d23 3903{
bc79cded
L
3904 if ((section->flags & SEC_DEBUGGING) == 0)
3905 {
3906 bfd_boolean *has_reloc_p = (bfd_boolean *) arg;
3907 section->vma += adjust_section_vma;
3908 if (*has_reloc_p)
3909 section->lma += adjust_section_vma;
3910 }
155e0d23
NC
3911}
3912
2379f9c4
FS
3913/* Return the sign-extended form of an ARCH_SIZE sized VMA. */
3914
3915static bfd_vma
3916sign_extend_address (bfd *abfd ATTRIBUTE_UNUSED,
3917 bfd_vma vma,
3918 unsigned arch_size)
3919{
3920 bfd_vma mask;
3921 mask = (bfd_vma) 1 << (arch_size - 1);
3922 return (((vma & ((mask << 1) - 1)) ^ mask) - mask);
3923}
3924
155e0d23
NC
3925/* Dump selected contents of ABFD. */
3926
3927static void
39f0547e 3928dump_bfd (bfd *abfd, bfd_boolean is_mainfile)
155e0d23 3929{
2379f9c4
FS
3930 const struct elf_backend_data * bed;
3931
39f0547e
NC
3932 if (bfd_big_endian (abfd))
3933 byte_get = byte_get_big_endian;
3934 else if (bfd_little_endian (abfd))
3935 byte_get = byte_get_little_endian;
3936 else
3937 byte_get = NULL;
3938
3939 /* Load any separate debug information files.
3940 We do this now and without checking do_follow_links because separate
3941 debug info files may contain symbol tables that we will need when
3942 displaying information about the main file. Any memory allocated by
3943 load_separate_debug_files will be released when we call
3944 free_debug_memory below.
3945
3946 The test on is_mainfile is there because the chain of separate debug
3947 info files is a global variable shared by all invocations of dump_bfd. */
3948 if (is_mainfile)
3949 {
3950 load_separate_debug_files (abfd, bfd_get_filename (abfd));
3951
3952 /* If asked to do so, recursively dump the separate files. */
3953 if (do_follow_links)
3954 {
3955 separate_info * i;
3956
3957 for (i = first_separate_info; i != NULL; i = i->next)
3958 dump_bfd (i->handle, FALSE);
3959 }
3960 }
3961
2379f9c4
FS
3962 /* Adjust user-specified start and stop limits for targets that use
3963 signed addresses. */
3964 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
3965 && (bed = get_elf_backend_data (abfd)) != NULL
3966 && bed->sign_extend_vma)
3967 {
3968 start_address = sign_extend_address (abfd, start_address,
3969 bed->s->arch_size);
3970 stop_address = sign_extend_address (abfd, stop_address,
3971 bed->s->arch_size);
3972 }
3973
155e0d23
NC
3974 /* If we are adjusting section VMA's, change them all now. Changing
3975 the BFD information is a hack. However, we must do it, or
3976 bfd_find_nearest_line will not do the right thing. */
3977 if (adjust_section_vma != 0)
bc79cded
L
3978 {
3979 bfd_boolean has_reloc = (abfd->flags & HAS_RELOC);
3980 bfd_map_over_sections (abfd, adjust_addresses, &has_reloc);
3981 }
155e0d23 3982
fd2f0033 3983 if (! dump_debugging_tags && ! suppress_bfd_header)
12add40e
NC
3984 printf (_("\n%s: file format %s\n"),
3985 sanitize_string (bfd_get_filename (abfd)),
155e0d23
NC
3986 abfd->xvec->name);
3987 if (dump_ar_hdrs)
1869e86f 3988 print_arelt_descr (stdout, abfd, TRUE, FALSE);
155e0d23
NC
3989 if (dump_file_header)
3990 dump_bfd_header (abfd);
3991 if (dump_private_headers)
3992 dump_bfd_private_header (abfd);
6abcee90
TG
3993 if (dump_private_options != NULL)
3994 dump_target_specific (abfd);
fd2f0033 3995 if (! dump_debugging_tags && ! suppress_bfd_header)
155e0d23 3996 putchar ('\n');
155e0d23 3997
365544c3
L
3998 if (dump_symtab
3999 || dump_reloc_info
4000 || disassemble
4001 || dump_debugging
4002 || dump_dwarf_section_info)
39f0547e
NC
4003 {
4004 syms = slurp_symtab (abfd);
4005
4006 /* If following links, load any symbol tables from the linked files as well. */
4007 if (do_follow_links && is_mainfile)
4008 {
4009 separate_info * i;
4010
4011 for (i = first_separate_info; i != NULL; i = i->next)
4012 {
4013 asymbol ** extra_syms;
4014 long old_symcount = symcount;
4015
4016 extra_syms = slurp_symtab (i->handle);
4017
4018 if (extra_syms)
4019 {
4020 if (old_symcount == 0)
4021 {
4022 syms = extra_syms;
4023 }
4024 else
4025 {
4026 syms = xrealloc (syms, (symcount + old_symcount) * sizeof (asymbol *));
4027 memcpy (syms + old_symcount,
4028 extra_syms,
4029 symcount * sizeof (asymbol *));
4030 }
4031 }
4032
4033 symcount += old_symcount;
4034 }
4035 }
4036 }
a29a8af8
KT
4037
4038 if (dump_section_headers)
4039 dump_headers (abfd);
4040
4c45e5c9
JJ
4041 if (dump_dynamic_symtab || dump_dynamic_reloc_info
4042 || (disassemble && bfd_get_dynamic_symtab_upper_bound (abfd) > 0))
155e0d23 4043 dynsyms = slurp_dynamic_symtab (abfd);
39f0547e 4044
90e3cdf2 4045 if (disassemble)
4c45e5c9 4046 {
c9727e01
AM
4047 synthcount = bfd_get_synthetic_symtab (abfd, symcount, syms,
4048 dynsymcount, dynsyms, &synthsyms);
4049 if (synthcount < 0)
4050 synthcount = 0;
4c45e5c9 4051 }
155e0d23
NC
4052
4053 if (dump_symtab)
4054 dump_symbols (abfd, FALSE);
4055 if (dump_dynamic_symtab)
4056 dump_symbols (abfd, TRUE);
365544c3
L
4057 if (dump_dwarf_section_info)
4058 dump_dwarf (abfd);
7d9813f1
NA
4059 if (dump_ctf_section_info)
4060 dump_ctf (abfd, dump_ctf_section_name, dump_ctf_parent_name);
155e0d23
NC
4061 if (dump_stab_section_info)
4062 dump_stabs (abfd);
4063 if (dump_reloc_info && ! disassemble)
4064 dump_relocs (abfd);
4065 if (dump_dynamic_reloc_info && ! disassemble)
4066 dump_dynamic_relocs (abfd);
4067 if (dump_section_contents)
4068 dump_data (abfd);
4069 if (disassemble)
4070 disassemble_data (abfd);
4071
4072 if (dump_debugging)
4073 {
4074 void *dhandle;
4075
b922d590 4076 dhandle = read_debugging_info (abfd, syms, symcount, TRUE);
155e0d23
NC
4077 if (dhandle != NULL)
4078 {
ed180cc5
AM
4079 if (!print_debugging_info (stdout, dhandle, abfd, syms,
4080 bfd_demangle,
4081 dump_debugging_tags ? TRUE : FALSE))
155e0d23
NC
4082 {
4083 non_fatal (_("%s: printing debugging information failed"),
4084 bfd_get_filename (abfd));
4085 exit_status = 1;
4086 }
cf0ad5bb
NC
4087
4088 free (dhandle);
155e0d23 4089 }
fdef3943
AM
4090 /* PR 6483: If there was no STABS debug info in the file, try
4091 DWARF instead. */
b922d590
NC
4092 else if (! dump_dwarf_section_info)
4093 {
3aade688 4094 dwarf_select_sections_all ();
b922d590
NC
4095 dump_dwarf (abfd);
4096 }
155e0d23
NC
4097 }
4098
4099 if (syms)
4100 {
4101 free (syms);
4102 syms = NULL;
4103 }
4104
4105 if (dynsyms)
4106 {
4107 free (dynsyms);
4108 dynsyms = NULL;
4109 }
4c45e5c9
JJ
4110
4111 if (synthsyms)
4112 {
4113 free (synthsyms);
4114 synthsyms = NULL;
4115 }
4116
4117 symcount = 0;
4118 dynsymcount = 0;
4119 synthcount = 0;
39f0547e
NC
4120
4121 if (is_mainfile)
4122 free_debug_memory ();
155e0d23
NC
4123}
4124
4125static void
1598539f 4126display_object_bfd (bfd *abfd)
155e0d23
NC
4127{
4128 char **matching;
4129
4130 if (bfd_check_format_matches (abfd, bfd_object, &matching))
4131 {
39f0547e 4132 dump_bfd (abfd, TRUE);
155e0d23
NC
4133 return;
4134 }
4135
4136 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
4137 {
4138 nonfatal (bfd_get_filename (abfd));
4139 list_matching_formats (matching);
4140 free (matching);
4141 return;
4142 }
4143
4144 if (bfd_get_error () != bfd_error_file_not_recognized)
4145 {
4146 nonfatal (bfd_get_filename (abfd));
4147 return;
4148 }
4149
4150 if (bfd_check_format_matches (abfd, bfd_core, &matching))
4151 {
39f0547e 4152 dump_bfd (abfd, TRUE);
155e0d23
NC
4153 return;
4154 }
4155
4156 nonfatal (bfd_get_filename (abfd));
4157
4158 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
4159 {
4160 list_matching_formats (matching);
4161 free (matching);
4162 }
4163}
4164
4165static void
1598539f 4166display_any_bfd (bfd *file, int level)
155e0d23 4167{
4a114e3e
L
4168 /* Decompress sections unless dumping the section contents. */
4169 if (!dump_section_contents)
4170 file->flags |= BFD_DECOMPRESS;
4171
155e0d23
NC
4172 /* If the file is an archive, process all of its elements. */
4173 if (bfd_check_format (file, bfd_archive))
4174 {
1598539f 4175 bfd *arfile = NULL;
155e0d23 4176 bfd *last_arfile = NULL;
bdc4de1b 4177
1598539f 4178 if (level == 0)
12add40e 4179 printf (_("In archive %s:\n"), sanitize_string (bfd_get_filename (file)));
c88f5b8e
NC
4180 else if (level > 100)
4181 {
4182 /* Prevent corrupted files from spinning us into an
4183 infinite loop. 100 is an arbitrary heuristic. */
64d29018 4184 fatal (_("Archive nesting is too deep"));
c88f5b8e
NC
4185 return;
4186 }
1598539f 4187 else
12add40e
NC
4188 printf (_("In nested archive %s:\n"),
4189 sanitize_string (bfd_get_filename (file)));
1598539f 4190
155e0d23
NC
4191 for (;;)
4192 {
4193 bfd_set_error (bfd_error_no_error);
4194
4195 arfile = bfd_openr_next_archived_file (file, arfile);
4196 if (arfile == NULL)
4197 {
4198 if (bfd_get_error () != bfd_error_no_more_archived_files)
4199 nonfatal (bfd_get_filename (file));
4200 break;
4201 }
4202
1598539f 4203 display_any_bfd (arfile, level + 1);
155e0d23
NC
4204
4205 if (last_arfile != NULL)
f64e188b
NC
4206 {
4207 bfd_close (last_arfile);
4208 /* PR 17512: file: ac585d01. */
4209 if (arfile == last_arfile)
4210 {
4211 last_arfile = NULL;
4212 break;
4213 }
4214 }
155e0d23
NC
4215 last_arfile = arfile;
4216 }
4217
4218 if (last_arfile != NULL)
4219 bfd_close (last_arfile);
4220 }
4221 else
1598539f
TG
4222 display_object_bfd (file);
4223}
4224
4225static void
cd6581da 4226display_file (char *filename, char *target, bfd_boolean last_file)
1598539f
TG
4227{
4228 bfd *file;
4229
4230 if (get_file_size (filename) < 1)
4231 {
4232 exit_status = 1;
4233 return;
4234 }
4235
4236 file = bfd_openr (filename, target);
4237 if (file == NULL)
4238 {
4239 nonfatal (filename);
4240 return;
4241 }
4242
4243 display_any_bfd (file, 0);
155e0d23 4244
cd6581da
NC
4245 /* This is an optimization to improve the speed of objdump, especially when
4246 dumping a file with lots of associated debug informatiom. Calling
4247 bfd_close on such a file can take a non-trivial amount of time as there
4248 are lots of lists to walk and buffers to free. This is only really
4249 necessary however if we are about to load another file and we need the
4250 memory back. Otherwise, if we are about to exit, then we can save (a lot
4251 of) time by only doing a quick close, and allowing the OS to reclaim the
4252 memory for us. */
4253 if (! last_file)
4254 bfd_close (file);
4255 else
4256 bfd_close_all_done (file);
155e0d23 4257}
252b5132 4258\f
252b5132 4259int
46dca2e0 4260main (int argc, char **argv)
252b5132
RH
4261{
4262 int c;
4263 char *target = default_target;
b34976b6 4264 bfd_boolean seenflag = FALSE;
252b5132 4265
155e0d23
NC
4266#if defined (HAVE_SETLOCALE)
4267#if defined (HAVE_LC_MESSAGES)
252b5132 4268 setlocale (LC_MESSAGES, "");
3882b010 4269#endif
3882b010 4270 setlocale (LC_CTYPE, "");
252b5132 4271#endif
155e0d23 4272
252b5132
RH
4273 bindtextdomain (PACKAGE, LOCALEDIR);
4274 textdomain (PACKAGE);
4275
4276 program_name = *argv;
4277 xmalloc_set_program_name (program_name);
86eafac0 4278 bfd_set_error_program_name (program_name);
252b5132
RH
4279
4280 START_PROGRESS (program_name, 0);
4281
869b9d07
MM
4282 expandargv (&argc, &argv);
4283
bf2dd8d7
AM
4284 if (bfd_init () != BFD_INIT_MAGIC)
4285 fatal (_("fatal error: libbfd ABI mismatch"));
252b5132
RH
4286 set_default_bfd_target ();
4287
4cb93e3b 4288 while ((c = getopt_long (argc, argv,
6abcee90 4289 "pP:ib:m:M:VvCdDlfFaHhrRtTxsSI:j:wE:zgeGW::",
252b5132
RH
4290 long_options, (int *) 0))
4291 != EOF)
4292 {
252b5132
RH
4293 switch (c)
4294 {
4295 case 0:
8b53311e 4296 break; /* We've been given a long option. */
252b5132
RH
4297 case 'm':
4298 machine = optarg;
4299 break;
dd92f639 4300 case 'M':
65b48a81
PB
4301 {
4302 char *options;
4303 if (disassembler_options)
4304 /* Ignore potential memory leak for now. */
4305 options = concat (disassembler_options, ",",
4306 optarg, (const char *) NULL);
4307 else
4308 options = optarg;
4309 disassembler_options = remove_whitespace_and_extra_commas (options);
4310 }
dd92f639 4311 break;
252b5132 4312 case 'j':
70ecb384 4313 add_only (optarg);
252b5132 4314 break;
98ec6e72
NC
4315 case 'F':
4316 display_file_offsets = TRUE;
4317 break;
252b5132 4318 case 'l':
b34976b6 4319 with_line_numbers = TRUE;
252b5132
RH
4320 break;
4321 case 'b':
4322 target = optarg;
4323 break;
1dada9c5 4324 case 'C':
b34976b6 4325 do_demangle = TRUE;
28c309a2
NC
4326 if (optarg != NULL)
4327 {
4328 enum demangling_styles style;
8b53311e 4329
28c309a2 4330 style = cplus_demangle_name_to_style (optarg);
0af11b59 4331 if (style == unknown_demangling)
28c309a2
NC
4332 fatal (_("unknown demangling style `%s'"),
4333 optarg);
8b53311e 4334
28c309a2 4335 cplus_demangle_set_style (style);
0af11b59 4336 }
1dada9c5 4337 break;
af03af8f
NC
4338 case OPTION_RECURSE_LIMIT:
4339 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
4340 break;
4341 case OPTION_NO_RECURSE_LIMIT:
4342 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
4343 break;
1dada9c5 4344 case 'w':
7b5d4822 4345 do_wide = wide_output = TRUE;
1dada9c5
NC
4346 break;
4347 case OPTION_ADJUST_VMA:
4348 adjust_section_vma = parse_vma (optarg, "--adjust-vma");
4349 break;
4350 case OPTION_START_ADDRESS:
4351 start_address = parse_vma (optarg, "--start-address");
98ec6e72
NC
4352 if ((stop_address != (bfd_vma) -1) && stop_address <= start_address)
4353 fatal (_("error: the start address should be before the end address"));
1dada9c5
NC
4354 break;
4355 case OPTION_STOP_ADDRESS:
4356 stop_address = parse_vma (optarg, "--stop-address");
98ec6e72
NC
4357 if ((start_address != (bfd_vma) -1) && stop_address <= start_address)
4358 fatal (_("error: the stop address should be after the start address"));
1dada9c5 4359 break;
0dafdf3f
L
4360 case OPTION_PREFIX:
4361 prefix = optarg;
4362 prefix_length = strlen (prefix);
4363 /* Remove an unnecessary trailing '/' */
4364 while (IS_DIR_SEPARATOR (prefix[prefix_length - 1]))
4365 prefix_length--;
4366 break;
4367 case OPTION_PREFIX_STRIP:
4368 prefix_strip = atoi (optarg);
4369 if (prefix_strip < 0)
4370 fatal (_("error: prefix strip must be non-negative"));
4371 break;
3dcb3fcb
L
4372 case OPTION_INSN_WIDTH:
4373 insn_width = strtoul (optarg, NULL, 0);
4374 if (insn_width <= 0)
4375 fatal (_("error: instruction width must be positive"));
4376 break;
4a14e306
AK
4377 case OPTION_INLINES:
4378 unwind_inlines = TRUE;
4379 break;
1dada9c5
NC
4380 case 'E':
4381 if (strcmp (optarg, "B") == 0)
4382 endian = BFD_ENDIAN_BIG;
4383 else if (strcmp (optarg, "L") == 0)
4384 endian = BFD_ENDIAN_LITTLE;
4385 else
4386 {
a8c62f1c 4387 nonfatal (_("unrecognized -E option"));
1dada9c5
NC
4388 usage (stderr, 1);
4389 }
4390 break;
4391 case OPTION_ENDIAN:
4392 if (strncmp (optarg, "big", strlen (optarg)) == 0)
4393 endian = BFD_ENDIAN_BIG;
4394 else if (strncmp (optarg, "little", strlen (optarg)) == 0)
4395 endian = BFD_ENDIAN_LITTLE;
4396 else
4397 {
37cc8ec1 4398 non_fatal (_("unrecognized --endian type `%s'"), optarg);
a8c62f1c 4399 exit_status = 1;
1dada9c5
NC
4400 usage (stderr, 1);
4401 }
4402 break;
8b53311e 4403
252b5132 4404 case 'f':
b34976b6
AM
4405 dump_file_header = TRUE;
4406 seenflag = TRUE;
252b5132
RH
4407 break;
4408 case 'i':
b34976b6
AM
4409 formats_info = TRUE;
4410 seenflag = TRUE;
252b5132 4411 break;
43ac9881
AM
4412 case 'I':
4413 add_include_path (optarg);
4414 break;
252b5132 4415 case 'p':
b34976b6
AM
4416 dump_private_headers = TRUE;
4417 seenflag = TRUE;
252b5132 4418 break;
6abcee90
TG
4419 case 'P':
4420 dump_private_options = optarg;
4421 seenflag = TRUE;
4422 break;
252b5132 4423 case 'x':
b34976b6
AM
4424 dump_private_headers = TRUE;
4425 dump_symtab = TRUE;
4426 dump_reloc_info = TRUE;
4427 dump_file_header = TRUE;
4428 dump_ar_hdrs = TRUE;
4429 dump_section_headers = TRUE;
4430 seenflag = TRUE;
252b5132
RH
4431 break;
4432 case 't':
b34976b6
AM
4433 dump_symtab = TRUE;
4434 seenflag = TRUE;
252b5132
RH
4435 break;
4436 case 'T':
b34976b6
AM
4437 dump_dynamic_symtab = TRUE;
4438 seenflag = TRUE;
252b5132
RH
4439 break;
4440 case 'd':
b34976b6
AM
4441 disassemble = TRUE;
4442 seenflag = TRUE;
d3def5d7 4443 disasm_sym = optarg;
1dada9c5
NC
4444 break;
4445 case 'z':
b34976b6 4446 disassemble_zeroes = TRUE;
252b5132
RH
4447 break;
4448 case 'D':
b34976b6
AM
4449 disassemble = TRUE;
4450 disassemble_all = TRUE;
4451 seenflag = TRUE;
252b5132
RH
4452 break;
4453 case 'S':
b34976b6
AM
4454 disassemble = TRUE;
4455 with_source_code = TRUE;
4456 seenflag = TRUE;
1dada9c5
NC
4457 break;
4458 case 'g':
4459 dump_debugging = 1;
b34976b6 4460 seenflag = TRUE;
1dada9c5 4461 break;
51cdc6e0
NC
4462 case 'e':
4463 dump_debugging = 1;
4464 dump_debugging_tags = 1;
4465 do_demangle = TRUE;
4466 seenflag = TRUE;
4467 break;
365544c3
L
4468 case 'W':
4469 dump_dwarf_section_info = TRUE;
4470 seenflag = TRUE;
4cb93e3b
TG
4471 if (optarg)
4472 dwarf_select_sections_by_letters (optarg);
4473 else
4474 dwarf_select_sections_all ();
4475 break;
4476 case OPTION_DWARF:
4477 dump_dwarf_section_info = TRUE;
4478 seenflag = TRUE;
4479 if (optarg)
4480 dwarf_select_sections_by_names (optarg);
4481 else
4482 dwarf_select_sections_all ();
365544c3 4483 break;
fd2f0033
TT
4484 case OPTION_DWARF_DEPTH:
4485 {
4486 char *cp;
4487 dwarf_cutoff_level = strtoul (optarg, & cp, 0);
4488 }
4489 break;
4490 case OPTION_DWARF_START:
4491 {
4492 char *cp;
4493 dwarf_start_die = strtoul (optarg, & cp, 0);
4494 suppress_bfd_header = 1;
4495 }
4496 break;
4723351a
CC
4497 case OPTION_DWARF_CHECK:
4498 dwarf_check = TRUE;
4499 break;
7d9813f1
NA
4500 case OPTION_CTF:
4501 dump_ctf_section_info = TRUE;
4502 dump_ctf_section_name = xstrdup (optarg);
4503 seenflag = TRUE;
4504 break;
4505 case OPTION_CTF_PARENT:
4506 dump_ctf_parent_name = xstrdup (optarg);
4507 break;
1dada9c5 4508 case 'G':
b34976b6
AM
4509 dump_stab_section_info = TRUE;
4510 seenflag = TRUE;
252b5132
RH
4511 break;
4512 case 's':
b34976b6
AM
4513 dump_section_contents = TRUE;
4514 seenflag = TRUE;
252b5132
RH
4515 break;
4516 case 'r':
b34976b6
AM
4517 dump_reloc_info = TRUE;
4518 seenflag = TRUE;
252b5132
RH
4519 break;
4520 case 'R':
b34976b6
AM
4521 dump_dynamic_reloc_info = TRUE;
4522 seenflag = TRUE;
252b5132
RH
4523 break;
4524 case 'a':
b34976b6
AM
4525 dump_ar_hdrs = TRUE;
4526 seenflag = TRUE;
252b5132
RH
4527 break;
4528 case 'h':
b34976b6
AM
4529 dump_section_headers = TRUE;
4530 seenflag = TRUE;
252b5132 4531 break;
8b53311e 4532 case 'v':
252b5132 4533 case 'V':
b34976b6
AM
4534 show_version = TRUE;
4535 seenflag = TRUE;
252b5132 4536 break;
0af11b59 4537
aebcf7b7
NC
4538 case 'H':
4539 usage (stdout, 0);
4540 /* No need to set seenflag or to break - usage() does not return. */
252b5132
RH
4541 default:
4542 usage (stderr, 1);
4543 }
4544 }
4545
4546 if (show_version)
4547 print_version ("objdump");
4548
b34976b6 4549 if (!seenflag)
1dada9c5 4550 usage (stderr, 2);
252b5132
RH
4551
4552 if (formats_info)
06d86cf7 4553 exit_status = display_info ();
252b5132
RH
4554 else
4555 {
4556 if (optind == argc)
cd6581da 4557 display_file ("a.out", target, TRUE);
252b5132
RH
4558 else
4559 for (; optind < argc;)
cd6581da
NC
4560 {
4561 display_file (argv[optind], target, optind == argc - 1);
4562 optind++;
4563 }
252b5132
RH
4564 }
4565
70ecb384 4566 free_only_list ();
7d9813f1
NA
4567 free (dump_ctf_section_name);
4568 free (dump_ctf_parent_name);
79b377b3 4569
252b5132
RH
4570 END_PROGRESS (program_name);
4571
75cd796a 4572 return exit_status;
252b5132 4573}
This page took 1.320941 seconds and 4 git commands to generate.