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