PR26337, Malloc size error in objdump
[deliverable/binutils-gdb.git] / binutils / objdump.c
CommitLineData
252b5132 1/* objdump.c -- dump information about an object file.
b3adc24a 2 Copyright (C) 1990-2020 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 {
343dbc36
L
3005 bfd_signed_vma addend = q->addend;
3006 if (addend < 0)
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);
0b97e818 3548 section->start = NULL;
dda8d76d 3549 }
365544c3 3550
dda8d76d 3551 section->filename = bfd_get_filename (abfd);
d1c4b12b 3552 section->reloc_info = NULL;
3aade688 3553 section->num_relocs = 0;
fd361982 3554 section->address = bfd_section_vma (sec);
11fa9f13 3555 section->user_data = sec;
fd361982 3556 section->size = bfd_section_size (sec);
63455780
NC
3557 /* PR 24360: On 32-bit hosts sizeof (size_t) < sizeof (bfd_size_type). */
3558 alloced = amt = section->size + 1;
3559 if (alloced != amt || alloced == 0)
11fa9f13 3560 {
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 }
0b97e818 3567 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
365544c3
L
3568 {
3569 free_debug_section (debug);
3570 printf (_("\nCan't get contents for section '%s'.\n"),
12add40e 3571 sanitize_string (section->name));
dda8d76d 3572 return FALSE;
1b315056 3573 }
0b97e818 3574 section->start = contents;
4f1881b9
AM
3575 /* Ensure any string section has a terminating NUL. */
3576 section->start[section->size] = 0;
1b315056 3577
2e8136f9
NC
3578 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
3579 && debug_displays [debug].relocate)
0acf065b 3580 {
dda8d76d
NC
3581 long reloc_size;
3582 bfd_boolean ret;
3583
8a72cc6e 3584 bfd_cache_section_contents (sec, section->start);
0acf065b
CC
3585
3586 ret = bfd_simple_get_relocated_section_contents (abfd,
3587 sec,
3588 section->start,
3589 syms) != NULL;
3590
3591 if (! ret)
3592 {
3593 free_debug_section (debug);
3594 printf (_("\nCan't get contents for section '%s'.\n"),
12add40e 3595 sanitize_string (section->name));
dda8d76d 3596 return FALSE;
0acf065b 3597 }
d1c4b12b 3598
d1c4b12b
NC
3599 reloc_size = bfd_get_reloc_upper_bound (abfd, sec);
3600 if (reloc_size > 0)
3601 {
3602 unsigned long reloc_count;
3603 arelent **relocs;
3604
3605 relocs = (arelent **) xmalloc (reloc_size);
3606
3607 reloc_count = bfd_canonicalize_reloc (abfd, sec, relocs, NULL);
3608 if (reloc_count == 0)
3609 free (relocs);
3610 else
3611 {
3612 section->reloc_info = relocs;
3613 section->num_relocs = reloc_count;
3614 }
3615 }
bdc4de1b 3616 }
0acf065b 3617
dda8d76d 3618 return TRUE;
7bcbeb0f
CC
3619}
3620
d1c4b12b
NC
3621bfd_boolean
3622reloc_at (struct dwarf_section * dsec, dwarf_vma offset)
3623{
3624 arelent ** relocs;
3625 arelent * rp;
3626
3627 if (dsec == NULL || dsec->reloc_info == NULL)
3628 return FALSE;
3629
3630 relocs = (arelent **) dsec->reloc_info;
3631
3632 for (; (rp = * relocs) != NULL; ++ relocs)
3633 if (rp->address == offset)
3634 return TRUE;
3635
3636 return FALSE;
3637}
3638
dda8d76d 3639bfd_boolean
7bcbeb0f
CC
3640load_debug_section (enum dwarf_section_display_enum debug, void *file)
3641{
3642 struct dwarf_section *section = &debug_displays [debug].section;
3f5e193b 3643 bfd *abfd = (bfd *) file;
7bcbeb0f
CC
3644 asection *sec;
3645
3646 /* If it is already loaded, do nothing. */
3647 if (section->start != NULL)
dda8d76d
NC
3648 {
3649 if (streq (section->filename, bfd_get_filename (abfd)))
3650 return TRUE;
3651 }
7bcbeb0f
CC
3652
3653 /* Locate the debug section. */
3654 sec = bfd_get_section_by_name (abfd, section->uncompressed_name);
3655 if (sec != NULL)
3656 section->name = section->uncompressed_name;
3657 else
3658 {
3659 sec = bfd_get_section_by_name (abfd, section->compressed_name);
3660 if (sec != NULL)
3661 section->name = section->compressed_name;
3662 }
3663 if (sec == NULL)
dda8d76d 3664 return FALSE;
7bcbeb0f
CC
3665
3666 return load_specific_debug_section (debug, sec, file);
365544c3
L
3667}
3668
3669void
3670free_debug_section (enum dwarf_section_display_enum debug)
3671{
3672 struct dwarf_section *section = &debug_displays [debug].section;
3673
3674 if (section->start == NULL)
3675 return;
3676
06614111
NC
3677 /* PR 17512: file: 0f67f69d. */
3678 if (section->user_data != NULL)
3679 {
3680 asection * sec = (asection *) section->user_data;
3681
3682 /* If we are freeing contents that are also pointed to by the BFD
3683 library's section structure then make sure to update those pointers
3684 too. Otherwise, the next time we try to load data for this section
3685 we can end up using a stale pointer. */
3686 if (section->start == sec->contents)
3687 {
3688 sec->contents = NULL;
3689 sec->flags &= ~ SEC_IN_MEMORY;
db6b071a 3690 sec->compress_status = COMPRESS_SECTION_NONE;
06614111
NC
3691 }
3692 }
3693
365544c3
L
3694 free ((char *) section->start);
3695 section->start = NULL;
3696 section->address = 0;
3697 section->size = 0;
3698}
3699
dda8d76d
NC
3700void
3701close_debug_file (void * file)
3702{
3703 bfd * abfd = (bfd *) file;
3704
3705 bfd_close (abfd);
3706}
3707
3708void *
3709open_debug_file (const char * pathname)
3710{
3711 bfd * data;
3712
3713 data = bfd_openr (pathname, NULL);
3714 if (data == NULL)
3715 return NULL;
3716
3717 if (! bfd_check_format (data, bfd_object))
3718 return NULL;
3719
3720 return data;
3721}
3722
301a9420
AM
3723#if HAVE_LIBDEBUGINFOD
3724/* Return a hex string represention of the build-id. */
3725
3726unsigned char *
3727get_build_id (void * data)
3728{
3729 unsigned i;
3730 char * build_id_str;
3731 bfd * abfd = (bfd *) data;
3732 const struct bfd_build_id * build_id;
3733
3734 build_id = abfd->build_id;
3735 if (build_id == NULL)
3736 return NULL;
3737
3738 build_id_str = malloc (build_id->size * 2 + 1);
3739 if (build_id_str == NULL)
3740 return NULL;
3741
3742 for (i = 0; i < build_id->size; i++)
3743 sprintf (build_id_str + (i * 2), "%02x", build_id->data[i]);
3744 build_id_str[build_id->size * 2] = '\0';
3745
3746 return (unsigned char *)build_id_str;
3747}
3748#endif /* HAVE_LIBDEBUGINFOD */
3749
365544c3
L
3750static void
3751dump_dwarf_section (bfd *abfd, asection *section,
3752 void *arg ATTRIBUTE_UNUSED)
3753{
fd361982 3754 const char *name = bfd_section_name (section);
365544c3 3755 const char *match;
3f5e193b 3756 int i;
365544c3 3757
0112cd26 3758 if (CONST_STRNEQ (name, ".gnu.linkonce.wi."))
365544c3
L
3759 match = ".debug_info";
3760 else
3761 match = name;
3762
3763 for (i = 0; i < max; i++)
4cb93e3b
TG
3764 if ((strcmp (debug_displays [i].section.uncompressed_name, match) == 0
3765 || strcmp (debug_displays [i].section.compressed_name, match) == 0)
3766 && debug_displays [i].enabled != NULL
3767 && *debug_displays [i].enabled)
365544c3 3768 {
c8450da8 3769 struct dwarf_section *sec = &debug_displays [i].section;
365544c3 3770
c8450da8
TG
3771 if (strcmp (sec->uncompressed_name, match) == 0)
3772 sec->name = sec->uncompressed_name;
3773 else
3774 sec->name = sec->compressed_name;
3f5e193b
NC
3775 if (load_specific_debug_section ((enum dwarf_section_display_enum) i,
3776 section, abfd))
c8450da8
TG
3777 {
3778 debug_displays [i].display (sec, abfd);
3aade688 3779
c8450da8 3780 if (i != info && i != abbrev)
3f5e193b 3781 free_debug_section ((enum dwarf_section_display_enum) i);
365544c3
L
3782 }
3783 break;
3784 }
3785}
3786
3787/* Dump the dwarf debugging information. */
3788
3789static void
3790dump_dwarf (bfd *abfd)
3791{
39f0547e
NC
3792 /* The byte_get pointer should have been set at the start of dump_bfd(). */
3793 if (byte_get == NULL)
f41e4712
NC
3794 {
3795 warn (_("File %s does not contain any dwarf debug information\n"),
3796 bfd_get_filename (abfd));
3797 return;
3798 }
365544c3 3799
b129eb0e 3800 switch (bfd_get_arch (abfd))
2dc4cec1 3801 {
8ab159a9
AM
3802 case bfd_arch_s12z:
3803 /* S12Z has a 24 bit address space. But the only known
3804 producer of dwarf_info encodes addresses into 32 bits. */
3805 eh_addr_size = 4;
3806 break;
3807
b129eb0e 3808 default:
229a22cf 3809 eh_addr_size = bfd_arch_bits_per_address (abfd) / 8;
b129eb0e 3810 break;
2dc4cec1
L
3811 }
3812
229a22cf
AB
3813 init_dwarf_regnames_by_bfd_arch_and_mach (bfd_get_arch (abfd),
3814 bfd_get_mach (abfd));
3815
365544c3 3816 bfd_map_over_sections (abfd, dump_dwarf_section, NULL);
365544c3
L
3817}
3818\f
29ca8dc5
NS
3819/* Read ABFD's stabs section STABSECT_NAME, and return a pointer to
3820 it. Return NULL on failure. */
252b5132 3821
bae7501e 3822static bfd_byte *
7d9813f1
NA
3823read_section_stabs (bfd *abfd, const char *sect_name, bfd_size_type *size_ptr,
3824 bfd_size_type *entsize_ptr)
252b5132 3825{
29ca8dc5 3826 asection *stabsect;
bae7501e 3827 bfd_byte *contents;
252b5132 3828
29ca8dc5 3829 stabsect = bfd_get_section_by_name (abfd, sect_name);
155e0d23 3830 if (stabsect == NULL)
252b5132 3831 {
12add40e
NC
3832 printf (_("No %s section present\n\n"),
3833 sanitize_string (sect_name));
b34976b6 3834 return FALSE;
252b5132
RH
3835 }
3836
bae7501e 3837 if (!bfd_malloc_and_get_section (abfd, stabsect, &contents))
252b5132 3838 {
a8c62f1c 3839 non_fatal (_("reading %s section of %s failed: %s"),
29ca8dc5 3840 sect_name, bfd_get_filename (abfd),
37cc8ec1 3841 bfd_errmsg (bfd_get_error ()));
75cd796a 3842 exit_status = 1;
a8c62f1c 3843 free (contents);
29ca8dc5 3844 return NULL;
252b5132
RH
3845 }
3846
fd361982 3847 *size_ptr = bfd_section_size (stabsect);
7d9813f1
NA
3848 if (entsize_ptr)
3849 *entsize_ptr = stabsect->entsize;
252b5132 3850
29ca8dc5 3851 return contents;
252b5132
RH
3852}
3853
3854/* Stabs entries use a 12 byte format:
3855 4 byte string table index
3856 1 byte stab type
3857 1 byte stab other field
3858 2 byte stab desc field
3859 4 byte stab value
3860 FIXME: This will have to change for a 64 bit object format. */
3861
46dca2e0
NC
3862#define STRDXOFF (0)
3863#define TYPEOFF (4)
3864#define OTHEROFF (5)
3865#define DESCOFF (6)
3866#define VALOFF (8)
252b5132
RH
3867#define STABSIZE (12)
3868
3869/* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
3870 using string table section STRSECT_NAME (in `strtab'). */
3871
3872static void
3b9ad1cc
AM
3873print_section_stabs (bfd *abfd,
3874 const char *stabsect_name,
3875 unsigned *string_offset_ptr)
252b5132
RH
3876{
3877 int i;
46dca2e0 3878 unsigned file_string_table_offset = 0;
29ca8dc5 3879 unsigned next_file_string_table_offset = *string_offset_ptr;
252b5132
RH
3880 bfd_byte *stabp, *stabs_end;
3881
3882 stabp = stabs;
3883 stabs_end = stabp + stab_size;
3884
12add40e 3885 printf (_("Contents of %s section:\n\n"), sanitize_string (stabsect_name));
252b5132
RH
3886 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
3887
3888 /* Loop through all symbols and print them.
3889
3890 We start the index at -1 because there is a dummy symbol on
3891 the front of stabs-in-{coff,elf} sections that supplies sizes. */
f41e4712 3892 for (i = -1; stabp <= stabs_end - STABSIZE; stabp += STABSIZE, i++)
252b5132
RH
3893 {
3894 const char *name;
3895 unsigned long strx;
3896 unsigned char type, other;
3897 unsigned short desc;
3898 bfd_vma value;
3899
3900 strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
3901 type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
3902 other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
3903 desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
3904 value = bfd_h_get_32 (abfd, stabp + VALOFF);
3905
3906 printf ("\n%-6d ", i);
3907 /* Either print the stab name, or, if unnamed, print its number
0af11b59 3908 again (makes consistent formatting for tools like awk). */
252b5132
RH
3909 name = bfd_get_stab_name (type);
3910 if (name != NULL)
12add40e 3911 printf ("%-6s", sanitize_string (name));
252b5132
RH
3912 else if (type == N_UNDF)
3913 printf ("HdrSym");
3914 else
3915 printf ("%-6d", type);
3916 printf (" %-6d %-6d ", other, desc);
d8180c76 3917 bfd_printf_vma (abfd, value);
252b5132
RH
3918 printf (" %-6lu", strx);
3919
3920 /* Symbols with type == 0 (N_UNDF) specify the length of the
3921 string table associated with this file. We use that info
3922 to know how to relocate the *next* file's string table indices. */
252b5132
RH
3923 if (type == N_UNDF)
3924 {
3925 file_string_table_offset = next_file_string_table_offset;
3926 next_file_string_table_offset += value;
3927 }
3928 else
3929 {
f41e4712
NC
3930 bfd_size_type amt = strx + file_string_table_offset;
3931
252b5132
RH
3932 /* Using the (possibly updated) string table offset, print the
3933 string (if any) associated with this symbol. */
f41e4712 3934 if (amt < stabstr_size)
12add40e
NC
3935 /* PR 17512: file: 079-79389-0.001:0.1.
3936 FIXME: May need to sanitize this string before displaying. */
f41e4712 3937 printf (" %.*s", (int)(stabstr_size - amt), strtab + amt);
252b5132
RH
3938 else
3939 printf (" *");
3940 }
3941 }
3942 printf ("\n\n");
29ca8dc5 3943 *string_offset_ptr = next_file_string_table_offset;
252b5132
RH
3944}
3945
155e0d23
NC
3946typedef struct
3947{
3948 const char * section_name;
3949 const char * string_section_name;
29ca8dc5 3950 unsigned string_offset;
155e0d23
NC
3951}
3952stab_section_names;
3953
252b5132 3954static void
155e0d23 3955find_stabs_section (bfd *abfd, asection *section, void *names)
252b5132 3956{
155e0d23
NC
3957 int len;
3958 stab_section_names * sought = (stab_section_names *) names;
252b5132
RH
3959
3960 /* Check for section names for which stabsect_name is a prefix, to
29ca8dc5 3961 handle .stab.N, etc. */
155e0d23
NC
3962 len = strlen (sought->section_name);
3963
3964 /* If the prefix matches, and the files section name ends with a
3965 nul or a digit, then we match. I.e., we want either an exact
3966 match or a section followed by a number. */
3967 if (strncmp (sought->section_name, section->name, len) == 0
3968 && (section->name[len] == 0
29ca8dc5 3969 || (section->name[len] == '.' && ISDIGIT (section->name[len + 1]))))
252b5132 3970 {
29ca8dc5
NS
3971 if (strtab == NULL)
3972 strtab = read_section_stabs (abfd, sought->string_section_name,
7d9813f1 3973 &stabstr_size, NULL);
3aade688 3974
29ca8dc5 3975 if (strtab)
252b5132 3976 {
7d9813f1 3977 stabs = read_section_stabs (abfd, section->name, &stab_size, NULL);
29ca8dc5
NS
3978 if (stabs)
3979 print_section_stabs (abfd, section->name, &sought->string_offset);
252b5132
RH
3980 }
3981 }
3982}
98a91d6a 3983
155e0d23
NC
3984static void
3985dump_stabs_section (bfd *abfd, char *stabsect_name, char *strsect_name)
3986{
3987 stab_section_names s;
3988
3989 s.section_name = stabsect_name;
3990 s.string_section_name = strsect_name;
29ca8dc5
NS
3991 s.string_offset = 0;
3992
155e0d23 3993 bfd_map_over_sections (abfd, find_stabs_section, & s);
29ca8dc5
NS
3994
3995 free (strtab);
3996 strtab = NULL;
155e0d23
NC
3997}
3998
3999/* Dump the any sections containing stabs debugging information. */
4000
4001static void
4002dump_stabs (bfd *abfd)
4003{
4004 dump_stabs_section (abfd, ".stab", ".stabstr");
4005 dump_stabs_section (abfd, ".stab.excl", ".stab.exclstr");
4006 dump_stabs_section (abfd, ".stab.index", ".stab.indexstr");
62bb81b8
TG
4007
4008 /* For Darwin. */
4009 dump_stabs_section (abfd, "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr");
4010
155e0d23
NC
4011 dump_stabs_section (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
4012}
252b5132
RH
4013\f
4014static void
46dca2e0 4015dump_bfd_header (bfd *abfd)
252b5132
RH
4016{
4017 char *comma = "";
4018
4019 printf (_("architecture: %s, "),
4020 bfd_printable_arch_mach (bfd_get_arch (abfd),
4021 bfd_get_mach (abfd)));
6b6bc957 4022 printf (_("flags 0x%08x:\n"), abfd->flags & ~BFD_FLAGS_FOR_BFD_USE_MASK);
252b5132 4023
82a9ed20 4024#define PF(x, y) if (abfd->flags & x) {printf ("%s%s", comma, y); comma=", ";}
252b5132
RH
4025 PF (HAS_RELOC, "HAS_RELOC");
4026 PF (EXEC_P, "EXEC_P");
4027 PF (HAS_LINENO, "HAS_LINENO");
4028 PF (HAS_DEBUG, "HAS_DEBUG");
4029 PF (HAS_SYMS, "HAS_SYMS");
4030 PF (HAS_LOCALS, "HAS_LOCALS");
4031 PF (DYNAMIC, "DYNAMIC");
4032 PF (WP_TEXT, "WP_TEXT");
4033 PF (D_PAGED, "D_PAGED");
4034 PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
4035 printf (_("\nstart address 0x"));
d8180c76 4036 bfd_printf_vma (abfd, abfd->start_address);
252b5132
RH
4037 printf ("\n");
4038}
7d9813f1
NA
4039\f
4040
094e34f2 4041#ifdef ENABLE_LIBCTF
7d9813f1
NA
4042/* Formatting callback function passed to ctf_dump. Returns either the pointer
4043 it is passed, or a pointer to newly-allocated storage, in which case
4044 dump_ctf() will free it when it no longer needs it. */
4045
4046static char *
4047dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
4048 char *s, void *arg)
4049{
63160fc9 4050 const char *blanks = arg;
7d9813f1
NA
4051 char *new_s;
4052
63160fc9 4053 if (asprintf (&new_s, "%s%s", blanks, s) < 0)
7d9813f1
NA
4054 return s;
4055 return new_s;
4056}
4057
4058/* Make a ctfsect suitable for ctf_bfdopen_ctfsect(). */
4059static ctf_sect_t
4060make_ctfsect (const char *name, bfd_byte *data,
4061 bfd_size_type size)
4062{
4063 ctf_sect_t ctfsect;
4064
4065 ctfsect.cts_name = name;
7d9813f1 4066 ctfsect.cts_entsize = 1;
7d9813f1
NA
4067 ctfsect.cts_size = size;
4068 ctfsect.cts_data = data;
4069
4070 return ctfsect;
4071}
4072
4073/* Dump one CTF archive member. */
4074
4075static int
4076dump_ctf_archive_member (ctf_file_t *ctf, const char *name, void *arg)
4077{
4078 ctf_file_t *parent = (ctf_file_t *) arg;
9b32cba4
NA
4079 const char *things[] = {"Header", "Labels", "Data objects",
4080 "Function objects", "Variables", "Types", "Strings",
4081 ""};
7d9813f1 4082 const char **thing;
8b37e7b6
NA
4083 ctf_next_t *it = NULL;
4084 char *errtext;
4085 int is_warning;
7d9813f1
NA
4086 size_t i;
4087
4088 /* Only print out the name of non-default-named archive members.
4089 The name .ctf appears everywhere, even for things that aren't
fd86991b
NA
4090 really archives, so printing it out is liable to be confusing.
4091
4092 The parent, if there is one, is the default-owned archive member:
4093 avoid importing it into itself. (This does no harm, but looks
4094 confusing.) */
4095
7d9813f1 4096 if (strcmp (name, ".ctf") != 0)
fd86991b
NA
4097 {
4098 printf (_("\nCTF archive member: %s:\n"), sanitize_string (name));
4099 ctf_import (ctf, parent);
4100 }
7d9813f1 4101
9b32cba4 4102 for (i = 0, thing = things; *thing[0]; thing++, i++)
7d9813f1
NA
4103 {
4104 ctf_dump_state_t *s = NULL;
4105 char *item;
4106
4107 printf ("\n %s:\n", *thing);
4108 while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
4109 (void *) " ")) != NULL)
4110 {
4111 printf ("%s\n", item);
4112 free (item);
4113 }
4114
4115 if (ctf_errno (ctf))
4116 {
3dd6b890 4117 non_fatal (_("Iteration failed: %s, %s"), *thing,
7d9813f1
NA
4118 ctf_errmsg (ctf_errno (ctf)));
4119 break;
4120 }
4121 }
8b37e7b6
NA
4122
4123 /* Dump accumulated errors and warnings. */
4124 while ((errtext = ctf_errwarning_next (ctf, &it, &is_warning)) != NULL)
4125 {
4126 non_fatal (_("%s: `%s'"), is_warning ? _("warning"): _("error"),
4127 errtext);
4128 free (errtext);
4129 }
4130 if (ctf_errno (ctf) != ECTF_NEXT_END)
4131 {
4132 non_fatal (_("CTF error: cannot get CTF errors: `%s'"),
4133 ctf_errmsg (ctf_errno (ctf)));
4134 }
4135
7d9813f1
NA
4136 return 0;
4137}
4138
4139/* Dump the CTF debugging information. */
4140
4141static void
4142dump_ctf (bfd *abfd, const char *sect_name, const char *parent_name)
4143{
fd86991b 4144 ctf_archive_t *ctfa, *parenta = NULL, *lookparent;
7d9813f1
NA
4145 bfd_byte *ctfdata, *parentdata = NULL;
4146 bfd_size_type ctfsize, parentsize;
4147 ctf_sect_t ctfsect;
4148 ctf_file_t *parent = NULL;
4149 int err;
4150
4151 if ((ctfdata = read_section_stabs (abfd, sect_name, &ctfsize, NULL)) == NULL)
4152 bfd_fatal (bfd_get_filename (abfd));
4153
4154 if (parent_name
4155 && (parentdata = read_section_stabs (abfd, parent_name, &parentsize,
4156 NULL)) == NULL)
4157 bfd_fatal (bfd_get_filename (abfd));
4158
4159 /* Load the CTF file and dump it. */
4160
4161 ctfsect = make_ctfsect (sect_name, ctfdata, ctfsize);
4162 if ((ctfa = ctf_bfdopen_ctfsect (abfd, &ctfsect, &err)) == NULL)
4163 {
3dd6b890 4164 non_fatal (_("CTF open failure: %s"), ctf_errmsg (err));
7d9813f1
NA
4165 bfd_fatal (bfd_get_filename (abfd));
4166 }
4167
4168 if (parentdata)
4169 {
4170 ctfsect = make_ctfsect (parent_name, parentdata, parentsize);
4171 if ((parenta = ctf_bfdopen_ctfsect (abfd, &ctfsect, &err)) == NULL)
4172 {
3dd6b890 4173 non_fatal (_("CTF open failure: %s"), ctf_errmsg (err));
7d9813f1
NA
4174 bfd_fatal (bfd_get_filename (abfd));
4175 }
4176
fd86991b
NA
4177 lookparent = parenta;
4178 }
4179 else
4180 lookparent = ctfa;
4181
4182 /* Assume that the applicable parent archive member is the default one.
4183 (This is what all known implementations are expected to do, if they
4184 put CTFs and their parents in archives together.) */
4185 if ((parent = ctf_arc_open_by_name (lookparent, NULL, &err)) == NULL)
4186 {
3dd6b890 4187 non_fatal (_("CTF open failure: %s"), ctf_errmsg (err));
fd86991b 4188 bfd_fatal (bfd_get_filename (abfd));
7d9813f1
NA
4189 }
4190
4191 printf (_("Contents of CTF section %s:\n"), sanitize_string (sect_name));
4192
4193 ctf_archive_iter (ctfa, dump_ctf_archive_member, parent);
4194 ctf_file_close (parent);
4195 ctf_close (ctfa);
4196 ctf_close (parenta);
4197 free (parentdata);
4198 free (ctfdata);
4199}
094e34f2
NA
4200#else
4201static void
4202dump_ctf (bfd *abfd ATTRIBUTE_UNUSED, const char *sect_name ATTRIBUTE_UNUSED,
4203 const char *parent_name ATTRIBUTE_UNUSED) {}
4204#endif
98a91d6a 4205
79b377b3 4206\f
252b5132 4207static void
46dca2e0 4208dump_bfd_private_header (bfd *abfd)
252b5132 4209{
7d272a55
AM
4210 if (!bfd_print_private_bfd_data (abfd, stdout))
4211 non_fatal (_("warning: private headers incomplete: %s"),
4212 bfd_errmsg (bfd_get_error ()));
252b5132
RH
4213}
4214
6abcee90
TG
4215static void
4216dump_target_specific (bfd *abfd)
4217{
4218 const struct objdump_private_desc * const *desc;
4219 struct objdump_private_option *opt;
4220 char *e, *b;
4221
4222 /* Find the desc. */
4223 for (desc = objdump_private_vectors; *desc != NULL; desc++)
4224 if ((*desc)->filter (abfd))
4225 break;
4226
c32d6f7b 4227 if (*desc == NULL)
6abcee90
TG
4228 {
4229 non_fatal (_("option -P/--private not supported by this file"));
4230 return;
4231 }
4232
4233 /* Clear all options. */
4234 for (opt = (*desc)->options; opt->name; opt++)
4235 opt->selected = FALSE;
4236
4237 /* Decode options. */
4238 b = dump_private_options;
4239 do
4240 {
4241 e = strchr (b, ',');
4242
4243 if (e)
4244 *e = 0;
4245
4246 for (opt = (*desc)->options; opt->name; opt++)
4247 if (strcmp (opt->name, b) == 0)
4248 {
4249 opt->selected = TRUE;
4250 break;
4251 }
4252 if (opt->name == NULL)
4253 non_fatal (_("target specific dump '%s' not supported"), b);
4254
4255 if (e)
4256 {
4257 *e = ',';
4258 b = e + 1;
4259 }
4260 }
4261 while (e != NULL);
4262
4263 /* Dump. */
4264 (*desc)->dump (abfd);
4265}
155e0d23
NC
4266\f
4267/* Display a section in hexadecimal format with associated characters.
4268 Each line prefixed by the zero padded address. */
d24de309 4269
252b5132 4270static void
155e0d23 4271dump_section (bfd *abfd, asection *section, void *dummy ATTRIBUTE_UNUSED)
252b5132 4272{
cfd14a50 4273 bfd_byte *data = NULL;
155e0d23 4274 bfd_size_type datasize;
bdc4de1b
NC
4275 bfd_vma addr_offset;
4276 bfd_vma start_offset;
4277 bfd_vma stop_offset;
61826503 4278 unsigned int opb = bfd_octets_per_byte (abfd, section);
155e0d23
NC
4279 /* Bytes per line. */
4280 const int onaline = 16;
4281 char buf[64];
4282 int count;
4283 int width;
4284
8fab9282 4285 if (! process_section_p (section))
155e0d23
NC
4286 return;
4287
8fab9282 4288 if ((section->flags & SEC_HAS_CONTENTS) == 0)
155e0d23 4289 return;
3aade688 4290
fd361982 4291 if ((datasize = bfd_section_size (section)) == 0)
155e0d23
NC
4292 return;
4293
155e0d23
NC
4294 /* Compute the address range to display. */
4295 if (start_address == (bfd_vma) -1
4296 || start_address < section->vma)
4297 start_offset = 0;
4298 else
4299 start_offset = start_address - section->vma;
4300
4301 if (stop_address == (bfd_vma) -1)
4302 stop_offset = datasize / opb;
4303 else
252b5132 4304 {
155e0d23
NC
4305 if (stop_address < section->vma)
4306 stop_offset = 0;
4307 else
4308 stop_offset = stop_address - section->vma;
252b5132 4309
155e0d23
NC
4310 if (stop_offset > datasize / opb)
4311 stop_offset = datasize / opb;
252b5132
RH
4312 }
4313
32760852
NC
4314 if (start_offset >= stop_offset)
4315 return;
3aade688 4316
12add40e 4317 printf (_("Contents of section %s:"), sanitize_string (section->name));
32760852 4318 if (display_file_offsets)
0af1713e
AM
4319 printf (_(" (Starting at file offset: 0x%lx)"),
4320 (unsigned long) (section->filepos + start_offset));
32760852
NC
4321 printf ("\n");
4322
4a114e3e
L
4323 if (!bfd_get_full_section_contents (abfd, section, &data))
4324 {
0821d5b1
NC
4325 non_fatal (_("Reading section %s failed because: %s"),
4326 section->name, bfd_errmsg (bfd_get_error ()));
4a114e3e
L
4327 return;
4328 }
32760852 4329
155e0d23 4330 width = 4;
026df7c5 4331
155e0d23
NC
4332 bfd_sprintf_vma (abfd, buf, start_offset + section->vma);
4333 if (strlen (buf) >= sizeof (buf))
4334 abort ();
026df7c5 4335
155e0d23
NC
4336 count = 0;
4337 while (buf[count] == '0' && buf[count+1] != '\0')
4338 count++;
4339 count = strlen (buf) - count;
4340 if (count > width)
4341 width = count;
252b5132 4342
155e0d23
NC
4343 bfd_sprintf_vma (abfd, buf, stop_offset + section->vma - 1);
4344 if (strlen (buf) >= sizeof (buf))
4345 abort ();
026df7c5 4346
155e0d23
NC
4347 count = 0;
4348 while (buf[count] == '0' && buf[count+1] != '\0')
4349 count++;
4350 count = strlen (buf) - count;
4351 if (count > width)
4352 width = count;
026df7c5 4353
155e0d23
NC
4354 for (addr_offset = start_offset;
4355 addr_offset < stop_offset; addr_offset += onaline / opb)
d24de309 4356 {
155e0d23 4357 bfd_size_type j;
d24de309 4358
155e0d23
NC
4359 bfd_sprintf_vma (abfd, buf, (addr_offset + section->vma));
4360 count = strlen (buf);
4361 if ((size_t) count >= sizeof (buf))
4362 abort ();
d24de309 4363
155e0d23
NC
4364 putchar (' ');
4365 while (count < width)
252b5132 4366 {
155e0d23
NC
4367 putchar ('0');
4368 count++;
4369 }
4370 fputs (buf + count - width, stdout);
4371 putchar (' ');
252b5132 4372
155e0d23
NC
4373 for (j = addr_offset * opb;
4374 j < addr_offset * opb + onaline; j++)
4375 {
4376 if (j < stop_offset * opb)
4377 printf ("%02x", (unsigned) (data[j]));
4378 else
4379 printf (" ");
4380 if ((j & 3) == 3)
4381 printf (" ");
252b5132
RH
4382 }
4383
155e0d23
NC
4384 printf (" ");
4385 for (j = addr_offset * opb;
4386 j < addr_offset * opb + onaline; j++)
4387 {
4388 if (j >= stop_offset * opb)
4389 printf (" ");
4390 else
4391 printf ("%c", ISPRINT (data[j]) ? data[j] : '.');
4392 }
4393 putchar ('\n');
252b5132 4394 }
155e0d23 4395 free (data);
252b5132 4396}
155e0d23 4397
98a91d6a 4398/* Actually display the various requested regions. */
252b5132
RH
4399
4400static void
46dca2e0 4401dump_data (bfd *abfd)
252b5132 4402{
155e0d23 4403 bfd_map_over_sections (abfd, dump_section, NULL);
252b5132
RH
4404}
4405
98a91d6a
NC
4406/* Should perhaps share code and display with nm? */
4407
252b5132 4408static void
46dca2e0 4409dump_symbols (bfd *abfd ATTRIBUTE_UNUSED, bfd_boolean dynamic)
252b5132
RH
4410{
4411 asymbol **current;
91d6fa6a 4412 long max_count;
252b5132
RH
4413 long count;
4414
4415 if (dynamic)
4416 {
4417 current = dynsyms;
91d6fa6a 4418 max_count = dynsymcount;
252b5132
RH
4419 printf ("DYNAMIC SYMBOL TABLE:\n");
4420 }
4421 else
4422 {
4423 current = syms;
91d6fa6a 4424 max_count = symcount;
252b5132
RH
4425 printf ("SYMBOL TABLE:\n");
4426 }
4427
91d6fa6a 4428 if (max_count == 0)
a1df01d1
AM
4429 printf (_("no symbols\n"));
4430
91d6fa6a 4431 for (count = 0; count < max_count; count++)
252b5132 4432 {
155e0d23
NC
4433 bfd *cur_bfd;
4434
4435 if (*current == NULL)
83ef0798 4436 printf (_("no information for symbol number %ld\n"), count);
155e0d23
NC
4437
4438 else if ((cur_bfd = bfd_asymbol_bfd (*current)) == NULL)
83ef0798 4439 printf (_("could not determine the type of symbol number %ld\n"),
155e0d23
NC
4440 count);
4441
661f7c35
NC
4442 else if (process_section_p ((* current)->section)
4443 && (dump_special_syms
4444 || !bfd_is_target_special_symbol (cur_bfd, *current)))
252b5132 4445 {
155e0d23 4446 const char *name = (*current)->name;
252b5132 4447
155e0d23 4448 if (do_demangle && name != NULL && *name != '\0')
252b5132 4449 {
252b5132
RH
4450 char *alloc;
4451
155e0d23
NC
4452 /* If we want to demangle the name, we demangle it
4453 here, and temporarily clobber it while calling
4454 bfd_print_symbol. FIXME: This is a gross hack. */
af03af8f 4455 alloc = bfd_demangle (cur_bfd, name, demangle_flags);
ed180cc5
AM
4456 if (alloc != NULL)
4457 (*current)->name = alloc;
252b5132
RH
4458 bfd_print_symbol (cur_bfd, stdout, *current,
4459 bfd_print_symbol_all);
ed180cc5
AM
4460 if (alloc != NULL)
4461 {
4462 (*current)->name = name;
4463 free (alloc);
4464 }
252b5132 4465 }
252b5132 4466 else
155e0d23
NC
4467 bfd_print_symbol (cur_bfd, stdout, *current,
4468 bfd_print_symbol_all);
83ef0798 4469 printf ("\n");
252b5132 4470 }
661f7c35 4471
155e0d23 4472 current++;
252b5132 4473 }
155e0d23 4474 printf ("\n\n");
252b5132 4475}
155e0d23 4476\f
252b5132 4477static void
46dca2e0 4478dump_reloc_set (bfd *abfd, asection *sec, arelent **relpp, long relcount)
252b5132
RH
4479{
4480 arelent **p;
4481 char *last_filename, *last_functionname;
4482 unsigned int last_line;
9b8d1a36 4483 unsigned int last_discriminator;
252b5132
RH
4484
4485 /* Get column headers lined up reasonably. */
4486 {
4487 static int width;
98a91d6a 4488
252b5132
RH
4489 if (width == 0)
4490 {
4491 char buf[30];
155e0d23 4492
d8180c76 4493 bfd_sprintf_vma (abfd, buf, (bfd_vma) -1);
252b5132
RH
4494 width = strlen (buf) - 7;
4495 }
4496 printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
4497 }
4498
4499 last_filename = NULL;
4500 last_functionname = NULL;
4501 last_line = 0;
9b8d1a36 4502 last_discriminator = 0;
252b5132 4503
d3ba0551 4504 for (p = relpp; relcount && *p != NULL; p++, relcount--)
252b5132
RH
4505 {
4506 arelent *q = *p;
4507 const char *filename, *functionname;
91d6fa6a 4508 unsigned int linenumber;
9b8d1a36 4509 unsigned int discriminator;
252b5132
RH
4510 const char *sym_name;
4511 const char *section_name;
bf03e632 4512 bfd_vma addend2 = 0;
252b5132
RH
4513
4514 if (start_address != (bfd_vma) -1
4515 && q->address < start_address)
4516 continue;
4517 if (stop_address != (bfd_vma) -1
4518 && q->address > stop_address)
4519 continue;
4520
4521 if (with_line_numbers
4522 && sec != NULL
9b8d1a36
CC
4523 && bfd_find_nearest_line_discriminator (abfd, sec, syms, q->address,
4524 &filename, &functionname,
4525 &linenumber, &discriminator))
252b5132
RH
4526 {
4527 if (functionname != NULL
4528 && (last_functionname == NULL
4529 || strcmp (functionname, last_functionname) != 0))
4530 {
12add40e 4531 printf ("%s():\n", sanitize_string (functionname));
252b5132
RH
4532 if (last_functionname != NULL)
4533 free (last_functionname);
4534 last_functionname = xstrdup (functionname);
4535 }
98a91d6a 4536
91d6fa6a
NC
4537 if (linenumber > 0
4538 && (linenumber != last_line
252b5132
RH
4539 || (filename != NULL
4540 && last_filename != NULL
9b8d1a36
CC
4541 && filename_cmp (filename, last_filename) != 0)
4542 || (discriminator != last_discriminator)))
252b5132 4543 {
9b8d1a36 4544 if (discriminator > 0)
12add40e
NC
4545 printf ("%s:%u\n", filename == NULL ? "???" :
4546 sanitize_string (filename), linenumber);
9b8d1a36 4547 else
12add40e
NC
4548 printf ("%s:%u (discriminator %u)\n",
4549 filename == NULL ? "???" : sanitize_string (filename),
9b8d1a36 4550 linenumber, discriminator);
91d6fa6a 4551 last_line = linenumber;
9b8d1a36 4552 last_discriminator = discriminator;
252b5132
RH
4553 if (last_filename != NULL)
4554 free (last_filename);
4555 if (filename == NULL)
4556 last_filename = NULL;
4557 else
4558 last_filename = xstrdup (filename);
4559 }
4560 }
4561
4562 if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
4563 {
4564 sym_name = (*(q->sym_ptr_ptr))->name;
4565 section_name = (*(q->sym_ptr_ptr))->section->name;
4566 }
4567 else
4568 {
4569 sym_name = NULL;
4570 section_name = NULL;
4571 }
98a91d6a 4572
f9ecb0a4
JJ
4573 bfd_printf_vma (abfd, q->address);
4574 if (q->howto == NULL)
4575 printf (" *unknown* ");
4576 else if (q->howto->name)
bf03e632
DM
4577 {
4578 const char *name = q->howto->name;
4579
4580 /* R_SPARC_OLO10 relocations contain two addends.
4581 But because 'arelent' lacks enough storage to
4582 store them both, the 64-bit ELF Sparc backend
4583 records this as two relocations. One R_SPARC_LO10
4584 and one R_SPARC_13, both pointing to the same
4585 address. This is merely so that we have some
4586 place to store both addend fields.
4587
4588 Undo this transformation, otherwise the output
4589 will be confusing. */
4590 if (abfd->xvec->flavour == bfd_target_elf_flavour
82a9ed20 4591 && elf_tdata (abfd)->elf_header->e_machine == EM_SPARCV9
bf03e632
DM
4592 && relcount > 1
4593 && !strcmp (q->howto->name, "R_SPARC_LO10"))
4594 {
4595 arelent *q2 = *(p + 1);
4596 if (q2 != NULL
4597 && q2->howto
4598 && q->address == q2->address
4599 && !strcmp (q2->howto->name, "R_SPARC_13"))
4600 {
4601 name = "R_SPARC_OLO10";
4602 addend2 = q2->addend;
4603 p++;
4604 }
4605 }
4606 printf (" %-16s ", name);
4607 }
f9ecb0a4
JJ
4608 else
4609 printf (" %-16d ", q->howto->type);
171191ba 4610
252b5132 4611 if (sym_name)
171191ba
NC
4612 {
4613 objdump_print_symname (abfd, NULL, *q->sym_ptr_ptr);
171191ba 4614 }
252b5132
RH
4615 else
4616 {
d3ba0551 4617 if (section_name == NULL)
252b5132 4618 section_name = "*unknown*";
12add40e 4619 printf ("[%s]", sanitize_string (section_name));
252b5132 4620 }
98a91d6a 4621
252b5132
RH
4622 if (q->addend)
4623 {
343dbc36
L
4624 bfd_signed_vma addend = q->addend;
4625 if (addend < 0)
4626 {
4627 printf ("-0x");
4628 addend = -addend;
4629 }
4630 else
4631 printf ("+0x");
4632 bfd_printf_vma (abfd, addend);
252b5132 4633 }
bf03e632
DM
4634 if (addend2)
4635 {
4636 printf ("+0x");
4637 bfd_printf_vma (abfd, addend2);
4638 }
98a91d6a 4639
252b5132
RH
4640 printf ("\n");
4641 }
4b41844b
NC
4642
4643 if (last_filename != NULL)
4644 free (last_filename);
4645 if (last_functionname != NULL)
4646 free (last_functionname);
252b5132 4647}
43ac9881 4648
155e0d23 4649static void
3b9ad1cc
AM
4650dump_relocs_in_section (bfd *abfd,
4651 asection *section,
4652 void *dummy ATTRIBUTE_UNUSED)
155e0d23 4653{
e8fba7f6 4654 arelent **relpp = NULL;
155e0d23
NC
4655 long relcount;
4656 long relsize;
4657
4658 if ( bfd_is_abs_section (section)
4659 || bfd_is_und_section (section)
4660 || bfd_is_com_section (section)
4661 || (! process_section_p (section))
4662 || ((section->flags & SEC_RELOC) == 0))
4663 return;
4664
12add40e 4665 printf ("RELOCATION RECORDS FOR [%s]:", sanitize_string (section->name));
155e0d23 4666
7a6e0d89 4667 relsize = bfd_get_reloc_upper_bound (abfd, section);
155e0d23
NC
4668 if (relsize == 0)
4669 {
4670 printf (" (none)\n\n");
4671 return;
4672 }
4673
7a6e0d89
AM
4674 if (relsize < 0)
4675 relcount = relsize;
4676 else
4677 {
4678 relpp = (arelent **) xmalloc (relsize);
4679 relcount = bfd_canonicalize_reloc (abfd, section, relpp, syms);
39ff1b79
NC
4680 }
4681
155e0d23 4682 if (relcount < 0)
5a3f568b
NC
4683 {
4684 printf ("\n");
7a6e0d89
AM
4685 non_fatal (_("failed to read relocs in: %s"),
4686 sanitize_string (bfd_get_filename (abfd)));
5a3f568b
NC
4687 bfd_fatal (_("error message was"));
4688 }
155e0d23
NC
4689 else if (relcount == 0)
4690 printf (" (none)\n\n");
4691 else
4692 {
4693 printf ("\n");
4694 dump_reloc_set (abfd, section, relpp, relcount);
4695 printf ("\n\n");
4696 }
4697 free (relpp);
4698}
4699
4700static void
4701dump_relocs (bfd *abfd)
4702{
4703 bfd_map_over_sections (abfd, dump_relocs_in_section, NULL);
4704}
4705
4706static void
4707dump_dynamic_relocs (bfd *abfd)
4708{
4709 long relsize;
4710 arelent **relpp;
4711 long relcount;
4712
4713 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
4714 if (relsize < 0)
4715 bfd_fatal (bfd_get_filename (abfd));
4716
4717 printf ("DYNAMIC RELOCATION RECORDS");
4718
4719 if (relsize == 0)
4720 printf (" (none)\n\n");
4721 else
4722 {
3f5e193b 4723 relpp = (arelent **) xmalloc (relsize);
155e0d23
NC
4724 relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
4725
4726 if (relcount < 0)
4727 bfd_fatal (bfd_get_filename (abfd));
4728 else if (relcount == 0)
4729 printf (" (none)\n\n");
4730 else
4731 {
4732 printf ("\n");
4733 dump_reloc_set (abfd, NULL, relpp, relcount);
4734 printf ("\n\n");
4735 }
4736 free (relpp);
4737 }
4738}
4739
43ac9881
AM
4740/* Creates a table of paths, to search for source files. */
4741
4742static void
4743add_include_path (const char *path)
4744{
4745 if (path[0] == 0)
4746 return;
4747 include_path_count++;
3f5e193b
NC
4748 include_paths = (const char **)
4749 xrealloc (include_paths, include_path_count * sizeof (*include_paths));
43ac9881
AM
4750#ifdef HAVE_DOS_BASED_FILE_SYSTEM
4751 if (path[1] == ':' && path[2] == 0)
4752 path = concat (path, ".", (const char *) 0);
4753#endif
4754 include_paths[include_path_count - 1] = path;
4755}
155e0d23
NC
4756
4757static void
3b9ad1cc
AM
4758adjust_addresses (bfd *abfd ATTRIBUTE_UNUSED,
4759 asection *section,
bc79cded 4760 void *arg)
155e0d23 4761{
bc79cded
L
4762 if ((section->flags & SEC_DEBUGGING) == 0)
4763 {
4764 bfd_boolean *has_reloc_p = (bfd_boolean *) arg;
4765 section->vma += adjust_section_vma;
4766 if (*has_reloc_p)
4767 section->lma += adjust_section_vma;
4768 }
155e0d23
NC
4769}
4770
2379f9c4
FS
4771/* Return the sign-extended form of an ARCH_SIZE sized VMA. */
4772
4773static bfd_vma
4774sign_extend_address (bfd *abfd ATTRIBUTE_UNUSED,
4775 bfd_vma vma,
4776 unsigned arch_size)
4777{
4778 bfd_vma mask;
4779 mask = (bfd_vma) 1 << (arch_size - 1);
4780 return (((vma & ((mask << 1) - 1)) ^ mask) - mask);
4781}
4782
155e0d23
NC
4783/* Dump selected contents of ABFD. */
4784
4785static void
39f0547e 4786dump_bfd (bfd *abfd, bfd_boolean is_mainfile)
155e0d23 4787{
2379f9c4
FS
4788 const struct elf_backend_data * bed;
4789
39f0547e
NC
4790 if (bfd_big_endian (abfd))
4791 byte_get = byte_get_big_endian;
4792 else if (bfd_little_endian (abfd))
4793 byte_get = byte_get_little_endian;
4794 else
4795 byte_get = NULL;
4796
4797 /* Load any separate debug information files.
4798 We do this now and without checking do_follow_links because separate
4799 debug info files may contain symbol tables that we will need when
4800 displaying information about the main file. Any memory allocated by
4801 load_separate_debug_files will be released when we call
4802 free_debug_memory below.
4803
4804 The test on is_mainfile is there because the chain of separate debug
4805 info files is a global variable shared by all invocations of dump_bfd. */
4806 if (is_mainfile)
4807 {
4808 load_separate_debug_files (abfd, bfd_get_filename (abfd));
4809
4810 /* If asked to do so, recursively dump the separate files. */
4811 if (do_follow_links)
4812 {
4813 separate_info * i;
4814
4815 for (i = first_separate_info; i != NULL; i = i->next)
4816 dump_bfd (i->handle, FALSE);
4817 }
4818 }
4819
2379f9c4
FS
4820 /* Adjust user-specified start and stop limits for targets that use
4821 signed addresses. */
4822 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
4823 && (bed = get_elf_backend_data (abfd)) != NULL
4824 && bed->sign_extend_vma)
4825 {
4826 start_address = sign_extend_address (abfd, start_address,
4827 bed->s->arch_size);
4828 stop_address = sign_extend_address (abfd, stop_address,
4829 bed->s->arch_size);
4830 }
4831
155e0d23
NC
4832 /* If we are adjusting section VMA's, change them all now. Changing
4833 the BFD information is a hack. However, we must do it, or
4834 bfd_find_nearest_line will not do the right thing. */
4835 if (adjust_section_vma != 0)
bc79cded
L
4836 {
4837 bfd_boolean has_reloc = (abfd->flags & HAS_RELOC);
4838 bfd_map_over_sections (abfd, adjust_addresses, &has_reloc);
4839 }
155e0d23 4840
fd2f0033 4841 if (! dump_debugging_tags && ! suppress_bfd_header)
12add40e
NC
4842 printf (_("\n%s: file format %s\n"),
4843 sanitize_string (bfd_get_filename (abfd)),
155e0d23
NC
4844 abfd->xvec->name);
4845 if (dump_ar_hdrs)
1869e86f 4846 print_arelt_descr (stdout, abfd, TRUE, FALSE);
155e0d23
NC
4847 if (dump_file_header)
4848 dump_bfd_header (abfd);
4849 if (dump_private_headers)
4850 dump_bfd_private_header (abfd);
6abcee90
TG
4851 if (dump_private_options != NULL)
4852 dump_target_specific (abfd);
fd2f0033 4853 if (! dump_debugging_tags && ! suppress_bfd_header)
155e0d23 4854 putchar ('\n');
155e0d23 4855
365544c3
L
4856 if (dump_symtab
4857 || dump_reloc_info
4858 || disassemble
4859 || dump_debugging
4860 || dump_dwarf_section_info)
39f0547e
NC
4861 {
4862 syms = slurp_symtab (abfd);
4863
4864 /* If following links, load any symbol tables from the linked files as well. */
4865 if (do_follow_links && is_mainfile)
4866 {
4867 separate_info * i;
4868
4869 for (i = first_separate_info; i != NULL; i = i->next)
4870 {
4871 asymbol ** extra_syms;
4872 long old_symcount = symcount;
4873
4874 extra_syms = slurp_symtab (i->handle);
4875
4876 if (extra_syms)
4877 {
4878 if (old_symcount == 0)
4879 {
4880 syms = extra_syms;
4881 }
4882 else
4883 {
4884 syms = xrealloc (syms, (symcount + old_symcount) * sizeof (asymbol *));
4885 memcpy (syms + old_symcount,
4886 extra_syms,
4887 symcount * sizeof (asymbol *));
4888 }
4889 }
4890
4891 symcount += old_symcount;
4892 }
4893 }
4894 }
a29a8af8
KT
4895
4896 if (dump_section_headers)
4897 dump_headers (abfd);
4898
4c45e5c9
JJ
4899 if (dump_dynamic_symtab || dump_dynamic_reloc_info
4900 || (disassemble && bfd_get_dynamic_symtab_upper_bound (abfd) > 0))
155e0d23 4901 dynsyms = slurp_dynamic_symtab (abfd);
39f0547e 4902
90e3cdf2 4903 if (disassemble)
4c45e5c9 4904 {
c9727e01
AM
4905 synthcount = bfd_get_synthetic_symtab (abfd, symcount, syms,
4906 dynsymcount, dynsyms, &synthsyms);
4907 if (synthcount < 0)
4908 synthcount = 0;
4c45e5c9 4909 }
155e0d23
NC
4910
4911 if (dump_symtab)
4912 dump_symbols (abfd, FALSE);
4913 if (dump_dynamic_symtab)
4914 dump_symbols (abfd, TRUE);
365544c3
L
4915 if (dump_dwarf_section_info)
4916 dump_dwarf (abfd);
7d9813f1
NA
4917 if (dump_ctf_section_info)
4918 dump_ctf (abfd, dump_ctf_section_name, dump_ctf_parent_name);
155e0d23
NC
4919 if (dump_stab_section_info)
4920 dump_stabs (abfd);
4921 if (dump_reloc_info && ! disassemble)
4922 dump_relocs (abfd);
4923 if (dump_dynamic_reloc_info && ! disassemble)
4924 dump_dynamic_relocs (abfd);
4925 if (dump_section_contents)
4926 dump_data (abfd);
4927 if (disassemble)
4928 disassemble_data (abfd);
4929
4930 if (dump_debugging)
4931 {
4932 void *dhandle;
4933
b922d590 4934 dhandle = read_debugging_info (abfd, syms, symcount, TRUE);
155e0d23
NC
4935 if (dhandle != NULL)
4936 {
ed180cc5
AM
4937 if (!print_debugging_info (stdout, dhandle, abfd, syms,
4938 bfd_demangle,
4939 dump_debugging_tags ? TRUE : FALSE))
155e0d23
NC
4940 {
4941 non_fatal (_("%s: printing debugging information failed"),
4942 bfd_get_filename (abfd));
4943 exit_status = 1;
4944 }
cf0ad5bb
NC
4945
4946 free (dhandle);
155e0d23 4947 }
fdef3943
AM
4948 /* PR 6483: If there was no STABS debug info in the file, try
4949 DWARF instead. */
b922d590
NC
4950 else if (! dump_dwarf_section_info)
4951 {
3aade688 4952 dwarf_select_sections_all ();
b922d590
NC
4953 dump_dwarf (abfd);
4954 }
155e0d23
NC
4955 }
4956
4957 if (syms)
4958 {
4959 free (syms);
4960 syms = NULL;
4961 }
4962
4963 if (dynsyms)
4964 {
4965 free (dynsyms);
4966 dynsyms = NULL;
4967 }
4c45e5c9
JJ
4968
4969 if (synthsyms)
4970 {
4971 free (synthsyms);
4972 synthsyms = NULL;
4973 }
4974
4975 symcount = 0;
4976 dynsymcount = 0;
4977 synthcount = 0;
39f0547e
NC
4978
4979 if (is_mainfile)
4980 free_debug_memory ();
155e0d23
NC
4981}
4982
4983static void
1598539f 4984display_object_bfd (bfd *abfd)
155e0d23
NC
4985{
4986 char **matching;
4987
4988 if (bfd_check_format_matches (abfd, bfd_object, &matching))
4989 {
39f0547e 4990 dump_bfd (abfd, TRUE);
155e0d23
NC
4991 return;
4992 }
4993
4994 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
4995 {
4996 nonfatal (bfd_get_filename (abfd));
4997 list_matching_formats (matching);
4998 free (matching);
4999 return;
5000 }
5001
5002 if (bfd_get_error () != bfd_error_file_not_recognized)
5003 {
5004 nonfatal (bfd_get_filename (abfd));
5005 return;
5006 }
5007
5008 if (bfd_check_format_matches (abfd, bfd_core, &matching))
5009 {
39f0547e 5010 dump_bfd (abfd, TRUE);
155e0d23
NC
5011 return;
5012 }
5013
5014 nonfatal (bfd_get_filename (abfd));
5015
5016 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
5017 {
5018 list_matching_formats (matching);
5019 free (matching);
5020 }
5021}
5022
5023static void
1598539f 5024display_any_bfd (bfd *file, int level)
155e0d23 5025{
4a114e3e
L
5026 /* Decompress sections unless dumping the section contents. */
5027 if (!dump_section_contents)
5028 file->flags |= BFD_DECOMPRESS;
5029
155e0d23
NC
5030 /* If the file is an archive, process all of its elements. */
5031 if (bfd_check_format (file, bfd_archive))
5032 {
1598539f 5033 bfd *arfile = NULL;
155e0d23 5034 bfd *last_arfile = NULL;
bdc4de1b 5035
1598539f 5036 if (level == 0)
12add40e 5037 printf (_("In archive %s:\n"), sanitize_string (bfd_get_filename (file)));
c88f5b8e
NC
5038 else if (level > 100)
5039 {
5040 /* Prevent corrupted files from spinning us into an
5041 infinite loop. 100 is an arbitrary heuristic. */
64d29018 5042 fatal (_("Archive nesting is too deep"));
c88f5b8e
NC
5043 return;
5044 }
1598539f 5045 else
12add40e
NC
5046 printf (_("In nested archive %s:\n"),
5047 sanitize_string (bfd_get_filename (file)));
1598539f 5048
155e0d23
NC
5049 for (;;)
5050 {
5051 bfd_set_error (bfd_error_no_error);
5052
5053 arfile = bfd_openr_next_archived_file (file, arfile);
5054 if (arfile == NULL)
5055 {
5056 if (bfd_get_error () != bfd_error_no_more_archived_files)
5057 nonfatal (bfd_get_filename (file));
5058 break;
5059 }
5060
1598539f 5061 display_any_bfd (arfile, level + 1);
155e0d23
NC
5062
5063 if (last_arfile != NULL)
f64e188b
NC
5064 {
5065 bfd_close (last_arfile);
5066 /* PR 17512: file: ac585d01. */
5067 if (arfile == last_arfile)
5068 {
5069 last_arfile = NULL;
5070 break;
5071 }
5072 }
155e0d23
NC
5073 last_arfile = arfile;
5074 }
5075
5076 if (last_arfile != NULL)
5077 bfd_close (last_arfile);
5078 }
5079 else
1598539f
TG
5080 display_object_bfd (file);
5081}
5082
5083static void
cd6581da 5084display_file (char *filename, char *target, bfd_boolean last_file)
1598539f
TG
5085{
5086 bfd *file;
5087
5088 if (get_file_size (filename) < 1)
5089 {
5090 exit_status = 1;
5091 return;
5092 }
5093
5094 file = bfd_openr (filename, target);
5095 if (file == NULL)
5096 {
5097 nonfatal (filename);
5098 return;
5099 }
5100
5101 display_any_bfd (file, 0);
155e0d23 5102
cd6581da
NC
5103 /* This is an optimization to improve the speed of objdump, especially when
5104 dumping a file with lots of associated debug informatiom. Calling
5105 bfd_close on such a file can take a non-trivial amount of time as there
5106 are lots of lists to walk and buffers to free. This is only really
5107 necessary however if we are about to load another file and we need the
5108 memory back. Otherwise, if we are about to exit, then we can save (a lot
5109 of) time by only doing a quick close, and allowing the OS to reclaim the
5110 memory for us. */
5111 if (! last_file)
5112 bfd_close (file);
5113 else
5114 bfd_close_all_done (file);
155e0d23 5115}
252b5132 5116\f
252b5132 5117int
46dca2e0 5118main (int argc, char **argv)
252b5132
RH
5119{
5120 int c;
5121 char *target = default_target;
b34976b6 5122 bfd_boolean seenflag = FALSE;
252b5132 5123
155e0d23
NC
5124#if defined (HAVE_SETLOCALE)
5125#if defined (HAVE_LC_MESSAGES)
252b5132 5126 setlocale (LC_MESSAGES, "");
3882b010 5127#endif
3882b010 5128 setlocale (LC_CTYPE, "");
252b5132 5129#endif
155e0d23 5130
252b5132
RH
5131 bindtextdomain (PACKAGE, LOCALEDIR);
5132 textdomain (PACKAGE);
5133
5134 program_name = *argv;
5135 xmalloc_set_program_name (program_name);
86eafac0 5136 bfd_set_error_program_name (program_name);
252b5132
RH
5137
5138 START_PROGRESS (program_name, 0);
5139
869b9d07
MM
5140 expandargv (&argc, &argv);
5141
bf2dd8d7
AM
5142 if (bfd_init () != BFD_INIT_MAGIC)
5143 fatal (_("fatal error: libbfd ABI mismatch"));
252b5132
RH
5144 set_default_bfd_target ();
5145
4cb93e3b 5146 while ((c = getopt_long (argc, argv,
6abcee90 5147 "pP:ib:m:M:VvCdDlfFaHhrRtTxsSI:j:wE:zgeGW::",
252b5132
RH
5148 long_options, (int *) 0))
5149 != EOF)
5150 {
252b5132
RH
5151 switch (c)
5152 {
5153 case 0:
8b53311e 5154 break; /* We've been given a long option. */
252b5132
RH
5155 case 'm':
5156 machine = optarg;
5157 break;
dd92f639 5158 case 'M':
65b48a81
PB
5159 {
5160 char *options;
5161 if (disassembler_options)
5162 /* Ignore potential memory leak for now. */
5163 options = concat (disassembler_options, ",",
5164 optarg, (const char *) NULL);
5165 else
5166 options = optarg;
5167 disassembler_options = remove_whitespace_and_extra_commas (options);
5168 }
dd92f639 5169 break;
252b5132 5170 case 'j':
70ecb384 5171 add_only (optarg);
252b5132 5172 break;
98ec6e72
NC
5173 case 'F':
5174 display_file_offsets = TRUE;
5175 break;
252b5132 5176 case 'l':
b34976b6 5177 with_line_numbers = TRUE;
252b5132
RH
5178 break;
5179 case 'b':
5180 target = optarg;
5181 break;
1dada9c5 5182 case 'C':
b34976b6 5183 do_demangle = TRUE;
28c309a2
NC
5184 if (optarg != NULL)
5185 {
5186 enum demangling_styles style;
8b53311e 5187
28c309a2 5188 style = cplus_demangle_name_to_style (optarg);
0af11b59 5189 if (style == unknown_demangling)
28c309a2
NC
5190 fatal (_("unknown demangling style `%s'"),
5191 optarg);
8b53311e 5192
28c309a2 5193 cplus_demangle_set_style (style);
0af11b59 5194 }
1dada9c5 5195 break;
af03af8f
NC
5196 case OPTION_RECURSE_LIMIT:
5197 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
5198 break;
5199 case OPTION_NO_RECURSE_LIMIT:
5200 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
5201 break;
1dada9c5 5202 case 'w':
7b5d4822 5203 do_wide = wide_output = TRUE;
1dada9c5
NC
5204 break;
5205 case OPTION_ADJUST_VMA:
5206 adjust_section_vma = parse_vma (optarg, "--adjust-vma");
5207 break;
5208 case OPTION_START_ADDRESS:
5209 start_address = parse_vma (optarg, "--start-address");
98ec6e72
NC
5210 if ((stop_address != (bfd_vma) -1) && stop_address <= start_address)
5211 fatal (_("error: the start address should be before the end address"));
1dada9c5
NC
5212 break;
5213 case OPTION_STOP_ADDRESS:
5214 stop_address = parse_vma (optarg, "--stop-address");
98ec6e72
NC
5215 if ((start_address != (bfd_vma) -1) && stop_address <= start_address)
5216 fatal (_("error: the stop address should be after the start address"));
1dada9c5 5217 break;
0dafdf3f
L
5218 case OPTION_PREFIX:
5219 prefix = optarg;
5220 prefix_length = strlen (prefix);
5221 /* Remove an unnecessary trailing '/' */
5222 while (IS_DIR_SEPARATOR (prefix[prefix_length - 1]))
5223 prefix_length--;
5224 break;
5225 case OPTION_PREFIX_STRIP:
5226 prefix_strip = atoi (optarg);
5227 if (prefix_strip < 0)
5228 fatal (_("error: prefix strip must be non-negative"));
5229 break;
3dcb3fcb
L
5230 case OPTION_INSN_WIDTH:
5231 insn_width = strtoul (optarg, NULL, 0);
5232 if (insn_width <= 0)
5233 fatal (_("error: instruction width must be positive"));
5234 break;
4a14e306
AK
5235 case OPTION_INLINES:
5236 unwind_inlines = TRUE;
5237 break;
1d67fe3b
TT
5238 case OPTION_VISUALIZE_JUMPS:
5239 visualize_jumps = TRUE;
5240 color_output = FALSE;
5241 extended_color_output = FALSE;
5242 if (optarg != NULL)
5243 {
5244 if (streq (optarg, "color"))
5245 color_output = TRUE;
5246 else if (streq (optarg, "extended-color"))
5247 {
5248 color_output = TRUE;
5249 extended_color_output = TRUE;
5250 }
5251 else if (streq (optarg, "off"))
5252 visualize_jumps = FALSE;
5253 else
5254 nonfatal (_("unrecognized argument to --visualize-option"));
5255 }
5256 break;
1dada9c5
NC
5257 case 'E':
5258 if (strcmp (optarg, "B") == 0)
5259 endian = BFD_ENDIAN_BIG;
5260 else if (strcmp (optarg, "L") == 0)
5261 endian = BFD_ENDIAN_LITTLE;
5262 else
5263 {
a8c62f1c 5264 nonfatal (_("unrecognized -E option"));
1dada9c5
NC
5265 usage (stderr, 1);
5266 }
5267 break;
5268 case OPTION_ENDIAN:
5269 if (strncmp (optarg, "big", strlen (optarg)) == 0)
5270 endian = BFD_ENDIAN_BIG;
5271 else if (strncmp (optarg, "little", strlen (optarg)) == 0)
5272 endian = BFD_ENDIAN_LITTLE;
5273 else
5274 {
37cc8ec1 5275 non_fatal (_("unrecognized --endian type `%s'"), optarg);
a8c62f1c 5276 exit_status = 1;
1dada9c5
NC
5277 usage (stderr, 1);
5278 }
5279 break;
8b53311e 5280
252b5132 5281 case 'f':
b34976b6
AM
5282 dump_file_header = TRUE;
5283 seenflag = TRUE;
252b5132
RH
5284 break;
5285 case 'i':
b34976b6
AM
5286 formats_info = TRUE;
5287 seenflag = TRUE;
252b5132 5288 break;
43ac9881
AM
5289 case 'I':
5290 add_include_path (optarg);
5291 break;
252b5132 5292 case 'p':
b34976b6
AM
5293 dump_private_headers = TRUE;
5294 seenflag = TRUE;
252b5132 5295 break;
6abcee90
TG
5296 case 'P':
5297 dump_private_options = optarg;
5298 seenflag = TRUE;
5299 break;
252b5132 5300 case 'x':
b34976b6
AM
5301 dump_private_headers = TRUE;
5302 dump_symtab = TRUE;
5303 dump_reloc_info = TRUE;
5304 dump_file_header = TRUE;
5305 dump_ar_hdrs = TRUE;
5306 dump_section_headers = TRUE;
5307 seenflag = TRUE;
252b5132
RH
5308 break;
5309 case 't':
b34976b6
AM
5310 dump_symtab = TRUE;
5311 seenflag = TRUE;
252b5132
RH
5312 break;
5313 case 'T':
b34976b6
AM
5314 dump_dynamic_symtab = TRUE;
5315 seenflag = TRUE;
252b5132
RH
5316 break;
5317 case 'd':
b34976b6
AM
5318 disassemble = TRUE;
5319 seenflag = TRUE;
d3def5d7 5320 disasm_sym = optarg;
1dada9c5
NC
5321 break;
5322 case 'z':
b34976b6 5323 disassemble_zeroes = TRUE;
252b5132
RH
5324 break;
5325 case 'D':
b34976b6
AM
5326 disassemble = TRUE;
5327 disassemble_all = TRUE;
5328 seenflag = TRUE;
252b5132
RH
5329 break;
5330 case 'S':
b34976b6
AM
5331 disassemble = TRUE;
5332 with_source_code = TRUE;
5333 seenflag = TRUE;
1dada9c5 5334 break;
a1c110a3
NC
5335 case OPTION_SOURCE_COMMENT:
5336 disassemble = TRUE;
5337 with_source_code = TRUE;
5338 seenflag = TRUE;
5339 if (optarg)
5340 source_comment = xstrdup (sanitize_string (optarg));
5341 else
5342 source_comment = xstrdup ("# ");
5343 break;
1dada9c5
NC
5344 case 'g':
5345 dump_debugging = 1;
b34976b6 5346 seenflag = TRUE;
1dada9c5 5347 break;
51cdc6e0
NC
5348 case 'e':
5349 dump_debugging = 1;
5350 dump_debugging_tags = 1;
5351 do_demangle = TRUE;
5352 seenflag = TRUE;
5353 break;
365544c3
L
5354 case 'W':
5355 dump_dwarf_section_info = TRUE;
5356 seenflag = TRUE;
4cb93e3b
TG
5357 if (optarg)
5358 dwarf_select_sections_by_letters (optarg);
5359 else
5360 dwarf_select_sections_all ();
5361 break;
5362 case OPTION_DWARF:
5363 dump_dwarf_section_info = TRUE;
5364 seenflag = TRUE;
5365 if (optarg)
5366 dwarf_select_sections_by_names (optarg);
5367 else
5368 dwarf_select_sections_all ();
365544c3 5369 break;
fd2f0033
TT
5370 case OPTION_DWARF_DEPTH:
5371 {
5372 char *cp;
5373 dwarf_cutoff_level = strtoul (optarg, & cp, 0);
5374 }
5375 break;
5376 case OPTION_DWARF_START:
5377 {
5378 char *cp;
5379 dwarf_start_die = strtoul (optarg, & cp, 0);
5380 suppress_bfd_header = 1;
5381 }
5382 break;
4723351a
CC
5383 case OPTION_DWARF_CHECK:
5384 dwarf_check = TRUE;
5385 break;
094e34f2 5386#ifdef ENABLE_LIBCTF
d344b407
NA
5387 case OPTION_CTF:
5388 dump_ctf_section_info = TRUE;
5389 dump_ctf_section_name = xstrdup (optarg);
5390 seenflag = TRUE;
5391 break;
7d9813f1
NA
5392 case OPTION_CTF_PARENT:
5393 dump_ctf_parent_name = xstrdup (optarg);
5394 break;
094e34f2 5395#endif
1dada9c5 5396 case 'G':
b34976b6
AM
5397 dump_stab_section_info = TRUE;
5398 seenflag = TRUE;
252b5132
RH
5399 break;
5400 case 's':
b34976b6
AM
5401 dump_section_contents = TRUE;
5402 seenflag = TRUE;
252b5132
RH
5403 break;
5404 case 'r':
b34976b6
AM
5405 dump_reloc_info = TRUE;
5406 seenflag = TRUE;
252b5132
RH
5407 break;
5408 case 'R':
b34976b6
AM
5409 dump_dynamic_reloc_info = TRUE;
5410 seenflag = TRUE;
252b5132
RH
5411 break;
5412 case 'a':
b34976b6
AM
5413 dump_ar_hdrs = TRUE;
5414 seenflag = TRUE;
252b5132
RH
5415 break;
5416 case 'h':
b34976b6
AM
5417 dump_section_headers = TRUE;
5418 seenflag = TRUE;
252b5132 5419 break;
8b53311e 5420 case 'v':
252b5132 5421 case 'V':
b34976b6
AM
5422 show_version = TRUE;
5423 seenflag = TRUE;
252b5132 5424 break;
0af11b59 5425
aebcf7b7
NC
5426 case 'H':
5427 usage (stdout, 0);
5428 /* No need to set seenflag or to break - usage() does not return. */
252b5132
RH
5429 default:
5430 usage (stderr, 1);
5431 }
5432 }
5433
5434 if (show_version)
5435 print_version ("objdump");
5436
b34976b6 5437 if (!seenflag)
1dada9c5 5438 usage (stderr, 2);
252b5132
RH
5439
5440 if (formats_info)
06d86cf7 5441 exit_status = display_info ();
252b5132
RH
5442 else
5443 {
5444 if (optind == argc)
cd6581da 5445 display_file ("a.out", target, TRUE);
252b5132
RH
5446 else
5447 for (; optind < argc;)
cd6581da
NC
5448 {
5449 display_file (argv[optind], target, optind == argc - 1);
5450 optind++;
5451 }
252b5132
RH
5452 }
5453
70ecb384 5454 free_only_list ();
7d9813f1
NA
5455 free (dump_ctf_section_name);
5456 free (dump_ctf_parent_name);
a1c110a3 5457 free ((void *) source_comment);
79b377b3 5458
252b5132
RH
5459 END_PROGRESS (program_name);
5460
75cd796a 5461 return exit_status;
252b5132 5462}
This page took 1.254853 seconds and 4 git commands to generate.