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