objdump.c revision 1.6 1 /* objdump.c -- dump information about an object file.
2 Copyright (C) 1990-2018 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 "getopt.h"
60 #include "safe-ctype.h"
61 #include "dis-asm.h"
62 #include "libiberty.h"
63 #include "demangle.h"
64 #include "filenames.h"
65 #include "debug.h"
66 #include "budbg.h"
67 #include "objdump.h"
68
69 #ifdef HAVE_MMAP
70 #include <sys/mman.h>
71 #endif
72
73 /* Internal headers for the ELF .stab-dump code - sorry. */
74 #define BYTES_IN_WORD 32
75 #include "aout/aout64.h"
76
77 /* Exit status. */
78 static int exit_status = 0;
79
80 static char *default_target = NULL; /* Default at runtime. */
81
82 /* The following variables are set based on arguments passed on the
83 command line. */
84 static int show_version = 0; /* Show the version number. */
85 static int dump_section_contents; /* -s */
86 static int dump_section_headers; /* -h */
87 static bfd_boolean dump_file_header; /* -f */
88 static int dump_symtab; /* -t */
89 static int dump_dynamic_symtab; /* -T */
90 static int dump_reloc_info; /* -r */
91 static int dump_dynamic_reloc_info; /* -R */
92 static int dump_ar_hdrs; /* -a */
93 static int dump_private_headers; /* -p */
94 static char *dump_private_options; /* -P */
95 static int prefix_addresses; /* --prefix-addresses */
96 static int with_line_numbers; /* -l */
97 static bfd_boolean with_source_code; /* -S */
98 static int show_raw_insn; /* --show-raw-insn */
99 static int dump_dwarf_section_info; /* --dwarf */
100 static int dump_stab_section_info; /* --stabs */
101 static int do_demangle; /* -C, --demangle */
102 static bfd_boolean disassemble; /* -d */
103 static bfd_boolean disassemble_all; /* -D */
104 static int disassemble_zeroes; /* --disassemble-zeroes */
105 static bfd_boolean formats_info; /* -i */
106 static int wide_output; /* -w */
107 static int insn_width; /* --insn-width */
108 static bfd_vma start_address = (bfd_vma) -1; /* --start-address */
109 static bfd_vma stop_address = (bfd_vma) -1; /* --stop-address */
110 static int dump_debugging; /* --debugging */
111 static int dump_debugging_tags; /* --debugging-tags */
112 static int suppress_bfd_header;
113 static int dump_special_syms = 0; /* --special-syms */
114 static bfd_vma adjust_section_vma = 0; /* --adjust-vma */
115 static int file_start_context = 0; /* --file-start-context */
116 static bfd_boolean display_file_offsets;/* -F */
117 static const char *prefix; /* --prefix */
118 static int prefix_strip; /* --prefix-strip */
119 static size_t prefix_length;
120 static bfd_boolean unwind_inlines; /* --inlines. */
121
122 /* A structure to record the sections mentioned in -j switches. */
123 struct only
124 {
125 const char * name; /* The name of the section. */
126 bfd_boolean seen; /* A flag to indicate that the section has been found in one or more input files. */
127 struct only * next; /* Pointer to the next structure in the list. */
128 };
129 /* Pointer to an array of 'only' structures.
130 This pointer is NULL if the -j switch has not been used. */
131 static struct only * only_list = NULL;
132
133 /* Variables for handling include file path table. */
134 static const char **include_paths;
135 static int include_path_count;
136
137 /* Extra info to pass to the section disassembler and address printing
138 function. */
139 struct objdump_disasm_info
140 {
141 bfd * abfd;
142 asection * sec;
143 bfd_boolean require_sec;
144 arelent ** dynrelbuf;
145 long dynrelcount;
146 disassembler_ftype disassemble_fn;
147 arelent * reloc;
148 };
149
150 /* Architecture to disassemble for, or default if NULL. */
151 static char *machine = NULL;
152
153 /* Target specific options to the disassembler. */
154 static char *disassembler_options = NULL;
155
156 /* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */
157 static enum bfd_endian endian = BFD_ENDIAN_UNKNOWN;
158
159 /* The symbol table. */
160 static asymbol **syms;
161
162 /* Number of symbols in `syms'. */
163 static long symcount = 0;
164
165 /* The sorted symbol table. */
166 static asymbol **sorted_syms;
167
168 /* Number of symbols in `sorted_syms'. */
169 static long sorted_symcount = 0;
170
171 /* The dynamic symbol table. */
172 static asymbol **dynsyms;
173
174 /* The synthetic symbol table. */
175 static asymbol *synthsyms;
176 static long synthcount = 0;
177
178 /* Number of symbols in `dynsyms'. */
179 static long dynsymcount = 0;
180
181 static bfd_byte *stabs;
182 static bfd_size_type stab_size;
183
184 static bfd_byte *strtab;
185 static bfd_size_type stabstr_size;
186
187 static bfd_boolean is_relocatable = FALSE;
188
189 /* Handlers for -P/--private. */
190 static const struct objdump_private_desc * const objdump_private_vectors[] =
191 {
192 OBJDUMP_PRIVATE_VECTORS
193 NULL
194 };
195
196 static void usage (FILE *, int) ATTRIBUTE_NORETURN;
198 static void
199 usage (FILE *stream, int status)
200 {
201 fprintf (stream, _("Usage: %s <option(s)> <file(s)>\n"), program_name);
202 fprintf (stream, _(" Display information from object <file(s)>.\n"));
203 fprintf (stream, _(" At least one of the following switches must be given:\n"));
204 fprintf (stream, _("\
205 -a, --archive-headers Display archive header information\n\
206 -f, --file-headers Display the contents of the overall file header\n\
207 -p, --private-headers Display object format specific file header contents\n\
208 -P, --private=OPT,OPT... Display object format specific contents\n\
209 -h, --[section-]headers Display the contents of the section headers\n\
210 -x, --all-headers Display the contents of all headers\n\
211 -d, --disassemble Display assembler contents of executable sections\n\
212 -D, --disassemble-all Display assembler contents of all sections\n\
213 -S, --source Intermix source code with disassembly\n\
214 -s, --full-contents Display the full contents of all sections requested\n\
215 -g, --debugging Display debug information in object file\n\
216 -e, --debugging-tags Display debug information using ctags style\n\
217 -G, --stabs Display (in raw form) any STABS info in the file\n\
218 -W[lLiaprmfFsoRtUuTgAckK] or\n\
219 --dwarf[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,\n\
220 =frames-interp,=str,=loc,=Ranges,=pubtypes,\n\
221 =gdb_index,=trace_info,=trace_abbrev,=trace_aranges,\n\
222 =addr,=cu_index,=links,=follow-links]\n\
223 Display DWARF info in the file\n\
224 -t, --syms Display the contents of the symbol table(s)\n\
225 -T, --dynamic-syms Display the contents of the dynamic symbol table\n\
226 -r, --reloc Display the relocation entries in the file\n\
227 -R, --dynamic-reloc Display the dynamic relocation entries in the file\n\
228 @<file> Read options from <file>\n\
229 -v, --version Display this program's version number\n\
230 -i, --info List object formats and architectures supported\n\
231 -H, --help Display this information\n\
232 "));
233 if (status != 2)
234 {
235 const struct objdump_private_desc * const *desc;
236
237 fprintf (stream, _("\n The following switches are optional:\n"));
238 fprintf (stream, _("\
239 -b, --target=BFDNAME Specify the target object format as BFDNAME\n\
240 -m, --architecture=MACHINE Specify the target architecture as MACHINE\n\
241 -j, --section=NAME Only display information for section NAME\n\
242 -M, --disassembler-options=OPT Pass text OPT on to the disassembler\n\
243 -EB --endian=big Assume big endian format when disassembling\n\
244 -EL --endian=little Assume little endian format when disassembling\n\
245 --file-start-context Include context from start of file (with -S)\n\
246 -I, --include=DIR Add DIR to search list for source files\n\
247 -l, --line-numbers Include line numbers and filenames in output\n\
248 -F, --file-offsets Include file offsets when displaying information\n\
249 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n\
250 The STYLE, if specified, can be `auto', `gnu',\n\
251 `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
252 or `gnat'\n\
253 -w, --wide Format output for more than 80 columns\n\
254 -z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling\n\
255 --start-address=ADDR Only process data whose address is >= ADDR\n\
256 --stop-address=ADDR Only process data whose address is <= ADDR\n\
257 --prefix-addresses Print complete address alongside disassembly\n\
258 --[no-]show-raw-insn Display hex alongside symbolic disassembly\n\
259 --insn-width=WIDTH Display WIDTH bytes on a single line for -d\n\
260 --adjust-vma=OFFSET Add OFFSET to all displayed section addresses\n\
261 --special-syms Include special symbols in symbol dumps\n\
262 --inlines Print all inlines for source line (with -l)\n\
263 --prefix=PREFIX Add PREFIX to absolute paths for -S\n\
264 --prefix-strip=LEVEL Strip initial directory names for -S\n"));
265 fprintf (stream, _("\
266 --dwarf-depth=N Do not display DIEs at depth N or greater\n\
267 --dwarf-start=N Display DIEs starting with N, at the same depth\n\
268 or deeper\n\
269 --dwarf-check Make additional dwarf internal consistency checks.\
270 \n\n"));
271 list_supported_targets (program_name, stream);
272 list_supported_architectures (program_name, stream);
273
274 disassembler_usage (stream);
275
276 if (objdump_private_vectors[0] != NULL)
277 {
278 fprintf (stream,
279 _("\nOptions supported for -P/--private switch:\n"));
280 for (desc = objdump_private_vectors; *desc != NULL; desc++)
281 (*desc)->help (stream);
282 }
283 }
284 if (REPORT_BUGS_TO[0] && status == 0)
285 fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
286 exit (status);
287 }
288
289 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
290 enum option_values
291 {
292 OPTION_ENDIAN=150,
293 OPTION_START_ADDRESS,
294 OPTION_STOP_ADDRESS,
295 OPTION_DWARF,
296 OPTION_PREFIX,
297 OPTION_PREFIX_STRIP,
298 OPTION_INSN_WIDTH,
299 OPTION_ADJUST_VMA,
300 OPTION_DWARF_DEPTH,
301 OPTION_DWARF_CHECK,
302 OPTION_DWARF_START,
303 OPTION_INLINES
304 };
305
306 static struct option long_options[]=
307 {
308 {"adjust-vma", required_argument, NULL, OPTION_ADJUST_VMA},
309 {"all-headers", no_argument, NULL, 'x'},
310 {"private-headers", no_argument, NULL, 'p'},
311 {"private", required_argument, NULL, 'P'},
312 {"architecture", required_argument, NULL, 'm'},
313 {"archive-headers", no_argument, NULL, 'a'},
314 {"debugging", no_argument, NULL, 'g'},
315 {"debugging-tags", no_argument, NULL, 'e'},
316 {"demangle", optional_argument, NULL, 'C'},
317 {"disassemble", no_argument, NULL, 'd'},
318 {"disassemble-all", no_argument, NULL, 'D'},
319 {"disassembler-options", required_argument, NULL, 'M'},
320 {"disassemble-zeroes", no_argument, NULL, 'z'},
321 {"dynamic-reloc", no_argument, NULL, 'R'},
322 {"dynamic-syms", no_argument, NULL, 'T'},
323 {"endian", required_argument, NULL, OPTION_ENDIAN},
324 {"file-headers", no_argument, NULL, 'f'},
325 {"file-offsets", no_argument, NULL, 'F'},
326 {"file-start-context", no_argument, &file_start_context, 1},
327 {"full-contents", no_argument, NULL, 's'},
328 {"headers", no_argument, NULL, 'h'},
329 {"help", no_argument, NULL, 'H'},
330 {"info", no_argument, NULL, 'i'},
331 {"line-numbers", no_argument, NULL, 'l'},
332 {"no-show-raw-insn", no_argument, &show_raw_insn, -1},
333 {"prefix-addresses", no_argument, &prefix_addresses, 1},
334 {"reloc", no_argument, NULL, 'r'},
335 {"section", required_argument, NULL, 'j'},
336 {"section-headers", no_argument, NULL, 'h'},
337 {"show-raw-insn", no_argument, &show_raw_insn, 1},
338 {"source", no_argument, NULL, 'S'},
339 {"special-syms", no_argument, &dump_special_syms, 1},
340 {"include", required_argument, NULL, 'I'},
341 {"dwarf", optional_argument, NULL, OPTION_DWARF},
342 {"stabs", no_argument, NULL, 'G'},
343 {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
344 {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
345 {"syms", no_argument, NULL, 't'},
346 {"target", required_argument, NULL, 'b'},
347 {"version", no_argument, NULL, 'V'},
348 {"wide", no_argument, NULL, 'w'},
349 {"prefix", required_argument, NULL, OPTION_PREFIX},
350 {"prefix-strip", required_argument, NULL, OPTION_PREFIX_STRIP},
351 {"insn-width", required_argument, NULL, OPTION_INSN_WIDTH},
352 {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
353 {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
354 {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
355 {"inlines", no_argument, 0, OPTION_INLINES},
356 {0, no_argument, 0, 0}
357 };
358
359 static void
361 nonfatal (const char *msg)
362 {
363 bfd_nonfatal (msg);
364 exit_status = 1;
365 }
366
367 /* Returns TRUE if the specified section should be dumped. */
369
370 static bfd_boolean
371 process_section_p (asection * section)
372 {
373 struct only * only;
374
375 if (only_list == NULL)
376 return TRUE;
377
378 for (only = only_list; only; only = only->next)
379 if (strcmp (only->name, section->name) == 0)
380 {
381 only->seen = TRUE;
382 return TRUE;
383 }
384
385 return FALSE;
386 }
387
388 /* Add an entry to the 'only' list. */
389
390 static void
391 add_only (char * name)
392 {
393 struct only * only;
394
395 /* First check to make sure that we do not
396 already have an entry for this name. */
397 for (only = only_list; only; only = only->next)
398 if (strcmp (only->name, name) == 0)
399 return;
400
401 only = xmalloc (sizeof * only);
402 only->name = name;
403 only->seen = FALSE;
404 only->next = only_list;
405 only_list = only;
406 }
407
408 /* Release the memory used by the 'only' list.
409 PR 11225: Issue a warning message for unseen sections.
410 Only do this if none of the sections were seen. This is mainly to support
411 tools like the GAS testsuite where an object file is dumped with a list of
412 generic section names known to be present in a range of different file
413 formats. */
414
415 static void
416 free_only_list (void)
417 {
418 bfd_boolean at_least_one_seen = FALSE;
419 struct only * only;
420 struct only * next;
421
422 if (only_list == NULL)
423 return;
424
425 for (only = only_list; only; only = only->next)
426 if (only->seen)
427 {
428 at_least_one_seen = TRUE;
429 break;
430 }
431
432 for (only = only_list; only; only = next)
433 {
434 if (! at_least_one_seen)
435 {
436 non_fatal (_("section '%s' mentioned in a -j option, "
437 "but not found in any input file"),
438 only->name);
439 exit_status = 1;
440 }
441 next = only->next;
442 free (only);
443 }
444 }
445
446
447 static void
449 dump_section_header (bfd *abfd, asection *section, void *data)
450 {
451 char *comma = "";
452 unsigned int opb = bfd_octets_per_byte (abfd);
453 int longest_section_name = *((int *) data);
454
455 /* Ignore linker created section. See elfNN_ia64_object_p in
456 bfd/elfxx-ia64.c. */
457 if (section->flags & SEC_LINKER_CREATED)
458 return;
459
460 /* PR 10413: Skip sections that we are ignoring. */
461 if (! process_section_p (section))
462 return;
463
464 printf ("%3d %-*s %08lx ", section->index, longest_section_name,
465 bfd_get_section_name (abfd, section),
466 (unsigned long) bfd_section_size (abfd, section) / opb);
467 bfd_printf_vma (abfd, bfd_get_section_vma (abfd, section));
468 printf (" ");
469 bfd_printf_vma (abfd, section->lma);
470 printf (" %08lx 2**%u", (unsigned long) section->filepos,
471 bfd_get_section_alignment (abfd, section));
472 if (! wide_output)
473 printf ("\n ");
474 printf (" ");
475
476 #define PF(x, y) \
477 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
478
479 PF (SEC_HAS_CONTENTS, "CONTENTS");
480 PF (SEC_ALLOC, "ALLOC");
481 PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
482 PF (SEC_LOAD, "LOAD");
483 PF (SEC_RELOC, "RELOC");
484 PF (SEC_READONLY, "READONLY");
485 PF (SEC_CODE, "CODE");
486 PF (SEC_DATA, "DATA");
487 PF (SEC_ROM, "ROM");
488 PF (SEC_DEBUGGING, "DEBUGGING");
489 PF (SEC_NEVER_LOAD, "NEVER_LOAD");
490 PF (SEC_EXCLUDE, "EXCLUDE");
491 PF (SEC_SORT_ENTRIES, "SORT_ENTRIES");
492 if (bfd_get_arch (abfd) == bfd_arch_tic54x)
493 {
494 PF (SEC_TIC54X_BLOCK, "BLOCK");
495 PF (SEC_TIC54X_CLINK, "CLINK");
496 }
497 PF (SEC_SMALL_DATA, "SMALL_DATA");
498 if (bfd_get_flavour (abfd) == bfd_target_coff_flavour)
499 {
500 PF (SEC_COFF_SHARED, "SHARED");
501 PF (SEC_COFF_NOREAD, "NOREAD");
502 }
503 else if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
504 PF (SEC_ELF_PURECODE, "PURECODE");
505 PF (SEC_THREAD_LOCAL, "THREAD_LOCAL");
506 PF (SEC_GROUP, "GROUP");
507 if (bfd_get_arch (abfd) == bfd_arch_mep)
508 {
509 PF (SEC_MEP_VLIW, "VLIW");
510 }
511
512 if ((section->flags & SEC_LINK_ONCE) != 0)
513 {
514 const char *ls;
515 struct coff_comdat_info *comdat;
516
517 switch (section->flags & SEC_LINK_DUPLICATES)
518 {
519 default:
520 abort ();
521 case SEC_LINK_DUPLICATES_DISCARD:
522 ls = "LINK_ONCE_DISCARD";
523 break;
524 case SEC_LINK_DUPLICATES_ONE_ONLY:
525 ls = "LINK_ONCE_ONE_ONLY";
526 break;
527 case SEC_LINK_DUPLICATES_SAME_SIZE:
528 ls = "LINK_ONCE_SAME_SIZE";
529 break;
530 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
531 ls = "LINK_ONCE_SAME_CONTENTS";
532 break;
533 }
534 printf ("%s%s", comma, ls);
535
536 comdat = bfd_coff_get_comdat_section (abfd, section);
537 if (comdat != NULL)
538 printf (" (COMDAT %s %ld)", comdat->name, comdat->symbol);
539
540 comma = ", ";
541 }
542
543 printf ("\n");
544 #undef PF
545 }
546
547 /* Called on each SECTION in ABFD, update the int variable pointed to by
548 DATA which contains the string length of the longest section name. */
549
550 static void
551 find_longest_section_name (bfd *abfd, asection *section, void *data)
552 {
553 int *longest_so_far = (int *) data;
554 const char *name;
555 int len;
556
557 /* Ignore linker created section. */
558 if (section->flags & SEC_LINKER_CREATED)
559 return;
560
561 /* Skip sections that we are ignoring. */
562 if (! process_section_p (section))
563 return;
564
565 name = bfd_get_section_name (abfd, section);
566 len = (int) strlen (name);
567 if (len > *longest_so_far)
568 *longest_so_far = len;
569 }
570
571 static void
572 dump_headers (bfd *abfd)
573 {
574 /* The default width of 13 is just an arbitrary choice. */
575 int max_section_name_length = 13;
576 int bfd_vma_width;
577
578 #ifndef BFD64
579 bfd_vma_width = 10;
580 #else
581 /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */
582 if (bfd_get_arch_size (abfd) == 32)
583 bfd_vma_width = 10;
584 else
585 bfd_vma_width = 18;
586 #endif
587
588 printf (_("Sections:\n"));
589
590 if (wide_output)
591 bfd_map_over_sections (abfd, find_longest_section_name,
592 &max_section_name_length);
593
594 printf (_("Idx %-*s Size %-*s%-*sFile off Algn"),
595 max_section_name_length, "Name",
596 bfd_vma_width, "VMA",
597 bfd_vma_width, "LMA");
598
599 if (wide_output)
600 printf (_(" Flags"));
601 printf ("\n");
602
603 bfd_map_over_sections (abfd, dump_section_header,
604 &max_section_name_length);
605 }
606
607 static asymbol **
609 slurp_symtab (bfd *abfd)
610 {
611 asymbol **sy = NULL;
612 long storage;
613
614 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
615 {
616 symcount = 0;
617 return NULL;
618 }
619
620 storage = bfd_get_symtab_upper_bound (abfd);
621 if (storage < 0)
622 {
623 non_fatal (_("failed to read symbol table from: %s"), bfd_get_filename (abfd));
624 bfd_fatal (_("error message was"));
625 }
626 if (storage)
627 sy = (asymbol **) xmalloc (storage);
628
629 symcount = bfd_canonicalize_symtab (abfd, sy);
630 if (symcount < 0)
631 bfd_fatal (bfd_get_filename (abfd));
632 return sy;
633 }
634
635 /* Read in the dynamic symbols. */
636
637 static asymbol **
638 slurp_dynamic_symtab (bfd *abfd)
639 {
640 asymbol **sy = NULL;
641 long storage;
642
643 storage = bfd_get_dynamic_symtab_upper_bound (abfd);
644 if (storage < 0)
645 {
646 if (!(bfd_get_file_flags (abfd) & DYNAMIC))
647 {
648 non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd));
649 exit_status = 1;
650 dynsymcount = 0;
651 return NULL;
652 }
653
654 bfd_fatal (bfd_get_filename (abfd));
655 }
656 if (storage)
657 sy = (asymbol **) xmalloc (storage);
658
659 dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
660 if (dynsymcount < 0)
661 bfd_fatal (bfd_get_filename (abfd));
662 return sy;
663 }
664
665 /* Some symbol names are significant and should be kept in the
666 table of sorted symbol names, even if they are marked as
667 debugging/section symbols. */
668
669 static bfd_boolean
670 is_significant_symbol_name (const char * name)
671 {
672 return strncmp (name, ".plt", 4) == 0 || strcmp (name, ".got") == 0;
673 }
674
675 /* Filter out (in place) symbols that are useless for disassembly.
676 COUNT is the number of elements in SYMBOLS.
677 Return the number of useful symbols. */
678
679 static long
680 remove_useless_symbols (asymbol **symbols, long count)
681 {
682 asymbol **in_ptr = symbols, **out_ptr = symbols;
683
684 while (--count >= 0)
685 {
686 asymbol *sym = *in_ptr++;
687
688 if (sym->name == NULL || sym->name[0] == '\0')
689 continue;
690 if ((sym->flags & (BSF_DEBUGGING | BSF_SECTION_SYM))
691 && ! is_significant_symbol_name (sym->name))
692 continue;
693 if (bfd_is_und_section (sym->section)
694 || bfd_is_com_section (sym->section))
695 continue;
696
697 *out_ptr++ = sym;
698 }
699 return out_ptr - symbols;
700 }
701
702 /* Sort symbols into value order. */
703
704 static int
705 compare_symbols (const void *ap, const void *bp)
706 {
707 const asymbol *a = * (const asymbol **) ap;
708 const asymbol *b = * (const asymbol **) bp;
709 const char *an;
710 const char *bn;
711 size_t anl;
712 size_t bnl;
713 bfd_boolean af;
714 bfd_boolean bf;
715 flagword aflags;
716 flagword bflags;
717
718 if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
719 return 1;
720 else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
721 return -1;
722
723 if (a->section > b->section)
724 return 1;
725 else if (a->section < b->section)
726 return -1;
727
728 an = bfd_asymbol_name (a);
729 bn = bfd_asymbol_name (b);
730 anl = strlen (an);
731 bnl = strlen (bn);
732
733 /* The symbols gnu_compiled and gcc2_compiled convey no real
734 information, so put them after other symbols with the same value. */
735 af = (strstr (an, "gnu_compiled") != NULL
736 || strstr (an, "gcc2_compiled") != NULL);
737 bf = (strstr (bn, "gnu_compiled") != NULL
738 || strstr (bn, "gcc2_compiled") != NULL);
739
740 if (af && ! bf)
741 return 1;
742 if (! af && bf)
743 return -1;
744
745 /* We use a heuristic for the file name, to try to sort it after
746 more useful symbols. It may not work on non Unix systems, but it
747 doesn't really matter; the only difference is precisely which
748 symbol names get printed. */
749
750 #define file_symbol(s, sn, snl) \
751 (((s)->flags & BSF_FILE) != 0 \
752 || ((sn)[(snl) - 2] == '.' \
753 && ((sn)[(snl) - 1] == 'o' \
754 || (sn)[(snl) - 1] == 'a')))
755
756 af = file_symbol (a, an, anl);
757 bf = file_symbol (b, bn, bnl);
758
759 if (af && ! bf)
760 return 1;
761 if (! af && bf)
762 return -1;
763
764 /* Try to sort global symbols before local symbols before function
765 symbols before debugging symbols. */
766
767 aflags = a->flags;
768 bflags = b->flags;
769
770 if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING))
771 {
772 if ((aflags & BSF_DEBUGGING) != 0)
773 return 1;
774 else
775 return -1;
776 }
777 if ((aflags & BSF_FUNCTION) != (bflags & BSF_FUNCTION))
778 {
779 if ((aflags & BSF_FUNCTION) != 0)
780 return -1;
781 else
782 return 1;
783 }
784 if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL))
785 {
786 if ((aflags & BSF_LOCAL) != 0)
787 return 1;
788 else
789 return -1;
790 }
791 if ((aflags & BSF_GLOBAL) != (bflags & BSF_GLOBAL))
792 {
793 if ((aflags & BSF_GLOBAL) != 0)
794 return -1;
795 else
796 return 1;
797 }
798
799 if (bfd_get_flavour (bfd_asymbol_bfd (a)) == bfd_target_elf_flavour
800 && bfd_get_flavour (bfd_asymbol_bfd (b)) == bfd_target_elf_flavour)
801 {
802 bfd_vma asz, bsz;
803
804 asz = 0;
805 if ((a->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0)
806 asz = ((elf_symbol_type *) a)->internal_elf_sym.st_size;
807 bsz = 0;
808 if ((b->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0)
809 bsz = ((elf_symbol_type *) b)->internal_elf_sym.st_size;
810 if (asz != bsz)
811 return asz > bsz ? -1 : 1;
812 }
813
814 /* Symbols that start with '.' might be section names, so sort them
815 after symbols that don't start with '.'. */
816 if (an[0] == '.' && bn[0] != '.')
817 return 1;
818 if (an[0] != '.' && bn[0] == '.')
819 return -1;
820
821 /* Finally, if we can't distinguish them in any other way, try to
822 get consistent results by sorting the symbols by name. */
823 return strcmp (an, bn);
824 }
825
826 /* Sort relocs into address order. */
827
828 static int
829 compare_relocs (const void *ap, const void *bp)
830 {
831 const arelent *a = * (const arelent **) ap;
832 const arelent *b = * (const arelent **) bp;
833
834 if (a->address > b->address)
835 return 1;
836 else if (a->address < b->address)
837 return -1;
838
839 /* So that associated relocations tied to the same address show up
840 in the correct order, we don't do any further sorting. */
841 if (a > b)
842 return 1;
843 else if (a < b)
844 return -1;
845 else
846 return 0;
847 }
848
849 /* Print an address (VMA) to the output stream in INFO.
850 If SKIP_ZEROES is TRUE, omit leading zeroes. */
851
852 static void
853 objdump_print_value (bfd_vma vma, struct disassemble_info *inf,
854 bfd_boolean skip_zeroes)
855 {
856 char buf[30];
857 char *p;
858 struct objdump_disasm_info *aux;
859
860 aux = (struct objdump_disasm_info *) inf->application_data;
861 bfd_sprintf_vma (aux->abfd, buf, vma);
862 if (! skip_zeroes)
863 p = buf;
864 else
865 {
866 for (p = buf; *p == '0'; ++p)
867 ;
868 if (*p == '\0')
869 --p;
870 }
871 (*inf->fprintf_func) (inf->stream, "%s", p);
872 }
873
874 /* Print the name of a symbol. */
875
876 static void
877 objdump_print_symname (bfd *abfd, struct disassemble_info *inf,
878 asymbol *sym)
879 {
880 char *alloc;
881 const char *name, *version_string = NULL;
882 bfd_boolean hidden = FALSE;
883
884 alloc = NULL;
885 name = bfd_asymbol_name (sym);
886 if (do_demangle && name[0] != '\0')
887 {
888 /* Demangle the name. */
889 alloc = bfd_demangle (abfd, name, DMGL_ANSI | DMGL_PARAMS);
890 if (alloc != NULL)
891 name = alloc;
892 }
893
894 if ((sym->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0)
895 version_string = bfd_get_symbol_version_string (abfd, sym, &hidden);
896
897 if (bfd_is_und_section (bfd_get_section (sym)))
898 hidden = TRUE;
899
900 if (inf != NULL)
901 {
902 (*inf->fprintf_func) (inf->stream, "%s", name);
903 if (version_string && *version_string != '\0')
904 (*inf->fprintf_func) (inf->stream, hidden ? "@%s" : "@@%s",
905 version_string);
906 }
907 else
908 {
909 printf ("%s", name);
910 if (version_string && *version_string != '\0')
911 printf (hidden ? "@%s" : "@@%s", version_string);
912 }
913
914 if (alloc != NULL)
915 free (alloc);
916 }
917
918 /* Locate a symbol given a bfd and a section (from INFO->application_data),
919 and a VMA. If INFO->application_data->require_sec is TRUE, then always
920 require the symbol to be in the section. Returns NULL if there is no
921 suitable symbol. If PLACE is not NULL, then *PLACE is set to the index
922 of the symbol in sorted_syms. */
923
924 static asymbol *
925 find_symbol_for_address (bfd_vma vma,
926 struct disassemble_info *inf,
927 long *place)
928 {
929 /* @@ Would it speed things up to cache the last two symbols returned,
930 and maybe their address ranges? For many processors, only one memory
931 operand can be present at a time, so the 2-entry cache wouldn't be
932 constantly churned by code doing heavy memory accesses. */
933
934 /* Indices in `sorted_syms'. */
935 long min = 0;
936 long max_count = sorted_symcount;
937 long thisplace;
938 struct objdump_disasm_info *aux;
939 bfd *abfd;
940 asection *sec;
941 unsigned int opb;
942 bfd_boolean want_section;
943 long rel_count;
944
945 if (sorted_symcount < 1)
946 return NULL;
947
948 aux = (struct objdump_disasm_info *) inf->application_data;
949 abfd = aux->abfd;
950 sec = aux->sec;
951 opb = inf->octets_per_byte;
952
953 /* Perform a binary search looking for the closest symbol to the
954 required value. We are searching the range (min, max_count]. */
955 while (min + 1 < max_count)
956 {
957 asymbol *sym;
958
959 thisplace = (max_count + min) / 2;
960 sym = sorted_syms[thisplace];
961
962 if (bfd_asymbol_value (sym) > vma)
963 max_count = thisplace;
964 else if (bfd_asymbol_value (sym) < vma)
965 min = thisplace;
966 else
967 {
968 min = thisplace;
969 break;
970 }
971 }
972
973 /* The symbol we want is now in min, the low end of the range we
974 were searching. If there are several symbols with the same
975 value, we want the first (non-section/non-debugging) one. */
976 thisplace = min;
977 while (thisplace > 0
978 && (bfd_asymbol_value (sorted_syms[thisplace])
979 == bfd_asymbol_value (sorted_syms[thisplace - 1]))
980 && ((sorted_syms[thisplace - 1]->flags
981 & (BSF_SECTION_SYM | BSF_DEBUGGING)) == 0)
982 )
983 --thisplace;
984
985 /* Prefer a symbol in the current section if we have multple symbols
986 with the same value, as can occur with overlays or zero size
987 sections. */
988 min = thisplace;
989 while (min < max_count
990 && (bfd_asymbol_value (sorted_syms[min])
991 == bfd_asymbol_value (sorted_syms[thisplace])))
992 {
993 if (sorted_syms[min]->section == sec
994 && inf->symbol_is_valid (sorted_syms[min], inf))
995 {
996 thisplace = min;
997
998 if (place != NULL)
999 *place = thisplace;
1000
1001 return sorted_syms[thisplace];
1002 }
1003 ++min;
1004 }
1005
1006 /* If the file is relocatable, and the symbol could be from this
1007 section, prefer a symbol from this section over symbols from
1008 others, even if the other symbol's value might be closer.
1009
1010 Note that this may be wrong for some symbol references if the
1011 sections have overlapping memory ranges, but in that case there's
1012 no way to tell what's desired without looking at the relocation
1013 table.
1014
1015 Also give the target a chance to reject symbols. */
1016 want_section = (aux->require_sec
1017 || ((abfd->flags & HAS_RELOC) != 0
1018 && vma >= bfd_get_section_vma (abfd, sec)
1019 && vma < (bfd_get_section_vma (abfd, sec)
1020 + bfd_section_size (abfd, sec) / opb)));
1021 if ((sorted_syms[thisplace]->section != sec && want_section)
1022 || ! inf->symbol_is_valid (sorted_syms[thisplace], inf))
1023 {
1024 long i;
1025 long newplace = sorted_symcount;
1026
1027 for (i = min - 1; i >= 0; i--)
1028 {
1029 if ((sorted_syms[i]->section == sec || !want_section)
1030 && inf->symbol_is_valid (sorted_syms[i], inf))
1031 {
1032 if (newplace == sorted_symcount)
1033 newplace = i;
1034
1035 if (bfd_asymbol_value (sorted_syms[i])
1036 != bfd_asymbol_value (sorted_syms[newplace]))
1037 break;
1038
1039 /* Remember this symbol and keep searching until we reach
1040 an earlier address. */
1041 newplace = i;
1042 }
1043 }
1044
1045 if (newplace != sorted_symcount)
1046 thisplace = newplace;
1047 else
1048 {
1049 /* We didn't find a good symbol with a smaller value.
1050 Look for one with a larger value. */
1051 for (i = thisplace + 1; i < sorted_symcount; i++)
1052 {
1053 if ((sorted_syms[i]->section == sec || !want_section)
1054 && inf->symbol_is_valid (sorted_syms[i], inf))
1055 {
1056 thisplace = i;
1057 break;
1058 }
1059 }
1060 }
1061
1062 if ((sorted_syms[thisplace]->section != sec && want_section)
1063 || ! inf->symbol_is_valid (sorted_syms[thisplace], inf))
1064 /* There is no suitable symbol. */
1065 return NULL;
1066 }
1067
1068 /* If we have not found an exact match for the specified address
1069 and we have dynamic relocations available, then we can produce
1070 a better result by matching a relocation to the address and
1071 using the symbol associated with that relocation. */
1072 rel_count = aux->dynrelcount;
1073 if (!want_section
1074 && sorted_syms[thisplace]->value != vma
1075 && rel_count > 0
1076 && aux->dynrelbuf != NULL
1077 && aux->dynrelbuf[0]->address <= vma
1078 && aux->dynrelbuf[rel_count - 1]->address >= vma
1079 /* If we have matched a synthetic symbol, then stick with that. */
1080 && (sorted_syms[thisplace]->flags & BSF_SYNTHETIC) == 0)
1081 {
1082 arelent ** rel_low;
1083 arelent ** rel_high;
1084
1085 rel_low = aux->dynrelbuf;
1086 rel_high = rel_low + rel_count - 1;
1087 while (rel_low <= rel_high)
1088 {
1089 arelent **rel_mid = &rel_low[(rel_high - rel_low) / 2];
1090 arelent * rel = *rel_mid;
1091
1092 if (rel->address == vma)
1093 {
1094 /* Absolute relocations do not provide a more helpful
1095 symbolic address. Find a non-absolute relocation
1096 with the same address. */
1097 arelent **rel_vma = rel_mid;
1098 for (rel_mid--;
1099 rel_mid >= rel_low && rel_mid[0]->address == vma;
1100 rel_mid--)
1101 rel_vma = rel_mid;
1102
1103 for (; rel_vma <= rel_high && rel_vma[0]->address == vma;
1104 rel_vma++)
1105 {
1106 rel = *rel_vma;
1107 if (rel->sym_ptr_ptr != NULL
1108 && ! bfd_is_abs_section ((* rel->sym_ptr_ptr)->section))
1109 {
1110 if (place != NULL)
1111 * place = thisplace;
1112 return * rel->sym_ptr_ptr;
1113 }
1114 }
1115 break;
1116 }
1117
1118 if (vma < rel->address)
1119 rel_high = rel_mid;
1120 else if (vma >= rel_mid[1]->address)
1121 rel_low = rel_mid + 1;
1122 else
1123 break;
1124 }
1125 }
1126
1127 if (place != NULL)
1128 *place = thisplace;
1129
1130 return sorted_syms[thisplace];
1131 }
1132
1133 /* Print an address and the offset to the nearest symbol. */
1134
1135 static void
1136 objdump_print_addr_with_sym (bfd *abfd, asection *sec, asymbol *sym,
1137 bfd_vma vma, struct disassemble_info *inf,
1138 bfd_boolean skip_zeroes)
1139 {
1140 objdump_print_value (vma, inf, skip_zeroes);
1141
1142 if (sym == NULL)
1143 {
1144 bfd_vma secaddr;
1145
1146 (*inf->fprintf_func) (inf->stream, " <%s",
1147 bfd_get_section_name (abfd, sec));
1148 secaddr = bfd_get_section_vma (abfd, sec);
1149 if (vma < secaddr)
1150 {
1151 (*inf->fprintf_func) (inf->stream, "-0x");
1152 objdump_print_value (secaddr - vma, inf, TRUE);
1153 }
1154 else if (vma > secaddr)
1155 {
1156 (*inf->fprintf_func) (inf->stream, "+0x");
1157 objdump_print_value (vma - secaddr, inf, TRUE);
1158 }
1159 (*inf->fprintf_func) (inf->stream, ">");
1160 }
1161 else
1162 {
1163 (*inf->fprintf_func) (inf->stream, " <");
1164
1165 objdump_print_symname (abfd, inf, sym);
1166
1167 if (bfd_asymbol_value (sym) == vma)
1168 ;
1169 /* Undefined symbols in an executables and dynamic objects do not have
1170 a value associated with them, so it does not make sense to display
1171 an offset relative to them. Normally we would not be provided with
1172 this kind of symbol, but the target backend might choose to do so,
1173 and the code in find_symbol_for_address might return an as yet
1174 unresolved symbol associated with a dynamic reloc. */
1175 else if ((bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC))
1176 && bfd_is_und_section (sym->section))
1177 ;
1178 else if (bfd_asymbol_value (sym) > vma)
1179 {
1180 (*inf->fprintf_func) (inf->stream, "-0x");
1181 objdump_print_value (bfd_asymbol_value (sym) - vma, inf, TRUE);
1182 }
1183 else if (vma > bfd_asymbol_value (sym))
1184 {
1185 (*inf->fprintf_func) (inf->stream, "+0x");
1186 objdump_print_value (vma - bfd_asymbol_value (sym), inf, TRUE);
1187 }
1188
1189 (*inf->fprintf_func) (inf->stream, ">");
1190 }
1191
1192 if (display_file_offsets)
1193 inf->fprintf_func (inf->stream, _(" (File Offset: 0x%lx)"),
1194 (long int)(sec->filepos + (vma - sec->vma)));
1195 }
1196
1197 /* Print an address (VMA), symbolically if possible.
1198 If SKIP_ZEROES is TRUE, don't output leading zeroes. */
1199
1200 static void
1201 objdump_print_addr (bfd_vma vma,
1202 struct disassemble_info *inf,
1203 bfd_boolean skip_zeroes)
1204 {
1205 struct objdump_disasm_info *aux;
1206 asymbol *sym = NULL;
1207 bfd_boolean skip_find = FALSE;
1208
1209 aux = (struct objdump_disasm_info *) inf->application_data;
1210
1211 if (sorted_symcount < 1)
1212 {
1213 (*inf->fprintf_func) (inf->stream, "0x");
1214 objdump_print_value (vma, inf, skip_zeroes);
1215
1216 if (display_file_offsets)
1217 inf->fprintf_func (inf->stream, _(" (File Offset: 0x%lx)"),
1218 (long int)(aux->sec->filepos + (vma - aux->sec->vma)));
1219 return;
1220 }
1221
1222 if (aux->reloc != NULL
1223 && aux->reloc->sym_ptr_ptr != NULL
1224 && * aux->reloc->sym_ptr_ptr != NULL)
1225 {
1226 sym = * aux->reloc->sym_ptr_ptr;
1227
1228 /* Adjust the vma to the reloc. */
1229 vma += bfd_asymbol_value (sym);
1230
1231 if (bfd_is_und_section (bfd_get_section (sym)))
1232 skip_find = TRUE;
1233 }
1234
1235 if (!skip_find)
1236 sym = find_symbol_for_address (vma, inf, NULL);
1237
1238 objdump_print_addr_with_sym (aux->abfd, aux->sec, sym, vma, inf,
1239 skip_zeroes);
1240 }
1241
1242 /* Print VMA to INFO. This function is passed to the disassembler
1243 routine. */
1244
1245 static void
1246 objdump_print_address (bfd_vma vma, struct disassemble_info *inf)
1247 {
1248 objdump_print_addr (vma, inf, ! prefix_addresses);
1249 }
1250
1251 /* Determine if the given address has a symbol associated with it. */
1252
1253 static int
1254 objdump_symbol_at_address (bfd_vma vma, struct disassemble_info * inf)
1255 {
1256 asymbol * sym;
1257
1258 sym = find_symbol_for_address (vma, inf, NULL);
1259
1260 return (sym != NULL && (bfd_asymbol_value (sym) == vma));
1261 }
1262
1263 /* Hold the last function name and the last line number we displayed
1264 in a disassembly. */
1265
1266 static char *prev_functionname;
1267 static unsigned int prev_line;
1268 static unsigned int prev_discriminator;
1269
1270 /* We keep a list of all files that we have seen when doing a
1271 disassembly with source, so that we know how much of the file to
1272 display. This can be important for inlined functions. */
1273
1274 struct print_file_list
1275 {
1276 struct print_file_list *next;
1277 const char *filename;
1278 const char *modname;
1279 const char *map;
1280 size_t mapsize;
1281 const char **linemap;
1282 unsigned maxline;
1283 unsigned last_line;
1284 unsigned max_printed;
1285 int first;
1286 };
1287
1288 static struct print_file_list *print_files;
1289
1290 /* The number of preceding context lines to show when we start
1291 displaying a file for the first time. */
1292
1293 #define SHOW_PRECEDING_CONTEXT_LINES (5)
1294
1295 /* Read a complete file into memory. */
1296
1297 static const char *
1298 slurp_file (const char *fn, size_t *size, struct stat *fst)
1299 {
1300 #ifdef HAVE_MMAP
1301 int ps = getpagesize ();
1302 size_t msize;
1303 #endif
1304 const char *map;
1305 int fd = open (fn, O_RDONLY | O_BINARY);
1306
1307 if (fd < 0)
1308 return NULL;
1309 if (fstat (fd, fst) < 0)
1310 {
1311 close (fd);
1312 return NULL;
1313 }
1314 *size = fst->st_size;
1315 #ifdef HAVE_MMAP
1316 msize = (*size + ps - 1) & ~(ps - 1);
1317 map = mmap (NULL, msize, PROT_READ, MAP_SHARED, fd, 0);
1318 if (map != (char *) -1L)
1319 {
1320 close (fd);
1321 return map;
1322 }
1323 #endif
1324 map = (const char *) malloc (*size);
1325 if (!map || (size_t) read (fd, (char *) map, *size) != *size)
1326 {
1327 free ((void *) map);
1328 map = NULL;
1329 }
1330 close (fd);
1331 return map;
1332 }
1333
1334 #define line_map_decrease 5
1335
1336 /* Precompute array of lines for a mapped file. */
1337
1338 static const char **
1339 index_file (const char *map, size_t size, unsigned int *maxline)
1340 {
1341 const char *p, *lstart, *end;
1342 int chars_per_line = 45; /* First iteration will use 40. */
1343 unsigned int lineno;
1344 const char **linemap = NULL;
1345 unsigned long line_map_size = 0;
1346
1347 lineno = 0;
1348 lstart = map;
1349 end = map + size;
1350
1351 for (p = map; p < end; p++)
1352 {
1353 if (*p == '\n')
1354 {
1355 if (p + 1 < end && p[1] == '\r')
1356 p++;
1357 }
1358 else if (*p == '\r')
1359 {
1360 if (p + 1 < end && p[1] == '\n')
1361 p++;
1362 }
1363 else
1364 continue;
1365
1366 /* End of line found. */
1367
1368 if (linemap == NULL || line_map_size < lineno + 1)
1369 {
1370 unsigned long newsize;
1371
1372 chars_per_line -= line_map_decrease;
1373 if (chars_per_line <= 1)
1374 chars_per_line = 1;
1375 line_map_size = size / chars_per_line + 1;
1376 if (line_map_size < lineno + 1)
1377 line_map_size = lineno + 1;
1378 newsize = line_map_size * sizeof (char *);
1379 linemap = (const char **) xrealloc (linemap, newsize);
1380 }
1381
1382 linemap[lineno++] = lstart;
1383 lstart = p + 1;
1384 }
1385
1386 *maxline = lineno;
1387 return linemap;
1388 }
1389
1390 /* Tries to open MODNAME, and if successful adds a node to print_files
1391 linked list and returns that node. Returns NULL on failure. */
1392
1393 static struct print_file_list *
1394 try_print_file_open (const char *origname, const char *modname, struct stat *fst)
1395 {
1396 struct print_file_list *p;
1397
1398 p = (struct print_file_list *) xmalloc (sizeof (struct print_file_list));
1399
1400 p->map = slurp_file (modname, &p->mapsize, fst);
1401 if (p->map == NULL)
1402 {
1403 free (p);
1404 return NULL;
1405 }
1406
1407 p->linemap = index_file (p->map, p->mapsize, &p->maxline);
1408 p->last_line = 0;
1409 p->max_printed = 0;
1410 p->filename = origname;
1411 p->modname = modname;
1412 p->next = print_files;
1413 p->first = 1;
1414 print_files = p;
1415 return p;
1416 }
1417
1418 /* If the source file, as described in the symtab, is not found
1419 try to locate it in one of the paths specified with -I
1420 If found, add location to print_files linked list. */
1421
1422 static struct print_file_list *
1423 update_source_path (const char *filename, bfd *abfd)
1424 {
1425 struct print_file_list *p;
1426 const char *fname;
1427 struct stat fst;
1428 int i;
1429
1430 p = try_print_file_open (filename, filename, &fst);
1431 if (p == NULL)
1432 {
1433 if (include_path_count == 0)
1434 return NULL;
1435
1436 /* Get the name of the file. */
1437 fname = lbasename (filename);
1438
1439 /* If file exists under a new path, we need to add it to the list
1440 so that show_line knows about it. */
1441 for (i = 0; i < include_path_count; i++)
1442 {
1443 char *modname = concat (include_paths[i], "/", fname,
1444 (const char *) 0);
1445
1446 p = try_print_file_open (filename, modname, &fst);
1447 if (p)
1448 break;
1449
1450 free (modname);
1451 }
1452 }
1453
1454 if (p != NULL)
1455 {
1456 long mtime = bfd_get_mtime (abfd);
1457
1458 if (fst.st_mtime > mtime)
1459 warn (_("source file %s is more recent than object file\n"),
1460 filename);
1461 }
1462
1463 return p;
1464 }
1465
1466 /* Print a source file line. */
1467
1468 static void
1469 print_line (struct print_file_list *p, unsigned int linenum)
1470 {
1471 const char *l;
1472 size_t len;
1473
1474 --linenum;
1475 if (linenum >= p->maxline)
1476 return;
1477 l = p->linemap [linenum];
1478 /* Test fwrite return value to quiet glibc warning. */
1479 len = strcspn (l, "\n\r");
1480 if (len == 0 || fwrite (l, len, 1, stdout) == 1)
1481 putchar ('\n');
1482 }
1483
1484 /* Print a range of source code lines. */
1485
1486 static void
1487 dump_lines (struct print_file_list *p, unsigned int start, unsigned int end)
1488 {
1489 if (p->map == NULL)
1490 return;
1491 while (start <= end)
1492 {
1493 print_line (p, start);
1494 start++;
1495 }
1496 }
1497
1498 /* Show the line number, or the source line, in a disassembly
1499 listing. */
1500
1501 static void
1502 show_line (bfd *abfd, asection *section, bfd_vma addr_offset)
1503 {
1504 const char *filename;
1505 const char *functionname;
1506 unsigned int linenumber;
1507 unsigned int discriminator;
1508 bfd_boolean reloc;
1509 char *path = NULL;
1510
1511 if (! with_line_numbers && ! with_source_code)
1512 return;
1513
1514 if (! bfd_find_nearest_line_discriminator (abfd, section, syms, addr_offset,
1515 &filename, &functionname,
1516 &linenumber, &discriminator))
1517 return;
1518
1519 if (filename != NULL && *filename == '\0')
1520 filename = NULL;
1521 if (functionname != NULL && *functionname == '\0')
1522 functionname = NULL;
1523
1524 if (filename
1525 && IS_ABSOLUTE_PATH (filename)
1526 && prefix)
1527 {
1528 char *path_up;
1529 const char *fname = filename;
1530
1531 path = xmalloc (prefix_length + PATH_MAX + 1);
1532
1533 if (prefix_length)
1534 memcpy (path, prefix, prefix_length);
1535 path_up = path + prefix_length;
1536
1537 /* Build relocated filename, stripping off leading directories
1538 from the initial filename if requested. */
1539 if (prefix_strip > 0)
1540 {
1541 int level = 0;
1542 const char *s;
1543
1544 /* Skip selected directory levels. */
1545 for (s = fname + 1; *s != '\0' && level < prefix_strip; s++)
1546 if (IS_DIR_SEPARATOR(*s))
1547 {
1548 fname = s;
1549 level++;
1550 }
1551 }
1552
1553 /* Update complete filename. */
1554 strncpy (path_up, fname, PATH_MAX);
1555 path_up[PATH_MAX] = '\0';
1556
1557 filename = path;
1558 reloc = TRUE;
1559 }
1560 else
1561 reloc = FALSE;
1562
1563 if (with_line_numbers)
1564 {
1565 if (functionname != NULL
1566 && (prev_functionname == NULL
1567 || strcmp (functionname, prev_functionname) != 0))
1568 {
1569 printf ("%s():\n", functionname);
1570 prev_line = -1;
1571 }
1572 if (linenumber > 0
1573 && (linenumber != prev_line
1574 || discriminator != prev_discriminator))
1575 {
1576 if (discriminator > 0)
1577 printf ("%s:%u (discriminator %u)\n",
1578 filename == NULL ? "???" : filename,
1579 linenumber, discriminator);
1580 else
1581 printf ("%s:%u\n", filename == NULL ? "???" : filename,
1582 linenumber);
1583 }
1584 if (unwind_inlines)
1585 {
1586 const char *filename2;
1587 const char *functionname2;
1588 unsigned line2;
1589
1590 while (bfd_find_inliner_info (abfd, &filename2, &functionname2,
1591 &line2))
1592 printf ("inlined by %s:%u (%s)\n", filename2, line2,
1593 functionname2);
1594 }
1595 }
1596
1597 if (with_source_code
1598 && filename != NULL
1599 && linenumber > 0)
1600 {
1601 struct print_file_list **pp, *p;
1602 unsigned l;
1603
1604 for (pp = &print_files; *pp != NULL; pp = &(*pp)->next)
1605 if (filename_cmp ((*pp)->filename, filename) == 0)
1606 break;
1607 p = *pp;
1608
1609 if (p == NULL)
1610 {
1611 if (reloc)
1612 filename = xstrdup (filename);
1613 p = update_source_path (filename, abfd);
1614 }
1615
1616 if (p != NULL && linenumber != p->last_line)
1617 {
1618 if (file_start_context && p->first)
1619 l = 1;
1620 else
1621 {
1622 l = linenumber - SHOW_PRECEDING_CONTEXT_LINES;
1623 if (l >= linenumber)
1624 l = 1;
1625 if (p->max_printed >= l)
1626 {
1627 if (p->max_printed < linenumber)
1628 l = p->max_printed + 1;
1629 else
1630 l = linenumber;
1631 }
1632 }
1633 dump_lines (p, l, linenumber);
1634 if (p->max_printed < linenumber)
1635 p->max_printed = linenumber;
1636 p->last_line = linenumber;
1637 p->first = 0;
1638 }
1639 }
1640
1641 if (functionname != NULL
1642 && (prev_functionname == NULL
1643 || strcmp (functionname, prev_functionname) != 0))
1644 {
1645 if (prev_functionname != NULL)
1646 free (prev_functionname);
1647 prev_functionname = (char *) xmalloc (strlen (functionname) + 1);
1648 strcpy (prev_functionname, functionname);
1649 }
1650
1651 if (linenumber > 0 && linenumber != prev_line)
1652 prev_line = linenumber;
1653
1654 if (discriminator != prev_discriminator)
1655 prev_discriminator = discriminator;
1656
1657 if (path)
1658 free (path);
1659 }
1660
1661 /* Pseudo FILE object for strings. */
1662 typedef struct
1663 {
1664 char *buffer;
1665 size_t pos;
1666 size_t alloc;
1667 } SFILE;
1668
1669 /* sprintf to a "stream". */
1670
1671 static int ATTRIBUTE_PRINTF_2
1672 objdump_sprintf (SFILE *f, const char *format, ...)
1673 {
1674 size_t n;
1675 va_list args;
1676
1677 while (1)
1678 {
1679 size_t space = f->alloc - f->pos;
1680
1681 va_start (args, format);
1682 n = vsnprintf (f->buffer + f->pos, space, format, args);
1683 va_end (args);
1684
1685 if (space > n)
1686 break;
1687
1688 f->alloc = (f->alloc + n) * 2;
1689 f->buffer = (char *) xrealloc (f->buffer, f->alloc);
1690 }
1691 f->pos += n;
1692
1693 return n;
1694 }
1695
1696 /* The number of zeroes we want to see before we start skipping them.
1697 The number is arbitrarily chosen. */
1698
1699 #define DEFAULT_SKIP_ZEROES 8
1700
1701 /* The number of zeroes to skip at the end of a section. If the
1702 number of zeroes at the end is between SKIP_ZEROES_AT_END and
1703 SKIP_ZEROES, they will be disassembled. If there are fewer than
1704 SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic
1705 attempt to avoid disassembling zeroes inserted by section
1706 alignment. */
1707
1708 #define DEFAULT_SKIP_ZEROES_AT_END 3
1709
1710 /* Disassemble some data in memory between given values. */
1711
1712 static void
1713 disassemble_bytes (struct disassemble_info * inf,
1714 disassembler_ftype disassemble_fn,
1715 bfd_boolean insns,
1716 bfd_byte * data,
1717 bfd_vma start_offset,
1718 bfd_vma stop_offset,
1719 bfd_vma rel_offset,
1720 arelent *** relppp,
1721 arelent ** relppend)
1722 {
1723 struct objdump_disasm_info *aux;
1724 asection *section;
1725 int octets_per_line;
1726 int skip_addr_chars;
1727 bfd_vma addr_offset;
1728 unsigned int opb = inf->octets_per_byte;
1729 unsigned int skip_zeroes = inf->skip_zeroes;
1730 unsigned int skip_zeroes_at_end = inf->skip_zeroes_at_end;
1731 int octets = opb;
1732 SFILE sfile;
1733
1734 aux = (struct objdump_disasm_info *) inf->application_data;
1735 section = aux->sec;
1736
1737 sfile.alloc = 120;
1738 sfile.buffer = (char *) xmalloc (sfile.alloc);
1739 sfile.pos = 0;
1740
1741 if (insn_width)
1742 octets_per_line = insn_width;
1743 else if (insns)
1744 octets_per_line = 4;
1745 else
1746 octets_per_line = 16;
1747
1748 /* Figure out how many characters to skip at the start of an
1749 address, to make the disassembly look nicer. We discard leading
1750 zeroes in chunks of 4, ensuring that there is always a leading
1751 zero remaining. */
1752 skip_addr_chars = 0;
1753 if (! prefix_addresses)
1754 {
1755 char buf[30];
1756
1757 bfd_sprintf_vma (aux->abfd, buf, section->vma + section->size / opb);
1758
1759 while (buf[skip_addr_chars] == '0')
1760 ++skip_addr_chars;
1761
1762 /* Don't discard zeros on overflow. */
1763 if (buf[skip_addr_chars] == '\0' && section->vma != 0)
1764 skip_addr_chars = 0;
1765
1766 if (skip_addr_chars != 0)
1767 skip_addr_chars = (skip_addr_chars - 1) & -4;
1768 }
1769
1770 inf->insn_info_valid = 0;
1771
1772 addr_offset = start_offset;
1773 while (addr_offset < stop_offset)
1774 {
1775 bfd_vma z;
1776 bfd_boolean need_nl = FALSE;
1777 int previous_octets;
1778
1779 /* Remember the length of the previous instruction. */
1780 previous_octets = octets;
1781 octets = 0;
1782
1783 /* Make sure we don't use relocs from previous instructions. */
1784 aux->reloc = NULL;
1785
1786 /* If we see more than SKIP_ZEROES octets of zeroes, we just
1787 print `...'. */
1788 for (z = addr_offset * opb; z < stop_offset * opb; z++)
1789 if (data[z] != 0)
1790 break;
1791 if (! disassemble_zeroes
1792 && (inf->insn_info_valid == 0
1793 || inf->branch_delay_insns == 0)
1794 && (z - addr_offset * opb >= skip_zeroes
1795 || (z == stop_offset * opb &&
1796 z - addr_offset * opb < skip_zeroes_at_end)))
1797 {
1798 /* If there are more nonzero octets to follow, we only skip
1799 zeroes in multiples of 4, to try to avoid running over
1800 the start of an instruction which happens to start with
1801 zero. */
1802 if (z != stop_offset * opb)
1803 z = addr_offset * opb + ((z - addr_offset * opb) &~ 3);
1804
1805 octets = z - addr_offset * opb;
1806
1807 /* If we are going to display more data, and we are displaying
1808 file offsets, then tell the user how many zeroes we skip
1809 and the file offset from where we resume dumping. */
1810 if (display_file_offsets && ((addr_offset + (octets / opb)) < stop_offset))
1811 printf ("\t... (skipping %d zeroes, resuming at file offset: 0x%lx)\n",
1812 octets / opb,
1813 (unsigned long) (section->filepos
1814 + (addr_offset + (octets / opb))));
1815 else
1816 printf ("\t...\n");
1817 }
1818 else
1819 {
1820 char buf[50];
1821 int bpc = 0;
1822 int pb = 0;
1823
1824 if (with_line_numbers || with_source_code)
1825 show_line (aux->abfd, section, addr_offset);
1826
1827 if (! prefix_addresses)
1828 {
1829 char *s;
1830
1831 bfd_sprintf_vma (aux->abfd, buf, section->vma + addr_offset);
1832 for (s = buf + skip_addr_chars; *s == '0'; s++)
1833 *s = ' ';
1834 if (*s == '\0')
1835 *--s = '0';
1836 printf ("%s:\t", buf + skip_addr_chars);
1837 }
1838 else
1839 {
1840 aux->require_sec = TRUE;
1841 objdump_print_address (section->vma + addr_offset, inf);
1842 aux->require_sec = FALSE;
1843 putchar (' ');
1844 }
1845
1846 if (insns)
1847 {
1848 sfile.pos = 0;
1849 inf->fprintf_func = (fprintf_ftype) objdump_sprintf;
1850 inf->stream = &sfile;
1851 inf->bytes_per_line = 0;
1852 inf->bytes_per_chunk = 0;
1853 inf->flags = disassemble_all ? DISASSEMBLE_DATA : 0;
1854 if (machine)
1855 inf->flags |= USER_SPECIFIED_MACHINE_TYPE;
1856
1857 if (inf->disassembler_needs_relocs
1858 && (bfd_get_file_flags (aux->abfd) & EXEC_P) == 0
1859 && (bfd_get_file_flags (aux->abfd) & DYNAMIC) == 0
1860 && *relppp < relppend)
1861 {
1862 bfd_signed_vma distance_to_rel;
1863
1864 distance_to_rel = (**relppp)->address
1865 - (rel_offset + addr_offset);
1866
1867 /* Check to see if the current reloc is associated with
1868 the instruction that we are about to disassemble. */
1869 if (distance_to_rel == 0
1870 /* FIXME: This is wrong. We are trying to catch
1871 relocs that are addressed part way through the
1872 current instruction, as might happen with a packed
1873 VLIW instruction. Unfortunately we do not know the
1874 length of the current instruction since we have not
1875 disassembled it yet. Instead we take a guess based
1876 upon the length of the previous instruction. The
1877 proper solution is to have a new target-specific
1878 disassembler function which just returns the length
1879 of an instruction at a given address without trying
1880 to display its disassembly. */
1881 || (distance_to_rel > 0
1882 && distance_to_rel < (bfd_signed_vma) (previous_octets/ opb)))
1883 {
1884 inf->flags |= INSN_HAS_RELOC;
1885 aux->reloc = **relppp;
1886 }
1887 }
1888
1889 if (! disassemble_all
1890 && (section->flags & (SEC_CODE | SEC_HAS_CONTENTS))
1891 == (SEC_CODE | SEC_HAS_CONTENTS))
1892 /* Set a stop_vma so that the disassembler will not read
1893 beyond the next symbol. We assume that symbols appear on
1894 the boundaries between instructions. We only do this when
1895 disassembling code of course, and when -D is in effect. */
1896 inf->stop_vma = section->vma + stop_offset;
1897
1898 octets = (*disassemble_fn) (section->vma + addr_offset, inf);
1899
1900 inf->stop_vma = 0;
1901 inf->fprintf_func = (fprintf_ftype) fprintf;
1902 inf->stream = stdout;
1903 if (insn_width == 0 && inf->bytes_per_line != 0)
1904 octets_per_line = inf->bytes_per_line;
1905 if (octets < (int) opb)
1906 {
1907 if (sfile.pos)
1908 printf ("%s\n", sfile.buffer);
1909 if (octets >= 0)
1910 {
1911 non_fatal (_("disassemble_fn returned length %d"),
1912 octets);
1913 exit_status = 1;
1914 }
1915 break;
1916 }
1917 }
1918 else
1919 {
1920 bfd_vma j;
1921
1922 octets = octets_per_line;
1923 if (addr_offset + octets / opb > stop_offset)
1924 octets = (stop_offset - addr_offset) * opb;
1925
1926 for (j = addr_offset * opb; j < addr_offset * opb + octets; ++j)
1927 {
1928 if (ISPRINT (data[j]))
1929 buf[j - addr_offset * opb] = data[j];
1930 else
1931 buf[j - addr_offset * opb] = '.';
1932 }
1933 buf[j - addr_offset * opb] = '\0';
1934 }
1935
1936 if (prefix_addresses
1937 ? show_raw_insn > 0
1938 : show_raw_insn >= 0)
1939 {
1940 bfd_vma j;
1941
1942 /* If ! prefix_addresses and ! wide_output, we print
1943 octets_per_line octets per line. */
1944 pb = octets;
1945 if (pb > octets_per_line && ! prefix_addresses && ! wide_output)
1946 pb = octets_per_line;
1947
1948 if (inf->bytes_per_chunk)
1949 bpc = inf->bytes_per_chunk;
1950 else
1951 bpc = 1;
1952
1953 for (j = addr_offset * opb; j < addr_offset * opb + pb; j += bpc)
1954 {
1955 /* PR 21580: Check for a buffer ending early. */
1956 if (j + bpc <= stop_offset * opb)
1957 {
1958 int k;
1959
1960 if (inf->display_endian == BFD_ENDIAN_LITTLE)
1961 {
1962 for (k = bpc - 1; k >= 0; k--)
1963 printf ("%02x", (unsigned) data[j + k]);
1964 }
1965 else
1966 {
1967 for (k = 0; k < bpc; k++)
1968 printf ("%02x", (unsigned) data[j + k]);
1969 }
1970 }
1971 putchar (' ');
1972 }
1973
1974 for (; pb < octets_per_line; pb += bpc)
1975 {
1976 int k;
1977
1978 for (k = 0; k < bpc; k++)
1979 printf (" ");
1980 putchar (' ');
1981 }
1982
1983 /* Separate raw data from instruction by extra space. */
1984 if (insns)
1985 putchar ('\t');
1986 else
1987 printf (" ");
1988 }
1989
1990 if (! insns)
1991 printf ("%s", buf);
1992 else if (sfile.pos)
1993 printf ("%s", sfile.buffer);
1994
1995 if (prefix_addresses
1996 ? show_raw_insn > 0
1997 : show_raw_insn >= 0)
1998 {
1999 while (pb < octets)
2000 {
2001 bfd_vma j;
2002 char *s;
2003
2004 putchar ('\n');
2005 j = addr_offset * opb + pb;
2006
2007 bfd_sprintf_vma (aux->abfd, buf, section->vma + j / opb);
2008 for (s = buf + skip_addr_chars; *s == '0'; s++)
2009 *s = ' ';
2010 if (*s == '\0')
2011 *--s = '0';
2012 printf ("%s:\t", buf + skip_addr_chars);
2013
2014 pb += octets_per_line;
2015 if (pb > octets)
2016 pb = octets;
2017 for (; j < addr_offset * opb + pb; j += bpc)
2018 {
2019 /* PR 21619: Check for a buffer ending early. */
2020 if (j + bpc <= stop_offset * opb)
2021 {
2022 int k;
2023
2024 if (inf->display_endian == BFD_ENDIAN_LITTLE)
2025 {
2026 for (k = bpc - 1; k >= 0; k--)
2027 printf ("%02x", (unsigned) data[j + k]);
2028 }
2029 else
2030 {
2031 for (k = 0; k < bpc; k++)
2032 printf ("%02x", (unsigned) data[j + k]);
2033 }
2034 }
2035 putchar (' ');
2036 }
2037 }
2038 }
2039
2040 if (!wide_output)
2041 putchar ('\n');
2042 else
2043 need_nl = TRUE;
2044 }
2045
2046 while ((*relppp) < relppend
2047 && (**relppp)->address < rel_offset + addr_offset + octets / opb)
2048 {
2049 if (dump_reloc_info || dump_dynamic_reloc_info)
2050 {
2051 arelent *q;
2052
2053 q = **relppp;
2054
2055 if (wide_output)
2056 putchar ('\t');
2057 else
2058 printf ("\t\t\t");
2059
2060 objdump_print_value (section->vma - rel_offset + q->address,
2061 inf, TRUE);
2062
2063 if (q->howto == NULL)
2064 printf (": *unknown*\t");
2065 else if (q->howto->name)
2066 printf (": %s\t", q->howto->name);
2067 else
2068 printf (": %d\t", q->howto->type);
2069
2070 if (q->sym_ptr_ptr == NULL || *q->sym_ptr_ptr == NULL)
2071 printf ("*unknown*");
2072 else
2073 {
2074 const char *sym_name;
2075
2076 sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
2077 if (sym_name != NULL && *sym_name != '\0')
2078 objdump_print_symname (aux->abfd, inf, *q->sym_ptr_ptr);
2079 else
2080 {
2081 asection *sym_sec;
2082
2083 sym_sec = bfd_get_section (*q->sym_ptr_ptr);
2084 sym_name = bfd_get_section_name (aux->abfd, sym_sec);
2085 if (sym_name == NULL || *sym_name == '\0')
2086 sym_name = "*unknown*";
2087 printf ("%s", sym_name);
2088 }
2089 }
2090
2091 if (q->addend)
2092 {
2093 bfd_signed_vma addend = q->addend;
2094 if (addend < 0)
2095 {
2096 printf ("-0x");
2097 addend = -addend;
2098 }
2099 else
2100 printf ("+0x");
2101 objdump_print_value (addend, inf, TRUE);
2102 }
2103
2104 printf ("\n");
2105 need_nl = FALSE;
2106 }
2107 ++(*relppp);
2108 }
2109
2110 if (need_nl)
2111 printf ("\n");
2112
2113 addr_offset += octets / opb;
2114 }
2115
2116 free (sfile.buffer);
2117 }
2118
2119 static void
2120 disassemble_section (bfd *abfd, asection *section, void *inf)
2121 {
2122 const struct elf_backend_data * bed;
2123 bfd_vma sign_adjust = 0;
2124 struct disassemble_info * pinfo = (struct disassemble_info *) inf;
2125 struct objdump_disasm_info * paux;
2126 unsigned int opb = pinfo->octets_per_byte;
2127 bfd_byte * data = NULL;
2128 bfd_size_type datasize = 0;
2129 arelent ** rel_pp = NULL;
2130 arelent ** rel_ppstart = NULL;
2131 arelent ** rel_ppend;
2132 bfd_vma stop_offset;
2133 asymbol * sym = NULL;
2134 long place = 0;
2135 long rel_count;
2136 bfd_vma rel_offset;
2137 unsigned long addr_offset;
2138
2139 /* Sections that do not contain machine
2140 code are not normally disassembled. */
2141 if (! disassemble_all
2142 && only_list == NULL
2143 && ((section->flags & (SEC_CODE | SEC_HAS_CONTENTS))
2144 != (SEC_CODE | SEC_HAS_CONTENTS)))
2145 return;
2146
2147 if (! process_section_p (section))
2148 return;
2149
2150 datasize = bfd_get_section_size (section);
2151 if (datasize == 0)
2152 return;
2153
2154 if (start_address == (bfd_vma) -1
2155 || start_address < section->vma)
2156 addr_offset = 0;
2157 else
2158 addr_offset = start_address - section->vma;
2159
2160 if (stop_address == (bfd_vma) -1)
2161 stop_offset = datasize / opb;
2162 else
2163 {
2164 if (stop_address < section->vma)
2165 stop_offset = 0;
2166 else
2167 stop_offset = stop_address - section->vma;
2168 if (stop_offset > datasize / opb)
2169 stop_offset = datasize / opb;
2170 }
2171
2172 if (addr_offset >= stop_offset)
2173 return;
2174
2175 /* Decide which set of relocs to use. Load them if necessary. */
2176 paux = (struct objdump_disasm_info *) pinfo->application_data;
2177 if (paux->dynrelbuf && dump_dynamic_reloc_info)
2178 {
2179 rel_pp = paux->dynrelbuf;
2180 rel_count = paux->dynrelcount;
2181 /* Dynamic reloc addresses are absolute, non-dynamic are section
2182 relative. REL_OFFSET specifies the reloc address corresponding
2183 to the start of this section. */
2184 rel_offset = section->vma;
2185 }
2186 else
2187 {
2188 rel_count = 0;
2189 rel_pp = NULL;
2190 rel_offset = 0;
2191
2192 if ((section->flags & SEC_RELOC) != 0
2193 && (dump_reloc_info || pinfo->disassembler_needs_relocs))
2194 {
2195 long relsize;
2196
2197 relsize = bfd_get_reloc_upper_bound (abfd, section);
2198 if (relsize < 0)
2199 bfd_fatal (bfd_get_filename (abfd));
2200
2201 if (relsize > 0)
2202 {
2203 rel_ppstart = rel_pp = (arelent **) xmalloc (relsize);
2204 rel_count = bfd_canonicalize_reloc (abfd, section, rel_pp, syms);
2205 if (rel_count < 0)
2206 bfd_fatal (bfd_get_filename (abfd));
2207
2208 /* Sort the relocs by address. */
2209 qsort (rel_pp, rel_count, sizeof (arelent *), compare_relocs);
2210 }
2211 }
2212 }
2213 rel_ppend = rel_pp + rel_count;
2214
2215 if (!bfd_malloc_and_get_section (abfd, section, &data))
2216 {
2217 non_fatal (_("Reading section %s failed because: %s"),
2218 section->name, bfd_errmsg (bfd_get_error ()));
2219 return;
2220 }
2221
2222 paux->sec = section;
2223 pinfo->buffer = data;
2224 pinfo->buffer_vma = section->vma;
2225 pinfo->buffer_length = datasize;
2226 pinfo->section = section;
2227
2228 /* Skip over the relocs belonging to addresses below the
2229 start address. */
2230 while (rel_pp < rel_ppend
2231 && (*rel_pp)->address < rel_offset + addr_offset)
2232 ++rel_pp;
2233
2234 printf (_("\nDisassembly of section %s:\n"), section->name);
2235
2236 /* Find the nearest symbol forwards from our current position. */
2237 paux->require_sec = TRUE;
2238 sym = (asymbol *) find_symbol_for_address (section->vma + addr_offset,
2239 (struct disassemble_info *) inf,
2240 &place);
2241 paux->require_sec = FALSE;
2242
2243 /* PR 9774: If the target used signed addresses then we must make
2244 sure that we sign extend the value that we calculate for 'addr'
2245 in the loop below. */
2246 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
2247 && (bed = get_elf_backend_data (abfd)) != NULL
2248 && bed->sign_extend_vma)
2249 sign_adjust = (bfd_vma) 1 << (bed->s->arch_size - 1);
2250
2251 /* Disassemble a block of instructions up to the address associated with
2252 the symbol we have just found. Then print the symbol and find the
2253 next symbol on. Repeat until we have disassembled the entire section
2254 or we have reached the end of the address range we are interested in. */
2255 while (addr_offset < stop_offset)
2256 {
2257 bfd_vma addr;
2258 asymbol *nextsym;
2259 bfd_vma nextstop_offset;
2260 bfd_boolean insns;
2261
2262 addr = section->vma + addr_offset;
2263 addr = ((addr & ((sign_adjust << 1) - 1)) ^ sign_adjust) - sign_adjust;
2264
2265 if (sym != NULL && bfd_asymbol_value (sym) <= addr)
2266 {
2267 int x;
2268
2269 for (x = place;
2270 (x < sorted_symcount
2271 && (bfd_asymbol_value (sorted_syms[x]) <= addr));
2272 ++x)
2273 continue;
2274
2275 pinfo->symbols = sorted_syms + place;
2276 pinfo->num_symbols = x - place;
2277 pinfo->symtab_pos = place;
2278 }
2279 else
2280 {
2281 pinfo->symbols = NULL;
2282 pinfo->num_symbols = 0;
2283 pinfo->symtab_pos = -1;
2284 }
2285
2286 if (! prefix_addresses)
2287 {
2288 pinfo->fprintf_func (pinfo->stream, "\n");
2289 objdump_print_addr_with_sym (abfd, section, sym, addr,
2290 pinfo, FALSE);
2291 pinfo->fprintf_func (pinfo->stream, ":\n");
2292 }
2293
2294 if (sym != NULL && bfd_asymbol_value (sym) > addr)
2295 nextsym = sym;
2296 else if (sym == NULL)
2297 nextsym = NULL;
2298 else
2299 {
2300 #define is_valid_next_sym(SYM) \
2301 ((SYM)->section == section \
2302 && (bfd_asymbol_value (SYM) > bfd_asymbol_value (sym)) \
2303 && pinfo->symbol_is_valid (SYM, pinfo))
2304
2305 /* Search forward for the next appropriate symbol in
2306 SECTION. Note that all the symbols are sorted
2307 together into one big array, and that some sections
2308 may have overlapping addresses. */
2309 while (place < sorted_symcount
2310 && ! is_valid_next_sym (sorted_syms [place]))
2311 ++place;
2312
2313 if (place >= sorted_symcount)
2314 nextsym = NULL;
2315 else
2316 nextsym = sorted_syms[place];
2317 }
2318
2319 if (sym != NULL && bfd_asymbol_value (sym) > addr)
2320 nextstop_offset = bfd_asymbol_value (sym) - section->vma;
2321 else if (nextsym == NULL)
2322 nextstop_offset = stop_offset;
2323 else
2324 nextstop_offset = bfd_asymbol_value (nextsym) - section->vma;
2325
2326 if (nextstop_offset > stop_offset
2327 || nextstop_offset <= addr_offset)
2328 nextstop_offset = stop_offset;
2329
2330 /* If a symbol is explicitly marked as being an object
2331 rather than a function, just dump the bytes without
2332 disassembling them. */
2333 if (disassemble_all
2334 || sym == NULL
2335 || sym->section != section
2336 || bfd_asymbol_value (sym) > addr
2337 || ((sym->flags & BSF_OBJECT) == 0
2338 && (strstr (bfd_asymbol_name (sym), "gnu_compiled")
2339 == NULL)
2340 && (strstr (bfd_asymbol_name (sym), "gcc2_compiled")
2341 == NULL))
2342 || (sym->flags & BSF_FUNCTION) != 0)
2343 insns = TRUE;
2344 else
2345 insns = FALSE;
2346
2347 disassemble_bytes (pinfo, paux->disassemble_fn, insns, data,
2348 addr_offset, nextstop_offset,
2349 rel_offset, &rel_pp, rel_ppend);
2350
2351 addr_offset = nextstop_offset;
2352 sym = nextsym;
2353 }
2354
2355 free (data);
2356
2357 if (rel_ppstart != NULL)
2358 free (rel_ppstart);
2359 }
2360
2361 /* Disassemble the contents of an object file. */
2362
2363 static void
2364 disassemble_data (bfd *abfd)
2365 {
2366 struct disassemble_info disasm_info;
2367 struct objdump_disasm_info aux;
2368 long i;
2369
2370 print_files = NULL;
2371 prev_functionname = NULL;
2372 prev_line = -1;
2373 prev_discriminator = 0;
2374
2375 /* We make a copy of syms to sort. We don't want to sort syms
2376 because that will screw up the relocs. */
2377 sorted_symcount = symcount ? symcount : dynsymcount;
2378 sorted_syms = (asymbol **) xmalloc ((sorted_symcount + synthcount)
2379 * sizeof (asymbol *));
2380 memcpy (sorted_syms, symcount ? syms : dynsyms,
2381 sorted_symcount * sizeof (asymbol *));
2382
2383 sorted_symcount = remove_useless_symbols (sorted_syms, sorted_symcount);
2384
2385 for (i = 0; i < synthcount; ++i)
2386 {
2387 sorted_syms[sorted_symcount] = synthsyms + i;
2388 ++sorted_symcount;
2389 }
2390
2391 /* Sort the symbols into section and symbol order. */
2392 qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols);
2393
2394 init_disassemble_info (&disasm_info, stdout, (fprintf_ftype) fprintf);
2395
2396 disasm_info.application_data = (void *) &aux;
2397 aux.abfd = abfd;
2398 aux.require_sec = FALSE;
2399 aux.dynrelbuf = NULL;
2400 aux.dynrelcount = 0;
2401 aux.reloc = NULL;
2402
2403 disasm_info.print_address_func = objdump_print_address;
2404 disasm_info.symbol_at_address_func = objdump_symbol_at_address;
2405
2406 if (machine != NULL)
2407 {
2408 const bfd_arch_info_type *inf = bfd_scan_arch (machine);
2409
2410 if (inf == NULL)
2411 fatal (_("can't use supplied machine %s"), machine);
2412
2413 abfd->arch_info = inf;
2414 }
2415
2416 if (endian != BFD_ENDIAN_UNKNOWN)
2417 {
2418 struct bfd_target *xvec;
2419
2420 xvec = (struct bfd_target *) xmalloc (sizeof (struct bfd_target));
2421 memcpy (xvec, abfd->xvec, sizeof (struct bfd_target));
2422 xvec->byteorder = endian;
2423 abfd->xvec = xvec;
2424 }
2425
2426 /* Use libopcodes to locate a suitable disassembler. */
2427 aux.disassemble_fn = disassembler (bfd_get_arch (abfd),
2428 bfd_big_endian (abfd),
2429 bfd_get_mach (abfd), abfd);
2430 if (!aux.disassemble_fn)
2431 {
2432 non_fatal (_("can't disassemble for architecture %s\n"),
2433 bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
2434 exit_status = 1;
2435 return;
2436 }
2437
2438 disasm_info.flavour = bfd_get_flavour (abfd);
2439 disasm_info.arch = bfd_get_arch (abfd);
2440 disasm_info.mach = bfd_get_mach (abfd);
2441 disasm_info.disassembler_options = disassembler_options;
2442 disasm_info.octets_per_byte = bfd_octets_per_byte (abfd);
2443 disasm_info.skip_zeroes = DEFAULT_SKIP_ZEROES;
2444 disasm_info.skip_zeroes_at_end = DEFAULT_SKIP_ZEROES_AT_END;
2445 disasm_info.disassembler_needs_relocs = FALSE;
2446
2447 if (bfd_big_endian (abfd))
2448 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_BIG;
2449 else if (bfd_little_endian (abfd))
2450 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_LITTLE;
2451 else
2452 /* ??? Aborting here seems too drastic. We could default to big or little
2453 instead. */
2454 disasm_info.endian = BFD_ENDIAN_UNKNOWN;
2455
2456 /* Allow the target to customize the info structure. */
2457 disassemble_init_for_target (& disasm_info);
2458
2459 /* Pre-load the dynamic relocs as we may need them during the disassembly. */
2460 {
2461 long relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
2462
2463 if (relsize < 0 && dump_dynamic_reloc_info)
2464 bfd_fatal (bfd_get_filename (abfd));
2465
2466 if (relsize > 0)
2467 {
2468 aux.dynrelbuf = (arelent **) xmalloc (relsize);
2469 aux.dynrelcount = bfd_canonicalize_dynamic_reloc (abfd,
2470 aux.dynrelbuf,
2471 dynsyms);
2472 if (aux.dynrelcount < 0)
2473 bfd_fatal (bfd_get_filename (abfd));
2474
2475 /* Sort the relocs by address. */
2476 qsort (aux.dynrelbuf, aux.dynrelcount, sizeof (arelent *),
2477 compare_relocs);
2478 }
2479 }
2480 disasm_info.symtab = sorted_syms;
2481 disasm_info.symtab_size = sorted_symcount;
2482
2483 bfd_map_over_sections (abfd, disassemble_section, & disasm_info);
2484
2485 if (aux.dynrelbuf != NULL)
2486 free (aux.dynrelbuf);
2487 free (sorted_syms);
2488 }
2489
2490 static bfd_boolean
2492 load_specific_debug_section (enum dwarf_section_display_enum debug,
2493 asection *sec, void *file)
2494 {
2495 struct dwarf_section *section = &debug_displays [debug].section;
2496 bfd *abfd = (bfd *) file;
2497 bfd_byte *contents;
2498 bfd_size_type amt;
2499
2500 if (section->start != NULL)
2501 {
2502 /* If it is already loaded, do nothing. */
2503 if (streq (section->filename, bfd_get_filename (abfd)))
2504 return TRUE;
2505 free (section->start);
2506 }
2507
2508 section->filename = bfd_get_filename (abfd);
2509 section->reloc_info = NULL;
2510 section->num_relocs = 0;
2511 section->address = bfd_get_section_vma (abfd, sec);
2512 section->size = bfd_get_section_size (sec);
2513 amt = section->size + 1;
2514 section->start = contents = malloc (amt);
2515 section->user_data = sec;
2516 if (amt == 0
2517 || section->start == NULL
2518 || !bfd_get_full_section_contents (abfd, sec, &contents))
2519 {
2520 free_debug_section (debug);
2521 printf (_("\nCan't get contents for section '%s'.\n"),
2522 section->name);
2523 return FALSE;
2524 }
2525 /* Ensure any string section has a terminating NUL. */
2526 section->start[section->size] = 0;
2527
2528 if (is_relocatable && debug_displays [debug].relocate)
2529 {
2530 long reloc_size;
2531 bfd_boolean ret;
2532
2533 bfd_cache_section_contents (sec, section->start);
2534
2535 ret = bfd_simple_get_relocated_section_contents (abfd,
2536 sec,
2537 section->start,
2538 syms) != NULL;
2539
2540 if (! ret)
2541 {
2542 free_debug_section (debug);
2543 printf (_("\nCan't get contents for section '%s'.\n"),
2544 section->name);
2545 return FALSE;
2546 }
2547
2548 reloc_size = bfd_get_reloc_upper_bound (abfd, sec);
2549 if (reloc_size > 0)
2550 {
2551 unsigned long reloc_count;
2552 arelent **relocs;
2553
2554 relocs = (arelent **) xmalloc (reloc_size);
2555
2556 reloc_count = bfd_canonicalize_reloc (abfd, sec, relocs, NULL);
2557 if (reloc_count == 0)
2558 free (relocs);
2559 else
2560 {
2561 section->reloc_info = relocs;
2562 section->num_relocs = reloc_count;
2563 }
2564 }
2565 }
2566
2567 return TRUE;
2568 }
2569
2570 bfd_boolean
2571 reloc_at (struct dwarf_section * dsec, dwarf_vma offset)
2572 {
2573 arelent ** relocs;
2574 arelent * rp;
2575
2576 if (dsec == NULL || dsec->reloc_info == NULL)
2577 return FALSE;
2578
2579 relocs = (arelent **) dsec->reloc_info;
2580
2581 for (; (rp = * relocs) != NULL; ++ relocs)
2582 if (rp->address == offset)
2583 return TRUE;
2584
2585 return FALSE;
2586 }
2587
2588 bfd_boolean
2589 load_debug_section (enum dwarf_section_display_enum debug, void *file)
2590 {
2591 struct dwarf_section *section = &debug_displays [debug].section;
2592 bfd *abfd = (bfd *) file;
2593 asection *sec;
2594
2595 /* If it is already loaded, do nothing. */
2596 if (section->start != NULL)
2597 {
2598 if (streq (section->filename, bfd_get_filename (abfd)))
2599 return TRUE;
2600 }
2601
2602 /* Locate the debug section. */
2603 sec = bfd_get_section_by_name (abfd, section->uncompressed_name);
2604 if (sec != NULL)
2605 section->name = section->uncompressed_name;
2606 else
2607 {
2608 sec = bfd_get_section_by_name (abfd, section->compressed_name);
2609 if (sec != NULL)
2610 section->name = section->compressed_name;
2611 }
2612 if (sec == NULL)
2613 return FALSE;
2614
2615 return load_specific_debug_section (debug, sec, file);
2616 }
2617
2618 void
2619 free_debug_section (enum dwarf_section_display_enum debug)
2620 {
2621 struct dwarf_section *section = &debug_displays [debug].section;
2622
2623 if (section->start == NULL)
2624 return;
2625
2626 /* PR 17512: file: 0f67f69d. */
2627 if (section->user_data != NULL)
2628 {
2629 asection * sec = (asection *) section->user_data;
2630
2631 /* If we are freeing contents that are also pointed to by the BFD
2632 library's section structure then make sure to update those pointers
2633 too. Otherwise, the next time we try to load data for this section
2634 we can end up using a stale pointer. */
2635 if (section->start == sec->contents)
2636 {
2637 sec->contents = NULL;
2638 sec->flags &= ~ SEC_IN_MEMORY;
2639 sec->compress_status = COMPRESS_SECTION_NONE;
2640 }
2641 }
2642
2643 free ((char *) section->start);
2644 section->start = NULL;
2645 section->address = 0;
2646 section->size = 0;
2647 }
2648
2649 void
2650 close_debug_file (void * file)
2651 {
2652 bfd * abfd = (bfd *) file;
2653
2654 bfd_close (abfd);
2655 }
2656
2657 void *
2658 open_debug_file (const char * pathname)
2659 {
2660 bfd * data;
2661
2662 data = bfd_openr (pathname, NULL);
2663 if (data == NULL)
2664 return NULL;
2665
2666 if (! bfd_check_format (data, bfd_object))
2667 return NULL;
2668
2669 return data;
2670 }
2671
2672 static void
2673 dump_dwarf_section (bfd *abfd, asection *section,
2674 void *arg ATTRIBUTE_UNUSED)
2675 {
2676 const char *name = bfd_get_section_name (abfd, section);
2677 const char *match;
2678 int i;
2679
2680 if (CONST_STRNEQ (name, ".gnu.linkonce.wi."))
2681 match = ".debug_info";
2682 else
2683 match = name;
2684
2685 for (i = 0; i < max; i++)
2686 if ((strcmp (debug_displays [i].section.uncompressed_name, match) == 0
2687 || strcmp (debug_displays [i].section.compressed_name, match) == 0)
2688 && debug_displays [i].enabled != NULL
2689 && *debug_displays [i].enabled)
2690 {
2691 struct dwarf_section *sec = &debug_displays [i].section;
2692
2693 if (strcmp (sec->uncompressed_name, match) == 0)
2694 sec->name = sec->uncompressed_name;
2695 else
2696 sec->name = sec->compressed_name;
2697 if (load_specific_debug_section ((enum dwarf_section_display_enum) i,
2698 section, abfd))
2699 {
2700 debug_displays [i].display (sec, abfd);
2701
2702 if (i != info && i != abbrev)
2703 free_debug_section ((enum dwarf_section_display_enum) i);
2704 }
2705 break;
2706 }
2707 }
2708
2709 /* Dump the dwarf debugging information. */
2710
2711 static void
2712 dump_dwarf (bfd *abfd)
2713 {
2714 bfd * separates;
2715
2716 is_relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2717
2718 eh_addr_size = bfd_arch_bits_per_address (abfd) / 8;
2719
2720 if (bfd_big_endian (abfd))
2721 byte_get = byte_get_big_endian;
2722 else if (bfd_little_endian (abfd))
2723 byte_get = byte_get_little_endian;
2724 else
2725 /* PR 17512: file: objdump-s-endless-loop.tekhex. */
2726 {
2727 warn (_("File %s does not contain any dwarf debug information\n"),
2728 bfd_get_filename (abfd));
2729 return;
2730 }
2731
2732 switch (bfd_get_arch (abfd))
2733 {
2734 case bfd_arch_i386:
2735 switch (bfd_get_mach (abfd))
2736 {
2737 case bfd_mach_x86_64:
2738 case bfd_mach_x86_64_intel_syntax:
2739 case bfd_mach_x86_64_nacl:
2740 case bfd_mach_x64_32:
2741 case bfd_mach_x64_32_intel_syntax:
2742 case bfd_mach_x64_32_nacl:
2743 init_dwarf_regnames_x86_64 ();
2744 break;
2745
2746 default:
2747 init_dwarf_regnames_i386 ();
2748 break;
2749 }
2750 break;
2751
2752 case bfd_arch_iamcu:
2753 init_dwarf_regnames_iamcu ();
2754 break;
2755
2756 case bfd_arch_aarch64:
2757 init_dwarf_regnames_aarch64();
2758 break;
2759
2760 case bfd_arch_s390:
2761 init_dwarf_regnames_s390 ();
2762 break;
2763
2764 case bfd_arch_riscv:
2765 init_dwarf_regnames_riscv ();
2766 break;
2767
2768 default:
2769 break;
2770 }
2771
2772 separates = load_separate_debug_file (abfd, bfd_get_filename (abfd));
2773
2774 bfd_map_over_sections (abfd, dump_dwarf_section, NULL);
2775
2776 if (separates)
2777 bfd_map_over_sections (separates, dump_dwarf_section, NULL);
2778
2779 free_debug_memory ();
2780 }
2781
2782 /* Read ABFD's stabs section STABSECT_NAME, and return a pointer to
2784 it. Return NULL on failure. */
2785
2786 static bfd_byte *
2787 read_section_stabs (bfd *abfd, const char *sect_name, bfd_size_type *size_ptr)
2788 {
2789 asection *stabsect;
2790 bfd_byte *contents;
2791
2792 stabsect = bfd_get_section_by_name (abfd, sect_name);
2793 if (stabsect == NULL)
2794 {
2795 printf (_("No %s section present\n\n"), sect_name);
2796 return FALSE;
2797 }
2798
2799 if (!bfd_malloc_and_get_section (abfd, stabsect, &contents))
2800 {
2801 non_fatal (_("reading %s section of %s failed: %s"),
2802 sect_name, bfd_get_filename (abfd),
2803 bfd_errmsg (bfd_get_error ()));
2804 exit_status = 1;
2805 free (contents);
2806 return NULL;
2807 }
2808
2809 *size_ptr = bfd_section_size (abfd, stabsect);
2810
2811 return contents;
2812 }
2813
2814 /* Stabs entries use a 12 byte format:
2815 4 byte string table index
2816 1 byte stab type
2817 1 byte stab other field
2818 2 byte stab desc field
2819 4 byte stab value
2820 FIXME: This will have to change for a 64 bit object format. */
2821
2822 #define STRDXOFF (0)
2823 #define TYPEOFF (4)
2824 #define OTHEROFF (5)
2825 #define DESCOFF (6)
2826 #define VALOFF (8)
2827 #define STABSIZE (12)
2828
2829 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
2830 using string table section STRSECT_NAME (in `strtab'). */
2831
2832 static void
2833 print_section_stabs (bfd *abfd,
2834 const char *stabsect_name,
2835 unsigned *string_offset_ptr)
2836 {
2837 int i;
2838 unsigned file_string_table_offset = 0;
2839 unsigned next_file_string_table_offset = *string_offset_ptr;
2840 bfd_byte *stabp, *stabs_end;
2841
2842 stabp = stabs;
2843 stabs_end = stabp + stab_size;
2844
2845 printf (_("Contents of %s section:\n\n"), stabsect_name);
2846 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
2847
2848 /* Loop through all symbols and print them.
2849
2850 We start the index at -1 because there is a dummy symbol on
2851 the front of stabs-in-{coff,elf} sections that supplies sizes. */
2852 for (i = -1; stabp <= stabs_end - STABSIZE; stabp += STABSIZE, i++)
2853 {
2854 const char *name;
2855 unsigned long strx;
2856 unsigned char type, other;
2857 unsigned short desc;
2858 bfd_vma value;
2859
2860 strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
2861 type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
2862 other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
2863 desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
2864 value = bfd_h_get_32 (abfd, stabp + VALOFF);
2865
2866 printf ("\n%-6d ", i);
2867 /* Either print the stab name, or, if unnamed, print its number
2868 again (makes consistent formatting for tools like awk). */
2869 name = bfd_get_stab_name (type);
2870 if (name != NULL)
2871 printf ("%-6s", name);
2872 else if (type == N_UNDF)
2873 printf ("HdrSym");
2874 else
2875 printf ("%-6d", type);
2876 printf (" %-6d %-6d ", other, desc);
2877 bfd_printf_vma (abfd, value);
2878 printf (" %-6lu", strx);
2879
2880 /* Symbols with type == 0 (N_UNDF) specify the length of the
2881 string table associated with this file. We use that info
2882 to know how to relocate the *next* file's string table indices. */
2883 if (type == N_UNDF)
2884 {
2885 file_string_table_offset = next_file_string_table_offset;
2886 next_file_string_table_offset += value;
2887 }
2888 else
2889 {
2890 bfd_size_type amt = strx + file_string_table_offset;
2891
2892 /* Using the (possibly updated) string table offset, print the
2893 string (if any) associated with this symbol. */
2894 if (amt < stabstr_size)
2895 /* PR 17512: file: 079-79389-0.001:0.1. */
2896 printf (" %.*s", (int)(stabstr_size - amt), strtab + amt);
2897 else
2898 printf (" *");
2899 }
2900 }
2901 printf ("\n\n");
2902 *string_offset_ptr = next_file_string_table_offset;
2903 }
2904
2905 typedef struct
2906 {
2907 const char * section_name;
2908 const char * string_section_name;
2909 unsigned string_offset;
2910 }
2911 stab_section_names;
2912
2913 static void
2914 find_stabs_section (bfd *abfd, asection *section, void *names)
2915 {
2916 int len;
2917 stab_section_names * sought = (stab_section_names *) names;
2918
2919 /* Check for section names for which stabsect_name is a prefix, to
2920 handle .stab.N, etc. */
2921 len = strlen (sought->section_name);
2922
2923 /* If the prefix matches, and the files section name ends with a
2924 nul or a digit, then we match. I.e., we want either an exact
2925 match or a section followed by a number. */
2926 if (strncmp (sought->section_name, section->name, len) == 0
2927 && (section->name[len] == 0
2928 || (section->name[len] == '.' && ISDIGIT (section->name[len + 1]))))
2929 {
2930 if (strtab == NULL)
2931 strtab = read_section_stabs (abfd, sought->string_section_name,
2932 &stabstr_size);
2933
2934 if (strtab)
2935 {
2936 stabs = read_section_stabs (abfd, section->name, &stab_size);
2937 if (stabs)
2938 print_section_stabs (abfd, section->name, &sought->string_offset);
2939 }
2940 }
2941 }
2942
2943 static void
2944 dump_stabs_section (bfd *abfd, char *stabsect_name, char *strsect_name)
2945 {
2946 stab_section_names s;
2947
2948 s.section_name = stabsect_name;
2949 s.string_section_name = strsect_name;
2950 s.string_offset = 0;
2951
2952 bfd_map_over_sections (abfd, find_stabs_section, & s);
2953
2954 free (strtab);
2955 strtab = NULL;
2956 }
2957
2958 /* Dump the any sections containing stabs debugging information. */
2959
2960 static void
2961 dump_stabs (bfd *abfd)
2962 {
2963 dump_stabs_section (abfd, ".stab", ".stabstr");
2964 dump_stabs_section (abfd, ".stab.excl", ".stab.exclstr");
2965 dump_stabs_section (abfd, ".stab.index", ".stab.indexstr");
2966
2967 /* For Darwin. */
2968 dump_stabs_section (abfd, "LC_SYMTAB.stabs", "LC_SYMTAB.stabstr");
2969
2970 dump_stabs_section (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
2971 }
2972
2973 static void
2975 dump_bfd_header (bfd *abfd)
2976 {
2977 char *comma = "";
2978
2979 printf (_("architecture: %s, "),
2980 bfd_printable_arch_mach (bfd_get_arch (abfd),
2981 bfd_get_mach (abfd)));
2982 printf (_("flags 0x%08x:\n"), abfd->flags & ~BFD_FLAGS_FOR_BFD_USE_MASK);
2983
2984 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
2985 PF (HAS_RELOC, "HAS_RELOC");
2986 PF (EXEC_P, "EXEC_P");
2987 PF (HAS_LINENO, "HAS_LINENO");
2988 PF (HAS_DEBUG, "HAS_DEBUG");
2989 PF (HAS_SYMS, "HAS_SYMS");
2990 PF (HAS_LOCALS, "HAS_LOCALS");
2991 PF (DYNAMIC, "DYNAMIC");
2992 PF (WP_TEXT, "WP_TEXT");
2993 PF (D_PAGED, "D_PAGED");
2994 PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
2995 printf (_("\nstart address 0x"));
2996 bfd_printf_vma (abfd, abfd->start_address);
2997 printf ("\n");
2998 }
2999
3000
3001 static void
3003 dump_bfd_private_header (bfd *abfd)
3004 {
3005 bfd_print_private_bfd_data (abfd, stdout);
3006 }
3007
3008 static void
3009 dump_target_specific (bfd *abfd)
3010 {
3011 const struct objdump_private_desc * const *desc;
3012 struct objdump_private_option *opt;
3013 char *e, *b;
3014
3015 /* Find the desc. */
3016 for (desc = objdump_private_vectors; *desc != NULL; desc++)
3017 if ((*desc)->filter (abfd))
3018 break;
3019
3020 if (*desc == NULL)
3021 {
3022 non_fatal (_("option -P/--private not supported by this file"));
3023 return;
3024 }
3025
3026 /* Clear all options. */
3027 for (opt = (*desc)->options; opt->name; opt++)
3028 opt->selected = FALSE;
3029
3030 /* Decode options. */
3031 b = dump_private_options;
3032 do
3033 {
3034 e = strchr (b, ',');
3035
3036 if (e)
3037 *e = 0;
3038
3039 for (opt = (*desc)->options; opt->name; opt++)
3040 if (strcmp (opt->name, b) == 0)
3041 {
3042 opt->selected = TRUE;
3043 break;
3044 }
3045 if (opt->name == NULL)
3046 non_fatal (_("target specific dump '%s' not supported"), b);
3047
3048 if (e)
3049 {
3050 *e = ',';
3051 b = e + 1;
3052 }
3053 }
3054 while (e != NULL);
3055
3056 /* Dump. */
3057 (*desc)->dump (abfd);
3058 }
3059
3060 /* Display a section in hexadecimal format with associated characters.
3062 Each line prefixed by the zero padded address. */
3063
3064 static void
3065 dump_section (bfd *abfd, asection *section, void *dummy ATTRIBUTE_UNUSED)
3066 {
3067 bfd_byte *data = NULL;
3068 bfd_size_type datasize;
3069 bfd_vma addr_offset;
3070 bfd_vma start_offset;
3071 bfd_vma stop_offset;
3072 unsigned int opb = bfd_octets_per_byte (abfd);
3073 /* Bytes per line. */
3074 const int onaline = 16;
3075 char buf[64];
3076 int count;
3077 int width;
3078
3079 if ((section->flags & SEC_HAS_CONTENTS) == 0)
3080 return;
3081
3082 if (! process_section_p (section))
3083 return;
3084
3085 if ((datasize = bfd_section_size (abfd, section)) == 0)
3086 return;
3087
3088 /* Compute the address range to display. */
3089 if (start_address == (bfd_vma) -1
3090 || start_address < section->vma)
3091 start_offset = 0;
3092 else
3093 start_offset = start_address - section->vma;
3094
3095 if (stop_address == (bfd_vma) -1)
3096 stop_offset = datasize / opb;
3097 else
3098 {
3099 if (stop_address < section->vma)
3100 stop_offset = 0;
3101 else
3102 stop_offset = stop_address - section->vma;
3103
3104 if (stop_offset > datasize / opb)
3105 stop_offset = datasize / opb;
3106 }
3107
3108 if (start_offset >= stop_offset)
3109 return;
3110
3111 printf (_("Contents of section %s:"), section->name);
3112 if (display_file_offsets)
3113 printf (_(" (Starting at file offset: 0x%lx)"),
3114 (unsigned long) (section->filepos + start_offset));
3115 printf ("\n");
3116
3117 if (!bfd_get_full_section_contents (abfd, section, &data))
3118 {
3119 non_fatal (_("Reading section %s failed because: %s"),
3120 section->name, bfd_errmsg (bfd_get_error ()));
3121 return;
3122 }
3123
3124 width = 4;
3125
3126 bfd_sprintf_vma (abfd, buf, start_offset + section->vma);
3127 if (strlen (buf) >= sizeof (buf))
3128 abort ();
3129
3130 count = 0;
3131 while (buf[count] == '0' && buf[count+1] != '\0')
3132 count++;
3133 count = strlen (buf) - count;
3134 if (count > width)
3135 width = count;
3136
3137 bfd_sprintf_vma (abfd, buf, stop_offset + section->vma - 1);
3138 if (strlen (buf) >= sizeof (buf))
3139 abort ();
3140
3141 count = 0;
3142 while (buf[count] == '0' && buf[count+1] != '\0')
3143 count++;
3144 count = strlen (buf) - count;
3145 if (count > width)
3146 width = count;
3147
3148 for (addr_offset = start_offset;
3149 addr_offset < stop_offset; addr_offset += onaline / opb)
3150 {
3151 bfd_size_type j;
3152
3153 bfd_sprintf_vma (abfd, buf, (addr_offset + section->vma));
3154 count = strlen (buf);
3155 if ((size_t) count >= sizeof (buf))
3156 abort ();
3157
3158 putchar (' ');
3159 while (count < width)
3160 {
3161 putchar ('0');
3162 count++;
3163 }
3164 fputs (buf + count - width, stdout);
3165 putchar (' ');
3166
3167 for (j = addr_offset * opb;
3168 j < addr_offset * opb + onaline; j++)
3169 {
3170 if (j < stop_offset * opb)
3171 printf ("%02x", (unsigned) (data[j]));
3172 else
3173 printf (" ");
3174 if ((j & 3) == 3)
3175 printf (" ");
3176 }
3177
3178 printf (" ");
3179 for (j = addr_offset * opb;
3180 j < addr_offset * opb + onaline; j++)
3181 {
3182 if (j >= stop_offset * opb)
3183 printf (" ");
3184 else
3185 printf ("%c", ISPRINT (data[j]) ? data[j] : '.');
3186 }
3187 putchar ('\n');
3188 }
3189 free (data);
3190 }
3191
3192 /* Actually display the various requested regions. */
3193
3194 static void
3195 dump_data (bfd *abfd)
3196 {
3197 bfd_map_over_sections (abfd, dump_section, NULL);
3198 }
3199
3200 /* Should perhaps share code and display with nm? */
3201
3202 static void
3203 dump_symbols (bfd *abfd ATTRIBUTE_UNUSED, bfd_boolean dynamic)
3204 {
3205 asymbol **current;
3206 long max_count;
3207 long count;
3208
3209 if (dynamic)
3210 {
3211 current = dynsyms;
3212 max_count = dynsymcount;
3213 printf ("DYNAMIC SYMBOL TABLE:\n");
3214 }
3215 else
3216 {
3217 current = syms;
3218 max_count = symcount;
3219 printf ("SYMBOL TABLE:\n");
3220 }
3221
3222 if (max_count == 0)
3223 printf (_("no symbols\n"));
3224
3225 for (count = 0; count < max_count; count++)
3226 {
3227 bfd *cur_bfd;
3228
3229 if (*current == NULL)
3230 printf (_("no information for symbol number %ld\n"), count);
3231
3232 else if ((cur_bfd = bfd_asymbol_bfd (*current)) == NULL)
3233 printf (_("could not determine the type of symbol number %ld\n"),
3234 count);
3235
3236 else if (process_section_p ((* current)->section)
3237 && (dump_special_syms
3238 || !bfd_is_target_special_symbol (cur_bfd, *current)))
3239 {
3240 const char *name = (*current)->name;
3241
3242 if (do_demangle && name != NULL && *name != '\0')
3243 {
3244 char *alloc;
3245
3246 /* If we want to demangle the name, we demangle it
3247 here, and temporarily clobber it while calling
3248 bfd_print_symbol. FIXME: This is a gross hack. */
3249 alloc = bfd_demangle (cur_bfd, name, DMGL_ANSI | DMGL_PARAMS);
3250 if (alloc != NULL)
3251 (*current)->name = alloc;
3252 bfd_print_symbol (cur_bfd, stdout, *current,
3253 bfd_print_symbol_all);
3254 if (alloc != NULL)
3255 {
3256 (*current)->name = name;
3257 free (alloc);
3258 }
3259 }
3260 else
3261 bfd_print_symbol (cur_bfd, stdout, *current,
3262 bfd_print_symbol_all);
3263 printf ("\n");
3264 }
3265
3266 current++;
3267 }
3268 printf ("\n\n");
3269 }
3270
3271 static void
3273 dump_reloc_set (bfd *abfd, asection *sec, arelent **relpp, long relcount)
3274 {
3275 arelent **p;
3276 char *last_filename, *last_functionname;
3277 unsigned int last_line;
3278 unsigned int last_discriminator;
3279
3280 /* Get column headers lined up reasonably. */
3281 {
3282 static int width;
3283
3284 if (width == 0)
3285 {
3286 char buf[30];
3287
3288 bfd_sprintf_vma (abfd, buf, (bfd_vma) -1);
3289 width = strlen (buf) - 7;
3290 }
3291 printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
3292 }
3293
3294 last_filename = NULL;
3295 last_functionname = NULL;
3296 last_line = 0;
3297 last_discriminator = 0;
3298
3299 for (p = relpp; relcount && *p != NULL; p++, relcount--)
3300 {
3301 arelent *q = *p;
3302 const char *filename, *functionname;
3303 unsigned int linenumber;
3304 unsigned int discriminator;
3305 const char *sym_name;
3306 const char *section_name;
3307 bfd_vma addend2 = 0;
3308
3309 if (start_address != (bfd_vma) -1
3310 && q->address < start_address)
3311 continue;
3312 if (stop_address != (bfd_vma) -1
3313 && q->address > stop_address)
3314 continue;
3315
3316 if (with_line_numbers
3317 && sec != NULL
3318 && bfd_find_nearest_line_discriminator (abfd, sec, syms, q->address,
3319 &filename, &functionname,
3320 &linenumber, &discriminator))
3321 {
3322 if (functionname != NULL
3323 && (last_functionname == NULL
3324 || strcmp (functionname, last_functionname) != 0))
3325 {
3326 printf ("%s():\n", functionname);
3327 if (last_functionname != NULL)
3328 free (last_functionname);
3329 last_functionname = xstrdup (functionname);
3330 }
3331
3332 if (linenumber > 0
3333 && (linenumber != last_line
3334 || (filename != NULL
3335 && last_filename != NULL
3336 && filename_cmp (filename, last_filename) != 0)
3337 || (discriminator != last_discriminator)))
3338 {
3339 if (discriminator > 0)
3340 printf ("%s:%u\n", filename == NULL ? "???" : filename, linenumber);
3341 else
3342 printf ("%s:%u (discriminator %u)\n", filename == NULL ? "???" : filename,
3343 linenumber, discriminator);
3344 last_line = linenumber;
3345 last_discriminator = discriminator;
3346 if (last_filename != NULL)
3347 free (last_filename);
3348 if (filename == NULL)
3349 last_filename = NULL;
3350 else
3351 last_filename = xstrdup (filename);
3352 }
3353 }
3354
3355 if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
3356 {
3357 sym_name = (*(q->sym_ptr_ptr))->name;
3358 section_name = (*(q->sym_ptr_ptr))->section->name;
3359 }
3360 else
3361 {
3362 sym_name = NULL;
3363 section_name = NULL;
3364 }
3365
3366 bfd_printf_vma (abfd, q->address);
3367 if (q->howto == NULL)
3368 printf (" *unknown* ");
3369 else if (q->howto->name)
3370 {
3371 const char *name = q->howto->name;
3372
3373 /* R_SPARC_OLO10 relocations contain two addends.
3374 But because 'arelent' lacks enough storage to
3375 store them both, the 64-bit ELF Sparc backend
3376 records this as two relocations. One R_SPARC_LO10
3377 and one R_SPARC_13, both pointing to the same
3378 address. This is merely so that we have some
3379 place to store both addend fields.
3380
3381 Undo this transformation, otherwise the output
3382 will be confusing. */
3383 if (abfd->xvec->flavour == bfd_target_elf_flavour
3384 && elf_tdata(abfd)->elf_header->e_machine == EM_SPARCV9
3385 && relcount > 1
3386 && !strcmp (q->howto->name, "R_SPARC_LO10"))
3387 {
3388 arelent *q2 = *(p + 1);
3389 if (q2 != NULL
3390 && q2->howto
3391 && q->address == q2->address
3392 && !strcmp (q2->howto->name, "R_SPARC_13"))
3393 {
3394 name = "R_SPARC_OLO10";
3395 addend2 = q2->addend;
3396 p++;
3397 }
3398 }
3399 printf (" %-16s ", name);
3400 }
3401 else
3402 printf (" %-16d ", q->howto->type);
3403
3404 if (sym_name)
3405 {
3406 objdump_print_symname (abfd, NULL, *q->sym_ptr_ptr);
3407 }
3408 else
3409 {
3410 if (section_name == NULL)
3411 section_name = "*unknown*";
3412 printf ("[%s]", section_name);
3413 }
3414
3415 if (q->addend)
3416 {
3417 bfd_signed_vma addend = q->addend;
3418 if (addend < 0)
3419 {
3420 printf ("-0x");
3421 addend = -addend;
3422 }
3423 else
3424 printf ("+0x");
3425 bfd_printf_vma (abfd, addend);
3426 }
3427 if (addend2)
3428 {
3429 printf ("+0x");
3430 bfd_printf_vma (abfd, addend2);
3431 }
3432
3433 printf ("\n");
3434 }
3435
3436 if (last_filename != NULL)
3437 free (last_filename);
3438 if (last_functionname != NULL)
3439 free (last_functionname);
3440 }
3441
3442 static void
3443 dump_relocs_in_section (bfd *abfd,
3444 asection *section,
3445 void *dummy ATTRIBUTE_UNUSED)
3446 {
3447 arelent **relpp;
3448 long relcount;
3449 long relsize;
3450
3451 if ( bfd_is_abs_section (section)
3452 || bfd_is_und_section (section)
3453 || bfd_is_com_section (section)
3454 || (! process_section_p (section))
3455 || ((section->flags & SEC_RELOC) == 0))
3456 return;
3457
3458 relsize = bfd_get_reloc_upper_bound (abfd, section);
3459 if (relsize < 0)
3460 bfd_fatal (bfd_get_filename (abfd));
3461
3462 printf ("RELOCATION RECORDS FOR [%s]:", section->name);
3463
3464 if (relsize == 0)
3465 {
3466 printf (" (none)\n\n");
3467 return;
3468 }
3469
3470 if ((bfd_get_file_flags (abfd) & (BFD_IN_MEMORY | BFD_LINKER_CREATED)) == 0
3471 && (((ufile_ptr) relsize > bfd_get_file_size (abfd))
3472 /* Also check the section's reloc count since if this is negative
3473 (or very large) the computation in bfd_get_reloc_upper_bound
3474 may have resulted in returning a small, positive integer.
3475 See PR 22508 for a reproducer.
3476
3477 Note - we check against file size rather than section size as
3478 it is possible for there to be more relocs that apply to a
3479 section than there are bytes in that section. */
3480 || (section->reloc_count > bfd_get_file_size (abfd))))
3481 {
3482 printf (" (too many: 0x%x)\n", section->reloc_count);
3483 bfd_set_error (bfd_error_file_truncated);
3484 bfd_fatal (bfd_get_filename (abfd));
3485 }
3486
3487 relpp = (arelent **) xmalloc (relsize);
3488 relcount = bfd_canonicalize_reloc (abfd, section, relpp, syms);
3489
3490 if (relcount < 0)
3491 {
3492 printf ("\n");
3493 non_fatal (_("failed to read relocs in: %s"), bfd_get_filename (abfd));
3494 bfd_fatal (_("error message was"));
3495 }
3496 else if (relcount == 0)
3497 printf (" (none)\n\n");
3498 else
3499 {
3500 printf ("\n");
3501 dump_reloc_set (abfd, section, relpp, relcount);
3502 printf ("\n\n");
3503 }
3504 free (relpp);
3505 }
3506
3507 static void
3508 dump_relocs (bfd *abfd)
3509 {
3510 bfd_map_over_sections (abfd, dump_relocs_in_section, NULL);
3511 }
3512
3513 static void
3514 dump_dynamic_relocs (bfd *abfd)
3515 {
3516 long relsize;
3517 arelent **relpp;
3518 long relcount;
3519
3520 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
3521 if (relsize < 0)
3522 bfd_fatal (bfd_get_filename (abfd));
3523
3524 printf ("DYNAMIC RELOCATION RECORDS");
3525
3526 if (relsize == 0)
3527 printf (" (none)\n\n");
3528 else
3529 {
3530 relpp = (arelent **) xmalloc (relsize);
3531 relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
3532
3533 if (relcount < 0)
3534 bfd_fatal (bfd_get_filename (abfd));
3535 else if (relcount == 0)
3536 printf (" (none)\n\n");
3537 else
3538 {
3539 printf ("\n");
3540 dump_reloc_set (abfd, NULL, relpp, relcount);
3541 printf ("\n\n");
3542 }
3543 free (relpp);
3544 }
3545 }
3546
3547 /* Creates a table of paths, to search for source files. */
3548
3549 static void
3550 add_include_path (const char *path)
3551 {
3552 if (path[0] == 0)
3553 return;
3554 include_path_count++;
3555 include_paths = (const char **)
3556 xrealloc (include_paths, include_path_count * sizeof (*include_paths));
3557 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
3558 if (path[1] == ':' && path[2] == 0)
3559 path = concat (path, ".", (const char *) 0);
3560 #endif
3561 include_paths[include_path_count - 1] = path;
3562 }
3563
3564 static void
3565 adjust_addresses (bfd *abfd ATTRIBUTE_UNUSED,
3566 asection *section,
3567 void *arg)
3568 {
3569 if ((section->flags & SEC_DEBUGGING) == 0)
3570 {
3571 bfd_boolean *has_reloc_p = (bfd_boolean *) arg;
3572 section->vma += adjust_section_vma;
3573 if (*has_reloc_p)
3574 section->lma += adjust_section_vma;
3575 }
3576 }
3577
3578 /* Dump selected contents of ABFD. */
3579
3580 static void
3581 dump_bfd (bfd *abfd)
3582 {
3583 /* If we are adjusting section VMA's, change them all now. Changing
3584 the BFD information is a hack. However, we must do it, or
3585 bfd_find_nearest_line will not do the right thing. */
3586 if (adjust_section_vma != 0)
3587 {
3588 bfd_boolean has_reloc = (abfd->flags & HAS_RELOC);
3589 bfd_map_over_sections (abfd, adjust_addresses, &has_reloc);
3590 }
3591
3592 if (! dump_debugging_tags && ! suppress_bfd_header)
3593 printf (_("\n%s: file format %s\n"), bfd_get_filename (abfd),
3594 abfd->xvec->name);
3595 if (dump_ar_hdrs)
3596 print_arelt_descr (stdout, abfd, TRUE, FALSE);
3597 if (dump_file_header)
3598 dump_bfd_header (abfd);
3599 if (dump_private_headers)
3600 dump_bfd_private_header (abfd);
3601 if (dump_private_options != NULL)
3602 dump_target_specific (abfd);
3603 if (! dump_debugging_tags && ! suppress_bfd_header)
3604 putchar ('\n');
3605
3606 if (dump_symtab
3607 || dump_reloc_info
3608 || disassemble
3609 || dump_debugging
3610 || dump_dwarf_section_info)
3611 syms = slurp_symtab (abfd);
3612
3613 if (dump_section_headers)
3614 dump_headers (abfd);
3615
3616 if (dump_dynamic_symtab || dump_dynamic_reloc_info
3617 || (disassemble && bfd_get_dynamic_symtab_upper_bound (abfd) > 0))
3618 dynsyms = slurp_dynamic_symtab (abfd);
3619 if (disassemble)
3620 {
3621 synthcount = bfd_get_synthetic_symtab (abfd, symcount, syms,
3622 dynsymcount, dynsyms, &synthsyms);
3623 if (synthcount < 0)
3624 synthcount = 0;
3625 }
3626
3627 if (dump_symtab)
3628 dump_symbols (abfd, FALSE);
3629 if (dump_dynamic_symtab)
3630 dump_symbols (abfd, TRUE);
3631 if (dump_dwarf_section_info)
3632 dump_dwarf (abfd);
3633 if (dump_stab_section_info)
3634 dump_stabs (abfd);
3635 if (dump_reloc_info && ! disassemble)
3636 dump_relocs (abfd);
3637 if (dump_dynamic_reloc_info && ! disassemble)
3638 dump_dynamic_relocs (abfd);
3639 if (dump_section_contents)
3640 dump_data (abfd);
3641 if (disassemble)
3642 disassemble_data (abfd);
3643
3644 if (dump_debugging)
3645 {
3646 void *dhandle;
3647
3648 dhandle = read_debugging_info (abfd, syms, symcount, TRUE);
3649 if (dhandle != NULL)
3650 {
3651 if (!print_debugging_info (stdout, dhandle, abfd, syms,
3652 bfd_demangle,
3653 dump_debugging_tags ? TRUE : FALSE))
3654 {
3655 non_fatal (_("%s: printing debugging information failed"),
3656 bfd_get_filename (abfd));
3657 exit_status = 1;
3658 }
3659 }
3660 /* PR 6483: If there was no STABS debug info in the file, try
3661 DWARF instead. */
3662 else if (! dump_dwarf_section_info)
3663 {
3664 dwarf_select_sections_all ();
3665 dump_dwarf (abfd);
3666 }
3667 }
3668
3669 if (syms)
3670 {
3671 free (syms);
3672 syms = NULL;
3673 }
3674
3675 if (dynsyms)
3676 {
3677 free (dynsyms);
3678 dynsyms = NULL;
3679 }
3680
3681 if (synthsyms)
3682 {
3683 free (synthsyms);
3684 synthsyms = NULL;
3685 }
3686
3687 symcount = 0;
3688 dynsymcount = 0;
3689 synthcount = 0;
3690 }
3691
3692 static void
3693 display_object_bfd (bfd *abfd)
3694 {
3695 char **matching;
3696
3697 if (bfd_check_format_matches (abfd, bfd_object, &matching))
3698 {
3699 dump_bfd (abfd);
3700 return;
3701 }
3702
3703 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
3704 {
3705 nonfatal (bfd_get_filename (abfd));
3706 list_matching_formats (matching);
3707 free (matching);
3708 return;
3709 }
3710
3711 if (bfd_get_error () != bfd_error_file_not_recognized)
3712 {
3713 nonfatal (bfd_get_filename (abfd));
3714 return;
3715 }
3716
3717 if (bfd_check_format_matches (abfd, bfd_core, &matching))
3718 {
3719 dump_bfd (abfd);
3720 return;
3721 }
3722
3723 nonfatal (bfd_get_filename (abfd));
3724
3725 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
3726 {
3727 list_matching_formats (matching);
3728 free (matching);
3729 }
3730 }
3731
3732 static void
3733 display_any_bfd (bfd *file, int level)
3734 {
3735 /* Decompress sections unless dumping the section contents. */
3736 if (!dump_section_contents)
3737 file->flags |= BFD_DECOMPRESS;
3738
3739 /* If the file is an archive, process all of its elements. */
3740 if (bfd_check_format (file, bfd_archive))
3741 {
3742 bfd *arfile = NULL;
3743 bfd *last_arfile = NULL;
3744
3745 if (level == 0)
3746 printf (_("In archive %s:\n"), bfd_get_filename (file));
3747 else if (level > 100)
3748 {
3749 /* Prevent corrupted files from spinning us into an
3750 infinite loop. 100 is an arbitrary heuristic. */
3751 fatal (_("Archive nesting is too deep"));
3752 return;
3753 }
3754 else
3755 printf (_("In nested archive %s:\n"), bfd_get_filename (file));
3756
3757 for (;;)
3758 {
3759 bfd_set_error (bfd_error_no_error);
3760
3761 arfile = bfd_openr_next_archived_file (file, arfile);
3762 if (arfile == NULL)
3763 {
3764 if (bfd_get_error () != bfd_error_no_more_archived_files)
3765 nonfatal (bfd_get_filename (file));
3766 break;
3767 }
3768
3769 display_any_bfd (arfile, level + 1);
3770
3771 if (last_arfile != NULL)
3772 {
3773 bfd_close (last_arfile);
3774 /* PR 17512: file: ac585d01. */
3775 if (arfile == last_arfile)
3776 {
3777 last_arfile = NULL;
3778 break;
3779 }
3780 }
3781 last_arfile = arfile;
3782 }
3783
3784 if (last_arfile != NULL)
3785 bfd_close (last_arfile);
3786 }
3787 else
3788 display_object_bfd (file);
3789 }
3790
3791 static void
3792 display_file (char *filename, char *target, bfd_boolean last_file)
3793 {
3794 bfd *file;
3795
3796 if (get_file_size (filename) < 1)
3797 {
3798 exit_status = 1;
3799 return;
3800 }
3801
3802 file = bfd_openr (filename, target);
3803 if (file == NULL)
3804 {
3805 nonfatal (filename);
3806 return;
3807 }
3808
3809 display_any_bfd (file, 0);
3810
3811 /* This is an optimization to improve the speed of objdump, especially when
3812 dumping a file with lots of associated debug informatiom. Calling
3813 bfd_close on such a file can take a non-trivial amount of time as there
3814 are lots of lists to walk and buffers to free. This is only really
3815 necessary however if we are about to load another file and we need the
3816 memory back. Otherwise, if we are about to exit, then we can save (a lot
3817 of) time by only doing a quick close, and allowing the OS to reclaim the
3818 memory for us. */
3819 if (! last_file)
3820 bfd_close (file);
3821 else
3822 bfd_close_all_done (file);
3823 }
3824
3825 int
3827 main (int argc, char **argv)
3828 {
3829 int c;
3830 char *target = default_target;
3831 bfd_boolean seenflag = FALSE;
3832
3833 #if defined (HAVE_SETLOCALE)
3834 #if defined (HAVE_LC_MESSAGES)
3835 setlocale (LC_MESSAGES, "");
3836 #endif
3837 setlocale (LC_CTYPE, "");
3838 #endif
3839
3840 bindtextdomain (PACKAGE, LOCALEDIR);
3841 textdomain (PACKAGE);
3842
3843 program_name = *argv;
3844 xmalloc_set_program_name (program_name);
3845 bfd_set_error_program_name (program_name);
3846
3847 START_PROGRESS (program_name, 0);
3848
3849 expandargv (&argc, &argv);
3850
3851 bfd_init ();
3852 set_default_bfd_target ();
3853
3854 while ((c = getopt_long (argc, argv,
3855 "pP:ib:m:M:VvCdDlfFaHhrRtTxsSI:j:wE:zgeGW::",
3856 long_options, (int *) 0))
3857 != EOF)
3858 {
3859 switch (c)
3860 {
3861 case 0:
3862 break; /* We've been given a long option. */
3863 case 'm':
3864 machine = optarg;
3865 break;
3866 case 'M':
3867 {
3868 char *options;
3869 if (disassembler_options)
3870 /* Ignore potential memory leak for now. */
3871 options = concat (disassembler_options, ",",
3872 optarg, (const char *) NULL);
3873 else
3874 options = optarg;
3875 disassembler_options = remove_whitespace_and_extra_commas (options);
3876 }
3877 break;
3878 case 'j':
3879 add_only (optarg);
3880 break;
3881 case 'F':
3882 display_file_offsets = TRUE;
3883 break;
3884 case 'l':
3885 with_line_numbers = TRUE;
3886 break;
3887 case 'b':
3888 target = optarg;
3889 break;
3890 case 'C':
3891 do_demangle = TRUE;
3892 if (optarg != NULL)
3893 {
3894 enum demangling_styles style;
3895
3896 style = cplus_demangle_name_to_style (optarg);
3897 if (style == unknown_demangling)
3898 fatal (_("unknown demangling style `%s'"),
3899 optarg);
3900
3901 cplus_demangle_set_style (style);
3902 }
3903 break;
3904 case 'w':
3905 do_wide = wide_output = TRUE;
3906 break;
3907 case OPTION_ADJUST_VMA:
3908 adjust_section_vma = parse_vma (optarg, "--adjust-vma");
3909 break;
3910 case OPTION_START_ADDRESS:
3911 start_address = parse_vma (optarg, "--start-address");
3912 if ((stop_address != (bfd_vma) -1) && stop_address <= start_address)
3913 fatal (_("error: the start address should be before the end address"));
3914 break;
3915 case OPTION_STOP_ADDRESS:
3916 stop_address = parse_vma (optarg, "--stop-address");
3917 if ((start_address != (bfd_vma) -1) && stop_address <= start_address)
3918 fatal (_("error: the stop address should be after the start address"));
3919 break;
3920 case OPTION_PREFIX:
3921 prefix = optarg;
3922 prefix_length = strlen (prefix);
3923 /* Remove an unnecessary trailing '/' */
3924 while (IS_DIR_SEPARATOR (prefix[prefix_length - 1]))
3925 prefix_length--;
3926 break;
3927 case OPTION_PREFIX_STRIP:
3928 prefix_strip = atoi (optarg);
3929 if (prefix_strip < 0)
3930 fatal (_("error: prefix strip must be non-negative"));
3931 break;
3932 case OPTION_INSN_WIDTH:
3933 insn_width = strtoul (optarg, NULL, 0);
3934 if (insn_width <= 0)
3935 fatal (_("error: instruction width must be positive"));
3936 break;
3937 case OPTION_INLINES:
3938 unwind_inlines = TRUE;
3939 break;
3940 case 'E':
3941 if (strcmp (optarg, "B") == 0)
3942 endian = BFD_ENDIAN_BIG;
3943 else if (strcmp (optarg, "L") == 0)
3944 endian = BFD_ENDIAN_LITTLE;
3945 else
3946 {
3947 nonfatal (_("unrecognized -E option"));
3948 usage (stderr, 1);
3949 }
3950 break;
3951 case OPTION_ENDIAN:
3952 if (strncmp (optarg, "big", strlen (optarg)) == 0)
3953 endian = BFD_ENDIAN_BIG;
3954 else if (strncmp (optarg, "little", strlen (optarg)) == 0)
3955 endian = BFD_ENDIAN_LITTLE;
3956 else
3957 {
3958 non_fatal (_("unrecognized --endian type `%s'"), optarg);
3959 exit_status = 1;
3960 usage (stderr, 1);
3961 }
3962 break;
3963
3964 case 'f':
3965 dump_file_header = TRUE;
3966 seenflag = TRUE;
3967 break;
3968 case 'i':
3969 formats_info = TRUE;
3970 seenflag = TRUE;
3971 break;
3972 case 'I':
3973 add_include_path (optarg);
3974 break;
3975 case 'p':
3976 dump_private_headers = TRUE;
3977 seenflag = TRUE;
3978 break;
3979 case 'P':
3980 dump_private_options = optarg;
3981 seenflag = TRUE;
3982 break;
3983 case 'x':
3984 dump_private_headers = TRUE;
3985 dump_symtab = TRUE;
3986 dump_reloc_info = TRUE;
3987 dump_file_header = TRUE;
3988 dump_ar_hdrs = TRUE;
3989 dump_section_headers = TRUE;
3990 seenflag = TRUE;
3991 break;
3992 case 't':
3993 dump_symtab = TRUE;
3994 seenflag = TRUE;
3995 break;
3996 case 'T':
3997 dump_dynamic_symtab = TRUE;
3998 seenflag = TRUE;
3999 break;
4000 case 'd':
4001 disassemble = TRUE;
4002 seenflag = TRUE;
4003 break;
4004 case 'z':
4005 disassemble_zeroes = TRUE;
4006 break;
4007 case 'D':
4008 disassemble = TRUE;
4009 disassemble_all = TRUE;
4010 seenflag = TRUE;
4011 break;
4012 case 'S':
4013 disassemble = TRUE;
4014 with_source_code = TRUE;
4015 seenflag = TRUE;
4016 break;
4017 case 'g':
4018 dump_debugging = 1;
4019 seenflag = TRUE;
4020 break;
4021 case 'e':
4022 dump_debugging = 1;
4023 dump_debugging_tags = 1;
4024 do_demangle = TRUE;
4025 seenflag = TRUE;
4026 break;
4027 case 'W':
4028 dump_dwarf_section_info = TRUE;
4029 seenflag = TRUE;
4030 if (optarg)
4031 dwarf_select_sections_by_letters (optarg);
4032 else
4033 dwarf_select_sections_all ();
4034 break;
4035 case OPTION_DWARF:
4036 dump_dwarf_section_info = TRUE;
4037 seenflag = TRUE;
4038 if (optarg)
4039 dwarf_select_sections_by_names (optarg);
4040 else
4041 dwarf_select_sections_all ();
4042 break;
4043 case OPTION_DWARF_DEPTH:
4044 {
4045 char *cp;
4046 dwarf_cutoff_level = strtoul (optarg, & cp, 0);
4047 }
4048 break;
4049 case OPTION_DWARF_START:
4050 {
4051 char *cp;
4052 dwarf_start_die = strtoul (optarg, & cp, 0);
4053 suppress_bfd_header = 1;
4054 }
4055 break;
4056 case OPTION_DWARF_CHECK:
4057 dwarf_check = TRUE;
4058 break;
4059 case 'G':
4060 dump_stab_section_info = TRUE;
4061 seenflag = TRUE;
4062 break;
4063 case 's':
4064 dump_section_contents = TRUE;
4065 seenflag = TRUE;
4066 break;
4067 case 'r':
4068 dump_reloc_info = TRUE;
4069 seenflag = TRUE;
4070 break;
4071 case 'R':
4072 dump_dynamic_reloc_info = TRUE;
4073 seenflag = TRUE;
4074 break;
4075 case 'a':
4076 dump_ar_hdrs = TRUE;
4077 seenflag = TRUE;
4078 break;
4079 case 'h':
4080 dump_section_headers = TRUE;
4081 seenflag = TRUE;
4082 break;
4083 case 'v':
4084 case 'V':
4085 show_version = TRUE;
4086 seenflag = TRUE;
4087 break;
4088
4089 case 'H':
4090 usage (stdout, 0);
4091 /* No need to set seenflag or to break - usage() does not return. */
4092 default:
4093 usage (stderr, 1);
4094 }
4095 }
4096
4097 if (show_version)
4098 print_version ("objdump");
4099
4100 if (!seenflag)
4101 usage (stderr, 2);
4102
4103 if (formats_info)
4104 exit_status = display_info ();
4105 else
4106 {
4107 if (optind == argc)
4108 display_file ("a.out", target, TRUE);
4109 else
4110 for (; optind < argc;)
4111 {
4112 display_file (argv[optind], target, optind == argc - 1);
4113 optind++;
4114 }
4115 }
4116
4117 free_only_list ();
4118
4119 END_PROGRESS (program_name);
4120
4121 return exit_status;
4122 }
4123