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