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