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