]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - binutils/objdump.c
objdump segfault after symbol table error
[thirdparty/binutils-gdb.git] / binutils / objdump.c
CommitLineData
252b5132 1/* objdump.c -- dump information about an object file.
d87bef3a 2 Copyright (C) 1990-2023 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 55#include "bucomm.h"
3284fe0c 56#include "elfcomm.h"
0d646226 57#include "demanguse.h"
365544c3 58#include "dwarf.h"
7d9813f1 59#include "ctf-api.h"
42b6953b 60#include "sframe-api.h"
d7a283d4 61#include "getopt.h"
3882b010 62#include "safe-ctype.h"
252b5132
RH
63#include "dis-asm.h"
64#include "libiberty.h"
65#include "demangle.h"
0dafdf3f 66#include "filenames.h"
252b5132
RH
67#include "debug.h"
68#include "budbg.h"
6abcee90 69#include "objdump.h"
252b5132 70
e8f5eee4
NC
71#ifdef HAVE_MMAP
72#include <sys/mman.h>
73#endif
74
d647c797
AM
75#ifdef HAVE_LIBDEBUGINFOD
76#include <elfutils/debuginfod.h>
77#endif
78
252b5132
RH
79/* Internal headers for the ELF .stab-dump code - sorry. */
80#define BYTES_IN_WORD 32
81#include "aout/aout64.h"
82
75cd796a
ILT
83/* Exit status. */
84static int exit_status = 0;
85
98a91d6a 86static char *default_target = NULL; /* Default at runtime. */
252b5132 87
3b9ad1cc
AM
88/* The following variables are set based on arguments passed on the
89 command line. */
98a91d6a 90static int show_version = 0; /* Show the version number. */
252b5132
RH
91static int dump_section_contents; /* -s */
92static int dump_section_headers; /* -h */
015dc7e1 93static bool dump_file_header; /* -f */
252b5132
RH
94static int dump_symtab; /* -t */
95static int dump_dynamic_symtab; /* -T */
96static int dump_reloc_info; /* -r */
97static int dump_dynamic_reloc_info; /* -R */
98static int dump_ar_hdrs; /* -a */
99static int dump_private_headers; /* -p */
6abcee90 100static char *dump_private_options; /* -P */
b1bc1394 101static int no_addresses; /* --no-addresses */
252b5132
RH
102static int prefix_addresses; /* --prefix-addresses */
103static int with_line_numbers; /* -l */
015dc7e1 104static bool with_source_code; /* -S */
252b5132 105static int show_raw_insn; /* --show-raw-insn */
365544c3 106static int dump_dwarf_section_info; /* --dwarf */
252b5132 107static int dump_stab_section_info; /* --stabs */
7d9813f1
NA
108static int dump_ctf_section_info; /* --ctf */
109static char *dump_ctf_section_name;
110static char *dump_ctf_parent_name; /* --ctf-parent */
42b6953b
IB
111static int dump_sframe_section_info; /* --sframe */
112static char *dump_sframe_section_name;
252b5132 113static int do_demangle; /* -C, --demangle */
015dc7e1
AM
114static bool disassemble; /* -d */
115static bool disassemble_all; /* -D */
252b5132 116static int disassemble_zeroes; /* --disassemble-zeroes */
015dc7e1 117static bool formats_info; /* -i */
252b5132 118static int wide_output; /* -w */
3dcb3fcb 119static int insn_width; /* --insn-width */
252b5132
RH
120static bfd_vma start_address = (bfd_vma) -1; /* --start-address */
121static bfd_vma stop_address = (bfd_vma) -1; /* --stop-address */
122static int dump_debugging; /* --debugging */
51cdc6e0 123static int dump_debugging_tags; /* --debugging-tags */
fd2f0033 124static int suppress_bfd_header;
3c9458e9 125static int dump_special_syms = 0; /* --special-syms */
252b5132 126static bfd_vma adjust_section_vma = 0; /* --adjust-vma */
f1563258 127static int file_start_context = 0; /* --file-start-context */
015dc7e1 128static bool display_file_offsets; /* -F */
0dafdf3f
L
129static const char *prefix; /* --prefix */
130static int prefix_strip; /* --prefix-strip */
131static size_t prefix_length;
015dc7e1 132static bool unwind_inlines; /* --inlines. */
d3def5d7 133static const char * disasm_sym; /* Disassembly start symbol. */
a1c110a3 134static const char * source_comment; /* --source_comment. */
015dc7e1
AM
135static bool visualize_jumps = false; /* --visualize-jumps. */
136static bool color_output = false; /* --visualize-jumps=color. */
137static bool extended_color_output = false; /* --visualize-jumps=extended-color. */
138static int process_links = false; /* --process-links. */
c7ce51d8 139static int show_all_symbols; /* --show-all-symbols. */
a88c79b7
NC
140
141static enum color_selection
142 {
143 on_if_terminal_output,
144 on, /* --disassembler-color=color. */
145 off, /* --disassembler-color=off. */
146 extended /* --disassembler-color=extended-color. */
18bf5643
NC
147 } disassembler_color =
148#if DEFAULT_FOR_COLORED_DISASSEMBLY
149 on_if_terminal_output;
150#else
151 off;
152#endif
252b5132 153
e1dbfc17 154static int dump_any_debugging;
af03af8f
NC
155static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
156
60a3da00
AB
157/* This is reset to false each time we enter the disassembler, and set true
158 when the disassembler emits something in the dis_style_comment_start
159 style. Once this is true, all further output on that line is done in
160 the comment style. This only has an effect when disassembler coloring
161 is turned on. */
162static bool disassembler_in_comment = false;
163
70ecb384
NC
164/* A structure to record the sections mentioned in -j switches. */
165struct only
166{
015dc7e1
AM
167 const char *name; /* The name of the section. */
168 bool seen; /* A flag to indicate that the section has been found in one or more input files. */
169 struct only *next; /* Pointer to the next structure in the list. */
70ecb384
NC
170};
171/* Pointer to an array of 'only' structures.
172 This pointer is NULL if the -j switch has not been used. */
173static struct only * only_list = NULL;
155e0d23 174
43ac9881
AM
175/* Variables for handling include file path table. */
176static const char **include_paths;
177static int include_path_count;
178
3b9ad1cc
AM
179/* Extra info to pass to the section disassembler and address printing
180 function. */
026df7c5
NC
181struct objdump_disasm_info
182{
015dc7e1
AM
183 bfd *abfd;
184 bool require_sec;
155e0d23 185 disassembler_ftype disassemble_fn;
015dc7e1
AM
186 arelent *reloc;
187 const char *symbol;
252b5132
RH
188};
189
190/* Architecture to disassemble for, or default if NULL. */
d3ba0551 191static char *machine = NULL;
252b5132 192
dd92f639 193/* Target specific options to the disassembler. */
d3ba0551 194static char *disassembler_options = NULL;
dd92f639 195
252b5132
RH
196/* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */
197static enum bfd_endian endian = BFD_ENDIAN_UNKNOWN;
198
199/* The symbol table. */
200static asymbol **syms;
201
202/* Number of symbols in `syms'. */
203static long symcount = 0;
204
205/* The sorted symbol table. */
206static asymbol **sorted_syms;
207
208/* Number of symbols in `sorted_syms'. */
209static long sorted_symcount = 0;
210
211/* The dynamic symbol table. */
212static asymbol **dynsyms;
213
4c45e5c9
JJ
214/* The synthetic symbol table. */
215static asymbol *synthsyms;
216static long synthcount = 0;
217
252b5132
RH
218/* Number of symbols in `dynsyms'. */
219static long dynsymcount = 0;
220
98a91d6a
NC
221static bfd_byte *stabs;
222static bfd_size_type stab_size;
223
bae7501e 224static bfd_byte *strtab;
98a91d6a 225static bfd_size_type stabstr_size;
41e92641 226
6abcee90
TG
227/* Handlers for -P/--private. */
228static const struct objdump_private_desc * const objdump_private_vectors[] =
229 {
230 OBJDUMP_PRIVATE_VECTORS
231 NULL
232 };
1d67fe3b
TT
233
234/* The list of detected jumps inside a function. */
235static struct jump_info *detected_jumps = NULL;
b3aa80b4
NC
236
237typedef enum unicode_display_type
238{
239 unicode_default = 0,
240 unicode_locale,
241 unicode_escape,
242 unicode_hex,
243 unicode_highlight,
244 unicode_invalid
245} unicode_display_type;
246
247static unicode_display_type unicode_display = unicode_default;
252b5132 248\f
aebcf7b7 249static void usage (FILE *, int) ATTRIBUTE_NORETURN;
252b5132 250static void
46dca2e0 251usage (FILE *stream, int status)
252b5132 252{
8b53311e
NC
253 fprintf (stream, _("Usage: %s <option(s)> <file(s)>\n"), program_name);
254 fprintf (stream, _(" Display information from object <file(s)>.\n"));
255 fprintf (stream, _(" At least one of the following switches must be given:\n"));
252b5132 256 fprintf (stream, _("\
d6249f5f
AM
257 -a, --archive-headers Display archive header information\n"));
258 fprintf (stream, _("\
259 -f, --file-headers Display the contents of the overall file header\n"));
260 fprintf (stream, _("\
261 -p, --private-headers Display object format specific file header contents\n"));
262 fprintf (stream, _("\
263 -P, --private=OPT,OPT... Display object format specific contents\n"));
264 fprintf (stream, _("\
265 -h, --[section-]headers Display the contents of the section headers\n"));
266 fprintf (stream, _("\
267 -x, --all-headers Display the contents of all headers\n"));
268 fprintf (stream, _("\
269 -d, --disassemble Display assembler contents of executable sections\n"));
270 fprintf (stream, _("\
271 -D, --disassemble-all Display assembler contents of all sections\n"));
272 fprintf (stream, _("\
273 --disassemble=<sym> Display assembler contents from <sym>\n"));
274 fprintf (stream, _("\
275 -S, --source Intermix source code with disassembly\n"));
276 fprintf (stream, _("\
277 --source-comment[=<txt>] Prefix lines of source code with <txt>\n"));
278 fprintf (stream, _("\
279 -s, --full-contents Display the full contents of all sections requested\n"));
280 fprintf (stream, _("\
281 -g, --debugging Display debug information in object file\n"));
282 fprintf (stream, _("\
283 -e, --debugging-tags Display debug information using ctags style\n"));
284 fprintf (stream, _("\
285 -G, --stabs Display (in raw form) any STABS info in the file\n"));
286 fprintf (stream, _("\
287 -W, --dwarf[a/=abbrev, A/=addr, r/=aranges, c/=cu_index, L/=decodedline,\n\
288 f/=frames, F/=frames-interp, g/=gdb_index, i/=info, o/=loc,\n\
289 m/=macro, p/=pubnames, t/=pubtypes, R/=Ranges, l/=rawline,\n\
290 s/=str, O/=str-offsets, u/=trace_abbrev, T/=trace_aranges,\n\
291 U/=trace_info]\n\
292 Display the contents of DWARF debug sections\n"));
293 fprintf (stream, _("\
294 -Wk,--dwarf=links Display the contents of sections that link to\n\
295 separate debuginfo files\n"));
c46b7066
NC
296#if DEFAULT_FOR_FOLLOW_LINKS
297 fprintf (stream, _("\
63e47e10 298 -WK,--dwarf=follow-links\n\
d6249f5f
AM
299 Follow links to separate debug info files (default)\n"));
300 fprintf (stream, _("\
63e47e10 301 -WN,--dwarf=no-follow-links\n\
d6249f5f 302 Do not follow links to separate debug info files\n"));
c46b7066
NC
303#else
304 fprintf (stream, _("\
63e47e10 305 -WK,--dwarf=follow-links\n\
d6249f5f
AM
306 Follow links to separate debug info files\n"));
307 fprintf (stream, _("\
63e47e10 308 -WN,--dwarf=no-follow-links\n\
d6249f5f
AM
309 Do not follow links to separate debug info files\n\
310 (default)\n"));
bed566bb
NC
311#endif
312#if HAVE_LIBDEBUGINFOD
313 fprintf (stream, _("\
314 -WD --dwarf=use-debuginfod\n\
315 When following links, also query debuginfod servers (default)\n"));
316 fprintf (stream, _("\
317 -WE --dwarf=do-not-use-debuginfod\n\
318 When following links, do not query debuginfod servers\n"));
c46b7066 319#endif
ca0e11aa 320 fprintf (stream, _("\
d6249f5f
AM
321 -L, --process-links Display the contents of non-debug sections in\n\
322 separate debuginfo files. (Implies -WK)\n"));
094e34f2
NA
323#ifdef ENABLE_LIBCTF
324 fprintf (stream, _("\
10909ea8 325 --ctf[=SECTION] Display CTF info from SECTION, (default `.ctf')\n"));
094e34f2 326#endif
42b6953b
IB
327 fprintf (stream, _("\
328 --sframe[=SECTION] Display SFrame info from SECTION, (default '.sframe')\n"));
094e34f2 329 fprintf (stream, _("\
d6249f5f
AM
330 -t, --syms Display the contents of the symbol table(s)\n"));
331 fprintf (stream, _("\
332 -T, --dynamic-syms Display the contents of the dynamic symbol table\n"));
333 fprintf (stream, _("\
334 -r, --reloc Display the relocation entries in the file\n"));
335 fprintf (stream, _("\
336 -R, --dynamic-reloc Display the dynamic relocation entries in the file\n"));
337 fprintf (stream, _("\
338 @<file> Read options from <file>\n"));
339 fprintf (stream, _("\
340 -v, --version Display this program's version number\n"));
341 fprintf (stream, _("\
342 -i, --info List object formats and architectures supported\n"));
343 fprintf (stream, _("\
344 -H, --help Display this information\n"));
345
1dada9c5
NC
346 if (status != 2)
347 {
6abcee90
TG
348 const struct objdump_private_desc * const *desc;
349
1dada9c5
NC
350 fprintf (stream, _("\n The following switches are optional:\n"));
351 fprintf (stream, _("\
d6249f5f
AM
352 -b, --target=BFDNAME Specify the target object format as BFDNAME\n"));
353 fprintf (stream, _("\
354 -m, --architecture=MACHINE Specify the target architecture as MACHINE\n"));
355 fprintf (stream, _("\
356 -j, --section=NAME Only display information for section NAME\n"));
357 fprintf (stream, _("\
358 -M, --disassembler-options=OPT Pass text OPT on to the disassembler\n"));
359 fprintf (stream, _("\
360 -EB --endian=big Assume big endian format when disassembling\n"));
361 fprintf (stream, _("\
362 -EL --endian=little Assume little endian format when disassembling\n"));
363 fprintf (stream, _("\
364 --file-start-context Include context from start of file (with -S)\n"));
365 fprintf (stream, _("\
366 -I, --include=DIR Add DIR to search list for source files\n"));
367 fprintf (stream, _("\
368 -l, --line-numbers Include line numbers and filenames in output\n"));
369 fprintf (stream, _("\
370 -F, --file-offsets Include file offsets when displaying information\n"));
371 fprintf (stream, _("\
0d646226
AM
372 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n"));
373 display_demangler_styles (stream, _("\
374 STYLE can be "));
d6249f5f
AM
375 fprintf (stream, _("\
376 --recurse-limit Enable a limit on recursion whilst demangling\n\
377 (default)\n"));
378 fprintf (stream, _("\
379 --no-recurse-limit Disable a limit on recursion whilst demangling\n"));
380 fprintf (stream, _("\
381 -w, --wide Format output for more than 80 columns\n"));
382 fprintf (stream, _("\
b3aa80b4
NC
383 -U[d|l|i|x|e|h] Controls the display of UTF-8 unicode characters\n\
384 --unicode=[default|locale|invalid|hex|escape|highlight]\n"));
385 fprintf (stream, _("\
d6249f5f
AM
386 -z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling\n"));
387 fprintf (stream, _("\
388 --start-address=ADDR Only process data whose address is >= ADDR\n"));
389 fprintf (stream, _("\
390 --stop-address=ADDR Only process data whose address is < ADDR\n"));
391 fprintf (stream, _("\
392 --no-addresses Do not print address alongside disassembly\n"));
393 fprintf (stream, _("\
394 --prefix-addresses Print complete address alongside disassembly\n"));
395 fprintf (stream, _("\
396 --[no-]show-raw-insn Display hex alongside symbolic disassembly\n"));
397 fprintf (stream, _("\
398 --insn-width=WIDTH Display WIDTH bytes on a single line for -d\n"));
399 fprintf (stream, _("\
400 --adjust-vma=OFFSET Add OFFSET to all displayed section addresses\n"));
401 fprintf (stream, _("\
c7ce51d8
NC
402 --show-all-symbols When disassembling, display all symbols at a given address\n"));
403 fprintf (stream, _("\
d6249f5f
AM
404 --special-syms Include special symbols in symbol dumps\n"));
405 fprintf (stream, _("\
406 --inlines Print all inlines for source line (with -l)\n"));
407 fprintf (stream, _("\
408 --prefix=PREFIX Add PREFIX to absolute paths for -S\n"));
409 fprintf (stream, _("\
fd2f0033
TT
410 --prefix-strip=LEVEL Strip initial directory names for -S\n"));
411 fprintf (stream, _("\
d6249f5f
AM
412 --dwarf-depth=N Do not display DIEs at depth N or greater\n"));
413 fprintf (stream, _("\
414 --dwarf-start=N Display DIEs starting at offset N\n"));
415 fprintf (stream, _("\
416 --dwarf-check Make additional dwarf consistency checks.\n"));
094e34f2
NA
417#ifdef ENABLE_LIBCTF
418 fprintf (stream, _("\
80b56fad 419 --ctf-parent=NAME Use CTF archive member NAME as the CTF parent\n"));
094e34f2
NA
420#endif
421 fprintf (stream, _("\
d6249f5f
AM
422 --visualize-jumps Visualize jumps by drawing ASCII art lines\n"));
423 fprintf (stream, _("\
424 --visualize-jumps=color Use colors in the ASCII art\n"));
425 fprintf (stream, _("\
426 --visualize-jumps=extended-color\n\
427 Use extended 8-bit color codes\n"));
428 fprintf (stream, _("\
18bf5643
NC
429 --visualize-jumps=off Disable jump visualization\n"));
430#if DEFAULT_FOR_COLORED_DISASSEMBLY
60a3da00 431 fprintf (stream, _("\
18bf5643
NC
432 --disassembler-color=off Disable disassembler color output.\n"));
433 fprintf (stream, _("\
434 --disassembler-color=terminal Enable disassembler color output if displaying on a terminal. (default)\n"));
435#else
436 fprintf (stream, _("\
437 --disassembler-color=off Disable disassembler color output. (default)\n"));
438 fprintf (stream, _("\
439 --disassembler-color=terminal Enable disassembler color output if displaying on a terminal.\n"));
440#endif
60a3da00 441 fprintf (stream, _("\
18bf5643 442 --disassembler-color=on Enable disassembler color output.\n"));
a88c79b7 443 fprintf (stream, _("\
18bf5643 444 --disassembler-color=extended Use 8-bit colors in disassembler output.\n\n"));
1d67fe3b 445
1dada9c5 446 list_supported_targets (program_name, stream);
2f83960e 447 list_supported_architectures (program_name, stream);
86d65c94 448
94470b23 449 disassembler_usage (stream);
6abcee90
TG
450
451 if (objdump_private_vectors[0] != NULL)
452 {
453 fprintf (stream,
454 _("\nOptions supported for -P/--private switch:\n"));
455 for (desc = objdump_private_vectors; *desc != NULL; desc++)
456 (*desc)->help (stream);
457 }
1dada9c5 458 }
92f01d61 459 if (REPORT_BUGS_TO[0] && status == 0)
86d65c94 460 fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
252b5132
RH
461 exit (status);
462}
463
464/* 150 isn't special; it's just an arbitrary non-ASCII char value. */
46dca2e0
NC
465enum option_values
466 {
467 OPTION_ENDIAN=150,
468 OPTION_START_ADDRESS,
469 OPTION_STOP_ADDRESS,
4cb93e3b 470 OPTION_DWARF,
0dafdf3f
L
471 OPTION_PREFIX,
472 OPTION_PREFIX_STRIP,
3dcb3fcb 473 OPTION_INSN_WIDTH,
fd2f0033
TT
474 OPTION_ADJUST_VMA,
475 OPTION_DWARF_DEPTH,
4723351a 476 OPTION_DWARF_CHECK,
4a14e306 477 OPTION_DWARF_START,
af03af8f
NC
478 OPTION_RECURSE_LIMIT,
479 OPTION_NO_RECURSE_LIMIT,
79b377b3 480 OPTION_INLINES,
a1c110a3 481 OPTION_SOURCE_COMMENT,
094e34f2 482#ifdef ENABLE_LIBCTF
7d9813f1 483 OPTION_CTF,
1d67fe3b 484 OPTION_CTF_PARENT,
094e34f2 485#endif
42b6953b 486 OPTION_SFRAME,
60a3da00
AB
487 OPTION_VISUALIZE_JUMPS,
488 OPTION_DISASSEMBLER_COLOR
46dca2e0 489 };
252b5132
RH
490
491static struct option long_options[]=
492{
493 {"adjust-vma", required_argument, NULL, OPTION_ADJUST_VMA},
494 {"all-headers", no_argument, NULL, 'x'},
252b5132
RH
495 {"architecture", required_argument, NULL, 'm'},
496 {"archive-headers", no_argument, NULL, 'a'},
b3aa80b4 497#ifdef ENABLE_LIBCTF
9b49454b 498 {"ctf", optional_argument, NULL, OPTION_CTF},
b3aa80b4
NC
499 {"ctf-parent", required_argument, NULL, OPTION_CTF_PARENT},
500#endif
1dada9c5 501 {"debugging", no_argument, NULL, 'g'},
51cdc6e0 502 {"debugging-tags", no_argument, NULL, 'e'},
28c309a2 503 {"demangle", optional_argument, NULL, 'C'},
d3def5d7 504 {"disassemble", optional_argument, NULL, 'd'},
252b5132 505 {"disassemble-all", no_argument, NULL, 'D'},
1dada9c5 506 {"disassemble-zeroes", no_argument, NULL, 'z'},
b3aa80b4
NC
507 {"disassembler-options", required_argument, NULL, 'M'},
508 {"dwarf", optional_argument, NULL, OPTION_DWARF},
509 {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
510 {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
511 {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
252b5132
RH
512 {"dynamic-reloc", no_argument, NULL, 'R'},
513 {"dynamic-syms", no_argument, NULL, 'T'},
514 {"endian", required_argument, NULL, OPTION_ENDIAN},
515 {"file-headers", no_argument, NULL, 'f'},
98ec6e72 516 {"file-offsets", no_argument, NULL, 'F'},
f1563258 517 {"file-start-context", no_argument, &file_start_context, 1},
252b5132
RH
518 {"full-contents", no_argument, NULL, 's'},
519 {"headers", no_argument, NULL, 'h'},
520 {"help", no_argument, NULL, 'H'},
b3aa80b4 521 {"include", required_argument, NULL, 'I'},
252b5132 522 {"info", no_argument, NULL, 'i'},
b3aa80b4
NC
523 {"inlines", no_argument, 0, OPTION_INLINES},
524 {"insn-width", required_argument, NULL, OPTION_INSN_WIDTH},
252b5132 525 {"line-numbers", no_argument, NULL, 'l'},
b1bc1394 526 {"no-addresses", no_argument, &no_addresses, 1},
b3aa80b4
NC
527 {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
528 {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
529 {"no-show-raw-insn", no_argument, &show_raw_insn, -1},
530 {"prefix", required_argument, NULL, OPTION_PREFIX},
252b5132 531 {"prefix-addresses", no_argument, &prefix_addresses, 1},
b3aa80b4
NC
532 {"prefix-strip", required_argument, NULL, OPTION_PREFIX_STRIP},
533 {"private", required_argument, NULL, 'P'},
534 {"private-headers", no_argument, NULL, 'p'},
535 {"process-links", no_argument, &process_links, true},
af03af8f
NC
536 {"recurse-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
537 {"recursion-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
252b5132
RH
538 {"reloc", no_argument, NULL, 'r'},
539 {"section", required_argument, NULL, 'j'},
540 {"section-headers", no_argument, NULL, 'h'},
42b6953b 541 {"sframe", optional_argument, NULL, OPTION_SFRAME},
c7ce51d8 542 {"show-all-symbols", no_argument, &show_all_symbols, 1},
252b5132
RH
543 {"show-raw-insn", no_argument, &show_raw_insn, 1},
544 {"source", no_argument, NULL, 'S'},
a1c110a3 545 {"source-comment", optional_argument, NULL, OPTION_SOURCE_COMMENT},
3c9458e9 546 {"special-syms", no_argument, &dump_special_syms, 1},
1dada9c5 547 {"stabs", no_argument, NULL, 'G'},
252b5132
RH
548 {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
549 {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
550 {"syms", no_argument, NULL, 't'},
551 {"target", required_argument, NULL, 'b'},
b3aa80b4 552 {"unicode", required_argument, NULL, 'U'},
1dada9c5 553 {"version", no_argument, NULL, 'V'},
1d67fe3b 554 {"visualize-jumps", optional_argument, 0, OPTION_VISUALIZE_JUMPS},
b3aa80b4 555 {"wide", no_argument, NULL, 'w'},
60a3da00 556 {"disassembler-color", required_argument, NULL, OPTION_DISASSEMBLER_COLOR},
b3aa80b4 557 {NULL, no_argument, NULL, 0}
252b5132
RH
558};
559\f
560static void
a734d906 561my_bfd_nonfatal (const char *msg)
75cd796a
ILT
562{
563 bfd_nonfatal (msg);
564 exit_status = 1;
565}
12add40e 566
b3aa80b4
NC
567/* Convert a potential UTF-8 encoded sequence in IN into characters in OUT.
568 The conversion format is controlled by the unicode_display variable.
569 Returns the number of characters added to OUT.
570 Returns the number of bytes consumed from IN in CONSUMED.
571 Always consumes at least one byte and displays at least one character. */
9b49454b 572
b3aa80b4
NC
573static unsigned int
574display_utf8 (const unsigned char * in, char * out, unsigned int * consumed)
575{
576 char * orig_out = out;
577 unsigned int nchars = 0;
578 unsigned int j;
579
580 if (unicode_display == unicode_default)
581 goto invalid;
582
583 if (in[0] < 0xc0)
584 goto invalid;
585
586 if ((in[1] & 0xc0) != 0x80)
587 goto invalid;
588
589 if ((in[0] & 0x20) == 0)
590 {
591 nchars = 2;
592 goto valid;
593 }
594
595 if ((in[2] & 0xc0) != 0x80)
596 goto invalid;
597
598 if ((in[0] & 0x10) == 0)
599 {
600 nchars = 3;
601 goto valid;
602 }
603
604 if ((in[3] & 0xc0) != 0x80)
605 goto invalid;
606
607 nchars = 4;
608
609 valid:
610 switch (unicode_display)
611 {
612 case unicode_locale:
613 /* Copy the bytes into the output buffer as is. */
614 memcpy (out, in, nchars);
615 out += nchars;
616 break;
617
618 case unicode_invalid:
619 case unicode_hex:
620 out += sprintf (out, "%c", unicode_display == unicode_hex ? '<' : '{');
621 out += sprintf (out, "0x");
622 for (j = 0; j < nchars; j++)
623 out += sprintf (out, "%02x", in [j]);
624 out += sprintf (out, "%c", unicode_display == unicode_hex ? '>' : '}');
625 break;
9b49454b 626
b3aa80b4
NC
627 case unicode_highlight:
628 if (isatty (1))
629 out += sprintf (out, "\x1B[31;47m"); /* Red. */
630 /* Fall through. */
631 case unicode_escape:
632 switch (nchars)
633 {
634 case 2:
635 out += sprintf (out, "\\u%02x%02x",
9b49454b 636 ((in[0] & 0x1c) >> 2),
b3aa80b4
NC
637 ((in[0] & 0x03) << 6) | (in[1] & 0x3f));
638 break;
639
640 case 3:
641 out += sprintf (out, "\\u%02x%02x",
642 ((in[0] & 0x0f) << 4) | ((in[1] & 0x3c) >> 2),
643 ((in[1] & 0x03) << 6) | ((in[2] & 0x3f)));
644 break;
645
646 case 4:
647 out += sprintf (out, "\\u%02x%02x%02x",
648 ((in[0] & 0x07) << 6) | ((in[1] & 0x3c) >> 2),
649 ((in[1] & 0x03) << 6) | ((in[2] & 0x3c) >> 2),
650 ((in[2] & 0x03) << 6) | ((in[3] & 0x3f)));
651 break;
652 default:
653 /* URG. */
654 break;
655 }
656
657 if (unicode_display == unicode_highlight && isatty (1))
658 out += sprintf (out, "\033[0m"); /* Default colour. */
659 break;
660
661 default:
662 /* URG */
663 break;
664 }
665
666 * consumed = nchars;
667 return out - orig_out;
668
669 invalid:
670 /* Not a valid UTF-8 sequence. */
671 *out = *in;
672 * consumed = 1;
673 return 1;
674}
675
12add40e
NC
676/* Returns a version of IN with any control characters
677 replaced by escape sequences. Uses a static buffer
b3aa80b4
NC
678 if necessary.
679
680 If unicode display is enabled, then also handles the
681 conversion of unicode characters. */
12add40e
NC
682
683static const char *
684sanitize_string (const char * in)
685{
63455780
NC
686 static char * buffer = NULL;
687 static size_t buffer_len = 0;
688 const char * original = in;
689 char * out;
12add40e
NC
690
691 /* Paranoia. */
692 if (in == NULL)
693 return "";
694
695 /* See if any conversion is necessary. In the majority
696 of cases it will not be needed. */
697 do
698 {
b3aa80b4 699 unsigned char c = *in++;
12add40e
NC
700
701 if (c == 0)
702 return original;
703
704 if (ISCNTRL (c))
705 break;
b3aa80b4
NC
706
707 if (unicode_display != unicode_default && c >= 0xc0)
708 break;
12add40e
NC
709 }
710 while (1);
711
712 /* Copy the input, translating as needed. */
713 in = original;
b3aa80b4 714 if (buffer_len < (strlen (in) * 9))
12add40e
NC
715 {
716 free ((void *) buffer);
b3aa80b4 717 buffer_len = strlen (in) * 9;
12add40e
NC
718 buffer = xmalloc (buffer_len + 1);
719 }
720
721 out = buffer;
722 do
723 {
b3aa80b4 724 unsigned char c = *in++;
12add40e
NC
725
726 if (c == 0)
727 break;
728
b3aa80b4 729 if (ISCNTRL (c))
12add40e
NC
730 {
731 *out++ = '^';
732 *out++ = c + 0x40;
733 }
b3aa80b4
NC
734 else if (unicode_display != unicode_default && c >= 0xc0)
735 {
736 unsigned int num_consumed;
737
738 out += display_utf8 ((const unsigned char *)(in - 1), out, & num_consumed);
739 in += num_consumed - 1;
740 }
741 else
742 *out++ = c;
12add40e
NC
743 }
744 while (1);
745
746 *out = 0;
747 return buffer;
748}
749
75cd796a 750\f
d2fcac5c
NC
751/* Returns TRUE if the specified section should be dumped. */
752
015dc7e1 753static bool
d2fcac5c
NC
754process_section_p (asection * section)
755{
70ecb384 756 struct only * only;
d2fcac5c 757
70ecb384 758 if (only_list == NULL)
015dc7e1 759 return true;
d2fcac5c 760
70ecb384
NC
761 for (only = only_list; only; only = only->next)
762 if (strcmp (only->name, section->name) == 0)
763 {
015dc7e1
AM
764 only->seen = true;
765 return true;
70ecb384 766 }
d2fcac5c 767
015dc7e1 768 return false;
d2fcac5c 769}
70ecb384
NC
770
771/* Add an entry to the 'only' list. */
772
773static void
774add_only (char * name)
775{
776 struct only * only;
777
778 /* First check to make sure that we do not
779 already have an entry for this name. */
780 for (only = only_list; only; only = only->next)
781 if (strcmp (only->name, name) == 0)
782 return;
783
784 only = xmalloc (sizeof * only);
785 only->name = name;
015dc7e1 786 only->seen = false;
70ecb384
NC
787 only->next = only_list;
788 only_list = only;
789}
790
791/* Release the memory used by the 'only' list.
792 PR 11225: Issue a warning message for unseen sections.
793 Only do this if none of the sections were seen. This is mainly to support
794 tools like the GAS testsuite where an object file is dumped with a list of
795 generic section names known to be present in a range of different file
796 formats. */
797
798static void
799free_only_list (void)
800{
015dc7e1 801 bool at_least_one_seen = false;
70ecb384
NC
802 struct only * only;
803 struct only * next;
804
805 if (only_list == NULL)
806 return;
807
808 for (only = only_list; only; only = only->next)
809 if (only->seen)
810 {
015dc7e1 811 at_least_one_seen = true;
70ecb384
NC
812 break;
813 }
814
815 for (only = only_list; only; only = next)
816 {
817 if (! at_least_one_seen)
818 {
a8c62f1c
AM
819 non_fatal (_("section '%s' mentioned in a -j option, "
820 "but not found in any input file"),
70ecb384
NC
821 only->name);
822 exit_status = 1;
823 }
824 next = only->next;
825 free (only);
826 }
827}
828
d2fcac5c 829\f
75cd796a 830static void
1737c640 831dump_section_header (bfd *abfd, asection *section, void *data)
252b5132
RH
832{
833 char *comma = "";
61826503 834 unsigned int opb = bfd_octets_per_byte (abfd, section);
1737c640 835 int longest_section_name = *((int *) data);
252b5132 836
3bee8bcd
L
837 /* Ignore linker created section. See elfNN_ia64_object_p in
838 bfd/elfxx-ia64.c. */
839 if (section->flags & SEC_LINKER_CREATED)
840 return;
841
d2fcac5c
NC
842 /* PR 10413: Skip sections that we are ignoring. */
843 if (! process_section_p (section))
844 return;
845
1737c640 846 printf ("%3d %-*s %08lx ", section->index, longest_section_name,
fd361982
AM
847 sanitize_string (bfd_section_name (section)),
848 (unsigned long) bfd_section_size (section) / opb);
849 bfd_printf_vma (abfd, bfd_section_vma (section));
252b5132 850 printf (" ");
d8180c76 851 bfd_printf_vma (abfd, section->lma);
e59b4dfb 852 printf (" %08lx 2**%u", (unsigned long) section->filepos,
fd361982 853 bfd_section_alignment (section));
252b5132
RH
854 if (! wide_output)
855 printf ("\n ");
856 printf (" ");
857
858#define PF(x, y) \
859 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
860
861 PF (SEC_HAS_CONTENTS, "CONTENTS");
862 PF (SEC_ALLOC, "ALLOC");
863 PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
252b5132
RH
864 PF (SEC_LOAD, "LOAD");
865 PF (SEC_RELOC, "RELOC");
252b5132
RH
866 PF (SEC_READONLY, "READONLY");
867 PF (SEC_CODE, "CODE");
868 PF (SEC_DATA, "DATA");
869 PF (SEC_ROM, "ROM");
870 PF (SEC_DEBUGGING, "DEBUGGING");
871 PF (SEC_NEVER_LOAD, "NEVER_LOAD");
872 PF (SEC_EXCLUDE, "EXCLUDE");
873 PF (SEC_SORT_ENTRIES, "SORT_ENTRIES");
ebe372c1
L
874 if (bfd_get_arch (abfd) == bfd_arch_tic54x)
875 {
876 PF (SEC_TIC54X_BLOCK, "BLOCK");
877 PF (SEC_TIC54X_CLINK, "CLINK");
878 }
24c411ed 879 PF (SEC_SMALL_DATA, "SMALL_DATA");
ebe372c1 880 if (bfd_get_flavour (abfd) == bfd_target_coff_flavour)
91f68a68
MG
881 {
882 PF (SEC_COFF_SHARED, "SHARED");
883 PF (SEC_COFF_NOREAD, "NOREAD");
884 }
885 else if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
61826503
CE
886 {
887 PF (SEC_ELF_OCTETS, "OCTETS");
888 PF (SEC_ELF_PURECODE, "PURECODE");
889 }
13ae64f3 890 PF (SEC_THREAD_LOCAL, "THREAD_LOCAL");
64c1196b 891 PF (SEC_GROUP, "GROUP");
91f68a68
MG
892 if (bfd_get_arch (abfd) == bfd_arch_mep)
893 {
894 PF (SEC_MEP_VLIW, "VLIW");
895 }
252b5132
RH
896
897 if ((section->flags & SEC_LINK_ONCE) != 0)
898 {
899 const char *ls;
082b7297 900 struct coff_comdat_info *comdat;
252b5132
RH
901
902 switch (section->flags & SEC_LINK_DUPLICATES)
903 {
904 default:
905 abort ();
906 case SEC_LINK_DUPLICATES_DISCARD:
907 ls = "LINK_ONCE_DISCARD";
908 break;
909 case SEC_LINK_DUPLICATES_ONE_ONLY:
910 ls = "LINK_ONCE_ONE_ONLY";
911 break;
912 case SEC_LINK_DUPLICATES_SAME_SIZE:
913 ls = "LINK_ONCE_SAME_SIZE";
914 break;
915 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
916 ls = "LINK_ONCE_SAME_CONTENTS";
917 break;
918 }
919 printf ("%s%s", comma, ls);
deecf979 920
082b7297
L
921 comdat = bfd_coff_get_comdat_section (abfd, section);
922 if (comdat != NULL)
923 printf (" (COMDAT %s %ld)", comdat->name, comdat->symbol);
deecf979 924
252b5132
RH
925 comma = ", ";
926 }
927
928 printf ("\n");
929#undef PF
930}
931
1737c640
AB
932/* Called on each SECTION in ABFD, update the int variable pointed to by
933 DATA which contains the string length of the longest section name. */
934
935static void
fd361982
AM
936find_longest_section_name (bfd *abfd ATTRIBUTE_UNUSED,
937 asection *section, void *data)
1737c640
AB
938{
939 int *longest_so_far = (int *) data;
940 const char *name;
941 int len;
942
943 /* Ignore linker created section. */
944 if (section->flags & SEC_LINKER_CREATED)
945 return;
946
947 /* Skip sections that we are ignoring. */
948 if (! process_section_p (section))
949 return;
950
fd361982 951 name = bfd_section_name (section);
1737c640
AB
952 len = (int) strlen (name);
953 if (len > *longest_so_far)
954 *longest_so_far = len;
955}
956
252b5132 957static void
46dca2e0 958dump_headers (bfd *abfd)
252b5132 959{
1737c640
AB
960 /* The default width of 13 is just an arbitrary choice. */
961 int max_section_name_length = 13;
962 int bfd_vma_width;
8bea4d5c 963
252b5132 964#ifndef BFD64
1737c640 965 bfd_vma_width = 10;
252b5132 966#else
21611032
TS
967 /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */
968 if (bfd_get_arch_size (abfd) == 32)
1737c640 969 bfd_vma_width = 10;
21611032 970 else
1737c640 971 bfd_vma_width = 18;
252b5132 972#endif
8bea4d5c 973
1737c640
AB
974 printf (_("Sections:\n"));
975
976 if (wide_output)
977 bfd_map_over_sections (abfd, find_longest_section_name,
9b49454b 978 &max_section_name_length);
1737c640
AB
979
980 printf (_("Idx %-*s Size %-*s%-*sFile off Algn"),
981 max_section_name_length, "Name",
982 bfd_vma_width, "VMA",
983 bfd_vma_width, "LMA");
984
8bea4d5c
ILT
985 if (wide_output)
986 printf (_(" Flags"));
987 printf ("\n");
988
1737c640 989 bfd_map_over_sections (abfd, dump_section_header,
9b49454b 990 &max_section_name_length);
252b5132
RH
991}
992\f
993static asymbol **
46dca2e0 994slurp_symtab (bfd *abfd)
252b5132 995{
87d20657 996 symcount = 0;
252b5132 997 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
87d20657 998 return NULL;
252b5132 999
87d20657 1000 long storage = bfd_get_symtab_upper_bound (abfd);
252b5132 1001 if (storage < 0)
5a3f568b 1002 {
87d20657
AM
1003 non_fatal (_("failed to read symbol table from: %s"),
1004 bfd_get_filename (abfd));
ffdfc835 1005 my_bfd_nonfatal (_("error message was"));
5a3f568b 1006 }
781152ec 1007
ffdfc835 1008 if (storage <= 0)
87d20657 1009 return NULL;
28b18af1 1010
87d20657 1011 asymbol **sy = (asymbol **) xmalloc (storage);
252b5132
RH
1012 symcount = bfd_canonicalize_symtab (abfd, sy);
1013 if (symcount < 0)
ffdfc835
AM
1014 {
1015 my_bfd_nonfatal (bfd_get_filename (abfd));
1016 free (sy);
1017 sy = NULL;
c8b3d02c 1018 symcount = 0;
ffdfc835 1019 }
252b5132
RH
1020 return sy;
1021}
1022
1023/* Read in the dynamic symbols. */
1024
1025static asymbol **
46dca2e0 1026slurp_dynamic_symtab (bfd *abfd)
252b5132 1027{
87d20657
AM
1028 dynsymcount = 0;
1029 long storage = bfd_get_dynamic_symtab_upper_bound (abfd);
252b5132
RH
1030 if (storage < 0)
1031 {
1032 if (!(bfd_get_file_flags (abfd) & DYNAMIC))
1033 {
37cc8ec1 1034 non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd));
a8c62f1c 1035 exit_status = 1;
252b5132
RH
1036 return NULL;
1037 }
1038
ffdfc835 1039 my_bfd_nonfatal (bfd_get_filename (abfd));
252b5132 1040 }
c46b7066 1041
ffdfc835 1042 if (storage <= 0)
87d20657 1043 return NULL;
28b18af1 1044
87d20657 1045 asymbol **sy = (asymbol **) xmalloc (storage);
252b5132
RH
1046 dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
1047 if (dynsymcount < 0)
ffdfc835
AM
1048 {
1049 my_bfd_nonfatal (bfd_get_filename (abfd));
1050 free (sy);
1051 sy = NULL;
c8b3d02c 1052 dynsymcount = 0;
ffdfc835 1053 }
252b5132
RH
1054 return sy;
1055}
1056
a24bb4f0
NC
1057/* Some symbol names are significant and should be kept in the
1058 table of sorted symbol names, even if they are marked as
1059 debugging/section symbols. */
1060
015dc7e1 1061static bool
a24bb4f0
NC
1062is_significant_symbol_name (const char * name)
1063{
3f3328b8 1064 return startswith (name, ".plt") || startswith (name, ".got");
a24bb4f0
NC
1065}
1066
252b5132
RH
1067/* Filter out (in place) symbols that are useless for disassembly.
1068 COUNT is the number of elements in SYMBOLS.
0af11b59 1069 Return the number of useful symbols. */
252b5132
RH
1070
1071static long
46dca2e0 1072remove_useless_symbols (asymbol **symbols, long count)
252b5132 1073{
46dca2e0 1074 asymbol **in_ptr = symbols, **out_ptr = symbols;
252b5132
RH
1075
1076 while (--count >= 0)
1077 {
1078 asymbol *sym = *in_ptr++;
1079
1080 if (sym->name == NULL || sym->name[0] == '\0')
1081 continue;
a24bb4f0
NC
1082 if ((sym->flags & (BSF_DEBUGGING | BSF_SECTION_SYM))
1083 && ! is_significant_symbol_name (sym->name))
252b5132
RH
1084 continue;
1085 if (bfd_is_und_section (sym->section)
1086 || bfd_is_com_section (sym->section))
1087 continue;
1088
1089 *out_ptr++ = sym;
1090 }
1091 return out_ptr - symbols;
1092}
1093
660df28a
AM
1094static const asection *compare_section;
1095
252b5132
RH
1096/* Sort symbols into value order. */
1097
0af11b59 1098static int
46dca2e0 1099compare_symbols (const void *ap, const void *bp)
252b5132 1100{
46dca2e0
NC
1101 const asymbol *a = * (const asymbol **) ap;
1102 const asymbol *b = * (const asymbol **) bp;
1103 const char *an;
1104 const char *bn;
1105 size_t anl;
1106 size_t bnl;
015dc7e1 1107 bool as, af, bs, bf;
46dca2e0
NC
1108 flagword aflags;
1109 flagword bflags;
252b5132
RH
1110
1111 if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
1112 return 1;
1113 else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
1114 return -1;
1115
660df28a
AM
1116 /* Prefer symbols from the section currently being disassembled.
1117 Don't sort symbols from other sections by section, since there
1118 isn't much reason to prefer one section over another otherwise.
1119 See sym_ok comment for why we compare by section name. */
1120 as = strcmp (compare_section->name, a->section->name) == 0;
1121 bs = strcmp (compare_section->name, b->section->name) == 0;
1122 if (as && !bs)
252b5132 1123 return -1;
660df28a
AM
1124 if (!as && bs)
1125 return 1;
252b5132
RH
1126
1127 an = bfd_asymbol_name (a);
1128 bn = bfd_asymbol_name (b);
1129 anl = strlen (an);
1130 bnl = strlen (bn);
1131
1132 /* The symbols gnu_compiled and gcc2_compiled convey no real
1133 information, so put them after other symbols with the same value. */
252b5132
RH
1134 af = (strstr (an, "gnu_compiled") != NULL
1135 || strstr (an, "gcc2_compiled") != NULL);
1136 bf = (strstr (bn, "gnu_compiled") != NULL
1137 || strstr (bn, "gcc2_compiled") != NULL);
1138
1139 if (af && ! bf)
1140 return 1;
1141 if (! af && bf)
1142 return -1;
1143
1144 /* We use a heuristic for the file name, to try to sort it after
1145 more useful symbols. It may not work on non Unix systems, but it
1146 doesn't really matter; the only difference is precisely which
1147 symbol names get printed. */
1148
1149#define file_symbol(s, sn, snl) \
1150 (((s)->flags & BSF_FILE) != 0 \
660df28a
AM
1151 || ((snl) > 2 \
1152 && (sn)[(snl) - 2] == '.' \
252b5132
RH
1153 && ((sn)[(snl) - 1] == 'o' \
1154 || (sn)[(snl) - 1] == 'a')))
1155
1156 af = file_symbol (a, an, anl);
1157 bf = file_symbol (b, bn, bnl);
1158
1159 if (af && ! bf)
1160 return 1;
1161 if (! af && bf)
1162 return -1;
1163
660df28a
AM
1164 /* Sort function and object symbols before global symbols before
1165 local symbols before section symbols before debugging symbols. */
252b5132
RH
1166
1167 aflags = a->flags;
1168 bflags = b->flags;
1169
1170 if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING))
1171 {
1172 if ((aflags & BSF_DEBUGGING) != 0)
1173 return 1;
1174 else
1175 return -1;
1176 }
660df28a
AM
1177 if ((aflags & BSF_SECTION_SYM) != (bflags & BSF_SECTION_SYM))
1178 {
1179 if ((aflags & BSF_SECTION_SYM) != 0)
1180 return 1;
1181 else
1182 return -1;
1183 }
252b5132
RH
1184 if ((aflags & BSF_FUNCTION) != (bflags & BSF_FUNCTION))
1185 {
1186 if ((aflags & BSF_FUNCTION) != 0)
1187 return -1;
1188 else
1189 return 1;
1190 }
660df28a
AM
1191 if ((aflags & BSF_OBJECT) != (bflags & BSF_OBJECT))
1192 {
1193 if ((aflags & BSF_OBJECT) != 0)
1194 return -1;
1195 else
1196 return 1;
1197 }
252b5132
RH
1198 if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL))
1199 {
1200 if ((aflags & BSF_LOCAL) != 0)
1201 return 1;
1202 else
1203 return -1;
1204 }
1205 if ((aflags & BSF_GLOBAL) != (bflags & BSF_GLOBAL))
1206 {
1207 if ((aflags & BSF_GLOBAL) != 0)
1208 return -1;
1209 else
1210 return 1;
1211 }
1212
3d3af4ba
AM
1213 /* Sort larger size ELF symbols before smaller. See PR20337. */
1214 bfd_vma asz = 0;
1215 if ((a->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0
1216 && bfd_get_flavour (bfd_asymbol_bfd (a)) == bfd_target_elf_flavour)
1217 asz = ((elf_symbol_type *) a)->internal_elf_sym.st_size;
1218 bfd_vma bsz = 0;
1219 if ((b->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0
32a0481f 1220 && bfd_get_flavour (bfd_asymbol_bfd (b)) == bfd_target_elf_flavour)
3d3af4ba
AM
1221 bsz = ((elf_symbol_type *) b)->internal_elf_sym.st_size;
1222 if (asz != bsz)
1223 return asz > bsz ? -1 : 1;
32a0481f 1224
252b5132
RH
1225 /* Symbols that start with '.' might be section names, so sort them
1226 after symbols that don't start with '.'. */
1227 if (an[0] == '.' && bn[0] != '.')
1228 return 1;
1229 if (an[0] != '.' && bn[0] == '.')
1230 return -1;
1231
1232 /* Finally, if we can't distinguish them in any other way, try to
1233 get consistent results by sorting the symbols by name. */
1234 return strcmp (an, bn);
1235}
1236
1237/* Sort relocs into address order. */
1238
1239static int
46dca2e0 1240compare_relocs (const void *ap, const void *bp)
252b5132 1241{
46dca2e0
NC
1242 const arelent *a = * (const arelent **) ap;
1243 const arelent *b = * (const arelent **) bp;
252b5132
RH
1244
1245 if (a->address > b->address)
1246 return 1;
1247 else if (a->address < b->address)
1248 return -1;
1249
1250 /* So that associated relocations tied to the same address show up
1251 in the correct order, we don't do any further sorting. */
1252 if (a > b)
1253 return 1;
1254 else if (a < b)
1255 return -1;
1256 else
1257 return 0;
1258}
1259
155e0d23
NC
1260/* Print an address (VMA) to the output stream in INFO.
1261 If SKIP_ZEROES is TRUE, omit leading zeroes. */
252b5132
RH
1262
1263static void
91d6fa6a 1264objdump_print_value (bfd_vma vma, struct disassemble_info *inf,
015dc7e1 1265 bool skip_zeroes)
252b5132
RH
1266{
1267 char buf[30];
1268 char *p;
3b9ad1cc 1269 struct objdump_disasm_info *aux;
252b5132 1270
91d6fa6a 1271 aux = (struct objdump_disasm_info *) inf->application_data;
d8180c76 1272 bfd_sprintf_vma (aux->abfd, buf, vma);
252b5132
RH
1273 if (! skip_zeroes)
1274 p = buf;
1275 else
1276 {
1277 for (p = buf; *p == '0'; ++p)
1278 ;
1279 if (*p == '\0')
1280 --p;
1281 }
60a3da00 1282 (*inf->fprintf_styled_func) (inf->stream, dis_style_address, "%s", p);
252b5132
RH
1283}
1284
1285/* Print the name of a symbol. */
1286
1287static void
91d6fa6a 1288objdump_print_symname (bfd *abfd, struct disassemble_info *inf,
46dca2e0 1289 asymbol *sym)
252b5132
RH
1290{
1291 char *alloc;
bb4d2ac2 1292 const char *name, *version_string = NULL;
015dc7e1 1293 bool hidden = false;
252b5132
RH
1294
1295 alloc = NULL;
1296 name = bfd_asymbol_name (sym);
a6637ec0 1297 if (do_demangle && name[0] != '\0')
252b5132
RH
1298 {
1299 /* Demangle the name. */
af03af8f 1300 alloc = bfd_demangle (abfd, name, demangle_flags);
ed180cc5
AM
1301 if (alloc != NULL)
1302 name = alloc;
252b5132
RH
1303 }
1304
160b1a61 1305 if ((sym->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0)
015dc7e1 1306 version_string = bfd_get_symbol_version_string (abfd, sym, true,
1081065c 1307 &hidden);
bb4d2ac2 1308
e6f7f6d1 1309 if (bfd_is_und_section (bfd_asymbol_section (sym)))
015dc7e1 1310 hidden = true;
bb4d2ac2 1311
12add40e
NC
1312 name = sanitize_string (name);
1313
91d6fa6a 1314 if (inf != NULL)
bb4d2ac2 1315 {
60a3da00 1316 (*inf->fprintf_styled_func) (inf->stream, dis_style_symbol, "%s", name);
bb4d2ac2 1317 if (version_string && *version_string != '\0')
60a3da00
AB
1318 (*inf->fprintf_styled_func) (inf->stream, dis_style_symbol,
1319 hidden ? "@%s" : "@@%s",
1320 version_string);
bb4d2ac2 1321 }
252b5132 1322 else
bb4d2ac2
L
1323 {
1324 printf ("%s", name);
1325 if (version_string && *version_string != '\0')
1326 printf (hidden ? "@%s" : "@@%s", version_string);
1327 }
252b5132
RH
1328
1329 if (alloc != NULL)
1330 free (alloc);
1331}
1332
015dc7e1
AM
1333static inline bool
1334sym_ok (bool want_section,
1335 bfd *abfd ATTRIBUTE_UNUSED,
1336 long place,
1337 asection *sec,
1338 struct disassemble_info *inf)
39f0547e
NC
1339{
1340 if (want_section)
1341 {
a8c4d40b 1342 /* NB: An object file can have different sections with the same
9b49454b 1343 section name. Compare compare section pointers if they have
a8c4d40b
L
1344 the same owner. */
1345 if (sorted_syms[place]->section->owner == sec->owner
1346 && sorted_syms[place]->section != sec)
015dc7e1 1347 return false;
a8c4d40b 1348
39f0547e
NC
1349 /* Note - we cannot just compare section pointers because they could
1350 be different, but the same... Ie the symbol that we are trying to
1351 find could have come from a separate debug info file. Under such
1352 circumstances the symbol will be associated with a section in the
1353 debug info file, whilst the section we want is in a normal file.
1354 So the section pointers will be different, but the section names
1355 will be the same. */
fd361982
AM
1356 if (strcmp (bfd_section_name (sorted_syms[place]->section),
1357 bfd_section_name (sec)) != 0)
015dc7e1 1358 return false;
39f0547e
NC
1359 }
1360
1361 return inf->symbol_is_valid (sorted_syms[place], inf);
1362}
1363
22a398e1
NC
1364/* Locate a symbol given a bfd and a section (from INFO->application_data),
1365 and a VMA. If INFO->application_data->require_sec is TRUE, then always
1366 require the symbol to be in the section. Returns NULL if there is no
1367 suitable symbol. If PLACE is not NULL, then *PLACE is set to the index
1368 of the symbol in sorted_syms. */
252b5132
RH
1369
1370static asymbol *
3b9ad1cc 1371find_symbol_for_address (bfd_vma vma,
91d6fa6a 1372 struct disassemble_info *inf,
3b9ad1cc 1373 long *place)
252b5132
RH
1374{
1375 /* @@ Would it speed things up to cache the last two symbols returned,
1376 and maybe their address ranges? For many processors, only one memory
1377 operand can be present at a time, so the 2-entry cache wouldn't be
1378 constantly churned by code doing heavy memory accesses. */
1379
1380 /* Indices in `sorted_syms'. */
1381 long min = 0;
91d6fa6a 1382 long max_count = sorted_symcount;
252b5132 1383 long thisplace;
3b9ad1cc
AM
1384 struct objdump_disasm_info *aux;
1385 bfd *abfd;
1386 asection *sec;
1387 unsigned int opb;
015dc7e1 1388 bool want_section;
0e70b27b 1389 long rel_count;
252b5132
RH
1390
1391 if (sorted_symcount < 1)
1392 return NULL;
1393
91d6fa6a 1394 aux = (struct objdump_disasm_info *) inf->application_data;
3b9ad1cc 1395 abfd = aux->abfd;
f59f8978 1396 sec = inf->section;
91d6fa6a 1397 opb = inf->octets_per_byte;
3b9ad1cc 1398
252b5132 1399 /* Perform a binary search looking for the closest symbol to the
91d6fa6a
NC
1400 required value. We are searching the range (min, max_count]. */
1401 while (min + 1 < max_count)
252b5132
RH
1402 {
1403 asymbol *sym;
1404
91d6fa6a 1405 thisplace = (max_count + min) / 2;
252b5132
RH
1406 sym = sorted_syms[thisplace];
1407
1408 if (bfd_asymbol_value (sym) > vma)
91d6fa6a 1409 max_count = thisplace;
252b5132
RH
1410 else if (bfd_asymbol_value (sym) < vma)
1411 min = thisplace;
1412 else
1413 {
1414 min = thisplace;
1415 break;
1416 }
1417 }
1418
1419 /* The symbol we want is now in min, the low end of the range we
1420 were searching. If there are several symbols with the same
660df28a 1421 value, we want the first one. */
252b5132
RH
1422 thisplace = min;
1423 while (thisplace > 0
1424 && (bfd_asymbol_value (sorted_syms[thisplace])
660df28a 1425 == bfd_asymbol_value (sorted_syms[thisplace - 1])))
252b5132
RH
1426 --thisplace;
1427
2b4590fb
AM
1428 /* Prefer a symbol in the current section if we have multple symbols
1429 with the same value, as can occur with overlays or zero size
1430 sections. */
1431 min = thisplace;
91d6fa6a 1432 while (min < max_count
2b4590fb
AM
1433 && (bfd_asymbol_value (sorted_syms[min])
1434 == bfd_asymbol_value (sorted_syms[thisplace])))
1435 {
015dc7e1 1436 if (sym_ok (true, abfd, min, sec, inf))
2b4590fb
AM
1437 {
1438 thisplace = min;
1439
1440 if (place != NULL)
1441 *place = thisplace;
1442
1443 return sorted_syms[thisplace];
1444 }
1445 ++min;
1446 }
1447
1049f94e 1448 /* If the file is relocatable, and the symbol could be from this
252b5132
RH
1449 section, prefer a symbol from this section over symbols from
1450 others, even if the other symbol's value might be closer.
0af11b59 1451
252b5132
RH
1452 Note that this may be wrong for some symbol references if the
1453 sections have overlapping memory ranges, but in that case there's
1454 no way to tell what's desired without looking at the relocation
e39ff52a 1455 table.
3aade688 1456
e39ff52a
PB
1457 Also give the target a chance to reject symbols. */
1458 want_section = (aux->require_sec
1459 || ((abfd->flags & HAS_RELOC) != 0
fd361982
AM
1460 && vma >= bfd_section_vma (sec)
1461 && vma < (bfd_section_vma (sec)
1462 + bfd_section_size (sec) / opb)));
9b49454b 1463
39f0547e 1464 if (! sym_ok (want_section, abfd, thisplace, sec, inf))
252b5132
RH
1465 {
1466 long i;
2b4590fb 1467 long newplace = sorted_symcount;
98a91d6a 1468
2b4590fb 1469 for (i = min - 1; i >= 0; i--)
252b5132 1470 {
39f0547e 1471 if (sym_ok (want_section, abfd, i, sec, inf))
252b5132 1472 {
e39ff52a
PB
1473 if (newplace == sorted_symcount)
1474 newplace = i;
1475
1476 if (bfd_asymbol_value (sorted_syms[i])
1477 != bfd_asymbol_value (sorted_syms[newplace]))
1478 break;
1479
1480 /* Remember this symbol and keep searching until we reach
1481 an earlier address. */
1482 newplace = i;
252b5132
RH
1483 }
1484 }
1485
e39ff52a
PB
1486 if (newplace != sorted_symcount)
1487 thisplace = newplace;
1488 else
252b5132
RH
1489 {
1490 /* We didn't find a good symbol with a smaller value.
1491 Look for one with a larger value. */
1492 for (i = thisplace + 1; i < sorted_symcount; i++)
1493 {
39f0547e 1494 if (sym_ok (want_section, abfd, i, sec, inf))
252b5132
RH
1495 {
1496 thisplace = i;
1497 break;
1498 }
1499 }
1500 }
1501
39f0547e 1502 if (! sym_ok (want_section, abfd, thisplace, sec, inf))
22a398e1
NC
1503 /* There is no suitable symbol. */
1504 return NULL;
1505 }
1506
a24bb4f0
NC
1507 /* If we have not found an exact match for the specified address
1508 and we have dynamic relocations available, then we can produce
1509 a better result by matching a relocation to the address and
1510 using the symbol associated with that relocation. */
c3f72de4 1511 rel_count = inf->dynrelcount;
a24bb4f0 1512 if (!want_section
a24bb4f0 1513 && sorted_syms[thisplace]->value != vma
0e70b27b 1514 && rel_count > 0
c3f72de4
AM
1515 && inf->dynrelbuf != NULL
1516 && inf->dynrelbuf[0]->address <= vma
1517 && inf->dynrelbuf[rel_count - 1]->address >= vma
a24bb4f0
NC
1518 /* If we have matched a synthetic symbol, then stick with that. */
1519 && (sorted_syms[thisplace]->flags & BSF_SYNTHETIC) == 0)
1520 {
0e70b27b
L
1521 arelent ** rel_low;
1522 arelent ** rel_high;
a24bb4f0 1523
c3f72de4 1524 rel_low = inf->dynrelbuf;
0e70b27b
L
1525 rel_high = rel_low + rel_count - 1;
1526 while (rel_low <= rel_high)
a24bb4f0 1527 {
0e70b27b
L
1528 arelent **rel_mid = &rel_low[(rel_high - rel_low) / 2];
1529 arelent * rel = *rel_mid;
a24bb4f0 1530
0e70b27b 1531 if (rel->address == vma)
a24bb4f0 1532 {
0e70b27b 1533 /* Absolute relocations do not provide a more helpful
9b49454b 1534 symbolic address. Find a non-absolute relocation
0e70b27b
L
1535 with the same address. */
1536 arelent **rel_vma = rel_mid;
1537 for (rel_mid--;
1538 rel_mid >= rel_low && rel_mid[0]->address == vma;
1539 rel_mid--)
1540 rel_vma = rel_mid;
1541
1542 for (; rel_vma <= rel_high && rel_vma[0]->address == vma;
1543 rel_vma++)
1544 {
1545 rel = *rel_vma;
1546 if (rel->sym_ptr_ptr != NULL
1547 && ! bfd_is_abs_section ((* rel->sym_ptr_ptr)->section))
1548 {
1549 if (place != NULL)
1550 * place = thisplace;
1551 return * rel->sym_ptr_ptr;
1552 }
1553 }
1554 break;
a24bb4f0
NC
1555 }
1556
0e70b27b
L
1557 if (vma < rel->address)
1558 rel_high = rel_mid;
1559 else if (vma >= rel_mid[1]->address)
1560 rel_low = rel_mid + 1;
1561 else
a24bb4f0
NC
1562 break;
1563 }
1564 }
1565
252b5132
RH
1566 if (place != NULL)
1567 *place = thisplace;
1568
1569 return sorted_syms[thisplace];
1570}
1571
155e0d23 1572/* Print an address and the offset to the nearest symbol. */
252b5132
RH
1573
1574static void
46dca2e0 1575objdump_print_addr_with_sym (bfd *abfd, asection *sec, asymbol *sym,
91d6fa6a 1576 bfd_vma vma, struct disassemble_info *inf,
015dc7e1 1577 bool skip_zeroes)
252b5132 1578{
b1bc1394
AM
1579 if (!no_addresses)
1580 {
1581 objdump_print_value (vma, inf, skip_zeroes);
60a3da00 1582 (*inf->fprintf_styled_func) (inf->stream, dis_style_text, " ");
b1bc1394 1583 }
252b5132
RH
1584
1585 if (sym == NULL)
1586 {
1587 bfd_vma secaddr;
1588
60a3da00
AB
1589 (*inf->fprintf_styled_func) (inf->stream, dis_style_text,"<");
1590 (*inf->fprintf_styled_func) (inf->stream, dis_style_symbol, "%s",
1591 sanitize_string (bfd_section_name (sec)));
fd361982 1592 secaddr = bfd_section_vma (sec);
252b5132
RH
1593 if (vma < secaddr)
1594 {
60a3da00
AB
1595 (*inf->fprintf_styled_func) (inf->stream, dis_style_immediate,
1596 "-0x");
015dc7e1 1597 objdump_print_value (secaddr - vma, inf, true);
252b5132
RH
1598 }
1599 else if (vma > secaddr)
1600 {
60a3da00 1601 (*inf->fprintf_styled_func) (inf->stream, dis_style_immediate, "+0x");
015dc7e1 1602 objdump_print_value (vma - secaddr, inf, true);
252b5132 1603 }
60a3da00 1604 (*inf->fprintf_styled_func) (inf->stream, dis_style_text, ">");
252b5132
RH
1605 }
1606 else
1607 {
60a3da00 1608 (*inf->fprintf_styled_func) (inf->stream, dis_style_text, "<");
a24bb4f0 1609
91d6fa6a 1610 objdump_print_symname (abfd, inf, sym);
a24bb4f0
NC
1611
1612 if (bfd_asymbol_value (sym) == vma)
1613 ;
1614 /* Undefined symbols in an executables and dynamic objects do not have
1615 a value associated with them, so it does not make sense to display
1616 an offset relative to them. Normally we would not be provided with
1617 this kind of symbol, but the target backend might choose to do so,
1618 and the code in find_symbol_for_address might return an as yet
1619 unresolved symbol associated with a dynamic reloc. */
1620 else if ((bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC))
1621 && bfd_is_und_section (sym->section))
1622 ;
1623 else if (bfd_asymbol_value (sym) > vma)
252b5132 1624 {
60a3da00 1625 (*inf->fprintf_styled_func) (inf->stream, dis_style_immediate,"-0x");
015dc7e1 1626 objdump_print_value (bfd_asymbol_value (sym) - vma, inf, true);
252b5132
RH
1627 }
1628 else if (vma > bfd_asymbol_value (sym))
1629 {
60a3da00 1630 (*inf->fprintf_styled_func) (inf->stream, dis_style_immediate, "+0x");
015dc7e1 1631 objdump_print_value (vma - bfd_asymbol_value (sym), inf, true);
252b5132 1632 }
a24bb4f0 1633
60a3da00 1634 (*inf->fprintf_styled_func) (inf->stream, dis_style_text, ">");
252b5132 1635 }
98ec6e72
NC
1636
1637 if (display_file_offsets)
60a3da00
AB
1638 inf->fprintf_styled_func (inf->stream, dis_style_text,
1639 _(" (File Offset: 0x%lx)"),
1640 (long int)(sec->filepos + (vma - sec->vma)));
252b5132
RH
1641}
1642
155e0d23
NC
1643/* Print an address (VMA), symbolically if possible.
1644 If SKIP_ZEROES is TRUE, don't output leading zeroes. */
252b5132
RH
1645
1646static void
3b9ad1cc 1647objdump_print_addr (bfd_vma vma,
91d6fa6a 1648 struct disassemble_info *inf,
015dc7e1 1649 bool skip_zeroes)
252b5132 1650{
3b9ad1cc 1651 struct objdump_disasm_info *aux;
d253b654 1652 asymbol *sym = NULL;
015dc7e1 1653 bool skip_find = false;
252b5132 1654
91d6fa6a 1655 aux = (struct objdump_disasm_info *) inf->application_data;
32760852 1656
252b5132
RH
1657 if (sorted_symcount < 1)
1658 {
b1bc1394
AM
1659 if (!no_addresses)
1660 {
4bb461e4 1661 (*inf->fprintf_styled_func) (inf->stream, dis_style_address, "0x");
b1bc1394
AM
1662 objdump_print_value (vma, inf, skip_zeroes);
1663 }
32760852
NC
1664
1665 if (display_file_offsets)
4bb461e4
AB
1666 inf->fprintf_styled_func (inf->stream, dis_style_text,
1667 _(" (File Offset: 0x%lx)"),
1668 (long int) (inf->section->filepos
1669 + (vma - inf->section->vma)));
252b5132
RH
1670 return;
1671 }
1672
ce04548a
NC
1673 if (aux->reloc != NULL
1674 && aux->reloc->sym_ptr_ptr != NULL
1675 && * aux->reloc->sym_ptr_ptr != NULL)
1676 {
1677 sym = * aux->reloc->sym_ptr_ptr;
1678
1679 /* Adjust the vma to the reloc. */
1680 vma += bfd_asymbol_value (sym);
1681
e6f7f6d1 1682 if (bfd_is_und_section (bfd_asymbol_section (sym)))
015dc7e1 1683 skip_find = true;
ce04548a
NC
1684 }
1685
1686 if (!skip_find)
91d6fa6a 1687 sym = find_symbol_for_address (vma, inf, NULL);
ce04548a 1688
f59f8978 1689 objdump_print_addr_with_sym (aux->abfd, inf->section, sym, vma, inf,
252b5132
RH
1690 skip_zeroes);
1691}
1692
1693/* Print VMA to INFO. This function is passed to the disassembler
1694 routine. */
1695
1696static void
91d6fa6a 1697objdump_print_address (bfd_vma vma, struct disassemble_info *inf)
252b5132 1698{
91d6fa6a 1699 objdump_print_addr (vma, inf, ! prefix_addresses);
252b5132
RH
1700}
1701
2ae86dfc 1702/* Determine if the given address has a symbol associated with it. */
252b5132 1703
a2e66773 1704static asymbol *
91d6fa6a 1705objdump_symbol_at_address (bfd_vma vma, struct disassemble_info * inf)
252b5132 1706{
252b5132
RH
1707 asymbol * sym;
1708
91d6fa6a 1709 sym = find_symbol_for_address (vma, inf, NULL);
a2e66773
AM
1710 if (sym != NULL && bfd_asymbol_value (sym) == vma)
1711 return sym;
252b5132 1712
a2e66773 1713 return NULL;
252b5132
RH
1714}
1715
1716/* Hold the last function name and the last line number we displayed
1717 in a disassembly. */
1718
1719static char *prev_functionname;
1720static unsigned int prev_line;
9b8d1a36 1721static unsigned int prev_discriminator;
252b5132
RH
1722
1723/* We keep a list of all files that we have seen when doing a
50c2245b 1724 disassembly with source, so that we know how much of the file to
252b5132
RH
1725 display. This can be important for inlined functions. */
1726
1727struct print_file_list
1728{
1729 struct print_file_list *next;
43ac9881
AM
1730 const char *filename;
1731 const char *modname;
3aade688 1732 const char *map;
e8f5eee4 1733 size_t mapsize;
3aade688 1734 const char **linemap;
e8f5eee4
NC
1735 unsigned maxline;
1736 unsigned last_line;
43339b1d 1737 unsigned max_printed;
e8f5eee4 1738 int first;
252b5132
RH
1739};
1740
1741static struct print_file_list *print_files;
1742
1743/* The number of preceding context lines to show when we start
1744 displaying a file for the first time. */
1745
1746#define SHOW_PRECEDING_CONTEXT_LINES (5)
1747
d647c797
AM
1748#if HAVE_LIBDEBUGINFOD
1749/* Return a hex string represention of the build-id. */
1750
1751unsigned char *
1752get_build_id (void * data)
1753{
1754 unsigned i;
1755 char * build_id_str;
1756 bfd * abfd = (bfd *) data;
1757 const struct bfd_build_id * build_id;
1758
1759 build_id = abfd->build_id;
1760 if (build_id == NULL)
1761 return NULL;
1762
1763 build_id_str = malloc (build_id->size * 2 + 1);
1764 if (build_id_str == NULL)
1765 return NULL;
1766
1767 for (i = 0; i < build_id->size; i++)
1768 sprintf (build_id_str + (i * 2), "%02x", build_id->data[i]);
1769 build_id_str[build_id->size * 2] = '\0';
1770
1771 return (unsigned char *) build_id_str;
1772}
1773
1774/* Search for a separate debug file matching ABFD's build-id. */
1775
1776static bfd *
1777find_separate_debug (const bfd * abfd)
1778{
1779 const struct bfd_build_id * build_id = abfd->build_id;
1780 separate_info * i = first_separate_info;
1781
1782 if (build_id == NULL || i == NULL)
1783 return NULL;
1784
1785 while (i != NULL)
1786 {
1787 const bfd * i_bfd = (bfd *) i->handle;
1788
1789 if (abfd != NULL && i_bfd->build_id != NULL)
1790 {
1791 const unsigned char * data = i_bfd->build_id->data;
1792 size_t size = i_bfd->build_id->size;
1793
1794 if (size == build_id->size
1795 && memcmp (data, build_id->data, size) == 0)
1796 return (bfd *) i->handle;
1797 }
1798
1799 i = i->next;
1800 }
1801
1802 return NULL;
1803}
1804
1805/* Search for a separate debug file matching ABFD's .gnu_debugaltlink
1806 build-id. */
1807
1808static bfd *
1809find_alt_debug (const bfd * abfd)
1810{
1811 size_t namelen;
1812 size_t id_len;
1813 const char * name;
1814 struct dwarf_section * section;
1815 const struct bfd_build_id * build_id = abfd->build_id;
1816 separate_info * i = first_separate_info;
1817
1818 if (i == NULL
1819 || build_id == NULL
1820 || !load_debug_section (gnu_debugaltlink, (void *) abfd))
1821 return NULL;
1822
1823 section = &debug_displays[gnu_debugaltlink].section;
1824 if (section == NULL)
1825 return NULL;
1826
1827 name = (const char *) section->start;
1828 namelen = strnlen (name, section->size) + 1;
1829 if (namelen == 1)
1830 return NULL;
1831 if (namelen >= section->size)
1832 return NULL;
1833
1834 id_len = section->size - namelen;
1835 if (id_len < 0x14)
1836 return NULL;
1837
1838 /* Compare the .gnu_debugaltlink build-id with the build-ids of the
1839 known separate_info files. */
1840 while (i != NULL)
1841 {
1842 const bfd * i_bfd = (bfd *) i->handle;
1843
1844 if (i_bfd != NULL && i_bfd->build_id != NULL)
1845 {
1846 const unsigned char * data = i_bfd->build_id->data;
1847 size_t size = i_bfd->build_id->size;
1848
1849 if (id_len == size
1850 && memcmp (section->start + namelen, data, size) == 0)
1851 return (bfd *) i->handle;
1852 }
1853
1854 i = i->next;
1855 }
1856
1857 return NULL;
1858}
1859
1860#endif /* HAVE_LIBDEBUGINFOD */
1861
1862/* Reads the contents of file FN into memory. Returns a pointer to the buffer.
1863 Also returns the size of the buffer in SIZE_RETURN and a filled out
1864 stat structure in FST_RETURN. Returns NULL upon failure. */
e8f5eee4
NC
1865
1866static const char *
d647c797
AM
1867slurp_file (const char * fn,
1868 size_t * size_return,
1869 struct stat * fst_return,
1870 bfd * abfd ATTRIBUTE_UNUSED)
e8f5eee4
NC
1871{
1872#ifdef HAVE_MMAP
d647c797 1873 int ps;
e8f5eee4
NC
1874 size_t msize;
1875#endif
1876 const char *map;
d647c797
AM
1877 int fd;
1878
1879 /* Paranoia. */
1880 if (fn == NULL || * fn == 0 || size_return == NULL || fst_return == NULL)
1881 return NULL;
1882
1883 fd = open (fn, O_RDONLY | O_BINARY);
1884
1885#if HAVE_LIBDEBUGINFOD
1886 if (fd < 0 && use_debuginfod && fn[0] == '/' && abfd != NULL)
1887 {
6175be41 1888 unsigned char *build_id = get_build_id (abfd);
d647c797 1889
6175be41
AM
1890 if (build_id)
1891 {
1892 debuginfod_client *client = debuginfod_begin ();
d647c797 1893
6175be41
AM
1894 if (client)
1895 {
1896 fd = debuginfod_find_source (client, build_id, 0, fn, NULL);
1897 debuginfod_end (client);
1898 }
1899 free (build_id);
1900 }
d647c797
AM
1901 }
1902#endif
e8f5eee4
NC
1903
1904 if (fd < 0)
1905 return NULL;
d647c797
AM
1906
1907 if (fstat (fd, fst_return) < 0)
6c713012
AM
1908 {
1909 close (fd);
1910 return NULL;
1911 }
d647c797
AM
1912
1913 *size_return = fst_return->st_size;
1914
e8f5eee4 1915#ifdef HAVE_MMAP
d647c797
AM
1916 ps = getpagesize ();
1917 msize = (*size_return + ps - 1) & ~(ps - 1);
e8f5eee4 1918 map = mmap (NULL, msize, PROT_READ, MAP_SHARED, fd, 0);
6c713012 1919 if (map != (char *) -1L)
e8f5eee4 1920 {
6c713012
AM
1921 close (fd);
1922 return map;
e8f5eee4
NC
1923 }
1924#endif
d647c797
AM
1925
1926 map = (const char *) malloc (*size_return);
1927 if (!map || (size_t) read (fd, (char *) map, *size_return) != *size_return)
6c713012
AM
1928 {
1929 free ((void *) map);
e8f5eee4
NC
1930 map = NULL;
1931 }
1932 close (fd);
6c713012 1933 return map;
e8f5eee4
NC
1934}
1935
1936#define line_map_decrease 5
1937
1938/* Precompute array of lines for a mapped file. */
1939
3aade688
L
1940static const char **
1941index_file (const char *map, size_t size, unsigned int *maxline)
e8f5eee4
NC
1942{
1943 const char *p, *lstart, *end;
1944 int chars_per_line = 45; /* First iteration will use 40. */
1945 unsigned int lineno;
3aade688 1946 const char **linemap = NULL;
e8f5eee4 1947 unsigned long line_map_size = 0;
3aade688 1948
e8f5eee4
NC
1949 lineno = 0;
1950 lstart = map;
1951 end = map + size;
1952
3aade688
L
1953 for (p = map; p < end; p++)
1954 {
1955 if (*p == '\n')
1956 {
1957 if (p + 1 < end && p[1] == '\r')
1958 p++;
1959 }
1960 else if (*p == '\r')
1961 {
e8f5eee4
NC
1962 if (p + 1 < end && p[1] == '\n')
1963 p++;
1964 }
1965 else
1966 continue;
3aade688 1967
e8f5eee4
NC
1968 /* End of line found. */
1969
3aade688
L
1970 if (linemap == NULL || line_map_size < lineno + 1)
1971 {
e8f5eee4
NC
1972 unsigned long newsize;
1973
1974 chars_per_line -= line_map_decrease;
1975 if (chars_per_line <= 1)
1976 chars_per_line = 1;
1977 line_map_size = size / chars_per_line + 1;
1978 if (line_map_size < lineno + 1)
1979 line_map_size = lineno + 1;
1980 newsize = line_map_size * sizeof (char *);
3f5e193b 1981 linemap = (const char **) xrealloc (linemap, newsize);
e8f5eee4
NC
1982 }
1983
3aade688
L
1984 linemap[lineno++] = lstart;
1985 lstart = p + 1;
e8f5eee4 1986 }
3aade688
L
1987
1988 *maxline = lineno;
e8f5eee4
NC
1989 return linemap;
1990}
1991
43ac9881 1992/* Tries to open MODNAME, and if successful adds a node to print_files
d647c797
AM
1993 linked list and returns that node. Also fills in the stat structure
1994 pointed to by FST_RETURN. Returns NULL on failure. */
43ac9881
AM
1995
1996static struct print_file_list *
d647c797
AM
1997try_print_file_open (const char * origname,
1998 const char * modname,
1999 struct stat * fst_return,
2000 bfd * abfd)
43ac9881
AM
2001{
2002 struct print_file_list *p;
43ac9881 2003
3f5e193b 2004 p = (struct print_file_list *) xmalloc (sizeof (struct print_file_list));
43ac9881 2005
d647c797 2006 p->map = slurp_file (modname, &p->mapsize, fst_return, abfd);
e8f5eee4 2007 if (p->map == NULL)
43ac9881 2008 {
e8f5eee4
NC
2009 free (p);
2010 return NULL;
43ac9881 2011 }
3aade688 2012
e8f5eee4
NC
2013 p->linemap = index_file (p->map, p->mapsize, &p->maxline);
2014 p->last_line = 0;
43339b1d 2015 p->max_printed = 0;
43ac9881
AM
2016 p->filename = origname;
2017 p->modname = modname;
43ac9881 2018 p->next = print_files;
e8f5eee4 2019 p->first = 1;
43ac9881
AM
2020 print_files = p;
2021 return p;
2022}
2023
a8685210 2024/* If the source file, as described in the symtab, is not found
43ac9881
AM
2025 try to locate it in one of the paths specified with -I
2026 If found, add location to print_files linked list. */
2027
2028static struct print_file_list *
5ef2d51b 2029update_source_path (const char *filename, bfd *abfd)
43ac9881
AM
2030{
2031 struct print_file_list *p;
2032 const char *fname;
5ef2d51b 2033 struct stat fst;
43ac9881
AM
2034 int i;
2035
d647c797 2036 p = try_print_file_open (filename, filename, &fst, abfd);
5ef2d51b
AM
2037 if (p == NULL)
2038 {
2039 if (include_path_count == 0)
2040 return NULL;
43ac9881 2041
5ef2d51b
AM
2042 /* Get the name of the file. */
2043 fname = lbasename (filename);
43ac9881 2044
5ef2d51b
AM
2045 /* If file exists under a new path, we need to add it to the list
2046 so that show_line knows about it. */
2047 for (i = 0; i < include_path_count; i++)
2048 {
2049 char *modname = concat (include_paths[i], "/", fname,
2050 (const char *) 0);
43ac9881 2051
d647c797 2052 p = try_print_file_open (filename, modname, &fst, abfd);
5ef2d51b
AM
2053 if (p)
2054 break;
43ac9881 2055
5ef2d51b
AM
2056 free (modname);
2057 }
2058 }
2059
2060 if (p != NULL)
2061 {
2062 long mtime = bfd_get_mtime (abfd);
43ac9881 2063
5ef2d51b
AM
2064 if (fst.st_mtime > mtime)
2065 warn (_("source file %s is more recent than object file\n"),
2066 filename);
43ac9881
AM
2067 }
2068
5ef2d51b 2069 return p;
43ac9881
AM
2070}
2071
e8f5eee4 2072/* Print a source file line. */
252b5132 2073
3aade688 2074static void
91d6fa6a 2075print_line (struct print_file_list *p, unsigned int linenum)
252b5132 2076{
e8f5eee4 2077 const char *l;
615f3149 2078 size_t len;
3aade688 2079
91d6fa6a 2080 if (linenum >= p->maxline)
e8f5eee4 2081 return;
91d6fa6a 2082 l = p->linemap [linenum];
a1c110a3
NC
2083 if (source_comment != NULL && strlen (l) > 0)
2084 printf ("%s", source_comment);
615f3149 2085 len = strcspn (l, "\n\r");
a1c110a3 2086 /* Test fwrite return value to quiet glibc warning. */
615f3149
AM
2087 if (len == 0 || fwrite (l, len, 1, stdout) == 1)
2088 putchar ('\n');
2089}
252b5132 2090
e8f5eee4 2091/* Print a range of source code lines. */
252b5132 2092
e8f5eee4
NC
2093static void
2094dump_lines (struct print_file_list *p, unsigned int start, unsigned int end)
2095{
2096 if (p->map == NULL)
2097 return;
306253b2
AM
2098 if (start != 0)
2099 --start;
2100 while (start < end)
e8f5eee4
NC
2101 {
2102 print_line (p, start);
2103 start++;
252b5132 2104 }
0af11b59 2105}
252b5132 2106
50c2245b 2107/* Show the line number, or the source line, in a disassembly
252b5132
RH
2108 listing. */
2109
2110static void
46dca2e0 2111show_line (bfd *abfd, asection *section, bfd_vma addr_offset)
252b5132 2112{
b1f88ebe
AM
2113 const char *filename;
2114 const char *functionname;
91d6fa6a 2115 unsigned int linenumber;
9b8d1a36 2116 unsigned int discriminator;
015dc7e1 2117 bool reloc;
e1fa0163 2118 char *path = NULL;
252b5132
RH
2119
2120 if (! with_line_numbers && ! with_source_code)
2121 return;
2122
d647c797
AM
2123#ifdef HAVE_LIBDEBUGINFOD
2124 {
2125 bfd *debug_bfd;
2126 const char *alt_filename = NULL;
2127
2128 if (use_debuginfod)
2129 {
2130 bfd *alt_bfd;
2131
2132 /* PR 29075: Check for separate debuginfo and .gnu_debugaltlink files.
2133 They need to be passed to bfd_find_nearest_line_with_alt in case they
2134 were downloaded from debuginfod. Otherwise libbfd will attempt to
2135 search for them and fail to locate them. */
2136 debug_bfd = find_separate_debug (abfd);
2137 if (debug_bfd == NULL)
2138 debug_bfd = abfd;
2139
2140 alt_bfd = find_alt_debug (debug_bfd);
2141 if (alt_bfd != NULL)
2142 alt_filename = bfd_get_filename (alt_bfd);
2143 }
2144 else
2145 debug_bfd = abfd;
2146
2147 bfd_set_error (bfd_error_no_error);
2148 if (! bfd_find_nearest_line_with_alt (debug_bfd, alt_filename,
2149 section, syms,
2150 addr_offset, &filename,
2151 &functionname, &linenumber,
2152 &discriminator))
2153 {
2154 if (bfd_get_error () == bfd_error_no_error)
2155 return;
2156 if (! bfd_find_nearest_line_discriminator (abfd, section, syms,
2157 addr_offset, &filename,
2158 &functionname, &linenumber,
2159 &discriminator))
2160 return;
2161 }
2162 }
2163#else
9b8d1a36 2164 if (! bfd_find_nearest_line_discriminator (abfd, section, syms, addr_offset,
8b5b2529
AM
2165 &filename, &functionname,
2166 &linenumber, &discriminator))
252b5132 2167 return;
d647c797 2168#endif
252b5132
RH
2169
2170 if (filename != NULL && *filename == '\0')
2171 filename = NULL;
2172 if (functionname != NULL && *functionname == '\0')
2173 functionname = NULL;
2174
0dafdf3f
L
2175 if (filename
2176 && IS_ABSOLUTE_PATH (filename)
2177 && prefix)
2178 {
2179 char *path_up;
2180 const char *fname = filename;
e1fa0163 2181
13acb58d 2182 path = xmalloc (prefix_length + 1 + strlen (filename));
0dafdf3f
L
2183
2184 if (prefix_length)
2185 memcpy (path, prefix, prefix_length);
2186 path_up = path + prefix_length;
2187
2188 /* Build relocated filename, stripping off leading directories
e1fa0163 2189 from the initial filename if requested. */
0dafdf3f
L
2190 if (prefix_strip > 0)
2191 {
2192 int level = 0;
2193 const char *s;
2194
e1fa0163 2195 /* Skip selected directory levels. */
0dafdf3f 2196 for (s = fname + 1; *s != '\0' && level < prefix_strip; s++)
82a9ed20 2197 if (IS_DIR_SEPARATOR (*s))
0dafdf3f
L
2198 {
2199 fname = s;
2200 level++;
2201 }
2202 }
2203
e1fa0163 2204 /* Update complete filename. */
13acb58d 2205 strcpy (path_up, fname);
0dafdf3f
L
2206
2207 filename = path;
015dc7e1 2208 reloc = true;
0dafdf3f
L
2209 }
2210 else
015dc7e1 2211 reloc = false;
0dafdf3f 2212
252b5132
RH
2213 if (with_line_numbers)
2214 {
2215 if (functionname != NULL
2216 && (prev_functionname == NULL
2217 || strcmp (functionname, prev_functionname) != 0))
8b5b2529 2218 {
741cb839
EC
2219 char *demangle_alloc = NULL;
2220 if (do_demangle && functionname[0] != '\0')
2221 {
2222 /* Demangle the name. */
2223 demangle_alloc = bfd_demangle (abfd, functionname,
9b49454b 2224 demangle_flags);
741cb839
EC
2225 }
2226
2227 /* Demangling adds trailing parens, so don't print those. */
2228 if (demangle_alloc != NULL)
2229 printf ("%s:\n", sanitize_string (demangle_alloc));
2230 else
2231 printf ("%s():\n", sanitize_string (functionname));
2232
8b5b2529 2233 prev_line = -1;
741cb839 2234 free (demangle_alloc);
8b5b2529
AM
2235 }
2236 if (linenumber > 0
2237 && (linenumber != prev_line
2238 || discriminator != prev_discriminator))
2239 {
2240 if (discriminator > 0)
2241 printf ("%s:%u (discriminator %u)\n",
12add40e 2242 filename == NULL ? "???" : sanitize_string (filename),
8b5b2529
AM
2243 linenumber, discriminator);
2244 else
12add40e
NC
2245 printf ("%s:%u\n", filename == NULL
2246 ? "???" : sanitize_string (filename),
8b5b2529
AM
2247 linenumber);
2248 }
4a14e306
AK
2249 if (unwind_inlines)
2250 {
2251 const char *filename2;
2252 const char *functionname2;
2253 unsigned line2;
2254
2255 while (bfd_find_inliner_info (abfd, &filename2, &functionname2,
2256 &line2))
12add40e
NC
2257 {
2258 printf ("inlined by %s:%u",
2259 sanitize_string (filename2), line2);
2260 printf (" (%s)\n", sanitize_string (functionname2));
2261 }
4a14e306 2262 }
252b5132
RH
2263 }
2264
2265 if (with_source_code
2266 && filename != NULL
91d6fa6a 2267 && linenumber > 0)
252b5132
RH
2268 {
2269 struct print_file_list **pp, *p;
e8f5eee4 2270 unsigned l;
252b5132
RH
2271
2272 for (pp = &print_files; *pp != NULL; pp = &(*pp)->next)
8b6efd89 2273 if (filename_cmp ((*pp)->filename, filename) == 0)
252b5132
RH
2274 break;
2275 p = *pp;
2276
e8f5eee4 2277 if (p == NULL)
0dafdf3f
L
2278 {
2279 if (reloc)
2280 filename = xstrdup (filename);
5ef2d51b 2281 p = update_source_path (filename, abfd);
0dafdf3f 2282 }
252b5132 2283
91d6fa6a 2284 if (p != NULL && linenumber != p->last_line)
e8f5eee4 2285 {
3aade688 2286 if (file_start_context && p->first)
e8f5eee4 2287 l = 1;
3aade688 2288 else
252b5132 2289 {
91d6fa6a 2290 l = linenumber - SHOW_PRECEDING_CONTEXT_LINES;
3aade688 2291 if (l >= linenumber)
e8f5eee4 2292 l = 1;
43339b1d
AM
2293 if (p->max_printed >= l)
2294 {
2295 if (p->max_printed < linenumber)
2296 l = p->max_printed + 1;
2297 else
2298 l = linenumber;
2299 }
252b5132 2300 }
91d6fa6a 2301 dump_lines (p, l, linenumber);
43339b1d
AM
2302 if (p->max_printed < linenumber)
2303 p->max_printed = linenumber;
91d6fa6a 2304 p->last_line = linenumber;
e8f5eee4 2305 p->first = 0;
252b5132
RH
2306 }
2307 }
2308
2309 if (functionname != NULL
2310 && (prev_functionname == NULL
2311 || strcmp (functionname, prev_functionname) != 0))
2312 {
2313 if (prev_functionname != NULL)
2314 free (prev_functionname);
3f5e193b 2315 prev_functionname = (char *) xmalloc (strlen (functionname) + 1);
252b5132
RH
2316 strcpy (prev_functionname, functionname);
2317 }
2318
91d6fa6a
NC
2319 if (linenumber > 0 && linenumber != prev_line)
2320 prev_line = linenumber;
9b8d1a36
CC
2321
2322 if (discriminator != prev_discriminator)
2323 prev_discriminator = discriminator;
e1fa0163
NC
2324
2325 if (path)
2326 free (path);
252b5132
RH
2327}
2328
2329/* Pseudo FILE object for strings. */
2330typedef struct
2331{
2332 char *buffer;
6f104306
NS
2333 size_t pos;
2334 size_t alloc;
252b5132
RH
2335} SFILE;
2336
46dca2e0 2337/* sprintf to a "stream". */
252b5132 2338
0fd3a477 2339static int ATTRIBUTE_PRINTF_2
46dca2e0 2340objdump_sprintf (SFILE *f, const char *format, ...)
252b5132 2341{
252b5132 2342 size_t n;
46dca2e0 2343 va_list args;
252b5132 2344
6f104306 2345 while (1)
252b5132 2346 {
6f104306 2347 size_t space = f->alloc - f->pos;
3aade688 2348
6f104306
NS
2349 va_start (args, format);
2350 n = vsnprintf (f->buffer + f->pos, space, format, args);
451dad9c 2351 va_end (args);
252b5132 2352
6f104306
NS
2353 if (space > n)
2354 break;
3aade688 2355
6f104306 2356 f->alloc = (f->alloc + n) * 2;
3f5e193b 2357 f->buffer = (char *) xrealloc (f->buffer, f->alloc);
252b5132 2358 }
6f104306 2359 f->pos += n;
3aade688 2360
252b5132
RH
2361 return n;
2362}
2363
60a3da00
AB
2364/* Return an integer greater than, or equal to zero, representing the color
2365 for STYLE, or -1 if no color should be used. */
2366
2367static int
2368objdump_color_for_disassembler_style (enum disassembler_style style)
2369{
2370 int color = -1;
2371
2372 if (style == dis_style_comment_start)
2373 disassembler_in_comment = true;
2374
a88c79b7 2375 if (disassembler_color == on)
60a3da00
AB
2376 {
2377 if (disassembler_in_comment)
2378 return color;
2379
2380 switch (style)
2381 {
a88c79b7
NC
2382 case dis_style_symbol:
2383 color = 32;
2384 break;
60a3da00 2385 case dis_style_assembler_directive:
4f46c0bc 2386 case dis_style_sub_mnemonic:
a88c79b7
NC
2387 case dis_style_mnemonic:
2388 color = 33;
2389 break;
2390 case dis_style_register:
2391 color = 34;
2392 break;
60a3da00
AB
2393 case dis_style_address:
2394 case dis_style_address_offset:
a88c79b7
NC
2395 case dis_style_immediate:
2396 color = 35;
2397 break;
60a3da00 2398 default:
a88c79b7
NC
2399 case dis_style_text:
2400 color = -1;
2401 break;
60a3da00
AB
2402 }
2403 }
a88c79b7 2404 else if (disassembler_color == extended)
60a3da00
AB
2405 {
2406 if (disassembler_in_comment)
2407 return 250;
2408
2409 switch (style)
2410 {
a88c79b7
NC
2411 case dis_style_symbol:
2412 color = 40;
2413 break;
60a3da00 2414 case dis_style_assembler_directive:
4f46c0bc 2415 case dis_style_sub_mnemonic:
a88c79b7
NC
2416 case dis_style_mnemonic:
2417 color = 142;
2418 break;
2419 case dis_style_register:
2420 color = 27;
2421 break;
60a3da00
AB
2422 case dis_style_address:
2423 case dis_style_address_offset:
a88c79b7
NC
2424 case dis_style_immediate:
2425 color = 134;
2426 break;
60a3da00 2427 default:
a88c79b7
NC
2428 case dis_style_text:
2429 color = -1;
2430 break;
60a3da00
AB
2431 }
2432 }
a88c79b7
NC
2433 else if (disassembler_color != off)
2434 bfd_fatal (_("disassembly color not correctly selected"));
60a3da00
AB
2435
2436 return color;
2437}
2438
2439/* Like objdump_sprintf, but add in escape sequences to highlight the
2440 content according to STYLE. */
2441
2442static int ATTRIBUTE_PRINTF_3
2443objdump_styled_sprintf (SFILE *f, enum disassembler_style style,
2444 const char *format, ...)
2445{
2446 size_t n;
2447 va_list args;
2448 int color = objdump_color_for_disassembler_style (style);
2449
2450 if (color >= 0)
2451 {
2452 while (1)
2453 {
2454 size_t space = f->alloc - f->pos;
2455
daf2618a 2456 if (disassembler_color == on)
60a3da00
AB
2457 n = snprintf (f->buffer + f->pos, space, "\033[%dm", color);
2458 else
2459 n = snprintf (f->buffer + f->pos, space, "\033[38;5;%dm", color);
2460 if (space > n)
2461 break;
2462
2463 f->alloc = (f->alloc + n) * 2;
2464 f->buffer = (char *) xrealloc (f->buffer, f->alloc);
2465 }
2466 f->pos += n;
2467 }
2468
2469 while (1)
2470 {
2471 size_t space = f->alloc - f->pos;
2472
2473 va_start (args, format);
2474 n = vsnprintf (f->buffer + f->pos, space, format, args);
2475 va_end (args);
2476
2477 if (space > n)
2478 break;
2479
2480 f->alloc = (f->alloc + n) * 2;
2481 f->buffer = (char *) xrealloc (f->buffer, f->alloc);
2482 }
2483 f->pos += n;
2484
2485 if (color >= 0)
2486 {
2487 while (1)
2488 {
2489 size_t space = f->alloc - f->pos;
2490
2491 n = snprintf (f->buffer + f->pos, space, "\033[0m");
2492
2493 if (space > n)
2494 break;
2495
2496 f->alloc = (f->alloc + n) * 2;
2497 f->buffer = (char *) xrealloc (f->buffer, f->alloc);
2498 }
2499 f->pos += n;
2500 }
2501
2502 return n;
2503}
2504
2505/* We discard the styling information here. This function is only used
2506 when objdump is printing auxiliary information, the symbol headers, and
2507 disassembly address, or the bytes of the disassembled instruction. We
2508 don't (currently) apply styling to any of this stuff, so, for now, just
2509 print the content with no additional style added. */
2510
2511static int ATTRIBUTE_PRINTF_3
2512fprintf_styled (FILE *f, enum disassembler_style style ATTRIBUTE_UNUSED,
2513 const char *fmt, ...)
2514{
2515 int res;
2516 va_list ap;
2517
2518 va_start (ap, fmt);
2519 res = vfprintf (f, fmt, ap);
2520 va_end (ap);
2521
2522 return res;
2523}
2524
1d67fe3b
TT
2525/* Code for generating (colored) diagrams of control flow start and end
2526 points. */
2527
2528/* Structure used to store the properties of a jump. */
2529
2530struct jump_info
2531{
2532 /* The next jump, or NULL if this is the last object. */
2533 struct jump_info *next;
2534 /* The previous jump, or NULL if this is the first object. */
2535 struct jump_info *prev;
2536 /* The start addresses of the jump. */
2537 struct
2538 {
2539 /* The list of start addresses. */
2540 bfd_vma *addresses;
2541 /* The number of elements. */
2542 size_t count;
2543 /* The maximum number of elements that fit into the array. */
2544 size_t max_count;
2545 } start;
2546 /* The end address of the jump. */
2547 bfd_vma end;
2548 /* The drawing level of the jump. */
2549 int level;
2550};
2551
2552/* Construct a jump object for a jump from start
2553 to end with the corresponding level. */
2554
2555static struct jump_info *
2556jump_info_new (bfd_vma start, bfd_vma end, int level)
2557{
2558 struct jump_info *result = xmalloc (sizeof (struct jump_info));
2559
2560 result->next = NULL;
2561 result->prev = NULL;
2562 result->start.addresses = xmalloc (sizeof (bfd_vma *) * 2);
2563 result->start.addresses[0] = start;
2564 result->start.count = 1;
2565 result->start.max_count = 2;
2566 result->end = end;
2567 result->level = level;
2568
2569 return result;
2570}
2571
2572/* Free a jump object and return the next object
2573 or NULL if this was the last one. */
2574
2575static struct jump_info *
2576jump_info_free (struct jump_info *ji)
2577{
2578 struct jump_info *result = NULL;
2579
2580 if (ji)
2581 {
2582 result = ji->next;
2583 if (ji->start.addresses)
2584 free (ji->start.addresses);
2585 free (ji);
2586 }
2587
2588 return result;
2589}
2590
2591/* Get the smallest value of all start and end addresses. */
2592
2593static bfd_vma
2594jump_info_min_address (const struct jump_info *ji)
2595{
2596 bfd_vma min_address = ji->end;
2597 size_t i;
2598
2599 for (i = ji->start.count; i-- > 0;)
2600 if (ji->start.addresses[i] < min_address)
2601 min_address = ji->start.addresses[i];
2602 return min_address;
2603}
2604
2605/* Get the largest value of all start and end addresses. */
2606
2607static bfd_vma
2608jump_info_max_address (const struct jump_info *ji)
2609{
2610 bfd_vma max_address = ji->end;
2611 size_t i;
2612
2613 for (i = ji->start.count; i-- > 0;)
2614 if (ji->start.addresses[i] > max_address)
2615 max_address = ji->start.addresses[i];
2616 return max_address;
2617}
2618
2619/* Get the target address of a jump. */
2620
2621static bfd_vma
2622jump_info_end_address (const struct jump_info *ji)
2623{
2624 return ji->end;
2625}
2626
2627/* Test if an address is one of the start addresses of a jump. */
2628
015dc7e1 2629static bool
1d67fe3b
TT
2630jump_info_is_start_address (const struct jump_info *ji, bfd_vma address)
2631{
015dc7e1 2632 bool result = false;
1d67fe3b
TT
2633 size_t i;
2634
2635 for (i = ji->start.count; i-- > 0;)
2636 if (address == ji->start.addresses[i])
2637 {
015dc7e1 2638 result = true;
1d67fe3b
TT
2639 break;
2640 }
2641
2642 return result;
2643}
2644
2645/* Test if an address is the target address of a jump. */
2646
015dc7e1 2647static bool
1d67fe3b
TT
2648jump_info_is_end_address (const struct jump_info *ji, bfd_vma address)
2649{
2650 return (address == ji->end);
2651}
2652
2653/* Get the difference between the smallest and largest address of a jump. */
2654
2655static bfd_vma
2656jump_info_size (const struct jump_info *ji)
2657{
2658 return jump_info_max_address (ji) - jump_info_min_address (ji);
2659}
2660
2661/* Unlink a jump object from a list. */
2662
2663static void
2664jump_info_unlink (struct jump_info *node,
2665 struct jump_info **base)
2666{
2667 if (node->next)
2668 node->next->prev = node->prev;
2669 if (node->prev)
2670 node->prev->next = node->next;
2671 else
2672 *base = node->next;
2673 node->next = NULL;
2674 node->prev = NULL;
2675}
2676
2677/* Insert unlinked jump info node into a list. */
2678
2679static void
2680jump_info_insert (struct jump_info *node,
2681 struct jump_info *target,
2682 struct jump_info **base)
2683{
2684 node->next = target;
2685 node->prev = target->prev;
2686 target->prev = node;
2687 if (node->prev)
2688 node->prev->next = node;
2689 else
2690 *base = node;
2691}
2692
2693/* Add unlinked node to the front of a list. */
2694
2695static void
2696jump_info_add_front (struct jump_info *node,
2697 struct jump_info **base)
2698{
2699 node->next = *base;
2700 if (node->next)
2701 node->next->prev = node;
2702 node->prev = NULL;
2703 *base = node;
2704}
2705
2706/* Move linked node to target position. */
2707
2708static void
2709jump_info_move_linked (struct jump_info *node,
2710 struct jump_info *target,
2711 struct jump_info **base)
2712{
2713 /* Unlink node. */
2714 jump_info_unlink (node, base);
2715 /* Insert node at target position. */
2716 jump_info_insert (node, target, base);
2717}
2718
2719/* Test if two jumps intersect. */
2720
015dc7e1 2721static bool
1d67fe3b
TT
2722jump_info_intersect (const struct jump_info *a,
2723 const struct jump_info *b)
2724{
2725 return ((jump_info_max_address (a) >= jump_info_min_address (b))
2726 && (jump_info_min_address (a) <= jump_info_max_address (b)));
2727}
2728
2729/* Merge two compatible jump info objects. */
2730
2731static void
2732jump_info_merge (struct jump_info **base)
2733{
2734 struct jump_info *a;
2735
2736 for (a = *base; a; a = a->next)
2737 {
2738 struct jump_info *b;
2739
2740 for (b = a->next; b; b = b->next)
2741 {
2742 /* Merge both jumps into one. */
2743 if (a->end == b->end)
2744 {
2745 /* Reallocate addresses. */
2746 size_t needed_size = a->start.count + b->start.count;
2747 size_t i;
2748
2749 if (needed_size > a->start.max_count)
2750 {
2751 a->start.max_count += b->start.max_count;
2752 a->start.addresses =
2753 xrealloc (a->start.addresses,
82a9ed20 2754 a->start.max_count * sizeof (bfd_vma *));
1d67fe3b
TT
2755 }
2756
2757 /* Append start addresses. */
2758 for (i = 0; i < b->start.count; ++i)
2759 a->start.addresses[a->start.count++] =
2760 b->start.addresses[i];
2761
2762 /* Remove and delete jump. */
2763 struct jump_info *tmp = b->prev;
2764 jump_info_unlink (b, base);
2765 jump_info_free (b);
2766 b = tmp;
2767 }
2768 }
2769 }
2770}
2771
2772/* Sort jumps by their size and starting point using a stable
2773 minsort. This could be improved if sorting performance is
2774 an issue, for example by using mergesort. */
2775
2776static void
2777jump_info_sort (struct jump_info **base)
2778{
2779 struct jump_info *current_element = *base;
2780
2781 while (current_element)
2782 {
2783 struct jump_info *best_match = current_element;
2784 struct jump_info *runner = current_element->next;
2785 bfd_vma best_size = jump_info_size (best_match);
2786
2787 while (runner)
2788 {
2789 bfd_vma runner_size = jump_info_size (runner);
2790
2791 if ((runner_size < best_size)
2792 || ((runner_size == best_size)
2793 && (jump_info_min_address (runner)
2794 < jump_info_min_address (best_match))))
2795 {
2796 best_match = runner;
2797 best_size = runner_size;
2798 }
2799
2800 runner = runner->next;
2801 }
2802
2803 if (best_match == current_element)
2804 current_element = current_element->next;
2805 else
2806 jump_info_move_linked (best_match, current_element, base);
2807 }
2808}
2809
2810/* Visualize all jumps at a given address. */
2811
2812static void
82a9ed20 2813jump_info_visualize_address (bfd_vma address,
1d67fe3b
TT
2814 int max_level,
2815 char *line_buffer,
2816 uint8_t *color_buffer)
2817{
82a9ed20 2818 struct jump_info *ji = detected_jumps;
1d67fe3b 2819 size_t len = (max_level + 1) * 3;
1d67fe3b
TT
2820
2821 /* Clear line buffer. */
82a9ed20
TT
2822 memset (line_buffer, ' ', len);
2823 memset (color_buffer, 0, len);
1d67fe3b
TT
2824
2825 /* Iterate over jumps and add their ASCII art. */
82a9ed20 2826 while (ji)
1d67fe3b 2827 {
82a9ed20
TT
2828 /* Discard jumps that are never needed again. */
2829 if (jump_info_max_address (ji) < address)
2830 {
2831 struct jump_info *tmp = ji;
2832
2833 ji = ji->next;
2834 jump_info_unlink (tmp, &detected_jumps);
2835 jump_info_free (tmp);
2836 continue;
2837 }
2838
2839 /* This jump intersects with the current address. */
2840 if (jump_info_min_address (ji) <= address)
1d67fe3b
TT
2841 {
2842 /* Hash target address to get an even
2843 distribution between all values. */
2844 bfd_vma hash_address = jump_info_end_address (ji);
2845 uint8_t color = iterative_hash_object (hash_address, 0);
2846 /* Fetch line offset. */
2847 int offset = (max_level - ji->level) * 3;
2848
2849 /* Draw start line. */
2850 if (jump_info_is_start_address (ji, address))
2851 {
2852 size_t i = offset + 1;
2853
2854 for (; i < len - 1; ++i)
2855 if (line_buffer[i] == ' ')
2856 {
2857 line_buffer[i] = '-';
2858 color_buffer[i] = color;
2859 }
2860
2861 if (line_buffer[i] == ' ')
2862 {
2863 line_buffer[i] = '-';
2864 color_buffer[i] = color;
2865 }
2866 else if (line_buffer[i] == '>')
2867 {
2868 line_buffer[i] = 'X';
2869 color_buffer[i] = color;
2870 }
2871
2872 if (line_buffer[offset] == ' ')
2873 {
2874 if (address <= ji->end)
2875 line_buffer[offset] =
2876 (jump_info_min_address (ji) == address) ? '/': '+';
2877 else
2878 line_buffer[offset] =
2879 (jump_info_max_address (ji) == address) ? '\\': '+';
2880 color_buffer[offset] = color;
2881 }
2882 }
2883 /* Draw jump target. */
2884 else if (jump_info_is_end_address (ji, address))
2885 {
2886 size_t i = offset + 1;
2887
2888 for (; i < len - 1; ++i)
2889 if (line_buffer[i] == ' ')
2890 {
2891 line_buffer[i] = '-';
2892 color_buffer[i] = color;
2893 }
2894
2895 if (line_buffer[i] == ' ')
2896 {
2897 line_buffer[i] = '>';
2898 color_buffer[i] = color;
2899 }
2900 else if (line_buffer[i] == '-')
2901 {
2902 line_buffer[i] = 'X';
2903 color_buffer[i] = color;
2904 }
2905
2906 if (line_buffer[offset] == ' ')
2907 {
2908 if (jump_info_min_address (ji) < address)
2909 line_buffer[offset] =
2910 (jump_info_max_address (ji) > address) ? '>' : '\\';
2911 else
2912 line_buffer[offset] = '/';
2913 color_buffer[offset] = color;
2914 }
2915 }
2916 /* Draw intermediate line segment. */
2917 else if (line_buffer[offset] == ' ')
2918 {
2919 line_buffer[offset] = '|';
2920 color_buffer[offset] = color;
2921 }
2922 }
82a9ed20
TT
2923
2924 ji = ji->next;
1d67fe3b
TT
2925 }
2926}
2927
2928/* Clone of disassemble_bytes to detect jumps inside a function. */
2929/* FIXME: is this correct? Can we strip it down even further? */
2930
2931static struct jump_info *
2932disassemble_jumps (struct disassemble_info * inf,
2933 disassembler_ftype disassemble_fn,
2934 bfd_vma start_offset,
2935 bfd_vma stop_offset,
2936 bfd_vma rel_offset,
2937 arelent *** relppp,
2938 arelent ** relppend)
2939{
2940 struct objdump_disasm_info *aux;
2941 struct jump_info *jumps = NULL;
2942 asection *section;
2943 bfd_vma addr_offset;
2944 unsigned int opb = inf->octets_per_byte;
2945 int octets = opb;
2946 SFILE sfile;
2947
2948 aux = (struct objdump_disasm_info *) inf->application_data;
2949 section = inf->section;
2950
2951 sfile.alloc = 120;
2952 sfile.buffer = (char *) xmalloc (sfile.alloc);
2953 sfile.pos = 0;
2954
2955 inf->insn_info_valid = 0;
60a3da00
AB
2956 disassemble_set_printf (inf, &sfile, (fprintf_ftype) objdump_sprintf,
2957 (fprintf_styled_ftype) objdump_styled_sprintf);
1d67fe3b
TT
2958
2959 addr_offset = start_offset;
2960 while (addr_offset < stop_offset)
2961 {
2962 int previous_octets;
2963
2964 /* Remember the length of the previous instruction. */
2965 previous_octets = octets;
2966 octets = 0;
2967
2968 sfile.pos = 0;
2969 inf->bytes_per_line = 0;
2970 inf->bytes_per_chunk = 0;
2971 inf->flags = ((disassemble_all ? DISASSEMBLE_DATA : 0)
9b49454b 2972 | (wide_output ? WIDE_OUTPUT : 0));
1d67fe3b
TT
2973 if (machine)
2974 inf->flags |= USER_SPECIFIED_MACHINE_TYPE;
2975
2976 if (inf->disassembler_needs_relocs
2977 && (bfd_get_file_flags (aux->abfd) & EXEC_P) == 0
2978 && (bfd_get_file_flags (aux->abfd) & DYNAMIC) == 0
2979 && *relppp < relppend)
2980 {
2981 bfd_signed_vma distance_to_rel;
2982
2983 distance_to_rel = (**relppp)->address - (rel_offset + addr_offset);
2984
2985 /* Check to see if the current reloc is associated with
2986 the instruction that we are about to disassemble. */
2987 if (distance_to_rel == 0
2988 /* FIXME: This is wrong. We are trying to catch
2989 relocs that are addressed part way through the
2990 current instruction, as might happen with a packed
2991 VLIW instruction. Unfortunately we do not know the
2992 length of the current instruction since we have not
2993 disassembled it yet. Instead we take a guess based
2994 upon the length of the previous instruction. The
2995 proper solution is to have a new target-specific
2996 disassembler function which just returns the length
2997 of an instruction at a given address without trying
2998 to display its disassembly. */
2999 || (distance_to_rel > 0
3000 && distance_to_rel < (bfd_signed_vma) (previous_octets/ opb)))
3001 {
3002 inf->flags |= INSN_HAS_RELOC;
3003 }
3004 }
3005
3006 if (! disassemble_all
3007 && (section->flags & (SEC_CODE | SEC_HAS_CONTENTS))
3008 == (SEC_CODE | SEC_HAS_CONTENTS))
3009 /* Set a stop_vma so that the disassembler will not read
3010 beyond the next symbol. We assume that symbols appear on
3011 the boundaries between instructions. We only do this when
3012 disassembling code of course, and when -D is in effect. */
3013 inf->stop_vma = section->vma + stop_offset;
3014
3015 inf->stop_offset = stop_offset;
3016
3017 /* Extract jump information. */
3018 inf->insn_info_valid = 0;
60a3da00 3019 disassembler_in_comment = false;
1d67fe3b
TT
3020 octets = (*disassemble_fn) (section->vma + addr_offset, inf);
3021 /* Test if a jump was detected. */
3022 if (inf->insn_info_valid
3023 && ((inf->insn_type == dis_branch)
3024 || (inf->insn_type == dis_condbranch)
3025 || (inf->insn_type == dis_jsr)
3026 || (inf->insn_type == dis_condjsr))
3027 && (inf->target >= section->vma + start_offset)
3028 && (inf->target < section->vma + stop_offset))
3029 {
3030 struct jump_info *ji =
3031 jump_info_new (section->vma + addr_offset, inf->target, -1);
3032 jump_info_add_front (ji, &jumps);
3033 }
3034
3035 inf->stop_vma = 0;
3036
3037 addr_offset += octets / opb;
3038 }
3039
60a3da00
AB
3040 disassemble_set_printf (inf, (void *) stdout, (fprintf_ftype) fprintf,
3041 (fprintf_styled_ftype) fprintf_styled);
1d67fe3b
TT
3042 free (sfile.buffer);
3043
3044 /* Merge jumps. */
3045 jump_info_merge (&jumps);
3046 /* Process jumps. */
3047 jump_info_sort (&jumps);
3048
3049 /* Group jumps by level. */
3050 struct jump_info *last_jump = jumps;
3051 int max_level = -1;
3052
3053 while (last_jump)
3054 {
3055 /* The last jump is part of the next group. */
3056 struct jump_info *base = last_jump;
3057 /* Increment level. */
3058 base->level = ++max_level;
3059
3060 /* Find jumps that can be combined on the same
3061 level, with the largest jumps tested first.
3062 This has the advantage that large jumps are on
3063 lower levels and do not intersect with small
3064 jumps that get grouped on higher levels. */
3065 struct jump_info *exchange_item = last_jump->next;
3066 struct jump_info *it = exchange_item;
3067
3068 for (; it; it = it->next)
3069 {
3070 /* Test if the jump intersects with any
3071 jump from current group. */
015dc7e1 3072 bool ok = true;
1d67fe3b
TT
3073 struct jump_info *it_collision;
3074
3075 for (it_collision = base;
3076 it_collision != exchange_item;
3077 it_collision = it_collision->next)
3078 {
3079 /* This jump intersects so we leave it out. */
3080 if (jump_info_intersect (it_collision, it))
3081 {
015dc7e1 3082 ok = false;
1d67fe3b
TT
3083 break;
3084 }
3085 }
3086
3087 /* Add jump to group. */
3088 if (ok)
3089 {
3090 /* Move current element to the front. */
3091 if (it != exchange_item)
3092 {
3093 struct jump_info *save = it->prev;
3094 jump_info_move_linked (it, exchange_item, &jumps);
3095 last_jump = it;
3096 it = save;
3097 }
3098 else
3099 {
3100 last_jump = exchange_item;
3101 exchange_item = exchange_item->next;
3102 }
3103 last_jump->level = max_level;
3104 }
3105 }
3106
3107 /* Move to next group. */
3108 last_jump = exchange_item;
3109 }
3110
3111 return jumps;
3112}
3113
252b5132
RH
3114/* The number of zeroes we want to see before we start skipping them.
3115 The number is arbitrarily chosen. */
3116
0bcb06d2 3117#define DEFAULT_SKIP_ZEROES 8
252b5132
RH
3118
3119/* The number of zeroes to skip at the end of a section. If the
3120 number of zeroes at the end is between SKIP_ZEROES_AT_END and
3121 SKIP_ZEROES, they will be disassembled. If there are fewer than
3122 SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic
3123 attempt to avoid disassembling zeroes inserted by section
3124 alignment. */
3125
0bcb06d2 3126#define DEFAULT_SKIP_ZEROES_AT_END 3
252b5132 3127
aebcfb76
NC
3128static int
3129null_print (const void * stream ATTRIBUTE_UNUSED, const char * format ATTRIBUTE_UNUSED, ...)
3130{
3131 return 1;
3132}
3133
60a3da00
AB
3134/* Like null_print, but takes the extra STYLE argument. As this is not
3135 going to print anything, the extra argument is just ignored. */
3136
3137static int
3138null_styled_print (const void * stream ATTRIBUTE_UNUSED,
3139 enum disassembler_style style ATTRIBUTE_UNUSED,
3140 const char * format ATTRIBUTE_UNUSED, ...)
3141{
3142 return 1;
3143}
3144
ece12829
TT
3145/* Print out jump visualization. */
3146
3147static void
3148print_jump_visualisation (bfd_vma addr, int max_level, char *line_buffer,
3149 uint8_t *color_buffer)
3150{
3151 if (!line_buffer)
3152 return;
3153
3154 jump_info_visualize_address (addr, max_level, line_buffer, color_buffer);
3155
3156 size_t line_buffer_size = strlen (line_buffer);
3157 char last_color = 0;
3158 size_t i;
3159
3160 for (i = 0; i <= line_buffer_size; ++i)
3161 {
3162 if (color_output)
3163 {
3164 uint8_t color = (i < line_buffer_size) ? color_buffer[i]: 0;
3165
3166 if (color != last_color)
3167 {
3168 if (color)
3169 if (extended_color_output)
3170 /* Use extended 8bit color, but
3171 do not choose dark colors. */
3172 printf ("\033[38;5;%dm", 124 + (color % 108));
3173 else
3174 /* Use simple terminal colors. */
3175 printf ("\033[%dm", 31 + (color % 7));
3176 else
3177 /* Clear color. */
3178 printf ("\033[0m");
3179 last_color = color;
3180 }
3181 }
3182 putchar ((i < line_buffer_size) ? line_buffer[i]: ' ');
3183 }
3184}
3185
252b5132
RH
3186/* Disassemble some data in memory between given values. */
3187
3188static void
015dc7e1
AM
3189disassemble_bytes (struct disassemble_info *inf,
3190 disassembler_ftype disassemble_fn,
3191 bool insns,
3192 bfd_byte *data,
3193 bfd_vma start_offset,
3194 bfd_vma stop_offset,
3195 bfd_vma rel_offset,
3196 arelent ***relppp,
3197 arelent **relppend)
252b5132
RH
3198{
3199 struct objdump_disasm_info *aux;
3200 asection *section;
60832332
AM
3201 unsigned int octets_per_line;
3202 unsigned int skip_addr_chars;
940b2b78 3203 bfd_vma addr_offset;
91d6fa6a
NC
3204 unsigned int opb = inf->octets_per_byte;
3205 unsigned int skip_zeroes = inf->skip_zeroes;
3206 unsigned int skip_zeroes_at_end = inf->skip_zeroes_at_end;
60832332 3207 size_t octets;
6f104306 3208 SFILE sfile;
252b5132 3209
91d6fa6a 3210 aux = (struct objdump_disasm_info *) inf->application_data;
f59f8978 3211 section = inf->section;
252b5132 3212
6f104306 3213 sfile.alloc = 120;
3f5e193b 3214 sfile.buffer = (char *) xmalloc (sfile.alloc);
6f104306 3215 sfile.pos = 0;
3aade688 3216
3dcb3fcb
L
3217 if (insn_width)
3218 octets_per_line = insn_width;
3219 else if (insns)
940b2b78 3220 octets_per_line = 4;
252b5132 3221 else
940b2b78 3222 octets_per_line = 16;
252b5132
RH
3223
3224 /* Figure out how many characters to skip at the start of an
3225 address, to make the disassembly look nicer. We discard leading
3226 zeroes in chunks of 4, ensuring that there is always a leading
3227 zero remaining. */
3228 skip_addr_chars = 0;
b1bc1394 3229 if (!no_addresses && !prefix_addresses)
252b5132
RH
3230 {
3231 char buf[30];
17ceb936
AM
3232
3233 bfd_sprintf_vma (aux->abfd, buf, section->vma + section->size / opb);
3234
3235 while (buf[skip_addr_chars] == '0')
3236 ++skip_addr_chars;
3237
3238 /* Don't discard zeros on overflow. */
3239 if (buf[skip_addr_chars] == '\0' && section->vma != 0)
3240 skip_addr_chars = 0;
3241
3242 if (skip_addr_chars != 0)
3243 skip_addr_chars = (skip_addr_chars - 1) & -4;
252b5132
RH
3244 }
3245
91d6fa6a 3246 inf->insn_info_valid = 0;
252b5132 3247
1d67fe3b 3248 /* Determine maximum level. */
82a9ed20
TT
3249 uint8_t *color_buffer = NULL;
3250 char *line_buffer = NULL;
1d67fe3b 3251 int max_level = -1;
1d67fe3b 3252
82a9ed20
TT
3253 /* Some jumps were detected. */
3254 if (detected_jumps)
1d67fe3b 3255 {
82a9ed20
TT
3256 struct jump_info *ji;
3257
3258 /* Find maximum jump level. */
3259 for (ji = detected_jumps; ji; ji = ji->next)
1d67fe3b 3260 {
82a9ed20
TT
3261 if (ji->level > max_level)
3262 max_level = ji->level;
1d67fe3b 3263 }
1d67fe3b 3264
82a9ed20
TT
3265 /* Allocate buffers. */
3266 size_t len = (max_level + 1) * 3 + 1;
3267 line_buffer = xmalloc (len);
1d67fe3b 3268 line_buffer[len - 1] = 0;
82a9ed20 3269 color_buffer = xmalloc (len);
1d67fe3b
TT
3270 color_buffer[len - 1] = 0;
3271 }
3272
940b2b78
TW
3273 addr_offset = start_offset;
3274 while (addr_offset < stop_offset)
252b5132 3275 {
015dc7e1 3276 bool need_nl = false;
ce04548a 3277
ce04548a 3278 octets = 0;
252b5132 3279
bb7c70ed
NC
3280 /* Make sure we don't use relocs from previous instructions. */
3281 aux->reloc = NULL;
3282
940b2b78 3283 /* If we see more than SKIP_ZEROES octets of zeroes, we just
43ac9881 3284 print `...'. */
60832332
AM
3285 if (! disassemble_zeroes)
3286 for (; addr_offset * opb + octets < stop_offset * opb; octets++)
3287 if (data[addr_offset * opb + octets] != 0)
3288 break;
252b5132 3289 if (! disassemble_zeroes
91d6fa6a
NC
3290 && (inf->insn_info_valid == 0
3291 || inf->branch_delay_insns == 0)
60832332
AM
3292 && (octets >= skip_zeroes
3293 || (addr_offset * opb + octets == stop_offset * opb
3294 && octets < skip_zeroes_at_end)))
252b5132 3295 {
940b2b78 3296 /* If there are more nonzero octets to follow, we only skip
43ac9881
AM
3297 zeroes in multiples of 4, to try to avoid running over
3298 the start of an instruction which happens to start with
3299 zero. */
60832332
AM
3300 if (addr_offset * opb + octets != stop_offset * opb)
3301 octets &= ~3;
98ec6e72
NC
3302
3303 /* If we are going to display more data, and we are displaying
3304 file offsets, then tell the user how many zeroes we skip
3305 and the file offset from where we resume dumping. */
60832332
AM
3306 if (display_file_offsets
3307 && addr_offset + octets / opb < stop_offset)
3308 printf (_("\t... (skipping %lu zeroes, "
3309 "resuming at file offset: 0x%lx)\n"),
3310 (unsigned long) (octets / opb),
0af1713e 3311 (unsigned long) (section->filepos
60832332 3312 + addr_offset + octets / opb));
98ec6e72
NC
3313 else
3314 printf ("\t...\n");
252b5132
RH
3315 }
3316 else
3317 {
3318 char buf[50];
60832332
AM
3319 unsigned int bpc = 0;
3320 unsigned int pb = 0;
252b5132 3321
252b5132 3322 if (with_line_numbers || with_source_code)
bc79cded 3323 show_line (aux->abfd, section, addr_offset);
252b5132 3324
b1bc1394
AM
3325 if (no_addresses)
3326 printf ("\t");
3327 else if (!prefix_addresses)
252b5132
RH
3328 {
3329 char *s;
3330
d8180c76 3331 bfd_sprintf_vma (aux->abfd, buf, section->vma + addr_offset);
252b5132
RH
3332 for (s = buf + skip_addr_chars; *s == '0'; s++)
3333 *s = ' ';
3334 if (*s == '\0')
3335 *--s = '0';
3336 printf ("%s:\t", buf + skip_addr_chars);
3337 }
3338 else
3339 {
015dc7e1 3340 aux->require_sec = true;
91d6fa6a 3341 objdump_print_address (section->vma + addr_offset, inf);
015dc7e1 3342 aux->require_sec = false;
252b5132
RH
3343 putchar (' ');
3344 }
3345
ece12829
TT
3346 print_jump_visualisation (section->vma + addr_offset,
3347 max_level, line_buffer,
3348 color_buffer);
1d67fe3b 3349
252b5132
RH
3350 if (insns)
3351 {
60832332
AM
3352 int insn_size;
3353
6f104306 3354 sfile.pos = 0;
60a3da00
AB
3355 disassemble_set_printf
3356 (inf, &sfile, (fprintf_ftype) objdump_sprintf,
3357 (fprintf_styled_ftype) objdump_styled_sprintf);
91d6fa6a
NC
3358 inf->bytes_per_line = 0;
3359 inf->bytes_per_chunk = 0;
dd7efa79
PB
3360 inf->flags = ((disassemble_all ? DISASSEMBLE_DATA : 0)
3361 | (wide_output ? WIDE_OUTPUT : 0));
0313a2b8 3362 if (machine)
91d6fa6a 3363 inf->flags |= USER_SPECIFIED_MACHINE_TYPE;
252b5132 3364
91d6fa6a 3365 if (inf->disassembler_needs_relocs
7df428b1
RS
3366 && (bfd_get_file_flags (aux->abfd) & EXEC_P) == 0
3367 && (bfd_get_file_flags (aux->abfd) & DYNAMIC) == 0
d99b6465 3368 && *relppp < relppend)
ce04548a
NC
3369 {
3370 bfd_signed_vma distance_to_rel;
0a4632b5
AM
3371 int max_reloc_offset
3372 = aux->abfd->arch_info->max_reloc_offset_into_insn;
ce04548a 3373
0a4632b5
AM
3374 distance_to_rel = ((**relppp)->address - rel_offset
3375 - addr_offset);
ce04548a 3376
60832332 3377 insn_size = 0;
aebcfb76 3378 if (distance_to_rel > 0
0a4632b5
AM
3379 && (max_reloc_offset < 0
3380 || distance_to_rel <= max_reloc_offset))
aebcfb76
NC
3381 {
3382 /* This reloc *might* apply to the current insn,
3383 starting somewhere inside it. Discover the length
3384 of the current insn so that the check below will
3385 work. */
3386 if (insn_width)
3387 insn_size = insn_width;
3388 else
3389 {
3390 /* We find the length by calling the dissassembler
3391 function with a dummy print handler. This should
3392 work unless the disassembler is not expecting to
3393 be called multiple times for the same address.
3394
3395 This does mean disassembling the instruction
3396 twice, but we only do this when there is a high
3397 probability that there is a reloc that will
3398 affect the instruction. */
60a3da00
AB
3399 disassemble_set_printf
3400 (inf, inf->stream, (fprintf_ftype) null_print,
3401 (fprintf_styled_ftype) null_styled_print);
aebcfb76
NC
3402 insn_size = disassemble_fn (section->vma
3403 + addr_offset, inf);
60a3da00
AB
3404 disassemble_set_printf
3405 (inf, inf->stream,
3406 (fprintf_ftype) objdump_sprintf,
3407 (fprintf_styled_ftype) objdump_styled_sprintf);
aebcfb76
NC
3408 }
3409 }
3410
ce04548a
NC
3411 /* Check to see if the current reloc is associated with
3412 the instruction that we are about to disassemble. */
3413 if (distance_to_rel == 0
ce04548a 3414 || (distance_to_rel > 0
0a4632b5 3415 && distance_to_rel < insn_size / (int) opb))
ce04548a 3416 {
91d6fa6a 3417 inf->flags |= INSN_HAS_RELOC;
ce04548a
NC
3418 aux->reloc = **relppp;
3419 }
ce04548a 3420 }
d99b6465 3421
bdc4de1b 3422 if (! disassemble_all
60832332
AM
3423 && ((section->flags & (SEC_CODE | SEC_HAS_CONTENTS))
3424 == (SEC_CODE | SEC_HAS_CONTENTS)))
bdc4de1b
NC
3425 /* Set a stop_vma so that the disassembler will not read
3426 beyond the next symbol. We assume that symbols appear on
3427 the boundaries between instructions. We only do this when
3428 disassembling code of course, and when -D is in effect. */
3429 inf->stop_vma = section->vma + stop_offset;
3aade688 3430
53b2f36b 3431 inf->stop_offset = stop_offset;
60a3da00 3432 disassembler_in_comment = false;
60832332
AM
3433 insn_size = (*disassemble_fn) (section->vma + addr_offset, inf);
3434 octets = insn_size;
bdc4de1b
NC
3435
3436 inf->stop_vma = 0;
60a3da00
AB
3437 disassemble_set_printf (inf, stdout, (fprintf_ftype) fprintf,
3438 (fprintf_styled_ftype) fprintf_styled);
91d6fa6a
NC
3439 if (insn_width == 0 && inf->bytes_per_line != 0)
3440 octets_per_line = inf->bytes_per_line;
60832332 3441 if (insn_size < (int) opb)
e07bf1ac 3442 {
6f104306 3443 if (sfile.pos)
e07bf1ac 3444 printf ("%s\n", sfile.buffer);
60832332 3445 if (insn_size >= 0)
a8c62f1c
AM
3446 {
3447 non_fatal (_("disassemble_fn returned length %d"),
60832332 3448 insn_size);
a8c62f1c
AM
3449 exit_status = 1;
3450 }
e07bf1ac
ILT
3451 break;
3452 }
252b5132
RH
3453 }
3454 else
3455 {
b4c96d0d 3456 bfd_vma j;
252b5132 3457
940b2b78
TW
3458 octets = octets_per_line;
3459 if (addr_offset + octets / opb > stop_offset)
3460 octets = (stop_offset - addr_offset) * opb;
252b5132 3461
940b2b78 3462 for (j = addr_offset * opb; j < addr_offset * opb + octets; ++j)
252b5132 3463 {
3882b010 3464 if (ISPRINT (data[j]))
940b2b78 3465 buf[j - addr_offset * opb] = data[j];
252b5132 3466 else
940b2b78 3467 buf[j - addr_offset * opb] = '.';
252b5132 3468 }
940b2b78 3469 buf[j - addr_offset * opb] = '\0';
252b5132
RH
3470 }
3471
3472 if (prefix_addresses
3473 ? show_raw_insn > 0
3474 : show_raw_insn >= 0)
3475 {
b4c96d0d 3476 bfd_vma j;
252b5132
RH
3477
3478 /* If ! prefix_addresses and ! wide_output, we print
43ac9881 3479 octets_per_line octets per line. */
940b2b78
TW
3480 pb = octets;
3481 if (pb > octets_per_line && ! prefix_addresses && ! wide_output)
3482 pb = octets_per_line;
252b5132 3483
91d6fa6a
NC
3484 if (inf->bytes_per_chunk)
3485 bpc = inf->bytes_per_chunk;
252b5132
RH
3486 else
3487 bpc = 1;
3488
940b2b78 3489 for (j = addr_offset * opb; j < addr_offset * opb + pb; j += bpc)
252b5132 3490 {
ae87f7e7
NC
3491 /* PR 21580: Check for a buffer ending early. */
3492 if (j + bpc <= stop_offset * opb)
252b5132 3493 {
60832332 3494 unsigned int k;
ae87f7e7
NC
3495
3496 if (inf->display_endian == BFD_ENDIAN_LITTLE)
3497 {
60832332 3498 for (k = bpc; k-- != 0; )
ae87f7e7
NC
3499 printf ("%02x", (unsigned) data[j + k]);
3500 }
3501 else
3502 {
3503 for (k = 0; k < bpc; k++)
3504 printf ("%02x", (unsigned) data[j + k]);
3505 }
252b5132 3506 }
ae87f7e7 3507 putchar (' ');
252b5132
RH
3508 }
3509
940b2b78 3510 for (; pb < octets_per_line; pb += bpc)
252b5132 3511 {
60832332 3512 unsigned int k;
252b5132
RH
3513
3514 for (k = 0; k < bpc; k++)
3515 printf (" ");
3516 putchar (' ');
3517 }
3518
3519 /* Separate raw data from instruction by extra space. */
3520 if (insns)
3521 putchar ('\t');
3522 else
3523 printf (" ");
3524 }
3525
3526 if (! insns)
3527 printf ("%s", buf);
6f104306
NS
3528 else if (sfile.pos)
3529 printf ("%s", sfile.buffer);
252b5132
RH
3530
3531 if (prefix_addresses
3532 ? show_raw_insn > 0
3533 : show_raw_insn >= 0)
3534 {
940b2b78 3535 while (pb < octets)
252b5132 3536 {
b4c96d0d 3537 bfd_vma j;
252b5132
RH
3538 char *s;
3539
3540 putchar ('\n');
940b2b78 3541 j = addr_offset * opb + pb;
252b5132 3542
b1bc1394
AM
3543 if (no_addresses)
3544 printf ("\t");
3545 else
3546 {
3547 bfd_sprintf_vma (aux->abfd, buf, section->vma + j / opb);
3548 for (s = buf + skip_addr_chars; *s == '0'; s++)
3549 *s = ' ';
3550 if (*s == '\0')
3551 *--s = '0';
3552 printf ("%s:\t", buf + skip_addr_chars);
3553 }
252b5132 3554
ece12829
TT
3555 print_jump_visualisation (section->vma + j / opb,
3556 max_level, line_buffer,
3557 color_buffer);
3558
940b2b78
TW
3559 pb += octets_per_line;
3560 if (pb > octets)
3561 pb = octets;
3562 for (; j < addr_offset * opb + pb; j += bpc)
252b5132 3563 {
d16fdddb
NC
3564 /* PR 21619: Check for a buffer ending early. */
3565 if (j + bpc <= stop_offset * opb)
252b5132 3566 {
60832332 3567 unsigned int k;
d16fdddb
NC
3568
3569 if (inf->display_endian == BFD_ENDIAN_LITTLE)
3570 {
60832332 3571 for (k = bpc; k-- != 0; )
d16fdddb
NC
3572 printf ("%02x", (unsigned) data[j + k]);
3573 }
3574 else
3575 {
3576 for (k = 0; k < bpc; k++)
3577 printf ("%02x", (unsigned) data[j + k]);
3578 }
252b5132 3579 }
d16fdddb 3580 putchar (' ');
252b5132
RH
3581 }
3582 }
3583 }
3584
3585 if (!wide_output)
3586 putchar ('\n');
3587 else
015dc7e1 3588 need_nl = true;
252b5132
RH
3589 }
3590
fd7bb956
AM
3591 while ((*relppp) < relppend
3592 && (**relppp)->address < rel_offset + addr_offset + octets / opb)
252b5132 3593 {
fd7bb956 3594 if (dump_reloc_info || dump_dynamic_reloc_info)
252b5132
RH
3595 {
3596 arelent *q;
3597
3598 q = **relppp;
3599
3600 if (wide_output)
3601 putchar ('\t');
3602 else
3603 printf ("\t\t\t");
3604
b1bc1394
AM
3605 if (!no_addresses)
3606 {
3607 objdump_print_value (section->vma - rel_offset + q->address,
015dc7e1 3608 inf, true);
b1bc1394
AM
3609 printf (": ");
3610 }
252b5132 3611
f9ecb0a4 3612 if (q->howto == NULL)
b1bc1394 3613 printf ("*unknown*\t");
f9ecb0a4 3614 else if (q->howto->name)
b1bc1394 3615 printf ("%s\t", q->howto->name);
f9ecb0a4 3616 else
b1bc1394 3617 printf ("%d\t", q->howto->type);
252b5132
RH
3618
3619 if (q->sym_ptr_ptr == NULL || *q->sym_ptr_ptr == NULL)
3620 printf ("*unknown*");
3621 else
3622 {
3623 const char *sym_name;
3624
3625 sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
3626 if (sym_name != NULL && *sym_name != '\0')
91d6fa6a 3627 objdump_print_symname (aux->abfd, inf, *q->sym_ptr_ptr);
252b5132
RH
3628 else
3629 {
3630 asection *sym_sec;
3631
e6f7f6d1 3632 sym_sec = bfd_asymbol_section (*q->sym_ptr_ptr);
fd361982 3633 sym_name = bfd_section_name (sym_sec);
252b5132
RH
3634 if (sym_name == NULL || *sym_name == '\0')
3635 sym_name = "*unknown*";
12add40e 3636 printf ("%s", sanitize_string (sym_name));
252b5132
RH
3637 }
3638 }
3639
3640 if (q->addend)
3641 {
839f41a3
AM
3642 bfd_vma addend = q->addend;
3643 if ((bfd_signed_vma) addend < 0)
343dbc36
L
3644 {
3645 printf ("-0x");
3646 addend = -addend;
3647 }
3648 else
3649 printf ("+0x");
015dc7e1 3650 objdump_print_value (addend, inf, true);
252b5132
RH
3651 }
3652
3653 printf ("\n");
015dc7e1 3654 need_nl = false;
252b5132 3655 }
fd7bb956 3656 ++(*relppp);
252b5132
RH
3657 }
3658
3659 if (need_nl)
3660 printf ("\n");
3661
940b2b78 3662 addr_offset += octets / opb;
252b5132 3663 }
6f104306
NS
3664
3665 free (sfile.buffer);
1d67fe3b
TT
3666 free (line_buffer);
3667 free (color_buffer);
252b5132
RH
3668}
3669
155e0d23 3670static void
91d6fa6a 3671disassemble_section (bfd *abfd, asection *section, void *inf)
155e0d23 3672{
015dc7e1
AM
3673 const struct elf_backend_data *bed;
3674 bfd_vma sign_adjust = 0;
3675 struct disassemble_info *pinfo = (struct disassemble_info *) inf;
3676 struct objdump_disasm_info *paux;
3677 unsigned int opb = pinfo->octets_per_byte;
3678 bfd_byte *data = NULL;
3679 bfd_size_type datasize = 0;
3680 arelent **rel_pp = NULL;
3681 arelent **rel_ppstart = NULL;
3682 arelent **rel_ppend;
3683 bfd_vma stop_offset;
3684 asymbol *sym = NULL;
3685 long place = 0;
3686 long rel_count;
3687 bfd_vma rel_offset;
3688 unsigned long addr_offset;
3689 bool do_print;
baae986a
NC
3690 enum loop_control
3691 {
3692 stop_offset_reached,
3693 function_sym,
3694 next_sym
3695 } loop_until;
155e0d23 3696
0a3137ce
AM
3697 if (only_list == NULL)
3698 {
3699 /* Sections that do not contain machine
3700 code are not normally disassembled. */
3701 if ((section->flags & SEC_HAS_CONTENTS) == 0)
3702 return;
155e0d23 3703
0a3137ce
AM
3704 if (! disassemble_all
3705 && (section->flags & SEC_CODE) == 0)
3706 return;
3707 }
3708 else if (!process_section_p (section))
155e0d23
NC
3709 return;
3710
fd361982 3711 datasize = bfd_section_size (section);
60a02042 3712 if (datasize == 0)
155e0d23
NC
3713 return;
3714
643902a4
AS
3715 if (start_address == (bfd_vma) -1
3716 || start_address < section->vma)
3717 addr_offset = 0;
3718 else
3719 addr_offset = start_address - section->vma;
3720
3721 if (stop_address == (bfd_vma) -1)
3722 stop_offset = datasize / opb;
3723 else
3724 {
3725 if (stop_address < section->vma)
3726 stop_offset = 0;
3727 else
3728 stop_offset = stop_address - section->vma;
3729 if (stop_offset > datasize / opb)
3730 stop_offset = datasize / opb;
3731 }
3732
3733 if (addr_offset >= stop_offset)
3734 return;
3735
155e0d23 3736 /* Decide which set of relocs to use. Load them if necessary. */
3b9ad1cc 3737 paux = (struct objdump_disasm_info *) pinfo->application_data;
c3f72de4 3738 if (pinfo->dynrelbuf && dump_dynamic_reloc_info)
155e0d23 3739 {
c3f72de4
AM
3740 rel_pp = pinfo->dynrelbuf;
3741 rel_count = pinfo->dynrelcount;
155e0d23 3742 /* Dynamic reloc addresses are absolute, non-dynamic are section
50c2245b 3743 relative. REL_OFFSET specifies the reloc address corresponding
155e0d23 3744 to the start of this section. */
68b3b8dc 3745 rel_offset = section->vma;
155e0d23
NC
3746 }
3747 else
3748 {
3749 rel_count = 0;
3750 rel_pp = NULL;
3751 rel_offset = 0;
3752
3753 if ((section->flags & SEC_RELOC) != 0
d99b6465 3754 && (dump_reloc_info || pinfo->disassembler_needs_relocs))
155e0d23
NC
3755 {
3756 long relsize;
3757
3758 relsize = bfd_get_reloc_upper_bound (abfd, section);
3759 if (relsize < 0)
ffdfc835 3760 my_bfd_nonfatal (bfd_get_filename (abfd));
155e0d23
NC
3761
3762 if (relsize > 0)
3763 {
ffdfc835 3764 rel_pp = (arelent **) xmalloc (relsize);
155e0d23
NC
3765 rel_count = bfd_canonicalize_reloc (abfd, section, rel_pp, syms);
3766 if (rel_count < 0)
ffdfc835
AM
3767 {
3768 my_bfd_nonfatal (bfd_get_filename (abfd));
3769 free (rel_pp);
3770 rel_pp = NULL;
3771 rel_count = 0;
3772 }
3773 else if (rel_count > 1)
3774 /* Sort the relocs by address. */
3775 qsort (rel_pp, rel_count, sizeof (arelent *), compare_relocs);
3776 rel_ppstart = rel_pp;
155e0d23
NC
3777 }
3778 }
155e0d23 3779 }
36f61bf2 3780 rel_ppend = PTR_ADD (rel_pp, rel_count);
155e0d23 3781
bae7501e 3782 if (!bfd_malloc_and_get_section (abfd, section, &data))
b02cd3e9
AM
3783 {
3784 non_fatal (_("Reading section %s failed because: %s"),
3785 section->name, bfd_errmsg (bfd_get_error ()));
3786 return;
3787 }
155e0d23 3788
155e0d23
NC
3789 pinfo->buffer = data;
3790 pinfo->buffer_vma = section->vma;
3791 pinfo->buffer_length = datasize;
3792 pinfo->section = section;
3793
660df28a
AM
3794 /* Sort the symbols into value and section order. */
3795 compare_section = section;
41da0822
AM
3796 if (sorted_symcount > 1)
3797 qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols);
660df28a 3798
155e0d23
NC
3799 /* Skip over the relocs belonging to addresses below the
3800 start address. */
3801 while (rel_pp < rel_ppend
3802 && (*rel_pp)->address < rel_offset + addr_offset)
3803 ++rel_pp;
3804
12add40e 3805 printf (_("\nDisassembly of section %s:\n"), sanitize_string (section->name));
155e0d23
NC
3806
3807 /* Find the nearest symbol forwards from our current position. */
015dc7e1 3808 paux->require_sec = true;
3f5e193b 3809 sym = (asymbol *) find_symbol_for_address (section->vma + addr_offset,
9b49454b
AM
3810 (struct disassemble_info *) inf,
3811 &place);
015dc7e1 3812 paux->require_sec = false;
155e0d23 3813
46bc35a9
RS
3814 /* PR 9774: If the target used signed addresses then we must make
3815 sure that we sign extend the value that we calculate for 'addr'
3816 in the loop below. */
1b0adfe0
NC
3817 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
3818 && (bed = get_elf_backend_data (abfd)) != NULL
3819 && bed->sign_extend_vma)
46bc35a9 3820 sign_adjust = (bfd_vma) 1 << (bed->s->arch_size - 1);
1b0adfe0 3821
155e0d23
NC
3822 /* Disassemble a block of instructions up to the address associated with
3823 the symbol we have just found. Then print the symbol and find the
3824 next symbol on. Repeat until we have disassembled the entire section
3825 or we have reached the end of the address range we are interested in. */
baae986a
NC
3826 do_print = paux->symbol == NULL;
3827 loop_until = stop_offset_reached;
3828
155e0d23
NC
3829 while (addr_offset < stop_offset)
3830 {
22a398e1 3831 bfd_vma addr;
155e0d23 3832 asymbol *nextsym;
bdc4de1b 3833 bfd_vma nextstop_offset;
015dc7e1 3834 bool insns;
155e0d23 3835
22a398e1 3836 addr = section->vma + addr_offset;
095ad3b8 3837 addr = ((addr & ((sign_adjust << 1) - 1)) ^ sign_adjust) - sign_adjust;
22a398e1
NC
3838
3839 if (sym != NULL && bfd_asymbol_value (sym) <= addr)
155e0d23
NC
3840 {
3841 int x;
3842
3843 for (x = place;
3844 (x < sorted_symcount
22a398e1 3845 && (bfd_asymbol_value (sorted_syms[x]) <= addr));
155e0d23
NC
3846 ++x)
3847 continue;
3848
22a398e1 3849 pinfo->symbols = sorted_syms + place;
155e0d23 3850 pinfo->num_symbols = x - place;
2087ad84 3851 pinfo->symtab_pos = place;
155e0d23
NC
3852 }
3853 else
22a398e1
NC
3854 {
3855 pinfo->symbols = NULL;
3856 pinfo->num_symbols = 0;
2087ad84 3857 pinfo->symtab_pos = -1;
22a398e1 3858 }
155e0d23 3859
baae986a
NC
3860 /* If we are only disassembling from a specific symbol,
3861 check to see if we should start or stop displaying. */
d3def5d7
MY
3862 if (sym && paux->symbol)
3863 {
baae986a
NC
3864 if (do_print)
3865 {
3866 /* See if we should stop printing. */
3867 switch (loop_until)
3868 {
3869 case function_sym:
3870 if (sym->flags & BSF_FUNCTION)
015dc7e1 3871 do_print = false;
baae986a
NC
3872 break;
3873
3874 case stop_offset_reached:
3875 /* Handled by the while loop. */
3876 break;
d3def5d7 3877
baae986a
NC
3878 case next_sym:
3879 /* FIXME: There is an implicit assumption here
3880 that the name of sym is different from
3881 paux->symbol. */
3882 if (! bfd_is_local_label (abfd, sym))
015dc7e1 3883 do_print = false;
baae986a
NC
3884 break;
3885 }
3886 }
3887 else
d3def5d7 3888 {
baae986a
NC
3889 const char * name = bfd_asymbol_name (sym);
3890 char * alloc = NULL;
3891
3892 if (do_demangle && name[0] != '\0')
3893 {
3894 /* Demangle the name. */
3895 alloc = bfd_demangle (abfd, name, demangle_flags);
3896 if (alloc != NULL)
3897 name = alloc;
3898 }
3899
3900 /* We are not currently printing. Check to see
3901 if the current symbol matches the requested symbol. */
3902 if (streq (name, paux->symbol))
3903 {
015dc7e1 3904 do_print = true;
baae986a 3905
7e538762
VK
3906 /* Skip over the relocs belonging to addresses below the
3907 symbol address. */
3908 const bfd_vma sym_offset = bfd_asymbol_value (sym) - section->vma;
3909 while (rel_pp < rel_ppend &&
3910 (*rel_pp)->address - rel_offset < sym_offset)
3911 ++rel_pp;
3912
baae986a
NC
3913 if (sym->flags & BSF_FUNCTION)
3914 {
3915 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
3916 && ((elf_symbol_type *) sym)->internal_elf_sym.st_size > 0)
3917 {
3918 /* Sym is a function symbol with a size associated
3919 with it. Turn on automatic disassembly for the
3920 next VALUE bytes. */
3921 stop_offset = addr_offset
3922 + ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
3923 loop_until = stop_offset_reached;
3924 }
3925 else
3926 {
3927 /* Otherwise we need to tell the loop heuristic to
3928 loop until the next function symbol is encountered. */
3929 loop_until = function_sym;
3930 }
3931 }
3932 else
3933 {
3934 /* Otherwise loop until the next symbol is encountered. */
3935 loop_until = next_sym;
3936 }
3937 }
3938
3939 free (alloc);
d3def5d7 3940 }
d3def5d7
MY
3941 }
3942
3943 if (! prefix_addresses && do_print)
155e0d23
NC
3944 {
3945 pinfo->fprintf_func (pinfo->stream, "\n");
22a398e1 3946 objdump_print_addr_with_sym (abfd, section, sym, addr,
015dc7e1 3947 pinfo, false);
155e0d23 3948 pinfo->fprintf_func (pinfo->stream, ":\n");
c7ce51d8
NC
3949
3950 if (sym != NULL && show_all_symbols)
3951 {
3952 for (++place; place < sorted_symcount; place++)
3953 {
3954 sym = sorted_syms[place];
3955
3956 if (bfd_asymbol_value (sym) != addr)
3957 break;
3958 if (! pinfo->symbol_is_valid (sym, pinfo))
3959 continue;
3960 if (strcmp (bfd_section_name (sym->section), bfd_section_name (section)) != 0)
3961 break;
3962
3963 objdump_print_addr_with_sym (abfd, section, sym, addr, pinfo, false);
3964 pinfo->fprintf_func (pinfo->stream, ":\n");
3965 }
3966 }
155e0d23
NC
3967 }
3968
22a398e1 3969 if (sym != NULL && bfd_asymbol_value (sym) > addr)
155e0d23
NC
3970 nextsym = sym;
3971 else if (sym == NULL)
3972 nextsym = NULL;
3973 else
3974 {
22a398e1 3975#define is_valid_next_sym(SYM) \
fd361982 3976 (strcmp (bfd_section_name ((SYM)->section), bfd_section_name (section)) == 0 \
22a398e1
NC
3977 && (bfd_asymbol_value (SYM) > bfd_asymbol_value (sym)) \
3978 && pinfo->symbol_is_valid (SYM, pinfo))
3aade688 3979
155e0d23
NC
3980 /* Search forward for the next appropriate symbol in
3981 SECTION. Note that all the symbols are sorted
3982 together into one big array, and that some sections
3983 may have overlapping addresses. */
3984 while (place < sorted_symcount
22a398e1 3985 && ! is_valid_next_sym (sorted_syms [place]))
155e0d23 3986 ++place;
22a398e1 3987
155e0d23
NC
3988 if (place >= sorted_symcount)
3989 nextsym = NULL;
3990 else
3991 nextsym = sorted_syms[place];
3992 }
3993
22a398e1
NC
3994 if (sym != NULL && bfd_asymbol_value (sym) > addr)
3995 nextstop_offset = bfd_asymbol_value (sym) - section->vma;
155e0d23
NC
3996 else if (nextsym == NULL)
3997 nextstop_offset = stop_offset;
3998 else
22a398e1
NC
3999 nextstop_offset = bfd_asymbol_value (nextsym) - section->vma;
4000
84d7b001
NC
4001 if (nextstop_offset > stop_offset
4002 || nextstop_offset <= addr_offset)
22a398e1 4003 nextstop_offset = stop_offset;
155e0d23
NC
4004
4005 /* If a symbol is explicitly marked as being an object
4006 rather than a function, just dump the bytes without
4007 disassembling them. */
4008 if (disassemble_all
4009 || sym == NULL
abf71725 4010 || sym->section != section
22a398e1 4011 || bfd_asymbol_value (sym) > addr
155e0d23
NC
4012 || ((sym->flags & BSF_OBJECT) == 0
4013 && (strstr (bfd_asymbol_name (sym), "gnu_compiled")
4014 == NULL)
4015 && (strstr (bfd_asymbol_name (sym), "gcc2_compiled")
4016 == NULL))
4017 || (sym->flags & BSF_FUNCTION) != 0)
015dc7e1 4018 insns = true;
155e0d23 4019 else
015dc7e1 4020 insns = false;
155e0d23 4021
d3def5d7 4022 if (do_print)
1d67fe3b
TT
4023 {
4024 /* Resolve symbol name. */
4025 if (visualize_jumps && abfd && sym && sym->name)
4026 {
4027 struct disassemble_info di;
4028 SFILE sf;
4029
4030 sf.alloc = strlen (sym->name) + 40;
4031 sf.buffer = (char*) xmalloc (sf.alloc);
4032 sf.pos = 0;
60a3da00
AB
4033 disassemble_set_printf
4034 (&di, &sf, (fprintf_ftype) objdump_sprintf,
4035 (fprintf_styled_ftype) objdump_styled_sprintf);
1d67fe3b
TT
4036
4037 objdump_print_symname (abfd, &di, sym);
4038
4039 /* Fetch jump information. */
4040 detected_jumps = disassemble_jumps
4041 (pinfo, paux->disassemble_fn,
4042 addr_offset, nextstop_offset,
4043 rel_offset, &rel_pp, rel_ppend);
4044
4045 /* Free symbol name. */
4046 free (sf.buffer);
4047 }
4048
4049 /* Add jumps to output. */
4050 disassemble_bytes (pinfo, paux->disassemble_fn, insns, data,
4051 addr_offset, nextstop_offset,
4052 rel_offset, &rel_pp, rel_ppend);
4053
4054 /* Free jumps. */
4055 while (detected_jumps)
4056 {
4057 detected_jumps = jump_info_free (detected_jumps);
4058 }
4059 }
3aade688 4060
155e0d23
NC
4061 addr_offset = nextstop_offset;
4062 sym = nextsym;
4063 }
4064
4065 free (data);
4066
4067 if (rel_ppstart != NULL)
4068 free (rel_ppstart);
4069}
4070
252b5132
RH
4071/* Disassemble the contents of an object file. */
4072
4073static void
46dca2e0 4074disassemble_data (bfd *abfd)
252b5132 4075{
252b5132
RH
4076 struct disassemble_info disasm_info;
4077 struct objdump_disasm_info aux;
4c45e5c9 4078 long i;
252b5132
RH
4079
4080 print_files = NULL;
4081 prev_functionname = NULL;
4082 prev_line = -1;
9b8d1a36 4083 prev_discriminator = 0;
252b5132
RH
4084
4085 /* We make a copy of syms to sort. We don't want to sort syms
4086 because that will screw up the relocs. */
4c45e5c9 4087 sorted_symcount = symcount ? symcount : dynsymcount;
3f5e193b 4088 sorted_syms = (asymbol **) xmalloc ((sorted_symcount + synthcount)
9b49454b 4089 * sizeof (asymbol *));
41da0822
AM
4090 if (sorted_symcount != 0)
4091 {
4092 memcpy (sorted_syms, symcount ? syms : dynsyms,
4093 sorted_symcount * sizeof (asymbol *));
252b5132 4094
41da0822
AM
4095 sorted_symcount = remove_useless_symbols (sorted_syms, sorted_symcount);
4096 }
4c45e5c9
JJ
4097
4098 for (i = 0; i < synthcount; ++i)
4099 {
4100 sorted_syms[sorted_symcount] = synthsyms + i;
4101 ++sorted_symcount;
4102 }
252b5132 4103
60a3da00
AB
4104 init_disassemble_info (&disasm_info, stdout, (fprintf_ftype) fprintf,
4105 (fprintf_styled_ftype) fprintf_styled);
46dca2e0 4106 disasm_info.application_data = (void *) &aux;
252b5132 4107 aux.abfd = abfd;
015dc7e1 4108 aux.require_sec = false;
c3f72de4
AM
4109 disasm_info.dynrelbuf = NULL;
4110 disasm_info.dynrelcount = 0;
ce04548a 4111 aux.reloc = NULL;
d3def5d7 4112 aux.symbol = disasm_sym;
155e0d23 4113
252b5132
RH
4114 disasm_info.print_address_func = objdump_print_address;
4115 disasm_info.symbol_at_address_func = objdump_symbol_at_address;
4116
d3ba0551 4117 if (machine != NULL)
252b5132 4118 {
91d6fa6a 4119 const bfd_arch_info_type *inf = bfd_scan_arch (machine);
98a91d6a 4120
91d6fa6a 4121 if (inf == NULL)
ffdfc835
AM
4122 {
4123 non_fatal (_("can't use supplied machine %s"), machine);
4124 exit_status = 1;
4125 }
4126 else
4127 abfd->arch_info = inf;
252b5132
RH
4128 }
4129
4130 if (endian != BFD_ENDIAN_UNKNOWN)
4131 {
4132 struct bfd_target *xvec;
4133
3f5e193b 4134 xvec = (struct bfd_target *) xmalloc (sizeof (struct bfd_target));
252b5132
RH
4135 memcpy (xvec, abfd->xvec, sizeof (struct bfd_target));
4136 xvec->byteorder = endian;
4137 abfd->xvec = xvec;
4138 }
4139
155e0d23 4140 /* Use libopcodes to locate a suitable disassembler. */
003ca0fd
YQ
4141 aux.disassemble_fn = disassembler (bfd_get_arch (abfd),
4142 bfd_big_endian (abfd),
4143 bfd_get_mach (abfd), abfd);
155e0d23 4144 if (!aux.disassemble_fn)
252b5132 4145 {
a8c62f1c 4146 non_fatal (_("can't disassemble for architecture %s\n"),
37cc8ec1 4147 bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
75cd796a 4148 exit_status = 1;
252b5132
RH
4149 return;
4150 }
4151
4152 disasm_info.flavour = bfd_get_flavour (abfd);
4153 disasm_info.arch = bfd_get_arch (abfd);
4154 disasm_info.mach = bfd_get_mach (abfd);
dd92f639 4155 disasm_info.disassembler_options = disassembler_options;
61826503 4156 disasm_info.octets_per_byte = bfd_octets_per_byte (abfd, NULL);
0bcb06d2
AS
4157 disasm_info.skip_zeroes = DEFAULT_SKIP_ZEROES;
4158 disasm_info.skip_zeroes_at_end = DEFAULT_SKIP_ZEROES_AT_END;
015dc7e1 4159 disasm_info.disassembler_needs_relocs = false;
0af11b59 4160
252b5132 4161 if (bfd_big_endian (abfd))
a8a9050d 4162 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_BIG;
252b5132 4163 else if (bfd_little_endian (abfd))
a8a9050d 4164 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_LITTLE;
252b5132
RH
4165 else
4166 /* ??? Aborting here seems too drastic. We could default to big or little
4167 instead. */
4168 disasm_info.endian = BFD_ENDIAN_UNKNOWN;
4169
b3db6d07
JM
4170 disasm_info.endian_code = disasm_info.endian;
4171
22a398e1
NC
4172 /* Allow the target to customize the info structure. */
4173 disassemble_init_for_target (& disasm_info);
4174
a24bb4f0 4175 /* Pre-load the dynamic relocs as we may need them during the disassembly. */
c3f72de4 4176 long relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
3aade688 4177
c3f72de4
AM
4178 if (relsize > 0)
4179 {
4180 disasm_info.dynrelbuf = (arelent **) xmalloc (relsize);
4181 disasm_info.dynrelcount
4182 = bfd_canonicalize_dynamic_reloc (abfd, disasm_info.dynrelbuf, dynsyms);
4183 if (disasm_info.dynrelcount < 0)
ffdfc835
AM
4184 {
4185 my_bfd_nonfatal (bfd_get_filename (abfd));
4186 free (disasm_info.dynrelbuf);
4187 disasm_info.dynrelbuf = NULL;
4188 disasm_info.dynrelcount = 0;
4189 }
4190 else if (disasm_info.dynrelcount > 1)
4191 /* Sort the relocs by address. */
4192 qsort (disasm_info.dynrelbuf, disasm_info.dynrelcount,
4193 sizeof (arelent *), compare_relocs);
fd7bb956 4194 }
c3f72de4 4195
2087ad84
PB
4196 disasm_info.symtab = sorted_syms;
4197 disasm_info.symtab_size = sorted_symcount;
fd7bb956 4198
155e0d23 4199 bfd_map_over_sections (abfd, disassemble_section, & disasm_info);
98a91d6a 4200
c3f72de4
AM
4201 free (disasm_info.dynrelbuf);
4202 disasm_info.dynrelbuf = NULL;
252b5132 4203 free (sorted_syms);
20135676 4204 disassemble_free_target (&disasm_info);
252b5132
RH
4205}
4206\f
015dc7e1 4207static bool
7bcbeb0f
CC
4208load_specific_debug_section (enum dwarf_section_display_enum debug,
4209 asection *sec, void *file)
365544c3
L
4210{
4211 struct dwarf_section *section = &debug_displays [debug].section;
3f5e193b 4212 bfd *abfd = (bfd *) file;
bfec0f11 4213 bfd_byte *contents;
f2023ce7 4214 bfd_size_type amt;
63455780 4215 size_t alloced;
015dc7e1 4216 bool ret;
365544c3 4217
365544c3 4218 if (section->start != NULL)
dda8d76d
NC
4219 {
4220 /* If it is already loaded, do nothing. */
4221 if (streq (section->filename, bfd_get_filename (abfd)))
015dc7e1 4222 return true;
dda8d76d
NC
4223 free (section->start);
4224 }
365544c3 4225
dda8d76d 4226 section->filename = bfd_get_filename (abfd);
d1c4b12b 4227 section->reloc_info = NULL;
3aade688 4228 section->num_relocs = 0;
fd361982 4229 section->address = bfd_section_vma (sec);
fd361982 4230 section->size = bfd_section_size (sec);
63455780
NC
4231 /* PR 24360: On 32-bit hosts sizeof (size_t) < sizeof (bfd_size_type). */
4232 alloced = amt = section->size + 1;
75393a2d
NC
4233 if (alloced != amt
4234 || alloced == 0
4235 || (bfd_get_size (abfd) != 0 && alloced >= bfd_get_size (abfd)))
11fa9f13 4236 {
787bbc56 4237 section->start = NULL;
11fa9f13 4238 free_debug_section (debug);
bde9f9d7 4239 printf (_("\nSection '%s' has an invalid size: %#" PRIx64 ".\n"),
12add40e 4240 sanitize_string (section->name),
bde9f9d7 4241 section->size);
015dc7e1 4242 return false;
11fa9f13 4243 }
208599d9
AM
4244
4245 section->start = contents = xmalloc (alloced);
4f1881b9
AM
4246 /* Ensure any string section has a terminating NUL. */
4247 section->start[section->size] = 0;
1b315056 4248
2e8136f9
NC
4249 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4250 && debug_displays [debug].relocate)
0acf065b 4251 {
0acf065b
CC
4252 ret = bfd_simple_get_relocated_section_contents (abfd,
4253 sec,
4254 section->start,
4255 syms) != NULL;
208599d9 4256 if (ret)
d1c4b12b 4257 {
208599d9 4258 long reloc_size = bfd_get_reloc_upper_bound (abfd, sec);
d1c4b12b 4259
208599d9 4260 if (reloc_size > 0)
d1c4b12b 4261 {
4d664d57 4262 long reloc_count;
208599d9
AM
4263 arelent **relocs;
4264
4265 relocs = (arelent **) xmalloc (reloc_size);
4266
e6b6fad2 4267 reloc_count = bfd_canonicalize_reloc (abfd, sec, relocs, syms);
4d664d57 4268 if (reloc_count <= 0)
208599d9
AM
4269 free (relocs);
4270 else
4271 {
4272 section->reloc_info = relocs;
4273 section->num_relocs = reloc_count;
4274 }
d1c4b12b
NC
4275 }
4276 }
bdc4de1b 4277 }
208599d9
AM
4278 else
4279 ret = bfd_get_full_section_contents (abfd, sec, &contents);
4280
4281 if (!ret)
4282 {
4283 free_debug_section (debug);
4284 printf (_("\nCan't get contents for section '%s'.\n"),
4285 sanitize_string (section->name));
015dc7e1 4286 return false;
208599d9 4287 }
0acf065b 4288
015dc7e1 4289 return true;
7bcbeb0f
CC
4290}
4291
015dc7e1 4292bool
31e5a3a3 4293reloc_at (struct dwarf_section * dsec, uint64_t offset)
d1c4b12b
NC
4294{
4295 arelent ** relocs;
4296 arelent * rp;
4297
4298 if (dsec == NULL || dsec->reloc_info == NULL)
015dc7e1 4299 return false;
d1c4b12b
NC
4300
4301 relocs = (arelent **) dsec->reloc_info;
4302
4303 for (; (rp = * relocs) != NULL; ++ relocs)
4304 if (rp->address == offset)
015dc7e1 4305 return true;
d1c4b12b 4306
015dc7e1 4307 return false;
d1c4b12b
NC
4308}
4309
015dc7e1 4310bool
7bcbeb0f
CC
4311load_debug_section (enum dwarf_section_display_enum debug, void *file)
4312{
4313 struct dwarf_section *section = &debug_displays [debug].section;
3f5e193b 4314 bfd *abfd = (bfd *) file;
7bcbeb0f 4315 asection *sec;
55e3926e 4316 const char *name;
7bcbeb0f 4317
e1dbfc17
L
4318 if (!dump_any_debugging)
4319 return false;
4320
7bcbeb0f
CC
4321 /* If it is already loaded, do nothing. */
4322 if (section->start != NULL)
dda8d76d
NC
4323 {
4324 if (streq (section->filename, bfd_get_filename (abfd)))
015dc7e1 4325 return true;
dda8d76d 4326 }
7bcbeb0f 4327 /* Locate the debug section. */
55e3926e
AM
4328 name = section->uncompressed_name;
4329 sec = bfd_get_section_by_name (abfd, name);
4330 if (sec == NULL)
7bcbeb0f 4331 {
55e3926e
AM
4332 name = section->compressed_name;
4333 if (*name)
4334 sec = bfd_get_section_by_name (abfd, name);
4335 }
4336 if (sec == NULL)
4337 {
4338 name = section->xcoff_name;
4339 if (*name)
4340 sec = bfd_get_section_by_name (abfd, name);
7bcbeb0f
CC
4341 }
4342 if (sec == NULL)
015dc7e1 4343 return false;
7bcbeb0f 4344
55e3926e 4345 section->name = name;
7bcbeb0f 4346 return load_specific_debug_section (debug, sec, file);
365544c3
L
4347}
4348
4349void
4350free_debug_section (enum dwarf_section_display_enum debug)
4351{
4352 struct dwarf_section *section = &debug_displays [debug].section;
4353
365544c3
L
4354 free ((char *) section->start);
4355 section->start = NULL;
4356 section->address = 0;
4357 section->size = 0;
450da4bd
AM
4358 free ((char*) section->reloc_info);
4359 section->reloc_info = NULL;
4360 section->num_relocs= 0;
365544c3
L
4361}
4362
dda8d76d
NC
4363void
4364close_debug_file (void * file)
4365{
4366 bfd * abfd = (bfd *) file;
4367
4368 bfd_close (abfd);
4369}
4370
4371void *
4372open_debug_file (const char * pathname)
4373{
4374 bfd * data;
4375
4376 data = bfd_openr (pathname, NULL);
4377 if (data == NULL)
4378 return NULL;
4379
4380 if (! bfd_check_format (data, bfd_object))
4381 return NULL;
9b49454b 4382
dda8d76d
NC
4383 return data;
4384}
4385
365544c3
L
4386static void
4387dump_dwarf_section (bfd *abfd, asection *section,
e2c0149e 4388 void *arg)
365544c3 4389{
fd361982 4390 const char *name = bfd_section_name (section);
365544c3 4391 const char *match;
3f5e193b 4392 int i;
e2c0149e 4393 bool is_mainfile = *(bool *) arg;
365544c3 4394
55e3926e
AM
4395 if (*name == 0)
4396 return;
4397
e2c0149e
AM
4398 if (!is_mainfile && !process_links
4399 && (section->flags & SEC_DEBUGGING) == 0)
4400 return;
4401
08dedd66 4402 if (startswith (name, ".gnu.linkonce.wi."))
365544c3
L
4403 match = ".debug_info";
4404 else
4405 match = name;
4406
4407 for (i = 0; i < max; i++)
4cb93e3b 4408 if ((strcmp (debug_displays [i].section.uncompressed_name, match) == 0
51d29b8c
CC
4409 || strcmp (debug_displays [i].section.compressed_name, match) == 0
4410 || strcmp (debug_displays [i].section.xcoff_name, match) == 0)
4cb93e3b
TG
4411 && debug_displays [i].enabled != NULL
4412 && *debug_displays [i].enabled)
365544c3 4413 {
c8450da8 4414 struct dwarf_section *sec = &debug_displays [i].section;
365544c3 4415
c8450da8
TG
4416 if (strcmp (sec->uncompressed_name, match) == 0)
4417 sec->name = sec->uncompressed_name;
51d29b8c 4418 else if (strcmp (sec->compressed_name, match) == 0)
c8450da8 4419 sec->name = sec->compressed_name;
51d29b8c
CC
4420 else
4421 sec->name = sec->xcoff_name;
3f5e193b 4422 if (load_specific_debug_section ((enum dwarf_section_display_enum) i,
9b49454b 4423 section, abfd))
c8450da8
TG
4424 {
4425 debug_displays [i].display (sec, abfd);
3aade688 4426
c8450da8 4427 if (i != info && i != abbrev)
3f5e193b 4428 free_debug_section ((enum dwarf_section_display_enum) i);
365544c3
L
4429 }
4430 break;
4431 }
4432}
4433
4434/* Dump the dwarf debugging information. */
4435
4436static void
e2c0149e 4437dump_dwarf (bfd *abfd, bool is_mainfile)
365544c3 4438{
39f0547e
NC
4439 /* The byte_get pointer should have been set at the start of dump_bfd(). */
4440 if (byte_get == NULL)
f41e4712
NC
4441 {
4442 warn (_("File %s does not contain any dwarf debug information\n"),
4443 bfd_get_filename (abfd));
4444 return;
4445 }
365544c3 4446
b129eb0e 4447 switch (bfd_get_arch (abfd))
2dc4cec1 4448 {
8ab159a9
AM
4449 case bfd_arch_s12z:
4450 /* S12Z has a 24 bit address space. But the only known
4451 producer of dwarf_info encodes addresses into 32 bits. */
4452 eh_addr_size = 4;
4453 break;
4454
b129eb0e 4455 default:
229a22cf 4456 eh_addr_size = bfd_arch_bits_per_address (abfd) / 8;
b129eb0e 4457 break;
2dc4cec1
L
4458 }
4459
229a22cf
AB
4460 init_dwarf_regnames_by_bfd_arch_and_mach (bfd_get_arch (abfd),
4461 bfd_get_mach (abfd));
4462
e2c0149e 4463 bfd_map_over_sections (abfd, dump_dwarf_section, (void *) &is_mainfile);
365544c3
L
4464}
4465\f
f9c36cc9
AM
4466/* Read ABFD's section SECT_NAME into *CONTENTS, and return a pointer to
4467 the section. Return NULL on failure. */
252b5132 4468
f9c36cc9
AM
4469static asection *
4470read_section (bfd *abfd, const char *sect_name, bfd_byte **contents)
252b5132 4471{
f9c36cc9 4472 asection *sec;
252b5132 4473
f9c36cc9
AM
4474 *contents = NULL;
4475 sec = bfd_get_section_by_name (abfd, sect_name);
4476 if (sec == NULL)
252b5132 4477 {
f9c36cc9
AM
4478 printf (_("No %s section present\n\n"), sanitize_string (sect_name));
4479 return NULL;
252b5132
RH
4480 }
4481
093b5677
AM
4482 if ((bfd_section_flags (sec) & SEC_HAS_CONTENTS) == 0)
4483 bfd_set_error (bfd_error_no_contents);
4484 else if (bfd_malloc_and_get_section (abfd, sec, contents))
4485 return sec;
4486
4487 non_fatal (_("reading %s section of %s failed: %s"),
4488 sect_name, bfd_get_filename (abfd),
4489 bfd_errmsg (bfd_get_error ()));
4490 exit_status = 1;
4491 return NULL;
252b5132
RH
4492}
4493
4494/* Stabs entries use a 12 byte format:
4495 4 byte string table index
4496 1 byte stab type
4497 1 byte stab other field
4498 2 byte stab desc field
4499 4 byte stab value
4500 FIXME: This will have to change for a 64 bit object format. */
4501
46dca2e0
NC
4502#define STRDXOFF (0)
4503#define TYPEOFF (4)
4504#define OTHEROFF (5)
4505#define DESCOFF (6)
4506#define VALOFF (8)
252b5132
RH
4507#define STABSIZE (12)
4508
4509/* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
4510 using string table section STRSECT_NAME (in `strtab'). */
4511
4512static void
3b9ad1cc
AM
4513print_section_stabs (bfd *abfd,
4514 const char *stabsect_name,
4515 unsigned *string_offset_ptr)
252b5132
RH
4516{
4517 int i;
46dca2e0 4518 unsigned file_string_table_offset = 0;
29ca8dc5 4519 unsigned next_file_string_table_offset = *string_offset_ptr;
252b5132
RH
4520 bfd_byte *stabp, *stabs_end;
4521
4522 stabp = stabs;
c65f94a0 4523 stabs_end = PTR_ADD (stabp, stab_size);
252b5132 4524
12add40e 4525 printf (_("Contents of %s section:\n\n"), sanitize_string (stabsect_name));
252b5132
RH
4526 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
4527
4528 /* Loop through all symbols and print them.
4529
4530 We start the index at -1 because there is a dummy symbol on
4531 the front of stabs-in-{coff,elf} sections that supplies sizes. */
45b36294 4532 for (i = -1; (size_t) (stabs_end - stabp) >= STABSIZE; stabp += STABSIZE, i++)
252b5132
RH
4533 {
4534 const char *name;
4535 unsigned long strx;
4536 unsigned char type, other;
4537 unsigned short desc;
4538 bfd_vma value;
4539
4540 strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
4541 type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
4542 other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
4543 desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
4544 value = bfd_h_get_32 (abfd, stabp + VALOFF);
4545
4546 printf ("\n%-6d ", i);
4547 /* Either print the stab name, or, if unnamed, print its number
0af11b59 4548 again (makes consistent formatting for tools like awk). */
252b5132
RH
4549 name = bfd_get_stab_name (type);
4550 if (name != NULL)
12add40e 4551 printf ("%-6s", sanitize_string (name));
252b5132
RH
4552 else if (type == N_UNDF)
4553 printf ("HdrSym");
4554 else
4555 printf ("%-6d", type);
4556 printf (" %-6d %-6d ", other, desc);
d8180c76 4557 bfd_printf_vma (abfd, value);
252b5132
RH
4558 printf (" %-6lu", strx);
4559
4560 /* Symbols with type == 0 (N_UNDF) specify the length of the
4561 string table associated with this file. We use that info
4562 to know how to relocate the *next* file's string table indices. */
252b5132
RH
4563 if (type == N_UNDF)
4564 {
4565 file_string_table_offset = next_file_string_table_offset;
4566 next_file_string_table_offset += value;
4567 }
4568 else
4569 {
f41e4712
NC
4570 bfd_size_type amt = strx + file_string_table_offset;
4571
252b5132
RH
4572 /* Using the (possibly updated) string table offset, print the
4573 string (if any) associated with this symbol. */
f41e4712 4574 if (amt < stabstr_size)
12add40e
NC
4575 /* PR 17512: file: 079-79389-0.001:0.1.
4576 FIXME: May need to sanitize this string before displaying. */
f41e4712 4577 printf (" %.*s", (int)(stabstr_size - amt), strtab + amt);
252b5132
RH
4578 else
4579 printf (" *");
4580 }
4581 }
4582 printf ("\n\n");
29ca8dc5 4583 *string_offset_ptr = next_file_string_table_offset;
252b5132
RH
4584}
4585
155e0d23
NC
4586typedef struct
4587{
4588 const char * section_name;
4589 const char * string_section_name;
29ca8dc5 4590 unsigned string_offset;
155e0d23
NC
4591}
4592stab_section_names;
4593
252b5132 4594static void
155e0d23 4595find_stabs_section (bfd *abfd, asection *section, void *names)
252b5132 4596{
155e0d23
NC
4597 int len;
4598 stab_section_names * sought = (stab_section_names *) names;
252b5132
RH
4599
4600 /* Check for section names for which stabsect_name is a prefix, to
29ca8dc5 4601 handle .stab.N, etc. */
155e0d23
NC
4602 len = strlen (sought->section_name);
4603
4604 /* If the prefix matches, and the files section name ends with a
4605 nul or a digit, then we match. I.e., we want either an exact
4606 match or a section followed by a number. */
4607 if (strncmp (sought->section_name, section->name, len) == 0
4608 && (section->name[len] == 0
29ca8dc5 4609 || (section->name[len] == '.' && ISDIGIT (section->name[len + 1]))))
252b5132 4610 {
f9c36cc9 4611 asection *s;
29ca8dc5 4612 if (strtab == NULL)
f9c36cc9
AM
4613 {
4614 s = read_section (abfd, sought->string_section_name, &strtab);
4615 if (s != NULL)
4616 stabstr_size = bfd_section_size (s);
4617 }
3aade688 4618
29ca8dc5 4619 if (strtab)
252b5132 4620 {
f9c36cc9
AM
4621 s = read_section (abfd, section->name, &stabs);
4622 if (s != NULL)
11066c2a 4623 {
f9c36cc9 4624 stab_size = bfd_section_size (s);
11066c2a
AM
4625 print_section_stabs (abfd, section->name, &sought->string_offset);
4626 free (stabs);
4627 }
252b5132
RH
4628 }
4629 }
4630}
98a91d6a 4631
155e0d23
NC
4632static void
4633dump_stabs_section (bfd *abfd, char *stabsect_name, char *strsect_name)
4634{
4635 stab_section_names s;
4636
4637 s.section_name = stabsect_name;
4638 s.string_section_name = strsect_name;
29ca8dc5
NS
4639 s.string_offset = 0;
4640
155e0d23 4641 bfd_map_over_sections (abfd, find_stabs_section, & s);
29ca8dc5
NS
4642
4643 free (strtab);
4644 strtab = NULL;
155e0d23
NC
4645}
4646
4647/* Dump the any sections containing stabs debugging information. */
4648
4649static void
4650dump_stabs (bfd *abfd)
4651{
4652 dump_stabs_section (abfd, ".stab", ".stabstr");
4653 dump_stabs_section (abfd, ".stab.excl", ".stab.exclstr");
4654 dump_stabs_section (abfd, ".stab.index", ".stab.indexstr");
62bb81b8
TG
4655
4656 /* For Darwin. */
4657 dump_stabs_section (abfd, "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr");
4658
155e0d23
NC
4659 dump_stabs_section (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
4660}
252b5132
RH
4661\f
4662static void
46dca2e0 4663dump_bfd_header (bfd *abfd)
252b5132
RH
4664{
4665 char *comma = "";
4666
4667 printf (_("architecture: %s, "),
4668 bfd_printable_arch_mach (bfd_get_arch (abfd),
4669 bfd_get_mach (abfd)));
6b6bc957 4670 printf (_("flags 0x%08x:\n"), abfd->flags & ~BFD_FLAGS_FOR_BFD_USE_MASK);
252b5132 4671
82a9ed20 4672#define PF(x, y) if (abfd->flags & x) {printf ("%s%s", comma, y); comma=", ";}
252b5132
RH
4673 PF (HAS_RELOC, "HAS_RELOC");
4674 PF (EXEC_P, "EXEC_P");
4675 PF (HAS_LINENO, "HAS_LINENO");
4676 PF (HAS_DEBUG, "HAS_DEBUG");
4677 PF (HAS_SYMS, "HAS_SYMS");
4678 PF (HAS_LOCALS, "HAS_LOCALS");
4679 PF (DYNAMIC, "DYNAMIC");
4680 PF (WP_TEXT, "WP_TEXT");
4681 PF (D_PAGED, "D_PAGED");
4682 PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
4683 printf (_("\nstart address 0x"));
d8180c76 4684 bfd_printf_vma (abfd, abfd->start_address);
252b5132
RH
4685 printf ("\n");
4686}
7d9813f1
NA
4687\f
4688
094e34f2 4689#ifdef ENABLE_LIBCTF
7d9813f1
NA
4690/* Formatting callback function passed to ctf_dump. Returns either the pointer
4691 it is passed, or a pointer to newly-allocated storage, in which case
4692 dump_ctf() will free it when it no longer needs it. */
4693
4694static char *
4695dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
4696 char *s, void *arg)
4697{
63160fc9 4698 const char *blanks = arg;
7d9813f1
NA
4699 char *new_s;
4700
63160fc9 4701 if (asprintf (&new_s, "%s%s", blanks, s) < 0)
7d9813f1
NA
4702 return s;
4703 return new_s;
4704}
4705
4706/* Make a ctfsect suitable for ctf_bfdopen_ctfsect(). */
4707static ctf_sect_t
4708make_ctfsect (const char *name, bfd_byte *data,
4709 bfd_size_type size)
4710{
4711 ctf_sect_t ctfsect;
4712
4713 ctfsect.cts_name = name;
7d9813f1 4714 ctfsect.cts_entsize = 1;
7d9813f1
NA
4715 ctfsect.cts_size = size;
4716 ctfsect.cts_data = data;
4717
4718 return ctfsect;
4719}
4720
926c9e76
NA
4721/* Dump CTF errors/warnings. */
4722static void
139633c3 4723dump_ctf_errs (ctf_dict_t *fp)
926c9e76
NA
4724{
4725 ctf_next_t *it = NULL;
4726 char *errtext;
4727 int is_warning;
4728 int err;
4729
4730 /* Dump accumulated errors and warnings. */
4731 while ((errtext = ctf_errwarning_next (fp, &it, &is_warning, &err)) != NULL)
4732 {
5e9b84f7 4733 non_fatal (_("%s: %s"), is_warning ? _("warning"): _("error"),
926c9e76
NA
4734 errtext);
4735 free (errtext);
4736 }
4737 if (err != ECTF_NEXT_END)
4738 {
4739 non_fatal (_("CTF error: cannot get CTF errors: `%s'"),
4740 ctf_errmsg (err));
4741 }
4742}
4743
7d9813f1
NA
4744/* Dump one CTF archive member. */
4745
80b56fad
NA
4746static void
4747dump_ctf_archive_member (ctf_dict_t *ctf, const char *name, ctf_dict_t *parent,
4748 size_t member)
7d9813f1 4749{
9b32cba4
NA
4750 const char *things[] = {"Header", "Labels", "Data objects",
4751 "Function objects", "Variables", "Types", "Strings",
4752 ""};
7d9813f1
NA
4753 const char **thing;
4754 size_t i;
4755
80b56fad
NA
4756 /* Don't print out the name of the default-named archive member if it appears
4757 first in the list. The name .ctf appears everywhere, even for things that
4758 aren't really archives, so printing it out is liable to be confusing; also,
4759 the common case by far is for only one archive member to exist, and hiding
4760 it in that case seems worthwhile. */
fd86991b 4761
80b56fad
NA
4762 if (strcmp (name, ".ctf") != 0 || member != 0)
4763 printf (_("\nCTF archive member: %s:\n"), sanitize_string (name));
fd86991b 4764
80b56fad
NA
4765 if (ctf_parent_name (ctf) != NULL)
4766 ctf_import (ctf, parent);
7d9813f1 4767
9b32cba4 4768 for (i = 0, thing = things; *thing[0]; thing++, i++)
7d9813f1
NA
4769 {
4770 ctf_dump_state_t *s = NULL;
4771 char *item;
4772
4773 printf ("\n %s:\n", *thing);
4774 while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
4775 (void *) " ")) != NULL)
4776 {
4777 printf ("%s\n", item);
4778 free (item);
4779 }
4780
4781 if (ctf_errno (ctf))
4782 {
3dd6b890 4783 non_fatal (_("Iteration failed: %s, %s"), *thing,
7d9813f1
NA
4784 ctf_errmsg (ctf_errno (ctf)));
4785 break;
4786 }
4787 }
8b37e7b6 4788
926c9e76 4789 dump_ctf_errs (ctf);
7d9813f1
NA
4790}
4791
4792/* Dump the CTF debugging information. */
4793
4794static void
4795dump_ctf (bfd *abfd, const char *sect_name, const char *parent_name)
4796{
f9c36cc9 4797 asection *sec;
80b56fad 4798 ctf_archive_t *ctfa = NULL;
f9c36cc9 4799 bfd_byte *ctfdata;
7d9813f1 4800 ctf_sect_t ctfsect;
80b56fad
NA
4801 ctf_dict_t *parent;
4802 ctf_dict_t *fp;
4803 ctf_next_t *i = NULL;
4804 const char *name;
4805 size_t member = 0;
7d9813f1
NA
4806 int err;
4807
10909ea8
NA
4808 if (sect_name == NULL)
4809 sect_name = ".ctf";
7d9813f1 4810
f9c36cc9
AM
4811 sec = read_section (abfd, sect_name, &ctfdata);
4812 if (sec == NULL)
ffdfc835
AM
4813 {
4814 my_bfd_nonfatal (bfd_get_filename (abfd));
4815 return;
4816 }
7d9813f1 4817
80b56fad
NA
4818 /* Load the CTF file and dump it. Preload the parent dict, since it will
4819 need to be imported into every child in turn. */
7d9813f1 4820
f9c36cc9 4821 ctfsect = make_ctfsect (sect_name, ctfdata, bfd_section_size (sec));
7d9813f1
NA
4822 if ((ctfa = ctf_bfdopen_ctfsect (abfd, &ctfsect, &err)) == NULL)
4823 {
926c9e76 4824 dump_ctf_errs (NULL);
3dd6b890 4825 non_fatal (_("CTF open failure: %s"), ctf_errmsg (err));
ffdfc835
AM
4826 my_bfd_nonfatal (bfd_get_filename (abfd));
4827 free (ctfdata);
4828 return;
7d9813f1
NA
4829 }
4830
80b56fad 4831 if ((parent = ctf_dict_open (ctfa, parent_name, &err)) == NULL)
fd86991b 4832 {
926c9e76 4833 dump_ctf_errs (NULL);
3dd6b890 4834 non_fatal (_("CTF open failure: %s"), ctf_errmsg (err));
ffdfc835
AM
4835 my_bfd_nonfatal (bfd_get_filename (abfd));
4836 ctf_close (ctfa);
4837 free (ctfdata);
4838 return;
7d9813f1
NA
4839 }
4840
4841 printf (_("Contents of CTF section %s:\n"), sanitize_string (sect_name));
4842
80b56fad
NA
4843 while ((fp = ctf_archive_next (ctfa, &i, &name, 0, &err)) != NULL)
4844 dump_ctf_archive_member (fp, name, parent, member++);
4845 if (err != ECTF_NEXT_END)
83d59285
NA
4846 {
4847 dump_ctf_errs (NULL);
4848 non_fatal (_("CTF archive member open failure: %s"), ctf_errmsg (err));
ffdfc835 4849 my_bfd_nonfatal (bfd_get_filename (abfd));
83d59285 4850 }
139633c3 4851 ctf_dict_close (parent);
7d9813f1 4852 ctf_close (ctfa);
7d9813f1
NA
4853 free (ctfdata);
4854}
094e34f2
NA
4855#else
4856static void
4857dump_ctf (bfd *abfd ATTRIBUTE_UNUSED, const char *sect_name ATTRIBUTE_UNUSED,
4858 const char *parent_name ATTRIBUTE_UNUSED) {}
4859#endif
98a91d6a 4860
42b6953b
IB
4861static void
4862dump_section_sframe (bfd *abfd ATTRIBUTE_UNUSED,
4863 const char * sect_name)
4864{
f9c36cc9 4865 asection *sec;
42b6953b
IB
4866 sframe_decoder_ctx *sfd_ctx = NULL;
4867 bfd_size_type sf_size;
f9c36cc9 4868 bfd_byte *sframe_data;
42b6953b
IB
4869 bfd_vma sf_vma;
4870 int err = 0;
4871
4872 if (sect_name == NULL)
4873 sect_name = ".sframe";
4874
f9c36cc9
AM
4875 sec = read_section (abfd, sect_name, &sframe_data);
4876 if (sec == NULL)
ffdfc835
AM
4877 {
4878 my_bfd_nonfatal (bfd_get_filename (abfd));
4879 return;
4880 }
f9c36cc9
AM
4881 sf_size = bfd_section_size (sec);
4882 sf_vma = bfd_section_vma (sec);
42b6953b
IB
4883
4884 /* Decode the contents of the section. */
4885 sfd_ctx = sframe_decode ((const char*)sframe_data, sf_size, &err);
4886 if (!sfd_ctx)
ed38cbc3 4887 {
ffdfc835 4888 my_bfd_nonfatal (bfd_get_filename (abfd));
ed38cbc3 4889 free (sframe_data);
ffdfc835 4890 return;
ed38cbc3 4891 }
42b6953b
IB
4892
4893 printf (_("Contents of the SFrame section %s:"),
4894 sanitize_string (sect_name));
4895 /* Dump the contents as text. */
4896 dump_sframe (sfd_ctx, sf_vma);
ed38cbc3 4897
ed38cbc3 4898 sframe_decoder_free (&sfd_ctx);
ffdfc835 4899 free (sframe_data);
42b6953b
IB
4900}
4901
79b377b3 4902\f
252b5132 4903static void
46dca2e0 4904dump_bfd_private_header (bfd *abfd)
252b5132 4905{
7d272a55
AM
4906 if (!bfd_print_private_bfd_data (abfd, stdout))
4907 non_fatal (_("warning: private headers incomplete: %s"),
4908 bfd_errmsg (bfd_get_error ()));
252b5132
RH
4909}
4910
6abcee90
TG
4911static void
4912dump_target_specific (bfd *abfd)
4913{
4914 const struct objdump_private_desc * const *desc;
4915 struct objdump_private_option *opt;
4916 char *e, *b;
4917
4918 /* Find the desc. */
4919 for (desc = objdump_private_vectors; *desc != NULL; desc++)
4920 if ((*desc)->filter (abfd))
4921 break;
4922
c32d6f7b 4923 if (*desc == NULL)
6abcee90
TG
4924 {
4925 non_fatal (_("option -P/--private not supported by this file"));
4926 return;
4927 }
4928
4929 /* Clear all options. */
4930 for (opt = (*desc)->options; opt->name; opt++)
015dc7e1 4931 opt->selected = false;
6abcee90
TG
4932
4933 /* Decode options. */
4934 b = dump_private_options;
4935 do
4936 {
4937 e = strchr (b, ',');
4938
4939 if (e)
9b49454b 4940 *e = 0;
6abcee90
TG
4941
4942 for (opt = (*desc)->options; opt->name; opt++)
9b49454b
AM
4943 if (strcmp (opt->name, b) == 0)
4944 {
4945 opt->selected = true;
4946 break;
4947 }
6abcee90 4948 if (opt->name == NULL)
9b49454b 4949 non_fatal (_("target specific dump '%s' not supported"), b);
6abcee90
TG
4950
4951 if (e)
9b49454b
AM
4952 {
4953 *e = ',';
4954 b = e + 1;
4955 }
6abcee90
TG
4956 }
4957 while (e != NULL);
4958
4959 /* Dump. */
4960 (*desc)->dump (abfd);
4961}
155e0d23
NC
4962\f
4963/* Display a section in hexadecimal format with associated characters.
4964 Each line prefixed by the zero padded address. */
d24de309 4965
252b5132 4966static void
155e0d23 4967dump_section (bfd *abfd, asection *section, void *dummy ATTRIBUTE_UNUSED)
252b5132 4968{
cfd14a50 4969 bfd_byte *data = NULL;
155e0d23 4970 bfd_size_type datasize;
bdc4de1b
NC
4971 bfd_vma addr_offset;
4972 bfd_vma start_offset;
4973 bfd_vma stop_offset;
61826503 4974 unsigned int opb = bfd_octets_per_byte (abfd, section);
155e0d23
NC
4975 /* Bytes per line. */
4976 const int onaline = 16;
4977 char buf[64];
4978 int count;
4979 int width;
4980
0a3137ce
AM
4981 if (only_list == NULL)
4982 {
4983 if ((section->flags & SEC_HAS_CONTENTS) == 0)
4984 return;
4985 }
4986 else if (!process_section_p (section))
155e0d23 4987 return;
3aade688 4988
fd361982 4989 if ((datasize = bfd_section_size (section)) == 0)
155e0d23
NC
4990 return;
4991
155e0d23
NC
4992 /* Compute the address range to display. */
4993 if (start_address == (bfd_vma) -1
4994 || start_address < section->vma)
4995 start_offset = 0;
4996 else
4997 start_offset = start_address - section->vma;
4998
4999 if (stop_address == (bfd_vma) -1)
5000 stop_offset = datasize / opb;
5001 else
252b5132 5002 {
155e0d23
NC
5003 if (stop_address < section->vma)
5004 stop_offset = 0;
5005 else
5006 stop_offset = stop_address - section->vma;
252b5132 5007
155e0d23
NC
5008 if (stop_offset > datasize / opb)
5009 stop_offset = datasize / opb;
252b5132
RH
5010 }
5011
32760852
NC
5012 if (start_offset >= stop_offset)
5013 return;
3aade688 5014
12add40e 5015 printf (_("Contents of section %s:"), sanitize_string (section->name));
32760852 5016 if (display_file_offsets)
0af1713e
AM
5017 printf (_(" (Starting at file offset: 0x%lx)"),
5018 (unsigned long) (section->filepos + start_offset));
32760852
NC
5019 printf ("\n");
5020
4a114e3e
L
5021 if (!bfd_get_full_section_contents (abfd, section, &data))
5022 {
0821d5b1
NC
5023 non_fatal (_("Reading section %s failed because: %s"),
5024 section->name, bfd_errmsg (bfd_get_error ()));
4a114e3e
L
5025 return;
5026 }
32760852 5027
155e0d23 5028 width = 4;
026df7c5 5029
155e0d23
NC
5030 bfd_sprintf_vma (abfd, buf, start_offset + section->vma);
5031 if (strlen (buf) >= sizeof (buf))
5032 abort ();
026df7c5 5033
155e0d23
NC
5034 count = 0;
5035 while (buf[count] == '0' && buf[count+1] != '\0')
5036 count++;
5037 count = strlen (buf) - count;
5038 if (count > width)
5039 width = count;
252b5132 5040
155e0d23
NC
5041 bfd_sprintf_vma (abfd, buf, stop_offset + section->vma - 1);
5042 if (strlen (buf) >= sizeof (buf))
5043 abort ();
026df7c5 5044
155e0d23
NC
5045 count = 0;
5046 while (buf[count] == '0' && buf[count+1] != '\0')
5047 count++;
5048 count = strlen (buf) - count;
5049 if (count > width)
5050 width = count;
026df7c5 5051
155e0d23
NC
5052 for (addr_offset = start_offset;
5053 addr_offset < stop_offset; addr_offset += onaline / opb)
d24de309 5054 {
155e0d23 5055 bfd_size_type j;
d24de309 5056
155e0d23
NC
5057 bfd_sprintf_vma (abfd, buf, (addr_offset + section->vma));
5058 count = strlen (buf);
5059 if ((size_t) count >= sizeof (buf))
5060 abort ();
d24de309 5061
155e0d23
NC
5062 putchar (' ');
5063 while (count < width)
252b5132 5064 {
155e0d23
NC
5065 putchar ('0');
5066 count++;
5067 }
5068 fputs (buf + count - width, stdout);
5069 putchar (' ');
252b5132 5070
155e0d23
NC
5071 for (j = addr_offset * opb;
5072 j < addr_offset * opb + onaline; j++)
5073 {
5074 if (j < stop_offset * opb)
5075 printf ("%02x", (unsigned) (data[j]));
5076 else
5077 printf (" ");
5078 if ((j & 3) == 3)
5079 printf (" ");
252b5132
RH
5080 }
5081
155e0d23
NC
5082 printf (" ");
5083 for (j = addr_offset * opb;
5084 j < addr_offset * opb + onaline; j++)
5085 {
5086 if (j >= stop_offset * opb)
5087 printf (" ");
5088 else
5089 printf ("%c", ISPRINT (data[j]) ? data[j] : '.');
5090 }
5091 putchar ('\n');
252b5132 5092 }
155e0d23 5093 free (data);
252b5132 5094}
155e0d23 5095
98a91d6a 5096/* Actually display the various requested regions. */
252b5132
RH
5097
5098static void
46dca2e0 5099dump_data (bfd *abfd)
252b5132 5100{
155e0d23 5101 bfd_map_over_sections (abfd, dump_section, NULL);
252b5132
RH
5102}
5103
98a91d6a
NC
5104/* Should perhaps share code and display with nm? */
5105
252b5132 5106static void
015dc7e1 5107dump_symbols (bfd *abfd ATTRIBUTE_UNUSED, bool dynamic)
252b5132
RH
5108{
5109 asymbol **current;
91d6fa6a 5110 long max_count;
252b5132
RH
5111 long count;
5112
5113 if (dynamic)
5114 {
5115 current = dynsyms;
91d6fa6a 5116 max_count = dynsymcount;
252b5132
RH
5117 printf ("DYNAMIC SYMBOL TABLE:\n");
5118 }
5119 else
5120 {
5121 current = syms;
91d6fa6a 5122 max_count = symcount;
252b5132
RH
5123 printf ("SYMBOL TABLE:\n");
5124 }
5125
91d6fa6a 5126 if (max_count == 0)
a1df01d1
AM
5127 printf (_("no symbols\n"));
5128
91d6fa6a 5129 for (count = 0; count < max_count; count++)
252b5132 5130 {
155e0d23
NC
5131 bfd *cur_bfd;
5132
5133 if (*current == NULL)
83ef0798 5134 printf (_("no information for symbol number %ld\n"), count);
155e0d23
NC
5135
5136 else if ((cur_bfd = bfd_asymbol_bfd (*current)) == NULL)
83ef0798 5137 printf (_("could not determine the type of symbol number %ld\n"),
155e0d23
NC
5138 count);
5139
661f7c35
NC
5140 else if (process_section_p ((* current)->section)
5141 && (dump_special_syms
5142 || !bfd_is_target_special_symbol (cur_bfd, *current)))
252b5132 5143 {
155e0d23 5144 const char *name = (*current)->name;
252b5132 5145
155e0d23 5146 if (do_demangle && name != NULL && *name != '\0')
252b5132 5147 {
252b5132
RH
5148 char *alloc;
5149
155e0d23
NC
5150 /* If we want to demangle the name, we demangle it
5151 here, and temporarily clobber it while calling
5152 bfd_print_symbol. FIXME: This is a gross hack. */
af03af8f 5153 alloc = bfd_demangle (cur_bfd, name, demangle_flags);
ed180cc5
AM
5154 if (alloc != NULL)
5155 (*current)->name = alloc;
252b5132
RH
5156 bfd_print_symbol (cur_bfd, stdout, *current,
5157 bfd_print_symbol_all);
ed180cc5
AM
5158 if (alloc != NULL)
5159 {
5160 (*current)->name = name;
5161 free (alloc);
5162 }
252b5132 5163 }
b3aa80b4
NC
5164 else if (unicode_display != unicode_default
5165 && name != NULL && *name != '\0')
5166 {
5167 const char * sanitized_name;
5168
5169 /* If we want to sanitize the name, we do it here, and
5170 temporarily clobber it while calling bfd_print_symbol.
5171 FIXME: This is a gross hack. */
5172 sanitized_name = sanitize_string (name);
5173 if (sanitized_name != name)
5174 (*current)->name = sanitized_name;
5175 else
5176 sanitized_name = NULL;
5177 bfd_print_symbol (cur_bfd, stdout, *current,
5178 bfd_print_symbol_all);
5179 if (sanitized_name != NULL)
5180 (*current)->name = name;
5181 }
252b5132 5182 else
155e0d23
NC
5183 bfd_print_symbol (cur_bfd, stdout, *current,
5184 bfd_print_symbol_all);
83ef0798 5185 printf ("\n");
252b5132 5186 }
661f7c35 5187
155e0d23 5188 current++;
252b5132 5189 }
155e0d23 5190 printf ("\n\n");
252b5132 5191}
155e0d23 5192\f
252b5132 5193static void
46dca2e0 5194dump_reloc_set (bfd *abfd, asection *sec, arelent **relpp, long relcount)
252b5132
RH
5195{
5196 arelent **p;
5197 char *last_filename, *last_functionname;
5198 unsigned int last_line;
9b8d1a36 5199 unsigned int last_discriminator;
252b5132
RH
5200
5201 /* Get column headers lined up reasonably. */
5202 {
5203 static int width;
98a91d6a 5204
252b5132
RH
5205 if (width == 0)
5206 {
5207 char buf[30];
155e0d23 5208
d8180c76 5209 bfd_sprintf_vma (abfd, buf, (bfd_vma) -1);
252b5132
RH
5210 width = strlen (buf) - 7;
5211 }
99f647e2 5212 printf ("OFFSET %*s TYPE %*s VALUE\n", width, "", 12, "");
252b5132
RH
5213 }
5214
5215 last_filename = NULL;
5216 last_functionname = NULL;
5217 last_line = 0;
9b8d1a36 5218 last_discriminator = 0;
252b5132 5219
d3ba0551 5220 for (p = relpp; relcount && *p != NULL; p++, relcount--)
252b5132
RH
5221 {
5222 arelent *q = *p;
5223 const char *filename, *functionname;
91d6fa6a 5224 unsigned int linenumber;
9b8d1a36 5225 unsigned int discriminator;
252b5132
RH
5226 const char *sym_name;
5227 const char *section_name;
bf03e632 5228 bfd_vma addend2 = 0;
252b5132
RH
5229
5230 if (start_address != (bfd_vma) -1
5231 && q->address < start_address)
5232 continue;
5233 if (stop_address != (bfd_vma) -1
5234 && q->address > stop_address)
5235 continue;
5236
5237 if (with_line_numbers
5238 && sec != NULL
9b8d1a36 5239 && bfd_find_nearest_line_discriminator (abfd, sec, syms, q->address,
9b49454b
AM
5240 &filename, &functionname,
5241 &linenumber, &discriminator))
252b5132
RH
5242 {
5243 if (functionname != NULL
5244 && (last_functionname == NULL
5245 || strcmp (functionname, last_functionname) != 0))
5246 {
12add40e 5247 printf ("%s():\n", sanitize_string (functionname));
252b5132
RH
5248 if (last_functionname != NULL)
5249 free (last_functionname);
5250 last_functionname = xstrdup (functionname);
5251 }
98a91d6a 5252
91d6fa6a
NC
5253 if (linenumber > 0
5254 && (linenumber != last_line
252b5132
RH
5255 || (filename != NULL
5256 && last_filename != NULL
9b8d1a36 5257 && filename_cmp (filename, last_filename) != 0)
9b49454b 5258 || (discriminator != last_discriminator)))
252b5132 5259 {
9b49454b
AM
5260 if (discriminator > 0)
5261 printf ("%s:%u\n", filename == NULL ? "???" :
12add40e 5262 sanitize_string (filename), linenumber);
9b49454b
AM
5263 else
5264 printf ("%s:%u (discriminator %u)\n",
12add40e 5265 filename == NULL ? "???" : sanitize_string (filename),
9b49454b 5266 linenumber, discriminator);
91d6fa6a 5267 last_line = linenumber;
9b8d1a36 5268 last_discriminator = discriminator;
252b5132
RH
5269 if (last_filename != NULL)
5270 free (last_filename);
5271 if (filename == NULL)
5272 last_filename = NULL;
5273 else
5274 last_filename = xstrdup (filename);
5275 }
5276 }
5277
5278 if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
5279 {
5280 sym_name = (*(q->sym_ptr_ptr))->name;
5281 section_name = (*(q->sym_ptr_ptr))->section->name;
5282 }
5283 else
5284 {
5285 sym_name = NULL;
5286 section_name = NULL;
5287 }
98a91d6a 5288
f9ecb0a4
JJ
5289 bfd_printf_vma (abfd, q->address);
5290 if (q->howto == NULL)
5291 printf (" *unknown* ");
5292 else if (q->howto->name)
bf03e632
DM
5293 {
5294 const char *name = q->howto->name;
5295
5296 /* R_SPARC_OLO10 relocations contain two addends.
5297 But because 'arelent' lacks enough storage to
5298 store them both, the 64-bit ELF Sparc backend
5299 records this as two relocations. One R_SPARC_LO10
5300 and one R_SPARC_13, both pointing to the same
5301 address. This is merely so that we have some
5302 place to store both addend fields.
5303
5304 Undo this transformation, otherwise the output
5305 will be confusing. */
5306 if (abfd->xvec->flavour == bfd_target_elf_flavour
82a9ed20 5307 && elf_tdata (abfd)->elf_header->e_machine == EM_SPARCV9
bf03e632
DM
5308 && relcount > 1
5309 && !strcmp (q->howto->name, "R_SPARC_LO10"))
5310 {
5311 arelent *q2 = *(p + 1);
5312 if (q2 != NULL
5313 && q2->howto
5314 && q->address == q2->address
5315 && !strcmp (q2->howto->name, "R_SPARC_13"))
5316 {
5317 name = "R_SPARC_OLO10";
5318 addend2 = q2->addend;
5319 p++;
5320 }
5321 }
5322 printf (" %-16s ", name);
5323 }
f9ecb0a4
JJ
5324 else
5325 printf (" %-16d ", q->howto->type);
171191ba 5326
252b5132 5327 if (sym_name)
171191ba
NC
5328 {
5329 objdump_print_symname (abfd, NULL, *q->sym_ptr_ptr);
171191ba 5330 }
252b5132
RH
5331 else
5332 {
d3ba0551 5333 if (section_name == NULL)
252b5132 5334 section_name = "*unknown*";
12add40e 5335 printf ("[%s]", sanitize_string (section_name));
252b5132 5336 }
98a91d6a 5337
252b5132
RH
5338 if (q->addend)
5339 {
343dbc36
L
5340 bfd_signed_vma addend = q->addend;
5341 if (addend < 0)
5342 {
5343 printf ("-0x");
5344 addend = -addend;
5345 }
5346 else
5347 printf ("+0x");
5348 bfd_printf_vma (abfd, addend);
252b5132 5349 }
bf03e632
DM
5350 if (addend2)
5351 {
5352 printf ("+0x");
5353 bfd_printf_vma (abfd, addend2);
5354 }
98a91d6a 5355
252b5132
RH
5356 printf ("\n");
5357 }
4b41844b
NC
5358
5359 if (last_filename != NULL)
5360 free (last_filename);
5361 if (last_functionname != NULL)
5362 free (last_functionname);
252b5132 5363}
43ac9881 5364
155e0d23 5365static void
3b9ad1cc
AM
5366dump_relocs_in_section (bfd *abfd,
5367 asection *section,
5368 void *dummy ATTRIBUTE_UNUSED)
155e0d23 5369{
ffdfc835 5370 arelent **relpp;
155e0d23
NC
5371 long relcount;
5372 long relsize;
5373
5374 if ( bfd_is_abs_section (section)
5375 || bfd_is_und_section (section)
5376 || bfd_is_com_section (section)
5377 || (! process_section_p (section))
5378 || ((section->flags & SEC_RELOC) == 0))
5379 return;
5380
12add40e 5381 printf ("RELOCATION RECORDS FOR [%s]:", sanitize_string (section->name));
155e0d23 5382
7a6e0d89 5383 relsize = bfd_get_reloc_upper_bound (abfd, section);
155e0d23
NC
5384 if (relsize == 0)
5385 {
5386 printf (" (none)\n\n");
5387 return;
5388 }
5389
7a6e0d89 5390 if (relsize < 0)
ffdfc835
AM
5391 {
5392 relpp = NULL;
5393 relcount = relsize;
5394 }
7a6e0d89
AM
5395 else
5396 {
5397 relpp = (arelent **) xmalloc (relsize);
5398 relcount = bfd_canonicalize_reloc (abfd, section, relpp, syms);
39ff1b79
NC
5399 }
5400
155e0d23 5401 if (relcount < 0)
5a3f568b
NC
5402 {
5403 printf ("\n");
7a6e0d89
AM
5404 non_fatal (_("failed to read relocs in: %s"),
5405 sanitize_string (bfd_get_filename (abfd)));
ffdfc835 5406 my_bfd_nonfatal (_("error message was"));
5a3f568b 5407 }
155e0d23
NC
5408 else if (relcount == 0)
5409 printf (" (none)\n\n");
5410 else
5411 {
5412 printf ("\n");
5413 dump_reloc_set (abfd, section, relpp, relcount);
5414 printf ("\n\n");
5415 }
5416 free (relpp);
5417}
5418
5419static void
5420dump_relocs (bfd *abfd)
5421{
5422 bfd_map_over_sections (abfd, dump_relocs_in_section, NULL);
5423}
5424
5425static void
5426dump_dynamic_relocs (bfd *abfd)
5427{
5428 long relsize;
5429 arelent **relpp;
5430 long relcount;
5431
5432 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
155e0d23
NC
5433
5434 printf ("DYNAMIC RELOCATION RECORDS");
5435
5436 if (relsize == 0)
ffdfc835
AM
5437 {
5438 printf (" (none)\n\n");
5439 return;
5440 }
5441
5442 if (relsize < 0)
5443 {
5444 relpp = NULL;
5445 relcount = relsize;
5446 }
155e0d23
NC
5447 else
5448 {
3f5e193b 5449 relpp = (arelent **) xmalloc (relsize);
155e0d23 5450 relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
ffdfc835 5451 }
155e0d23 5452
ffdfc835
AM
5453 if (relcount < 0)
5454 {
5455 printf ("\n");
5456 non_fatal (_("failed to read relocs in: %s"),
5457 sanitize_string (bfd_get_filename (abfd)));
5458 my_bfd_nonfatal (_("error message was"));
155e0d23 5459 }
ffdfc835
AM
5460 else if (relcount == 0)
5461 printf (" (none)\n\n");
5462 else
5463 {
5464 printf ("\n");
5465 dump_reloc_set (abfd, NULL, relpp, relcount);
5466 printf ("\n\n");
5467 }
5468 free (relpp);
155e0d23
NC
5469}
5470
43ac9881
AM
5471/* Creates a table of paths, to search for source files. */
5472
5473static void
5474add_include_path (const char *path)
5475{
5476 if (path[0] == 0)
5477 return;
5478 include_path_count++;
3f5e193b
NC
5479 include_paths = (const char **)
5480 xrealloc (include_paths, include_path_count * sizeof (*include_paths));
43ac9881
AM
5481#ifdef HAVE_DOS_BASED_FILE_SYSTEM
5482 if (path[1] == ':' && path[2] == 0)
5483 path = concat (path, ".", (const char *) 0);
5484#endif
5485 include_paths[include_path_count - 1] = path;
5486}
155e0d23
NC
5487
5488static void
3b9ad1cc
AM
5489adjust_addresses (bfd *abfd ATTRIBUTE_UNUSED,
5490 asection *section,
bc79cded 5491 void *arg)
155e0d23 5492{
bc79cded
L
5493 if ((section->flags & SEC_DEBUGGING) == 0)
5494 {
015dc7e1 5495 bool *has_reloc_p = (bool *) arg;
bc79cded
L
5496 section->vma += adjust_section_vma;
5497 if (*has_reloc_p)
5498 section->lma += adjust_section_vma;
5499 }
155e0d23
NC
5500}
5501
2379f9c4
FS
5502/* Return the sign-extended form of an ARCH_SIZE sized VMA. */
5503
5504static bfd_vma
5505sign_extend_address (bfd *abfd ATTRIBUTE_UNUSED,
5506 bfd_vma vma,
5507 unsigned arch_size)
5508{
5509 bfd_vma mask;
5510 mask = (bfd_vma) 1 << (arch_size - 1);
5511 return (((vma & ((mask << 1) - 1)) ^ mask) - mask);
5512}
5513
94585d6d
NC
5514static bool
5515might_need_separate_debug_info (bool is_mainfile)
5516{
5517 /* We do not follow links from debug info files. */
5518 if (! is_mainfile)
5519 return false;
5520
5521 /* Since do_follow_links might be enabled by default, only treat it as an
5522 indication that separate files should be loaded if setting it was a
5523 deliberate user action. */
5524 if (DEFAULT_FOR_FOLLOW_LINKS == 0 && do_follow_links)
5525 return true;
5526
5527 if (process_links || dump_symtab || dump_debugging
e9a241e8 5528 || dump_dwarf_section_info || with_source_code)
94585d6d
NC
5529 return true;
5530
5531 return false;
5532}
5533
155e0d23
NC
5534/* Dump selected contents of ABFD. */
5535
5536static void
015dc7e1 5537dump_bfd (bfd *abfd, bool is_mainfile)
155e0d23 5538{
2379f9c4
FS
5539 const struct elf_backend_data * bed;
5540
39f0547e
NC
5541 if (bfd_big_endian (abfd))
5542 byte_get = byte_get_big_endian;
5543 else if (bfd_little_endian (abfd))
5544 byte_get = byte_get_little_endian;
5545 else
5546 byte_get = NULL;
5547
94585d6d
NC
5548 /* Load any separate debug information files. */
5549 if (byte_get != NULL && might_need_separate_debug_info (is_mainfile))
39f0547e
NC
5550 {
5551 load_separate_debug_files (abfd, bfd_get_filename (abfd));
5552
5553 /* If asked to do so, recursively dump the separate files. */
5554 if (do_follow_links)
5555 {
5556 separate_info * i;
5557
5558 for (i = first_separate_info; i != NULL; i = i->next)
015dc7e1 5559 dump_bfd (i->handle, false);
39f0547e
NC
5560 }
5561 }
5562
2379f9c4
FS
5563 /* Adjust user-specified start and stop limits for targets that use
5564 signed addresses. */
5565 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
5566 && (bed = get_elf_backend_data (abfd)) != NULL
5567 && bed->sign_extend_vma)
5568 {
5569 start_address = sign_extend_address (abfd, start_address,
5570 bed->s->arch_size);
5571 stop_address = sign_extend_address (abfd, stop_address,
5572 bed->s->arch_size);
5573 }
5574
155e0d23
NC
5575 /* If we are adjusting section VMA's, change them all now. Changing
5576 the BFD information is a hack. However, we must do it, or
5577 bfd_find_nearest_line will not do the right thing. */
5578 if (adjust_section_vma != 0)
bc79cded 5579 {
015dc7e1 5580 bool has_reloc = (abfd->flags & HAS_RELOC);
bc79cded
L
5581 bfd_map_over_sections (abfd, adjust_addresses, &has_reloc);
5582 }
155e0d23 5583
e2c0149e
AM
5584 if (is_mainfile || process_links)
5585 {
5586 if (! dump_debugging_tags && ! suppress_bfd_header)
5587 printf (_("\n%s: file format %s\n"),
5588 sanitize_string (bfd_get_filename (abfd)),
5589 abfd->xvec->name);
5590 if (dump_ar_hdrs)
5591 print_arelt_descr (stdout, abfd, true, false);
5592 if (dump_file_header)
5593 dump_bfd_header (abfd);
5594 if (dump_private_headers)
5595 dump_bfd_private_header (abfd);
5596 if (dump_private_options != NULL)
5597 dump_target_specific (abfd);
5598 if (! dump_debugging_tags && ! suppress_bfd_header)
5599 putchar ('\n');
5600 }
155e0d23 5601
365544c3
L
5602 if (dump_symtab
5603 || dump_reloc_info
5604 || disassemble
5605 || dump_debugging
5606 || dump_dwarf_section_info)
39f0547e
NC
5607 {
5608 syms = slurp_symtab (abfd);
5609
5610 /* If following links, load any symbol tables from the linked files as well. */
5611 if (do_follow_links && is_mainfile)
5612 {
5613 separate_info * i;
5614
5615 for (i = first_separate_info; i != NULL; i = i->next)
5616 {
5617 asymbol ** extra_syms;
5618 long old_symcount = symcount;
9b49454b 5619
39f0547e
NC
5620 extra_syms = slurp_symtab (i->handle);
5621
5622 if (extra_syms)
5623 {
5624 if (old_symcount == 0)
5625 {
5626 syms = extra_syms;
5627 }
5628 else
5629 {
1944212b
AM
5630 syms = xrealloc (syms, ((symcount + old_symcount + 1)
5631 * sizeof (asymbol *)));
39f0547e
NC
5632 memcpy (syms + old_symcount,
5633 extra_syms,
1944212b 5634 (symcount + 1) * sizeof (asymbol *));
39f0547e
NC
5635 }
5636 }
5637
5638 symcount += old_symcount;
5639 }
5640 }
5641 }
a29a8af8 5642
e2c0149e
AM
5643 if (is_mainfile || process_links)
5644 {
5645 if (dump_section_headers)
5646 dump_headers (abfd);
a29a8af8 5647
e2c0149e
AM
5648 if (dump_dynamic_symtab || dump_dynamic_reloc_info
5649 || (disassemble && bfd_get_dynamic_symtab_upper_bound (abfd) > 0))
5650 dynsyms = slurp_dynamic_symtab (abfd);
39f0547e 5651
e2c0149e
AM
5652 if (disassemble)
5653 {
5654 synthcount = bfd_get_synthetic_symtab (abfd, symcount, syms,
5655 dynsymcount, dynsyms,
5656 &synthsyms);
5657 if (synthcount < 0)
5658 synthcount = 0;
5659 }
155e0d23 5660
e2c0149e
AM
5661 if (dump_symtab)
5662 dump_symbols (abfd, false);
5663 if (dump_dynamic_symtab)
5664 dump_symbols (abfd, true);
5665 }
365544c3 5666 if (dump_dwarf_section_info)
e2c0149e
AM
5667 dump_dwarf (abfd, is_mainfile);
5668 if (is_mainfile || process_links)
5669 {
5670 if (dump_ctf_section_info)
5671 dump_ctf (abfd, dump_ctf_section_name, dump_ctf_parent_name);
42b6953b
IB
5672 if (dump_sframe_section_info)
5673 dump_section_sframe (abfd, dump_sframe_section_name);
e2c0149e
AM
5674 if (dump_stab_section_info)
5675 dump_stabs (abfd);
5676 if (dump_reloc_info && ! disassemble)
5677 dump_relocs (abfd);
5678 if (dump_dynamic_reloc_info && ! disassemble)
5679 dump_dynamic_relocs (abfd);
5680 if (dump_section_contents)
5681 dump_data (abfd);
5682 if (disassemble)
5683 disassemble_data (abfd);
5684 }
155e0d23
NC
5685
5686 if (dump_debugging)
5687 {
5688 void *dhandle;
5689
015dc7e1 5690 dhandle = read_debugging_info (abfd, syms, symcount, true);
155e0d23
NC
5691 if (dhandle != NULL)
5692 {
ed180cc5
AM
5693 if (!print_debugging_info (stdout, dhandle, abfd, syms,
5694 bfd_demangle,
63b4cc53 5695 dump_debugging_tags != 0))
155e0d23
NC
5696 {
5697 non_fatal (_("%s: printing debugging information failed"),
5698 bfd_get_filename (abfd));
5699 exit_status = 1;
5700 }
cf0ad5bb
NC
5701
5702 free (dhandle);
155e0d23 5703 }
fdef3943
AM
5704 /* PR 6483: If there was no STABS debug info in the file, try
5705 DWARF instead. */
b922d590
NC
5706 else if (! dump_dwarf_section_info)
5707 {
3aade688 5708 dwarf_select_sections_all ();
e2c0149e 5709 dump_dwarf (abfd, is_mainfile);
b922d590 5710 }
155e0d23
NC
5711 }
5712
5713 if (syms)
5714 {
5715 free (syms);
5716 syms = NULL;
5717 }
5718
5719 if (dynsyms)
5720 {
5721 free (dynsyms);
5722 dynsyms = NULL;
5723 }
4c45e5c9
JJ
5724
5725 if (synthsyms)
5726 {
5727 free (synthsyms);
5728 synthsyms = NULL;
5729 }
5730
5731 symcount = 0;
5732 dynsymcount = 0;
5733 synthcount = 0;
39f0547e
NC
5734
5735 if (is_mainfile)
5736 free_debug_memory ();
155e0d23
NC
5737}
5738
5739static void
1598539f 5740display_object_bfd (bfd *abfd)
155e0d23
NC
5741{
5742 char **matching;
5743
5744 if (bfd_check_format_matches (abfd, bfd_object, &matching))
5745 {
015dc7e1 5746 dump_bfd (abfd, true);
155e0d23
NC
5747 return;
5748 }
5749
5750 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
5751 {
a734d906 5752 my_bfd_nonfatal (bfd_get_filename (abfd));
155e0d23 5753 list_matching_formats (matching);
155e0d23
NC
5754 return;
5755 }
5756
5757 if (bfd_get_error () != bfd_error_file_not_recognized)
5758 {
a734d906 5759 my_bfd_nonfatal (bfd_get_filename (abfd));
155e0d23
NC
5760 return;
5761 }
5762
5763 if (bfd_check_format_matches (abfd, bfd_core, &matching))
5764 {
015dc7e1 5765 dump_bfd (abfd, true);
155e0d23
NC
5766 return;
5767 }
5768
a734d906 5769 my_bfd_nonfatal (bfd_get_filename (abfd));
155e0d23
NC
5770
5771 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
370426d0 5772 list_matching_formats (matching);
155e0d23
NC
5773}
5774
5775static void
1598539f 5776display_any_bfd (bfd *file, int level)
155e0d23 5777{
4a114e3e
L
5778 /* Decompress sections unless dumping the section contents. */
5779 if (!dump_section_contents)
5780 file->flags |= BFD_DECOMPRESS;
5781
155e0d23
NC
5782 /* If the file is an archive, process all of its elements. */
5783 if (bfd_check_format (file, bfd_archive))
5784 {
1598539f 5785 bfd *arfile = NULL;
155e0d23 5786 bfd *last_arfile = NULL;
bdc4de1b 5787
1598539f 5788 if (level == 0)
9b49454b 5789 printf (_("In archive %s:\n"), sanitize_string (bfd_get_filename (file)));
c88f5b8e
NC
5790 else if (level > 100)
5791 {
5792 /* Prevent corrupted files from spinning us into an
5793 infinite loop. 100 is an arbitrary heuristic. */
ffdfc835
AM
5794 non_fatal (_("Archive nesting is too deep"));
5795 exit_status = 1;
c88f5b8e
NC
5796 return;
5797 }
1598539f 5798 else
9b49454b 5799 printf (_("In nested archive %s:\n"),
12add40e 5800 sanitize_string (bfd_get_filename (file)));
1598539f 5801
155e0d23
NC
5802 for (;;)
5803 {
5804 bfd_set_error (bfd_error_no_error);
5805
5806 arfile = bfd_openr_next_archived_file (file, arfile);
5807 if (arfile == NULL)
5808 {
5809 if (bfd_get_error () != bfd_error_no_more_archived_files)
a734d906 5810 my_bfd_nonfatal (bfd_get_filename (file));
155e0d23
NC
5811 break;
5812 }
5813
1598539f 5814 display_any_bfd (arfile, level + 1);
155e0d23
NC
5815
5816 if (last_arfile != NULL)
f64e188b
NC
5817 {
5818 bfd_close (last_arfile);
5819 /* PR 17512: file: ac585d01. */
5820 if (arfile == last_arfile)
5821 {
5822 last_arfile = NULL;
5823 break;
5824 }
5825 }
155e0d23
NC
5826 last_arfile = arfile;
5827 }
5828
5829 if (last_arfile != NULL)
5830 bfd_close (last_arfile);
5831 }
5832 else
1598539f
TG
5833 display_object_bfd (file);
5834}
5835
5836static void
015dc7e1 5837display_file (char *filename, char *target, bool last_file)
1598539f
TG
5838{
5839 bfd *file;
5840
5841 if (get_file_size (filename) < 1)
5842 {
5843 exit_status = 1;
5844 return;
5845 }
5846
5847 file = bfd_openr (filename, target);
5848 if (file == NULL)
5849 {
a734d906 5850 my_bfd_nonfatal (filename);
1598539f
TG
5851 return;
5852 }
5853
5854 display_any_bfd (file, 0);
155e0d23 5855
cd6581da
NC
5856 /* This is an optimization to improve the speed of objdump, especially when
5857 dumping a file with lots of associated debug informatiom. Calling
5858 bfd_close on such a file can take a non-trivial amount of time as there
5859 are lots of lists to walk and buffers to free. This is only really
5860 necessary however if we are about to load another file and we need the
5861 memory back. Otherwise, if we are about to exit, then we can save (a lot
5862 of) time by only doing a quick close, and allowing the OS to reclaim the
5863 memory for us. */
5864 if (! last_file)
5865 bfd_close (file);
5866 else
5867 bfd_close_all_done (file);
155e0d23 5868}
252b5132 5869\f
252b5132 5870int
46dca2e0 5871main (int argc, char **argv)
252b5132
RH
5872{
5873 int c;
5874 char *target = default_target;
015dc7e1 5875 bool seenflag = false;
252b5132 5876
87b9f255 5877#ifdef HAVE_LC_MESSAGES
252b5132 5878 setlocale (LC_MESSAGES, "");
3882b010 5879#endif
3882b010 5880 setlocale (LC_CTYPE, "");
155e0d23 5881
252b5132
RH
5882 bindtextdomain (PACKAGE, LOCALEDIR);
5883 textdomain (PACKAGE);
5884
5885 program_name = *argv;
5886 xmalloc_set_program_name (program_name);
86eafac0 5887 bfd_set_error_program_name (program_name);
252b5132 5888
869b9d07
MM
5889 expandargv (&argc, &argv);
5890
bf2dd8d7
AM
5891 if (bfd_init () != BFD_INIT_MAGIC)
5892 fatal (_("fatal error: libbfd ABI mismatch"));
252b5132
RH
5893 set_default_bfd_target ();
5894
4cb93e3b 5895 while ((c = getopt_long (argc, argv,
b3aa80b4 5896 "CDE:FGHI:LM:P:RSTU:VW::ab:defghij:lm:prstvwxz",
252b5132
RH
5897 long_options, (int *) 0))
5898 != EOF)
5899 {
252b5132
RH
5900 switch (c)
5901 {
5902 case 0:
8b53311e 5903 break; /* We've been given a long option. */
252b5132
RH
5904 case 'm':
5905 machine = optarg;
5906 break;
dd92f639 5907 case 'M':
65b48a81
PB
5908 {
5909 char *options;
5910 if (disassembler_options)
5911 /* Ignore potential memory leak for now. */
5912 options = concat (disassembler_options, ",",
5913 optarg, (const char *) NULL);
5914 else
5915 options = optarg;
5916 disassembler_options = remove_whitespace_and_extra_commas (options);
5917 }
dd92f639 5918 break;
252b5132 5919 case 'j':
70ecb384 5920 add_only (optarg);
252b5132 5921 break;
98ec6e72 5922 case 'F':
015dc7e1 5923 display_file_offsets = true;
98ec6e72 5924 break;
252b5132 5925 case 'l':
015dc7e1 5926 with_line_numbers = true;
252b5132
RH
5927 break;
5928 case 'b':
5929 target = optarg;
5930 break;
1dada9c5 5931 case 'C':
015dc7e1 5932 do_demangle = true;
28c309a2
NC
5933 if (optarg != NULL)
5934 {
5935 enum demangling_styles style;
8b53311e 5936
28c309a2 5937 style = cplus_demangle_name_to_style (optarg);
0af11b59 5938 if (style == unknown_demangling)
28c309a2
NC
5939 fatal (_("unknown demangling style `%s'"),
5940 optarg);
8b53311e 5941
28c309a2 5942 cplus_demangle_set_style (style);
0af11b59 5943 }
1dada9c5 5944 break;
af03af8f
NC
5945 case OPTION_RECURSE_LIMIT:
5946 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
5947 break;
5948 case OPTION_NO_RECURSE_LIMIT:
5949 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
5950 break;
1dada9c5 5951 case 'w':
015dc7e1 5952 do_wide = wide_output = true;
1dada9c5
NC
5953 break;
5954 case OPTION_ADJUST_VMA:
5955 adjust_section_vma = parse_vma (optarg, "--adjust-vma");
5956 break;
5957 case OPTION_START_ADDRESS:
5958 start_address = parse_vma (optarg, "--start-address");
98ec6e72
NC
5959 if ((stop_address != (bfd_vma) -1) && stop_address <= start_address)
5960 fatal (_("error: the start address should be before the end address"));
1dada9c5
NC
5961 break;
5962 case OPTION_STOP_ADDRESS:
5963 stop_address = parse_vma (optarg, "--stop-address");
98ec6e72
NC
5964 if ((start_address != (bfd_vma) -1) && stop_address <= start_address)
5965 fatal (_("error: the stop address should be after the start address"));
1dada9c5 5966 break;
0dafdf3f
L
5967 case OPTION_PREFIX:
5968 prefix = optarg;
5969 prefix_length = strlen (prefix);
5970 /* Remove an unnecessary trailing '/' */
5971 while (IS_DIR_SEPARATOR (prefix[prefix_length - 1]))
5972 prefix_length--;
5973 break;
5974 case OPTION_PREFIX_STRIP:
5975 prefix_strip = atoi (optarg);
5976 if (prefix_strip < 0)
5977 fatal (_("error: prefix strip must be non-negative"));
5978 break;
3dcb3fcb
L
5979 case OPTION_INSN_WIDTH:
5980 insn_width = strtoul (optarg, NULL, 0);
5981 if (insn_width <= 0)
5982 fatal (_("error: instruction width must be positive"));
5983 break;
4a14e306 5984 case OPTION_INLINES:
015dc7e1 5985 unwind_inlines = true;
4a14e306 5986 break;
1d67fe3b 5987 case OPTION_VISUALIZE_JUMPS:
015dc7e1
AM
5988 visualize_jumps = true;
5989 color_output = false;
5990 extended_color_output = false;
1d67fe3b
TT
5991 if (optarg != NULL)
5992 {
5993 if (streq (optarg, "color"))
015dc7e1 5994 color_output = true;
1d67fe3b
TT
5995 else if (streq (optarg, "extended-color"))
5996 {
015dc7e1
AM
5997 color_output = true;
5998 extended_color_output = true;
1d67fe3b
TT
5999 }
6000 else if (streq (optarg, "off"))
015dc7e1 6001 visualize_jumps = false;
1d67fe3b 6002 else
a734d906
AM
6003 {
6004 non_fatal (_("unrecognized argument to --visualize-option"));
6005 usage (stderr, 1);
6006 }
1d67fe3b
TT
6007 }
6008 break;
60a3da00
AB
6009 case OPTION_DISASSEMBLER_COLOR:
6010 if (streq (optarg, "off"))
a88c79b7 6011 disassembler_color = off;
18bf5643
NC
6012 else if (streq (optarg, "terminal"))
6013 disassembler_color = on_if_terminal_output;
6014 else if (streq (optarg, "color")
6015 || streq (optarg, "colour")
6016 || streq (optarg, "on"))
a88c79b7 6017 disassembler_color = on;
18bf5643
NC
6018 else if (streq (optarg, "extended")
6019 || streq (optarg, "extended-color")
6020 || streq (optarg, "extended-colour"))
a88c79b7 6021 disassembler_color = extended;
60a3da00 6022 else
a734d906
AM
6023 {
6024 non_fatal (_("unrecognized argument to --disassembler-color"));
6025 usage (stderr, 1);
6026 }
60a3da00 6027 break;
1dada9c5
NC
6028 case 'E':
6029 if (strcmp (optarg, "B") == 0)
6030 endian = BFD_ENDIAN_BIG;
6031 else if (strcmp (optarg, "L") == 0)
6032 endian = BFD_ENDIAN_LITTLE;
6033 else
6034 {
a734d906 6035 non_fatal (_("unrecognized -E option"));
1dada9c5
NC
6036 usage (stderr, 1);
6037 }
6038 break;
6039 case OPTION_ENDIAN:
6040 if (strncmp (optarg, "big", strlen (optarg)) == 0)
6041 endian = BFD_ENDIAN_BIG;
6042 else if (strncmp (optarg, "little", strlen (optarg)) == 0)
6043 endian = BFD_ENDIAN_LITTLE;
6044 else
6045 {
37cc8ec1 6046 non_fatal (_("unrecognized --endian type `%s'"), optarg);
1dada9c5
NC
6047 usage (stderr, 1);
6048 }
6049 break;
8b53311e 6050
252b5132 6051 case 'f':
015dc7e1
AM
6052 dump_file_header = true;
6053 seenflag = true;
252b5132
RH
6054 break;
6055 case 'i':
015dc7e1
AM
6056 formats_info = true;
6057 seenflag = true;
252b5132 6058 break;
43ac9881
AM
6059 case 'I':
6060 add_include_path (optarg);
6061 break;
252b5132 6062 case 'p':
015dc7e1
AM
6063 dump_private_headers = true;
6064 seenflag = true;
252b5132 6065 break;
6abcee90
TG
6066 case 'P':
6067 dump_private_options = optarg;
015dc7e1 6068 seenflag = true;
6abcee90 6069 break;
252b5132 6070 case 'x':
015dc7e1
AM
6071 dump_private_headers = true;
6072 dump_symtab = true;
6073 dump_reloc_info = true;
6074 dump_file_header = true;
6075 dump_ar_hdrs = true;
6076 dump_section_headers = true;
6077 seenflag = true;
252b5132
RH
6078 break;
6079 case 't':
015dc7e1
AM
6080 dump_symtab = true;
6081 seenflag = true;
252b5132
RH
6082 break;
6083 case 'T':
015dc7e1
AM
6084 dump_dynamic_symtab = true;
6085 seenflag = true;
252b5132
RH
6086 break;
6087 case 'd':
015dc7e1
AM
6088 disassemble = true;
6089 seenflag = true;
d3def5d7 6090 disasm_sym = optarg;
1dada9c5
NC
6091 break;
6092 case 'z':
015dc7e1 6093 disassemble_zeroes = true;
252b5132
RH
6094 break;
6095 case 'D':
015dc7e1
AM
6096 disassemble = true;
6097 disassemble_all = true;
6098 seenflag = true;
252b5132
RH
6099 break;
6100 case 'S':
015dc7e1
AM
6101 disassemble = true;
6102 with_source_code = true;
6103 seenflag = true;
1dada9c5 6104 break;
a1c110a3 6105 case OPTION_SOURCE_COMMENT:
015dc7e1
AM
6106 disassemble = true;
6107 with_source_code = true;
6108 seenflag = true;
a1c110a3
NC
6109 if (optarg)
6110 source_comment = xstrdup (sanitize_string (optarg));
6111 else
6112 source_comment = xstrdup ("# ");
6113 break;
1dada9c5
NC
6114 case 'g':
6115 dump_debugging = 1;
015dc7e1 6116 seenflag = true;
1dada9c5 6117 break;
51cdc6e0
NC
6118 case 'e':
6119 dump_debugging = 1;
6120 dump_debugging_tags = 1;
015dc7e1
AM
6121 do_demangle = true;
6122 seenflag = true;
51cdc6e0 6123 break;
ca0e11aa 6124 case 'L':
015dc7e1
AM
6125 process_links = true;
6126 do_follow_links = true;
ca0e11aa 6127 break;
365544c3 6128 case 'W':
015dc7e1 6129 seenflag = true;
4cb93e3b 6130 if (optarg)
94585d6d
NC
6131 {
6132 if (dwarf_select_sections_by_letters (optarg))
6133 dump_dwarf_section_info = true;
6134 }
4cb93e3b 6135 else
94585d6d
NC
6136 {
6137 dump_dwarf_section_info = true;
6138 dwarf_select_sections_all ();
6139 }
4cb93e3b
TG
6140 break;
6141 case OPTION_DWARF:
015dc7e1 6142 seenflag = true;
4cb93e3b 6143 if (optarg)
94585d6d
NC
6144 {
6145 if (dwarf_select_sections_by_names (optarg))
6146 dump_dwarf_section_info = true;
6147 }
4cb93e3b 6148 else
94585d6d
NC
6149 {
6150 dwarf_select_sections_all ();
6151 dump_dwarf_section_info = true;
6152 }
365544c3 6153 break;
fd2f0033
TT
6154 case OPTION_DWARF_DEPTH:
6155 {
6156 char *cp;
6157 dwarf_cutoff_level = strtoul (optarg, & cp, 0);
6158 }
6159 break;
6160 case OPTION_DWARF_START:
6161 {
6162 char *cp;
6163 dwarf_start_die = strtoul (optarg, & cp, 0);
6164 suppress_bfd_header = 1;
6165 }
6166 break;
4723351a 6167 case OPTION_DWARF_CHECK:
015dc7e1 6168 dwarf_check = true;
4723351a 6169 break;
094e34f2 6170#ifdef ENABLE_LIBCTF
d344b407 6171 case OPTION_CTF:
015dc7e1 6172 dump_ctf_section_info = true;
10909ea8
NA
6173 if (optarg)
6174 dump_ctf_section_name = xstrdup (optarg);
015dc7e1 6175 seenflag = true;
d344b407 6176 break;
7d9813f1
NA
6177 case OPTION_CTF_PARENT:
6178 dump_ctf_parent_name = xstrdup (optarg);
6179 break;
094e34f2 6180#endif
42b6953b
IB
6181 case OPTION_SFRAME:
6182 dump_sframe_section_info = true;
6183 if (optarg)
6184 dump_sframe_section_name = xstrdup (optarg);
6185 seenflag = true;
6186 break;
1dada9c5 6187 case 'G':
015dc7e1
AM
6188 dump_stab_section_info = true;
6189 seenflag = true;
252b5132
RH
6190 break;
6191 case 's':
015dc7e1
AM
6192 dump_section_contents = true;
6193 seenflag = true;
252b5132
RH
6194 break;
6195 case 'r':
015dc7e1
AM
6196 dump_reloc_info = true;
6197 seenflag = true;
252b5132
RH
6198 break;
6199 case 'R':
015dc7e1
AM
6200 dump_dynamic_reloc_info = true;
6201 seenflag = true;
252b5132
RH
6202 break;
6203 case 'a':
015dc7e1
AM
6204 dump_ar_hdrs = true;
6205 seenflag = true;
252b5132
RH
6206 break;
6207 case 'h':
015dc7e1
AM
6208 dump_section_headers = true;
6209 seenflag = true;
252b5132 6210 break;
8b53311e 6211 case 'v':
252b5132 6212 case 'V':
015dc7e1
AM
6213 show_version = true;
6214 seenflag = true;
252b5132 6215 break;
0af11b59 6216
b3aa80b4
NC
6217 case 'U':
6218 if (streq (optarg, "default") || streq (optarg, "d"))
6219 unicode_display = unicode_default;
6220 else if (streq (optarg, "locale") || streq (optarg, "l"))
6221 unicode_display = unicode_locale;
6222 else if (streq (optarg, "escape") || streq (optarg, "e"))
6223 unicode_display = unicode_escape;
6224 else if (streq (optarg, "invalid") || streq (optarg, "i"))
6225 unicode_display = unicode_invalid;
6226 else if (streq (optarg, "hex") || streq (optarg, "x"))
6227 unicode_display = unicode_hex;
6228 else if (streq (optarg, "highlight") || streq (optarg, "h"))
6229 unicode_display = unicode_highlight;
6230 else
6231 fatal (_("invalid argument to -U/--unicode: %s"), optarg);
6232 break;
6233
aebcf7b7
NC
6234 case 'H':
6235 usage (stdout, 0);
6236 /* No need to set seenflag or to break - usage() does not return. */
252b5132
RH
6237 default:
6238 usage (stderr, 1);
6239 }
6240 }
6241
a88c79b7
NC
6242 if (disassembler_color == on_if_terminal_output)
6243 disassembler_color = isatty (1) ? on : off;
6244
252b5132
RH
6245 if (show_version)
6246 print_version ("objdump");
6247
b34976b6 6248 if (!seenflag)
1dada9c5 6249 usage (stderr, 2);
252b5132 6250
e1dbfc17
L
6251 dump_any_debugging = (dump_debugging
6252 || dump_dwarf_section_info
e9a241e8
AM
6253 || process_links
6254 || with_source_code);
e1dbfc17 6255
252b5132 6256 if (formats_info)
06d86cf7 6257 exit_status = display_info ();
252b5132
RH
6258 else
6259 {
6260 if (optind == argc)
015dc7e1 6261 display_file ("a.out", target, true);
252b5132
RH
6262 else
6263 for (; optind < argc;)
cd6581da
NC
6264 {
6265 display_file (argv[optind], target, optind == argc - 1);
6266 optind++;
6267 }
252b5132
RH
6268 }
6269
70ecb384 6270 free_only_list ();
7d9813f1
NA
6271 free (dump_ctf_section_name);
6272 free (dump_ctf_parent_name);
a1c110a3 6273 free ((void *) source_comment);
79b377b3 6274
75cd796a 6275 return exit_status;
252b5132 6276}