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