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