struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / sdbinutils / binutils / objdump.c
blob08a0fe521d88a088e0ed45982ff2895311204ab6
1 /* objdump.c -- dump information about an object file.
2 Copyright (C) 1990-2022 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
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.
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.
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. */
22 /* Objdump overview.
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.
29 The flow of execution is as follows:
31 1. Command line arguments are checked for control switches and the
32 information to be displayed is selected.
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.
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.
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.
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. */
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 "demanguse.h"
59 #include "dwarf.h"
60 #include "ctf-api.h"
61 #include "getopt.h"
62 #include "safe-ctype.h"
63 #include "dis-asm.h"
64 #include "libiberty.h"
65 #include "demangle.h"
66 #include "filenames.h"
67 #include "debug.h"
68 #include "budbg.h"
69 #include "objdump.h"
71 #ifdef HAVE_MMAP
72 #include <sys/mman.h>
73 #endif
75 /* Internal headers for the ELF .stab-dump code - sorry. */
76 #define BYTES_IN_WORD 32
77 #include "aout/aout64.h"
79 /* Exit status. */
80 static int exit_status = 0;
82 static char *default_target = NULL; /* Default at runtime. */
84 /* The following variables are set based on arguments passed on the
85 command line. */
86 static int show_version = 0; /* Show the version number. */
87 static int dump_section_contents; /* -s */
88 static int dump_section_headers; /* -h */
89 static bool dump_file_header; /* -f */
90 static int dump_symtab; /* -t */
91 static int dump_dynamic_symtab; /* -T */
92 static int dump_reloc_info; /* -r */
93 static int dump_dynamic_reloc_info; /* -R */
94 static int dump_ar_hdrs; /* -a */
95 static int dump_private_headers; /* -p */
96 static char *dump_private_options; /* -P */
97 static int no_addresses; /* --no-addresses */
98 static int prefix_addresses; /* --prefix-addresses */
99 static int with_line_numbers; /* -l */
100 static bool with_source_code; /* -S */
101 static int show_raw_insn; /* --show-raw-insn */
102 static int dump_dwarf_section_info; /* --dwarf */
103 static int dump_stab_section_info; /* --stabs */
104 static int dump_ctf_section_info; /* --ctf */
105 static char *dump_ctf_section_name;
106 static char *dump_ctf_parent_name; /* --ctf-parent */
107 static int do_demangle; /* -C, --demangle */
108 static bool disassemble; /* -d */
109 static bool disassemble_all; /* -D */
110 static int disassemble_zeroes; /* --disassemble-zeroes */
111 static bool formats_info; /* -i */
112 static int wide_output; /* -w */
113 static int insn_width; /* --insn-width */
114 static bfd_vma start_address = (bfd_vma) -1; /* --start-address */
115 static bfd_vma stop_address = (bfd_vma) -1; /* --stop-address */
116 static int dump_debugging; /* --debugging */
117 static int dump_debugging_tags; /* --debugging-tags */
118 static int suppress_bfd_header;
119 static int dump_special_syms = 0; /* --special-syms */
120 static bfd_vma adjust_section_vma = 0; /* --adjust-vma */
121 static int file_start_context = 0; /* --file-start-context */
122 static bool display_file_offsets; /* -F */
123 static const char *prefix; /* --prefix */
124 static int prefix_strip; /* --prefix-strip */
125 static size_t prefix_length;
126 static bool unwind_inlines; /* --inlines. */
127 static const char * disasm_sym; /* Disassembly start symbol. */
128 static const char * source_comment; /* --source_comment. */
129 static bool visualize_jumps = false; /* --visualize-jumps. */
130 static bool color_output = false; /* --visualize-jumps=color. */
131 static bool extended_color_output = false; /* --visualize-jumps=extended-color. */
132 static int process_links = false; /* --process-links. */
134 static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
136 /* A structure to record the sections mentioned in -j switches. */
137 struct only
139 const char *name; /* The name of the section. */
140 bool seen; /* A flag to indicate that the section has been found in one or more input files. */
141 struct only *next; /* Pointer to the next structure in the list. */
143 /* Pointer to an array of 'only' structures.
144 This pointer is NULL if the -j switch has not been used. */
145 static struct only * only_list = NULL;
147 /* Variables for handling include file path table. */
148 static const char **include_paths;
149 static int include_path_count;
151 /* Extra info to pass to the section disassembler and address printing
152 function. */
153 struct objdump_disasm_info
155 bfd *abfd;
156 bool require_sec;
157 disassembler_ftype disassemble_fn;
158 arelent *reloc;
159 const char *symbol;
162 /* Architecture to disassemble for, or default if NULL. */
163 static char *machine = NULL;
165 /* Target specific options to the disassembler. */
166 static char *disassembler_options = NULL;
168 /* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */
169 static enum bfd_endian endian = BFD_ENDIAN_UNKNOWN;
171 /* The symbol table. */
172 static asymbol **syms;
174 /* Number of symbols in `syms'. */
175 static long symcount = 0;
177 /* The sorted symbol table. */
178 static asymbol **sorted_syms;
180 /* Number of symbols in `sorted_syms'. */
181 static long sorted_symcount = 0;
183 /* The dynamic symbol table. */
184 static asymbol **dynsyms;
186 /* The synthetic symbol table. */
187 static asymbol *synthsyms;
188 static long synthcount = 0;
190 /* Number of symbols in `dynsyms'. */
191 static long dynsymcount = 0;
193 static bfd_byte *stabs;
194 static bfd_size_type stab_size;
196 static bfd_byte *strtab;
197 static bfd_size_type stabstr_size;
199 /* Handlers for -P/--private. */
200 static const struct objdump_private_desc * const objdump_private_vectors[] =
202 OBJDUMP_PRIVATE_VECTORS
203 NULL
206 /* The list of detected jumps inside a function. */
207 static struct jump_info *detected_jumps = NULL;
209 typedef enum unicode_display_type
211 unicode_default = 0,
212 unicode_locale,
213 unicode_escape,
214 unicode_hex,
215 unicode_highlight,
216 unicode_invalid
217 } unicode_display_type;
219 static unicode_display_type unicode_display = unicode_default;
221 static void usage (FILE *, int) ATTRIBUTE_NORETURN;
222 static void
223 usage (FILE *stream, int status)
225 fprintf (stream, _("Usage: %s <option(s)> <file(s)>\n"), program_name);
226 fprintf (stream, _(" Display information from object <file(s)>.\n"));
227 fprintf (stream, _(" At least one of the following switches must be given:\n"));
228 fprintf (stream, _("\
229 -a, --archive-headers Display archive header information\n"));
230 fprintf (stream, _("\
231 -f, --file-headers Display the contents of the overall file header\n"));
232 fprintf (stream, _("\
233 -p, --private-headers Display object format specific file header contents\n"));
234 fprintf (stream, _("\
235 -P, --private=OPT,OPT... Display object format specific contents\n"));
236 fprintf (stream, _("\
237 -h, --[section-]headers Display the contents of the section headers\n"));
238 fprintf (stream, _("\
239 -x, --all-headers Display the contents of all headers\n"));
240 fprintf (stream, _("\
241 -d, --disassemble Display assembler contents of executable sections\n"));
242 fprintf (stream, _("\
243 -D, --disassemble-all Display assembler contents of all sections\n"));
244 fprintf (stream, _("\
245 --disassemble=<sym> Display assembler contents from <sym>\n"));
246 fprintf (stream, _("\
247 -S, --source Intermix source code with disassembly\n"));
248 fprintf (stream, _("\
249 --source-comment[=<txt>] Prefix lines of source code with <txt>\n"));
250 fprintf (stream, _("\
251 -s, --full-contents Display the full contents of all sections requested\n"));
252 fprintf (stream, _("\
253 -g, --debugging Display debug information in object file\n"));
254 fprintf (stream, _("\
255 -e, --debugging-tags Display debug information using ctags style\n"));
256 fprintf (stream, _("\
257 -G, --stabs Display (in raw form) any STABS info in the file\n"));
258 fprintf (stream, _("\
259 -W, --dwarf[a/=abbrev, A/=addr, r/=aranges, c/=cu_index, L/=decodedline,\n\
260 f/=frames, F/=frames-interp, g/=gdb_index, i/=info, o/=loc,\n\
261 m/=macro, p/=pubnames, t/=pubtypes, R/=Ranges, l/=rawline,\n\
262 s/=str, O/=str-offsets, u/=trace_abbrev, T/=trace_aranges,\n\
263 U/=trace_info]\n\
264 Display the contents of DWARF debug sections\n"));
265 fprintf (stream, _("\
266 -Wk,--dwarf=links Display the contents of sections that link to\n\
267 separate debuginfo files\n"));
268 #if DEFAULT_FOR_FOLLOW_LINKS
269 fprintf (stream, _("\
270 -WK,--dwarf=follow-links\n\
271 Follow links to separate debug info files (default)\n"));
272 fprintf (stream, _("\
273 -WN,--dwarf=no-follow-links\n\
274 Do not follow links to separate debug info files\n"));
275 #else
276 fprintf (stream, _("\
277 -WK,--dwarf=follow-links\n\
278 Follow links to separate debug info files\n"));
279 fprintf (stream, _("\
280 -WN,--dwarf=no-follow-links\n\
281 Do not follow links to separate debug info files\n\
282 (default)\n"));
283 #endif
284 fprintf (stream, _("\
285 -L, --process-links Display the contents of non-debug sections in\n\
286 separate debuginfo files. (Implies -WK)\n"));
287 #ifdef ENABLE_LIBCTF
288 fprintf (stream, _("\
289 --ctf[=SECTION] Display CTF info from SECTION, (default `.ctf')\n"));
290 #endif
291 fprintf (stream, _("\
292 -t, --syms Display the contents of the symbol table(s)\n"));
293 fprintf (stream, _("\
294 -T, --dynamic-syms Display the contents of the dynamic symbol table\n"));
295 fprintf (stream, _("\
296 -r, --reloc Display the relocation entries in the file\n"));
297 fprintf (stream, _("\
298 -R, --dynamic-reloc Display the dynamic relocation entries in the file\n"));
299 fprintf (stream, _("\
300 @<file> Read options from <file>\n"));
301 fprintf (stream, _("\
302 -v, --version Display this program's version number\n"));
303 fprintf (stream, _("\
304 -i, --info List object formats and architectures supported\n"));
305 fprintf (stream, _("\
306 -H, --help Display this information\n"));
308 if (status != 2)
310 const struct objdump_private_desc * const *desc;
312 fprintf (stream, _("\n The following switches are optional:\n"));
313 fprintf (stream, _("\
314 -b, --target=BFDNAME Specify the target object format as BFDNAME\n"));
315 fprintf (stream, _("\
316 -m, --architecture=MACHINE Specify the target architecture as MACHINE\n"));
317 fprintf (stream, _("\
318 -j, --section=NAME Only display information for section NAME\n"));
319 fprintf (stream, _("\
320 -M, --disassembler-options=OPT Pass text OPT on to the disassembler\n"));
321 fprintf (stream, _("\
322 -EB --endian=big Assume big endian format when disassembling\n"));
323 fprintf (stream, _("\
324 -EL --endian=little Assume little endian format when disassembling\n"));
325 fprintf (stream, _("\
326 --file-start-context Include context from start of file (with -S)\n"));
327 fprintf (stream, _("\
328 -I, --include=DIR Add DIR to search list for source files\n"));
329 fprintf (stream, _("\
330 -l, --line-numbers Include line numbers and filenames in output\n"));
331 fprintf (stream, _("\
332 -F, --file-offsets Include file offsets when displaying information\n"));
333 fprintf (stream, _("\
334 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n"));
335 display_demangler_styles (stream, _("\
336 STYLE can be "));
337 fprintf (stream, _("\
338 --recurse-limit Enable a limit on recursion whilst demangling\n\
339 (default)\n"));
340 fprintf (stream, _("\
341 --no-recurse-limit Disable a limit on recursion whilst demangling\n"));
342 fprintf (stream, _("\
343 -w, --wide Format output for more than 80 columns\n"));
344 fprintf (stream, _("\
345 -U[d|l|i|x|e|h] Controls the display of UTF-8 unicode characters\n\
346 --unicode=[default|locale|invalid|hex|escape|highlight]\n"));
347 fprintf (stream, _("\
348 -z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling\n"));
349 fprintf (stream, _("\
350 --start-address=ADDR Only process data whose address is >= ADDR\n"));
351 fprintf (stream, _("\
352 --stop-address=ADDR Only process data whose address is < ADDR\n"));
353 fprintf (stream, _("\
354 --no-addresses Do not print address alongside disassembly\n"));
355 fprintf (stream, _("\
356 --prefix-addresses Print complete address alongside disassembly\n"));
357 fprintf (stream, _("\
358 --[no-]show-raw-insn Display hex alongside symbolic disassembly\n"));
359 fprintf (stream, _("\
360 --insn-width=WIDTH Display WIDTH bytes on a single line for -d\n"));
361 fprintf (stream, _("\
362 --adjust-vma=OFFSET Add OFFSET to all displayed section addresses\n"));
363 fprintf (stream, _("\
364 --special-syms Include special symbols in symbol dumps\n"));
365 fprintf (stream, _("\
366 --inlines Print all inlines for source line (with -l)\n"));
367 fprintf (stream, _("\
368 --prefix=PREFIX Add PREFIX to absolute paths for -S\n"));
369 fprintf (stream, _("\
370 --prefix-strip=LEVEL Strip initial directory names for -S\n"));
371 fprintf (stream, _("\
372 --dwarf-depth=N Do not display DIEs at depth N or greater\n"));
373 fprintf (stream, _("\
374 --dwarf-start=N Display DIEs starting at offset N\n"));
375 fprintf (stream, _("\
376 --dwarf-check Make additional dwarf consistency checks.\n"));
377 #ifdef ENABLE_LIBCTF
378 fprintf (stream, _("\
379 --ctf-parent=NAME Use CTF archive member NAME as the CTF parent\n"));
380 #endif
381 fprintf (stream, _("\
382 --visualize-jumps Visualize jumps by drawing ASCII art lines\n"));
383 fprintf (stream, _("\
384 --visualize-jumps=color Use colors in the ASCII art\n"));
385 fprintf (stream, _("\
386 --visualize-jumps=extended-color\n\
387 Use extended 8-bit color codes\n"));
388 fprintf (stream, _("\
389 --visualize-jumps=off Disable jump visualization\n\n"));
391 list_supported_targets (program_name, stream);
392 list_supported_architectures (program_name, stream);
394 disassembler_usage (stream);
396 if (objdump_private_vectors[0] != NULL)
398 fprintf (stream,
399 _("\nOptions supported for -P/--private switch:\n"));
400 for (desc = objdump_private_vectors; *desc != NULL; desc++)
401 (*desc)->help (stream);
404 if (REPORT_BUGS_TO[0] && status == 0)
405 fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
406 exit (status);
409 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
410 enum option_values
412 OPTION_ENDIAN=150,
413 OPTION_START_ADDRESS,
414 OPTION_STOP_ADDRESS,
415 OPTION_DWARF,
416 OPTION_PREFIX,
417 OPTION_PREFIX_STRIP,
418 OPTION_INSN_WIDTH,
419 OPTION_ADJUST_VMA,
420 OPTION_DWARF_DEPTH,
421 OPTION_DWARF_CHECK,
422 OPTION_DWARF_START,
423 OPTION_RECURSE_LIMIT,
424 OPTION_NO_RECURSE_LIMIT,
425 OPTION_INLINES,
426 OPTION_SOURCE_COMMENT,
427 #ifdef ENABLE_LIBCTF
428 OPTION_CTF,
429 OPTION_CTF_PARENT,
430 #endif
431 OPTION_VISUALIZE_JUMPS
434 static struct option long_options[]=
436 {"adjust-vma", required_argument, NULL, OPTION_ADJUST_VMA},
437 {"all-headers", no_argument, NULL, 'x'},
438 {"architecture", required_argument, NULL, 'm'},
439 {"archive-headers", no_argument, NULL, 'a'},
440 #ifdef ENABLE_LIBCTF
441 {"ctf", optional_argument, NULL, OPTION_CTF},
442 {"ctf-parent", required_argument, NULL, OPTION_CTF_PARENT},
443 #endif
444 {"debugging", no_argument, NULL, 'g'},
445 {"debugging-tags", no_argument, NULL, 'e'},
446 {"demangle", optional_argument, NULL, 'C'},
447 {"disassemble", optional_argument, NULL, 'd'},
448 {"disassemble-all", no_argument, NULL, 'D'},
449 {"disassemble-zeroes", no_argument, NULL, 'z'},
450 {"disassembler-options", required_argument, NULL, 'M'},
451 {"dwarf", optional_argument, NULL, OPTION_DWARF},
452 {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
453 {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
454 {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
455 {"dynamic-reloc", no_argument, NULL, 'R'},
456 {"dynamic-syms", no_argument, NULL, 'T'},
457 {"endian", required_argument, NULL, OPTION_ENDIAN},
458 {"file-headers", no_argument, NULL, 'f'},
459 {"file-offsets", no_argument, NULL, 'F'},
460 {"file-start-context", no_argument, &file_start_context, 1},
461 {"full-contents", no_argument, NULL, 's'},
462 {"headers", no_argument, NULL, 'h'},
463 {"help", no_argument, NULL, 'H'},
464 {"include", required_argument, NULL, 'I'},
465 {"info", no_argument, NULL, 'i'},
466 {"inlines", no_argument, 0, OPTION_INLINES},
467 {"insn-width", required_argument, NULL, OPTION_INSN_WIDTH},
468 {"line-numbers", no_argument, NULL, 'l'},
469 {"no-addresses", no_argument, &no_addresses, 1},
470 {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
471 {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
472 {"no-show-raw-insn", no_argument, &show_raw_insn, -1},
473 {"prefix", required_argument, NULL, OPTION_PREFIX},
474 {"prefix-addresses", no_argument, &prefix_addresses, 1},
475 {"prefix-strip", required_argument, NULL, OPTION_PREFIX_STRIP},
476 {"private", required_argument, NULL, 'P'},
477 {"private-headers", no_argument, NULL, 'p'},
478 {"process-links", no_argument, &process_links, true},
479 {"recurse-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
480 {"recursion-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
481 {"reloc", no_argument, NULL, 'r'},
482 {"section", required_argument, NULL, 'j'},
483 {"section-headers", no_argument, NULL, 'h'},
484 {"show-raw-insn", no_argument, &show_raw_insn, 1},
485 {"source", no_argument, NULL, 'S'},
486 {"source-comment", optional_argument, NULL, OPTION_SOURCE_COMMENT},
487 {"special-syms", no_argument, &dump_special_syms, 1},
488 {"stabs", no_argument, NULL, 'G'},
489 {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
490 {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
491 {"syms", no_argument, NULL, 't'},
492 {"target", required_argument, NULL, 'b'},
493 {"unicode", required_argument, NULL, 'U'},
494 {"version", no_argument, NULL, 'V'},
495 {"visualize-jumps", optional_argument, 0, OPTION_VISUALIZE_JUMPS},
496 {"wide", no_argument, NULL, 'w'},
497 {NULL, no_argument, NULL, 0}
500 static void
501 nonfatal (const char *msg)
503 bfd_nonfatal (msg);
504 exit_status = 1;
507 /* Convert a potential UTF-8 encoded sequence in IN into characters in OUT.
508 The conversion format is controlled by the unicode_display variable.
509 Returns the number of characters added to OUT.
510 Returns the number of bytes consumed from IN in CONSUMED.
511 Always consumes at least one byte and displays at least one character. */
513 static unsigned int
514 display_utf8 (const unsigned char * in, char * out, unsigned int * consumed)
516 char * orig_out = out;
517 unsigned int nchars = 0;
518 unsigned int j;
520 if (unicode_display == unicode_default)
521 goto invalid;
523 if (in[0] < 0xc0)
524 goto invalid;
526 if ((in[1] & 0xc0) != 0x80)
527 goto invalid;
529 if ((in[0] & 0x20) == 0)
531 nchars = 2;
532 goto valid;
535 if ((in[2] & 0xc0) != 0x80)
536 goto invalid;
538 if ((in[0] & 0x10) == 0)
540 nchars = 3;
541 goto valid;
544 if ((in[3] & 0xc0) != 0x80)
545 goto invalid;
547 nchars = 4;
549 valid:
550 switch (unicode_display)
552 case unicode_locale:
553 /* Copy the bytes into the output buffer as is. */
554 memcpy (out, in, nchars);
555 out += nchars;
556 break;
558 case unicode_invalid:
559 case unicode_hex:
560 out += sprintf (out, "%c", unicode_display == unicode_hex ? '<' : '{');
561 out += sprintf (out, "0x");
562 for (j = 0; j < nchars; j++)
563 out += sprintf (out, "%02x", in [j]);
564 out += sprintf (out, "%c", unicode_display == unicode_hex ? '>' : '}');
565 break;
567 case unicode_highlight:
568 if (isatty (1))
569 out += sprintf (out, "\x1B[31;47m"); /* Red. */
570 /* Fall through. */
571 case unicode_escape:
572 switch (nchars)
574 case 2:
575 out += sprintf (out, "\\u%02x%02x",
576 ((in[0] & 0x1c) >> 2),
577 ((in[0] & 0x03) << 6) | (in[1] & 0x3f));
578 break;
580 case 3:
581 out += sprintf (out, "\\u%02x%02x",
582 ((in[0] & 0x0f) << 4) | ((in[1] & 0x3c) >> 2),
583 ((in[1] & 0x03) << 6) | ((in[2] & 0x3f)));
584 break;
586 case 4:
587 out += sprintf (out, "\\u%02x%02x%02x",
588 ((in[0] & 0x07) << 6) | ((in[1] & 0x3c) >> 2),
589 ((in[1] & 0x03) << 6) | ((in[2] & 0x3c) >> 2),
590 ((in[2] & 0x03) << 6) | ((in[3] & 0x3f)));
591 break;
592 default:
593 /* URG. */
594 break;
597 if (unicode_display == unicode_highlight && isatty (1))
598 out += sprintf (out, "\033[0m"); /* Default colour. */
599 break;
601 default:
602 /* URG */
603 break;
606 * consumed = nchars;
607 return out - orig_out;
609 invalid:
610 /* Not a valid UTF-8 sequence. */
611 *out = *in;
612 * consumed = 1;
613 return 1;
616 /* Returns a version of IN with any control characters
617 replaced by escape sequences. Uses a static buffer
618 if necessary.
620 If unicode display is enabled, then also handles the
621 conversion of unicode characters. */
623 static const char *
624 sanitize_string (const char * in)
626 static char * buffer = NULL;
627 static size_t buffer_len = 0;
628 const char * original = in;
629 char * out;
631 /* Paranoia. */
632 if (in == NULL)
633 return "";
635 /* See if any conversion is necessary. In the majority
636 of cases it will not be needed. */
639 unsigned char c = *in++;
641 if (c == 0)
642 return original;
644 if (ISCNTRL (c))
645 break;
647 if (unicode_display != unicode_default && c >= 0xc0)
648 break;
650 while (1);
652 /* Copy the input, translating as needed. */
653 in = original;
654 if (buffer_len < (strlen (in) * 9))
656 free ((void *) buffer);
657 buffer_len = strlen (in) * 9;
658 buffer = xmalloc (buffer_len + 1);
661 out = buffer;
664 unsigned char c = *in++;
666 if (c == 0)
667 break;
669 if (ISCNTRL (c))
671 *out++ = '^';
672 *out++ = c + 0x40;
674 else if (unicode_display != unicode_default && c >= 0xc0)
676 unsigned int num_consumed;
678 out += display_utf8 ((const unsigned char *)(in - 1), out, & num_consumed);
679 in += num_consumed - 1;
681 else
682 *out++ = c;
684 while (1);
686 *out = 0;
687 return buffer;
691 /* Returns TRUE if the specified section should be dumped. */
693 static bool
694 process_section_p (asection * section)
696 struct only * only;
698 if (only_list == NULL)
699 return true;
701 for (only = only_list; only; only = only->next)
702 if (strcmp (only->name, section->name) == 0)
704 only->seen = true;
705 return true;
708 return false;
711 /* Add an entry to the 'only' list. */
713 static void
714 add_only (char * name)
716 struct only * only;
718 /* First check to make sure that we do not
719 already have an entry for this name. */
720 for (only = only_list; only; only = only->next)
721 if (strcmp (only->name, name) == 0)
722 return;
724 only = xmalloc (sizeof * only);
725 only->name = name;
726 only->seen = false;
727 only->next = only_list;
728 only_list = only;
731 /* Release the memory used by the 'only' list.
732 PR 11225: Issue a warning message for unseen sections.
733 Only do this if none of the sections were seen. This is mainly to support
734 tools like the GAS testsuite where an object file is dumped with a list of
735 generic section names known to be present in a range of different file
736 formats. */
738 static void
739 free_only_list (void)
741 bool at_least_one_seen = false;
742 struct only * only;
743 struct only * next;
745 if (only_list == NULL)
746 return;
748 for (only = only_list; only; only = only->next)
749 if (only->seen)
751 at_least_one_seen = true;
752 break;
755 for (only = only_list; only; only = next)
757 if (! at_least_one_seen)
759 non_fatal (_("section '%s' mentioned in a -j option, "
760 "but not found in any input file"),
761 only->name);
762 exit_status = 1;
764 next = only->next;
765 free (only);
770 static void
771 dump_section_header (bfd *abfd, asection *section, void *data)
773 char *comma = "";
774 unsigned int opb = bfd_octets_per_byte (abfd, section);
775 int longest_section_name = *((int *) data);
777 /* Ignore linker created section. See elfNN_ia64_object_p in
778 bfd/elfxx-ia64.c. */
779 if (section->flags & SEC_LINKER_CREATED)
780 return;
782 /* PR 10413: Skip sections that we are ignoring. */
783 if (! process_section_p (section))
784 return;
786 printf ("%3d %-*s %08lx ", section->index, longest_section_name,
787 sanitize_string (bfd_section_name (section)),
788 (unsigned long) bfd_section_size (section) / opb);
789 bfd_printf_vma (abfd, bfd_section_vma (section));
790 printf (" ");
791 bfd_printf_vma (abfd, section->lma);
792 printf (" %08lx 2**%u", (unsigned long) section->filepos,
793 bfd_section_alignment (section));
794 if (! wide_output)
795 printf ("\n ");
796 printf (" ");
798 #define PF(x, y) \
799 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
801 PF (SEC_HAS_CONTENTS, "CONTENTS");
802 PF (SEC_ALLOC, "ALLOC");
803 PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
804 PF (SEC_LOAD, "LOAD");
805 PF (SEC_RELOC, "RELOC");
806 PF (SEC_READONLY, "READONLY");
807 PF (SEC_CODE, "CODE");
808 PF (SEC_DATA, "DATA");
809 PF (SEC_ROM, "ROM");
810 PF (SEC_DEBUGGING, "DEBUGGING");
811 PF (SEC_NEVER_LOAD, "NEVER_LOAD");
812 PF (SEC_EXCLUDE, "EXCLUDE");
813 PF (SEC_SORT_ENTRIES, "SORT_ENTRIES");
814 if (bfd_get_arch (abfd) == bfd_arch_tic54x)
816 PF (SEC_TIC54X_BLOCK, "BLOCK");
817 PF (SEC_TIC54X_CLINK, "CLINK");
819 PF (SEC_SMALL_DATA, "SMALL_DATA");
820 if (bfd_get_flavour (abfd) == bfd_target_coff_flavour)
822 PF (SEC_COFF_SHARED, "SHARED");
823 PF (SEC_COFF_NOREAD, "NOREAD");
825 else if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
827 PF (SEC_ELF_OCTETS, "OCTETS");
828 PF (SEC_ELF_PURECODE, "PURECODE");
830 PF (SEC_THREAD_LOCAL, "THREAD_LOCAL");
831 PF (SEC_GROUP, "GROUP");
832 if (bfd_get_arch (abfd) == bfd_arch_mep)
834 PF (SEC_MEP_VLIW, "VLIW");
837 if ((section->flags & SEC_LINK_ONCE) != 0)
839 const char *ls;
840 struct coff_comdat_info *comdat;
842 switch (section->flags & SEC_LINK_DUPLICATES)
844 default:
845 abort ();
846 case SEC_LINK_DUPLICATES_DISCARD:
847 ls = "LINK_ONCE_DISCARD";
848 break;
849 case SEC_LINK_DUPLICATES_ONE_ONLY:
850 ls = "LINK_ONCE_ONE_ONLY";
851 break;
852 case SEC_LINK_DUPLICATES_SAME_SIZE:
853 ls = "LINK_ONCE_SAME_SIZE";
854 break;
855 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
856 ls = "LINK_ONCE_SAME_CONTENTS";
857 break;
859 printf ("%s%s", comma, ls);
861 comdat = bfd_coff_get_comdat_section (abfd, section);
862 if (comdat != NULL)
863 printf (" (COMDAT %s %ld)", comdat->name, comdat->symbol);
865 comma = ", ";
868 printf ("\n");
869 #undef PF
872 /* Called on each SECTION in ABFD, update the int variable pointed to by
873 DATA which contains the string length of the longest section name. */
875 static void
876 find_longest_section_name (bfd *abfd ATTRIBUTE_UNUSED,
877 asection *section, void *data)
879 int *longest_so_far = (int *) data;
880 const char *name;
881 int len;
883 /* Ignore linker created section. */
884 if (section->flags & SEC_LINKER_CREATED)
885 return;
887 /* Skip sections that we are ignoring. */
888 if (! process_section_p (section))
889 return;
891 name = bfd_section_name (section);
892 len = (int) strlen (name);
893 if (len > *longest_so_far)
894 *longest_so_far = len;
897 static void
898 dump_headers (bfd *abfd)
900 /* The default width of 13 is just an arbitrary choice. */
901 int max_section_name_length = 13;
902 int bfd_vma_width;
904 #ifndef BFD64
905 bfd_vma_width = 10;
906 #else
907 /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */
908 if (bfd_get_arch_size (abfd) == 32)
909 bfd_vma_width = 10;
910 else
911 bfd_vma_width = 18;
912 #endif
914 printf (_("Sections:\n"));
916 if (wide_output)
917 bfd_map_over_sections (abfd, find_longest_section_name,
918 &max_section_name_length);
920 printf (_("Idx %-*s Size %-*s%-*sFile off Algn"),
921 max_section_name_length, "Name",
922 bfd_vma_width, "VMA",
923 bfd_vma_width, "LMA");
925 if (wide_output)
926 printf (_(" Flags"));
927 printf ("\n");
929 bfd_map_over_sections (abfd, dump_section_header,
930 &max_section_name_length);
933 static asymbol **
934 slurp_symtab (bfd *abfd)
936 asymbol **sy = NULL;
937 long storage;
939 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
941 symcount = 0;
942 return NULL;
945 storage = bfd_get_symtab_upper_bound (abfd);
946 if (storage < 0)
948 non_fatal (_("failed to read symbol table from: %s"), bfd_get_filename (abfd));
949 bfd_fatal (_("error message was"));
952 if (storage)
954 off_t filesize = bfd_get_file_size (abfd);
956 /* qv PR 24707. */
957 if (filesize > 0
958 && filesize < storage
959 /* The MMO file format supports its own special compression
960 technique, so its sections can be larger than the file size. */
961 && bfd_get_flavour (abfd) != bfd_target_mmo_flavour)
963 bfd_nonfatal_message (bfd_get_filename (abfd), abfd, NULL,
964 _("error: symbol table size (%#lx) "
965 "is larger than filesize (%#lx)"),
966 storage, (long) filesize);
967 exit_status = 1;
968 symcount = 0;
969 return NULL;
972 sy = (asymbol **) xmalloc (storage);
975 symcount = bfd_canonicalize_symtab (abfd, sy);
976 if (symcount < 0)
977 bfd_fatal (bfd_get_filename (abfd));
978 return sy;
981 /* Read in the dynamic symbols. */
983 static asymbol **
984 slurp_dynamic_symtab (bfd *abfd)
986 asymbol **sy = NULL;
987 long storage;
989 storage = bfd_get_dynamic_symtab_upper_bound (abfd);
990 if (storage < 0)
992 if (!(bfd_get_file_flags (abfd) & DYNAMIC))
994 non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd));
995 exit_status = 1;
996 dynsymcount = 0;
997 return NULL;
1000 bfd_fatal (bfd_get_filename (abfd));
1003 if (storage)
1004 sy = (asymbol **) xmalloc (storage);
1006 dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
1007 if (dynsymcount < 0)
1008 bfd_fatal (bfd_get_filename (abfd));
1009 return sy;
1012 /* Some symbol names are significant and should be kept in the
1013 table of sorted symbol names, even if they are marked as
1014 debugging/section symbols. */
1016 static bool
1017 is_significant_symbol_name (const char * name)
1019 return startswith (name, ".plt") || startswith (name, ".got");
1022 /* Filter out (in place) symbols that are useless for disassembly.
1023 COUNT is the number of elements in SYMBOLS.
1024 Return the number of useful symbols. */
1026 static long
1027 remove_useless_symbols (asymbol **symbols, long count)
1029 asymbol **in_ptr = symbols, **out_ptr = symbols;
1031 while (--count >= 0)
1033 asymbol *sym = *in_ptr++;
1035 if (sym->name == NULL || sym->name[0] == '\0')
1036 continue;
1037 if ((sym->flags & (BSF_DEBUGGING | BSF_SECTION_SYM))
1038 && ! is_significant_symbol_name (sym->name))
1039 continue;
1040 if (bfd_is_und_section (sym->section)
1041 || bfd_is_com_section (sym->section))
1042 continue;
1044 *out_ptr++ = sym;
1046 return out_ptr - symbols;
1049 static const asection *compare_section;
1051 /* Sort symbols into value order. */
1053 static int
1054 compare_symbols (const void *ap, const void *bp)
1056 const asymbol *a = * (const asymbol **) ap;
1057 const asymbol *b = * (const asymbol **) bp;
1058 const char *an;
1059 const char *bn;
1060 size_t anl;
1061 size_t bnl;
1062 bool as, af, bs, bf;
1063 flagword aflags;
1064 flagword bflags;
1066 if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
1067 return 1;
1068 else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
1069 return -1;
1071 /* Prefer symbols from the section currently being disassembled.
1072 Don't sort symbols from other sections by section, since there
1073 isn't much reason to prefer one section over another otherwise.
1074 See sym_ok comment for why we compare by section name. */
1075 as = strcmp (compare_section->name, a->section->name) == 0;
1076 bs = strcmp (compare_section->name, b->section->name) == 0;
1077 if (as && !bs)
1078 return -1;
1079 if (!as && bs)
1080 return 1;
1082 an = bfd_asymbol_name (a);
1083 bn = bfd_asymbol_name (b);
1084 anl = strlen (an);
1085 bnl = strlen (bn);
1087 /* The symbols gnu_compiled and gcc2_compiled convey no real
1088 information, so put them after other symbols with the same value. */
1089 af = (strstr (an, "gnu_compiled") != NULL
1090 || strstr (an, "gcc2_compiled") != NULL);
1091 bf = (strstr (bn, "gnu_compiled") != NULL
1092 || strstr (bn, "gcc2_compiled") != NULL);
1094 if (af && ! bf)
1095 return 1;
1096 if (! af && bf)
1097 return -1;
1099 /* We use a heuristic for the file name, to try to sort it after
1100 more useful symbols. It may not work on non Unix systems, but it
1101 doesn't really matter; the only difference is precisely which
1102 symbol names get printed. */
1104 #define file_symbol(s, sn, snl) \
1105 (((s)->flags & BSF_FILE) != 0 \
1106 || ((snl) > 2 \
1107 && (sn)[(snl) - 2] == '.' \
1108 && ((sn)[(snl) - 1] == 'o' \
1109 || (sn)[(snl) - 1] == 'a')))
1111 af = file_symbol (a, an, anl);
1112 bf = file_symbol (b, bn, bnl);
1114 if (af && ! bf)
1115 return 1;
1116 if (! af && bf)
1117 return -1;
1119 /* Sort function and object symbols before global symbols before
1120 local symbols before section symbols before debugging symbols. */
1122 aflags = a->flags;
1123 bflags = b->flags;
1125 if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING))
1127 if ((aflags & BSF_DEBUGGING) != 0)
1128 return 1;
1129 else
1130 return -1;
1132 if ((aflags & BSF_SECTION_SYM) != (bflags & BSF_SECTION_SYM))
1134 if ((aflags & BSF_SECTION_SYM) != 0)
1135 return 1;
1136 else
1137 return -1;
1139 if ((aflags & BSF_FUNCTION) != (bflags & BSF_FUNCTION))
1141 if ((aflags & BSF_FUNCTION) != 0)
1142 return -1;
1143 else
1144 return 1;
1146 if ((aflags & BSF_OBJECT) != (bflags & BSF_OBJECT))
1148 if ((aflags & BSF_OBJECT) != 0)
1149 return -1;
1150 else
1151 return 1;
1153 if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL))
1155 if ((aflags & BSF_LOCAL) != 0)
1156 return 1;
1157 else
1158 return -1;
1160 if ((aflags & BSF_GLOBAL) != (bflags & BSF_GLOBAL))
1162 if ((aflags & BSF_GLOBAL) != 0)
1163 return -1;
1164 else
1165 return 1;
1168 if (bfd_get_flavour (bfd_asymbol_bfd (a)) == bfd_target_elf_flavour
1169 && bfd_get_flavour (bfd_asymbol_bfd (b)) == bfd_target_elf_flavour)
1171 bfd_vma asz, bsz;
1173 asz = 0;
1174 if ((a->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0)
1175 asz = ((elf_symbol_type *) a)->internal_elf_sym.st_size;
1176 bsz = 0;
1177 if ((b->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0)
1178 bsz = ((elf_symbol_type *) b)->internal_elf_sym.st_size;
1179 if (asz != bsz)
1180 return asz > bsz ? -1 : 1;
1183 /* Symbols that start with '.' might be section names, so sort them
1184 after symbols that don't start with '.'. */
1185 if (an[0] == '.' && bn[0] != '.')
1186 return 1;
1187 if (an[0] != '.' && bn[0] == '.')
1188 return -1;
1190 /* Finally, if we can't distinguish them in any other way, try to
1191 get consistent results by sorting the symbols by name. */
1192 return strcmp (an, bn);
1195 /* Sort relocs into address order. */
1197 static int
1198 compare_relocs (const void *ap, const void *bp)
1200 const arelent *a = * (const arelent **) ap;
1201 const arelent *b = * (const arelent **) bp;
1203 if (a->address > b->address)
1204 return 1;
1205 else if (a->address < b->address)
1206 return -1;
1208 /* So that associated relocations tied to the same address show up
1209 in the correct order, we don't do any further sorting. */
1210 if (a > b)
1211 return 1;
1212 else if (a < b)
1213 return -1;
1214 else
1215 return 0;
1218 /* Print an address (VMA) to the output stream in INFO.
1219 If SKIP_ZEROES is TRUE, omit leading zeroes. */
1221 static void
1222 objdump_print_value (bfd_vma vma, struct disassemble_info *inf,
1223 bool skip_zeroes)
1225 char buf[30];
1226 char *p;
1227 struct objdump_disasm_info *aux;
1229 aux = (struct objdump_disasm_info *) inf->application_data;
1230 bfd_sprintf_vma (aux->abfd, buf, vma);
1231 if (! skip_zeroes)
1232 p = buf;
1233 else
1235 for (p = buf; *p == '0'; ++p)
1237 if (*p == '\0')
1238 --p;
1240 (*inf->fprintf_func) (inf->stream, "%s", p);
1243 /* Print the name of a symbol. */
1245 static void
1246 objdump_print_symname (bfd *abfd, struct disassemble_info *inf,
1247 asymbol *sym)
1249 char *alloc;
1250 const char *name, *version_string = NULL;
1251 bool hidden = false;
1253 alloc = NULL;
1254 name = bfd_asymbol_name (sym);
1255 if (do_demangle && name[0] != '\0')
1257 /* Demangle the name. */
1258 alloc = bfd_demangle (abfd, name, demangle_flags);
1259 if (alloc != NULL)
1260 name = alloc;
1263 if ((sym->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0)
1264 version_string = bfd_get_symbol_version_string (abfd, sym, true,
1265 &hidden);
1267 if (bfd_is_und_section (bfd_asymbol_section (sym)))
1268 hidden = true;
1270 name = sanitize_string (name);
1272 if (inf != NULL)
1274 (*inf->fprintf_func) (inf->stream, "%s", name);
1275 if (version_string && *version_string != '\0')
1276 (*inf->fprintf_func) (inf->stream, hidden ? "@%s" : "@@%s",
1277 version_string);
1279 else
1281 printf ("%s", name);
1282 if (version_string && *version_string != '\0')
1283 printf (hidden ? "@%s" : "@@%s", version_string);
1286 if (alloc != NULL)
1287 free (alloc);
1290 static inline bool
1291 sym_ok (bool want_section,
1292 bfd *abfd ATTRIBUTE_UNUSED,
1293 long place,
1294 asection *sec,
1295 struct disassemble_info *inf)
1297 if (want_section)
1299 /* NB: An object file can have different sections with the same
1300 section name. Compare compare section pointers if they have
1301 the same owner. */
1302 if (sorted_syms[place]->section->owner == sec->owner
1303 && sorted_syms[place]->section != sec)
1304 return false;
1306 /* Note - we cannot just compare section pointers because they could
1307 be different, but the same... Ie the symbol that we are trying to
1308 find could have come from a separate debug info file. Under such
1309 circumstances the symbol will be associated with a section in the
1310 debug info file, whilst the section we want is in a normal file.
1311 So the section pointers will be different, but the section names
1312 will be the same. */
1313 if (strcmp (bfd_section_name (sorted_syms[place]->section),
1314 bfd_section_name (sec)) != 0)
1315 return false;
1318 return inf->symbol_is_valid (sorted_syms[place], inf);
1321 /* Locate a symbol given a bfd and a section (from INFO->application_data),
1322 and a VMA. If INFO->application_data->require_sec is TRUE, then always
1323 require the symbol to be in the section. Returns NULL if there is no
1324 suitable symbol. If PLACE is not NULL, then *PLACE is set to the index
1325 of the symbol in sorted_syms. */
1327 static asymbol *
1328 find_symbol_for_address (bfd_vma vma,
1329 struct disassemble_info *inf,
1330 long *place)
1332 /* @@ Would it speed things up to cache the last two symbols returned,
1333 and maybe their address ranges? For many processors, only one memory
1334 operand can be present at a time, so the 2-entry cache wouldn't be
1335 constantly churned by code doing heavy memory accesses. */
1337 /* Indices in `sorted_syms'. */
1338 long min = 0;
1339 long max_count = sorted_symcount;
1340 long thisplace;
1341 struct objdump_disasm_info *aux;
1342 bfd *abfd;
1343 asection *sec;
1344 unsigned int opb;
1345 bool want_section;
1346 long rel_count;
1348 if (sorted_symcount < 1)
1349 return NULL;
1351 aux = (struct objdump_disasm_info *) inf->application_data;
1352 abfd = aux->abfd;
1353 sec = inf->section;
1354 opb = inf->octets_per_byte;
1356 /* Perform a binary search looking for the closest symbol to the
1357 required value. We are searching the range (min, max_count]. */
1358 while (min + 1 < max_count)
1360 asymbol *sym;
1362 thisplace = (max_count + min) / 2;
1363 sym = sorted_syms[thisplace];
1365 if (bfd_asymbol_value (sym) > vma)
1366 max_count = thisplace;
1367 else if (bfd_asymbol_value (sym) < vma)
1368 min = thisplace;
1369 else
1371 min = thisplace;
1372 break;
1376 /* The symbol we want is now in min, the low end of the range we
1377 were searching. If there are several symbols with the same
1378 value, we want the first one. */
1379 thisplace = min;
1380 while (thisplace > 0
1381 && (bfd_asymbol_value (sorted_syms[thisplace])
1382 == bfd_asymbol_value (sorted_syms[thisplace - 1])))
1383 --thisplace;
1385 /* Prefer a symbol in the current section if we have multple symbols
1386 with the same value, as can occur with overlays or zero size
1387 sections. */
1388 min = thisplace;
1389 while (min < max_count
1390 && (bfd_asymbol_value (sorted_syms[min])
1391 == bfd_asymbol_value (sorted_syms[thisplace])))
1393 if (sym_ok (true, abfd, min, sec, inf))
1395 thisplace = min;
1397 if (place != NULL)
1398 *place = thisplace;
1400 return sorted_syms[thisplace];
1402 ++min;
1405 /* If the file is relocatable, and the symbol could be from this
1406 section, prefer a symbol from this section over symbols from
1407 others, even if the other symbol's value might be closer.
1409 Note that this may be wrong for some symbol references if the
1410 sections have overlapping memory ranges, but in that case there's
1411 no way to tell what's desired without looking at the relocation
1412 table.
1414 Also give the target a chance to reject symbols. */
1415 want_section = (aux->require_sec
1416 || ((abfd->flags & HAS_RELOC) != 0
1417 && vma >= bfd_section_vma (sec)
1418 && vma < (bfd_section_vma (sec)
1419 + bfd_section_size (sec) / opb)));
1421 if (! sym_ok (want_section, abfd, thisplace, sec, inf))
1423 long i;
1424 long newplace = sorted_symcount;
1426 for (i = min - 1; i >= 0; i--)
1428 if (sym_ok (want_section, abfd, i, sec, inf))
1430 if (newplace == sorted_symcount)
1431 newplace = i;
1433 if (bfd_asymbol_value (sorted_syms[i])
1434 != bfd_asymbol_value (sorted_syms[newplace]))
1435 break;
1437 /* Remember this symbol and keep searching until we reach
1438 an earlier address. */
1439 newplace = i;
1443 if (newplace != sorted_symcount)
1444 thisplace = newplace;
1445 else
1447 /* We didn't find a good symbol with a smaller value.
1448 Look for one with a larger value. */
1449 for (i = thisplace + 1; i < sorted_symcount; i++)
1451 if (sym_ok (want_section, abfd, i, sec, inf))
1453 thisplace = i;
1454 break;
1459 if (! sym_ok (want_section, abfd, thisplace, sec, inf))
1460 /* There is no suitable symbol. */
1461 return NULL;
1464 /* If we have not found an exact match for the specified address
1465 and we have dynamic relocations available, then we can produce
1466 a better result by matching a relocation to the address and
1467 using the symbol associated with that relocation. */
1468 rel_count = inf->dynrelcount;
1469 if (!want_section
1470 && sorted_syms[thisplace]->value != vma
1471 && rel_count > 0
1472 && inf->dynrelbuf != NULL
1473 && inf->dynrelbuf[0]->address <= vma
1474 && inf->dynrelbuf[rel_count - 1]->address >= vma
1475 /* If we have matched a synthetic symbol, then stick with that. */
1476 && (sorted_syms[thisplace]->flags & BSF_SYNTHETIC) == 0)
1478 arelent ** rel_low;
1479 arelent ** rel_high;
1481 rel_low = inf->dynrelbuf;
1482 rel_high = rel_low + rel_count - 1;
1483 while (rel_low <= rel_high)
1485 arelent **rel_mid = &rel_low[(rel_high - rel_low) / 2];
1486 arelent * rel = *rel_mid;
1488 if (rel->address == vma)
1490 /* Absolute relocations do not provide a more helpful
1491 symbolic address. Find a non-absolute relocation
1492 with the same address. */
1493 arelent **rel_vma = rel_mid;
1494 for (rel_mid--;
1495 rel_mid >= rel_low && rel_mid[0]->address == vma;
1496 rel_mid--)
1497 rel_vma = rel_mid;
1499 for (; rel_vma <= rel_high && rel_vma[0]->address == vma;
1500 rel_vma++)
1502 rel = *rel_vma;
1503 if (rel->sym_ptr_ptr != NULL
1504 && ! bfd_is_abs_section ((* rel->sym_ptr_ptr)->section))
1506 if (place != NULL)
1507 * place = thisplace;
1508 return * rel->sym_ptr_ptr;
1511 break;
1514 if (vma < rel->address)
1515 rel_high = rel_mid;
1516 else if (vma >= rel_mid[1]->address)
1517 rel_low = rel_mid + 1;
1518 else
1519 break;
1523 if (place != NULL)
1524 *place = thisplace;
1526 return sorted_syms[thisplace];
1529 /* Print an address and the offset to the nearest symbol. */
1531 static void
1532 objdump_print_addr_with_sym (bfd *abfd, asection *sec, asymbol *sym,
1533 bfd_vma vma, struct disassemble_info *inf,
1534 bool skip_zeroes)
1536 if (!no_addresses)
1538 objdump_print_value (vma, inf, skip_zeroes);
1539 (*inf->fprintf_func) (inf->stream, " ");
1542 if (sym == NULL)
1544 bfd_vma secaddr;
1546 (*inf->fprintf_func) (inf->stream, "<%s",
1547 sanitize_string (bfd_section_name (sec)));
1548 secaddr = bfd_section_vma (sec);
1549 if (vma < secaddr)
1551 (*inf->fprintf_func) (inf->stream, "-0x");
1552 objdump_print_value (secaddr - vma, inf, true);
1554 else if (vma > secaddr)
1556 (*inf->fprintf_func) (inf->stream, "+0x");
1557 objdump_print_value (vma - secaddr, inf, true);
1559 (*inf->fprintf_func) (inf->stream, ">");
1561 else
1563 (*inf->fprintf_func) (inf->stream, "<");
1565 objdump_print_symname (abfd, inf, sym);
1567 if (bfd_asymbol_value (sym) == vma)
1569 /* Undefined symbols in an executables and dynamic objects do not have
1570 a value associated with them, so it does not make sense to display
1571 an offset relative to them. Normally we would not be provided with
1572 this kind of symbol, but the target backend might choose to do so,
1573 and the code in find_symbol_for_address might return an as yet
1574 unresolved symbol associated with a dynamic reloc. */
1575 else if ((bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC))
1576 && bfd_is_und_section (sym->section))
1578 else if (bfd_asymbol_value (sym) > vma)
1580 (*inf->fprintf_func) (inf->stream, "-0x");
1581 objdump_print_value (bfd_asymbol_value (sym) - vma, inf, true);
1583 else if (vma > bfd_asymbol_value (sym))
1585 (*inf->fprintf_func) (inf->stream, "+0x");
1586 objdump_print_value (vma - bfd_asymbol_value (sym), inf, true);
1589 (*inf->fprintf_func) (inf->stream, ">");
1592 if (display_file_offsets)
1593 inf->fprintf_func (inf->stream, _(" (File Offset: 0x%lx)"),
1594 (long int)(sec->filepos + (vma - sec->vma)));
1597 /* Print an address (VMA), symbolically if possible.
1598 If SKIP_ZEROES is TRUE, don't output leading zeroes. */
1600 static void
1601 objdump_print_addr (bfd_vma vma,
1602 struct disassemble_info *inf,
1603 bool skip_zeroes)
1605 struct objdump_disasm_info *aux;
1606 asymbol *sym = NULL;
1607 bool skip_find = false;
1609 aux = (struct objdump_disasm_info *) inf->application_data;
1611 if (sorted_symcount < 1)
1613 if (!no_addresses)
1615 (*inf->fprintf_func) (inf->stream, "0x");
1616 objdump_print_value (vma, inf, skip_zeroes);
1619 if (display_file_offsets)
1620 inf->fprintf_func (inf->stream, _(" (File Offset: 0x%lx)"),
1621 (long int) (inf->section->filepos
1622 + (vma - inf->section->vma)));
1623 return;
1626 if (aux->reloc != NULL
1627 && aux->reloc->sym_ptr_ptr != NULL
1628 && * aux->reloc->sym_ptr_ptr != NULL)
1630 sym = * aux->reloc->sym_ptr_ptr;
1632 /* Adjust the vma to the reloc. */
1633 vma += bfd_asymbol_value (sym);
1635 if (bfd_is_und_section (bfd_asymbol_section (sym)))
1636 skip_find = true;
1639 if (!skip_find)
1640 sym = find_symbol_for_address (vma, inf, NULL);
1642 objdump_print_addr_with_sym (aux->abfd, inf->section, sym, vma, inf,
1643 skip_zeroes);
1646 /* Print VMA to INFO. This function is passed to the disassembler
1647 routine. */
1649 static void
1650 objdump_print_address (bfd_vma vma, struct disassemble_info *inf)
1652 objdump_print_addr (vma, inf, ! prefix_addresses);
1655 /* Determine if the given address has a symbol associated with it. */
1657 static asymbol *
1658 objdump_symbol_at_address (bfd_vma vma, struct disassemble_info * inf)
1660 asymbol * sym;
1662 sym = find_symbol_for_address (vma, inf, NULL);
1663 if (sym != NULL && bfd_asymbol_value (sym) == vma)
1664 return sym;
1666 return NULL;
1669 /* Hold the last function name and the last line number we displayed
1670 in a disassembly. */
1672 static char *prev_functionname;
1673 static unsigned int prev_line;
1674 static unsigned int prev_discriminator;
1676 /* We keep a list of all files that we have seen when doing a
1677 disassembly with source, so that we know how much of the file to
1678 display. This can be important for inlined functions. */
1680 struct print_file_list
1682 struct print_file_list *next;
1683 const char *filename;
1684 const char *modname;
1685 const char *map;
1686 size_t mapsize;
1687 const char **linemap;
1688 unsigned maxline;
1689 unsigned last_line;
1690 unsigned max_printed;
1691 int first;
1694 static struct print_file_list *print_files;
1696 /* The number of preceding context lines to show when we start
1697 displaying a file for the first time. */
1699 #define SHOW_PRECEDING_CONTEXT_LINES (5)
1701 /* Read a complete file into memory. */
1703 static const char *
1704 slurp_file (const char *fn, size_t *size, struct stat *fst)
1706 #ifdef HAVE_MMAP
1707 int ps = getpagesize ();
1708 size_t msize;
1709 #endif
1710 const char *map;
1711 int fd = open (fn, O_RDONLY | O_BINARY);
1713 if (fd < 0)
1714 return NULL;
1715 if (fstat (fd, fst) < 0)
1717 close (fd);
1718 return NULL;
1720 *size = fst->st_size;
1721 #ifdef HAVE_MMAP
1722 msize = (*size + ps - 1) & ~(ps - 1);
1723 map = mmap (NULL, msize, PROT_READ, MAP_SHARED, fd, 0);
1724 if (map != (char *) -1L)
1726 close (fd);
1727 return map;
1729 #endif
1730 map = (const char *) malloc (*size);
1731 if (!map || (size_t) read (fd, (char *) map, *size) != *size)
1733 free ((void *) map);
1734 map = NULL;
1736 close (fd);
1737 return map;
1740 #define line_map_decrease 5
1742 /* Precompute array of lines for a mapped file. */
1744 static const char **
1745 index_file (const char *map, size_t size, unsigned int *maxline)
1747 const char *p, *lstart, *end;
1748 int chars_per_line = 45; /* First iteration will use 40. */
1749 unsigned int lineno;
1750 const char **linemap = NULL;
1751 unsigned long line_map_size = 0;
1753 lineno = 0;
1754 lstart = map;
1755 end = map + size;
1757 for (p = map; p < end; p++)
1759 if (*p == '\n')
1761 if (p + 1 < end && p[1] == '\r')
1762 p++;
1764 else if (*p == '\r')
1766 if (p + 1 < end && p[1] == '\n')
1767 p++;
1769 else
1770 continue;
1772 /* End of line found. */
1774 if (linemap == NULL || line_map_size < lineno + 1)
1776 unsigned long newsize;
1778 chars_per_line -= line_map_decrease;
1779 if (chars_per_line <= 1)
1780 chars_per_line = 1;
1781 line_map_size = size / chars_per_line + 1;
1782 if (line_map_size < lineno + 1)
1783 line_map_size = lineno + 1;
1784 newsize = line_map_size * sizeof (char *);
1785 linemap = (const char **) xrealloc (linemap, newsize);
1788 linemap[lineno++] = lstart;
1789 lstart = p + 1;
1792 *maxline = lineno;
1793 return linemap;
1796 /* Tries to open MODNAME, and if successful adds a node to print_files
1797 linked list and returns that node. Returns NULL on failure. */
1799 static struct print_file_list *
1800 try_print_file_open (const char *origname, const char *modname, struct stat *fst)
1802 struct print_file_list *p;
1804 p = (struct print_file_list *) xmalloc (sizeof (struct print_file_list));
1806 p->map = slurp_file (modname, &p->mapsize, fst);
1807 if (p->map == NULL)
1809 free (p);
1810 return NULL;
1813 p->linemap = index_file (p->map, p->mapsize, &p->maxline);
1814 p->last_line = 0;
1815 p->max_printed = 0;
1816 p->filename = origname;
1817 p->modname = modname;
1818 p->next = print_files;
1819 p->first = 1;
1820 print_files = p;
1821 return p;
1824 /* If the source file, as described in the symtab, is not found
1825 try to locate it in one of the paths specified with -I
1826 If found, add location to print_files linked list. */
1828 static struct print_file_list *
1829 update_source_path (const char *filename, bfd *abfd)
1831 struct print_file_list *p;
1832 const char *fname;
1833 struct stat fst;
1834 int i;
1836 p = try_print_file_open (filename, filename, &fst);
1837 if (p == NULL)
1839 if (include_path_count == 0)
1840 return NULL;
1842 /* Get the name of the file. */
1843 fname = lbasename (filename);
1845 /* If file exists under a new path, we need to add it to the list
1846 so that show_line knows about it. */
1847 for (i = 0; i < include_path_count; i++)
1849 char *modname = concat (include_paths[i], "/", fname,
1850 (const char *) 0);
1852 p = try_print_file_open (filename, modname, &fst);
1853 if (p)
1854 break;
1856 free (modname);
1860 if (p != NULL)
1862 long mtime = bfd_get_mtime (abfd);
1864 if (fst.st_mtime > mtime)
1865 warn (_("source file %s is more recent than object file\n"),
1866 filename);
1869 return p;
1872 /* Print a source file line. */
1874 static void
1875 print_line (struct print_file_list *p, unsigned int linenum)
1877 const char *l;
1878 size_t len;
1880 --linenum;
1881 if (linenum >= p->maxline)
1882 return;
1883 l = p->linemap [linenum];
1884 if (source_comment != NULL && strlen (l) > 0)
1885 printf ("%s", source_comment);
1886 len = strcspn (l, "\n\r");
1887 /* Test fwrite return value to quiet glibc warning. */
1888 if (len == 0 || fwrite (l, len, 1, stdout) == 1)
1889 putchar ('\n');
1892 /* Print a range of source code lines. */
1894 static void
1895 dump_lines (struct print_file_list *p, unsigned int start, unsigned int end)
1897 if (p->map == NULL)
1898 return;
1899 while (start <= end)
1901 print_line (p, start);
1902 start++;
1906 /* Show the line number, or the source line, in a disassembly
1907 listing. */
1909 static void
1910 show_line (bfd *abfd, asection *section, bfd_vma addr_offset)
1912 const char *filename;
1913 const char *functionname;
1914 unsigned int linenumber;
1915 unsigned int discriminator;
1916 bool reloc;
1917 char *path = NULL;
1919 if (! with_line_numbers && ! with_source_code)
1920 return;
1922 if (! bfd_find_nearest_line_discriminator (abfd, section, syms, addr_offset,
1923 &filename, &functionname,
1924 &linenumber, &discriminator))
1925 return;
1927 if (filename != NULL && *filename == '\0')
1928 filename = NULL;
1929 if (functionname != NULL && *functionname == '\0')
1930 functionname = NULL;
1932 if (filename
1933 && IS_ABSOLUTE_PATH (filename)
1934 && prefix)
1936 char *path_up;
1937 const char *fname = filename;
1939 path = xmalloc (prefix_length + 1 + strlen (filename));
1941 if (prefix_length)
1942 memcpy (path, prefix, prefix_length);
1943 path_up = path + prefix_length;
1945 /* Build relocated filename, stripping off leading directories
1946 from the initial filename if requested. */
1947 if (prefix_strip > 0)
1949 int level = 0;
1950 const char *s;
1952 /* Skip selected directory levels. */
1953 for (s = fname + 1; *s != '\0' && level < prefix_strip; s++)
1954 if (IS_DIR_SEPARATOR (*s))
1956 fname = s;
1957 level++;
1961 /* Update complete filename. */
1962 strcpy (path_up, fname);
1964 filename = path;
1965 reloc = true;
1967 else
1968 reloc = false;
1970 if (with_line_numbers)
1972 if (functionname != NULL
1973 && (prev_functionname == NULL
1974 || strcmp (functionname, prev_functionname) != 0))
1976 char *demangle_alloc = NULL;
1977 if (do_demangle && functionname[0] != '\0')
1979 /* Demangle the name. */
1980 demangle_alloc = bfd_demangle (abfd, functionname,
1981 demangle_flags);
1984 /* Demangling adds trailing parens, so don't print those. */
1985 if (demangle_alloc != NULL)
1986 printf ("%s:\n", sanitize_string (demangle_alloc));
1987 else
1988 printf ("%s():\n", sanitize_string (functionname));
1990 prev_line = -1;
1991 free (demangle_alloc);
1993 if (linenumber > 0
1994 && (linenumber != prev_line
1995 || discriminator != prev_discriminator))
1997 if (discriminator > 0)
1998 printf ("%s:%u (discriminator %u)\n",
1999 filename == NULL ? "???" : sanitize_string (filename),
2000 linenumber, discriminator);
2001 else
2002 printf ("%s:%u\n", filename == NULL
2003 ? "???" : sanitize_string (filename),
2004 linenumber);
2006 if (unwind_inlines)
2008 const char *filename2;
2009 const char *functionname2;
2010 unsigned line2;
2012 while (bfd_find_inliner_info (abfd, &filename2, &functionname2,
2013 &line2))
2015 printf ("inlined by %s:%u",
2016 sanitize_string (filename2), line2);
2017 printf (" (%s)\n", sanitize_string (functionname2));
2022 if (with_source_code
2023 && filename != NULL
2024 && linenumber > 0)
2026 struct print_file_list **pp, *p;
2027 unsigned l;
2029 for (pp = &print_files; *pp != NULL; pp = &(*pp)->next)
2030 if (filename_cmp ((*pp)->filename, filename) == 0)
2031 break;
2032 p = *pp;
2034 if (p == NULL)
2036 if (reloc)
2037 filename = xstrdup (filename);
2038 p = update_source_path (filename, abfd);
2041 if (p != NULL && linenumber != p->last_line)
2043 if (file_start_context && p->first)
2044 l = 1;
2045 else
2047 l = linenumber - SHOW_PRECEDING_CONTEXT_LINES;
2048 if (l >= linenumber)
2049 l = 1;
2050 if (p->max_printed >= l)
2052 if (p->max_printed < linenumber)
2053 l = p->max_printed + 1;
2054 else
2055 l = linenumber;
2058 dump_lines (p, l, linenumber);
2059 if (p->max_printed < linenumber)
2060 p->max_printed = linenumber;
2061 p->last_line = linenumber;
2062 p->first = 0;
2066 if (functionname != NULL
2067 && (prev_functionname == NULL
2068 || strcmp (functionname, prev_functionname) != 0))
2070 if (prev_functionname != NULL)
2071 free (prev_functionname);
2072 prev_functionname = (char *) xmalloc (strlen (functionname) + 1);
2073 strcpy (prev_functionname, functionname);
2076 if (linenumber > 0 && linenumber != prev_line)
2077 prev_line = linenumber;
2079 if (discriminator != prev_discriminator)
2080 prev_discriminator = discriminator;
2082 if (path)
2083 free (path);
2086 /* Pseudo FILE object for strings. */
2087 typedef struct
2089 char *buffer;
2090 size_t pos;
2091 size_t alloc;
2092 } SFILE;
2094 /* sprintf to a "stream". */
2096 static int ATTRIBUTE_PRINTF_2
2097 objdump_sprintf (SFILE *f, const char *format, ...)
2099 size_t n;
2100 va_list args;
2102 while (1)
2104 size_t space = f->alloc - f->pos;
2106 va_start (args, format);
2107 n = vsnprintf (f->buffer + f->pos, space, format, args);
2108 va_end (args);
2110 if (space > n)
2111 break;
2113 f->alloc = (f->alloc + n) * 2;
2114 f->buffer = (char *) xrealloc (f->buffer, f->alloc);
2116 f->pos += n;
2118 return n;
2121 /* Code for generating (colored) diagrams of control flow start and end
2122 points. */
2124 /* Structure used to store the properties of a jump. */
2126 struct jump_info
2128 /* The next jump, or NULL if this is the last object. */
2129 struct jump_info *next;
2130 /* The previous jump, or NULL if this is the first object. */
2131 struct jump_info *prev;
2132 /* The start addresses of the jump. */
2133 struct
2135 /* The list of start addresses. */
2136 bfd_vma *addresses;
2137 /* The number of elements. */
2138 size_t count;
2139 /* The maximum number of elements that fit into the array. */
2140 size_t max_count;
2141 } start;
2142 /* The end address of the jump. */
2143 bfd_vma end;
2144 /* The drawing level of the jump. */
2145 int level;
2148 /* Construct a jump object for a jump from start
2149 to end with the corresponding level. */
2151 static struct jump_info *
2152 jump_info_new (bfd_vma start, bfd_vma end, int level)
2154 struct jump_info *result = xmalloc (sizeof (struct jump_info));
2156 result->next = NULL;
2157 result->prev = NULL;
2158 result->start.addresses = xmalloc (sizeof (bfd_vma *) * 2);
2159 result->start.addresses[0] = start;
2160 result->start.count = 1;
2161 result->start.max_count = 2;
2162 result->end = end;
2163 result->level = level;
2165 return result;
2168 /* Free a jump object and return the next object
2169 or NULL if this was the last one. */
2171 static struct jump_info *
2172 jump_info_free (struct jump_info *ji)
2174 struct jump_info *result = NULL;
2176 if (ji)
2178 result = ji->next;
2179 if (ji->start.addresses)
2180 free (ji->start.addresses);
2181 free (ji);
2184 return result;
2187 /* Get the smallest value of all start and end addresses. */
2189 static bfd_vma
2190 jump_info_min_address (const struct jump_info *ji)
2192 bfd_vma min_address = ji->end;
2193 size_t i;
2195 for (i = ji->start.count; i-- > 0;)
2196 if (ji->start.addresses[i] < min_address)
2197 min_address = ji->start.addresses[i];
2198 return min_address;
2201 /* Get the largest value of all start and end addresses. */
2203 static bfd_vma
2204 jump_info_max_address (const struct jump_info *ji)
2206 bfd_vma max_address = ji->end;
2207 size_t i;
2209 for (i = ji->start.count; i-- > 0;)
2210 if (ji->start.addresses[i] > max_address)
2211 max_address = ji->start.addresses[i];
2212 return max_address;
2215 /* Get the target address of a jump. */
2217 static bfd_vma
2218 jump_info_end_address (const struct jump_info *ji)
2220 return ji->end;
2223 /* Test if an address is one of the start addresses of a jump. */
2225 static bool
2226 jump_info_is_start_address (const struct jump_info *ji, bfd_vma address)
2228 bool result = false;
2229 size_t i;
2231 for (i = ji->start.count; i-- > 0;)
2232 if (address == ji->start.addresses[i])
2234 result = true;
2235 break;
2238 return result;
2241 /* Test if an address is the target address of a jump. */
2243 static bool
2244 jump_info_is_end_address (const struct jump_info *ji, bfd_vma address)
2246 return (address == ji->end);
2249 /* Get the difference between the smallest and largest address of a jump. */
2251 static bfd_vma
2252 jump_info_size (const struct jump_info *ji)
2254 return jump_info_max_address (ji) - jump_info_min_address (ji);
2257 /* Unlink a jump object from a list. */
2259 static void
2260 jump_info_unlink (struct jump_info *node,
2261 struct jump_info **base)
2263 if (node->next)
2264 node->next->prev = node->prev;
2265 if (node->prev)
2266 node->prev->next = node->next;
2267 else
2268 *base = node->next;
2269 node->next = NULL;
2270 node->prev = NULL;
2273 /* Insert unlinked jump info node into a list. */
2275 static void
2276 jump_info_insert (struct jump_info *node,
2277 struct jump_info *target,
2278 struct jump_info **base)
2280 node->next = target;
2281 node->prev = target->prev;
2282 target->prev = node;
2283 if (node->prev)
2284 node->prev->next = node;
2285 else
2286 *base = node;
2289 /* Add unlinked node to the front of a list. */
2291 static void
2292 jump_info_add_front (struct jump_info *node,
2293 struct jump_info **base)
2295 node->next = *base;
2296 if (node->next)
2297 node->next->prev = node;
2298 node->prev = NULL;
2299 *base = node;
2302 /* Move linked node to target position. */
2304 static void
2305 jump_info_move_linked (struct jump_info *node,
2306 struct jump_info *target,
2307 struct jump_info **base)
2309 /* Unlink node. */
2310 jump_info_unlink (node, base);
2311 /* Insert node at target position. */
2312 jump_info_insert (node, target, base);
2315 /* Test if two jumps intersect. */
2317 static bool
2318 jump_info_intersect (const struct jump_info *a,
2319 const struct jump_info *b)
2321 return ((jump_info_max_address (a) >= jump_info_min_address (b))
2322 && (jump_info_min_address (a) <= jump_info_max_address (b)));
2325 /* Merge two compatible jump info objects. */
2327 static void
2328 jump_info_merge (struct jump_info **base)
2330 struct jump_info *a;
2332 for (a = *base; a; a = a->next)
2334 struct jump_info *b;
2336 for (b = a->next; b; b = b->next)
2338 /* Merge both jumps into one. */
2339 if (a->end == b->end)
2341 /* Reallocate addresses. */
2342 size_t needed_size = a->start.count + b->start.count;
2343 size_t i;
2345 if (needed_size > a->start.max_count)
2347 a->start.max_count += b->start.max_count;
2348 a->start.addresses =
2349 xrealloc (a->start.addresses,
2350 a->start.max_count * sizeof (bfd_vma *));
2353 /* Append start addresses. */
2354 for (i = 0; i < b->start.count; ++i)
2355 a->start.addresses[a->start.count++] =
2356 b->start.addresses[i];
2358 /* Remove and delete jump. */
2359 struct jump_info *tmp = b->prev;
2360 jump_info_unlink (b, base);
2361 jump_info_free (b);
2362 b = tmp;
2368 /* Sort jumps by their size and starting point using a stable
2369 minsort. This could be improved if sorting performance is
2370 an issue, for example by using mergesort. */
2372 static void
2373 jump_info_sort (struct jump_info **base)
2375 struct jump_info *current_element = *base;
2377 while (current_element)
2379 struct jump_info *best_match = current_element;
2380 struct jump_info *runner = current_element->next;
2381 bfd_vma best_size = jump_info_size (best_match);
2383 while (runner)
2385 bfd_vma runner_size = jump_info_size (runner);
2387 if ((runner_size < best_size)
2388 || ((runner_size == best_size)
2389 && (jump_info_min_address (runner)
2390 < jump_info_min_address (best_match))))
2392 best_match = runner;
2393 best_size = runner_size;
2396 runner = runner->next;
2399 if (best_match == current_element)
2400 current_element = current_element->next;
2401 else
2402 jump_info_move_linked (best_match, current_element, base);
2406 /* Visualize all jumps at a given address. */
2408 static void
2409 jump_info_visualize_address (bfd_vma address,
2410 int max_level,
2411 char *line_buffer,
2412 uint8_t *color_buffer)
2414 struct jump_info *ji = detected_jumps;
2415 size_t len = (max_level + 1) * 3;
2417 /* Clear line buffer. */
2418 memset (line_buffer, ' ', len);
2419 memset (color_buffer, 0, len);
2421 /* Iterate over jumps and add their ASCII art. */
2422 while (ji)
2424 /* Discard jumps that are never needed again. */
2425 if (jump_info_max_address (ji) < address)
2427 struct jump_info *tmp = ji;
2429 ji = ji->next;
2430 jump_info_unlink (tmp, &detected_jumps);
2431 jump_info_free (tmp);
2432 continue;
2435 /* This jump intersects with the current address. */
2436 if (jump_info_min_address (ji) <= address)
2438 /* Hash target address to get an even
2439 distribution between all values. */
2440 bfd_vma hash_address = jump_info_end_address (ji);
2441 uint8_t color = iterative_hash_object (hash_address, 0);
2442 /* Fetch line offset. */
2443 int offset = (max_level - ji->level) * 3;
2445 /* Draw start line. */
2446 if (jump_info_is_start_address (ji, address))
2448 size_t i = offset + 1;
2450 for (; i < len - 1; ++i)
2451 if (line_buffer[i] == ' ')
2453 line_buffer[i] = '-';
2454 color_buffer[i] = color;
2457 if (line_buffer[i] == ' ')
2459 line_buffer[i] = '-';
2460 color_buffer[i] = color;
2462 else if (line_buffer[i] == '>')
2464 line_buffer[i] = 'X';
2465 color_buffer[i] = color;
2468 if (line_buffer[offset] == ' ')
2470 if (address <= ji->end)
2471 line_buffer[offset] =
2472 (jump_info_min_address (ji) == address) ? '/': '+';
2473 else
2474 line_buffer[offset] =
2475 (jump_info_max_address (ji) == address) ? '\\': '+';
2476 color_buffer[offset] = color;
2479 /* Draw jump target. */
2480 else if (jump_info_is_end_address (ji, address))
2482 size_t i = offset + 1;
2484 for (; i < len - 1; ++i)
2485 if (line_buffer[i] == ' ')
2487 line_buffer[i] = '-';
2488 color_buffer[i] = color;
2491 if (line_buffer[i] == ' ')
2493 line_buffer[i] = '>';
2494 color_buffer[i] = color;
2496 else if (line_buffer[i] == '-')
2498 line_buffer[i] = 'X';
2499 color_buffer[i] = color;
2502 if (line_buffer[offset] == ' ')
2504 if (jump_info_min_address (ji) < address)
2505 line_buffer[offset] =
2506 (jump_info_max_address (ji) > address) ? '>' : '\\';
2507 else
2508 line_buffer[offset] = '/';
2509 color_buffer[offset] = color;
2512 /* Draw intermediate line segment. */
2513 else if (line_buffer[offset] == ' ')
2515 line_buffer[offset] = '|';
2516 color_buffer[offset] = color;
2520 ji = ji->next;
2524 /* Clone of disassemble_bytes to detect jumps inside a function. */
2525 /* FIXME: is this correct? Can we strip it down even further? */
2527 static struct jump_info *
2528 disassemble_jumps (struct disassemble_info * inf,
2529 disassembler_ftype disassemble_fn,
2530 bfd_vma start_offset,
2531 bfd_vma stop_offset,
2532 bfd_vma rel_offset,
2533 arelent *** relppp,
2534 arelent ** relppend)
2536 struct objdump_disasm_info *aux;
2537 struct jump_info *jumps = NULL;
2538 asection *section;
2539 bfd_vma addr_offset;
2540 unsigned int opb = inf->octets_per_byte;
2541 int octets = opb;
2542 SFILE sfile;
2544 aux = (struct objdump_disasm_info *) inf->application_data;
2545 section = inf->section;
2547 sfile.alloc = 120;
2548 sfile.buffer = (char *) xmalloc (sfile.alloc);
2549 sfile.pos = 0;
2551 inf->insn_info_valid = 0;
2552 inf->fprintf_func = (fprintf_ftype) objdump_sprintf;
2553 inf->stream = &sfile;
2555 addr_offset = start_offset;
2556 while (addr_offset < stop_offset)
2558 int previous_octets;
2560 /* Remember the length of the previous instruction. */
2561 previous_octets = octets;
2562 octets = 0;
2564 sfile.pos = 0;
2565 inf->bytes_per_line = 0;
2566 inf->bytes_per_chunk = 0;
2567 inf->flags = ((disassemble_all ? DISASSEMBLE_DATA : 0)
2568 | (wide_output ? WIDE_OUTPUT : 0));
2569 if (machine)
2570 inf->flags |= USER_SPECIFIED_MACHINE_TYPE;
2572 if (inf->disassembler_needs_relocs
2573 && (bfd_get_file_flags (aux->abfd) & EXEC_P) == 0
2574 && (bfd_get_file_flags (aux->abfd) & DYNAMIC) == 0
2575 && *relppp < relppend)
2577 bfd_signed_vma distance_to_rel;
2579 distance_to_rel = (**relppp)->address - (rel_offset + addr_offset);
2581 /* Check to see if the current reloc is associated with
2582 the instruction that we are about to disassemble. */
2583 if (distance_to_rel == 0
2584 /* FIXME: This is wrong. We are trying to catch
2585 relocs that are addressed part way through the
2586 current instruction, as might happen with a packed
2587 VLIW instruction. Unfortunately we do not know the
2588 length of the current instruction since we have not
2589 disassembled it yet. Instead we take a guess based
2590 upon the length of the previous instruction. The
2591 proper solution is to have a new target-specific
2592 disassembler function which just returns the length
2593 of an instruction at a given address without trying
2594 to display its disassembly. */
2595 || (distance_to_rel > 0
2596 && distance_to_rel < (bfd_signed_vma) (previous_octets/ opb)))
2598 inf->flags |= INSN_HAS_RELOC;
2602 if (! disassemble_all
2603 && (section->flags & (SEC_CODE | SEC_HAS_CONTENTS))
2604 == (SEC_CODE | SEC_HAS_CONTENTS))
2605 /* Set a stop_vma so that the disassembler will not read
2606 beyond the next symbol. We assume that symbols appear on
2607 the boundaries between instructions. We only do this when
2608 disassembling code of course, and when -D is in effect. */
2609 inf->stop_vma = section->vma + stop_offset;
2611 inf->stop_offset = stop_offset;
2613 /* Extract jump information. */
2614 inf->insn_info_valid = 0;
2615 octets = (*disassemble_fn) (section->vma + addr_offset, inf);
2616 /* Test if a jump was detected. */
2617 if (inf->insn_info_valid
2618 && ((inf->insn_type == dis_branch)
2619 || (inf->insn_type == dis_condbranch)
2620 || (inf->insn_type == dis_jsr)
2621 || (inf->insn_type == dis_condjsr))
2622 && (inf->target >= section->vma + start_offset)
2623 && (inf->target < section->vma + stop_offset))
2625 struct jump_info *ji =
2626 jump_info_new (section->vma + addr_offset, inf->target, -1);
2627 jump_info_add_front (ji, &jumps);
2630 inf->stop_vma = 0;
2632 addr_offset += octets / opb;
2635 inf->fprintf_func = (fprintf_ftype) fprintf;
2636 inf->stream = stdout;
2638 free (sfile.buffer);
2640 /* Merge jumps. */
2641 jump_info_merge (&jumps);
2642 /* Process jumps. */
2643 jump_info_sort (&jumps);
2645 /* Group jumps by level. */
2646 struct jump_info *last_jump = jumps;
2647 int max_level = -1;
2649 while (last_jump)
2651 /* The last jump is part of the next group. */
2652 struct jump_info *base = last_jump;
2653 /* Increment level. */
2654 base->level = ++max_level;
2656 /* Find jumps that can be combined on the same
2657 level, with the largest jumps tested first.
2658 This has the advantage that large jumps are on
2659 lower levels and do not intersect with small
2660 jumps that get grouped on higher levels. */
2661 struct jump_info *exchange_item = last_jump->next;
2662 struct jump_info *it = exchange_item;
2664 for (; it; it = it->next)
2666 /* Test if the jump intersects with any
2667 jump from current group. */
2668 bool ok = true;
2669 struct jump_info *it_collision;
2671 for (it_collision = base;
2672 it_collision != exchange_item;
2673 it_collision = it_collision->next)
2675 /* This jump intersects so we leave it out. */
2676 if (jump_info_intersect (it_collision, it))
2678 ok = false;
2679 break;
2683 /* Add jump to group. */
2684 if (ok)
2686 /* Move current element to the front. */
2687 if (it != exchange_item)
2689 struct jump_info *save = it->prev;
2690 jump_info_move_linked (it, exchange_item, &jumps);
2691 last_jump = it;
2692 it = save;
2694 else
2696 last_jump = exchange_item;
2697 exchange_item = exchange_item->next;
2699 last_jump->level = max_level;
2703 /* Move to next group. */
2704 last_jump = exchange_item;
2707 return jumps;
2710 /* The number of zeroes we want to see before we start skipping them.
2711 The number is arbitrarily chosen. */
2713 #define DEFAULT_SKIP_ZEROES 8
2715 /* The number of zeroes to skip at the end of a section. If the
2716 number of zeroes at the end is between SKIP_ZEROES_AT_END and
2717 SKIP_ZEROES, they will be disassembled. If there are fewer than
2718 SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic
2719 attempt to avoid disassembling zeroes inserted by section
2720 alignment. */
2722 #define DEFAULT_SKIP_ZEROES_AT_END 3
2724 static int
2725 null_print (const void * stream ATTRIBUTE_UNUSED, const char * format ATTRIBUTE_UNUSED, ...)
2727 return 1;
2730 /* Print out jump visualization. */
2732 static void
2733 print_jump_visualisation (bfd_vma addr, int max_level, char *line_buffer,
2734 uint8_t *color_buffer)
2736 if (!line_buffer)
2737 return;
2739 jump_info_visualize_address (addr, max_level, line_buffer, color_buffer);
2741 size_t line_buffer_size = strlen (line_buffer);
2742 char last_color = 0;
2743 size_t i;
2745 for (i = 0; i <= line_buffer_size; ++i)
2747 if (color_output)
2749 uint8_t color = (i < line_buffer_size) ? color_buffer[i]: 0;
2751 if (color != last_color)
2753 if (color)
2754 if (extended_color_output)
2755 /* Use extended 8bit color, but
2756 do not choose dark colors. */
2757 printf ("\033[38;5;%dm", 124 + (color % 108));
2758 else
2759 /* Use simple terminal colors. */
2760 printf ("\033[%dm", 31 + (color % 7));
2761 else
2762 /* Clear color. */
2763 printf ("\033[0m");
2764 last_color = color;
2767 putchar ((i < line_buffer_size) ? line_buffer[i]: ' ');
2771 /* Disassemble some data in memory between given values. */
2773 static void
2774 disassemble_bytes (struct disassemble_info *inf,
2775 disassembler_ftype disassemble_fn,
2776 bool insns,
2777 bfd_byte *data,
2778 bfd_vma start_offset,
2779 bfd_vma stop_offset,
2780 bfd_vma rel_offset,
2781 arelent ***relppp,
2782 arelent **relppend)
2784 struct objdump_disasm_info *aux;
2785 asection *section;
2786 unsigned int octets_per_line;
2787 unsigned int skip_addr_chars;
2788 bfd_vma addr_offset;
2789 unsigned int opb = inf->octets_per_byte;
2790 unsigned int skip_zeroes = inf->skip_zeroes;
2791 unsigned int skip_zeroes_at_end = inf->skip_zeroes_at_end;
2792 size_t octets;
2793 SFILE sfile;
2795 aux = (struct objdump_disasm_info *) inf->application_data;
2796 section = inf->section;
2798 sfile.alloc = 120;
2799 sfile.buffer = (char *) xmalloc (sfile.alloc);
2800 sfile.pos = 0;
2802 if (insn_width)
2803 octets_per_line = insn_width;
2804 else if (insns)
2805 octets_per_line = 4;
2806 else
2807 octets_per_line = 16;
2809 /* Figure out how many characters to skip at the start of an
2810 address, to make the disassembly look nicer. We discard leading
2811 zeroes in chunks of 4, ensuring that there is always a leading
2812 zero remaining. */
2813 skip_addr_chars = 0;
2814 if (!no_addresses && !prefix_addresses)
2816 char buf[30];
2818 bfd_sprintf_vma (aux->abfd, buf, section->vma + section->size / opb);
2820 while (buf[skip_addr_chars] == '0')
2821 ++skip_addr_chars;
2823 /* Don't discard zeros on overflow. */
2824 if (buf[skip_addr_chars] == '\0' && section->vma != 0)
2825 skip_addr_chars = 0;
2827 if (skip_addr_chars != 0)
2828 skip_addr_chars = (skip_addr_chars - 1) & -4;
2831 inf->insn_info_valid = 0;
2833 /* Determine maximum level. */
2834 uint8_t *color_buffer = NULL;
2835 char *line_buffer = NULL;
2836 int max_level = -1;
2838 /* Some jumps were detected. */
2839 if (detected_jumps)
2841 struct jump_info *ji;
2843 /* Find maximum jump level. */
2844 for (ji = detected_jumps; ji; ji = ji->next)
2846 if (ji->level > max_level)
2847 max_level = ji->level;
2850 /* Allocate buffers. */
2851 size_t len = (max_level + 1) * 3 + 1;
2852 line_buffer = xmalloc (len);
2853 line_buffer[len - 1] = 0;
2854 color_buffer = xmalloc (len);
2855 color_buffer[len - 1] = 0;
2858 addr_offset = start_offset;
2859 while (addr_offset < stop_offset)
2861 bool need_nl = false;
2863 octets = 0;
2865 /* Make sure we don't use relocs from previous instructions. */
2866 aux->reloc = NULL;
2868 /* If we see more than SKIP_ZEROES octets of zeroes, we just
2869 print `...'. */
2870 if (! disassemble_zeroes)
2871 for (; addr_offset * opb + octets < stop_offset * opb; octets++)
2872 if (data[addr_offset * opb + octets] != 0)
2873 break;
2874 if (! disassemble_zeroes
2875 && (inf->insn_info_valid == 0
2876 || inf->branch_delay_insns == 0)
2877 && (octets >= skip_zeroes
2878 || (addr_offset * opb + octets == stop_offset * opb
2879 && octets < skip_zeroes_at_end)))
2881 /* If there are more nonzero octets to follow, we only skip
2882 zeroes in multiples of 4, to try to avoid running over
2883 the start of an instruction which happens to start with
2884 zero. */
2885 if (addr_offset * opb + octets != stop_offset * opb)
2886 octets &= ~3;
2888 /* If we are going to display more data, and we are displaying
2889 file offsets, then tell the user how many zeroes we skip
2890 and the file offset from where we resume dumping. */
2891 if (display_file_offsets
2892 && addr_offset + octets / opb < stop_offset)
2893 printf (_("\t... (skipping %lu zeroes, "
2894 "resuming at file offset: 0x%lx)\n"),
2895 (unsigned long) (octets / opb),
2896 (unsigned long) (section->filepos
2897 + addr_offset + octets / opb));
2898 else
2899 printf ("\t...\n");
2901 else
2903 char buf[50];
2904 unsigned int bpc = 0;
2905 unsigned int pb = 0;
2907 if (with_line_numbers || with_source_code)
2908 show_line (aux->abfd, section, addr_offset);
2910 if (no_addresses)
2911 printf ("\t");
2912 else if (!prefix_addresses)
2914 char *s;
2916 bfd_sprintf_vma (aux->abfd, buf, section->vma + addr_offset);
2917 for (s = buf + skip_addr_chars; *s == '0'; s++)
2918 *s = ' ';
2919 if (*s == '\0')
2920 *--s = '0';
2921 printf ("%s:\t", buf + skip_addr_chars);
2923 else
2925 aux->require_sec = true;
2926 objdump_print_address (section->vma + addr_offset, inf);
2927 aux->require_sec = false;
2928 putchar (' ');
2931 print_jump_visualisation (section->vma + addr_offset,
2932 max_level, line_buffer,
2933 color_buffer);
2935 if (insns)
2937 int insn_size;
2939 sfile.pos = 0;
2940 inf->fprintf_func = (fprintf_ftype) objdump_sprintf;
2941 inf->stream = &sfile;
2942 inf->bytes_per_line = 0;
2943 inf->bytes_per_chunk = 0;
2944 inf->flags = ((disassemble_all ? DISASSEMBLE_DATA : 0)
2945 | (wide_output ? WIDE_OUTPUT : 0));
2946 if (machine)
2947 inf->flags |= USER_SPECIFIED_MACHINE_TYPE;
2949 if (inf->disassembler_needs_relocs
2950 && (bfd_get_file_flags (aux->abfd) & EXEC_P) == 0
2951 && (bfd_get_file_flags (aux->abfd) & DYNAMIC) == 0
2952 && *relppp < relppend)
2954 bfd_signed_vma distance_to_rel;
2955 int max_reloc_offset
2956 = aux->abfd->arch_info->max_reloc_offset_into_insn;
2958 distance_to_rel = ((**relppp)->address - rel_offset
2959 - addr_offset);
2961 insn_size = 0;
2962 if (distance_to_rel > 0
2963 && (max_reloc_offset < 0
2964 || distance_to_rel <= max_reloc_offset))
2966 /* This reloc *might* apply to the current insn,
2967 starting somewhere inside it. Discover the length
2968 of the current insn so that the check below will
2969 work. */
2970 if (insn_width)
2971 insn_size = insn_width;
2972 else
2974 /* We find the length by calling the dissassembler
2975 function with a dummy print handler. This should
2976 work unless the disassembler is not expecting to
2977 be called multiple times for the same address.
2979 This does mean disassembling the instruction
2980 twice, but we only do this when there is a high
2981 probability that there is a reloc that will
2982 affect the instruction. */
2983 inf->fprintf_func = (fprintf_ftype) null_print;
2984 insn_size = disassemble_fn (section->vma
2985 + addr_offset, inf);
2986 inf->fprintf_func = (fprintf_ftype) objdump_sprintf;
2990 /* Check to see if the current reloc is associated with
2991 the instruction that we are about to disassemble. */
2992 if (distance_to_rel == 0
2993 || (distance_to_rel > 0
2994 && distance_to_rel < insn_size / (int) opb))
2996 inf->flags |= INSN_HAS_RELOC;
2997 aux->reloc = **relppp;
3001 if (! disassemble_all
3002 && ((section->flags & (SEC_CODE | SEC_HAS_CONTENTS))
3003 == (SEC_CODE | SEC_HAS_CONTENTS)))
3004 /* Set a stop_vma so that the disassembler will not read
3005 beyond the next symbol. We assume that symbols appear on
3006 the boundaries between instructions. We only do this when
3007 disassembling code of course, and when -D is in effect. */
3008 inf->stop_vma = section->vma + stop_offset;
3010 inf->stop_offset = stop_offset;
3011 insn_size = (*disassemble_fn) (section->vma + addr_offset, inf);
3012 octets = insn_size;
3014 inf->stop_vma = 0;
3015 inf->fprintf_func = (fprintf_ftype) fprintf;
3016 inf->stream = stdout;
3017 if (insn_width == 0 && inf->bytes_per_line != 0)
3018 octets_per_line = inf->bytes_per_line;
3019 if (insn_size < (int) opb)
3021 if (sfile.pos)
3022 printf ("%s\n", sfile.buffer);
3023 if (insn_size >= 0)
3025 non_fatal (_("disassemble_fn returned length %d"),
3026 insn_size);
3027 exit_status = 1;
3029 break;
3032 else
3034 bfd_vma j;
3036 octets = octets_per_line;
3037 if (addr_offset + octets / opb > stop_offset)
3038 octets = (stop_offset - addr_offset) * opb;
3040 for (j = addr_offset * opb; j < addr_offset * opb + octets; ++j)
3042 if (ISPRINT (data[j]))
3043 buf[j - addr_offset * opb] = data[j];
3044 else
3045 buf[j - addr_offset * opb] = '.';
3047 buf[j - addr_offset * opb] = '\0';
3050 if (prefix_addresses
3051 ? show_raw_insn > 0
3052 : show_raw_insn >= 0)
3054 bfd_vma j;
3056 /* If ! prefix_addresses and ! wide_output, we print
3057 octets_per_line octets per line. */
3058 pb = octets;
3059 if (pb > octets_per_line && ! prefix_addresses && ! wide_output)
3060 pb = octets_per_line;
3062 if (inf->bytes_per_chunk)
3063 bpc = inf->bytes_per_chunk;
3064 else
3065 bpc = 1;
3067 for (j = addr_offset * opb; j < addr_offset * opb + pb; j += bpc)
3069 /* PR 21580: Check for a buffer ending early. */
3070 if (j + bpc <= stop_offset * opb)
3072 unsigned int k;
3074 if (inf->display_endian == BFD_ENDIAN_LITTLE)
3076 for (k = bpc; k-- != 0; )
3077 printf ("%02x", (unsigned) data[j + k]);
3079 else
3081 for (k = 0; k < bpc; k++)
3082 printf ("%02x", (unsigned) data[j + k]);
3085 putchar (' ');
3088 for (; pb < octets_per_line; pb += bpc)
3090 unsigned int k;
3092 for (k = 0; k < bpc; k++)
3093 printf (" ");
3094 putchar (' ');
3097 /* Separate raw data from instruction by extra space. */
3098 if (insns)
3099 putchar ('\t');
3100 else
3101 printf (" ");
3104 if (! insns)
3105 printf ("%s", buf);
3106 else if (sfile.pos)
3107 printf ("%s", sfile.buffer);
3109 if (prefix_addresses
3110 ? show_raw_insn > 0
3111 : show_raw_insn >= 0)
3113 while (pb < octets)
3115 bfd_vma j;
3116 char *s;
3118 putchar ('\n');
3119 j = addr_offset * opb + pb;
3121 if (no_addresses)
3122 printf ("\t");
3123 else
3125 bfd_sprintf_vma (aux->abfd, buf, section->vma + j / opb);
3126 for (s = buf + skip_addr_chars; *s == '0'; s++)
3127 *s = ' ';
3128 if (*s == '\0')
3129 *--s = '0';
3130 printf ("%s:\t", buf + skip_addr_chars);
3133 print_jump_visualisation (section->vma + j / opb,
3134 max_level, line_buffer,
3135 color_buffer);
3137 pb += octets_per_line;
3138 if (pb > octets)
3139 pb = octets;
3140 for (; j < addr_offset * opb + pb; j += bpc)
3142 /* PR 21619: Check for a buffer ending early. */
3143 if (j + bpc <= stop_offset * opb)
3145 unsigned int k;
3147 if (inf->display_endian == BFD_ENDIAN_LITTLE)
3149 for (k = bpc; k-- != 0; )
3150 printf ("%02x", (unsigned) data[j + k]);
3152 else
3154 for (k = 0; k < bpc; k++)
3155 printf ("%02x", (unsigned) data[j + k]);
3158 putchar (' ');
3163 if (!wide_output)
3164 putchar ('\n');
3165 else
3166 need_nl = true;
3169 while ((*relppp) < relppend
3170 && (**relppp)->address < rel_offset + addr_offset + octets / opb)
3172 if (dump_reloc_info || dump_dynamic_reloc_info)
3174 arelent *q;
3176 q = **relppp;
3178 if (wide_output)
3179 putchar ('\t');
3180 else
3181 printf ("\t\t\t");
3183 if (!no_addresses)
3185 objdump_print_value (section->vma - rel_offset + q->address,
3186 inf, true);
3187 printf (": ");
3190 if (q->howto == NULL)
3191 printf ("*unknown*\t");
3192 else if (q->howto->name)
3193 printf ("%s\t", q->howto->name);
3194 else
3195 printf ("%d\t", q->howto->type);
3197 if (q->sym_ptr_ptr == NULL || *q->sym_ptr_ptr == NULL)
3198 printf ("*unknown*");
3199 else
3201 const char *sym_name;
3203 sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
3204 if (sym_name != NULL && *sym_name != '\0')
3205 objdump_print_symname (aux->abfd, inf, *q->sym_ptr_ptr);
3206 else
3208 asection *sym_sec;
3210 sym_sec = bfd_asymbol_section (*q->sym_ptr_ptr);
3211 sym_name = bfd_section_name (sym_sec);
3212 if (sym_name == NULL || *sym_name == '\0')
3213 sym_name = "*unknown*";
3214 printf ("%s", sanitize_string (sym_name));
3218 if (q->addend)
3220 bfd_vma addend = q->addend;
3221 if ((bfd_signed_vma) addend < 0)
3223 printf ("-0x");
3224 addend = -addend;
3226 else
3227 printf ("+0x");
3228 objdump_print_value (addend, inf, true);
3231 printf ("\n");
3232 need_nl = false;
3234 ++(*relppp);
3237 if (need_nl)
3238 printf ("\n");
3240 addr_offset += octets / opb;
3243 free (sfile.buffer);
3244 free (line_buffer);
3245 free (color_buffer);
3248 static void
3249 disassemble_section (bfd *abfd, asection *section, void *inf)
3251 const struct elf_backend_data *bed;
3252 bfd_vma sign_adjust = 0;
3253 struct disassemble_info *pinfo = (struct disassemble_info *) inf;
3254 struct objdump_disasm_info *paux;
3255 unsigned int opb = pinfo->octets_per_byte;
3256 bfd_byte *data = NULL;
3257 bfd_size_type datasize = 0;
3258 arelent **rel_pp = NULL;
3259 arelent **rel_ppstart = NULL;
3260 arelent **rel_ppend;
3261 bfd_vma stop_offset;
3262 asymbol *sym = NULL;
3263 long place = 0;
3264 long rel_count;
3265 bfd_vma rel_offset;
3266 unsigned long addr_offset;
3267 bool do_print;
3268 enum loop_control
3270 stop_offset_reached,
3271 function_sym,
3272 next_sym
3273 } loop_until;
3275 /* Sections that do not contain machine
3276 code are not normally disassembled. */
3277 if (! disassemble_all
3278 && only_list == NULL
3279 && ((section->flags & (SEC_CODE | SEC_HAS_CONTENTS))
3280 != (SEC_CODE | SEC_HAS_CONTENTS)))
3281 return;
3283 if (! process_section_p (section))
3284 return;
3286 datasize = bfd_section_size (section);
3287 if (datasize == 0)
3288 return;
3290 if (start_address == (bfd_vma) -1
3291 || start_address < section->vma)
3292 addr_offset = 0;
3293 else
3294 addr_offset = start_address - section->vma;
3296 if (stop_address == (bfd_vma) -1)
3297 stop_offset = datasize / opb;
3298 else
3300 if (stop_address < section->vma)
3301 stop_offset = 0;
3302 else
3303 stop_offset = stop_address - section->vma;
3304 if (stop_offset > datasize / opb)
3305 stop_offset = datasize / opb;
3308 if (addr_offset >= stop_offset)
3309 return;
3311 /* Decide which set of relocs to use. Load them if necessary. */
3312 paux = (struct objdump_disasm_info *) pinfo->application_data;
3313 if (pinfo->dynrelbuf && dump_dynamic_reloc_info)
3315 rel_pp = pinfo->dynrelbuf;
3316 rel_count = pinfo->dynrelcount;
3317 /* Dynamic reloc addresses are absolute, non-dynamic are section
3318 relative. REL_OFFSET specifies the reloc address corresponding
3319 to the start of this section. */
3320 rel_offset = section->vma;
3322 else
3324 rel_count = 0;
3325 rel_pp = NULL;
3326 rel_offset = 0;
3328 if ((section->flags & SEC_RELOC) != 0
3329 && (dump_reloc_info || pinfo->disassembler_needs_relocs))
3331 long relsize;
3333 relsize = bfd_get_reloc_upper_bound (abfd, section);
3334 if (relsize < 0)
3335 bfd_fatal (bfd_get_filename (abfd));
3337 if (relsize > 0)
3339 rel_ppstart = rel_pp = (arelent **) xmalloc (relsize);
3340 rel_count = bfd_canonicalize_reloc (abfd, section, rel_pp, syms);
3341 if (rel_count < 0)
3342 bfd_fatal (bfd_get_filename (abfd));
3344 /* Sort the relocs by address. */
3345 qsort (rel_pp, rel_count, sizeof (arelent *), compare_relocs);
3349 rel_ppend = PTR_ADD (rel_pp, rel_count);
3351 if (!bfd_malloc_and_get_section (abfd, section, &data))
3353 non_fatal (_("Reading section %s failed because: %s"),
3354 section->name, bfd_errmsg (bfd_get_error ()));
3355 return;
3358 pinfo->buffer = data;
3359 pinfo->buffer_vma = section->vma;
3360 pinfo->buffer_length = datasize;
3361 pinfo->section = section;
3363 /* Sort the symbols into value and section order. */
3364 compare_section = section;
3365 if (sorted_symcount > 1)
3366 qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols);
3368 /* Skip over the relocs belonging to addresses below the
3369 start address. */
3370 while (rel_pp < rel_ppend
3371 && (*rel_pp)->address < rel_offset + addr_offset)
3372 ++rel_pp;
3374 printf (_("\nDisassembly of section %s:\n"), sanitize_string (section->name));
3376 /* Find the nearest symbol forwards from our current position. */
3377 paux->require_sec = true;
3378 sym = (asymbol *) find_symbol_for_address (section->vma + addr_offset,
3379 (struct disassemble_info *) inf,
3380 &place);
3381 paux->require_sec = false;
3383 /* PR 9774: If the target used signed addresses then we must make
3384 sure that we sign extend the value that we calculate for 'addr'
3385 in the loop below. */
3386 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
3387 && (bed = get_elf_backend_data (abfd)) != NULL
3388 && bed->sign_extend_vma)
3389 sign_adjust = (bfd_vma) 1 << (bed->s->arch_size - 1);
3391 /* Disassemble a block of instructions up to the address associated with
3392 the symbol we have just found. Then print the symbol and find the
3393 next symbol on. Repeat until we have disassembled the entire section
3394 or we have reached the end of the address range we are interested in. */
3395 do_print = paux->symbol == NULL;
3396 loop_until = stop_offset_reached;
3398 while (addr_offset < stop_offset)
3400 bfd_vma addr;
3401 asymbol *nextsym;
3402 bfd_vma nextstop_offset;
3403 bool insns;
3405 addr = section->vma + addr_offset;
3406 addr = ((addr & ((sign_adjust << 1) - 1)) ^ sign_adjust) - sign_adjust;
3408 if (sym != NULL && bfd_asymbol_value (sym) <= addr)
3410 int x;
3412 for (x = place;
3413 (x < sorted_symcount
3414 && (bfd_asymbol_value (sorted_syms[x]) <= addr));
3415 ++x)
3416 continue;
3418 pinfo->symbols = sorted_syms + place;
3419 pinfo->num_symbols = x - place;
3420 pinfo->symtab_pos = place;
3422 else
3424 pinfo->symbols = NULL;
3425 pinfo->num_symbols = 0;
3426 pinfo->symtab_pos = -1;
3429 /* If we are only disassembling from a specific symbol,
3430 check to see if we should start or stop displaying. */
3431 if (sym && paux->symbol)
3433 if (do_print)
3435 /* See if we should stop printing. */
3436 switch (loop_until)
3438 case function_sym:
3439 if (sym->flags & BSF_FUNCTION)
3440 do_print = false;
3441 break;
3443 case stop_offset_reached:
3444 /* Handled by the while loop. */
3445 break;
3447 case next_sym:
3448 /* FIXME: There is an implicit assumption here
3449 that the name of sym is different from
3450 paux->symbol. */
3451 if (! bfd_is_local_label (abfd, sym))
3452 do_print = false;
3453 break;
3456 else
3458 const char * name = bfd_asymbol_name (sym);
3459 char * alloc = NULL;
3461 if (do_demangle && name[0] != '\0')
3463 /* Demangle the name. */
3464 alloc = bfd_demangle (abfd, name, demangle_flags);
3465 if (alloc != NULL)
3466 name = alloc;
3469 /* We are not currently printing. Check to see
3470 if the current symbol matches the requested symbol. */
3471 if (streq (name, paux->symbol))
3473 do_print = true;
3475 if (sym->flags & BSF_FUNCTION)
3477 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
3478 && ((elf_symbol_type *) sym)->internal_elf_sym.st_size > 0)
3480 /* Sym is a function symbol with a size associated
3481 with it. Turn on automatic disassembly for the
3482 next VALUE bytes. */
3483 stop_offset = addr_offset
3484 + ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
3485 loop_until = stop_offset_reached;
3487 else
3489 /* Otherwise we need to tell the loop heuristic to
3490 loop until the next function symbol is encountered. */
3491 loop_until = function_sym;
3494 else
3496 /* Otherwise loop until the next symbol is encountered. */
3497 loop_until = next_sym;
3501 free (alloc);
3505 if (! prefix_addresses && do_print)
3507 pinfo->fprintf_func (pinfo->stream, "\n");
3508 objdump_print_addr_with_sym (abfd, section, sym, addr,
3509 pinfo, false);
3510 pinfo->fprintf_func (pinfo->stream, ":\n");
3513 if (sym != NULL && bfd_asymbol_value (sym) > addr)
3514 nextsym = sym;
3515 else if (sym == NULL)
3516 nextsym = NULL;
3517 else
3519 #define is_valid_next_sym(SYM) \
3520 (strcmp (bfd_section_name ((SYM)->section), bfd_section_name (section)) == 0 \
3521 && (bfd_asymbol_value (SYM) > bfd_asymbol_value (sym)) \
3522 && pinfo->symbol_is_valid (SYM, pinfo))
3524 /* Search forward for the next appropriate symbol in
3525 SECTION. Note that all the symbols are sorted
3526 together into one big array, and that some sections
3527 may have overlapping addresses. */
3528 while (place < sorted_symcount
3529 && ! is_valid_next_sym (sorted_syms [place]))
3530 ++place;
3532 if (place >= sorted_symcount)
3533 nextsym = NULL;
3534 else
3535 nextsym = sorted_syms[place];
3538 if (sym != NULL && bfd_asymbol_value (sym) > addr)
3539 nextstop_offset = bfd_asymbol_value (sym) - section->vma;
3540 else if (nextsym == NULL)
3541 nextstop_offset = stop_offset;
3542 else
3543 nextstop_offset = bfd_asymbol_value (nextsym) - section->vma;
3545 if (nextstop_offset > stop_offset
3546 || nextstop_offset <= addr_offset)
3547 nextstop_offset = stop_offset;
3549 /* If a symbol is explicitly marked as being an object
3550 rather than a function, just dump the bytes without
3551 disassembling them. */
3552 if (disassemble_all
3553 || sym == NULL
3554 || sym->section != section
3555 || bfd_asymbol_value (sym) > addr
3556 || ((sym->flags & BSF_OBJECT) == 0
3557 && (strstr (bfd_asymbol_name (sym), "gnu_compiled")
3558 == NULL)
3559 && (strstr (bfd_asymbol_name (sym), "gcc2_compiled")
3560 == NULL))
3561 || (sym->flags & BSF_FUNCTION) != 0)
3562 insns = true;
3563 else
3564 insns = false;
3566 if (do_print)
3568 /* Resolve symbol name. */
3569 if (visualize_jumps && abfd && sym && sym->name)
3571 struct disassemble_info di;
3572 SFILE sf;
3574 sf.alloc = strlen (sym->name) + 40;
3575 sf.buffer = (char*) xmalloc (sf.alloc);
3576 sf.pos = 0;
3577 di.fprintf_func = (fprintf_ftype) objdump_sprintf;
3578 di.stream = &sf;
3580 objdump_print_symname (abfd, &di, sym);
3582 /* Fetch jump information. */
3583 detected_jumps = disassemble_jumps
3584 (pinfo, paux->disassemble_fn,
3585 addr_offset, nextstop_offset,
3586 rel_offset, &rel_pp, rel_ppend);
3588 /* Free symbol name. */
3589 free (sf.buffer);
3592 /* Add jumps to output. */
3593 disassemble_bytes (pinfo, paux->disassemble_fn, insns, data,
3594 addr_offset, nextstop_offset,
3595 rel_offset, &rel_pp, rel_ppend);
3597 /* Free jumps. */
3598 while (detected_jumps)
3600 detected_jumps = jump_info_free (detected_jumps);
3604 addr_offset = nextstop_offset;
3605 sym = nextsym;
3608 free (data);
3610 if (rel_ppstart != NULL)
3611 free (rel_ppstart);
3614 /* Disassemble the contents of an object file. */
3616 static void
3617 disassemble_data (bfd *abfd)
3619 struct disassemble_info disasm_info;
3620 struct objdump_disasm_info aux;
3621 long i;
3623 print_files = NULL;
3624 prev_functionname = NULL;
3625 prev_line = -1;
3626 prev_discriminator = 0;
3628 /* We make a copy of syms to sort. We don't want to sort syms
3629 because that will screw up the relocs. */
3630 sorted_symcount = symcount ? symcount : dynsymcount;
3631 sorted_syms = (asymbol **) xmalloc ((sorted_symcount + synthcount)
3632 * sizeof (asymbol *));
3633 if (sorted_symcount != 0)
3635 memcpy (sorted_syms, symcount ? syms : dynsyms,
3636 sorted_symcount * sizeof (asymbol *));
3638 sorted_symcount = remove_useless_symbols (sorted_syms, sorted_symcount);
3641 for (i = 0; i < synthcount; ++i)
3643 sorted_syms[sorted_symcount] = synthsyms + i;
3644 ++sorted_symcount;
3647 init_disassemble_info (&disasm_info, stdout, (fprintf_ftype) fprintf);
3649 disasm_info.application_data = (void *) &aux;
3650 aux.abfd = abfd;
3651 aux.require_sec = false;
3652 disasm_info.dynrelbuf = NULL;
3653 disasm_info.dynrelcount = 0;
3654 aux.reloc = NULL;
3655 aux.symbol = disasm_sym;
3657 disasm_info.print_address_func = objdump_print_address;
3658 disasm_info.symbol_at_address_func = objdump_symbol_at_address;
3660 if (machine != NULL)
3662 const bfd_arch_info_type *inf = bfd_scan_arch (machine);
3664 if (inf == NULL)
3665 fatal (_("can't use supplied machine %s"), machine);
3667 abfd->arch_info = inf;
3670 if (endian != BFD_ENDIAN_UNKNOWN)
3672 struct bfd_target *xvec;
3674 xvec = (struct bfd_target *) xmalloc (sizeof (struct bfd_target));
3675 memcpy (xvec, abfd->xvec, sizeof (struct bfd_target));
3676 xvec->byteorder = endian;
3677 abfd->xvec = xvec;
3680 /* Use libopcodes to locate a suitable disassembler. */
3681 aux.disassemble_fn = disassembler (bfd_get_arch (abfd),
3682 bfd_big_endian (abfd),
3683 bfd_get_mach (abfd), abfd);
3684 if (!aux.disassemble_fn)
3686 non_fatal (_("can't disassemble for architecture %s\n"),
3687 bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
3688 exit_status = 1;
3689 return;
3692 disasm_info.flavour = bfd_get_flavour (abfd);
3693 disasm_info.arch = bfd_get_arch (abfd);
3694 disasm_info.mach = bfd_get_mach (abfd);
3695 disasm_info.disassembler_options = disassembler_options;
3696 disasm_info.octets_per_byte = bfd_octets_per_byte (abfd, NULL);
3697 disasm_info.skip_zeroes = DEFAULT_SKIP_ZEROES;
3698 disasm_info.skip_zeroes_at_end = DEFAULT_SKIP_ZEROES_AT_END;
3699 disasm_info.disassembler_needs_relocs = false;
3701 if (bfd_big_endian (abfd))
3702 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_BIG;
3703 else if (bfd_little_endian (abfd))
3704 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_LITTLE;
3705 else
3706 /* ??? Aborting here seems too drastic. We could default to big or little
3707 instead. */
3708 disasm_info.endian = BFD_ENDIAN_UNKNOWN;
3710 disasm_info.endian_code = disasm_info.endian;
3712 /* Allow the target to customize the info structure. */
3713 disassemble_init_for_target (& disasm_info);
3715 /* Pre-load the dynamic relocs as we may need them during the disassembly. */
3716 long relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
3718 if (relsize < 0 && dump_dynamic_reloc_info)
3719 bfd_fatal (bfd_get_filename (abfd));
3721 if (relsize > 0)
3723 disasm_info.dynrelbuf = (arelent **) xmalloc (relsize);
3724 disasm_info.dynrelcount
3725 = bfd_canonicalize_dynamic_reloc (abfd, disasm_info.dynrelbuf, dynsyms);
3726 if (disasm_info.dynrelcount < 0)
3727 bfd_fatal (bfd_get_filename (abfd));
3729 /* Sort the relocs by address. */
3730 qsort (disasm_info.dynrelbuf, disasm_info.dynrelcount, sizeof (arelent *),
3731 compare_relocs);
3734 disasm_info.symtab = sorted_syms;
3735 disasm_info.symtab_size = sorted_symcount;
3737 bfd_map_over_sections (abfd, disassemble_section, & disasm_info);
3739 free (disasm_info.dynrelbuf);
3740 disasm_info.dynrelbuf = NULL;
3741 free (sorted_syms);
3742 disassemble_free_target (&disasm_info);
3745 static bool
3746 load_specific_debug_section (enum dwarf_section_display_enum debug,
3747 asection *sec, void *file)
3749 struct dwarf_section *section = &debug_displays [debug].section;
3750 bfd *abfd = (bfd *) file;
3751 bfd_byte *contents;
3752 bfd_size_type amt;
3753 size_t alloced;
3754 bool ret;
3756 if (section->start != NULL)
3758 /* If it is already loaded, do nothing. */
3759 if (streq (section->filename, bfd_get_filename (abfd)))
3760 return true;
3761 free (section->start);
3764 section->filename = bfd_get_filename (abfd);
3765 section->reloc_info = NULL;
3766 section->num_relocs = 0;
3767 section->address = bfd_section_vma (sec);
3768 section->size = bfd_section_size (sec);
3769 /* PR 24360: On 32-bit hosts sizeof (size_t) < sizeof (bfd_size_type). */
3770 alloced = amt = section->size + 1;
3771 if (alloced != amt || alloced == 0)
3773 section->start = NULL;
3774 free_debug_section (debug);
3775 printf (_("\nSection '%s' has an invalid size: %#llx.\n"),
3776 sanitize_string (section->name),
3777 (unsigned long long) section->size);
3778 return false;
3781 section->start = contents = xmalloc (alloced);
3782 /* Ensure any string section has a terminating NUL. */
3783 section->start[section->size] = 0;
3785 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
3786 && debug_displays [debug].relocate)
3788 ret = bfd_simple_get_relocated_section_contents (abfd,
3789 sec,
3790 section->start,
3791 syms) != NULL;
3792 if (ret)
3794 long reloc_size = bfd_get_reloc_upper_bound (abfd, sec);
3796 if (reloc_size > 0)
3798 unsigned long reloc_count;
3799 arelent **relocs;
3801 relocs = (arelent **) xmalloc (reloc_size);
3803 reloc_count = bfd_canonicalize_reloc (abfd, sec, relocs, NULL);
3804 if (reloc_count == 0)
3805 free (relocs);
3806 else
3808 section->reloc_info = relocs;
3809 section->num_relocs = reloc_count;
3814 else
3815 ret = bfd_get_full_section_contents (abfd, sec, &contents);
3817 if (!ret)
3819 free_debug_section (debug);
3820 printf (_("\nCan't get contents for section '%s'.\n"),
3821 sanitize_string (section->name));
3822 return false;
3825 return true;
3828 bool
3829 reloc_at (struct dwarf_section * dsec, dwarf_vma offset)
3831 arelent ** relocs;
3832 arelent * rp;
3834 if (dsec == NULL || dsec->reloc_info == NULL)
3835 return false;
3837 relocs = (arelent **) dsec->reloc_info;
3839 for (; (rp = * relocs) != NULL; ++ relocs)
3840 if (rp->address == offset)
3841 return true;
3843 return false;
3846 bool
3847 load_debug_section (enum dwarf_section_display_enum debug, void *file)
3849 struct dwarf_section *section = &debug_displays [debug].section;
3850 bfd *abfd = (bfd *) file;
3851 asection *sec;
3852 const char *name;
3854 /* If it is already loaded, do nothing. */
3855 if (section->start != NULL)
3857 if (streq (section->filename, bfd_get_filename (abfd)))
3858 return true;
3860 /* Locate the debug section. */
3861 name = section->uncompressed_name;
3862 sec = bfd_get_section_by_name (abfd, name);
3863 if (sec == NULL)
3865 name = section->compressed_name;
3866 if (*name)
3867 sec = bfd_get_section_by_name (abfd, name);
3869 if (sec == NULL)
3871 name = section->xcoff_name;
3872 if (*name)
3873 sec = bfd_get_section_by_name (abfd, name);
3875 if (sec == NULL)
3876 return false;
3878 section->name = name;
3879 return load_specific_debug_section (debug, sec, file);
3882 void
3883 free_debug_section (enum dwarf_section_display_enum debug)
3885 struct dwarf_section *section = &debug_displays [debug].section;
3887 free ((char *) section->start);
3888 section->start = NULL;
3889 section->address = 0;
3890 section->size = 0;
3893 void
3894 close_debug_file (void * file)
3896 bfd * abfd = (bfd *) file;
3898 bfd_close (abfd);
3901 void *
3902 open_debug_file (const char * pathname)
3904 bfd * data;
3906 data = bfd_openr (pathname, NULL);
3907 if (data == NULL)
3908 return NULL;
3910 if (! bfd_check_format (data, bfd_object))
3911 return NULL;
3913 return data;
3916 #if HAVE_LIBDEBUGINFOD
3917 /* Return a hex string represention of the build-id. */
3919 unsigned char *
3920 get_build_id (void * data)
3922 unsigned i;
3923 char * build_id_str;
3924 bfd * abfd = (bfd *) data;
3925 const struct bfd_build_id * build_id;
3927 build_id = abfd->build_id;
3928 if (build_id == NULL)
3929 return NULL;
3931 build_id_str = malloc (build_id->size * 2 + 1);
3932 if (build_id_str == NULL)
3933 return NULL;
3935 for (i = 0; i < build_id->size; i++)
3936 sprintf (build_id_str + (i * 2), "%02x", build_id->data[i]);
3937 build_id_str[build_id->size * 2] = '\0';
3939 return (unsigned char *)build_id_str;
3941 #endif /* HAVE_LIBDEBUGINFOD */
3943 static void
3944 dump_dwarf_section (bfd *abfd, asection *section,
3945 void *arg)
3947 const char *name = bfd_section_name (section);
3948 const char *match;
3949 int i;
3950 bool is_mainfile = *(bool *) arg;
3952 if (*name == 0)
3953 return;
3955 if (!is_mainfile && !process_links
3956 && (section->flags & SEC_DEBUGGING) == 0)
3957 return;
3959 if (startswith (name, ".gnu.linkonce.wi."))
3960 match = ".debug_info";
3961 else
3962 match = name;
3964 for (i = 0; i < max; i++)
3965 if ((strcmp (debug_displays [i].section.uncompressed_name, match) == 0
3966 || strcmp (debug_displays [i].section.compressed_name, match) == 0
3967 || strcmp (debug_displays [i].section.xcoff_name, match) == 0)
3968 && debug_displays [i].enabled != NULL
3969 && *debug_displays [i].enabled)
3971 struct dwarf_section *sec = &debug_displays [i].section;
3973 if (strcmp (sec->uncompressed_name, match) == 0)
3974 sec->name = sec->uncompressed_name;
3975 else if (strcmp (sec->compressed_name, match) == 0)
3976 sec->name = sec->compressed_name;
3977 else
3978 sec->name = sec->xcoff_name;
3979 if (load_specific_debug_section ((enum dwarf_section_display_enum) i,
3980 section, abfd))
3982 debug_displays [i].display (sec, abfd);
3984 if (i != info && i != abbrev)
3985 free_debug_section ((enum dwarf_section_display_enum) i);
3987 break;
3991 /* Dump the dwarf debugging information. */
3993 static void
3994 dump_dwarf (bfd *abfd, bool is_mainfile)
3996 /* The byte_get pointer should have been set at the start of dump_bfd(). */
3997 if (byte_get == NULL)
3999 warn (_("File %s does not contain any dwarf debug information\n"),
4000 bfd_get_filename (abfd));
4001 return;
4004 switch (bfd_get_arch (abfd))
4006 case bfd_arch_s12z:
4007 /* S12Z has a 24 bit address space. But the only known
4008 producer of dwarf_info encodes addresses into 32 bits. */
4009 eh_addr_size = 4;
4010 break;
4012 default:
4013 eh_addr_size = bfd_arch_bits_per_address (abfd) / 8;
4014 break;
4017 init_dwarf_regnames_by_bfd_arch_and_mach (bfd_get_arch (abfd),
4018 bfd_get_mach (abfd));
4020 bfd_map_over_sections (abfd, dump_dwarf_section, (void *) &is_mainfile);
4023 /* Read ABFD's stabs section STABSECT_NAME, and return a pointer to
4024 it. Return NULL on failure. */
4026 static bfd_byte *
4027 read_section_stabs (bfd *abfd, const char *sect_name, bfd_size_type *size_ptr,
4028 bfd_size_type *entsize_ptr)
4030 asection *stabsect;
4031 bfd_byte *contents;
4033 stabsect = bfd_get_section_by_name (abfd, sect_name);
4034 if (stabsect == NULL)
4036 printf (_("No %s section present\n\n"),
4037 sanitize_string (sect_name));
4038 return false;
4041 if (!bfd_malloc_and_get_section (abfd, stabsect, &contents))
4043 non_fatal (_("reading %s section of %s failed: %s"),
4044 sect_name, bfd_get_filename (abfd),
4045 bfd_errmsg (bfd_get_error ()));
4046 exit_status = 1;
4047 free (contents);
4048 return NULL;
4051 *size_ptr = bfd_section_size (stabsect);
4052 if (entsize_ptr)
4053 *entsize_ptr = stabsect->entsize;
4055 return contents;
4058 /* Stabs entries use a 12 byte format:
4059 4 byte string table index
4060 1 byte stab type
4061 1 byte stab other field
4062 2 byte stab desc field
4063 4 byte stab value
4064 FIXME: This will have to change for a 64 bit object format. */
4066 #define STRDXOFF (0)
4067 #define TYPEOFF (4)
4068 #define OTHEROFF (5)
4069 #define DESCOFF (6)
4070 #define VALOFF (8)
4071 #define STABSIZE (12)
4073 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
4074 using string table section STRSECT_NAME (in `strtab'). */
4076 static void
4077 print_section_stabs (bfd *abfd,
4078 const char *stabsect_name,
4079 unsigned *string_offset_ptr)
4081 int i;
4082 unsigned file_string_table_offset = 0;
4083 unsigned next_file_string_table_offset = *string_offset_ptr;
4084 bfd_byte *stabp, *stabs_end;
4086 stabp = stabs;
4087 stabs_end = stabp + stab_size;
4089 printf (_("Contents of %s section:\n\n"), sanitize_string (stabsect_name));
4090 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
4092 /* Loop through all symbols and print them.
4094 We start the index at -1 because there is a dummy symbol on
4095 the front of stabs-in-{coff,elf} sections that supplies sizes. */
4096 for (i = -1; stabp <= stabs_end - STABSIZE; stabp += STABSIZE, i++)
4098 const char *name;
4099 unsigned long strx;
4100 unsigned char type, other;
4101 unsigned short desc;
4102 bfd_vma value;
4104 strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
4105 type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
4106 other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
4107 desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
4108 value = bfd_h_get_32 (abfd, stabp + VALOFF);
4110 printf ("\n%-6d ", i);
4111 /* Either print the stab name, or, if unnamed, print its number
4112 again (makes consistent formatting for tools like awk). */
4113 name = bfd_get_stab_name (type);
4114 if (name != NULL)
4115 printf ("%-6s", sanitize_string (name));
4116 else if (type == N_UNDF)
4117 printf ("HdrSym");
4118 else
4119 printf ("%-6d", type);
4120 printf (" %-6d %-6d ", other, desc);
4121 bfd_printf_vma (abfd, value);
4122 printf (" %-6lu", strx);
4124 /* Symbols with type == 0 (N_UNDF) specify the length of the
4125 string table associated with this file. We use that info
4126 to know how to relocate the *next* file's string table indices. */
4127 if (type == N_UNDF)
4129 file_string_table_offset = next_file_string_table_offset;
4130 next_file_string_table_offset += value;
4132 else
4134 bfd_size_type amt = strx + file_string_table_offset;
4136 /* Using the (possibly updated) string table offset, print the
4137 string (if any) associated with this symbol. */
4138 if (amt < stabstr_size)
4139 /* PR 17512: file: 079-79389-0.001:0.1.
4140 FIXME: May need to sanitize this string before displaying. */
4141 printf (" %.*s", (int)(stabstr_size - amt), strtab + amt);
4142 else
4143 printf (" *");
4146 printf ("\n\n");
4147 *string_offset_ptr = next_file_string_table_offset;
4150 typedef struct
4152 const char * section_name;
4153 const char * string_section_name;
4154 unsigned string_offset;
4156 stab_section_names;
4158 static void
4159 find_stabs_section (bfd *abfd, asection *section, void *names)
4161 int len;
4162 stab_section_names * sought = (stab_section_names *) names;
4164 /* Check for section names for which stabsect_name is a prefix, to
4165 handle .stab.N, etc. */
4166 len = strlen (sought->section_name);
4168 /* If the prefix matches, and the files section name ends with a
4169 nul or a digit, then we match. I.e., we want either an exact
4170 match or a section followed by a number. */
4171 if (strncmp (sought->section_name, section->name, len) == 0
4172 && (section->name[len] == 0
4173 || (section->name[len] == '.' && ISDIGIT (section->name[len + 1]))))
4175 if (strtab == NULL)
4176 strtab = read_section_stabs (abfd, sought->string_section_name,
4177 &stabstr_size, NULL);
4179 if (strtab)
4181 stabs = read_section_stabs (abfd, section->name, &stab_size, NULL);
4182 if (stabs)
4183 print_section_stabs (abfd, section->name, &sought->string_offset);
4188 static void
4189 dump_stabs_section (bfd *abfd, char *stabsect_name, char *strsect_name)
4191 stab_section_names s;
4193 s.section_name = stabsect_name;
4194 s.string_section_name = strsect_name;
4195 s.string_offset = 0;
4197 bfd_map_over_sections (abfd, find_stabs_section, & s);
4199 free (strtab);
4200 strtab = NULL;
4203 /* Dump the any sections containing stabs debugging information. */
4205 static void
4206 dump_stabs (bfd *abfd)
4208 dump_stabs_section (abfd, ".stab", ".stabstr");
4209 dump_stabs_section (abfd, ".stab.excl", ".stab.exclstr");
4210 dump_stabs_section (abfd, ".stab.index", ".stab.indexstr");
4212 /* For Darwin. */
4213 dump_stabs_section (abfd, "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr");
4215 dump_stabs_section (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
4218 static void
4219 dump_bfd_header (bfd *abfd)
4221 char *comma = "";
4223 printf (_("architecture: %s, "),
4224 bfd_printable_arch_mach (bfd_get_arch (abfd),
4225 bfd_get_mach (abfd)));
4226 printf (_("flags 0x%08x:\n"), abfd->flags & ~BFD_FLAGS_FOR_BFD_USE_MASK);
4228 #define PF(x, y) if (abfd->flags & x) {printf ("%s%s", comma, y); comma=", ";}
4229 PF (HAS_RELOC, "HAS_RELOC");
4230 PF (EXEC_P, "EXEC_P");
4231 PF (HAS_LINENO, "HAS_LINENO");
4232 PF (HAS_DEBUG, "HAS_DEBUG");
4233 PF (HAS_SYMS, "HAS_SYMS");
4234 PF (HAS_LOCALS, "HAS_LOCALS");
4235 PF (DYNAMIC, "DYNAMIC");
4236 PF (WP_TEXT, "WP_TEXT");
4237 PF (D_PAGED, "D_PAGED");
4238 PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
4239 printf (_("\nstart address 0x"));
4240 bfd_printf_vma (abfd, abfd->start_address);
4241 printf ("\n");
4245 #ifdef ENABLE_LIBCTF
4246 /* Formatting callback function passed to ctf_dump. Returns either the pointer
4247 it is passed, or a pointer to newly-allocated storage, in which case
4248 dump_ctf() will free it when it no longer needs it. */
4250 static char *
4251 dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
4252 char *s, void *arg)
4254 const char *blanks = arg;
4255 char *new_s;
4257 if (asprintf (&new_s, "%s%s", blanks, s) < 0)
4258 return s;
4259 return new_s;
4262 /* Make a ctfsect suitable for ctf_bfdopen_ctfsect(). */
4263 static ctf_sect_t
4264 make_ctfsect (const char *name, bfd_byte *data,
4265 bfd_size_type size)
4267 ctf_sect_t ctfsect;
4269 ctfsect.cts_name = name;
4270 ctfsect.cts_entsize = 1;
4271 ctfsect.cts_size = size;
4272 ctfsect.cts_data = data;
4274 return ctfsect;
4277 /* Dump CTF errors/warnings. */
4278 static void
4279 dump_ctf_errs (ctf_dict_t *fp)
4281 ctf_next_t *it = NULL;
4282 char *errtext;
4283 int is_warning;
4284 int err;
4286 /* Dump accumulated errors and warnings. */
4287 while ((errtext = ctf_errwarning_next (fp, &it, &is_warning, &err)) != NULL)
4289 non_fatal (_("%s: %s"), is_warning ? _("warning"): _("error"),
4290 errtext);
4291 free (errtext);
4293 if (err != ECTF_NEXT_END)
4295 non_fatal (_("CTF error: cannot get CTF errors: `%s'"),
4296 ctf_errmsg (err));
4300 /* Dump one CTF archive member. */
4302 static void
4303 dump_ctf_archive_member (ctf_dict_t *ctf, const char *name, ctf_dict_t *parent,
4304 size_t member)
4306 const char *things[] = {"Header", "Labels", "Data objects",
4307 "Function objects", "Variables", "Types", "Strings",
4308 ""};
4309 const char **thing;
4310 size_t i;
4312 /* Don't print out the name of the default-named archive member if it appears
4313 first in the list. The name .ctf appears everywhere, even for things that
4314 aren't really archives, so printing it out is liable to be confusing; also,
4315 the common case by far is for only one archive member to exist, and hiding
4316 it in that case seems worthwhile. */
4318 if (strcmp (name, ".ctf") != 0 || member != 0)
4319 printf (_("\nCTF archive member: %s:\n"), sanitize_string (name));
4321 if (ctf_parent_name (ctf) != NULL)
4322 ctf_import (ctf, parent);
4324 for (i = 0, thing = things; *thing[0]; thing++, i++)
4326 ctf_dump_state_t *s = NULL;
4327 char *item;
4329 printf ("\n %s:\n", *thing);
4330 while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
4331 (void *) " ")) != NULL)
4333 printf ("%s\n", item);
4334 free (item);
4337 if (ctf_errno (ctf))
4339 non_fatal (_("Iteration failed: %s, %s"), *thing,
4340 ctf_errmsg (ctf_errno (ctf)));
4341 break;
4345 dump_ctf_errs (ctf);
4348 /* Dump the CTF debugging information. */
4350 static void
4351 dump_ctf (bfd *abfd, const char *sect_name, const char *parent_name)
4353 ctf_archive_t *ctfa = NULL;
4354 bfd_byte *ctfdata = NULL;
4355 bfd_size_type ctfsize;
4356 ctf_sect_t ctfsect;
4357 ctf_dict_t *parent;
4358 ctf_dict_t *fp;
4359 ctf_next_t *i = NULL;
4360 const char *name;
4361 size_t member = 0;
4362 int err;
4364 if (sect_name == NULL)
4365 sect_name = ".ctf";
4367 if ((ctfdata = read_section_stabs (abfd, sect_name, &ctfsize, NULL)) == NULL)
4368 bfd_fatal (bfd_get_filename (abfd));
4370 /* Load the CTF file and dump it. Preload the parent dict, since it will
4371 need to be imported into every child in turn. */
4373 ctfsect = make_ctfsect (sect_name, ctfdata, ctfsize);
4374 if ((ctfa = ctf_bfdopen_ctfsect (abfd, &ctfsect, &err)) == NULL)
4376 dump_ctf_errs (NULL);
4377 non_fatal (_("CTF open failure: %s"), ctf_errmsg (err));
4378 bfd_fatal (bfd_get_filename (abfd));
4381 if ((parent = ctf_dict_open (ctfa, parent_name, &err)) == NULL)
4383 dump_ctf_errs (NULL);
4384 non_fatal (_("CTF open failure: %s"), ctf_errmsg (err));
4385 bfd_fatal (bfd_get_filename (abfd));
4388 printf (_("Contents of CTF section %s:\n"), sanitize_string (sect_name));
4390 while ((fp = ctf_archive_next (ctfa, &i, &name, 0, &err)) != NULL)
4391 dump_ctf_archive_member (fp, name, parent, member++);
4392 if (err != ECTF_NEXT_END)
4394 dump_ctf_errs (NULL);
4395 non_fatal (_("CTF archive member open failure: %s"), ctf_errmsg (err));
4396 bfd_fatal (bfd_get_filename (abfd));
4398 ctf_dict_close (parent);
4399 ctf_close (ctfa);
4400 free (ctfdata);
4402 #else
4403 static void
4404 dump_ctf (bfd *abfd ATTRIBUTE_UNUSED, const char *sect_name ATTRIBUTE_UNUSED,
4405 const char *parent_name ATTRIBUTE_UNUSED) {}
4406 #endif
4409 static void
4410 dump_bfd_private_header (bfd *abfd)
4412 if (!bfd_print_private_bfd_data (abfd, stdout))
4413 non_fatal (_("warning: private headers incomplete: %s"),
4414 bfd_errmsg (bfd_get_error ()));
4417 static void
4418 dump_target_specific (bfd *abfd)
4420 const struct objdump_private_desc * const *desc;
4421 struct objdump_private_option *opt;
4422 char *e, *b;
4424 /* Find the desc. */
4425 for (desc = objdump_private_vectors; *desc != NULL; desc++)
4426 if ((*desc)->filter (abfd))
4427 break;
4429 if (*desc == NULL)
4431 non_fatal (_("option -P/--private not supported by this file"));
4432 return;
4435 /* Clear all options. */
4436 for (opt = (*desc)->options; opt->name; opt++)
4437 opt->selected = false;
4439 /* Decode options. */
4440 b = dump_private_options;
4443 e = strchr (b, ',');
4445 if (e)
4446 *e = 0;
4448 for (opt = (*desc)->options; opt->name; opt++)
4449 if (strcmp (opt->name, b) == 0)
4451 opt->selected = true;
4452 break;
4454 if (opt->name == NULL)
4455 non_fatal (_("target specific dump '%s' not supported"), b);
4457 if (e)
4459 *e = ',';
4460 b = e + 1;
4463 while (e != NULL);
4465 /* Dump. */
4466 (*desc)->dump (abfd);
4469 /* Display a section in hexadecimal format with associated characters.
4470 Each line prefixed by the zero padded address. */
4472 static void
4473 dump_section (bfd *abfd, asection *section, void *dummy ATTRIBUTE_UNUSED)
4475 bfd_byte *data = NULL;
4476 bfd_size_type datasize;
4477 bfd_vma addr_offset;
4478 bfd_vma start_offset;
4479 bfd_vma stop_offset;
4480 unsigned int opb = bfd_octets_per_byte (abfd, section);
4481 /* Bytes per line. */
4482 const int onaline = 16;
4483 char buf[64];
4484 int count;
4485 int width;
4487 if (! process_section_p (section))
4488 return;
4490 if ((section->flags & SEC_HAS_CONTENTS) == 0)
4491 return;
4493 if ((datasize = bfd_section_size (section)) == 0)
4494 return;
4496 /* Compute the address range to display. */
4497 if (start_address == (bfd_vma) -1
4498 || start_address < section->vma)
4499 start_offset = 0;
4500 else
4501 start_offset = start_address - section->vma;
4503 if (stop_address == (bfd_vma) -1)
4504 stop_offset = datasize / opb;
4505 else
4507 if (stop_address < section->vma)
4508 stop_offset = 0;
4509 else
4510 stop_offset = stop_address - section->vma;
4512 if (stop_offset > datasize / opb)
4513 stop_offset = datasize / opb;
4516 if (start_offset >= stop_offset)
4517 return;
4519 printf (_("Contents of section %s:"), sanitize_string (section->name));
4520 if (display_file_offsets)
4521 printf (_(" (Starting at file offset: 0x%lx)"),
4522 (unsigned long) (section->filepos + start_offset));
4523 printf ("\n");
4525 if (!bfd_get_full_section_contents (abfd, section, &data))
4527 non_fatal (_("Reading section %s failed because: %s"),
4528 section->name, bfd_errmsg (bfd_get_error ()));
4529 return;
4532 width = 4;
4534 bfd_sprintf_vma (abfd, buf, start_offset + section->vma);
4535 if (strlen (buf) >= sizeof (buf))
4536 abort ();
4538 count = 0;
4539 while (buf[count] == '0' && buf[count+1] != '\0')
4540 count++;
4541 count = strlen (buf) - count;
4542 if (count > width)
4543 width = count;
4545 bfd_sprintf_vma (abfd, buf, stop_offset + section->vma - 1);
4546 if (strlen (buf) >= sizeof (buf))
4547 abort ();
4549 count = 0;
4550 while (buf[count] == '0' && buf[count+1] != '\0')
4551 count++;
4552 count = strlen (buf) - count;
4553 if (count > width)
4554 width = count;
4556 for (addr_offset = start_offset;
4557 addr_offset < stop_offset; addr_offset += onaline / opb)
4559 bfd_size_type j;
4561 bfd_sprintf_vma (abfd, buf, (addr_offset + section->vma));
4562 count = strlen (buf);
4563 if ((size_t) count >= sizeof (buf))
4564 abort ();
4566 putchar (' ');
4567 while (count < width)
4569 putchar ('0');
4570 count++;
4572 fputs (buf + count - width, stdout);
4573 putchar (' ');
4575 for (j = addr_offset * opb;
4576 j < addr_offset * opb + onaline; j++)
4578 if (j < stop_offset * opb)
4579 printf ("%02x", (unsigned) (data[j]));
4580 else
4581 printf (" ");
4582 if ((j & 3) == 3)
4583 printf (" ");
4586 printf (" ");
4587 for (j = addr_offset * opb;
4588 j < addr_offset * opb + onaline; j++)
4590 if (j >= stop_offset * opb)
4591 printf (" ");
4592 else
4593 printf ("%c", ISPRINT (data[j]) ? data[j] : '.');
4595 putchar ('\n');
4597 free (data);
4600 /* Actually display the various requested regions. */
4602 static void
4603 dump_data (bfd *abfd)
4605 bfd_map_over_sections (abfd, dump_section, NULL);
4608 /* Should perhaps share code and display with nm? */
4610 static void
4611 dump_symbols (bfd *abfd ATTRIBUTE_UNUSED, bool dynamic)
4613 asymbol **current;
4614 long max_count;
4615 long count;
4617 if (dynamic)
4619 current = dynsyms;
4620 max_count = dynsymcount;
4621 printf ("DYNAMIC SYMBOL TABLE:\n");
4623 else
4625 current = syms;
4626 max_count = symcount;
4627 printf ("SYMBOL TABLE:\n");
4630 if (max_count == 0)
4631 printf (_("no symbols\n"));
4633 for (count = 0; count < max_count; count++)
4635 bfd *cur_bfd;
4637 if (*current == NULL)
4638 printf (_("no information for symbol number %ld\n"), count);
4640 else if ((cur_bfd = bfd_asymbol_bfd (*current)) == NULL)
4641 printf (_("could not determine the type of symbol number %ld\n"),
4642 count);
4644 else if (process_section_p ((* current)->section)
4645 && (dump_special_syms
4646 || !bfd_is_target_special_symbol (cur_bfd, *current)))
4648 const char *name = (*current)->name;
4650 if (do_demangle && name != NULL && *name != '\0')
4652 char *alloc;
4654 /* If we want to demangle the name, we demangle it
4655 here, and temporarily clobber it while calling
4656 bfd_print_symbol. FIXME: This is a gross hack. */
4657 alloc = bfd_demangle (cur_bfd, name, demangle_flags);
4658 if (alloc != NULL)
4659 (*current)->name = alloc;
4660 bfd_print_symbol (cur_bfd, stdout, *current,
4661 bfd_print_symbol_all);
4662 if (alloc != NULL)
4664 (*current)->name = name;
4665 free (alloc);
4668 else if (unicode_display != unicode_default
4669 && name != NULL && *name != '\0')
4671 const char * sanitized_name;
4673 /* If we want to sanitize the name, we do it here, and
4674 temporarily clobber it while calling bfd_print_symbol.
4675 FIXME: This is a gross hack. */
4676 sanitized_name = sanitize_string (name);
4677 if (sanitized_name != name)
4678 (*current)->name = sanitized_name;
4679 else
4680 sanitized_name = NULL;
4681 bfd_print_symbol (cur_bfd, stdout, *current,
4682 bfd_print_symbol_all);
4683 if (sanitized_name != NULL)
4684 (*current)->name = name;
4686 else
4687 bfd_print_symbol (cur_bfd, stdout, *current,
4688 bfd_print_symbol_all);
4689 printf ("\n");
4692 current++;
4694 printf ("\n\n");
4697 static void
4698 dump_reloc_set (bfd *abfd, asection *sec, arelent **relpp, long relcount)
4700 arelent **p;
4701 char *last_filename, *last_functionname;
4702 unsigned int last_line;
4703 unsigned int last_discriminator;
4705 /* Get column headers lined up reasonably. */
4707 static int width;
4709 if (width == 0)
4711 char buf[30];
4713 bfd_sprintf_vma (abfd, buf, (bfd_vma) -1);
4714 width = strlen (buf) - 7;
4716 printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
4719 last_filename = NULL;
4720 last_functionname = NULL;
4721 last_line = 0;
4722 last_discriminator = 0;
4724 for (p = relpp; relcount && *p != NULL; p++, relcount--)
4726 arelent *q = *p;
4727 const char *filename, *functionname;
4728 unsigned int linenumber;
4729 unsigned int discriminator;
4730 const char *sym_name;
4731 const char *section_name;
4732 bfd_vma addend2 = 0;
4734 if (start_address != (bfd_vma) -1
4735 && q->address < start_address)
4736 continue;
4737 if (stop_address != (bfd_vma) -1
4738 && q->address > stop_address)
4739 continue;
4741 if (with_line_numbers
4742 && sec != NULL
4743 && bfd_find_nearest_line_discriminator (abfd, sec, syms, q->address,
4744 &filename, &functionname,
4745 &linenumber, &discriminator))
4747 if (functionname != NULL
4748 && (last_functionname == NULL
4749 || strcmp (functionname, last_functionname) != 0))
4751 printf ("%s():\n", sanitize_string (functionname));
4752 if (last_functionname != NULL)
4753 free (last_functionname);
4754 last_functionname = xstrdup (functionname);
4757 if (linenumber > 0
4758 && (linenumber != last_line
4759 || (filename != NULL
4760 && last_filename != NULL
4761 && filename_cmp (filename, last_filename) != 0)
4762 || (discriminator != last_discriminator)))
4764 if (discriminator > 0)
4765 printf ("%s:%u\n", filename == NULL ? "???" :
4766 sanitize_string (filename), linenumber);
4767 else
4768 printf ("%s:%u (discriminator %u)\n",
4769 filename == NULL ? "???" : sanitize_string (filename),
4770 linenumber, discriminator);
4771 last_line = linenumber;
4772 last_discriminator = discriminator;
4773 if (last_filename != NULL)
4774 free (last_filename);
4775 if (filename == NULL)
4776 last_filename = NULL;
4777 else
4778 last_filename = xstrdup (filename);
4782 if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
4784 sym_name = (*(q->sym_ptr_ptr))->name;
4785 section_name = (*(q->sym_ptr_ptr))->section->name;
4787 else
4789 sym_name = NULL;
4790 section_name = NULL;
4793 bfd_printf_vma (abfd, q->address);
4794 if (q->howto == NULL)
4795 printf (" *unknown* ");
4796 else if (q->howto->name)
4798 const char *name = q->howto->name;
4800 /* R_SPARC_OLO10 relocations contain two addends.
4801 But because 'arelent' lacks enough storage to
4802 store them both, the 64-bit ELF Sparc backend
4803 records this as two relocations. One R_SPARC_LO10
4804 and one R_SPARC_13, both pointing to the same
4805 address. This is merely so that we have some
4806 place to store both addend fields.
4808 Undo this transformation, otherwise the output
4809 will be confusing. */
4810 if (abfd->xvec->flavour == bfd_target_elf_flavour
4811 && elf_tdata (abfd)->elf_header->e_machine == EM_SPARCV9
4812 && relcount > 1
4813 && !strcmp (q->howto->name, "R_SPARC_LO10"))
4815 arelent *q2 = *(p + 1);
4816 if (q2 != NULL
4817 && q2->howto
4818 && q->address == q2->address
4819 && !strcmp (q2->howto->name, "R_SPARC_13"))
4821 name = "R_SPARC_OLO10";
4822 addend2 = q2->addend;
4823 p++;
4826 printf (" %-16s ", name);
4828 else
4829 printf (" %-16d ", q->howto->type);
4831 if (sym_name)
4833 objdump_print_symname (abfd, NULL, *q->sym_ptr_ptr);
4835 else
4837 if (section_name == NULL)
4838 section_name = "*unknown*";
4839 printf ("[%s]", sanitize_string (section_name));
4842 if (q->addend)
4844 bfd_signed_vma addend = q->addend;
4845 if (addend < 0)
4847 printf ("-0x");
4848 addend = -addend;
4850 else
4851 printf ("+0x");
4852 bfd_printf_vma (abfd, addend);
4854 if (addend2)
4856 printf ("+0x");
4857 bfd_printf_vma (abfd, addend2);
4860 printf ("\n");
4863 if (last_filename != NULL)
4864 free (last_filename);
4865 if (last_functionname != NULL)
4866 free (last_functionname);
4869 static void
4870 dump_relocs_in_section (bfd *abfd,
4871 asection *section,
4872 void *dummy ATTRIBUTE_UNUSED)
4874 arelent **relpp = NULL;
4875 long relcount;
4876 long relsize;
4878 if ( bfd_is_abs_section (section)
4879 || bfd_is_und_section (section)
4880 || bfd_is_com_section (section)
4881 || (! process_section_p (section))
4882 || ((section->flags & SEC_RELOC) == 0))
4883 return;
4885 printf ("RELOCATION RECORDS FOR [%s]:", sanitize_string (section->name));
4887 relsize = bfd_get_reloc_upper_bound (abfd, section);
4888 if (relsize == 0)
4890 printf (" (none)\n\n");
4891 return;
4894 if (relsize < 0)
4895 relcount = relsize;
4896 else
4898 relpp = (arelent **) xmalloc (relsize);
4899 relcount = bfd_canonicalize_reloc (abfd, section, relpp, syms);
4902 if (relcount < 0)
4904 printf ("\n");
4905 non_fatal (_("failed to read relocs in: %s"),
4906 sanitize_string (bfd_get_filename (abfd)));
4907 bfd_fatal (_("error message was"));
4909 else if (relcount == 0)
4910 printf (" (none)\n\n");
4911 else
4913 printf ("\n");
4914 dump_reloc_set (abfd, section, relpp, relcount);
4915 printf ("\n\n");
4917 free (relpp);
4920 static void
4921 dump_relocs (bfd *abfd)
4923 bfd_map_over_sections (abfd, dump_relocs_in_section, NULL);
4926 static void
4927 dump_dynamic_relocs (bfd *abfd)
4929 long relsize;
4930 arelent **relpp;
4931 long relcount;
4933 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
4934 if (relsize < 0)
4935 bfd_fatal (bfd_get_filename (abfd));
4937 printf ("DYNAMIC RELOCATION RECORDS");
4939 if (relsize == 0)
4940 printf (" (none)\n\n");
4941 else
4943 relpp = (arelent **) xmalloc (relsize);
4944 relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
4946 if (relcount < 0)
4947 bfd_fatal (bfd_get_filename (abfd));
4948 else if (relcount == 0)
4949 printf (" (none)\n\n");
4950 else
4952 printf ("\n");
4953 dump_reloc_set (abfd, NULL, relpp, relcount);
4954 printf ("\n\n");
4956 free (relpp);
4960 /* Creates a table of paths, to search for source files. */
4962 static void
4963 add_include_path (const char *path)
4965 if (path[0] == 0)
4966 return;
4967 include_path_count++;
4968 include_paths = (const char **)
4969 xrealloc (include_paths, include_path_count * sizeof (*include_paths));
4970 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
4971 if (path[1] == ':' && path[2] == 0)
4972 path = concat (path, ".", (const char *) 0);
4973 #endif
4974 include_paths[include_path_count - 1] = path;
4977 static void
4978 adjust_addresses (bfd *abfd ATTRIBUTE_UNUSED,
4979 asection *section,
4980 void *arg)
4982 if ((section->flags & SEC_DEBUGGING) == 0)
4984 bool *has_reloc_p = (bool *) arg;
4985 section->vma += adjust_section_vma;
4986 if (*has_reloc_p)
4987 section->lma += adjust_section_vma;
4991 /* Return the sign-extended form of an ARCH_SIZE sized VMA. */
4993 static bfd_vma
4994 sign_extend_address (bfd *abfd ATTRIBUTE_UNUSED,
4995 bfd_vma vma,
4996 unsigned arch_size)
4998 bfd_vma mask;
4999 mask = (bfd_vma) 1 << (arch_size - 1);
5000 return (((vma & ((mask << 1) - 1)) ^ mask) - mask);
5003 /* Dump selected contents of ABFD. */
5005 static void
5006 dump_bfd (bfd *abfd, bool is_mainfile)
5008 const struct elf_backend_data * bed;
5010 if (bfd_big_endian (abfd))
5011 byte_get = byte_get_big_endian;
5012 else if (bfd_little_endian (abfd))
5013 byte_get = byte_get_little_endian;
5014 else
5015 byte_get = NULL;
5017 /* Load any separate debug information files.
5018 We do this now and without checking do_follow_links because separate
5019 debug info files may contain symbol tables that we will need when
5020 displaying information about the main file. Any memory allocated by
5021 load_separate_debug_files will be released when we call
5022 free_debug_memory below.
5024 The test on is_mainfile is there because the chain of separate debug
5025 info files is a global variable shared by all invocations of dump_bfd. */
5026 if (byte_get != NULL && is_mainfile)
5028 load_separate_debug_files (abfd, bfd_get_filename (abfd));
5030 /* If asked to do so, recursively dump the separate files. */
5031 if (do_follow_links)
5033 separate_info * i;
5035 for (i = first_separate_info; i != NULL; i = i->next)
5036 dump_bfd (i->handle, false);
5040 /* Adjust user-specified start and stop limits for targets that use
5041 signed addresses. */
5042 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
5043 && (bed = get_elf_backend_data (abfd)) != NULL
5044 && bed->sign_extend_vma)
5046 start_address = sign_extend_address (abfd, start_address,
5047 bed->s->arch_size);
5048 stop_address = sign_extend_address (abfd, stop_address,
5049 bed->s->arch_size);
5052 /* If we are adjusting section VMA's, change them all now. Changing
5053 the BFD information is a hack. However, we must do it, or
5054 bfd_find_nearest_line will not do the right thing. */
5055 if (adjust_section_vma != 0)
5057 bool has_reloc = (abfd->flags & HAS_RELOC);
5058 bfd_map_over_sections (abfd, adjust_addresses, &has_reloc);
5061 if (is_mainfile || process_links)
5063 if (! dump_debugging_tags && ! suppress_bfd_header)
5064 printf (_("\n%s: file format %s\n"),
5065 sanitize_string (bfd_get_filename (abfd)),
5066 abfd->xvec->name);
5067 if (dump_ar_hdrs)
5068 print_arelt_descr (stdout, abfd, true, false);
5069 if (dump_file_header)
5070 dump_bfd_header (abfd);
5071 if (dump_private_headers)
5072 dump_bfd_private_header (abfd);
5073 if (dump_private_options != NULL)
5074 dump_target_specific (abfd);
5075 if (! dump_debugging_tags && ! suppress_bfd_header)
5076 putchar ('\n');
5079 if (dump_symtab
5080 || dump_reloc_info
5081 || disassemble
5082 || dump_debugging
5083 || dump_dwarf_section_info)
5085 syms = slurp_symtab (abfd);
5087 /* If following links, load any symbol tables from the linked files as well. */
5088 if (do_follow_links && is_mainfile)
5090 separate_info * i;
5092 for (i = first_separate_info; i != NULL; i = i->next)
5094 asymbol ** extra_syms;
5095 long old_symcount = symcount;
5097 extra_syms = slurp_symtab (i->handle);
5099 if (extra_syms)
5101 if (old_symcount == 0)
5103 syms = extra_syms;
5105 else
5107 syms = xrealloc (syms, ((symcount + old_symcount + 1)
5108 * sizeof (asymbol *)));
5109 memcpy (syms + old_symcount,
5110 extra_syms,
5111 (symcount + 1) * sizeof (asymbol *));
5115 symcount += old_symcount;
5120 if (is_mainfile || process_links)
5122 if (dump_section_headers)
5123 dump_headers (abfd);
5125 if (dump_dynamic_symtab || dump_dynamic_reloc_info
5126 || (disassemble && bfd_get_dynamic_symtab_upper_bound (abfd) > 0))
5127 dynsyms = slurp_dynamic_symtab (abfd);
5129 if (disassemble)
5131 synthcount = bfd_get_synthetic_symtab (abfd, symcount, syms,
5132 dynsymcount, dynsyms,
5133 &synthsyms);
5134 if (synthcount < 0)
5135 synthcount = 0;
5138 if (dump_symtab)
5139 dump_symbols (abfd, false);
5140 if (dump_dynamic_symtab)
5141 dump_symbols (abfd, true);
5143 if (dump_dwarf_section_info)
5144 dump_dwarf (abfd, is_mainfile);
5145 if (is_mainfile || process_links)
5147 if (dump_ctf_section_info)
5148 dump_ctf (abfd, dump_ctf_section_name, dump_ctf_parent_name);
5149 if (dump_stab_section_info)
5150 dump_stabs (abfd);
5151 if (dump_reloc_info && ! disassemble)
5152 dump_relocs (abfd);
5153 if (dump_dynamic_reloc_info && ! disassemble)
5154 dump_dynamic_relocs (abfd);
5155 if (dump_section_contents)
5156 dump_data (abfd);
5157 if (disassemble)
5158 disassemble_data (abfd);
5161 if (dump_debugging)
5163 void *dhandle;
5165 dhandle = read_debugging_info (abfd, syms, symcount, true);
5166 if (dhandle != NULL)
5168 if (!print_debugging_info (stdout, dhandle, abfd, syms,
5169 bfd_demangle,
5170 dump_debugging_tags != 0))
5172 non_fatal (_("%s: printing debugging information failed"),
5173 bfd_get_filename (abfd));
5174 exit_status = 1;
5177 free (dhandle);
5179 /* PR 6483: If there was no STABS debug info in the file, try
5180 DWARF instead. */
5181 else if (! dump_dwarf_section_info)
5183 dwarf_select_sections_all ();
5184 dump_dwarf (abfd, is_mainfile);
5188 if (syms)
5190 free (syms);
5191 syms = NULL;
5194 if (dynsyms)
5196 free (dynsyms);
5197 dynsyms = NULL;
5200 if (synthsyms)
5202 free (synthsyms);
5203 synthsyms = NULL;
5206 symcount = 0;
5207 dynsymcount = 0;
5208 synthcount = 0;
5210 if (is_mainfile)
5211 free_debug_memory ();
5214 static void
5215 display_object_bfd (bfd *abfd)
5217 char **matching;
5219 if (bfd_check_format_matches (abfd, bfd_object, &matching))
5221 dump_bfd (abfd, true);
5222 return;
5225 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
5227 nonfatal (bfd_get_filename (abfd));
5228 list_matching_formats (matching);
5229 free (matching);
5230 return;
5233 if (bfd_get_error () != bfd_error_file_not_recognized)
5235 nonfatal (bfd_get_filename (abfd));
5236 return;
5239 if (bfd_check_format_matches (abfd, bfd_core, &matching))
5241 dump_bfd (abfd, true);
5242 return;
5245 nonfatal (bfd_get_filename (abfd));
5247 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
5249 list_matching_formats (matching);
5250 free (matching);
5254 static void
5255 display_any_bfd (bfd *file, int level)
5257 /* Decompress sections unless dumping the section contents. */
5258 if (!dump_section_contents)
5259 file->flags |= BFD_DECOMPRESS;
5261 /* If the file is an archive, process all of its elements. */
5262 if (bfd_check_format (file, bfd_archive))
5264 bfd *arfile = NULL;
5265 bfd *last_arfile = NULL;
5267 if (level == 0)
5268 printf (_("In archive %s:\n"), sanitize_string (bfd_get_filename (file)));
5269 else if (level > 100)
5271 /* Prevent corrupted files from spinning us into an
5272 infinite loop. 100 is an arbitrary heuristic. */
5273 fatal (_("Archive nesting is too deep"));
5274 return;
5276 else
5277 printf (_("In nested archive %s:\n"),
5278 sanitize_string (bfd_get_filename (file)));
5280 for (;;)
5282 bfd_set_error (bfd_error_no_error);
5284 arfile = bfd_openr_next_archived_file (file, arfile);
5285 if (arfile == NULL)
5287 if (bfd_get_error () != bfd_error_no_more_archived_files)
5288 nonfatal (bfd_get_filename (file));
5289 break;
5292 display_any_bfd (arfile, level + 1);
5294 if (last_arfile != NULL)
5296 bfd_close (last_arfile);
5297 /* PR 17512: file: ac585d01. */
5298 if (arfile == last_arfile)
5300 last_arfile = NULL;
5301 break;
5304 last_arfile = arfile;
5307 if (last_arfile != NULL)
5308 bfd_close (last_arfile);
5310 else
5311 display_object_bfd (file);
5314 static void
5315 display_file (char *filename, char *target, bool last_file)
5317 bfd *file;
5319 if (get_file_size (filename) < 1)
5321 exit_status = 1;
5322 return;
5325 file = bfd_openr (filename, target);
5326 if (file == NULL)
5328 nonfatal (filename);
5329 return;
5332 display_any_bfd (file, 0);
5334 /* This is an optimization to improve the speed of objdump, especially when
5335 dumping a file with lots of associated debug informatiom. Calling
5336 bfd_close on such a file can take a non-trivial amount of time as there
5337 are lots of lists to walk and buffers to free. This is only really
5338 necessary however if we are about to load another file and we need the
5339 memory back. Otherwise, if we are about to exit, then we can save (a lot
5340 of) time by only doing a quick close, and allowing the OS to reclaim the
5341 memory for us. */
5342 if (! last_file)
5343 bfd_close (file);
5344 else
5345 bfd_close_all_done (file);
5349 main (int argc, char **argv)
5351 int c;
5352 char *target = default_target;
5353 bool seenflag = false;
5355 #ifdef HAVE_LC_MESSAGES
5356 setlocale (LC_MESSAGES, "");
5357 #endif
5358 setlocale (LC_CTYPE, "");
5360 bindtextdomain (PACKAGE, LOCALEDIR);
5361 textdomain (PACKAGE);
5363 program_name = *argv;
5364 xmalloc_set_program_name (program_name);
5365 bfd_set_error_program_name (program_name);
5367 START_PROGRESS (program_name, 0);
5369 expandargv (&argc, &argv);
5371 if (bfd_init () != BFD_INIT_MAGIC)
5372 fatal (_("fatal error: libbfd ABI mismatch"));
5373 set_default_bfd_target ();
5375 while ((c = getopt_long (argc, argv,
5376 "CDE:FGHI:LM:P:RSTU:VW::ab:defghij:lm:prstvwxz",
5377 long_options, (int *) 0))
5378 != EOF)
5380 switch (c)
5382 case 0:
5383 break; /* We've been given a long option. */
5384 case 'm':
5385 machine = optarg;
5386 break;
5387 case 'M':
5389 char *options;
5390 if (disassembler_options)
5391 /* Ignore potential memory leak for now. */
5392 options = concat (disassembler_options, ",",
5393 optarg, (const char *) NULL);
5394 else
5395 options = optarg;
5396 disassembler_options = remove_whitespace_and_extra_commas (options);
5398 break;
5399 case 'j':
5400 add_only (optarg);
5401 break;
5402 case 'F':
5403 display_file_offsets = true;
5404 break;
5405 case 'l':
5406 with_line_numbers = true;
5407 break;
5408 case 'b':
5409 target = optarg;
5410 break;
5411 case 'C':
5412 do_demangle = true;
5413 if (optarg != NULL)
5415 enum demangling_styles style;
5417 style = cplus_demangle_name_to_style (optarg);
5418 if (style == unknown_demangling)
5419 fatal (_("unknown demangling style `%s'"),
5420 optarg);
5422 cplus_demangle_set_style (style);
5424 break;
5425 case OPTION_RECURSE_LIMIT:
5426 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
5427 break;
5428 case OPTION_NO_RECURSE_LIMIT:
5429 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
5430 break;
5431 case 'w':
5432 do_wide = wide_output = true;
5433 break;
5434 case OPTION_ADJUST_VMA:
5435 adjust_section_vma = parse_vma (optarg, "--adjust-vma");
5436 break;
5437 case OPTION_START_ADDRESS:
5438 start_address = parse_vma (optarg, "--start-address");
5439 if ((stop_address != (bfd_vma) -1) && stop_address <= start_address)
5440 fatal (_("error: the start address should be before the end address"));
5441 break;
5442 case OPTION_STOP_ADDRESS:
5443 stop_address = parse_vma (optarg, "--stop-address");
5444 if ((start_address != (bfd_vma) -1) && stop_address <= start_address)
5445 fatal (_("error: the stop address should be after the start address"));
5446 break;
5447 case OPTION_PREFIX:
5448 prefix = optarg;
5449 prefix_length = strlen (prefix);
5450 /* Remove an unnecessary trailing '/' */
5451 while (IS_DIR_SEPARATOR (prefix[prefix_length - 1]))
5452 prefix_length--;
5453 break;
5454 case OPTION_PREFIX_STRIP:
5455 prefix_strip = atoi (optarg);
5456 if (prefix_strip < 0)
5457 fatal (_("error: prefix strip must be non-negative"));
5458 break;
5459 case OPTION_INSN_WIDTH:
5460 insn_width = strtoul (optarg, NULL, 0);
5461 if (insn_width <= 0)
5462 fatal (_("error: instruction width must be positive"));
5463 break;
5464 case OPTION_INLINES:
5465 unwind_inlines = true;
5466 break;
5467 case OPTION_VISUALIZE_JUMPS:
5468 visualize_jumps = true;
5469 color_output = false;
5470 extended_color_output = false;
5471 if (optarg != NULL)
5473 if (streq (optarg, "color"))
5474 color_output = true;
5475 else if (streq (optarg, "extended-color"))
5477 color_output = true;
5478 extended_color_output = true;
5480 else if (streq (optarg, "off"))
5481 visualize_jumps = false;
5482 else
5483 nonfatal (_("unrecognized argument to --visualize-option"));
5485 break;
5486 case 'E':
5487 if (strcmp (optarg, "B") == 0)
5488 endian = BFD_ENDIAN_BIG;
5489 else if (strcmp (optarg, "L") == 0)
5490 endian = BFD_ENDIAN_LITTLE;
5491 else
5493 nonfatal (_("unrecognized -E option"));
5494 usage (stderr, 1);
5496 break;
5497 case OPTION_ENDIAN:
5498 if (strncmp (optarg, "big", strlen (optarg)) == 0)
5499 endian = BFD_ENDIAN_BIG;
5500 else if (strncmp (optarg, "little", strlen (optarg)) == 0)
5501 endian = BFD_ENDIAN_LITTLE;
5502 else
5504 non_fatal (_("unrecognized --endian type `%s'"), optarg);
5505 exit_status = 1;
5506 usage (stderr, 1);
5508 break;
5510 case 'f':
5511 dump_file_header = true;
5512 seenflag = true;
5513 break;
5514 case 'i':
5515 formats_info = true;
5516 seenflag = true;
5517 break;
5518 case 'I':
5519 add_include_path (optarg);
5520 break;
5521 case 'p':
5522 dump_private_headers = true;
5523 seenflag = true;
5524 break;
5525 case 'P':
5526 dump_private_options = optarg;
5527 seenflag = true;
5528 break;
5529 case 'x':
5530 dump_private_headers = true;
5531 dump_symtab = true;
5532 dump_reloc_info = true;
5533 dump_file_header = true;
5534 dump_ar_hdrs = true;
5535 dump_section_headers = true;
5536 seenflag = true;
5537 break;
5538 case 't':
5539 dump_symtab = true;
5540 seenflag = true;
5541 break;
5542 case 'T':
5543 dump_dynamic_symtab = true;
5544 seenflag = true;
5545 break;
5546 case 'd':
5547 disassemble = true;
5548 seenflag = true;
5549 disasm_sym = optarg;
5550 break;
5551 case 'z':
5552 disassemble_zeroes = true;
5553 break;
5554 case 'D':
5555 disassemble = true;
5556 disassemble_all = true;
5557 seenflag = true;
5558 break;
5559 case 'S':
5560 disassemble = true;
5561 with_source_code = true;
5562 seenflag = true;
5563 break;
5564 case OPTION_SOURCE_COMMENT:
5565 disassemble = true;
5566 with_source_code = true;
5567 seenflag = true;
5568 if (optarg)
5569 source_comment = xstrdup (sanitize_string (optarg));
5570 else
5571 source_comment = xstrdup ("# ");
5572 break;
5573 case 'g':
5574 dump_debugging = 1;
5575 seenflag = true;
5576 break;
5577 case 'e':
5578 dump_debugging = 1;
5579 dump_debugging_tags = 1;
5580 do_demangle = true;
5581 seenflag = true;
5582 break;
5583 case 'L':
5584 process_links = true;
5585 do_follow_links = true;
5586 break;
5587 case 'W':
5588 dump_dwarf_section_info = true;
5589 seenflag = true;
5590 if (optarg)
5591 dwarf_select_sections_by_letters (optarg);
5592 else
5593 dwarf_select_sections_all ();
5594 break;
5595 case OPTION_DWARF:
5596 dump_dwarf_section_info = true;
5597 seenflag = true;
5598 if (optarg)
5599 dwarf_select_sections_by_names (optarg);
5600 else
5601 dwarf_select_sections_all ();
5602 break;
5603 case OPTION_DWARF_DEPTH:
5605 char *cp;
5606 dwarf_cutoff_level = strtoul (optarg, & cp, 0);
5608 break;
5609 case OPTION_DWARF_START:
5611 char *cp;
5612 dwarf_start_die = strtoul (optarg, & cp, 0);
5613 suppress_bfd_header = 1;
5615 break;
5616 case OPTION_DWARF_CHECK:
5617 dwarf_check = true;
5618 break;
5619 #ifdef ENABLE_LIBCTF
5620 case OPTION_CTF:
5621 dump_ctf_section_info = true;
5622 if (optarg)
5623 dump_ctf_section_name = xstrdup (optarg);
5624 seenflag = true;
5625 break;
5626 case OPTION_CTF_PARENT:
5627 dump_ctf_parent_name = xstrdup (optarg);
5628 break;
5629 #endif
5630 case 'G':
5631 dump_stab_section_info = true;
5632 seenflag = true;
5633 break;
5634 case 's':
5635 dump_section_contents = true;
5636 seenflag = true;
5637 break;
5638 case 'r':
5639 dump_reloc_info = true;
5640 seenflag = true;
5641 break;
5642 case 'R':
5643 dump_dynamic_reloc_info = true;
5644 seenflag = true;
5645 break;
5646 case 'a':
5647 dump_ar_hdrs = true;
5648 seenflag = true;
5649 break;
5650 case 'h':
5651 dump_section_headers = true;
5652 seenflag = true;
5653 break;
5654 case 'v':
5655 case 'V':
5656 show_version = true;
5657 seenflag = true;
5658 break;
5660 case 'U':
5661 if (streq (optarg, "default") || streq (optarg, "d"))
5662 unicode_display = unicode_default;
5663 else if (streq (optarg, "locale") || streq (optarg, "l"))
5664 unicode_display = unicode_locale;
5665 else if (streq (optarg, "escape") || streq (optarg, "e"))
5666 unicode_display = unicode_escape;
5667 else if (streq (optarg, "invalid") || streq (optarg, "i"))
5668 unicode_display = unicode_invalid;
5669 else if (streq (optarg, "hex") || streq (optarg, "x"))
5670 unicode_display = unicode_hex;
5671 else if (streq (optarg, "highlight") || streq (optarg, "h"))
5672 unicode_display = unicode_highlight;
5673 else
5674 fatal (_("invalid argument to -U/--unicode: %s"), optarg);
5675 break;
5677 case 'H':
5678 usage (stdout, 0);
5679 /* No need to set seenflag or to break - usage() does not return. */
5680 default:
5681 usage (stderr, 1);
5685 if (show_version)
5686 print_version ("objdump");
5688 if (!seenflag)
5689 usage (stderr, 2);
5691 if (formats_info)
5692 exit_status = display_info ();
5693 else
5695 if (optind == argc)
5696 display_file ("a.out", target, true);
5697 else
5698 for (; optind < argc;)
5700 display_file (argv[optind], target, optind == argc - 1);
5701 optind++;
5705 free_only_list ();
5706 free (dump_ctf_section_name);
5707 free (dump_ctf_parent_name);
5708 free ((void *) source_comment);
5710 END_PROGRESS (program_name);
5712 return exit_status;