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