* emultempl/elf32.em (place_orphan): Don't put non-allocated .rel*
[binutils.git] / binutils / objdump.c
blob73abc981ee1cf85fc8cf640bab7aa70a81103042
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.
6 This file is part of GNU Binutils.
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.
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.
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. */
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"
35 /* Internal headers for the ELF .stab-dump code - sorry. */
36 #define BYTES_IN_WORD 32
37 #include "aout/aout64.h"
39 #ifdef NEED_DECLARATION_FPRINTF
40 /* This is needed by INIT_DISASSEMBLE_INFO. */
41 extern int fprintf PARAMS ((FILE *, const char *, ...));
42 #endif
44 /* Exit status. */
45 static int exit_status = 0;
47 static char *default_target = NULL; /* default at runtime */
49 static int show_version = 0; /* show the version number */
50 static int dump_section_contents; /* -s */
51 static int dump_section_headers; /* -h */
52 static boolean dump_file_header; /* -f */
53 static int dump_symtab; /* -t */
54 static int dump_dynamic_symtab; /* -T */
55 static int dump_reloc_info; /* -r */
56 static int dump_dynamic_reloc_info; /* -R */
57 static int dump_ar_hdrs; /* -a */
58 static int dump_private_headers; /* -p */
59 static int prefix_addresses; /* --prefix-addresses */
60 static int with_line_numbers; /* -l */
61 static boolean with_source_code; /* -S */
62 static int show_raw_insn; /* --show-raw-insn */
63 static int dump_stab_section_info; /* --stabs */
64 static int do_demangle; /* -C, --demangle */
65 static boolean disassemble; /* -d */
66 static boolean disassemble_all; /* -D */
67 static int disassemble_zeroes; /* --disassemble-zeroes */
68 static boolean formats_info; /* -i */
69 static char *only; /* -j secname */
70 static int wide_output; /* -w */
71 static bfd_vma start_address = (bfd_vma) -1; /* --start-address */
72 static bfd_vma stop_address = (bfd_vma) -1; /* --stop-address */
73 static int dump_debugging; /* --debugging */
74 static bfd_vma adjust_section_vma = 0; /* --adjust-vma */
75 static int file_start_context = 0; /* --file-start-context */
77 /* Extra info to pass to the disassembler address printing function. */
78 struct objdump_disasm_info
80 bfd *abfd;
81 asection *sec;
82 boolean require_sec;
85 /* Architecture to disassemble for, or default if NULL. */
86 static char *machine = (char *) NULL;
88 /* Target specific options to the disassembler. */
89 static char *disassembler_options = (char *) NULL;
91 /* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */
92 static enum bfd_endian endian = BFD_ENDIAN_UNKNOWN;
94 /* The symbol table. */
95 static asymbol **syms;
97 /* Number of symbols in `syms'. */
98 static long symcount = 0;
100 /* The sorted symbol table. */
101 static asymbol **sorted_syms;
103 /* Number of symbols in `sorted_syms'. */
104 static long sorted_symcount = 0;
106 /* The dynamic symbol table. */
107 static asymbol **dynsyms;
109 /* Number of symbols in `dynsyms'. */
110 static long dynsymcount = 0;
112 /* Static declarations. */
114 static void usage PARAMS ((FILE *, int));
115 static void nonfatal PARAMS ((const char *));
116 static void display_file PARAMS ((char *filename, char *target));
117 static void dump_section_header PARAMS ((bfd *, asection *, PTR));
118 static void dump_headers PARAMS ((bfd *));
119 static void dump_data PARAMS ((bfd *abfd));
120 static void dump_relocs PARAMS ((bfd *abfd));
121 static void dump_dynamic_relocs PARAMS ((bfd * abfd));
122 static void dump_reloc_set PARAMS ((bfd *, asection *, arelent **, long));
123 static void dump_symbols PARAMS ((bfd *abfd, boolean dynamic));
124 static void dump_bfd_header PARAMS ((bfd *));
125 static void dump_bfd_private_header PARAMS ((bfd *));
126 static void dump_bfd PARAMS ((bfd *));
127 static void display_bfd PARAMS ((bfd *abfd));
128 static void display_target_list PARAMS ((void));
129 static void display_info_table PARAMS ((int, int));
130 static void display_target_tables PARAMS ((void));
131 static void display_info PARAMS ((void));
132 static void objdump_print_value
133 PARAMS ((bfd_vma, struct disassemble_info *, boolean));
134 static void objdump_print_symname
135 PARAMS ((bfd *, struct disassemble_info *, asymbol *));
136 static asymbol *find_symbol_for_address
137 PARAMS ((bfd *, asection *, bfd_vma, boolean, long *));
138 static void objdump_print_addr_with_sym
139 PARAMS ((bfd *, asection *, asymbol *, bfd_vma,
140 struct disassemble_info *, boolean));
141 static void objdump_print_addr
142 PARAMS ((bfd_vma, struct disassemble_info *, boolean));
143 static void objdump_print_address
144 PARAMS ((bfd_vma, struct disassemble_info *));
145 static int objdump_symbol_at_address
146 PARAMS ((bfd_vma, struct disassemble_info *));
147 static void show_line PARAMS ((bfd *, asection *, bfd_vma));
148 static void disassemble_bytes
149 PARAMS ((struct disassemble_info *, disassembler_ftype, boolean,
150 bfd_byte *, bfd_vma, bfd_vma, arelent ***, arelent **));
151 static void disassemble_data PARAMS ((bfd *));
152 static const char *endian_string PARAMS ((enum bfd_endian));
153 static asymbol ** slurp_symtab PARAMS ((bfd *));
154 static asymbol ** slurp_dynamic_symtab PARAMS ((bfd *));
155 static long remove_useless_symbols PARAMS ((asymbol **, long));
156 static int compare_symbols PARAMS ((const PTR, const PTR));
157 static int compare_relocs PARAMS ((const PTR, const PTR));
158 static void dump_stabs PARAMS ((bfd *));
159 static boolean read_section_stabs PARAMS ((bfd *, const char *, const char *));
160 static void print_section_stabs PARAMS ((bfd *, const char *, const char *));
162 static void
163 usage (stream, status)
164 FILE *stream;
165 int status;
167 fprintf (stream, _("Usage: %s <option(s)> <file(s)>\n"), program_name);
168 fprintf (stream, _(" Display information from object <file(s)>.\n"));
169 fprintf (stream, _(" At least one of the following switches must be given:\n"));
170 fprintf (stream, _("\
171 -a, --archive-headers Display archive header information\n\
172 -f, --file-headers Display the contents of the overall file header\n\
173 -p, --private-headers Display object format specific file header contents\n\
174 -h, --[section-]headers Display the contents of the section headers\n\
175 -x, --all-headers Display the contents of all headers\n\
176 -d, --disassemble Display assembler contents of executable sections\n\
177 -D, --disassemble-all Display assembler contents of all sections\n\
178 -S, --source Intermix source code with disassembly\n\
179 -s, --full-contents Display the full contents of all sections requested\n\
180 -g, --debugging Display debug information in object file\n\
181 -G, --stabs Display (in raw form) any STABS info in the file\n\
182 -t, --syms Display the contents of the symbol table(s)\n\
183 -T, --dynamic-syms Display the contents of the dynamic symbol table\n\
184 -r, --reloc Display the relocation entries in the file\n\
185 -R, --dynamic-reloc Display the dynamic relocation entries in the file\n\
186 -v, --version Display this program's version number\n\
187 -i, --info List object formats and architectures supported\n\
188 -H, --help Display this information\n\
189 "));
190 if (status != 2)
192 fprintf (stream, _("\n The following switches are optional:\n"));
193 fprintf (stream, _("\
194 -b, --target=BFDNAME Specify the target object format as BFDNAME\n\
195 -m, --architecture=MACHINE Specify the target architecture as MACHINE\n\
196 -j, --section=NAME Only display information for section NAME\n\
197 -M, --disassembler-options=OPT Pass text OPT on to the disassembler\n\
198 -EB --endian=big Assume big endian format when disassembling\n\
199 -EL --endian=little Assume little endian format when disassembling\n\
200 --file-start-context Include context from start of file (with -S)\n\
201 -l, --line-numbers Include line numbers and filenames in output\n\
202 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n\
203 The STYLE, if specified, can be `auto', 'gnu',\n\
204 'lucid', 'arm', 'hp', 'edg', or 'gnu-v3'\n\
205 -w, --wide Format output for more than 80 columns\n\
206 -z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling\n\
207 --start-address=ADDR Only process data whoes address is >= ADDR\n\
208 --stop-address=ADDR Only process data whoes address is <= ADDR\n\
209 --prefix-addresses Print complete address alongside disassembly\n\
210 --[no-]show-raw-insn Display hex alongside symbolic disassembly\n\
211 --adjust-vma=OFFSET Add OFFSET to all displayed section addresses\n\
212 \n"));
213 list_supported_targets (program_name, stream);
214 list_supported_architectures (program_name, stream);
216 disassembler_usage (stream);
218 if (status == 0)
219 fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
220 exit (status);
223 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
225 #define OPTION_ENDIAN (150)
226 #define OPTION_START_ADDRESS (OPTION_ENDIAN + 1)
227 #define OPTION_STOP_ADDRESS (OPTION_START_ADDRESS + 1)
228 #define OPTION_ADJUST_VMA (OPTION_STOP_ADDRESS + 1)
230 static struct option long_options[]=
232 {"adjust-vma", required_argument, NULL, OPTION_ADJUST_VMA},
233 {"all-headers", no_argument, NULL, 'x'},
234 {"private-headers", no_argument, NULL, 'p'},
235 {"architecture", required_argument, NULL, 'm'},
236 {"archive-headers", no_argument, NULL, 'a'},
237 {"debugging", no_argument, NULL, 'g'},
238 {"demangle", optional_argument, NULL, 'C'},
239 {"disassemble", no_argument, NULL, 'd'},
240 {"disassemble-all", no_argument, NULL, 'D'},
241 {"disassembler-options", required_argument, NULL, 'M'},
242 {"disassemble-zeroes", no_argument, NULL, 'z'},
243 {"dynamic-reloc", no_argument, NULL, 'R'},
244 {"dynamic-syms", no_argument, NULL, 'T'},
245 {"endian", required_argument, NULL, OPTION_ENDIAN},
246 {"file-headers", no_argument, NULL, 'f'},
247 {"file-start-context", no_argument, &file_start_context, 1},
248 {"full-contents", no_argument, NULL, 's'},
249 {"headers", no_argument, NULL, 'h'},
250 {"help", no_argument, NULL, 'H'},
251 {"info", no_argument, NULL, 'i'},
252 {"line-numbers", no_argument, NULL, 'l'},
253 {"no-show-raw-insn", no_argument, &show_raw_insn, -1},
254 {"prefix-addresses", no_argument, &prefix_addresses, 1},
255 {"reloc", no_argument, NULL, 'r'},
256 {"section", required_argument, NULL, 'j'},
257 {"section-headers", no_argument, NULL, 'h'},
258 {"show-raw-insn", no_argument, &show_raw_insn, 1},
259 {"source", no_argument, NULL, 'S'},
260 {"stabs", no_argument, NULL, 'G'},
261 {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
262 {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
263 {"syms", no_argument, NULL, 't'},
264 {"target", required_argument, NULL, 'b'},
265 {"version", no_argument, NULL, 'V'},
266 {"wide", no_argument, NULL, 'w'},
267 {0, no_argument, 0, 0}
270 static void
271 nonfatal (msg)
272 const char *msg;
274 bfd_nonfatal (msg);
275 exit_status = 1;
278 static void
279 dump_section_header (abfd, section, ignored)
280 bfd *abfd ATTRIBUTE_UNUSED;
281 asection *section;
282 PTR ignored ATTRIBUTE_UNUSED;
284 char *comma = "";
285 unsigned int opb = bfd_octets_per_byte (abfd);
287 printf ("%3d %-13s %08lx ", section->index,
288 bfd_get_section_name (abfd, section),
289 (unsigned long) bfd_section_size (abfd, section) / opb);
290 bfd_printf_vma (abfd, bfd_get_section_vma (abfd, section));
291 printf (" ");
292 bfd_printf_vma (abfd, section->lma);
293 printf (" %08lx 2**%u", (unsigned long) section->filepos,
294 bfd_get_section_alignment (abfd, section));
295 if (! wide_output)
296 printf ("\n ");
297 printf (" ");
299 #define PF(x, y) \
300 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
302 PF (SEC_HAS_CONTENTS, "CONTENTS");
303 PF (SEC_ALLOC, "ALLOC");
304 PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
305 PF (SEC_LOAD, "LOAD");
306 PF (SEC_RELOC, "RELOC");
307 PF (SEC_READONLY, "READONLY");
308 PF (SEC_CODE, "CODE");
309 PF (SEC_DATA, "DATA");
310 PF (SEC_ROM, "ROM");
311 PF (SEC_DEBUGGING, "DEBUGGING");
312 PF (SEC_NEVER_LOAD, "NEVER_LOAD");
313 PF (SEC_EXCLUDE, "EXCLUDE");
314 PF (SEC_SORT_ENTRIES, "SORT_ENTRIES");
315 PF (SEC_BLOCK, "BLOCK");
316 PF (SEC_CLINK, "CLINK");
317 PF (SEC_SMALL_DATA, "SMALL_DATA");
318 PF (SEC_SHARED, "SHARED");
319 PF (SEC_ARCH_BIT_0, "ARCH_BIT_0");
320 PF (SEC_THREAD_LOCAL, "THREAD_LOCAL");
322 if ((section->flags & SEC_LINK_ONCE) != 0)
324 const char *ls;
326 switch (section->flags & SEC_LINK_DUPLICATES)
328 default:
329 abort ();
330 case SEC_LINK_DUPLICATES_DISCARD:
331 ls = "LINK_ONCE_DISCARD";
332 break;
333 case SEC_LINK_DUPLICATES_ONE_ONLY:
334 ls = "LINK_ONCE_ONE_ONLY";
335 break;
336 case SEC_LINK_DUPLICATES_SAME_SIZE:
337 ls = "LINK_ONCE_SAME_SIZE";
338 break;
339 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
340 ls = "LINK_ONCE_SAME_CONTENTS";
341 break;
343 printf ("%s%s", comma, ls);
345 if (section->comdat != NULL)
346 printf (" (COMDAT %s %ld)", section->comdat->name,
347 section->comdat->symbol);
349 comma = ", ";
352 printf ("\n");
353 #undef PF
356 static void
357 dump_headers (abfd)
358 bfd *abfd;
360 printf (_("Sections:\n"));
362 #ifndef BFD64
363 printf (_("Idx Name Size VMA LMA File off Algn"));
364 #else
365 /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */
366 if (bfd_get_arch_size (abfd) == 32)
367 printf (_("Idx Name Size VMA LMA File off Algn"));
368 else
369 printf (_("Idx Name Size VMA LMA File off Algn"));
370 #endif
372 if (wide_output)
373 printf (_(" Flags"));
374 if (abfd->flags & HAS_LOAD_PAGE)
375 printf (_(" Pg"));
376 printf ("\n");
378 bfd_map_over_sections (abfd, dump_section_header, (PTR) NULL);
381 static asymbol **
382 slurp_symtab (abfd)
383 bfd *abfd;
385 asymbol **sy = (asymbol **) NULL;
386 long storage;
388 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
390 symcount = 0;
391 return NULL;
394 storage = bfd_get_symtab_upper_bound (abfd);
395 if (storage < 0)
396 bfd_fatal (bfd_get_filename (abfd));
397 if (storage)
398 sy = (asymbol **) xmalloc (storage);
400 symcount = bfd_canonicalize_symtab (abfd, sy);
401 if (symcount < 0)
402 bfd_fatal (bfd_get_filename (abfd));
403 return sy;
406 /* Read in the dynamic symbols. */
408 static asymbol **
409 slurp_dynamic_symtab (abfd)
410 bfd *abfd;
412 asymbol **sy = (asymbol **) NULL;
413 long storage;
415 storage = bfd_get_dynamic_symtab_upper_bound (abfd);
416 if (storage < 0)
418 if (!(bfd_get_file_flags (abfd) & DYNAMIC))
420 non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd));
421 dynsymcount = 0;
422 return NULL;
425 bfd_fatal (bfd_get_filename (abfd));
427 if (storage)
428 sy = (asymbol **) xmalloc (storage);
430 dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
431 if (dynsymcount < 0)
432 bfd_fatal (bfd_get_filename (abfd));
433 return sy;
436 /* Filter out (in place) symbols that are useless for disassembly.
437 COUNT is the number of elements in SYMBOLS.
438 Return the number of useful symbols. */
440 static long
441 remove_useless_symbols (symbols, count)
442 asymbol **symbols;
443 long count;
445 register asymbol **in_ptr = symbols, **out_ptr = symbols;
447 while (--count >= 0)
449 asymbol *sym = *in_ptr++;
451 if (sym->name == NULL || sym->name[0] == '\0')
452 continue;
453 if (sym->flags & (BSF_DEBUGGING))
454 continue;
455 if (bfd_is_und_section (sym->section)
456 || bfd_is_com_section (sym->section))
457 continue;
459 *out_ptr++ = sym;
461 return out_ptr - symbols;
464 /* Sort symbols into value order. */
466 static int
467 compare_symbols (ap, bp)
468 const PTR ap;
469 const PTR bp;
471 const asymbol *a = *(const asymbol **)ap;
472 const asymbol *b = *(const asymbol **)bp;
473 const char *an, *bn;
474 size_t anl, bnl;
475 boolean af, bf;
476 flagword aflags, bflags;
478 if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
479 return 1;
480 else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
481 return -1;
483 if (a->section > b->section)
484 return 1;
485 else if (a->section < b->section)
486 return -1;
488 an = bfd_asymbol_name (a);
489 bn = bfd_asymbol_name (b);
490 anl = strlen (an);
491 bnl = strlen (bn);
493 /* The symbols gnu_compiled and gcc2_compiled convey no real
494 information, so put them after other symbols with the same value. */
496 af = (strstr (an, "gnu_compiled") != NULL
497 || strstr (an, "gcc2_compiled") != NULL);
498 bf = (strstr (bn, "gnu_compiled") != NULL
499 || strstr (bn, "gcc2_compiled") != NULL);
501 if (af && ! bf)
502 return 1;
503 if (! af && bf)
504 return -1;
506 /* We use a heuristic for the file name, to try to sort it after
507 more useful symbols. It may not work on non Unix systems, but it
508 doesn't really matter; the only difference is precisely which
509 symbol names get printed. */
511 #define file_symbol(s, sn, snl) \
512 (((s)->flags & BSF_FILE) != 0 \
513 || ((sn)[(snl) - 2] == '.' \
514 && ((sn)[(snl) - 1] == 'o' \
515 || (sn)[(snl) - 1] == 'a')))
517 af = file_symbol (a, an, anl);
518 bf = file_symbol (b, bn, bnl);
520 if (af && ! bf)
521 return 1;
522 if (! af && bf)
523 return -1;
525 /* Try to sort global symbols before local symbols before function
526 symbols before debugging symbols. */
528 aflags = a->flags;
529 bflags = b->flags;
531 if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING))
533 if ((aflags & BSF_DEBUGGING) != 0)
534 return 1;
535 else
536 return -1;
538 if ((aflags & BSF_FUNCTION) != (bflags & BSF_FUNCTION))
540 if ((aflags & BSF_FUNCTION) != 0)
541 return -1;
542 else
543 return 1;
545 if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL))
547 if ((aflags & BSF_LOCAL) != 0)
548 return 1;
549 else
550 return -1;
552 if ((aflags & BSF_GLOBAL) != (bflags & BSF_GLOBAL))
554 if ((aflags & BSF_GLOBAL) != 0)
555 return -1;
556 else
557 return 1;
560 /* Symbols that start with '.' might be section names, so sort them
561 after symbols that don't start with '.'. */
562 if (an[0] == '.' && bn[0] != '.')
563 return 1;
564 if (an[0] != '.' && bn[0] == '.')
565 return -1;
567 /* Finally, if we can't distinguish them in any other way, try to
568 get consistent results by sorting the symbols by name. */
569 return strcmp (an, bn);
572 /* Sort relocs into address order. */
574 static int
575 compare_relocs (ap, bp)
576 const PTR ap;
577 const PTR bp;
579 const arelent *a = *(const arelent **)ap;
580 const arelent *b = *(const arelent **)bp;
582 if (a->address > b->address)
583 return 1;
584 else if (a->address < b->address)
585 return -1;
587 /* So that associated relocations tied to the same address show up
588 in the correct order, we don't do any further sorting. */
589 if (a > b)
590 return 1;
591 else if (a < b)
592 return -1;
593 else
594 return 0;
597 /* Print VMA to STREAM. If SKIP_ZEROES is true, omit leading zeroes. */
599 static void
600 objdump_print_value (vma, info, skip_zeroes)
601 bfd_vma vma;
602 struct disassemble_info *info;
603 boolean skip_zeroes;
605 char buf[30];
606 char *p;
607 struct objdump_disasm_info *aux
608 = (struct objdump_disasm_info *) info->application_data;
610 bfd_sprintf_vma (aux->abfd, buf, vma);
611 if (! skip_zeroes)
612 p = buf;
613 else
615 for (p = buf; *p == '0'; ++p)
617 if (*p == '\0')
618 --p;
620 (*info->fprintf_func) (info->stream, "%s", p);
623 /* Print the name of a symbol. */
625 static void
626 objdump_print_symname (abfd, info, sym)
627 bfd *abfd;
628 struct disassemble_info *info;
629 asymbol *sym;
631 char *alloc;
632 const char *name;
634 alloc = NULL;
635 name = bfd_asymbol_name (sym);
636 if (do_demangle && name[0] != '\0')
638 /* Demangle the name. */
639 alloc = demangle (abfd, name);
640 name = alloc;
643 if (info != NULL)
644 (*info->fprintf_func) (info->stream, "%s", name);
645 else
646 printf ("%s", name);
648 if (alloc != NULL)
649 free (alloc);
652 /* Locate a symbol given a bfd, a section, and a VMA. If REQUIRE_SEC
653 is true, then always require the symbol to be in the section. This
654 returns NULL if there is no suitable symbol. If PLACE is not NULL,
655 then *PLACE is set to the index of the symbol in sorted_syms. */
657 static asymbol *
658 find_symbol_for_address (abfd, sec, vma, require_sec, place)
659 bfd *abfd;
660 asection *sec;
661 bfd_vma vma;
662 boolean require_sec;
663 long *place;
665 /* @@ Would it speed things up to cache the last two symbols returned,
666 and maybe their address ranges? For many processors, only one memory
667 operand can be present at a time, so the 2-entry cache wouldn't be
668 constantly churned by code doing heavy memory accesses. */
670 /* Indices in `sorted_syms'. */
671 long min = 0;
672 long max = sorted_symcount;
673 long thisplace;
674 unsigned int opb = bfd_octets_per_byte (abfd);
676 if (sorted_symcount < 1)
677 return NULL;
679 /* Perform a binary search looking for the closest symbol to the
680 required value. We are searching the range (min, max]. */
681 while (min + 1 < max)
683 asymbol *sym;
685 thisplace = (max + min) / 2;
686 sym = sorted_syms[thisplace];
688 if (bfd_asymbol_value (sym) > vma)
689 max = thisplace;
690 else if (bfd_asymbol_value (sym) < vma)
691 min = thisplace;
692 else
694 min = thisplace;
695 break;
699 /* The symbol we want is now in min, the low end of the range we
700 were searching. If there are several symbols with the same
701 value, we want the first one. */
702 thisplace = min;
703 while (thisplace > 0
704 && (bfd_asymbol_value (sorted_syms[thisplace])
705 == bfd_asymbol_value (sorted_syms[thisplace - 1])))
706 --thisplace;
708 /* If the file is relocateable, and the symbol could be from this
709 section, prefer a symbol from this section over symbols from
710 others, even if the other symbol's value might be closer.
712 Note that this may be wrong for some symbol references if the
713 sections have overlapping memory ranges, but in that case there's
714 no way to tell what's desired without looking at the relocation
715 table. */
717 if (sorted_syms[thisplace]->section != sec
718 && (require_sec
719 || ((abfd->flags & HAS_RELOC) != 0
720 && vma >= bfd_get_section_vma (abfd, sec)
721 && vma < (bfd_get_section_vma (abfd, sec)
722 + bfd_section_size (abfd, sec) / opb))))
724 long i;
726 for (i = thisplace + 1; i < sorted_symcount; i++)
728 if (bfd_asymbol_value (sorted_syms[i])
729 != bfd_asymbol_value (sorted_syms[thisplace]))
730 break;
732 --i;
733 for (; i >= 0; i--)
735 if (sorted_syms[i]->section == sec
736 && (i == 0
737 || sorted_syms[i - 1]->section != sec
738 || (bfd_asymbol_value (sorted_syms[i])
739 != bfd_asymbol_value (sorted_syms[i - 1]))))
741 thisplace = i;
742 break;
746 if (sorted_syms[thisplace]->section != sec)
748 /* We didn't find a good symbol with a smaller value.
749 Look for one with a larger value. */
750 for (i = thisplace + 1; i < sorted_symcount; i++)
752 if (sorted_syms[i]->section == sec)
754 thisplace = i;
755 break;
760 if (sorted_syms[thisplace]->section != sec
761 && (require_sec
762 || ((abfd->flags & HAS_RELOC) != 0
763 && vma >= bfd_get_section_vma (abfd, sec)
764 && vma < (bfd_get_section_vma (abfd, sec)
765 + bfd_section_size (abfd, sec)))))
767 /* There is no suitable symbol. */
768 return NULL;
772 if (place != NULL)
773 *place = thisplace;
775 return sorted_syms[thisplace];
778 /* Print an address to INFO symbolically. */
780 static void
781 objdump_print_addr_with_sym (abfd, sec, sym, vma, info, skip_zeroes)
782 bfd *abfd;
783 asection *sec;
784 asymbol *sym;
785 bfd_vma vma;
786 struct disassemble_info *info;
787 boolean skip_zeroes;
789 objdump_print_value (vma, info, skip_zeroes);
791 if (sym == NULL)
793 bfd_vma secaddr;
795 (*info->fprintf_func) (info->stream, " <%s",
796 bfd_get_section_name (abfd, sec));
797 secaddr = bfd_get_section_vma (abfd, sec);
798 if (vma < secaddr)
800 (*info->fprintf_func) (info->stream, "-0x");
801 objdump_print_value (secaddr - vma, info, true);
803 else if (vma > secaddr)
805 (*info->fprintf_func) (info->stream, "+0x");
806 objdump_print_value (vma - secaddr, info, true);
808 (*info->fprintf_func) (info->stream, ">");
810 else
812 (*info->fprintf_func) (info->stream, " <");
813 objdump_print_symname (abfd, info, sym);
814 if (bfd_asymbol_value (sym) > vma)
816 (*info->fprintf_func) (info->stream, "-0x");
817 objdump_print_value (bfd_asymbol_value (sym) - vma, info, true);
819 else if (vma > bfd_asymbol_value (sym))
821 (*info->fprintf_func) (info->stream, "+0x");
822 objdump_print_value (vma - bfd_asymbol_value (sym), info, true);
824 (*info->fprintf_func) (info->stream, ">");
828 /* Print VMA to INFO, symbolically if possible. If SKIP_ZEROES is
829 true, don't output leading zeroes. */
831 static void
832 objdump_print_addr (vma, info, skip_zeroes)
833 bfd_vma vma;
834 struct disassemble_info *info;
835 boolean skip_zeroes;
837 struct objdump_disasm_info *aux;
838 asymbol *sym;
840 if (sorted_symcount < 1)
842 (*info->fprintf_func) (info->stream, "0x");
843 objdump_print_value (vma, info, skip_zeroes);
844 return;
847 aux = (struct objdump_disasm_info *) info->application_data;
848 sym = find_symbol_for_address (aux->abfd, aux->sec, vma, aux->require_sec,
849 (long *) NULL);
850 objdump_print_addr_with_sym (aux->abfd, aux->sec, sym, vma, info,
851 skip_zeroes);
854 /* Print VMA to INFO. This function is passed to the disassembler
855 routine. */
857 static void
858 objdump_print_address (vma, info)
859 bfd_vma vma;
860 struct disassemble_info *info;
862 objdump_print_addr (vma, info, ! prefix_addresses);
865 /* Determine of the given address has a symbol associated with it. */
867 static int
868 objdump_symbol_at_address (vma, info)
869 bfd_vma vma;
870 struct disassemble_info * info;
872 struct objdump_disasm_info * aux;
873 asymbol * sym;
875 /* No symbols - do not bother checking. */
876 if (sorted_symcount < 1)
877 return 0;
879 aux = (struct objdump_disasm_info *) info->application_data;
880 sym = find_symbol_for_address (aux->abfd, aux->sec, vma, aux->require_sec,
881 (long *) NULL);
883 return (sym != NULL && (bfd_asymbol_value (sym) == vma));
886 /* Hold the last function name and the last line number we displayed
887 in a disassembly. */
889 static char *prev_functionname;
890 static unsigned int prev_line;
892 /* We keep a list of all files that we have seen when doing a
893 dissassembly with source, so that we know how much of the file to
894 display. This can be important for inlined functions. */
896 struct print_file_list
898 struct print_file_list *next;
899 char *filename;
900 unsigned int line;
901 FILE *f;
904 static struct print_file_list *print_files;
906 /* The number of preceding context lines to show when we start
907 displaying a file for the first time. */
909 #define SHOW_PRECEDING_CONTEXT_LINES (5)
911 /* Skip ahead to a given line in a file, optionally printing each
912 line. */
914 static void
915 skip_to_line PARAMS ((struct print_file_list *, unsigned int, boolean));
917 static void
918 skip_to_line (p, line, show)
919 struct print_file_list *p;
920 unsigned int line;
921 boolean show;
923 while (p->line < line)
925 char buf[100];
927 if (fgets (buf, sizeof buf, p->f) == NULL)
929 fclose (p->f);
930 p->f = NULL;
931 break;
934 if (show)
935 printf ("%s", buf);
937 if (strchr (buf, '\n') != NULL)
938 ++p->line;
942 /* Show the line number, or the source line, in a dissassembly
943 listing. */
945 static void
946 show_line (abfd, section, addr_offset)
947 bfd *abfd;
948 asection *section;
949 bfd_vma addr_offset;
951 const char *filename;
952 const char *functionname;
953 unsigned int line;
955 if (! with_line_numbers && ! with_source_code)
956 return;
958 if (! bfd_find_nearest_line (abfd, section, syms, addr_offset, &filename,
959 &functionname, &line))
960 return;
962 if (filename != NULL && *filename == '\0')
963 filename = NULL;
964 if (functionname != NULL && *functionname == '\0')
965 functionname = NULL;
967 if (with_line_numbers)
969 if (functionname != NULL
970 && (prev_functionname == NULL
971 || strcmp (functionname, prev_functionname) != 0))
972 printf ("%s():\n", functionname);
973 if (line > 0 && line != prev_line)
974 printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
977 if (with_source_code
978 && filename != NULL
979 && line > 0)
981 struct print_file_list **pp, *p;
983 for (pp = &print_files; *pp != NULL; pp = &(*pp)->next)
984 if (strcmp ((*pp)->filename, filename) == 0)
985 break;
986 p = *pp;
988 if (p != NULL)
990 if (p != print_files)
992 int l;
994 /* We have reencountered a file name which we saw
995 earlier. This implies that either we are dumping out
996 code from an included file, or the same file was
997 linked in more than once. There are two common cases
998 of an included file: inline functions in a header
999 file, and a bison or flex skeleton file. In the
1000 former case we want to just start printing (but we
1001 back up a few lines to give context); in the latter
1002 case we want to continue from where we left off. I
1003 can't think of a good way to distinguish the cases,
1004 so I used a heuristic based on the file name. */
1005 if (strcmp (p->filename + strlen (p->filename) - 2, ".h") != 0)
1006 l = p->line;
1007 else
1009 l = line - SHOW_PRECEDING_CONTEXT_LINES;
1010 if (l < 0)
1011 l = 0;
1014 if (p->f == NULL)
1016 p->f = fopen (p->filename, "r");
1017 p->line = 0;
1019 if (p->f != NULL)
1020 skip_to_line (p, l, false);
1022 if (print_files->f != NULL)
1024 fclose (print_files->f);
1025 print_files->f = NULL;
1029 if (p->f != NULL)
1031 skip_to_line (p, line, true);
1032 *pp = p->next;
1033 p->next = print_files;
1034 print_files = p;
1037 else
1039 FILE *f;
1041 f = fopen (filename, "r");
1042 if (f != NULL)
1044 int l;
1046 p = ((struct print_file_list *)
1047 xmalloc (sizeof (struct print_file_list)));
1048 p->filename = xmalloc (strlen (filename) + 1);
1049 strcpy (p->filename, filename);
1050 p->line = 0;
1051 p->f = f;
1053 if (print_files != NULL && print_files->f != NULL)
1055 fclose (print_files->f);
1056 print_files->f = NULL;
1058 p->next = print_files;
1059 print_files = p;
1061 if (file_start_context)
1062 l = 0;
1063 else
1064 l = line - SHOW_PRECEDING_CONTEXT_LINES;
1065 if (l < 0)
1066 l = 0;
1067 skip_to_line (p, l, false);
1068 if (p->f != NULL)
1069 skip_to_line (p, line, true);
1074 if (functionname != NULL
1075 && (prev_functionname == NULL
1076 || strcmp (functionname, prev_functionname) != 0))
1078 if (prev_functionname != NULL)
1079 free (prev_functionname);
1080 prev_functionname = xmalloc (strlen (functionname) + 1);
1081 strcpy (prev_functionname, functionname);
1084 if (line > 0 && line != prev_line)
1085 prev_line = line;
1088 /* Pseudo FILE object for strings. */
1089 typedef struct
1091 char *buffer;
1092 size_t size;
1093 char *current;
1094 } SFILE;
1096 /* sprintf to a "stream" */
1098 static int
1099 objdump_sprintf VPARAMS ((SFILE *f, const char *format, ...))
1101 char *buf;
1102 size_t n;
1104 VA_OPEN (args, format);
1105 VA_FIXEDARG (args, SFILE *, f);
1106 VA_FIXEDARG (args, const char *, format);
1108 vasprintf (&buf, format, args);
1110 if (buf == NULL)
1112 va_end (args);
1113 fatal (_("Out of virtual memory"));
1116 n = strlen (buf);
1118 while ((size_t) ((f->buffer + f->size) - f->current) < n + 1)
1120 size_t curroff;
1122 curroff = f->current - f->buffer;
1123 f->size *= 2;
1124 f->buffer = xrealloc (f->buffer, f->size);
1125 f->current = f->buffer + curroff;
1128 memcpy (f->current, buf, n);
1129 f->current += n;
1130 f->current[0] = '\0';
1132 free (buf);
1134 VA_CLOSE (args);
1135 return n;
1138 /* The number of zeroes we want to see before we start skipping them.
1139 The number is arbitrarily chosen. */
1141 #ifndef SKIP_ZEROES
1142 #define SKIP_ZEROES (8)
1143 #endif
1145 /* The number of zeroes to skip at the end of a section. If the
1146 number of zeroes at the end is between SKIP_ZEROES_AT_END and
1147 SKIP_ZEROES, they will be disassembled. If there are fewer than
1148 SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic
1149 attempt to avoid disassembling zeroes inserted by section
1150 alignment. */
1152 #ifndef SKIP_ZEROES_AT_END
1153 #define SKIP_ZEROES_AT_END (3)
1154 #endif
1156 /* Disassemble some data in memory between given values. */
1158 static void
1159 disassemble_bytes (info, disassemble_fn, insns, data,
1160 start_offset, stop_offset, relppp,
1161 relppend)
1162 struct disassemble_info *info;
1163 disassembler_ftype disassemble_fn;
1164 boolean insns;
1165 bfd_byte *data;
1166 bfd_vma start_offset;
1167 bfd_vma stop_offset;
1168 arelent ***relppp;
1169 arelent **relppend;
1171 struct objdump_disasm_info *aux;
1172 asection *section;
1173 int octets_per_line;
1174 boolean done_dot;
1175 int skip_addr_chars;
1176 bfd_vma addr_offset;
1177 int opb = info->octets_per_byte;
1179 aux = (struct objdump_disasm_info *) info->application_data;
1180 section = aux->sec;
1182 if (insns)
1183 octets_per_line = 4;
1184 else
1185 octets_per_line = 16;
1187 /* Figure out how many characters to skip at the start of an
1188 address, to make the disassembly look nicer. We discard leading
1189 zeroes in chunks of 4, ensuring that there is always a leading
1190 zero remaining. */
1191 skip_addr_chars = 0;
1192 if (! prefix_addresses)
1194 char buf[30];
1195 char *s;
1197 bfd_sprintf_vma
1198 (aux->abfd, buf,
1199 (section->vma
1200 + bfd_section_size (section->owner, section) / opb));
1201 s = buf;
1202 while (s[0] == '0' && s[1] == '0' && s[2] == '0' && s[3] == '0'
1203 && s[4] == '0')
1205 skip_addr_chars += 4;
1206 s += 4;
1210 info->insn_info_valid = 0;
1212 done_dot = false;
1213 addr_offset = start_offset;
1214 while (addr_offset < stop_offset)
1216 bfd_vma z;
1217 int octets = 0;
1218 boolean need_nl = false;
1220 /* If we see more than SKIP_ZEROES octets of zeroes, we just
1221 print `...'. */
1222 for (z = addr_offset * opb; z < stop_offset * opb; z++)
1223 if (data[z] != 0)
1224 break;
1225 if (! disassemble_zeroes
1226 && (info->insn_info_valid == 0
1227 || info->branch_delay_insns == 0)
1228 && (z - addr_offset * opb >= SKIP_ZEROES
1229 || (z == stop_offset * opb &&
1230 z - addr_offset * opb < SKIP_ZEROES_AT_END)))
1232 printf ("\t...\n");
1234 /* If there are more nonzero octets to follow, we only skip
1235 zeroes in multiples of 4, to try to avoid running over
1236 the start of an instruction which happens to start with
1237 zero. */
1238 if (z != stop_offset * opb)
1239 z = addr_offset * opb + ((z - addr_offset * opb) &~ 3);
1241 octets = z - addr_offset * opb;
1243 else
1245 char buf[50];
1246 SFILE sfile;
1247 int bpc = 0;
1248 int pb = 0;
1250 done_dot = false;
1252 if (with_line_numbers || with_source_code)
1253 /* The line number tables will refer to unadjusted
1254 section VMAs, so we must undo any VMA modifications
1255 when calling show_line. */
1256 show_line (aux->abfd, section, addr_offset - adjust_section_vma);
1258 if (! prefix_addresses)
1260 char *s;
1262 bfd_sprintf_vma (aux->abfd, buf, section->vma + addr_offset);
1263 for (s = buf + skip_addr_chars; *s == '0'; s++)
1264 *s = ' ';
1265 if (*s == '\0')
1266 *--s = '0';
1267 printf ("%s:\t", buf + skip_addr_chars);
1269 else
1271 aux->require_sec = true;
1272 objdump_print_address (section->vma + addr_offset, info);
1273 aux->require_sec = false;
1274 putchar (' ');
1277 if (insns)
1279 sfile.size = 120;
1280 sfile.buffer = xmalloc (sfile.size);
1281 sfile.current = sfile.buffer;
1282 info->fprintf_func = (fprintf_ftype) objdump_sprintf;
1283 info->stream = (FILE *) &sfile;
1284 info->bytes_per_line = 0;
1285 info->bytes_per_chunk = 0;
1287 #ifdef DISASSEMBLER_NEEDS_RELOCS
1288 /* FIXME: This is wrong. It tests the number of octets
1289 in the last instruction, not the current one. */
1290 if (*relppp < relppend
1291 && (**relppp)->address >= addr_offset
1292 && (**relppp)->address <= addr_offset + octets / opb)
1293 info->flags = INSN_HAS_RELOC;
1294 else
1295 #endif
1296 info->flags = 0;
1298 octets = (*disassemble_fn) (section->vma + addr_offset, info);
1299 info->fprintf_func = (fprintf_ftype) fprintf;
1300 info->stream = stdout;
1301 if (info->bytes_per_line != 0)
1302 octets_per_line = info->bytes_per_line;
1303 if (octets < 0)
1305 if (sfile.current != sfile.buffer)
1306 printf ("%s\n", sfile.buffer);
1307 free (sfile.buffer);
1308 break;
1311 else
1313 bfd_vma j;
1315 octets = octets_per_line;
1316 if (addr_offset + octets / opb > stop_offset)
1317 octets = (stop_offset - addr_offset) * opb;
1319 for (j = addr_offset * opb; j < addr_offset * opb + octets; ++j)
1321 if (ISPRINT (data[j]))
1322 buf[j - addr_offset * opb] = data[j];
1323 else
1324 buf[j - addr_offset * opb] = '.';
1326 buf[j - addr_offset * opb] = '\0';
1329 if (prefix_addresses
1330 ? show_raw_insn > 0
1331 : show_raw_insn >= 0)
1333 bfd_vma j;
1335 /* If ! prefix_addresses and ! wide_output, we print
1336 octets_per_line octets per line. */
1337 pb = octets;
1338 if (pb > octets_per_line && ! prefix_addresses && ! wide_output)
1339 pb = octets_per_line;
1341 if (info->bytes_per_chunk)
1342 bpc = info->bytes_per_chunk;
1343 else
1344 bpc = 1;
1346 for (j = addr_offset * opb; j < addr_offset * opb + pb; j += bpc)
1348 int k;
1349 if (bpc > 1 && info->display_endian == BFD_ENDIAN_LITTLE)
1351 for (k = bpc - 1; k >= 0; k--)
1352 printf ("%02x", (unsigned) data[j + k]);
1353 putchar (' ');
1355 else
1357 for (k = 0; k < bpc; k++)
1358 printf ("%02x", (unsigned) data[j + k]);
1359 putchar (' ');
1363 for (; pb < octets_per_line; pb += bpc)
1365 int k;
1367 for (k = 0; k < bpc; k++)
1368 printf (" ");
1369 putchar (' ');
1372 /* Separate raw data from instruction by extra space. */
1373 if (insns)
1374 putchar ('\t');
1375 else
1376 printf (" ");
1379 if (! insns)
1380 printf ("%s", buf);
1381 else
1383 printf ("%s", sfile.buffer);
1384 free (sfile.buffer);
1387 if (prefix_addresses
1388 ? show_raw_insn > 0
1389 : show_raw_insn >= 0)
1391 while (pb < octets)
1393 bfd_vma j;
1394 char *s;
1396 putchar ('\n');
1397 j = addr_offset * opb + pb;
1399 bfd_sprintf_vma (aux->abfd, buf, section->vma + j / opb);
1400 for (s = buf + skip_addr_chars; *s == '0'; s++)
1401 *s = ' ';
1402 if (*s == '\0')
1403 *--s = '0';
1404 printf ("%s:\t", buf + skip_addr_chars);
1406 pb += octets_per_line;
1407 if (pb > octets)
1408 pb = octets;
1409 for (; j < addr_offset * opb + pb; j += bpc)
1411 int k;
1413 if (bpc > 1 && info->display_endian == BFD_ENDIAN_LITTLE)
1415 for (k = bpc - 1; k >= 0; k--)
1416 printf ("%02x", (unsigned) data[j + k]);
1417 putchar (' ');
1419 else
1421 for (k = 0; k < bpc; k++)
1422 printf ("%02x", (unsigned) data[j + k]);
1423 putchar (' ');
1429 if (!wide_output)
1430 putchar ('\n');
1431 else
1432 need_nl = true;
1435 if ((section->flags & SEC_RELOC) != 0
1436 #ifndef DISASSEMBLER_NEEDS_RELOCS
1437 && dump_reloc_info
1438 #endif
1441 while ((*relppp) < relppend
1442 && ((**relppp)->address >= (bfd_vma) addr_offset
1443 && (**relppp)->address < (bfd_vma) addr_offset + octets / opb))
1444 #ifdef DISASSEMBLER_NEEDS_RELOCS
1445 if (! dump_reloc_info)
1446 ++(*relppp);
1447 else
1448 #endif
1450 arelent *q;
1452 q = **relppp;
1454 if (wide_output)
1455 putchar ('\t');
1456 else
1457 printf ("\t\t\t");
1459 objdump_print_value (section->vma + q->address, info, true);
1461 printf (": %s\t", q->howto->name);
1463 if (q->sym_ptr_ptr == NULL || *q->sym_ptr_ptr == NULL)
1464 printf ("*unknown*");
1465 else
1467 const char *sym_name;
1469 sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
1470 if (sym_name != NULL && *sym_name != '\0')
1471 objdump_print_symname (aux->abfd, info, *q->sym_ptr_ptr);
1472 else
1474 asection *sym_sec;
1476 sym_sec = bfd_get_section (*q->sym_ptr_ptr);
1477 sym_name = bfd_get_section_name (aux->abfd, sym_sec);
1478 if (sym_name == NULL || *sym_name == '\0')
1479 sym_name = "*unknown*";
1480 printf ("%s", sym_name);
1484 if (q->addend)
1486 printf ("+0x");
1487 objdump_print_value (q->addend, info, true);
1490 printf ("\n");
1491 need_nl = false;
1492 ++(*relppp);
1496 if (need_nl)
1497 printf ("\n");
1499 addr_offset += octets / opb;
1503 /* Disassemble the contents of an object file. */
1505 static void
1506 disassemble_data (abfd)
1507 bfd *abfd;
1509 unsigned long addr_offset;
1510 disassembler_ftype disassemble_fn;
1511 struct disassemble_info disasm_info;
1512 struct objdump_disasm_info aux;
1513 asection *section;
1514 unsigned int opb;
1516 print_files = NULL;
1517 prev_functionname = NULL;
1518 prev_line = -1;
1520 /* We make a copy of syms to sort. We don't want to sort syms
1521 because that will screw up the relocs. */
1522 sorted_syms = (asymbol **) xmalloc (symcount * sizeof (asymbol *));
1523 memcpy (sorted_syms, syms, symcount * sizeof (asymbol *));
1525 sorted_symcount = remove_useless_symbols (sorted_syms, symcount);
1527 /* Sort the symbols into section and symbol order */
1528 qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols);
1530 INIT_DISASSEMBLE_INFO(disasm_info, stdout, fprintf);
1531 disasm_info.application_data = (PTR) &aux;
1532 aux.abfd = abfd;
1533 aux.require_sec = false;
1534 disasm_info.print_address_func = objdump_print_address;
1535 disasm_info.symbol_at_address_func = objdump_symbol_at_address;
1537 if (machine != (char *) NULL)
1539 const bfd_arch_info_type *info = bfd_scan_arch (machine);
1540 if (info == NULL)
1542 fatal (_("Can't use supplied machine %s"), machine);
1544 abfd->arch_info = info;
1547 if (endian != BFD_ENDIAN_UNKNOWN)
1549 struct bfd_target *xvec;
1551 xvec = (struct bfd_target *) xmalloc (sizeof (struct bfd_target));
1552 memcpy (xvec, abfd->xvec, sizeof (struct bfd_target));
1553 xvec->byteorder = endian;
1554 abfd->xvec = xvec;
1557 disassemble_fn = disassembler (abfd);
1558 if (!disassemble_fn)
1560 non_fatal (_("Can't disassemble for architecture %s\n"),
1561 bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
1562 exit_status = 1;
1563 return;
1566 opb = bfd_octets_per_byte (abfd);
1568 disasm_info.flavour = bfd_get_flavour (abfd);
1569 disasm_info.arch = bfd_get_arch (abfd);
1570 disasm_info.mach = bfd_get_mach (abfd);
1571 disasm_info.disassembler_options = disassembler_options;
1572 disasm_info.octets_per_byte = opb;
1574 if (bfd_big_endian (abfd))
1575 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_BIG;
1576 else if (bfd_little_endian (abfd))
1577 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_LITTLE;
1578 else
1579 /* ??? Aborting here seems too drastic. We could default to big or little
1580 instead. */
1581 disasm_info.endian = BFD_ENDIAN_UNKNOWN;
1583 for (section = abfd->sections;
1584 section != (asection *) NULL;
1585 section = section->next)
1587 bfd_byte *data = NULL;
1588 bfd_size_type datasize = 0;
1589 arelent **relbuf = NULL;
1590 arelent **relpp = NULL;
1591 arelent **relppend = NULL;
1592 unsigned long stop_offset;
1593 asymbol *sym = NULL;
1594 long place = 0;
1596 if ((section->flags & SEC_LOAD) == 0
1597 || (! disassemble_all
1598 && only == NULL
1599 && (section->flags & SEC_CODE) == 0))
1600 continue;
1601 if (only != (char *) NULL && strcmp (only, section->name) != 0)
1602 continue;
1604 if ((section->flags & SEC_RELOC) != 0
1605 #ifndef DISASSEMBLER_NEEDS_RELOCS
1606 && dump_reloc_info
1607 #endif
1610 long relsize;
1612 relsize = bfd_get_reloc_upper_bound (abfd, section);
1613 if (relsize < 0)
1614 bfd_fatal (bfd_get_filename (abfd));
1616 if (relsize > 0)
1618 long relcount;
1620 relbuf = (arelent **) xmalloc (relsize);
1621 relcount = bfd_canonicalize_reloc (abfd, section, relbuf, syms);
1622 if (relcount < 0)
1623 bfd_fatal (bfd_get_filename (abfd));
1625 /* Sort the relocs by address. */
1626 qsort (relbuf, relcount, sizeof (arelent *), compare_relocs);
1628 relpp = relbuf;
1629 relppend = relpp + relcount;
1631 /* Skip over the relocs belonging to addresses below the
1632 start address. */
1633 if (start_address != (bfd_vma) -1)
1635 while (relpp < relppend
1636 && (*relpp)->address < start_address)
1637 ++relpp;
1642 printf (_("Disassembly of section %s:\n"), section->name);
1644 datasize = bfd_get_section_size_before_reloc (section);
1645 if (datasize == 0)
1646 continue;
1648 data = (bfd_byte *) xmalloc ((size_t) datasize);
1650 bfd_get_section_contents (abfd, section, data, 0, datasize);
1652 aux.sec = section;
1653 disasm_info.buffer = data;
1654 disasm_info.buffer_vma = section->vma;
1655 disasm_info.buffer_length = datasize;
1656 disasm_info.section = section;
1657 if (start_address == (bfd_vma) -1
1658 || start_address < disasm_info.buffer_vma)
1659 addr_offset = 0;
1660 else
1661 addr_offset = start_address - disasm_info.buffer_vma;
1662 if (stop_address == (bfd_vma) -1)
1663 stop_offset = datasize / opb;
1664 else
1666 if (stop_address < disasm_info.buffer_vma)
1667 stop_offset = 0;
1668 else
1669 stop_offset = stop_address - disasm_info.buffer_vma;
1670 if (stop_offset > disasm_info.buffer_length / opb)
1671 stop_offset = disasm_info.buffer_length / opb;
1674 sym = find_symbol_for_address (abfd, section, section->vma + addr_offset,
1675 true, &place);
1677 while (addr_offset < stop_offset)
1679 asymbol *nextsym;
1680 unsigned long nextstop_offset;
1681 boolean insns;
1683 if (sym != NULL && bfd_asymbol_value (sym) <= section->vma + addr_offset)
1685 int x;
1687 for (x = place;
1688 (x < sorted_symcount
1689 && bfd_asymbol_value (sorted_syms[x]) <= section->vma + addr_offset);
1690 ++x)
1691 continue;
1692 disasm_info.symbols = & sorted_syms[place];
1693 disasm_info.num_symbols = x - place;
1695 else
1696 disasm_info.symbols = NULL;
1698 if (! prefix_addresses)
1700 printf ("\n");
1701 objdump_print_addr_with_sym (abfd, section, sym,
1702 section->vma + addr_offset,
1703 &disasm_info,
1704 false);
1705 printf (":\n");
1708 if (sym != NULL && bfd_asymbol_value (sym) > section->vma + addr_offset)
1709 nextsym = sym;
1710 else if (sym == NULL)
1711 nextsym = NULL;
1712 else
1714 /* Search forward for the next appropriate symbol in
1715 SECTION. Note that all the symbols are sorted
1716 together into one big array, and that some sections
1717 may have overlapping addresses. */
1718 while (place < sorted_symcount
1719 && (sorted_syms[place]->section != section
1720 || (bfd_asymbol_value (sorted_syms[place])
1721 <= bfd_asymbol_value (sym))))
1722 ++place;
1723 if (place >= sorted_symcount)
1724 nextsym = NULL;
1725 else
1726 nextsym = sorted_syms[place];
1729 if (sym != NULL && bfd_asymbol_value (sym) > section->vma + addr_offset)
1731 nextstop_offset = bfd_asymbol_value (sym) - section->vma;
1732 if (nextstop_offset > stop_offset)
1733 nextstop_offset = stop_offset;
1735 else if (nextsym == NULL)
1736 nextstop_offset = stop_offset;
1737 else
1739 nextstop_offset = bfd_asymbol_value (nextsym) - section->vma;
1740 if (nextstop_offset > stop_offset)
1741 nextstop_offset = stop_offset;
1744 /* If a symbol is explicitly marked as being an object
1745 rather than a function, just dump the bytes without
1746 disassembling them. */
1747 if (disassemble_all
1748 || sym == NULL
1749 || bfd_asymbol_value (sym) > section->vma + addr_offset
1750 || ((sym->flags & BSF_OBJECT) == 0
1751 && (strstr (bfd_asymbol_name (sym), "gnu_compiled")
1752 == NULL)
1753 && (strstr (bfd_asymbol_name (sym), "gcc2_compiled")
1754 == NULL))
1755 || (sym->flags & BSF_FUNCTION) != 0)
1756 insns = true;
1757 else
1758 insns = false;
1760 disassemble_bytes (&disasm_info, disassemble_fn, insns, data,
1761 addr_offset, nextstop_offset, &relpp, relppend);
1763 addr_offset = nextstop_offset;
1764 sym = nextsym;
1767 free (data);
1768 if (relbuf != NULL)
1769 free (relbuf);
1771 free (sorted_syms);
1775 /* Define a table of stab values and print-strings. We wish the initializer
1776 could be a direct-mapped table, but instead we build one the first
1777 time we need it. */
1779 static void dump_section_stabs PARAMS ((bfd *abfd, char *stabsect_name,
1780 char *strsect_name));
1782 /* Dump the stabs sections from an object file that has a section that
1783 uses Sun stabs encoding. */
1785 static void
1786 dump_stabs (abfd)
1787 bfd *abfd;
1789 dump_section_stabs (abfd, ".stab", ".stabstr");
1790 dump_section_stabs (abfd, ".stab.excl", ".stab.exclstr");
1791 dump_section_stabs (abfd, ".stab.index", ".stab.indexstr");
1792 dump_section_stabs (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
1795 static bfd_byte *stabs;
1796 static bfd_size_type stab_size;
1798 static char *strtab;
1799 static bfd_size_type stabstr_size;
1801 /* Read ABFD's stabs section STABSECT_NAME into `stabs'
1802 and string table section STRSECT_NAME into `strtab'.
1803 If the section exists and was read, allocate the space and return true.
1804 Otherwise return false. */
1806 static boolean
1807 read_section_stabs (abfd, stabsect_name, strsect_name)
1808 bfd *abfd;
1809 const char *stabsect_name;
1810 const char *strsect_name;
1812 asection *stabsect, *stabstrsect;
1814 stabsect = bfd_get_section_by_name (abfd, stabsect_name);
1815 if (0 == stabsect)
1817 printf (_("No %s section present\n\n"), stabsect_name);
1818 return false;
1821 stabstrsect = bfd_get_section_by_name (abfd, strsect_name);
1822 if (0 == stabstrsect)
1824 non_fatal (_("%s has no %s section"),
1825 bfd_get_filename (abfd), strsect_name);
1826 exit_status = 1;
1827 return false;
1830 stab_size = bfd_section_size (abfd, stabsect);
1831 stabstr_size = bfd_section_size (abfd, stabstrsect);
1833 stabs = (bfd_byte *) xmalloc (stab_size);
1834 strtab = (char *) xmalloc (stabstr_size);
1836 if (! bfd_get_section_contents (abfd, stabsect, (PTR) stabs, 0, stab_size))
1838 non_fatal (_("Reading %s section of %s failed: %s"),
1839 stabsect_name, bfd_get_filename (abfd),
1840 bfd_errmsg (bfd_get_error ()));
1841 free (stabs);
1842 free (strtab);
1843 exit_status = 1;
1844 return false;
1847 if (! bfd_get_section_contents (abfd, stabstrsect, (PTR) strtab, 0,
1848 stabstr_size))
1850 non_fatal (_("Reading %s section of %s failed: %s\n"),
1851 strsect_name, bfd_get_filename (abfd),
1852 bfd_errmsg (bfd_get_error ()));
1853 free (stabs);
1854 free (strtab);
1855 exit_status = 1;
1856 return false;
1859 return true;
1862 /* Stabs entries use a 12 byte format:
1863 4 byte string table index
1864 1 byte stab type
1865 1 byte stab other field
1866 2 byte stab desc field
1867 4 byte stab value
1868 FIXME: This will have to change for a 64 bit object format. */
1870 #define STRDXOFF (0)
1871 #define TYPEOFF (4)
1872 #define OTHEROFF (5)
1873 #define DESCOFF (6)
1874 #define VALOFF (8)
1875 #define STABSIZE (12)
1877 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
1878 using string table section STRSECT_NAME (in `strtab'). */
1880 static void
1881 print_section_stabs (abfd, stabsect_name, strsect_name)
1882 bfd *abfd;
1883 const char *stabsect_name;
1884 const char *strsect_name ATTRIBUTE_UNUSED;
1886 int i;
1887 unsigned file_string_table_offset = 0, next_file_string_table_offset = 0;
1888 bfd_byte *stabp, *stabs_end;
1890 stabp = stabs;
1891 stabs_end = stabp + stab_size;
1893 printf (_("Contents of %s section:\n\n"), stabsect_name);
1894 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
1896 /* Loop through all symbols and print them.
1898 We start the index at -1 because there is a dummy symbol on
1899 the front of stabs-in-{coff,elf} sections that supplies sizes. */
1901 for (i = -1; stabp < stabs_end; stabp += STABSIZE, i++)
1903 const char *name;
1904 unsigned long strx;
1905 unsigned char type, other;
1906 unsigned short desc;
1907 bfd_vma value;
1909 strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
1910 type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
1911 other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
1912 desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
1913 value = bfd_h_get_32 (abfd, stabp + VALOFF);
1915 printf ("\n%-6d ", i);
1916 /* Either print the stab name, or, if unnamed, print its number
1917 again (makes consistent formatting for tools like awk). */
1918 name = bfd_get_stab_name (type);
1919 if (name != NULL)
1920 printf ("%-6s", name);
1921 else if (type == N_UNDF)
1922 printf ("HdrSym");
1923 else
1924 printf ("%-6d", type);
1925 printf (" %-6d %-6d ", other, desc);
1926 bfd_printf_vma (abfd, value);
1927 printf (" %-6lu", strx);
1929 /* Symbols with type == 0 (N_UNDF) specify the length of the
1930 string table associated with this file. We use that info
1931 to know how to relocate the *next* file's string table indices. */
1933 if (type == N_UNDF)
1935 file_string_table_offset = next_file_string_table_offset;
1936 next_file_string_table_offset += value;
1938 else
1940 /* Using the (possibly updated) string table offset, print the
1941 string (if any) associated with this symbol. */
1943 if ((strx + file_string_table_offset) < stabstr_size)
1944 printf (" %s", &strtab[strx + file_string_table_offset]);
1945 else
1946 printf (" *");
1949 printf ("\n\n");
1952 static void
1953 dump_section_stabs (abfd, stabsect_name, strsect_name)
1954 bfd *abfd;
1955 char *stabsect_name;
1956 char *strsect_name;
1958 asection *s;
1960 /* Check for section names for which stabsect_name is a prefix, to
1961 handle .stab0, etc. */
1962 for (s = abfd->sections;
1963 s != NULL;
1964 s = s->next)
1966 int len;
1968 len = strlen (stabsect_name);
1970 /* If the prefix matches, and the files section name ends with a
1971 nul or a digit, then we match. I.e., we want either an exact
1972 match or a section followed by a number. */
1973 if (strncmp (stabsect_name, s->name, len) == 0
1974 && (s->name[len] == '\000'
1975 || ISDIGIT (s->name[len])))
1977 if (read_section_stabs (abfd, s->name, strsect_name))
1979 print_section_stabs (abfd, s->name, strsect_name);
1980 free (stabs);
1981 free (strtab);
1987 static void
1988 dump_bfd_header (abfd)
1989 bfd *abfd;
1991 char *comma = "";
1993 printf (_("architecture: %s, "),
1994 bfd_printable_arch_mach (bfd_get_arch (abfd),
1995 bfd_get_mach (abfd)));
1996 printf (_("flags 0x%08x:\n"), abfd->flags);
1998 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
1999 PF (HAS_RELOC, "HAS_RELOC");
2000 PF (EXEC_P, "EXEC_P");
2001 PF (HAS_LINENO, "HAS_LINENO");
2002 PF (HAS_DEBUG, "HAS_DEBUG");
2003 PF (HAS_SYMS, "HAS_SYMS");
2004 PF (HAS_LOCALS, "HAS_LOCALS");
2005 PF (DYNAMIC, "DYNAMIC");
2006 PF (WP_TEXT, "WP_TEXT");
2007 PF (D_PAGED, "D_PAGED");
2008 PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
2009 PF (HAS_LOAD_PAGE, "HAS_LOAD_PAGE");
2010 printf (_("\nstart address 0x"));
2011 bfd_printf_vma (abfd, abfd->start_address);
2012 printf ("\n");
2015 static void
2016 dump_bfd_private_header (abfd)
2017 bfd *abfd;
2019 bfd_print_private_bfd_data (abfd, stdout);
2022 /* Dump selected contents of ABFD */
2024 static void
2025 dump_bfd (abfd)
2026 bfd *abfd;
2028 /* If we are adjusting section VMA's, change them all now. Changing
2029 the BFD information is a hack. However, we must do it, or
2030 bfd_find_nearest_line will not do the right thing. */
2031 if (adjust_section_vma != 0)
2033 asection *s;
2035 for (s = abfd->sections; s != NULL; s = s->next)
2037 s->vma += adjust_section_vma;
2038 s->lma += adjust_section_vma;
2042 printf (_("\n%s: file format %s\n"), bfd_get_filename (abfd),
2043 abfd->xvec->name);
2044 if (dump_ar_hdrs)
2045 print_arelt_descr (stdout, abfd, true);
2046 if (dump_file_header)
2047 dump_bfd_header (abfd);
2048 if (dump_private_headers)
2049 dump_bfd_private_header (abfd);
2050 putchar ('\n');
2051 if (dump_section_headers)
2052 dump_headers (abfd);
2054 if (dump_symtab || dump_reloc_info || disassemble || dump_debugging)
2055 syms = slurp_symtab (abfd);
2056 if (dump_dynamic_symtab || dump_dynamic_reloc_info)
2057 dynsyms = slurp_dynamic_symtab (abfd);
2059 if (dump_symtab)
2060 dump_symbols (abfd, false);
2061 if (dump_dynamic_symtab)
2062 dump_symbols (abfd, true);
2063 if (dump_stab_section_info)
2064 dump_stabs (abfd);
2065 if (dump_reloc_info && ! disassemble)
2066 dump_relocs (abfd);
2067 if (dump_dynamic_reloc_info)
2068 dump_dynamic_relocs (abfd);
2069 if (dump_section_contents)
2070 dump_data (abfd);
2071 if (disassemble)
2072 disassemble_data (abfd);
2073 if (dump_debugging)
2075 PTR dhandle;
2077 dhandle = read_debugging_info (abfd, syms, symcount);
2078 if (dhandle != NULL)
2080 if (! print_debugging_info (stdout, dhandle))
2082 non_fatal (_("%s: printing debugging information failed"),
2083 bfd_get_filename (abfd));
2084 exit_status = 1;
2089 if (syms)
2091 free (syms);
2092 syms = NULL;
2095 if (dynsyms)
2097 free (dynsyms);
2098 dynsyms = NULL;
2102 static void
2103 display_bfd (abfd)
2104 bfd *abfd;
2106 char **matching;
2108 if (bfd_check_format_matches (abfd, bfd_object, &matching))
2110 dump_bfd (abfd);
2111 return;
2114 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
2116 nonfatal (bfd_get_filename (abfd));
2117 list_matching_formats (matching);
2118 free (matching);
2119 return;
2122 if (bfd_get_error () != bfd_error_file_not_recognized)
2124 nonfatal (bfd_get_filename (abfd));
2125 return;
2128 if (bfd_check_format_matches (abfd, bfd_core, &matching))
2130 dump_bfd (abfd);
2131 return;
2134 nonfatal (bfd_get_filename (abfd));
2136 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
2138 list_matching_formats (matching);
2139 free (matching);
2143 static void
2144 display_file (filename, target)
2145 char *filename;
2146 char *target;
2148 bfd *file, *arfile = (bfd *) NULL;
2150 file = bfd_openr (filename, target);
2151 if (file == NULL)
2153 nonfatal (filename);
2154 return;
2157 if (bfd_check_format (file, bfd_archive) == true)
2159 bfd *last_arfile = NULL;
2161 printf (_("In archive %s:\n"), bfd_get_filename (file));
2162 for (;;)
2164 bfd_set_error (bfd_error_no_error);
2166 arfile = bfd_openr_next_archived_file (file, arfile);
2167 if (arfile == NULL)
2169 if (bfd_get_error () != bfd_error_no_more_archived_files)
2170 nonfatal (bfd_get_filename (file));
2171 break;
2174 display_bfd (arfile);
2176 if (last_arfile != NULL)
2177 bfd_close (last_arfile);
2178 last_arfile = arfile;
2181 if (last_arfile != NULL)
2182 bfd_close (last_arfile);
2184 else
2185 display_bfd (file);
2187 bfd_close (file);
2190 /* Actually display the various requested regions */
2192 static void
2193 dump_data (abfd)
2194 bfd *abfd;
2196 asection *section;
2197 bfd_byte *data = 0;
2198 bfd_size_type datasize = 0;
2199 bfd_size_type addr_offset;
2200 bfd_size_type start_offset, stop_offset;
2201 unsigned int opb = bfd_octets_per_byte (abfd);
2203 for (section = abfd->sections; section != NULL; section =
2204 section->next)
2206 int onaline = 16;
2208 if (only == (char *) NULL ||
2209 strcmp (only, section->name) == 0)
2211 if (section->flags & SEC_HAS_CONTENTS)
2213 printf (_("Contents of section %s:\n"), section->name);
2215 if (bfd_section_size (abfd, section) == 0)
2216 continue;
2217 data = (bfd_byte *) xmalloc ((size_t) bfd_section_size (abfd, section));
2218 datasize = bfd_section_size (abfd, section);
2221 bfd_get_section_contents (abfd, section, (PTR) data, 0, bfd_section_size (abfd, section));
2223 if (start_address == (bfd_vma) -1
2224 || start_address < section->vma)
2225 start_offset = 0;
2226 else
2227 start_offset = start_address - section->vma;
2228 if (stop_address == (bfd_vma) -1)
2229 stop_offset = bfd_section_size (abfd, section) / opb;
2230 else
2232 if (stop_address < section->vma)
2233 stop_offset = 0;
2234 else
2235 stop_offset = stop_address - section->vma;
2236 if (stop_offset > bfd_section_size (abfd, section) / opb)
2237 stop_offset = bfd_section_size (abfd, section) / opb;
2239 for (addr_offset = start_offset;
2240 addr_offset < stop_offset; addr_offset += onaline / opb)
2242 bfd_size_type j;
2244 printf (" %04lx ", (unsigned long int)
2245 (addr_offset + section->vma));
2246 for (j = addr_offset * opb;
2247 j < addr_offset * opb + onaline; j++)
2249 if (j < stop_offset * opb)
2250 printf ("%02x", (unsigned) (data[j]));
2251 else
2252 printf (" ");
2253 if ((j & 3) == 3)
2254 printf (" ");
2257 printf (" ");
2258 for (j = addr_offset * opb;
2259 j < addr_offset * opb + onaline; j++)
2261 if (j >= stop_offset * opb)
2262 printf (" ");
2263 else
2264 printf ("%c", ISPRINT (data[j]) ? data[j] : '.');
2266 putchar ('\n');
2268 free (data);
2274 /* Should perhaps share code and display with nm? */
2275 static void
2276 dump_symbols (abfd, dynamic)
2277 bfd *abfd ATTRIBUTE_UNUSED;
2278 boolean dynamic;
2280 asymbol **current;
2281 long max;
2282 long count;
2284 if (dynamic)
2286 current = dynsyms;
2287 max = dynsymcount;
2288 printf ("DYNAMIC SYMBOL TABLE:\n");
2290 else
2292 current = syms;
2293 max = symcount;
2294 printf ("SYMBOL TABLE:\n");
2297 if (max == 0)
2298 printf (_("no symbols\n"));
2300 for (count = 0; count < max; count++)
2302 if (*current)
2304 bfd *cur_bfd = bfd_asymbol_bfd (*current);
2306 if (cur_bfd != NULL)
2308 const char *name;
2309 char *alloc;
2311 name = (*current)->name;
2312 alloc = NULL;
2313 if (do_demangle && name != NULL && *name != '\0')
2315 /* If we want to demangle the name, we demangle it
2316 here, and temporarily clobber it while calling
2317 bfd_print_symbol. FIXME: This is a gross hack. */
2319 alloc = demangle (cur_bfd, name);
2320 (*current)->name = alloc;
2323 bfd_print_symbol (cur_bfd, stdout, *current,
2324 bfd_print_symbol_all);
2326 (*current)->name = name;
2327 if (alloc != NULL)
2328 free (alloc);
2330 printf ("\n");
2333 current++;
2335 printf ("\n");
2336 printf ("\n");
2339 static void
2340 dump_relocs (abfd)
2341 bfd *abfd;
2343 arelent **relpp;
2344 long relcount;
2345 asection *a;
2347 for (a = abfd->sections; a != (asection *) NULL; a = a->next)
2349 long relsize;
2351 if (bfd_is_abs_section (a))
2352 continue;
2353 if (bfd_is_und_section (a))
2354 continue;
2355 if (bfd_is_com_section (a))
2356 continue;
2358 if (only)
2360 if (strcmp (only, a->name))
2361 continue;
2363 else if ((a->flags & SEC_RELOC) == 0)
2364 continue;
2366 relsize = bfd_get_reloc_upper_bound (abfd, a);
2367 if (relsize < 0)
2368 bfd_fatal (bfd_get_filename (abfd));
2370 printf ("RELOCATION RECORDS FOR [%s]:", a->name);
2372 if (relsize == 0)
2374 printf (" (none)\n\n");
2376 else
2378 relpp = (arelent **) xmalloc (relsize);
2379 relcount = bfd_canonicalize_reloc (abfd, a, relpp, syms);
2380 if (relcount < 0)
2381 bfd_fatal (bfd_get_filename (abfd));
2382 else if (relcount == 0)
2384 printf (" (none)\n\n");
2386 else
2388 printf ("\n");
2389 dump_reloc_set (abfd, a, relpp, relcount);
2390 printf ("\n\n");
2392 free (relpp);
2397 static void
2398 dump_dynamic_relocs (abfd)
2399 bfd *abfd;
2401 long relsize;
2402 arelent **relpp;
2403 long relcount;
2405 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
2406 if (relsize < 0)
2407 bfd_fatal (bfd_get_filename (abfd));
2409 printf ("DYNAMIC RELOCATION RECORDS");
2411 if (relsize == 0)
2413 printf (" (none)\n\n");
2415 else
2417 relpp = (arelent **) xmalloc (relsize);
2418 relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
2419 if (relcount < 0)
2420 bfd_fatal (bfd_get_filename (abfd));
2421 else if (relcount == 0)
2423 printf (" (none)\n\n");
2425 else
2427 printf ("\n");
2428 dump_reloc_set (abfd, (asection *) NULL, relpp, relcount);
2429 printf ("\n\n");
2431 free (relpp);
2435 static void
2436 dump_reloc_set (abfd, sec, relpp, relcount)
2437 bfd *abfd;
2438 asection *sec;
2439 arelent **relpp;
2440 long relcount;
2442 arelent **p;
2443 char *last_filename, *last_functionname;
2444 unsigned int last_line;
2446 /* Get column headers lined up reasonably. */
2448 static int width;
2449 if (width == 0)
2451 char buf[30];
2452 bfd_sprintf_vma (abfd, buf, (bfd_vma) -1);
2453 width = strlen (buf) - 7;
2455 printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
2458 last_filename = NULL;
2459 last_functionname = NULL;
2460 last_line = 0;
2462 for (p = relpp; relcount && *p != (arelent *) NULL; p++, relcount--)
2464 arelent *q = *p;
2465 const char *filename, *functionname;
2466 unsigned int line;
2467 const char *sym_name;
2468 const char *section_name;
2470 if (start_address != (bfd_vma) -1
2471 && q->address < start_address)
2472 continue;
2473 if (stop_address != (bfd_vma) -1
2474 && q->address > stop_address)
2475 continue;
2477 if (with_line_numbers
2478 && sec != NULL
2479 && bfd_find_nearest_line (abfd, sec, syms, q->address,
2480 &filename, &functionname, &line))
2482 if (functionname != NULL
2483 && (last_functionname == NULL
2484 || strcmp (functionname, last_functionname) != 0))
2486 printf ("%s():\n", functionname);
2487 if (last_functionname != NULL)
2488 free (last_functionname);
2489 last_functionname = xstrdup (functionname);
2491 if (line > 0
2492 && (line != last_line
2493 || (filename != NULL
2494 && last_filename != NULL
2495 && strcmp (filename, last_filename) != 0)))
2497 printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
2498 last_line = line;
2499 if (last_filename != NULL)
2500 free (last_filename);
2501 if (filename == NULL)
2502 last_filename = NULL;
2503 else
2504 last_filename = xstrdup (filename);
2508 if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
2510 sym_name = (*(q->sym_ptr_ptr))->name;
2511 section_name = (*(q->sym_ptr_ptr))->section->name;
2513 else
2515 sym_name = NULL;
2516 section_name = NULL;
2518 if (sym_name)
2520 bfd_printf_vma (abfd, q->address);
2521 if (q->howto->name)
2522 printf (" %-16s ", q->howto->name);
2523 else
2524 printf (" %-16d ", q->howto->type);
2525 objdump_print_symname (abfd, (struct disassemble_info *) NULL,
2526 *q->sym_ptr_ptr);
2528 else
2530 if (section_name == (const char *) NULL)
2531 section_name = "*unknown*";
2532 bfd_printf_vma (abfd, q->address);
2533 printf (" %-16s [%s]",
2534 q->howto->name,
2535 section_name);
2537 if (q->addend)
2539 printf ("+0x");
2540 bfd_printf_vma (abfd, q->addend);
2542 printf ("\n");
2546 /* The length of the longest architecture name + 1. */
2547 #define LONGEST_ARCH sizeof("powerpc:common")
2549 static const char *
2550 endian_string (endian)
2551 enum bfd_endian endian;
2553 if (endian == BFD_ENDIAN_BIG)
2554 return "big endian";
2555 else if (endian == BFD_ENDIAN_LITTLE)
2556 return "little endian";
2557 else
2558 return "endianness unknown";
2561 /* List the targets that BFD is configured to support, each followed
2562 by its endianness and the architectures it supports. */
2564 static void
2565 display_target_list ()
2567 extern const bfd_target *const *bfd_target_vector;
2568 char *dummy_name;
2569 int t;
2571 dummy_name = make_temp_file (NULL);
2572 for (t = 0; bfd_target_vector[t]; t++)
2574 const bfd_target *p = bfd_target_vector[t];
2575 bfd *abfd = bfd_openw (dummy_name, p->name);
2576 int a;
2578 printf ("%s\n (header %s, data %s)\n", p->name,
2579 endian_string (p->header_byteorder),
2580 endian_string (p->byteorder));
2582 if (abfd == NULL)
2584 nonfatal (dummy_name);
2585 continue;
2588 if (! bfd_set_format (abfd, bfd_object))
2590 if (bfd_get_error () != bfd_error_invalid_operation)
2591 nonfatal (p->name);
2592 bfd_close_all_done (abfd);
2593 continue;
2596 for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
2597 if (bfd_set_arch_mach (abfd, (enum bfd_architecture) a, 0))
2598 printf (" %s\n",
2599 bfd_printable_arch_mach ((enum bfd_architecture) a, 0));
2600 bfd_close_all_done (abfd);
2602 unlink (dummy_name);
2603 free (dummy_name);
2606 /* Print a table showing which architectures are supported for entries
2607 FIRST through LAST-1 of bfd_target_vector (targets across,
2608 architectures down). */
2610 static void
2611 display_info_table (first, last)
2612 int first;
2613 int last;
2615 extern const bfd_target *const *bfd_target_vector;
2616 int t, a;
2617 char *dummy_name;
2619 /* Print heading of target names. */
2620 printf ("\n%*s", (int) LONGEST_ARCH, " ");
2621 for (t = first; t < last && bfd_target_vector[t]; t++)
2622 printf ("%s ", bfd_target_vector[t]->name);
2623 putchar ('\n');
2625 dummy_name = make_temp_file (NULL);
2626 for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
2627 if (strcmp (bfd_printable_arch_mach (a, 0), "UNKNOWN!") != 0)
2629 printf ("%*s ", (int) LONGEST_ARCH - 1,
2630 bfd_printable_arch_mach (a, 0));
2631 for (t = first; t < last && bfd_target_vector[t]; t++)
2633 const bfd_target *p = bfd_target_vector[t];
2634 boolean ok = true;
2635 bfd *abfd = bfd_openw (dummy_name, p->name);
2637 if (abfd == NULL)
2639 nonfatal (p->name);
2640 ok = false;
2643 if (ok)
2645 if (! bfd_set_format (abfd, bfd_object))
2647 if (bfd_get_error () != bfd_error_invalid_operation)
2648 nonfatal (p->name);
2649 ok = false;
2653 if (ok)
2655 if (! bfd_set_arch_mach (abfd, a, 0))
2656 ok = false;
2659 if (ok)
2660 printf ("%s ", p->name);
2661 else
2663 int l = strlen (p->name);
2664 while (l--)
2665 putchar ('-');
2666 putchar (' ');
2668 if (abfd != NULL)
2669 bfd_close_all_done (abfd);
2671 putchar ('\n');
2673 unlink (dummy_name);
2674 free (dummy_name);
2677 /* Print tables of all the target-architecture combinations that
2678 BFD has been configured to support. */
2680 static void
2681 display_target_tables ()
2683 int t, columns;
2684 extern const bfd_target *const *bfd_target_vector;
2685 char *colum;
2687 columns = 0;
2688 colum = getenv ("COLUMNS");
2689 if (colum != NULL)
2690 columns = atoi (colum);
2691 if (columns == 0)
2692 columns = 80;
2694 t = 0;
2695 while (bfd_target_vector[t] != NULL)
2697 int oldt = t, wid;
2699 wid = LONGEST_ARCH + strlen (bfd_target_vector[t]->name) + 1;
2700 ++t;
2701 while (wid < columns && bfd_target_vector[t] != NULL)
2703 int newwid;
2705 newwid = wid + strlen (bfd_target_vector[t]->name) + 1;
2706 if (newwid >= columns)
2707 break;
2708 wid = newwid;
2709 ++t;
2711 display_info_table (oldt, t);
2715 static void
2716 display_info ()
2718 printf (_("BFD header file version %s\n"), BFD_VERSION_STRING);
2719 display_target_list ();
2720 display_target_tables ();
2723 int main PARAMS ((int, char **));
2726 main (argc, argv)
2727 int argc;
2728 char **argv;
2730 int c;
2731 char *target = default_target;
2732 boolean seenflag = false;
2734 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2735 setlocale (LC_MESSAGES, "");
2736 #endif
2737 #if defined (HAVE_SETLOCALE)
2738 setlocale (LC_CTYPE, "");
2739 #endif
2740 bindtextdomain (PACKAGE, LOCALEDIR);
2741 textdomain (PACKAGE);
2743 program_name = *argv;
2744 xmalloc_set_program_name (program_name);
2746 START_PROGRESS (program_name, 0);
2748 bfd_init ();
2749 set_default_bfd_target ();
2751 while ((c = getopt_long (argc, argv, "pib:m:M:VvCdDlfaHhrRtTxsSj:wE:zgG",
2752 long_options, (int *) 0))
2753 != EOF)
2755 switch (c)
2757 case 0:
2758 break; /* We've been given a long option. */
2759 case 'm':
2760 machine = optarg;
2761 break;
2762 case 'M':
2763 disassembler_options = optarg;
2764 break;
2765 case 'j':
2766 only = optarg;
2767 break;
2768 case 'l':
2769 with_line_numbers = true;
2770 break;
2771 case 'b':
2772 target = optarg;
2773 break;
2774 case 'C':
2775 do_demangle = true;
2776 if (optarg != NULL)
2778 enum demangling_styles style;
2780 style = cplus_demangle_name_to_style (optarg);
2781 if (style == unknown_demangling)
2782 fatal (_("unknown demangling style `%s'"),
2783 optarg);
2785 cplus_demangle_set_style (style);
2787 break;
2788 case 'w':
2789 wide_output = true;
2790 break;
2791 case OPTION_ADJUST_VMA:
2792 adjust_section_vma = parse_vma (optarg, "--adjust-vma");
2793 break;
2794 case OPTION_START_ADDRESS:
2795 start_address = parse_vma (optarg, "--start-address");
2796 break;
2797 case OPTION_STOP_ADDRESS:
2798 stop_address = parse_vma (optarg, "--stop-address");
2799 break;
2800 case 'E':
2801 if (strcmp (optarg, "B") == 0)
2802 endian = BFD_ENDIAN_BIG;
2803 else if (strcmp (optarg, "L") == 0)
2804 endian = BFD_ENDIAN_LITTLE;
2805 else
2807 non_fatal (_("unrecognized -E option"));
2808 usage (stderr, 1);
2810 break;
2811 case OPTION_ENDIAN:
2812 if (strncmp (optarg, "big", strlen (optarg)) == 0)
2813 endian = BFD_ENDIAN_BIG;
2814 else if (strncmp (optarg, "little", strlen (optarg)) == 0)
2815 endian = BFD_ENDIAN_LITTLE;
2816 else
2818 non_fatal (_("unrecognized --endian type `%s'"), optarg);
2819 usage (stderr, 1);
2821 break;
2823 case 'f':
2824 dump_file_header = true;
2825 seenflag = true;
2826 break;
2827 case 'i':
2828 formats_info = true;
2829 seenflag = true;
2830 break;
2831 case 'p':
2832 dump_private_headers = true;
2833 seenflag = true;
2834 break;
2835 case 'x':
2836 dump_private_headers = true;
2837 dump_symtab = true;
2838 dump_reloc_info = true;
2839 dump_file_header = true;
2840 dump_ar_hdrs = true;
2841 dump_section_headers = true;
2842 seenflag = true;
2843 break;
2844 case 't':
2845 dump_symtab = true;
2846 seenflag = true;
2847 break;
2848 case 'T':
2849 dump_dynamic_symtab = true;
2850 seenflag = true;
2851 break;
2852 case 'd':
2853 disassemble = true;
2854 seenflag = true;
2855 break;
2856 case 'z':
2857 disassemble_zeroes = true;
2858 break;
2859 case 'D':
2860 disassemble = true;
2861 disassemble_all = true;
2862 seenflag = true;
2863 break;
2864 case 'S':
2865 disassemble = true;
2866 with_source_code = true;
2867 seenflag = true;
2868 break;
2869 case 'g':
2870 dump_debugging = 1;
2871 seenflag = true;
2872 break;
2873 case 'G':
2874 dump_stab_section_info = true;
2875 seenflag = true;
2876 break;
2877 case 's':
2878 dump_section_contents = true;
2879 seenflag = true;
2880 break;
2881 case 'r':
2882 dump_reloc_info = true;
2883 seenflag = true;
2884 break;
2885 case 'R':
2886 dump_dynamic_reloc_info = true;
2887 seenflag = true;
2888 break;
2889 case 'a':
2890 dump_ar_hdrs = true;
2891 seenflag = true;
2892 break;
2893 case 'h':
2894 dump_section_headers = true;
2895 seenflag = true;
2896 break;
2897 case 'H':
2898 usage (stdout, 0);
2899 seenflag = true;
2900 case 'v':
2901 case 'V':
2902 show_version = true;
2903 seenflag = true;
2904 break;
2906 default:
2907 usage (stderr, 1);
2911 if (show_version)
2912 print_version ("objdump");
2914 if (seenflag == false)
2915 usage (stderr, 2);
2917 if (formats_info)
2918 display_info ();
2919 else
2921 if (optind == argc)
2922 display_file ("a.out", target);
2923 else
2924 for (; optind < argc;)
2925 display_file (argv[optind++], target);
2928 END_PROGRESS (program_name);
2930 return exit_status;