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