* Makefile.am: Run "make dep-am".
[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
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 2, 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
22 #include "bfd.h"
23 #include "bfdver.h"
24 #include "progress.h"
25 #include "bucomm.h"
26 #include "budemang.h"
27 #include "getopt.h"
28 #include "safe-ctype.h"
29 #include "dis-asm.h"
30 #include "libiberty.h"
31 #include "demangle.h"
32 #include "debug.h"
33 #include "budbg.h"
34
35 /* Internal headers for the ELF .stab-dump code - sorry. */
36 #define BYTES_IN_WORD 32
37 #include "aout/aout64.h"
38
39 #ifdef NEED_DECLARATION_FPRINTF
40 /* This is needed by INIT_DISASSEMBLE_INFO. */
41 extern int fprintf PARAMS ((FILE *, const char *, ...));
42 #endif
43
44 /* Exit status. */
45 static int exit_status = 0;
46
47 static char *default_target = NULL; /* default at runtime */
48
49 static int show_version = 0; /* show the version number */
50 static int dump_section_contents; /* -s */
51 static int dump_section_headers; /* -h */
52 static boolean dump_file_header; /* -f */
53 static int dump_symtab; /* -t */
54 static int dump_dynamic_symtab; /* -T */
55 static int dump_reloc_info; /* -r */
56 static int dump_dynamic_reloc_info; /* -R */
57 static int dump_ar_hdrs; /* -a */
58 static int dump_private_headers; /* -p */
59 static int prefix_addresses; /* --prefix-addresses */
60 static int with_line_numbers; /* -l */
61 static boolean with_source_code; /* -S */
62 static int show_raw_insn; /* --show-raw-insn */
63 static int dump_stab_section_info; /* --stabs */
64 static int do_demangle; /* -C, --demangle */
65 static boolean disassemble; /* -d */
66 static boolean disassemble_all; /* -D */
67 static int disassemble_zeroes; /* --disassemble-zeroes */
68 static boolean formats_info; /* -i */
69 static char *only; /* -j secname */
70 static int wide_output; /* -w */
71 static bfd_vma start_address = (bfd_vma) -1; /* --start-address */
72 static bfd_vma stop_address = (bfd_vma) -1; /* --stop-address */
73 static int dump_debugging; /* --debugging */
74 static bfd_vma adjust_section_vma = 0; /* --adjust-vma */
75 static int file_start_context = 0; /* --file-start-context */
76
77 /* Extra info to pass to the disassembler address printing function. */
78 struct objdump_disasm_info
79 {
80 bfd *abfd;
81 asection *sec;
82 boolean require_sec;
83 };
84
85 /* Architecture to disassemble for, or default if NULL. */
86 static char *machine = (char *) NULL;
87
88 /* Target specific options to the disassembler. */
89 static char *disassembler_options = (char *) NULL;
90
91 /* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */
92 static enum bfd_endian endian = BFD_ENDIAN_UNKNOWN;
93
94 /* The symbol table. */
95 static asymbol **syms;
96
97 /* Number of symbols in `syms'. */
98 static long symcount = 0;
99
100 /* The sorted symbol table. */
101 static asymbol **sorted_syms;
102
103 /* Number of symbols in `sorted_syms'. */
104 static long sorted_symcount = 0;
105
106 /* The dynamic symbol table. */
107 static asymbol **dynsyms;
108
109 /* Number of symbols in `dynsyms'. */
110 static long dynsymcount = 0;
111
112 /* Static declarations. */
113
114 static void usage PARAMS ((FILE *, int));
115 static void nonfatal PARAMS ((const char *));
116 static void display_file PARAMS ((char *filename, char *target));
117 static void dump_section_header PARAMS ((bfd *, asection *, PTR));
118 static void dump_headers PARAMS ((bfd *));
119 static void dump_data PARAMS ((bfd *abfd));
120 static void dump_relocs PARAMS ((bfd *abfd));
121 static void dump_dynamic_relocs PARAMS ((bfd * abfd));
122 static void dump_reloc_set PARAMS ((bfd *, asection *, arelent **, long));
123 static void dump_symbols PARAMS ((bfd *abfd, boolean dynamic));
124 static void dump_bfd_header PARAMS ((bfd *));
125 static void dump_bfd_private_header PARAMS ((bfd *));
126 static void dump_bfd PARAMS ((bfd *));
127 static void display_bfd PARAMS ((bfd *abfd));
128 static void display_target_list PARAMS ((void));
129 static void display_info_table PARAMS ((int, int));
130 static void display_target_tables PARAMS ((void));
131 static void display_info PARAMS ((void));
132 static void objdump_print_value
133 PARAMS ((bfd_vma, struct disassemble_info *, boolean));
134 static void objdump_print_symname
135 PARAMS ((bfd *, struct disassemble_info *, asymbol *));
136 static asymbol *find_symbol_for_address
137 PARAMS ((bfd *, asection *, bfd_vma, boolean, long *));
138 static void objdump_print_addr_with_sym
139 PARAMS ((bfd *, asection *, asymbol *, bfd_vma,
140 struct disassemble_info *, boolean));
141 static void objdump_print_addr
142 PARAMS ((bfd_vma, struct disassemble_info *, boolean));
143 static void objdump_print_address
144 PARAMS ((bfd_vma, struct disassemble_info *));
145 static int objdump_symbol_at_address
146 PARAMS ((bfd_vma, struct disassemble_info *));
147 static void show_line PARAMS ((bfd *, asection *, bfd_vma));
148 static void disassemble_bytes
149 PARAMS ((struct disassemble_info *, disassembler_ftype, boolean,
150 bfd_byte *, bfd_vma, bfd_vma, arelent ***, arelent **));
151 static void disassemble_data PARAMS ((bfd *));
152 static const char *endian_string PARAMS ((enum bfd_endian));
153 static asymbol ** slurp_symtab PARAMS ((bfd *));
154 static asymbol ** slurp_dynamic_symtab PARAMS ((bfd *));
155 static long remove_useless_symbols PARAMS ((asymbol **, long));
156 static int compare_symbols PARAMS ((const PTR, const PTR));
157 static int compare_relocs PARAMS ((const PTR, const PTR));
158 static void dump_stabs PARAMS ((bfd *));
159 static boolean read_section_stabs PARAMS ((bfd *, const char *, const char *));
160 static void print_section_stabs PARAMS ((bfd *, const char *, const char *));
161 \f
162 static void
163 usage (stream, status)
164 FILE *stream;
165 int status;
166 {
167 fprintf (stream, _("Usage: %s <option(s)> <file(s)>\n"), program_name);
168 fprintf (stream, _(" Display information from object <file(s)>.\n"));
169 fprintf (stream, _(" At least one of the following switches must be given:\n"));
170 fprintf (stream, _("\
171 -a, --archive-headers Display archive header information\n\
172 -f, --file-headers Display the contents of the overall file header\n\
173 -p, --private-headers Display object format specific file header contents\n\
174 -h, --[section-]headers Display the contents of the section headers\n\
175 -x, --all-headers Display the contents of all headers\n\
176 -d, --disassemble Display assembler contents of executable sections\n\
177 -D, --disassemble-all Display assembler contents of all sections\n\
178 -S, --source Intermix source code with disassembly\n\
179 -s, --full-contents Display the full contents of all sections requested\n\
180 -g, --debugging Display debug information in object file\n\
181 -G, --stabs Display (in raw form) any STABS info in the file\n\
182 -t, --syms Display the contents of the symbol table(s)\n\
183 -T, --dynamic-syms Display the contents of the dynamic symbol table\n\
184 -r, --reloc Display the relocation entries in the file\n\
185 -R, --dynamic-reloc Display the dynamic relocation entries in the file\n\
186 -v, --version Display this program's version number\n\
187 -i, --info List object formats and architectures supported\n\
188 -H, --help Display this information\n\
189 "));
190 if (status != 2)
191 {
192 fprintf (stream, _("\n The following switches are optional:\n"));
193 fprintf (stream, _("\
194 -b, --target=BFDNAME Specify the target object format as BFDNAME\n\
195 -m, --architecture=MACHINE Specify the target architecture as MACHINE\n\
196 -j, --section=NAME Only display information for section NAME\n\
197 -M, --disassembler-options=OPT Pass text OPT on to the disassembler\n\
198 -EB --endian=big Assume big endian format when disassembling\n\
199 -EL --endian=little Assume little endian format when disassembling\n\
200 --file-start-context Include context from start of file (with -S)\n\
201 -l, --line-numbers Include line numbers and filenames in output\n\
202 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n\
203 The STYLE, if specified, can be `auto', 'gnu',\n\
204 'lucid', 'arm', 'hp', 'edg', or 'gnu-v3'\n\
205 -w, --wide Format output for more than 80 columns\n\
206 -z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling\n\
207 --start-address=ADDR Only process data whoes address is >= ADDR\n\
208 --stop-address=ADDR Only process data whoes address is <= ADDR\n\
209 --prefix-addresses Print complete address alongside disassembly\n\
210 --[no-]show-raw-insn Display hex alongside symbolic disassembly\n\
211 --adjust-vma=OFFSET Add OFFSET to all displayed section addresses\n\
212 \n"));
213 list_supported_targets (program_name, stream);
214 list_supported_architectures (program_name, stream);
215
216 disassembler_usage (stream);
217 }
218 if (status == 0)
219 fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
220 exit (status);
221 }
222
223 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
224
225 #define OPTION_ENDIAN (150)
226 #define OPTION_START_ADDRESS (OPTION_ENDIAN + 1)
227 #define OPTION_STOP_ADDRESS (OPTION_START_ADDRESS + 1)
228 #define OPTION_ADJUST_VMA (OPTION_STOP_ADDRESS + 1)
229
230 static struct option long_options[]=
231 {
232 {"adjust-vma", required_argument, NULL, OPTION_ADJUST_VMA},
233 {"all-headers", no_argument, NULL, 'x'},
234 {"private-headers", no_argument, NULL, 'p'},
235 {"architecture", required_argument, NULL, 'm'},
236 {"archive-headers", no_argument, NULL, 'a'},
237 {"debugging", no_argument, NULL, 'g'},
238 {"demangle", optional_argument, NULL, 'C'},
239 {"disassemble", no_argument, NULL, 'd'},
240 {"disassemble-all", no_argument, NULL, 'D'},
241 {"disassembler-options", required_argument, NULL, 'M'},
242 {"disassemble-zeroes", no_argument, NULL, 'z'},
243 {"dynamic-reloc", no_argument, NULL, 'R'},
244 {"dynamic-syms", no_argument, NULL, 'T'},
245 {"endian", required_argument, NULL, OPTION_ENDIAN},
246 {"file-headers", no_argument, NULL, 'f'},
247 {"file-start-context", no_argument, &file_start_context, 1},
248 {"full-contents", no_argument, NULL, 's'},
249 {"headers", no_argument, NULL, 'h'},
250 {"help", no_argument, NULL, 'H'},
251 {"info", no_argument, NULL, 'i'},
252 {"line-numbers", no_argument, NULL, 'l'},
253 {"no-show-raw-insn", no_argument, &show_raw_insn, -1},
254 {"prefix-addresses", no_argument, &prefix_addresses, 1},
255 {"reloc", no_argument, NULL, 'r'},
256 {"section", required_argument, NULL, 'j'},
257 {"section-headers", no_argument, NULL, 'h'},
258 {"show-raw-insn", no_argument, &show_raw_insn, 1},
259 {"source", no_argument, NULL, 'S'},
260 {"stabs", no_argument, NULL, 'G'},
261 {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
262 {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
263 {"syms", no_argument, NULL, 't'},
264 {"target", required_argument, NULL, 'b'},
265 {"version", no_argument, NULL, 'V'},
266 {"wide", no_argument, NULL, 'w'},
267 {0, no_argument, 0, 0}
268 };
269 \f
270 static void
271 nonfatal (msg)
272 const char *msg;
273 {
274 bfd_nonfatal (msg);
275 exit_status = 1;
276 }
277 \f
278 static void
279 dump_section_header (abfd, section, ignored)
280 bfd *abfd ATTRIBUTE_UNUSED;
281 asection *section;
282 PTR ignored ATTRIBUTE_UNUSED;
283 {
284 char *comma = "";
285 unsigned int opb = bfd_octets_per_byte (abfd);
286
287 printf ("%3d %-13s %08lx ", section->index,
288 bfd_get_section_name (abfd, section),
289 (unsigned long) bfd_section_size (abfd, section) / opb);
290 bfd_printf_vma (abfd, bfd_get_section_vma (abfd, section));
291 printf (" ");
292 bfd_printf_vma (abfd, section->lma);
293 printf (" %08lx 2**%u", (unsigned long) section->filepos,
294 bfd_get_section_alignment (abfd, section));
295 if (! wide_output)
296 printf ("\n ");
297 printf (" ");
298
299 #define PF(x, y) \
300 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
301
302 PF (SEC_HAS_CONTENTS, "CONTENTS");
303 PF (SEC_ALLOC, "ALLOC");
304 PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
305 PF (SEC_LOAD, "LOAD");
306 PF (SEC_RELOC, "RELOC");
307 PF (SEC_READONLY, "READONLY");
308 PF (SEC_CODE, "CODE");
309 PF (SEC_DATA, "DATA");
310 PF (SEC_ROM, "ROM");
311 PF (SEC_DEBUGGING, "DEBUGGING");
312 PF (SEC_NEVER_LOAD, "NEVER_LOAD");
313 PF (SEC_EXCLUDE, "EXCLUDE");
314 PF (SEC_SORT_ENTRIES, "SORT_ENTRIES");
315 PF (SEC_BLOCK, "BLOCK");
316 PF (SEC_CLINK, "CLINK");
317 PF (SEC_SMALL_DATA, "SMALL_DATA");
318 PF (SEC_SHARED, "SHARED");
319 PF (SEC_ARCH_BIT_0, "ARCH_BIT_0");
320 PF (SEC_THREAD_LOCAL, "THREAD_LOCAL");
321
322 if ((section->flags & SEC_LINK_ONCE) != 0)
323 {
324 const char *ls;
325
326 switch (section->flags & SEC_LINK_DUPLICATES)
327 {
328 default:
329 abort ();
330 case SEC_LINK_DUPLICATES_DISCARD:
331 ls = "LINK_ONCE_DISCARD";
332 break;
333 case SEC_LINK_DUPLICATES_ONE_ONLY:
334 ls = "LINK_ONCE_ONE_ONLY";
335 break;
336 case SEC_LINK_DUPLICATES_SAME_SIZE:
337 ls = "LINK_ONCE_SAME_SIZE";
338 break;
339 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
340 ls = "LINK_ONCE_SAME_CONTENTS";
341 break;
342 }
343 printf ("%s%s", comma, ls);
344
345 if (section->comdat != NULL)
346 printf (" (COMDAT %s %ld)", section->comdat->name,
347 section->comdat->symbol);
348
349 comma = ", ";
350 }
351
352 printf ("\n");
353 #undef PF
354 }
355
356 static void
357 dump_headers (abfd)
358 bfd *abfd;
359 {
360 printf (_("Sections:\n"));
361
362 #ifndef BFD64
363 printf (_("Idx Name Size VMA LMA File off Algn"));
364 #else
365 /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */
366 if (bfd_get_arch_size (abfd) == 32)
367 printf (_("Idx Name Size VMA LMA File off Algn"));
368 else
369 printf (_("Idx Name Size VMA LMA File off Algn"));
370 #endif
371
372 if (wide_output)
373 printf (_(" Flags"));
374 if (abfd->flags & HAS_LOAD_PAGE)
375 printf (_(" Pg"));
376 printf ("\n");
377
378 bfd_map_over_sections (abfd, dump_section_header, (PTR) NULL);
379 }
380 \f
381 static asymbol **
382 slurp_symtab (abfd)
383 bfd *abfd;
384 {
385 asymbol **sy = (asymbol **) NULL;
386 long storage;
387
388 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
389 {
390 symcount = 0;
391 return NULL;
392 }
393
394 storage = bfd_get_symtab_upper_bound (abfd);
395 if (storage < 0)
396 bfd_fatal (bfd_get_filename (abfd));
397 if (storage)
398 sy = (asymbol **) xmalloc (storage);
399
400 symcount = bfd_canonicalize_symtab (abfd, sy);
401 if (symcount < 0)
402 bfd_fatal (bfd_get_filename (abfd));
403 return sy;
404 }
405
406 /* Read in the dynamic symbols. */
407
408 static asymbol **
409 slurp_dynamic_symtab (abfd)
410 bfd *abfd;
411 {
412 asymbol **sy = (asymbol **) NULL;
413 long storage;
414
415 storage = bfd_get_dynamic_symtab_upper_bound (abfd);
416 if (storage < 0)
417 {
418 if (!(bfd_get_file_flags (abfd) & DYNAMIC))
419 {
420 non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd));
421 dynsymcount = 0;
422 return NULL;
423 }
424
425 bfd_fatal (bfd_get_filename (abfd));
426 }
427 if (storage)
428 sy = (asymbol **) xmalloc (storage);
429
430 dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
431 if (dynsymcount < 0)
432 bfd_fatal (bfd_get_filename (abfd));
433 return sy;
434 }
435
436 /* Filter out (in place) symbols that are useless for disassembly.
437 COUNT is the number of elements in SYMBOLS.
438 Return the number of useful symbols. */
439
440 static long
441 remove_useless_symbols (symbols, count)
442 asymbol **symbols;
443 long count;
444 {
445 register asymbol **in_ptr = symbols, **out_ptr = symbols;
446
447 while (--count >= 0)
448 {
449 asymbol *sym = *in_ptr++;
450
451 if (sym->name == NULL || sym->name[0] == '\0')
452 continue;
453 if (sym->flags & (BSF_DEBUGGING))
454 continue;
455 if (bfd_is_und_section (sym->section)
456 || bfd_is_com_section (sym->section))
457 continue;
458
459 *out_ptr++ = sym;
460 }
461 return out_ptr - symbols;
462 }
463
464 /* Sort symbols into value order. */
465
466 static int
467 compare_symbols (ap, bp)
468 const PTR ap;
469 const PTR bp;
470 {
471 const asymbol *a = *(const asymbol **)ap;
472 const asymbol *b = *(const asymbol **)bp;
473 const char *an, *bn;
474 size_t anl, bnl;
475 boolean af, bf;
476 flagword aflags, bflags;
477
478 if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
479 return 1;
480 else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
481 return -1;
482
483 if (a->section > b->section)
484 return 1;
485 else if (a->section < b->section)
486 return -1;
487
488 an = bfd_asymbol_name (a);
489 bn = bfd_asymbol_name (b);
490 anl = strlen (an);
491 bnl = strlen (bn);
492
493 /* The symbols gnu_compiled and gcc2_compiled convey no real
494 information, so put them after other symbols with the same value. */
495
496 af = (strstr (an, "gnu_compiled") != NULL
497 || strstr (an, "gcc2_compiled") != NULL);
498 bf = (strstr (bn, "gnu_compiled") != NULL
499 || strstr (bn, "gcc2_compiled") != NULL);
500
501 if (af && ! bf)
502 return 1;
503 if (! af && bf)
504 return -1;
505
506 /* We use a heuristic for the file name, to try to sort it after
507 more useful symbols. It may not work on non Unix systems, but it
508 doesn't really matter; the only difference is precisely which
509 symbol names get printed. */
510
511 #define file_symbol(s, sn, snl) \
512 (((s)->flags & BSF_FILE) != 0 \
513 || ((sn)[(snl) - 2] == '.' \
514 && ((sn)[(snl) - 1] == 'o' \
515 || (sn)[(snl) - 1] == 'a')))
516
517 af = file_symbol (a, an, anl);
518 bf = file_symbol (b, bn, bnl);
519
520 if (af && ! bf)
521 return 1;
522 if (! af && bf)
523 return -1;
524
525 /* Try to sort global symbols before local symbols before function
526 symbols before debugging symbols. */
527
528 aflags = a->flags;
529 bflags = b->flags;
530
531 if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING))
532 {
533 if ((aflags & BSF_DEBUGGING) != 0)
534 return 1;
535 else
536 return -1;
537 }
538 if ((aflags & BSF_FUNCTION) != (bflags & BSF_FUNCTION))
539 {
540 if ((aflags & BSF_FUNCTION) != 0)
541 return -1;
542 else
543 return 1;
544 }
545 if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL))
546 {
547 if ((aflags & BSF_LOCAL) != 0)
548 return 1;
549 else
550 return -1;
551 }
552 if ((aflags & BSF_GLOBAL) != (bflags & BSF_GLOBAL))
553 {
554 if ((aflags & BSF_GLOBAL) != 0)
555 return -1;
556 else
557 return 1;
558 }
559
560 /* Symbols that start with '.' might be section names, so sort them
561 after symbols that don't start with '.'. */
562 if (an[0] == '.' && bn[0] != '.')
563 return 1;
564 if (an[0] != '.' && bn[0] == '.')
565 return -1;
566
567 /* Finally, if we can't distinguish them in any other way, try to
568 get consistent results by sorting the symbols by name. */
569 return strcmp (an, bn);
570 }
571
572 /* Sort relocs into address order. */
573
574 static int
575 compare_relocs (ap, bp)
576 const PTR ap;
577 const PTR bp;
578 {
579 const arelent *a = *(const arelent **)ap;
580 const arelent *b = *(const arelent **)bp;
581
582 if (a->address > b->address)
583 return 1;
584 else if (a->address < b->address)
585 return -1;
586
587 /* So that associated relocations tied to the same address show up
588 in the correct order, we don't do any further sorting. */
589 if (a > b)
590 return 1;
591 else if (a < b)
592 return -1;
593 else
594 return 0;
595 }
596
597 /* Print VMA to STREAM. If SKIP_ZEROES is true, omit leading zeroes. */
598
599 static void
600 objdump_print_value (vma, info, skip_zeroes)
601 bfd_vma vma;
602 struct disassemble_info *info;
603 boolean skip_zeroes;
604 {
605 char buf[30];
606 char *p;
607 struct objdump_disasm_info *aux
608 = (struct objdump_disasm_info *) info->application_data;
609
610 bfd_sprintf_vma (aux->abfd, buf, vma);
611 if (! skip_zeroes)
612 p = buf;
613 else
614 {
615 for (p = buf; *p == '0'; ++p)
616 ;
617 if (*p == '\0')
618 --p;
619 }
620 (*info->fprintf_func) (info->stream, "%s", p);
621 }
622
623 /* Print the name of a symbol. */
624
625 static void
626 objdump_print_symname (abfd, info, sym)
627 bfd *abfd;
628 struct disassemble_info *info;
629 asymbol *sym;
630 {
631 char *alloc;
632 const char *name;
633
634 alloc = NULL;
635 name = bfd_asymbol_name (sym);
636 if (do_demangle && name[0] != '\0')
637 {
638 /* Demangle the name. */
639 alloc = demangle (abfd, name);
640 name = alloc;
641 }
642
643 if (info != NULL)
644 (*info->fprintf_func) (info->stream, "%s", name);
645 else
646 printf ("%s", name);
647
648 if (alloc != NULL)
649 free (alloc);
650 }
651
652 /* Locate a symbol given a bfd, a section, and a VMA. If REQUIRE_SEC
653 is true, then always require the symbol to be in the section. This
654 returns NULL if there is no suitable symbol. If PLACE is not NULL,
655 then *PLACE is set to the index of the symbol in sorted_syms. */
656
657 static asymbol *
658 find_symbol_for_address (abfd, sec, vma, require_sec, place)
659 bfd *abfd;
660 asection *sec;
661 bfd_vma vma;
662 boolean require_sec;
663 long *place;
664 {
665 /* @@ Would it speed things up to cache the last two symbols returned,
666 and maybe their address ranges? For many processors, only one memory
667 operand can be present at a time, so the 2-entry cache wouldn't be
668 constantly churned by code doing heavy memory accesses. */
669
670 /* Indices in `sorted_syms'. */
671 long min = 0;
672 long max = sorted_symcount;
673 long thisplace;
674 unsigned int opb = bfd_octets_per_byte (abfd);
675
676 if (sorted_symcount < 1)
677 return NULL;
678
679 /* Perform a binary search looking for the closest symbol to the
680 required value. We are searching the range (min, max]. */
681 while (min + 1 < max)
682 {
683 asymbol *sym;
684
685 thisplace = (max + min) / 2;
686 sym = sorted_syms[thisplace];
687
688 if (bfd_asymbol_value (sym) > vma)
689 max = thisplace;
690 else if (bfd_asymbol_value (sym) < vma)
691 min = thisplace;
692 else
693 {
694 min = thisplace;
695 break;
696 }
697 }
698
699 /* The symbol we want is now in min, the low end of the range we
700 were searching. If there are several symbols with the same
701 value, we want the first one. */
702 thisplace = min;
703 while (thisplace > 0
704 && (bfd_asymbol_value (sorted_syms[thisplace])
705 == bfd_asymbol_value (sorted_syms[thisplace - 1])))
706 --thisplace;
707
708 /* If the file is relocateable, and the symbol could be from this
709 section, prefer a symbol from this section over symbols from
710 others, even if the other symbol's value might be closer.
711
712 Note that this may be wrong for some symbol references if the
713 sections have overlapping memory ranges, but in that case there's
714 no way to tell what's desired without looking at the relocation
715 table. */
716
717 if (sorted_syms[thisplace]->section != sec
718 && (require_sec
719 || ((abfd->flags & HAS_RELOC) != 0
720 && vma >= bfd_get_section_vma (abfd, sec)
721 && vma < (bfd_get_section_vma (abfd, sec)
722 + bfd_section_size (abfd, sec) / opb))))
723 {
724 long i;
725
726 for (i = thisplace + 1; i < sorted_symcount; i++)
727 {
728 if (bfd_asymbol_value (sorted_syms[i])
729 != bfd_asymbol_value (sorted_syms[thisplace]))
730 break;
731 }
732 --i;
733 for (; i >= 0; i--)
734 {
735 if (sorted_syms[i]->section == sec
736 && (i == 0
737 || sorted_syms[i - 1]->section != sec
738 || (bfd_asymbol_value (sorted_syms[i])
739 != bfd_asymbol_value (sorted_syms[i - 1]))))
740 {
741 thisplace = i;
742 break;
743 }
744 }
745
746 if (sorted_syms[thisplace]->section != sec)
747 {
748 /* We didn't find a good symbol with a smaller value.
749 Look for one with a larger value. */
750 for (i = thisplace + 1; i < sorted_symcount; i++)
751 {
752 if (sorted_syms[i]->section == sec)
753 {
754 thisplace = i;
755 break;
756 }
757 }
758 }
759
760 if (sorted_syms[thisplace]->section != sec
761 && (require_sec
762 || ((abfd->flags & HAS_RELOC) != 0
763 && vma >= bfd_get_section_vma (abfd, sec)
764 && vma < (bfd_get_section_vma (abfd, sec)
765 + bfd_section_size (abfd, sec)))))
766 {
767 /* There is no suitable symbol. */
768 return NULL;
769 }
770 }
771
772 if (place != NULL)
773 *place = thisplace;
774
775 return sorted_syms[thisplace];
776 }
777
778 /* Print an address to INFO symbolically. */
779
780 static void
781 objdump_print_addr_with_sym (abfd, sec, sym, vma, info, skip_zeroes)
782 bfd *abfd;
783 asection *sec;
784 asymbol *sym;
785 bfd_vma vma;
786 struct disassemble_info *info;
787 boolean skip_zeroes;
788 {
789 objdump_print_value (vma, info, skip_zeroes);
790
791 if (sym == NULL)
792 {
793 bfd_vma secaddr;
794
795 (*info->fprintf_func) (info->stream, " <%s",
796 bfd_get_section_name (abfd, sec));
797 secaddr = bfd_get_section_vma (abfd, sec);
798 if (vma < secaddr)
799 {
800 (*info->fprintf_func) (info->stream, "-0x");
801 objdump_print_value (secaddr - vma, info, true);
802 }
803 else if (vma > secaddr)
804 {
805 (*info->fprintf_func) (info->stream, "+0x");
806 objdump_print_value (vma - secaddr, info, true);
807 }
808 (*info->fprintf_func) (info->stream, ">");
809 }
810 else
811 {
812 (*info->fprintf_func) (info->stream, " <");
813 objdump_print_symname (abfd, info, sym);
814 if (bfd_asymbol_value (sym) > vma)
815 {
816 (*info->fprintf_func) (info->stream, "-0x");
817 objdump_print_value (bfd_asymbol_value (sym) - vma, info, true);
818 }
819 else if (vma > bfd_asymbol_value (sym))
820 {
821 (*info->fprintf_func) (info->stream, "+0x");
822 objdump_print_value (vma - bfd_asymbol_value (sym), info, true);
823 }
824 (*info->fprintf_func) (info->stream, ">");
825 }
826 }
827
828 /* Print VMA to INFO, symbolically if possible. If SKIP_ZEROES is
829 true, don't output leading zeroes. */
830
831 static void
832 objdump_print_addr (vma, info, skip_zeroes)
833 bfd_vma vma;
834 struct disassemble_info *info;
835 boolean skip_zeroes;
836 {
837 struct objdump_disasm_info *aux;
838 asymbol *sym;
839
840 if (sorted_symcount < 1)
841 {
842 (*info->fprintf_func) (info->stream, "0x");
843 objdump_print_value (vma, info, skip_zeroes);
844 return;
845 }
846
847 aux = (struct objdump_disasm_info *) info->application_data;
848 sym = find_symbol_for_address (aux->abfd, aux->sec, vma, aux->require_sec,
849 (long *) NULL);
850 objdump_print_addr_with_sym (aux->abfd, aux->sec, sym, vma, info,
851 skip_zeroes);
852 }
853
854 /* Print VMA to INFO. This function is passed to the disassembler
855 routine. */
856
857 static void
858 objdump_print_address (vma, info)
859 bfd_vma vma;
860 struct disassemble_info *info;
861 {
862 objdump_print_addr (vma, info, ! prefix_addresses);
863 }
864
865 /* Determine of the given address has a symbol associated with it. */
866
867 static int
868 objdump_symbol_at_address (vma, info)
869 bfd_vma vma;
870 struct disassemble_info * info;
871 {
872 struct objdump_disasm_info * aux;
873 asymbol * sym;
874
875 /* No symbols - do not bother checking. */
876 if (sorted_symcount < 1)
877 return 0;
878
879 aux = (struct objdump_disasm_info *) info->application_data;
880 sym = find_symbol_for_address (aux->abfd, aux->sec, vma, aux->require_sec,
881 (long *) NULL);
882
883 return (sym != NULL && (bfd_asymbol_value (sym) == vma));
884 }
885
886 /* Hold the last function name and the last line number we displayed
887 in a disassembly. */
888
889 static char *prev_functionname;
890 static unsigned int prev_line;
891
892 /* We keep a list of all files that we have seen when doing a
893 dissassembly with source, so that we know how much of the file to
894 display. This can be important for inlined functions. */
895
896 struct print_file_list
897 {
898 struct print_file_list *next;
899 char *filename;
900 unsigned int line;
901 FILE *f;
902 };
903
904 static struct print_file_list *print_files;
905
906 /* The number of preceding context lines to show when we start
907 displaying a file for the first time. */
908
909 #define SHOW_PRECEDING_CONTEXT_LINES (5)
910
911 /* Skip ahead to a given line in a file, optionally printing each
912 line. */
913
914 static void
915 skip_to_line PARAMS ((struct print_file_list *, unsigned int, boolean));
916
917 static void
918 skip_to_line (p, line, show)
919 struct print_file_list *p;
920 unsigned int line;
921 boolean show;
922 {
923 while (p->line < line)
924 {
925 char buf[100];
926
927 if (fgets (buf, sizeof buf, p->f) == NULL)
928 {
929 fclose (p->f);
930 p->f = NULL;
931 break;
932 }
933
934 if (show)
935 printf ("%s", buf);
936
937 if (strchr (buf, '\n') != NULL)
938 ++p->line;
939 }
940 }
941
942 /* Show the line number, or the source line, in a dissassembly
943 listing. */
944
945 static void
946 show_line (abfd, section, addr_offset)
947 bfd *abfd;
948 asection *section;
949 bfd_vma addr_offset;
950 {
951 const char *filename;
952 const char *functionname;
953 unsigned int line;
954
955 if (! with_line_numbers && ! with_source_code)
956 return;
957
958 if (! bfd_find_nearest_line (abfd, section, syms, addr_offset, &filename,
959 &functionname, &line))
960 return;
961
962 if (filename != NULL && *filename == '\0')
963 filename = NULL;
964 if (functionname != NULL && *functionname == '\0')
965 functionname = NULL;
966
967 if (with_line_numbers)
968 {
969 if (functionname != NULL
970 && (prev_functionname == NULL
971 || strcmp (functionname, prev_functionname) != 0))
972 printf ("%s():\n", functionname);
973 if (line > 0 && line != prev_line)
974 printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
975 }
976
977 if (with_source_code
978 && filename != NULL
979 && line > 0)
980 {
981 struct print_file_list **pp, *p;
982
983 for (pp = &print_files; *pp != NULL; pp = &(*pp)->next)
984 if (strcmp ((*pp)->filename, filename) == 0)
985 break;
986 p = *pp;
987
988 if (p != NULL)
989 {
990 if (p != print_files)
991 {
992 int l;
993
994 /* We have reencountered a file name which we saw
995 earlier. This implies that either we are dumping out
996 code from an included file, or the same file was
997 linked in more than once. There are two common cases
998 of an included file: inline functions in a header
999 file, and a bison or flex skeleton file. In the
1000 former case we want to just start printing (but we
1001 back up a few lines to give context); in the latter
1002 case we want to continue from where we left off. I
1003 can't think of a good way to distinguish the cases,
1004 so I used a heuristic based on the file name. */
1005 if (strcmp (p->filename + strlen (p->filename) - 2, ".h") != 0)
1006 l = p->line;
1007 else
1008 {
1009 l = line - SHOW_PRECEDING_CONTEXT_LINES;
1010 if (l < 0)
1011 l = 0;
1012 }
1013
1014 if (p->f == NULL)
1015 {
1016 p->f = fopen (p->filename, "r");
1017 p->line = 0;
1018 }
1019 if (p->f != NULL)
1020 skip_to_line (p, l, false);
1021
1022 if (print_files->f != NULL)
1023 {
1024 fclose (print_files->f);
1025 print_files->f = NULL;
1026 }
1027 }
1028
1029 if (p->f != NULL)
1030 {
1031 skip_to_line (p, line, true);
1032 *pp = p->next;
1033 p->next = print_files;
1034 print_files = p;
1035 }
1036 }
1037 else
1038 {
1039 FILE *f;
1040
1041 f = fopen (filename, "r");
1042 if (f != NULL)
1043 {
1044 int l;
1045
1046 p = ((struct print_file_list *)
1047 xmalloc (sizeof (struct print_file_list)));
1048 p->filename = xmalloc (strlen (filename) + 1);
1049 strcpy (p->filename, filename);
1050 p->line = 0;
1051 p->f = f;
1052
1053 if (print_files != NULL && print_files->f != NULL)
1054 {
1055 fclose (print_files->f);
1056 print_files->f = NULL;
1057 }
1058 p->next = print_files;
1059 print_files = p;
1060
1061 if (file_start_context)
1062 l = 0;
1063 else
1064 l = line - SHOW_PRECEDING_CONTEXT_LINES;
1065 if (l < 0)
1066 l = 0;
1067 skip_to_line (p, l, false);
1068 if (p->f != NULL)
1069 skip_to_line (p, line, true);
1070 }
1071 }
1072 }
1073
1074 if (functionname != NULL
1075 && (prev_functionname == NULL
1076 || strcmp (functionname, prev_functionname) != 0))
1077 {
1078 if (prev_functionname != NULL)
1079 free (prev_functionname);
1080 prev_functionname = xmalloc (strlen (functionname) + 1);
1081 strcpy (prev_functionname, functionname);
1082 }
1083
1084 if (line > 0 && line != prev_line)
1085 prev_line = line;
1086 }
1087
1088 /* Pseudo FILE object for strings. */
1089 typedef struct
1090 {
1091 char *buffer;
1092 size_t size;
1093 char *current;
1094 } SFILE;
1095
1096 /* sprintf to a "stream" */
1097
1098 static int
1099 objdump_sprintf VPARAMS ((SFILE *f, const char *format, ...))
1100 {
1101 char *buf;
1102 size_t n;
1103
1104 VA_OPEN (args, format);
1105 VA_FIXEDARG (args, SFILE *, f);
1106 VA_FIXEDARG (args, const char *, format);
1107
1108 vasprintf (&buf, format, args);
1109
1110 if (buf == NULL)
1111 {
1112 va_end (args);
1113 fatal (_("Out of virtual memory"));
1114 }
1115
1116 n = strlen (buf);
1117
1118 while ((size_t) ((f->buffer + f->size) - f->current) < n + 1)
1119 {
1120 size_t curroff;
1121
1122 curroff = f->current - f->buffer;
1123 f->size *= 2;
1124 f->buffer = xrealloc (f->buffer, f->size);
1125 f->current = f->buffer + curroff;
1126 }
1127
1128 memcpy (f->current, buf, n);
1129 f->current += n;
1130 f->current[0] = '\0';
1131
1132 free (buf);
1133
1134 VA_CLOSE (args);
1135 return n;
1136 }
1137
1138 /* The number of zeroes we want to see before we start skipping them.
1139 The number is arbitrarily chosen. */
1140
1141 #ifndef SKIP_ZEROES
1142 #define SKIP_ZEROES (8)
1143 #endif
1144
1145 /* The number of zeroes to skip at the end of a section. If the
1146 number of zeroes at the end is between SKIP_ZEROES_AT_END and
1147 SKIP_ZEROES, they will be disassembled. If there are fewer than
1148 SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic
1149 attempt to avoid disassembling zeroes inserted by section
1150 alignment. */
1151
1152 #ifndef SKIP_ZEROES_AT_END
1153 #define SKIP_ZEROES_AT_END (3)
1154 #endif
1155
1156 /* Disassemble some data in memory between given values. */
1157
1158 static void
1159 disassemble_bytes (info, disassemble_fn, insns, data,
1160 start_offset, stop_offset, relppp,
1161 relppend)
1162 struct disassemble_info *info;
1163 disassembler_ftype disassemble_fn;
1164 boolean insns;
1165 bfd_byte *data;
1166 bfd_vma start_offset;
1167 bfd_vma stop_offset;
1168 arelent ***relppp;
1169 arelent **relppend;
1170 {
1171 struct objdump_disasm_info *aux;
1172 asection *section;
1173 int octets_per_line;
1174 boolean done_dot;
1175 int skip_addr_chars;
1176 bfd_vma addr_offset;
1177 int opb = info->octets_per_byte;
1178
1179 aux = (struct objdump_disasm_info *) info->application_data;
1180 section = aux->sec;
1181
1182 if (insns)
1183 octets_per_line = 4;
1184 else
1185 octets_per_line = 16;
1186
1187 /* Figure out how many characters to skip at the start of an
1188 address, to make the disassembly look nicer. We discard leading
1189 zeroes in chunks of 4, ensuring that there is always a leading
1190 zero remaining. */
1191 skip_addr_chars = 0;
1192 if (! prefix_addresses)
1193 {
1194 char buf[30];
1195 char *s;
1196
1197 bfd_sprintf_vma
1198 (aux->abfd, buf,
1199 (section->vma
1200 + bfd_section_size (section->owner, section) / opb));
1201 s = buf;
1202 while (s[0] == '0' && s[1] == '0' && s[2] == '0' && s[3] == '0'
1203 && s[4] == '0')
1204 {
1205 skip_addr_chars += 4;
1206 s += 4;
1207 }
1208 }
1209
1210 info->insn_info_valid = 0;
1211
1212 done_dot = false;
1213 addr_offset = start_offset;
1214 while (addr_offset < stop_offset)
1215 {
1216 bfd_vma z;
1217 int octets = 0;
1218 boolean need_nl = false;
1219
1220 /* If we see more than SKIP_ZEROES octets of zeroes, we just
1221 print `...'. */
1222 for (z = addr_offset * opb; z < stop_offset * opb; z++)
1223 if (data[z] != 0)
1224 break;
1225 if (! disassemble_zeroes
1226 && (info->insn_info_valid == 0
1227 || info->branch_delay_insns == 0)
1228 && (z - addr_offset * opb >= SKIP_ZEROES
1229 || (z == stop_offset * opb &&
1230 z - addr_offset * opb < SKIP_ZEROES_AT_END)))
1231 {
1232 printf ("\t...\n");
1233
1234 /* If there are more nonzero octets to follow, we only skip
1235 zeroes in multiples of 4, to try to avoid running over
1236 the start of an instruction which happens to start with
1237 zero. */
1238 if (z != stop_offset * opb)
1239 z = addr_offset * opb + ((z - addr_offset * opb) &~ 3);
1240
1241 octets = z - addr_offset * opb;
1242 }
1243 else
1244 {
1245 char buf[50];
1246 SFILE sfile;
1247 int bpc = 0;
1248 int pb = 0;
1249
1250 done_dot = false;
1251
1252 if (with_line_numbers || with_source_code)
1253 /* The line number tables will refer to unadjusted
1254 section VMAs, so we must undo any VMA modifications
1255 when calling show_line. */
1256 show_line (aux->abfd, section, addr_offset - adjust_section_vma);
1257
1258 if (! prefix_addresses)
1259 {
1260 char *s;
1261
1262 bfd_sprintf_vma (aux->abfd, buf, section->vma + addr_offset);
1263 for (s = buf + skip_addr_chars; *s == '0'; s++)
1264 *s = ' ';
1265 if (*s == '\0')
1266 *--s = '0';
1267 printf ("%s:\t", buf + skip_addr_chars);
1268 }
1269 else
1270 {
1271 aux->require_sec = true;
1272 objdump_print_address (section->vma + addr_offset, info);
1273 aux->require_sec = false;
1274 putchar (' ');
1275 }
1276
1277 if (insns)
1278 {
1279 sfile.size = 120;
1280 sfile.buffer = xmalloc (sfile.size);
1281 sfile.current = sfile.buffer;
1282 info->fprintf_func = (fprintf_ftype) objdump_sprintf;
1283 info->stream = (FILE *) &sfile;
1284 info->bytes_per_line = 0;
1285 info->bytes_per_chunk = 0;
1286
1287 #ifdef DISASSEMBLER_NEEDS_RELOCS
1288 /* FIXME: This is wrong. It tests the number of octets
1289 in the last instruction, not the current one. */
1290 if (*relppp < relppend
1291 && (**relppp)->address >= addr_offset
1292 && (**relppp)->address <= addr_offset + octets / opb)
1293 info->flags = INSN_HAS_RELOC;
1294 else
1295 #endif
1296 info->flags = 0;
1297
1298 octets = (*disassemble_fn) (section->vma + addr_offset, info);
1299 info->fprintf_func = (fprintf_ftype) fprintf;
1300 info->stream = stdout;
1301 if (info->bytes_per_line != 0)
1302 octets_per_line = info->bytes_per_line;
1303 if (octets < 0)
1304 {
1305 if (sfile.current != sfile.buffer)
1306 printf ("%s\n", sfile.buffer);
1307 free (sfile.buffer);
1308 break;
1309 }
1310 }
1311 else
1312 {
1313 bfd_vma j;
1314
1315 octets = octets_per_line;
1316 if (addr_offset + octets / opb > stop_offset)
1317 octets = (stop_offset - addr_offset) * opb;
1318
1319 for (j = addr_offset * opb; j < addr_offset * opb + octets; ++j)
1320 {
1321 if (ISPRINT (data[j]))
1322 buf[j - addr_offset * opb] = data[j];
1323 else
1324 buf[j - addr_offset * opb] = '.';
1325 }
1326 buf[j - addr_offset * opb] = '\0';
1327 }
1328
1329 if (prefix_addresses
1330 ? show_raw_insn > 0
1331 : show_raw_insn >= 0)
1332 {
1333 bfd_vma j;
1334
1335 /* If ! prefix_addresses and ! wide_output, we print
1336 octets_per_line octets per line. */
1337 pb = octets;
1338 if (pb > octets_per_line && ! prefix_addresses && ! wide_output)
1339 pb = octets_per_line;
1340
1341 if (info->bytes_per_chunk)
1342 bpc = info->bytes_per_chunk;
1343 else
1344 bpc = 1;
1345
1346 for (j = addr_offset * opb; j < addr_offset * opb + pb; j += bpc)
1347 {
1348 int k;
1349 if (bpc > 1 && info->display_endian == BFD_ENDIAN_LITTLE)
1350 {
1351 for (k = bpc - 1; k >= 0; k--)
1352 printf ("%02x", (unsigned) data[j + k]);
1353 putchar (' ');
1354 }
1355 else
1356 {
1357 for (k = 0; k < bpc; k++)
1358 printf ("%02x", (unsigned) data[j + k]);
1359 putchar (' ');
1360 }
1361 }
1362
1363 for (; pb < octets_per_line; pb += bpc)
1364 {
1365 int k;
1366
1367 for (k = 0; k < bpc; k++)
1368 printf (" ");
1369 putchar (' ');
1370 }
1371
1372 /* Separate raw data from instruction by extra space. */
1373 if (insns)
1374 putchar ('\t');
1375 else
1376 printf (" ");
1377 }
1378
1379 if (! insns)
1380 printf ("%s", buf);
1381 else
1382 {
1383 printf ("%s", sfile.buffer);
1384 free (sfile.buffer);
1385 }
1386
1387 if (prefix_addresses
1388 ? show_raw_insn > 0
1389 : show_raw_insn >= 0)
1390 {
1391 while (pb < octets)
1392 {
1393 bfd_vma j;
1394 char *s;
1395
1396 putchar ('\n');
1397 j = addr_offset * opb + pb;
1398
1399 bfd_sprintf_vma (aux->abfd, buf, section->vma + j / opb);
1400 for (s = buf + skip_addr_chars; *s == '0'; s++)
1401 *s = ' ';
1402 if (*s == '\0')
1403 *--s = '0';
1404 printf ("%s:\t", buf + skip_addr_chars);
1405
1406 pb += octets_per_line;
1407 if (pb > octets)
1408 pb = octets;
1409 for (; j < addr_offset * opb + pb; j += bpc)
1410 {
1411 int k;
1412
1413 if (bpc > 1 && info->display_endian == BFD_ENDIAN_LITTLE)
1414 {
1415 for (k = bpc - 1; k >= 0; k--)
1416 printf ("%02x", (unsigned) data[j + k]);
1417 putchar (' ');
1418 }
1419 else
1420 {
1421 for (k = 0; k < bpc; k++)
1422 printf ("%02x", (unsigned) data[j + k]);
1423 putchar (' ');
1424 }
1425 }
1426 }
1427 }
1428
1429 if (!wide_output)
1430 putchar ('\n');
1431 else
1432 need_nl = true;
1433 }
1434
1435 if ((section->flags & SEC_RELOC) != 0
1436 #ifndef DISASSEMBLER_NEEDS_RELOCS
1437 && dump_reloc_info
1438 #endif
1439 )
1440 {
1441 while ((*relppp) < relppend
1442 && ((**relppp)->address >= (bfd_vma) addr_offset
1443 && (**relppp)->address < (bfd_vma) addr_offset + octets / opb))
1444 #ifdef DISASSEMBLER_NEEDS_RELOCS
1445 if (! dump_reloc_info)
1446 ++(*relppp);
1447 else
1448 #endif
1449 {
1450 arelent *q;
1451
1452 q = **relppp;
1453
1454 if (wide_output)
1455 putchar ('\t');
1456 else
1457 printf ("\t\t\t");
1458
1459 objdump_print_value (section->vma + q->address, info, true);
1460
1461 printf (": %s\t", q->howto->name);
1462
1463 if (q->sym_ptr_ptr == NULL || *q->sym_ptr_ptr == NULL)
1464 printf ("*unknown*");
1465 else
1466 {
1467 const char *sym_name;
1468
1469 sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
1470 if (sym_name != NULL && *sym_name != '\0')
1471 objdump_print_symname (aux->abfd, info, *q->sym_ptr_ptr);
1472 else
1473 {
1474 asection *sym_sec;
1475
1476 sym_sec = bfd_get_section (*q->sym_ptr_ptr);
1477 sym_name = bfd_get_section_name (aux->abfd, sym_sec);
1478 if (sym_name == NULL || *sym_name == '\0')
1479 sym_name = "*unknown*";
1480 printf ("%s", sym_name);
1481 }
1482 }
1483
1484 if (q->addend)
1485 {
1486 printf ("+0x");
1487 objdump_print_value (q->addend, info, true);
1488 }
1489
1490 printf ("\n");
1491 need_nl = false;
1492 ++(*relppp);
1493 }
1494 }
1495
1496 if (need_nl)
1497 printf ("\n");
1498
1499 addr_offset += octets / opb;
1500 }
1501 }
1502
1503 /* Disassemble the contents of an object file. */
1504
1505 static void
1506 disassemble_data (abfd)
1507 bfd *abfd;
1508 {
1509 unsigned long addr_offset;
1510 disassembler_ftype disassemble_fn;
1511 struct disassemble_info disasm_info;
1512 struct objdump_disasm_info aux;
1513 asection *section;
1514 unsigned int opb;
1515
1516 print_files = NULL;
1517 prev_functionname = NULL;
1518 prev_line = -1;
1519
1520 /* We make a copy of syms to sort. We don't want to sort syms
1521 because that will screw up the relocs. */
1522 sorted_syms = (asymbol **) xmalloc (symcount * sizeof (asymbol *));
1523 memcpy (sorted_syms, syms, symcount * sizeof (asymbol *));
1524
1525 sorted_symcount = remove_useless_symbols (sorted_syms, symcount);
1526
1527 /* Sort the symbols into section and symbol order */
1528 qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols);
1529
1530 INIT_DISASSEMBLE_INFO(disasm_info, stdout, fprintf);
1531 disasm_info.application_data = (PTR) &aux;
1532 aux.abfd = abfd;
1533 aux.require_sec = false;
1534 disasm_info.print_address_func = objdump_print_address;
1535 disasm_info.symbol_at_address_func = objdump_symbol_at_address;
1536
1537 if (machine != (char *) NULL)
1538 {
1539 const bfd_arch_info_type *info = bfd_scan_arch (machine);
1540 if (info == NULL)
1541 {
1542 fatal (_("Can't use supplied machine %s"), machine);
1543 }
1544 abfd->arch_info = info;
1545 }
1546
1547 if (endian != BFD_ENDIAN_UNKNOWN)
1548 {
1549 struct bfd_target *xvec;
1550
1551 xvec = (struct bfd_target *) xmalloc (sizeof (struct bfd_target));
1552 memcpy (xvec, abfd->xvec, sizeof (struct bfd_target));
1553 xvec->byteorder = endian;
1554 abfd->xvec = xvec;
1555 }
1556
1557 disassemble_fn = disassembler (abfd);
1558 if (!disassemble_fn)
1559 {
1560 non_fatal (_("Can't disassemble for architecture %s\n"),
1561 bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
1562 exit_status = 1;
1563 return;
1564 }
1565
1566 opb = bfd_octets_per_byte (abfd);
1567
1568 disasm_info.flavour = bfd_get_flavour (abfd);
1569 disasm_info.arch = bfd_get_arch (abfd);
1570 disasm_info.mach = bfd_get_mach (abfd);
1571 disasm_info.disassembler_options = disassembler_options;
1572 disasm_info.octets_per_byte = opb;
1573
1574 if (bfd_big_endian (abfd))
1575 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_BIG;
1576 else if (bfd_little_endian (abfd))
1577 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_LITTLE;
1578 else
1579 /* ??? Aborting here seems too drastic. We could default to big or little
1580 instead. */
1581 disasm_info.endian = BFD_ENDIAN_UNKNOWN;
1582
1583 for (section = abfd->sections;
1584 section != (asection *) NULL;
1585 section = section->next)
1586 {
1587 bfd_byte *data = NULL;
1588 bfd_size_type datasize = 0;
1589 arelent **relbuf = NULL;
1590 arelent **relpp = NULL;
1591 arelent **relppend = NULL;
1592 unsigned long stop_offset;
1593 asymbol *sym = NULL;
1594 long place = 0;
1595
1596 if ((section->flags & SEC_LOAD) == 0
1597 || (! disassemble_all
1598 && only == NULL
1599 && (section->flags & SEC_CODE) == 0))
1600 continue;
1601 if (only != (char *) NULL && strcmp (only, section->name) != 0)
1602 continue;
1603
1604 if ((section->flags & SEC_RELOC) != 0
1605 #ifndef DISASSEMBLER_NEEDS_RELOCS
1606 && dump_reloc_info
1607 #endif
1608 )
1609 {
1610 long relsize;
1611
1612 relsize = bfd_get_reloc_upper_bound (abfd, section);
1613 if (relsize < 0)
1614 bfd_fatal (bfd_get_filename (abfd));
1615
1616 if (relsize > 0)
1617 {
1618 long relcount;
1619
1620 relbuf = (arelent **) xmalloc (relsize);
1621 relcount = bfd_canonicalize_reloc (abfd, section, relbuf, syms);
1622 if (relcount < 0)
1623 bfd_fatal (bfd_get_filename (abfd));
1624
1625 /* Sort the relocs by address. */
1626 qsort (relbuf, relcount, sizeof (arelent *), compare_relocs);
1627
1628 relpp = relbuf;
1629 relppend = relpp + relcount;
1630
1631 /* Skip over the relocs belonging to addresses below the
1632 start address. */
1633 if (start_address != (bfd_vma) -1)
1634 {
1635 while (relpp < relppend
1636 && (*relpp)->address < start_address)
1637 ++relpp;
1638 }
1639 }
1640 }
1641
1642 printf (_("Disassembly of section %s:\n"), section->name);
1643
1644 datasize = bfd_get_section_size_before_reloc (section);
1645 if (datasize == 0)
1646 continue;
1647
1648 data = (bfd_byte *) xmalloc ((size_t) datasize);
1649
1650 bfd_get_section_contents (abfd, section, data, 0, datasize);
1651
1652 aux.sec = section;
1653 disasm_info.buffer = data;
1654 disasm_info.buffer_vma = section->vma;
1655 disasm_info.buffer_length = datasize;
1656 disasm_info.section = section;
1657 if (start_address == (bfd_vma) -1
1658 || start_address < disasm_info.buffer_vma)
1659 addr_offset = 0;
1660 else
1661 addr_offset = start_address - disasm_info.buffer_vma;
1662 if (stop_address == (bfd_vma) -1)
1663 stop_offset = datasize / opb;
1664 else
1665 {
1666 if (stop_address < disasm_info.buffer_vma)
1667 stop_offset = 0;
1668 else
1669 stop_offset = stop_address - disasm_info.buffer_vma;
1670 if (stop_offset > disasm_info.buffer_length / opb)
1671 stop_offset = disasm_info.buffer_length / opb;
1672 }
1673
1674 sym = find_symbol_for_address (abfd, section, section->vma + addr_offset,
1675 true, &place);
1676
1677 while (addr_offset < stop_offset)
1678 {
1679 asymbol *nextsym;
1680 unsigned long nextstop_offset;
1681 boolean insns;
1682
1683 if (sym != NULL && bfd_asymbol_value (sym) <= section->vma + addr_offset)
1684 {
1685 int x;
1686
1687 for (x = place;
1688 (x < sorted_symcount
1689 && bfd_asymbol_value (sorted_syms[x]) <= section->vma + addr_offset);
1690 ++x)
1691 continue;
1692 disasm_info.symbols = & sorted_syms[place];
1693 disasm_info.num_symbols = x - place;
1694 }
1695 else
1696 disasm_info.symbols = NULL;
1697
1698 if (! prefix_addresses)
1699 {
1700 printf ("\n");
1701 objdump_print_addr_with_sym (abfd, section, sym,
1702 section->vma + addr_offset,
1703 &disasm_info,
1704 false);
1705 printf (":\n");
1706 }
1707
1708 if (sym != NULL && bfd_asymbol_value (sym) > section->vma + addr_offset)
1709 nextsym = sym;
1710 else if (sym == NULL)
1711 nextsym = NULL;
1712 else
1713 {
1714 /* Search forward for the next appropriate symbol in
1715 SECTION. Note that all the symbols are sorted
1716 together into one big array, and that some sections
1717 may have overlapping addresses. */
1718 while (place < sorted_symcount
1719 && (sorted_syms[place]->section != section
1720 || (bfd_asymbol_value (sorted_syms[place])
1721 <= bfd_asymbol_value (sym))))
1722 ++place;
1723 if (place >= sorted_symcount)
1724 nextsym = NULL;
1725 else
1726 nextsym = sorted_syms[place];
1727 }
1728
1729 if (sym != NULL && bfd_asymbol_value (sym) > section->vma + addr_offset)
1730 {
1731 nextstop_offset = bfd_asymbol_value (sym) - section->vma;
1732 if (nextstop_offset > stop_offset)
1733 nextstop_offset = stop_offset;
1734 }
1735 else if (nextsym == NULL)
1736 nextstop_offset = stop_offset;
1737 else
1738 {
1739 nextstop_offset = bfd_asymbol_value (nextsym) - section->vma;
1740 if (nextstop_offset > stop_offset)
1741 nextstop_offset = stop_offset;
1742 }
1743
1744 /* If a symbol is explicitly marked as being an object
1745 rather than a function, just dump the bytes without
1746 disassembling them. */
1747 if (disassemble_all
1748 || sym == NULL
1749 || bfd_asymbol_value (sym) > section->vma + addr_offset
1750 || ((sym->flags & BSF_OBJECT) == 0
1751 && (strstr (bfd_asymbol_name (sym), "gnu_compiled")
1752 == NULL)
1753 && (strstr (bfd_asymbol_name (sym), "gcc2_compiled")
1754 == NULL))
1755 || (sym->flags & BSF_FUNCTION) != 0)
1756 insns = true;
1757 else
1758 insns = false;
1759
1760 disassemble_bytes (&disasm_info, disassemble_fn, insns, data,
1761 addr_offset, nextstop_offset, &relpp, relppend);
1762
1763 addr_offset = nextstop_offset;
1764 sym = nextsym;
1765 }
1766
1767 free (data);
1768 if (relbuf != NULL)
1769 free (relbuf);
1770 }
1771 free (sorted_syms);
1772 }
1773 \f
1774
1775 /* Define a table of stab values and print-strings. We wish the initializer
1776 could be a direct-mapped table, but instead we build one the first
1777 time we need it. */
1778
1779 static void dump_section_stabs PARAMS ((bfd *abfd, char *stabsect_name,
1780 char *strsect_name));
1781
1782 /* Dump the stabs sections from an object file that has a section that
1783 uses Sun stabs encoding. */
1784
1785 static void
1786 dump_stabs (abfd)
1787 bfd *abfd;
1788 {
1789 dump_section_stabs (abfd, ".stab", ".stabstr");
1790 dump_section_stabs (abfd, ".stab.excl", ".stab.exclstr");
1791 dump_section_stabs (abfd, ".stab.index", ".stab.indexstr");
1792 dump_section_stabs (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
1793 }
1794
1795 static bfd_byte *stabs;
1796 static bfd_size_type stab_size;
1797
1798 static char *strtab;
1799 static bfd_size_type stabstr_size;
1800
1801 /* Read ABFD's stabs section STABSECT_NAME into `stabs'
1802 and string table section STRSECT_NAME into `strtab'.
1803 If the section exists and was read, allocate the space and return true.
1804 Otherwise return false. */
1805
1806 static boolean
1807 read_section_stabs (abfd, stabsect_name, strsect_name)
1808 bfd *abfd;
1809 const char *stabsect_name;
1810 const char *strsect_name;
1811 {
1812 asection *stabsect, *stabstrsect;
1813
1814 stabsect = bfd_get_section_by_name (abfd, stabsect_name);
1815 if (0 == stabsect)
1816 {
1817 printf (_("No %s section present\n\n"), stabsect_name);
1818 return false;
1819 }
1820
1821 stabstrsect = bfd_get_section_by_name (abfd, strsect_name);
1822 if (0 == stabstrsect)
1823 {
1824 non_fatal (_("%s has no %s section"),
1825 bfd_get_filename (abfd), strsect_name);
1826 exit_status = 1;
1827 return false;
1828 }
1829
1830 stab_size = bfd_section_size (abfd, stabsect);
1831 stabstr_size = bfd_section_size (abfd, stabstrsect);
1832
1833 stabs = (bfd_byte *) xmalloc (stab_size);
1834 strtab = (char *) xmalloc (stabstr_size);
1835
1836 if (! bfd_get_section_contents (abfd, stabsect, (PTR) stabs, 0, stab_size))
1837 {
1838 non_fatal (_("Reading %s section of %s failed: %s"),
1839 stabsect_name, bfd_get_filename (abfd),
1840 bfd_errmsg (bfd_get_error ()));
1841 free (stabs);
1842 free (strtab);
1843 exit_status = 1;
1844 return false;
1845 }
1846
1847 if (! bfd_get_section_contents (abfd, stabstrsect, (PTR) strtab, 0,
1848 stabstr_size))
1849 {
1850 non_fatal (_("Reading %s section of %s failed: %s\n"),
1851 strsect_name, bfd_get_filename (abfd),
1852 bfd_errmsg (bfd_get_error ()));
1853 free (stabs);
1854 free (strtab);
1855 exit_status = 1;
1856 return false;
1857 }
1858
1859 return true;
1860 }
1861
1862 /* Stabs entries use a 12 byte format:
1863 4 byte string table index
1864 1 byte stab type
1865 1 byte stab other field
1866 2 byte stab desc field
1867 4 byte stab value
1868 FIXME: This will have to change for a 64 bit object format. */
1869
1870 #define STRDXOFF (0)
1871 #define TYPEOFF (4)
1872 #define OTHEROFF (5)
1873 #define DESCOFF (6)
1874 #define VALOFF (8)
1875 #define STABSIZE (12)
1876
1877 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
1878 using string table section STRSECT_NAME (in `strtab'). */
1879
1880 static void
1881 print_section_stabs (abfd, stabsect_name, strsect_name)
1882 bfd *abfd;
1883 const char *stabsect_name;
1884 const char *strsect_name ATTRIBUTE_UNUSED;
1885 {
1886 int i;
1887 unsigned file_string_table_offset = 0, next_file_string_table_offset = 0;
1888 bfd_byte *stabp, *stabs_end;
1889
1890 stabp = stabs;
1891 stabs_end = stabp + stab_size;
1892
1893 printf (_("Contents of %s section:\n\n"), stabsect_name);
1894 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
1895
1896 /* Loop through all symbols and print them.
1897
1898 We start the index at -1 because there is a dummy symbol on
1899 the front of stabs-in-{coff,elf} sections that supplies sizes. */
1900
1901 for (i = -1; stabp < stabs_end; stabp += STABSIZE, i++)
1902 {
1903 const char *name;
1904 unsigned long strx;
1905 unsigned char type, other;
1906 unsigned short desc;
1907 bfd_vma value;
1908
1909 strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
1910 type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
1911 other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
1912 desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
1913 value = bfd_h_get_32 (abfd, stabp + VALOFF);
1914
1915 printf ("\n%-6d ", i);
1916 /* Either print the stab name, or, if unnamed, print its number
1917 again (makes consistent formatting for tools like awk). */
1918 name = bfd_get_stab_name (type);
1919 if (name != NULL)
1920 printf ("%-6s", name);
1921 else if (type == N_UNDF)
1922 printf ("HdrSym");
1923 else
1924 printf ("%-6d", type);
1925 printf (" %-6d %-6d ", other, desc);
1926 bfd_printf_vma (abfd, value);
1927 printf (" %-6lu", strx);
1928
1929 /* Symbols with type == 0 (N_UNDF) specify the length of the
1930 string table associated with this file. We use that info
1931 to know how to relocate the *next* file's string table indices. */
1932
1933 if (type == N_UNDF)
1934 {
1935 file_string_table_offset = next_file_string_table_offset;
1936 next_file_string_table_offset += value;
1937 }
1938 else
1939 {
1940 /* Using the (possibly updated) string table offset, print the
1941 string (if any) associated with this symbol. */
1942
1943 if ((strx + file_string_table_offset) < stabstr_size)
1944 printf (" %s", &strtab[strx + file_string_table_offset]);
1945 else
1946 printf (" *");
1947 }
1948 }
1949 printf ("\n\n");
1950 }
1951
1952 static void
1953 dump_section_stabs (abfd, stabsect_name, strsect_name)
1954 bfd *abfd;
1955 char *stabsect_name;
1956 char *strsect_name;
1957 {
1958 asection *s;
1959
1960 /* Check for section names for which stabsect_name is a prefix, to
1961 handle .stab0, etc. */
1962 for (s = abfd->sections;
1963 s != NULL;
1964 s = s->next)
1965 {
1966 int len;
1967
1968 len = strlen (stabsect_name);
1969
1970 /* If the prefix matches, and the files section name ends with a
1971 nul or a digit, then we match. I.e., we want either an exact
1972 match or a section followed by a number. */
1973 if (strncmp (stabsect_name, s->name, len) == 0
1974 && (s->name[len] == '\000'
1975 || ISDIGIT (s->name[len])))
1976 {
1977 if (read_section_stabs (abfd, s->name, strsect_name))
1978 {
1979 print_section_stabs (abfd, s->name, strsect_name);
1980 free (stabs);
1981 free (strtab);
1982 }
1983 }
1984 }
1985 }
1986 \f
1987 static void
1988 dump_bfd_header (abfd)
1989 bfd *abfd;
1990 {
1991 char *comma = "";
1992
1993 printf (_("architecture: %s, "),
1994 bfd_printable_arch_mach (bfd_get_arch (abfd),
1995 bfd_get_mach (abfd)));
1996 printf (_("flags 0x%08x:\n"), abfd->flags);
1997
1998 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
1999 PF (HAS_RELOC, "HAS_RELOC");
2000 PF (EXEC_P, "EXEC_P");
2001 PF (HAS_LINENO, "HAS_LINENO");
2002 PF (HAS_DEBUG, "HAS_DEBUG");
2003 PF (HAS_SYMS, "HAS_SYMS");
2004 PF (HAS_LOCALS, "HAS_LOCALS");
2005 PF (DYNAMIC, "DYNAMIC");
2006 PF (WP_TEXT, "WP_TEXT");
2007 PF (D_PAGED, "D_PAGED");
2008 PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
2009 PF (HAS_LOAD_PAGE, "HAS_LOAD_PAGE");
2010 printf (_("\nstart address 0x"));
2011 bfd_printf_vma (abfd, abfd->start_address);
2012 printf ("\n");
2013 }
2014 \f
2015 static void
2016 dump_bfd_private_header (abfd)
2017 bfd *abfd;
2018 {
2019 bfd_print_private_bfd_data (abfd, stdout);
2020 }
2021
2022 /* Dump selected contents of ABFD */
2023
2024 static void
2025 dump_bfd (abfd)
2026 bfd *abfd;
2027 {
2028 /* If we are adjusting section VMA's, change them all now. Changing
2029 the BFD information is a hack. However, we must do it, or
2030 bfd_find_nearest_line will not do the right thing. */
2031 if (adjust_section_vma != 0)
2032 {
2033 asection *s;
2034
2035 for (s = abfd->sections; s != NULL; s = s->next)
2036 {
2037 s->vma += adjust_section_vma;
2038 s->lma += adjust_section_vma;
2039 }
2040 }
2041
2042 printf (_("\n%s: file format %s\n"), bfd_get_filename (abfd),
2043 abfd->xvec->name);
2044 if (dump_ar_hdrs)
2045 print_arelt_descr (stdout, abfd, true);
2046 if (dump_file_header)
2047 dump_bfd_header (abfd);
2048 if (dump_private_headers)
2049 dump_bfd_private_header (abfd);
2050 putchar ('\n');
2051 if (dump_section_headers)
2052 dump_headers (abfd);
2053
2054 if (dump_symtab || dump_reloc_info || disassemble || dump_debugging)
2055 syms = slurp_symtab (abfd);
2056 if (dump_dynamic_symtab || dump_dynamic_reloc_info)
2057 dynsyms = slurp_dynamic_symtab (abfd);
2058
2059 if (dump_symtab)
2060 dump_symbols (abfd, false);
2061 if (dump_dynamic_symtab)
2062 dump_symbols (abfd, true);
2063 if (dump_stab_section_info)
2064 dump_stabs (abfd);
2065 if (dump_reloc_info && ! disassemble)
2066 dump_relocs (abfd);
2067 if (dump_dynamic_reloc_info)
2068 dump_dynamic_relocs (abfd);
2069 if (dump_section_contents)
2070 dump_data (abfd);
2071 if (disassemble)
2072 disassemble_data (abfd);
2073 if (dump_debugging)
2074 {
2075 PTR dhandle;
2076
2077 dhandle = read_debugging_info (abfd, syms, symcount);
2078 if (dhandle != NULL)
2079 {
2080 if (! print_debugging_info (stdout, dhandle))
2081 {
2082 non_fatal (_("%s: printing debugging information failed"),
2083 bfd_get_filename (abfd));
2084 exit_status = 1;
2085 }
2086 }
2087 }
2088
2089 if (syms)
2090 {
2091 free (syms);
2092 syms = NULL;
2093 }
2094
2095 if (dynsyms)
2096 {
2097 free (dynsyms);
2098 dynsyms = NULL;
2099 }
2100 }
2101
2102 static void
2103 display_bfd (abfd)
2104 bfd *abfd;
2105 {
2106 char **matching;
2107
2108 if (bfd_check_format_matches (abfd, bfd_object, &matching))
2109 {
2110 dump_bfd (abfd);
2111 return;
2112 }
2113
2114 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
2115 {
2116 nonfatal (bfd_get_filename (abfd));
2117 list_matching_formats (matching);
2118 free (matching);
2119 return;
2120 }
2121
2122 if (bfd_get_error () != bfd_error_file_not_recognized)
2123 {
2124 nonfatal (bfd_get_filename (abfd));
2125 return;
2126 }
2127
2128 if (bfd_check_format_matches (abfd, bfd_core, &matching))
2129 {
2130 dump_bfd (abfd);
2131 return;
2132 }
2133
2134 nonfatal (bfd_get_filename (abfd));
2135
2136 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
2137 {
2138 list_matching_formats (matching);
2139 free (matching);
2140 }
2141 }
2142
2143 static void
2144 display_file (filename, target)
2145 char *filename;
2146 char *target;
2147 {
2148 bfd *file, *arfile = (bfd *) NULL;
2149
2150 file = bfd_openr (filename, target);
2151 if (file == NULL)
2152 {
2153 nonfatal (filename);
2154 return;
2155 }
2156
2157 if (bfd_check_format (file, bfd_archive) == true)
2158 {
2159 bfd *last_arfile = NULL;
2160
2161 printf (_("In archive %s:\n"), bfd_get_filename (file));
2162 for (;;)
2163 {
2164 bfd_set_error (bfd_error_no_error);
2165
2166 arfile = bfd_openr_next_archived_file (file, arfile);
2167 if (arfile == NULL)
2168 {
2169 if (bfd_get_error () != bfd_error_no_more_archived_files)
2170 nonfatal (bfd_get_filename (file));
2171 break;
2172 }
2173
2174 display_bfd (arfile);
2175
2176 if (last_arfile != NULL)
2177 bfd_close (last_arfile);
2178 last_arfile = arfile;
2179 }
2180
2181 if (last_arfile != NULL)
2182 bfd_close (last_arfile);
2183 }
2184 else
2185 display_bfd (file);
2186
2187 bfd_close (file);
2188 }
2189 \f
2190 /* Actually display the various requested regions */
2191
2192 static void
2193 dump_data (abfd)
2194 bfd *abfd;
2195 {
2196 asection *section;
2197 bfd_byte *data = 0;
2198 bfd_size_type datasize = 0;
2199 bfd_size_type addr_offset;
2200 bfd_size_type start_offset, stop_offset;
2201 unsigned int opb = bfd_octets_per_byte (abfd);
2202
2203 for (section = abfd->sections; section != NULL; section =
2204 section->next)
2205 {
2206 int onaline = 16;
2207
2208 if (only == (char *) NULL ||
2209 strcmp (only, section->name) == 0)
2210 {
2211 if (section->flags & SEC_HAS_CONTENTS)
2212 {
2213 printf (_("Contents of section %s:\n"), section->name);
2214
2215 if (bfd_section_size (abfd, section) == 0)
2216 continue;
2217 data = (bfd_byte *) xmalloc ((size_t) bfd_section_size (abfd, section));
2218 datasize = bfd_section_size (abfd, section);
2219
2220
2221 bfd_get_section_contents (abfd, section, (PTR) data, 0, bfd_section_size (abfd, section));
2222
2223 if (start_address == (bfd_vma) -1
2224 || start_address < section->vma)
2225 start_offset = 0;
2226 else
2227 start_offset = start_address - section->vma;
2228 if (stop_address == (bfd_vma) -1)
2229 stop_offset = bfd_section_size (abfd, section) / opb;
2230 else
2231 {
2232 if (stop_address < section->vma)
2233 stop_offset = 0;
2234 else
2235 stop_offset = stop_address - section->vma;
2236 if (stop_offset > bfd_section_size (abfd, section) / opb)
2237 stop_offset = bfd_section_size (abfd, section) / opb;
2238 }
2239 for (addr_offset = start_offset;
2240 addr_offset < stop_offset; addr_offset += onaline)
2241 {
2242 bfd_size_type j;
2243
2244 printf (" %04lx ", (unsigned long int)
2245 (addr_offset + section->vma));
2246 for (j = addr_offset * opb;
2247 j < addr_offset * opb + onaline; j++)
2248 {
2249 if (j < stop_offset * opb)
2250 printf ("%02x", (unsigned) (data[j]));
2251 else
2252 printf (" ");
2253 if ((j & 3) == 3)
2254 printf (" ");
2255 }
2256
2257 printf (" ");
2258 for (j = addr_offset; j < addr_offset * opb + onaline; j++)
2259 {
2260 if (j >= stop_offset * opb)
2261 printf (" ");
2262 else
2263 printf ("%c", ISPRINT (data[j]) ? data[j] : '.');
2264 }
2265 putchar ('\n');
2266 }
2267 free (data);
2268 }
2269 }
2270 }
2271 }
2272
2273 /* Should perhaps share code and display with nm? */
2274 static void
2275 dump_symbols (abfd, dynamic)
2276 bfd *abfd ATTRIBUTE_UNUSED;
2277 boolean dynamic;
2278 {
2279 asymbol **current;
2280 long max;
2281 long count;
2282
2283 if (dynamic)
2284 {
2285 current = dynsyms;
2286 max = dynsymcount;
2287 printf ("DYNAMIC SYMBOL TABLE:\n");
2288 }
2289 else
2290 {
2291 current = syms;
2292 max = symcount;
2293 printf ("SYMBOL TABLE:\n");
2294 }
2295
2296 if (max == 0)
2297 printf (_("no symbols\n"));
2298
2299 for (count = 0; count < max; count++)
2300 {
2301 if (*current)
2302 {
2303 bfd *cur_bfd = bfd_asymbol_bfd (*current);
2304
2305 if (cur_bfd != NULL)
2306 {
2307 const char *name;
2308 char *alloc;
2309
2310 name = (*current)->name;
2311 alloc = NULL;
2312 if (do_demangle && name != NULL && *name != '\0')
2313 {
2314 /* If we want to demangle the name, we demangle it
2315 here, and temporarily clobber it while calling
2316 bfd_print_symbol. FIXME: This is a gross hack. */
2317
2318 alloc = demangle (cur_bfd, name);
2319 (*current)->name = alloc;
2320 }
2321
2322 bfd_print_symbol (cur_bfd, stdout, *current,
2323 bfd_print_symbol_all);
2324
2325 (*current)->name = name;
2326 if (alloc != NULL)
2327 free (alloc);
2328
2329 printf ("\n");
2330 }
2331 }
2332 current++;
2333 }
2334 printf ("\n");
2335 printf ("\n");
2336 }
2337
2338 static void
2339 dump_relocs (abfd)
2340 bfd *abfd;
2341 {
2342 arelent **relpp;
2343 long relcount;
2344 asection *a;
2345
2346 for (a = abfd->sections; a != (asection *) NULL; a = a->next)
2347 {
2348 long relsize;
2349
2350 if (bfd_is_abs_section (a))
2351 continue;
2352 if (bfd_is_und_section (a))
2353 continue;
2354 if (bfd_is_com_section (a))
2355 continue;
2356
2357 if (only)
2358 {
2359 if (strcmp (only, a->name))
2360 continue;
2361 }
2362 else if ((a->flags & SEC_RELOC) == 0)
2363 continue;
2364
2365 relsize = bfd_get_reloc_upper_bound (abfd, a);
2366 if (relsize < 0)
2367 bfd_fatal (bfd_get_filename (abfd));
2368
2369 printf ("RELOCATION RECORDS FOR [%s]:", a->name);
2370
2371 if (relsize == 0)
2372 {
2373 printf (" (none)\n\n");
2374 }
2375 else
2376 {
2377 relpp = (arelent **) xmalloc (relsize);
2378 relcount = bfd_canonicalize_reloc (abfd, a, relpp, syms);
2379 if (relcount < 0)
2380 bfd_fatal (bfd_get_filename (abfd));
2381 else if (relcount == 0)
2382 {
2383 printf (" (none)\n\n");
2384 }
2385 else
2386 {
2387 printf ("\n");
2388 dump_reloc_set (abfd, a, relpp, relcount);
2389 printf ("\n\n");
2390 }
2391 free (relpp);
2392 }
2393 }
2394 }
2395
2396 static void
2397 dump_dynamic_relocs (abfd)
2398 bfd *abfd;
2399 {
2400 long relsize;
2401 arelent **relpp;
2402 long relcount;
2403
2404 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
2405 if (relsize < 0)
2406 bfd_fatal (bfd_get_filename (abfd));
2407
2408 printf ("DYNAMIC RELOCATION RECORDS");
2409
2410 if (relsize == 0)
2411 {
2412 printf (" (none)\n\n");
2413 }
2414 else
2415 {
2416 relpp = (arelent **) xmalloc (relsize);
2417 relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
2418 if (relcount < 0)
2419 bfd_fatal (bfd_get_filename (abfd));
2420 else if (relcount == 0)
2421 {
2422 printf (" (none)\n\n");
2423 }
2424 else
2425 {
2426 printf ("\n");
2427 dump_reloc_set (abfd, (asection *) NULL, relpp, relcount);
2428 printf ("\n\n");
2429 }
2430 free (relpp);
2431 }
2432 }
2433
2434 static void
2435 dump_reloc_set (abfd, sec, relpp, relcount)
2436 bfd *abfd;
2437 asection *sec;
2438 arelent **relpp;
2439 long relcount;
2440 {
2441 arelent **p;
2442 char *last_filename, *last_functionname;
2443 unsigned int last_line;
2444
2445 /* Get column headers lined up reasonably. */
2446 {
2447 static int width;
2448 if (width == 0)
2449 {
2450 char buf[30];
2451 bfd_sprintf_vma (abfd, buf, (bfd_vma) -1);
2452 width = strlen (buf) - 7;
2453 }
2454 printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
2455 }
2456
2457 last_filename = NULL;
2458 last_functionname = NULL;
2459 last_line = 0;
2460
2461 for (p = relpp; relcount && *p != (arelent *) NULL; p++, relcount--)
2462 {
2463 arelent *q = *p;
2464 const char *filename, *functionname;
2465 unsigned int line;
2466 const char *sym_name;
2467 const char *section_name;
2468
2469 if (start_address != (bfd_vma) -1
2470 && q->address < start_address)
2471 continue;
2472 if (stop_address != (bfd_vma) -1
2473 && q->address > stop_address)
2474 continue;
2475
2476 if (with_line_numbers
2477 && sec != NULL
2478 && bfd_find_nearest_line (abfd, sec, syms, q->address,
2479 &filename, &functionname, &line))
2480 {
2481 if (functionname != NULL
2482 && (last_functionname == NULL
2483 || strcmp (functionname, last_functionname) != 0))
2484 {
2485 printf ("%s():\n", functionname);
2486 if (last_functionname != NULL)
2487 free (last_functionname);
2488 last_functionname = xstrdup (functionname);
2489 }
2490 if (line > 0
2491 && (line != last_line
2492 || (filename != NULL
2493 && last_filename != NULL
2494 && strcmp (filename, last_filename) != 0)))
2495 {
2496 printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
2497 last_line = line;
2498 if (last_filename != NULL)
2499 free (last_filename);
2500 if (filename == NULL)
2501 last_filename = NULL;
2502 else
2503 last_filename = xstrdup (filename);
2504 }
2505 }
2506
2507 if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
2508 {
2509 sym_name = (*(q->sym_ptr_ptr))->name;
2510 section_name = (*(q->sym_ptr_ptr))->section->name;
2511 }
2512 else
2513 {
2514 sym_name = NULL;
2515 section_name = NULL;
2516 }
2517 if (sym_name)
2518 {
2519 bfd_printf_vma (abfd, q->address);
2520 if (q->howto->name)
2521 printf (" %-16s ", q->howto->name);
2522 else
2523 printf (" %-16d ", q->howto->type);
2524 objdump_print_symname (abfd, (struct disassemble_info *) NULL,
2525 *q->sym_ptr_ptr);
2526 }
2527 else
2528 {
2529 if (section_name == (const char *) NULL)
2530 section_name = "*unknown*";
2531 bfd_printf_vma (abfd, q->address);
2532 printf (" %-16s [%s]",
2533 q->howto->name,
2534 section_name);
2535 }
2536 if (q->addend)
2537 {
2538 printf ("+0x");
2539 bfd_printf_vma (abfd, q->addend);
2540 }
2541 printf ("\n");
2542 }
2543 }
2544 \f
2545 /* The length of the longest architecture name + 1. */
2546 #define LONGEST_ARCH sizeof("powerpc:common")
2547
2548 static const char *
2549 endian_string (endian)
2550 enum bfd_endian endian;
2551 {
2552 if (endian == BFD_ENDIAN_BIG)
2553 return "big endian";
2554 else if (endian == BFD_ENDIAN_LITTLE)
2555 return "little endian";
2556 else
2557 return "endianness unknown";
2558 }
2559
2560 /* List the targets that BFD is configured to support, each followed
2561 by its endianness and the architectures it supports. */
2562
2563 static void
2564 display_target_list ()
2565 {
2566 extern const bfd_target *const *bfd_target_vector;
2567 char *dummy_name;
2568 int t;
2569
2570 dummy_name = make_temp_file (NULL);
2571 for (t = 0; bfd_target_vector[t]; t++)
2572 {
2573 const bfd_target *p = bfd_target_vector[t];
2574 bfd *abfd = bfd_openw (dummy_name, p->name);
2575 int a;
2576
2577 printf ("%s\n (header %s, data %s)\n", p->name,
2578 endian_string (p->header_byteorder),
2579 endian_string (p->byteorder));
2580
2581 if (abfd == NULL)
2582 {
2583 nonfatal (dummy_name);
2584 continue;
2585 }
2586
2587 if (! bfd_set_format (abfd, bfd_object))
2588 {
2589 if (bfd_get_error () != bfd_error_invalid_operation)
2590 nonfatal (p->name);
2591 bfd_close_all_done (abfd);
2592 continue;
2593 }
2594
2595 for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
2596 if (bfd_set_arch_mach (abfd, (enum bfd_architecture) a, 0))
2597 printf (" %s\n",
2598 bfd_printable_arch_mach ((enum bfd_architecture) a, 0));
2599 bfd_close_all_done (abfd);
2600 }
2601 unlink (dummy_name);
2602 free (dummy_name);
2603 }
2604
2605 /* Print a table showing which architectures are supported for entries
2606 FIRST through LAST-1 of bfd_target_vector (targets across,
2607 architectures down). */
2608
2609 static void
2610 display_info_table (first, last)
2611 int first;
2612 int last;
2613 {
2614 extern const bfd_target *const *bfd_target_vector;
2615 int t, a;
2616 char *dummy_name;
2617
2618 /* Print heading of target names. */
2619 printf ("\n%*s", (int) LONGEST_ARCH, " ");
2620 for (t = first; t < last && bfd_target_vector[t]; t++)
2621 printf ("%s ", bfd_target_vector[t]->name);
2622 putchar ('\n');
2623
2624 dummy_name = make_temp_file (NULL);
2625 for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
2626 if (strcmp (bfd_printable_arch_mach (a, 0), "UNKNOWN!") != 0)
2627 {
2628 printf ("%*s ", (int) LONGEST_ARCH - 1,
2629 bfd_printable_arch_mach (a, 0));
2630 for (t = first; t < last && bfd_target_vector[t]; t++)
2631 {
2632 const bfd_target *p = bfd_target_vector[t];
2633 boolean ok = true;
2634 bfd *abfd = bfd_openw (dummy_name, p->name);
2635
2636 if (abfd == NULL)
2637 {
2638 nonfatal (p->name);
2639 ok = false;
2640 }
2641
2642 if (ok)
2643 {
2644 if (! bfd_set_format (abfd, bfd_object))
2645 {
2646 if (bfd_get_error () != bfd_error_invalid_operation)
2647 nonfatal (p->name);
2648 ok = false;
2649 }
2650 }
2651
2652 if (ok)
2653 {
2654 if (! bfd_set_arch_mach (abfd, a, 0))
2655 ok = false;
2656 }
2657
2658 if (ok)
2659 printf ("%s ", p->name);
2660 else
2661 {
2662 int l = strlen (p->name);
2663 while (l--)
2664 putchar ('-');
2665 putchar (' ');
2666 }
2667 if (abfd != NULL)
2668 bfd_close_all_done (abfd);
2669 }
2670 putchar ('\n');
2671 }
2672 unlink (dummy_name);
2673 free (dummy_name);
2674 }
2675
2676 /* Print tables of all the target-architecture combinations that
2677 BFD has been configured to support. */
2678
2679 static void
2680 display_target_tables ()
2681 {
2682 int t, columns;
2683 extern const bfd_target *const *bfd_target_vector;
2684 char *colum;
2685
2686 columns = 0;
2687 colum = getenv ("COLUMNS");
2688 if (colum != NULL)
2689 columns = atoi (colum);
2690 if (columns == 0)
2691 columns = 80;
2692
2693 t = 0;
2694 while (bfd_target_vector[t] != NULL)
2695 {
2696 int oldt = t, wid;
2697
2698 wid = LONGEST_ARCH + strlen (bfd_target_vector[t]->name) + 1;
2699 ++t;
2700 while (wid < columns && bfd_target_vector[t] != NULL)
2701 {
2702 int newwid;
2703
2704 newwid = wid + strlen (bfd_target_vector[t]->name) + 1;
2705 if (newwid >= columns)
2706 break;
2707 wid = newwid;
2708 ++t;
2709 }
2710 display_info_table (oldt, t);
2711 }
2712 }
2713
2714 static void
2715 display_info ()
2716 {
2717 printf (_("BFD header file version %s\n"), BFD_VERSION_STRING);
2718 display_target_list ();
2719 display_target_tables ();
2720 }
2721
2722 int main PARAMS ((int, char **));
2723
2724 int
2725 main (argc, argv)
2726 int argc;
2727 char **argv;
2728 {
2729 int c;
2730 char *target = default_target;
2731 boolean seenflag = false;
2732
2733 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2734 setlocale (LC_MESSAGES, "");
2735 #endif
2736 #if defined (HAVE_SETLOCALE)
2737 setlocale (LC_CTYPE, "");
2738 #endif
2739 bindtextdomain (PACKAGE, LOCALEDIR);
2740 textdomain (PACKAGE);
2741
2742 program_name = *argv;
2743 xmalloc_set_program_name (program_name);
2744
2745 START_PROGRESS (program_name, 0);
2746
2747 bfd_init ();
2748 set_default_bfd_target ();
2749
2750 while ((c = getopt_long (argc, argv, "pib:m:M:VvCdDlfaHhrRtTxsSj:wE:zgG",
2751 long_options, (int *) 0))
2752 != EOF)
2753 {
2754 switch (c)
2755 {
2756 case 0:
2757 break; /* We've been given a long option. */
2758 case 'm':
2759 machine = optarg;
2760 break;
2761 case 'M':
2762 disassembler_options = optarg;
2763 break;
2764 case 'j':
2765 only = optarg;
2766 break;
2767 case 'l':
2768 with_line_numbers = true;
2769 break;
2770 case 'b':
2771 target = optarg;
2772 break;
2773 case 'C':
2774 do_demangle = true;
2775 if (optarg != NULL)
2776 {
2777 enum demangling_styles style;
2778
2779 style = cplus_demangle_name_to_style (optarg);
2780 if (style == unknown_demangling)
2781 fatal (_("unknown demangling style `%s'"),
2782 optarg);
2783
2784 cplus_demangle_set_style (style);
2785 }
2786 break;
2787 case 'w':
2788 wide_output = true;
2789 break;
2790 case OPTION_ADJUST_VMA:
2791 adjust_section_vma = parse_vma (optarg, "--adjust-vma");
2792 break;
2793 case OPTION_START_ADDRESS:
2794 start_address = parse_vma (optarg, "--start-address");
2795 break;
2796 case OPTION_STOP_ADDRESS:
2797 stop_address = parse_vma (optarg, "--stop-address");
2798 break;
2799 case 'E':
2800 if (strcmp (optarg, "B") == 0)
2801 endian = BFD_ENDIAN_BIG;
2802 else if (strcmp (optarg, "L") == 0)
2803 endian = BFD_ENDIAN_LITTLE;
2804 else
2805 {
2806 non_fatal (_("unrecognized -E option"));
2807 usage (stderr, 1);
2808 }
2809 break;
2810 case OPTION_ENDIAN:
2811 if (strncmp (optarg, "big", strlen (optarg)) == 0)
2812 endian = BFD_ENDIAN_BIG;
2813 else if (strncmp (optarg, "little", strlen (optarg)) == 0)
2814 endian = BFD_ENDIAN_LITTLE;
2815 else
2816 {
2817 non_fatal (_("unrecognized --endian type `%s'"), optarg);
2818 usage (stderr, 1);
2819 }
2820 break;
2821
2822 case 'f':
2823 dump_file_header = true;
2824 seenflag = true;
2825 break;
2826 case 'i':
2827 formats_info = true;
2828 seenflag = true;
2829 break;
2830 case 'p':
2831 dump_private_headers = true;
2832 seenflag = true;
2833 break;
2834 case 'x':
2835 dump_private_headers = true;
2836 dump_symtab = true;
2837 dump_reloc_info = true;
2838 dump_file_header = true;
2839 dump_ar_hdrs = true;
2840 dump_section_headers = true;
2841 seenflag = true;
2842 break;
2843 case 't':
2844 dump_symtab = true;
2845 seenflag = true;
2846 break;
2847 case 'T':
2848 dump_dynamic_symtab = true;
2849 seenflag = true;
2850 break;
2851 case 'd':
2852 disassemble = true;
2853 seenflag = true;
2854 break;
2855 case 'z':
2856 disassemble_zeroes = true;
2857 break;
2858 case 'D':
2859 disassemble = true;
2860 disassemble_all = true;
2861 seenflag = true;
2862 break;
2863 case 'S':
2864 disassemble = true;
2865 with_source_code = true;
2866 seenflag = true;
2867 break;
2868 case 'g':
2869 dump_debugging = 1;
2870 seenflag = true;
2871 break;
2872 case 'G':
2873 dump_stab_section_info = true;
2874 seenflag = true;
2875 break;
2876 case 's':
2877 dump_section_contents = true;
2878 seenflag = true;
2879 break;
2880 case 'r':
2881 dump_reloc_info = true;
2882 seenflag = true;
2883 break;
2884 case 'R':
2885 dump_dynamic_reloc_info = true;
2886 seenflag = true;
2887 break;
2888 case 'a':
2889 dump_ar_hdrs = true;
2890 seenflag = true;
2891 break;
2892 case 'h':
2893 dump_section_headers = true;
2894 seenflag = true;
2895 break;
2896 case 'H':
2897 usage (stdout, 0);
2898 seenflag = true;
2899 case 'v':
2900 case 'V':
2901 show_version = true;
2902 seenflag = true;
2903 break;
2904
2905 default:
2906 usage (stderr, 1);
2907 }
2908 }
2909
2910 if (show_version)
2911 print_version ("objdump");
2912
2913 if (seenflag == false)
2914 usage (stderr, 2);
2915
2916 if (formats_info)
2917 display_info ();
2918 else
2919 {
2920 if (optind == argc)
2921 display_file ("a.out", target);
2922 else
2923 for (; optind < argc;)
2924 display_file (argv[optind++], target);
2925 }
2926
2927 END_PROGRESS (program_name);
2928
2929 return exit_status;
2930 }
This page took 0.117135 seconds and 5 git commands to generate.