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