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