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