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