Convert to C90
[binutils.git] / binutils / objdump.c
blobdd8a53624108b2957c86d55d37183e7b760d79fe
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.
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
42 PARAMS ((FILE *, const char *, ...));
43 #endif
45 /* Exit status. */
46 static int exit_status = 0;
48 static char *default_target = NULL; /* Default at runtime. */
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 bfd_vma adjust_section_vma = 0; /* --adjust-vma */
78 static int file_start_context = 0; /* --file-start-context */
80 /* Extra info to pass to the disassembler address printing function. */
81 struct objdump_disasm_info
83 bfd *abfd;
84 asection *sec;
85 bfd_boolean require_sec;
88 /* Architecture to disassemble for, or default if NULL. */
89 static char *machine = (char *) NULL;
91 /* Target specific options to the disassembler. */
92 static char *disassembler_options = (char *) NULL;
94 /* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */
95 static enum bfd_endian endian = BFD_ENDIAN_UNKNOWN;
97 /* The symbol table. */
98 static asymbol **syms;
100 /* Number of symbols in `syms'. */
101 static long symcount = 0;
103 /* The sorted symbol table. */
104 static asymbol **sorted_syms;
106 /* Number of symbols in `sorted_syms'. */
107 static long sorted_symcount = 0;
109 /* The dynamic symbol table. */
110 static asymbol **dynsyms;
112 /* Number of symbols in `dynsyms'. */
113 static long dynsymcount = 0;
115 static bfd_byte *stabs;
116 static bfd_size_type stab_size;
118 static char *strtab;
119 static bfd_size_type stabstr_size;
121 /* Static declarations. */
123 static void usage
124 PARAMS ((FILE *, int));
125 static void nonfatal
126 PARAMS ((const char *));
127 static void display_file
128 PARAMS ((char *, char *));
129 static void dump_section_header
130 PARAMS ((bfd *, asection *, PTR));
131 static void dump_headers
132 PARAMS ((bfd *));
133 static void dump_data
134 PARAMS ((bfd *));
135 static void dump_relocs
136 PARAMS ((bfd *));
137 static void dump_dynamic_relocs
138 PARAMS ((bfd *));
139 static void dump_reloc_set
140 PARAMS ((bfd *, asection *, arelent **, long));
141 static void dump_symbols
142 PARAMS ((bfd *, bfd_boolean));
143 static void dump_bfd_header
144 PARAMS ((bfd *));
145 static void dump_bfd_private_header
146 PARAMS ((bfd *));
147 static void dump_bfd
148 PARAMS ((bfd *));
149 static void display_bfd
150 PARAMS ((bfd *));
151 static void objdump_print_value
152 PARAMS ((bfd_vma, struct disassemble_info *, bfd_boolean));
153 static void objdump_print_symname
154 PARAMS ((bfd *, struct disassemble_info *, asymbol *));
155 static asymbol *find_symbol_for_address
156 PARAMS ((bfd *, asection *, bfd_vma, bfd_boolean, long *));
157 static void objdump_print_addr_with_sym
158 PARAMS ((bfd *, asection *, asymbol *, bfd_vma,
159 struct disassemble_info *, bfd_boolean));
160 static void objdump_print_addr
161 PARAMS ((bfd_vma, struct disassemble_info *, bfd_boolean));
162 static void objdump_print_address
163 PARAMS ((bfd_vma, struct disassemble_info *));
164 static int objdump_symbol_at_address
165 PARAMS ((bfd_vma, struct disassemble_info *));
166 static void show_line
167 PARAMS ((bfd *, asection *, bfd_vma));
168 static void disassemble_bytes
169 PARAMS ((struct disassemble_info *, disassembler_ftype, bfd_boolean,
170 bfd_byte *, bfd_vma, bfd_vma, arelent ***, arelent **));
171 static void disassemble_data
172 PARAMS ((bfd *));
173 static asymbol ** slurp_symtab
174 PARAMS ((bfd *));
175 static asymbol ** slurp_dynamic_symtab
176 PARAMS ((bfd *));
177 static long remove_useless_symbols
178 PARAMS ((asymbol **, long));
179 static int compare_symbols
180 PARAMS ((const PTR, const PTR));
181 static int compare_relocs
182 PARAMS ((const PTR, const PTR));
183 static void dump_stabs
184 PARAMS ((bfd *));
185 static bfd_boolean read_section_stabs
186 PARAMS ((bfd *, const char *, const char *));
187 static void print_section_stabs
188 PARAMS ((bfd *, const char *, const char *));
189 static void dump_section_stabs
190 PARAMS ((bfd *, char *, char *));
192 static void
193 usage (stream, status)
194 FILE *stream;
195 int status;
197 fprintf (stream, _("Usage: %s <option(s)> <file(s)>\n"), program_name);
198 fprintf (stream, _(" Display information from object <file(s)>.\n"));
199 fprintf (stream, _(" At least one of the following switches must be given:\n"));
200 fprintf (stream, _("\
201 -a, --archive-headers Display archive header information\n\
202 -f, --file-headers Display the contents of the overall file header\n\
203 -p, --private-headers Display object format specific file header contents\n\
204 -h, --[section-]headers Display the contents of the section headers\n\
205 -x, --all-headers Display the contents of all headers\n\
206 -d, --disassemble Display assembler contents of executable sections\n\
207 -D, --disassemble-all Display assembler contents of all sections\n\
208 -S, --source Intermix source code with disassembly\n\
209 -s, --full-contents Display the full contents of all sections requested\n\
210 -g, --debugging Display debug information in object file\n\
211 -G, --stabs Display (in raw form) any STABS info in the file\n\
212 -t, --syms Display the contents of the symbol table(s)\n\
213 -T, --dynamic-syms Display the contents of the dynamic symbol table\n\
214 -r, --reloc Display the relocation entries in the file\n\
215 -R, --dynamic-reloc Display the dynamic relocation entries in the file\n\
216 -v, --version Display this program's version number\n\
217 -i, --info List object formats and architectures supported\n\
218 -H, --help Display this information\n\
219 "));
220 if (status != 2)
222 fprintf (stream, _("\n The following switches are optional:\n"));
223 fprintf (stream, _("\
224 -b, --target=BFDNAME Specify the target object format as BFDNAME\n\
225 -m, --architecture=MACHINE Specify the target architecture as MACHINE\n\
226 -j, --section=NAME Only display information for section NAME\n\
227 -M, --disassembler-options=OPT Pass text OPT on to the disassembler\n\
228 -EB --endian=big Assume big endian format when disassembling\n\
229 -EL --endian=little Assume little endian format when disassembling\n\
230 --file-start-context Include context from start of file (with -S)\n\
231 -l, --line-numbers Include line numbers and filenames in output\n\
232 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n\
233 The STYLE, if specified, can be `auto', `gnu',\n\
234 `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
235 or `gnat'\n\
236 -w, --wide Format output for more than 80 columns\n\
237 -z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling\n\
238 --start-address=ADDR Only process data whose address is >= ADDR\n\
239 --stop-address=ADDR Only process data whose address is <= ADDR\n\
240 --prefix-addresses Print complete address alongside disassembly\n\
241 --[no-]show-raw-insn Display hex alongside symbolic disassembly\n\
242 --adjust-vma=OFFSET Add OFFSET to all displayed section addresses\n\
243 \n"));
244 list_supported_targets (program_name, stream);
245 list_supported_architectures (program_name, stream);
247 disassembler_usage (stream);
249 if (status == 0)
250 fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
251 exit (status);
254 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
256 #define OPTION_ENDIAN (150)
257 #define OPTION_START_ADDRESS (OPTION_ENDIAN + 1)
258 #define OPTION_STOP_ADDRESS (OPTION_START_ADDRESS + 1)
259 #define OPTION_ADJUST_VMA (OPTION_STOP_ADDRESS + 1)
261 static struct option long_options[]=
263 {"adjust-vma", required_argument, NULL, OPTION_ADJUST_VMA},
264 {"all-headers", no_argument, NULL, 'x'},
265 {"private-headers", no_argument, NULL, 'p'},
266 {"architecture", required_argument, NULL, 'm'},
267 {"archive-headers", no_argument, NULL, 'a'},
268 {"debugging", no_argument, NULL, 'g'},
269 {"demangle", optional_argument, NULL, 'C'},
270 {"disassemble", no_argument, NULL, 'd'},
271 {"disassemble-all", no_argument, NULL, 'D'},
272 {"disassembler-options", required_argument, NULL, 'M'},
273 {"disassemble-zeroes", no_argument, NULL, 'z'},
274 {"dynamic-reloc", no_argument, NULL, 'R'},
275 {"dynamic-syms", no_argument, NULL, 'T'},
276 {"endian", required_argument, NULL, OPTION_ENDIAN},
277 {"file-headers", no_argument, NULL, 'f'},
278 {"file-start-context", no_argument, &file_start_context, 1},
279 {"full-contents", no_argument, NULL, 's'},
280 {"headers", no_argument, NULL, 'h'},
281 {"help", no_argument, NULL, 'H'},
282 {"info", no_argument, NULL, 'i'},
283 {"line-numbers", no_argument, NULL, 'l'},
284 {"no-show-raw-insn", no_argument, &show_raw_insn, -1},
285 {"prefix-addresses", no_argument, &prefix_addresses, 1},
286 {"reloc", no_argument, NULL, 'r'},
287 {"section", required_argument, NULL, 'j'},
288 {"section-headers", no_argument, NULL, 'h'},
289 {"show-raw-insn", no_argument, &show_raw_insn, 1},
290 {"source", no_argument, NULL, 'S'},
291 {"stabs", no_argument, NULL, 'G'},
292 {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
293 {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
294 {"syms", no_argument, NULL, 't'},
295 {"target", required_argument, NULL, 'b'},
296 {"version", no_argument, NULL, 'V'},
297 {"wide", no_argument, NULL, 'w'},
298 {0, no_argument, 0, 0}
301 static void
302 nonfatal (msg)
303 const char *msg;
305 bfd_nonfatal (msg);
306 exit_status = 1;
309 static void
310 dump_section_header (abfd, section, ignored)
311 bfd *abfd ATTRIBUTE_UNUSED;
312 asection *section;
313 PTR ignored ATTRIBUTE_UNUSED;
315 char *comma = "";
316 unsigned int opb = bfd_octets_per_byte (abfd);
318 printf ("%3d %-13s %08lx ", section->index,
319 bfd_get_section_name (abfd, section),
320 (unsigned long) bfd_section_size (abfd, section) / opb);
321 bfd_printf_vma (abfd, bfd_get_section_vma (abfd, section));
322 printf (" ");
323 bfd_printf_vma (abfd, section->lma);
324 printf (" %08lx 2**%u", (unsigned long) section->filepos,
325 bfd_get_section_alignment (abfd, section));
326 if (! wide_output)
327 printf ("\n ");
328 printf (" ");
330 #define PF(x, y) \
331 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
333 PF (SEC_HAS_CONTENTS, "CONTENTS");
334 PF (SEC_ALLOC, "ALLOC");
335 PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
336 PF (SEC_LOAD, "LOAD");
337 PF (SEC_RELOC, "RELOC");
338 PF (SEC_READONLY, "READONLY");
339 PF (SEC_CODE, "CODE");
340 PF (SEC_DATA, "DATA");
341 PF (SEC_ROM, "ROM");
342 PF (SEC_DEBUGGING, "DEBUGGING");
343 PF (SEC_NEVER_LOAD, "NEVER_LOAD");
344 PF (SEC_EXCLUDE, "EXCLUDE");
345 PF (SEC_SORT_ENTRIES, "SORT_ENTRIES");
346 PF (SEC_BLOCK, "BLOCK");
347 PF (SEC_CLINK, "CLINK");
348 PF (SEC_SMALL_DATA, "SMALL_DATA");
349 PF (SEC_SHARED, "SHARED");
350 PF (SEC_ARCH_BIT_0, "ARCH_BIT_0");
351 PF (SEC_THREAD_LOCAL, "THREAD_LOCAL");
353 if ((section->flags & SEC_LINK_ONCE) != 0)
355 const char *ls;
357 switch (section->flags & SEC_LINK_DUPLICATES)
359 default:
360 abort ();
361 case SEC_LINK_DUPLICATES_DISCARD:
362 ls = "LINK_ONCE_DISCARD";
363 break;
364 case SEC_LINK_DUPLICATES_ONE_ONLY:
365 ls = "LINK_ONCE_ONE_ONLY";
366 break;
367 case SEC_LINK_DUPLICATES_SAME_SIZE:
368 ls = "LINK_ONCE_SAME_SIZE";
369 break;
370 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
371 ls = "LINK_ONCE_SAME_CONTENTS";
372 break;
374 printf ("%s%s", comma, ls);
376 if (section->comdat != NULL)
377 printf (" (COMDAT %s %ld)", section->comdat->name,
378 section->comdat->symbol);
380 comma = ", ";
383 printf ("\n");
384 #undef PF
387 static void
388 dump_headers (abfd)
389 bfd *abfd;
391 printf (_("Sections:\n"));
393 #ifndef BFD64
394 printf (_("Idx Name Size VMA LMA File off Algn"));
395 #else
396 /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */
397 if (bfd_get_arch_size (abfd) == 32)
398 printf (_("Idx Name Size VMA LMA File off Algn"));
399 else
400 printf (_("Idx Name Size VMA LMA File off Algn"));
401 #endif
403 if (wide_output)
404 printf (_(" Flags"));
405 if (abfd->flags & HAS_LOAD_PAGE)
406 printf (_(" Pg"));
407 printf ("\n");
409 bfd_map_over_sections (abfd, dump_section_header, (PTR) NULL);
412 static asymbol **
413 slurp_symtab (abfd)
414 bfd *abfd;
416 asymbol **sy = (asymbol **) NULL;
417 long storage;
419 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
421 symcount = 0;
422 return NULL;
425 storage = bfd_get_symtab_upper_bound (abfd);
426 if (storage < 0)
427 bfd_fatal (bfd_get_filename (abfd));
428 if (storage)
429 sy = (asymbol **) xmalloc (storage);
431 symcount = bfd_canonicalize_symtab (abfd, sy);
432 if (symcount < 0)
433 bfd_fatal (bfd_get_filename (abfd));
434 return sy;
437 /* Read in the dynamic symbols. */
439 static asymbol **
440 slurp_dynamic_symtab (abfd)
441 bfd *abfd;
443 asymbol **sy = (asymbol **) NULL;
444 long storage;
446 storage = bfd_get_dynamic_symtab_upper_bound (abfd);
447 if (storage < 0)
449 if (!(bfd_get_file_flags (abfd) & DYNAMIC))
451 non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd));
452 dynsymcount = 0;
453 return NULL;
456 bfd_fatal (bfd_get_filename (abfd));
458 if (storage)
459 sy = (asymbol **) xmalloc (storage);
461 dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
462 if (dynsymcount < 0)
463 bfd_fatal (bfd_get_filename (abfd));
464 return sy;
467 /* Filter out (in place) symbols that are useless for disassembly.
468 COUNT is the number of elements in SYMBOLS.
469 Return the number of useful symbols. */
471 static long
472 remove_useless_symbols (symbols, count)
473 asymbol **symbols;
474 long count;
476 register asymbol **in_ptr = symbols, **out_ptr = symbols;
478 while (--count >= 0)
480 asymbol *sym = *in_ptr++;
482 if (sym->name == NULL || sym->name[0] == '\0')
483 continue;
484 if (sym->flags & (BSF_DEBUGGING))
485 continue;
486 if (bfd_is_und_section (sym->section)
487 || bfd_is_com_section (sym->section))
488 continue;
490 *out_ptr++ = sym;
492 return out_ptr - symbols;
495 /* Sort symbols into value order. */
497 static int
498 compare_symbols (ap, bp)
499 const PTR ap;
500 const PTR bp;
502 const asymbol *a = *(const asymbol **)ap;
503 const asymbol *b = *(const asymbol **)bp;
504 const char *an, *bn;
505 size_t anl, bnl;
506 bfd_boolean af, bf;
507 flagword aflags, bflags;
509 if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
510 return 1;
511 else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
512 return -1;
514 if (a->section > b->section)
515 return 1;
516 else if (a->section < b->section)
517 return -1;
519 an = bfd_asymbol_name (a);
520 bn = bfd_asymbol_name (b);
521 anl = strlen (an);
522 bnl = strlen (bn);
524 /* The symbols gnu_compiled and gcc2_compiled convey no real
525 information, so put them after other symbols with the same value. */
526 af = (strstr (an, "gnu_compiled") != NULL
527 || strstr (an, "gcc2_compiled") != NULL);
528 bf = (strstr (bn, "gnu_compiled") != NULL
529 || strstr (bn, "gcc2_compiled") != NULL);
531 if (af && ! bf)
532 return 1;
533 if (! af && bf)
534 return -1;
536 /* We use a heuristic for the file name, to try to sort it after
537 more useful symbols. It may not work on non Unix systems, but it
538 doesn't really matter; the only difference is precisely which
539 symbol names get printed. */
541 #define file_symbol(s, sn, snl) \
542 (((s)->flags & BSF_FILE) != 0 \
543 || ((sn)[(snl) - 2] == '.' \
544 && ((sn)[(snl) - 1] == 'o' \
545 || (sn)[(snl) - 1] == 'a')))
547 af = file_symbol (a, an, anl);
548 bf = file_symbol (b, bn, bnl);
550 if (af && ! bf)
551 return 1;
552 if (! af && bf)
553 return -1;
555 /* Try to sort global symbols before local symbols before function
556 symbols before debugging symbols. */
558 aflags = a->flags;
559 bflags = b->flags;
561 if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING))
563 if ((aflags & BSF_DEBUGGING) != 0)
564 return 1;
565 else
566 return -1;
568 if ((aflags & BSF_FUNCTION) != (bflags & BSF_FUNCTION))
570 if ((aflags & BSF_FUNCTION) != 0)
571 return -1;
572 else
573 return 1;
575 if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL))
577 if ((aflags & BSF_LOCAL) != 0)
578 return 1;
579 else
580 return -1;
582 if ((aflags & BSF_GLOBAL) != (bflags & BSF_GLOBAL))
584 if ((aflags & BSF_GLOBAL) != 0)
585 return -1;
586 else
587 return 1;
590 /* Symbols that start with '.' might be section names, so sort them
591 after symbols that don't start with '.'. */
592 if (an[0] == '.' && bn[0] != '.')
593 return 1;
594 if (an[0] != '.' && bn[0] == '.')
595 return -1;
597 /* Finally, if we can't distinguish them in any other way, try to
598 get consistent results by sorting the symbols by name. */
599 return strcmp (an, bn);
602 /* Sort relocs into address order. */
604 static int
605 compare_relocs (ap, bp)
606 const PTR ap;
607 const PTR bp;
609 const arelent *a = *(const arelent **)ap;
610 const arelent *b = *(const arelent **)bp;
612 if (a->address > b->address)
613 return 1;
614 else if (a->address < b->address)
615 return -1;
617 /* So that associated relocations tied to the same address show up
618 in the correct order, we don't do any further sorting. */
619 if (a > b)
620 return 1;
621 else if (a < b)
622 return -1;
623 else
624 return 0;
627 /* Print VMA to STREAM. If SKIP_ZEROES is TRUE, omit leading zeroes. */
629 static void
630 objdump_print_value (vma, info, skip_zeroes)
631 bfd_vma vma;
632 struct disassemble_info *info;
633 bfd_boolean skip_zeroes;
635 char buf[30];
636 char *p;
637 struct objdump_disasm_info *aux
638 = (struct objdump_disasm_info *) info->application_data;
640 bfd_sprintf_vma (aux->abfd, buf, vma);
641 if (! skip_zeroes)
642 p = buf;
643 else
645 for (p = buf; *p == '0'; ++p)
647 if (*p == '\0')
648 --p;
650 (*info->fprintf_func) (info->stream, "%s", p);
653 /* Print the name of a symbol. */
655 static void
656 objdump_print_symname (abfd, info, sym)
657 bfd *abfd;
658 struct disassemble_info *info;
659 asymbol *sym;
661 char *alloc;
662 const char *name;
664 alloc = NULL;
665 name = bfd_asymbol_name (sym);
666 if (do_demangle && name[0] != '\0')
668 /* Demangle the name. */
669 alloc = demangle (abfd, name);
670 name = alloc;
673 if (info != NULL)
674 (*info->fprintf_func) (info->stream, "%s", name);
675 else
676 printf ("%s", name);
678 if (alloc != NULL)
679 free (alloc);
682 /* Locate a symbol given a bfd, a section, and a VMA. If REQUIRE_SEC
683 is TRUE, then always require the symbol to be in the section. This
684 returns NULL if there is no suitable symbol. If PLACE is not NULL,
685 then *PLACE is set to the index of the symbol in sorted_syms. */
687 static asymbol *
688 find_symbol_for_address (abfd, sec, vma, require_sec, place)
689 bfd *abfd;
690 asection *sec;
691 bfd_vma vma;
692 bfd_boolean require_sec;
693 long *place;
695 /* @@ Would it speed things up to cache the last two symbols returned,
696 and maybe their address ranges? For many processors, only one memory
697 operand can be present at a time, so the 2-entry cache wouldn't be
698 constantly churned by code doing heavy memory accesses. */
700 /* Indices in `sorted_syms'. */
701 long min = 0;
702 long max = sorted_symcount;
703 long thisplace;
704 unsigned int opb = bfd_octets_per_byte (abfd);
706 if (sorted_symcount < 1)
707 return NULL;
709 /* Perform a binary search looking for the closest symbol to the
710 required value. We are searching the range (min, max]. */
711 while (min + 1 < max)
713 asymbol *sym;
715 thisplace = (max + min) / 2;
716 sym = sorted_syms[thisplace];
718 if (bfd_asymbol_value (sym) > vma)
719 max = thisplace;
720 else if (bfd_asymbol_value (sym) < vma)
721 min = thisplace;
722 else
724 min = thisplace;
725 break;
729 /* The symbol we want is now in min, the low end of the range we
730 were searching. If there are several symbols with the same
731 value, we want the first one. */
732 thisplace = min;
733 while (thisplace > 0
734 && (bfd_asymbol_value (sorted_syms[thisplace])
735 == bfd_asymbol_value (sorted_syms[thisplace - 1])))
736 --thisplace;
738 /* If the file is relocatable, and the symbol could be from this
739 section, prefer a symbol from this section over symbols from
740 others, even if the other symbol's value might be closer.
742 Note that this may be wrong for some symbol references if the
743 sections have overlapping memory ranges, but in that case there's
744 no way to tell what's desired without looking at the relocation
745 table. */
746 if (sorted_syms[thisplace]->section != sec
747 && (require_sec
748 || ((abfd->flags & HAS_RELOC) != 0
749 && vma >= bfd_get_section_vma (abfd, sec)
750 && vma < (bfd_get_section_vma (abfd, sec)
751 + bfd_section_size (abfd, sec) / opb))))
753 long i;
755 for (i = thisplace + 1; i < sorted_symcount; i++)
757 if (bfd_asymbol_value (sorted_syms[i])
758 != bfd_asymbol_value (sorted_syms[thisplace]))
759 break;
762 --i;
764 for (; i >= 0; i--)
766 if (sorted_syms[i]->section == sec
767 && (i == 0
768 || sorted_syms[i - 1]->section != sec
769 || (bfd_asymbol_value (sorted_syms[i])
770 != bfd_asymbol_value (sorted_syms[i - 1]))))
772 thisplace = i;
773 break;
777 if (sorted_syms[thisplace]->section != sec)
779 /* We didn't find a good symbol with a smaller value.
780 Look for one with a larger value. */
781 for (i = thisplace + 1; i < sorted_symcount; i++)
783 if (sorted_syms[i]->section == sec)
785 thisplace = i;
786 break;
791 if (sorted_syms[thisplace]->section != sec
792 && (require_sec
793 || ((abfd->flags & HAS_RELOC) != 0
794 && vma >= bfd_get_section_vma (abfd, sec)
795 && vma < (bfd_get_section_vma (abfd, sec)
796 + bfd_section_size (abfd, sec)))))
798 /* There is no suitable symbol. */
799 return NULL;
803 if (place != NULL)
804 *place = thisplace;
806 return sorted_syms[thisplace];
809 /* Print an address to INFO symbolically. */
811 static void
812 objdump_print_addr_with_sym (abfd, sec, sym, vma, info, skip_zeroes)
813 bfd *abfd;
814 asection *sec;
815 asymbol *sym;
816 bfd_vma vma;
817 struct disassemble_info *info;
818 bfd_boolean skip_zeroes;
820 objdump_print_value (vma, info, skip_zeroes);
822 if (sym == NULL)
824 bfd_vma secaddr;
826 (*info->fprintf_func) (info->stream, " <%s",
827 bfd_get_section_name (abfd, sec));
828 secaddr = bfd_get_section_vma (abfd, sec);
829 if (vma < secaddr)
831 (*info->fprintf_func) (info->stream, "-0x");
832 objdump_print_value (secaddr - vma, info, TRUE);
834 else if (vma > secaddr)
836 (*info->fprintf_func) (info->stream, "+0x");
837 objdump_print_value (vma - secaddr, info, TRUE);
839 (*info->fprintf_func) (info->stream, ">");
841 else
843 (*info->fprintf_func) (info->stream, " <");
844 objdump_print_symname (abfd, info, sym);
845 if (bfd_asymbol_value (sym) > vma)
847 (*info->fprintf_func) (info->stream, "-0x");
848 objdump_print_value (bfd_asymbol_value (sym) - vma, info, TRUE);
850 else if (vma > bfd_asymbol_value (sym))
852 (*info->fprintf_func) (info->stream, "+0x");
853 objdump_print_value (vma - bfd_asymbol_value (sym), info, TRUE);
855 (*info->fprintf_func) (info->stream, ">");
859 /* Print VMA to INFO, symbolically if possible. If SKIP_ZEROES is
860 TRUE, don't output leading zeroes. */
862 static void
863 objdump_print_addr (vma, info, skip_zeroes)
864 bfd_vma vma;
865 struct disassemble_info *info;
866 bfd_boolean skip_zeroes;
868 struct objdump_disasm_info *aux;
869 asymbol *sym;
871 if (sorted_symcount < 1)
873 (*info->fprintf_func) (info->stream, "0x");
874 objdump_print_value (vma, info, skip_zeroes);
875 return;
878 aux = (struct objdump_disasm_info *) info->application_data;
879 sym = find_symbol_for_address (aux->abfd, aux->sec, vma, aux->require_sec,
880 (long *) NULL);
881 objdump_print_addr_with_sym (aux->abfd, aux->sec, sym, vma, info,
882 skip_zeroes);
885 /* Print VMA to INFO. This function is passed to the disassembler
886 routine. */
888 static void
889 objdump_print_address (vma, info)
890 bfd_vma vma;
891 struct disassemble_info *info;
893 objdump_print_addr (vma, info, ! prefix_addresses);
896 /* Determine of the given address has a symbol associated with it. */
898 static int
899 objdump_symbol_at_address (vma, info)
900 bfd_vma vma;
901 struct disassemble_info * info;
903 struct objdump_disasm_info * aux;
904 asymbol * sym;
906 /* No symbols - do not bother checking. */
907 if (sorted_symcount < 1)
908 return 0;
910 aux = (struct objdump_disasm_info *) info->application_data;
911 sym = find_symbol_for_address (aux->abfd, aux->sec, vma, aux->require_sec,
912 (long *) NULL);
914 return (sym != NULL && (bfd_asymbol_value (sym) == vma));
917 /* Hold the last function name and the last line number we displayed
918 in a disassembly. */
920 static char *prev_functionname;
921 static unsigned int prev_line;
923 /* We keep a list of all files that we have seen when doing a
924 dissassembly with source, so that we know how much of the file to
925 display. This can be important for inlined functions. */
927 struct print_file_list
929 struct print_file_list *next;
930 char *filename;
931 unsigned int line;
932 FILE *f;
935 static struct print_file_list *print_files;
937 /* The number of preceding context lines to show when we start
938 displaying a file for the first time. */
940 #define SHOW_PRECEDING_CONTEXT_LINES (5)
942 /* Skip ahead to a given line in a file, optionally printing each
943 line. */
945 static void skip_to_line
946 PARAMS ((struct print_file_list *, unsigned int, bfd_boolean));
948 static void
949 skip_to_line (p, line, show)
950 struct print_file_list *p;
951 unsigned int line;
952 bfd_boolean show;
954 while (p->line < line)
956 char buf[100];
958 if (fgets (buf, sizeof buf, p->f) == NULL)
960 fclose (p->f);
961 p->f = NULL;
962 break;
965 if (show)
966 printf ("%s", buf);
968 if (strchr (buf, '\n') != NULL)
969 ++p->line;
973 /* Show the line number, or the source line, in a dissassembly
974 listing. */
976 static void
977 show_line (abfd, section, addr_offset)
978 bfd *abfd;
979 asection *section;
980 bfd_vma addr_offset;
982 const char *filename;
983 const char *functionname;
984 unsigned int line;
986 if (! with_line_numbers && ! with_source_code)
987 return;
989 if (! bfd_find_nearest_line (abfd, section, syms, addr_offset, &filename,
990 &functionname, &line))
991 return;
993 if (filename != NULL && *filename == '\0')
994 filename = NULL;
995 if (functionname != NULL && *functionname == '\0')
996 functionname = NULL;
998 if (with_line_numbers)
1000 if (functionname != NULL
1001 && (prev_functionname == NULL
1002 || strcmp (functionname, prev_functionname) != 0))
1003 printf ("%s():\n", functionname);
1004 if (line > 0 && line != prev_line)
1005 printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
1008 if (with_source_code
1009 && filename != NULL
1010 && line > 0)
1012 struct print_file_list **pp, *p;
1014 for (pp = &print_files; *pp != NULL; pp = &(*pp)->next)
1015 if (strcmp ((*pp)->filename, filename) == 0)
1016 break;
1017 p = *pp;
1019 if (p != NULL)
1021 if (p != print_files)
1023 int l;
1025 /* We have reencountered a file name which we saw
1026 earlier. This implies that either we are dumping out
1027 code from an included file, or the same file was
1028 linked in more than once. There are two common cases
1029 of an included file: inline functions in a header
1030 file, and a bison or flex skeleton file. In the
1031 former case we want to just start printing (but we
1032 back up a few lines to give context); in the latter
1033 case we want to continue from where we left off. I
1034 can't think of a good way to distinguish the cases,
1035 so I used a heuristic based on the file name. */
1036 if (strcmp (p->filename + strlen (p->filename) - 2, ".h") != 0)
1037 l = p->line;
1038 else
1040 l = line - SHOW_PRECEDING_CONTEXT_LINES;
1041 if (l < 0)
1042 l = 0;
1045 if (p->f == NULL)
1047 p->f = fopen (p->filename, "r");
1048 p->line = 0;
1050 if (p->f != NULL)
1051 skip_to_line (p, l, FALSE);
1053 if (print_files->f != NULL)
1055 fclose (print_files->f);
1056 print_files->f = NULL;
1060 if (p->f != NULL)
1062 skip_to_line (p, line, TRUE);
1063 *pp = p->next;
1064 p->next = print_files;
1065 print_files = p;
1068 else
1070 FILE *f;
1072 f = fopen (filename, "r");
1073 if (f != NULL)
1075 int l;
1077 p = ((struct print_file_list *)
1078 xmalloc (sizeof (struct print_file_list)));
1079 p->filename = xmalloc (strlen (filename) + 1);
1080 strcpy (p->filename, filename);
1081 p->line = 0;
1082 p->f = f;
1084 if (print_files != NULL && print_files->f != NULL)
1086 fclose (print_files->f);
1087 print_files->f = NULL;
1089 p->next = print_files;
1090 print_files = p;
1092 if (file_start_context)
1093 l = 0;
1094 else
1095 l = line - SHOW_PRECEDING_CONTEXT_LINES;
1096 if (l < 0)
1097 l = 0;
1098 skip_to_line (p, l, FALSE);
1099 if (p->f != NULL)
1100 skip_to_line (p, line, TRUE);
1105 if (functionname != NULL
1106 && (prev_functionname == NULL
1107 || strcmp (functionname, prev_functionname) != 0))
1109 if (prev_functionname != NULL)
1110 free (prev_functionname);
1111 prev_functionname = xmalloc (strlen (functionname) + 1);
1112 strcpy (prev_functionname, functionname);
1115 if (line > 0 && line != prev_line)
1116 prev_line = line;
1119 /* Pseudo FILE object for strings. */
1120 typedef struct
1122 char *buffer;
1123 size_t size;
1124 char *current;
1125 } SFILE;
1127 /* sprintf to a "stream" */
1129 static int
1130 objdump_sprintf VPARAMS ((SFILE *f, const char *format, ...))
1132 char *buf;
1133 size_t n;
1135 VA_OPEN (args, format);
1136 VA_FIXEDARG (args, SFILE *, f);
1137 VA_FIXEDARG (args, const char *, format);
1139 vasprintf (&buf, format, args);
1141 if (buf == NULL)
1143 va_end (args);
1144 fatal (_("Out of virtual memory"));
1147 n = strlen (buf);
1149 while ((size_t) ((f->buffer + f->size) - f->current) < n + 1)
1151 size_t curroff;
1153 curroff = f->current - f->buffer;
1154 f->size *= 2;
1155 f->buffer = xrealloc (f->buffer, f->size);
1156 f->current = f->buffer + curroff;
1159 memcpy (f->current, buf, n);
1160 f->current += n;
1161 f->current[0] = '\0';
1163 free (buf);
1165 VA_CLOSE (args);
1166 return n;
1169 /* The number of zeroes we want to see before we start skipping them.
1170 The number is arbitrarily chosen. */
1172 #ifndef SKIP_ZEROES
1173 #define SKIP_ZEROES (8)
1174 #endif
1176 /* The number of zeroes to skip at the end of a section. If the
1177 number of zeroes at the end is between SKIP_ZEROES_AT_END and
1178 SKIP_ZEROES, they will be disassembled. If there are fewer than
1179 SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic
1180 attempt to avoid disassembling zeroes inserted by section
1181 alignment. */
1183 #ifndef SKIP_ZEROES_AT_END
1184 #define SKIP_ZEROES_AT_END (3)
1185 #endif
1187 /* Disassemble some data in memory between given values. */
1189 static void
1190 disassemble_bytes (info, disassemble_fn, insns, data,
1191 start_offset, stop_offset, relppp,
1192 relppend)
1193 struct disassemble_info *info;
1194 disassembler_ftype disassemble_fn;
1195 bfd_boolean insns;
1196 bfd_byte *data;
1197 bfd_vma start_offset;
1198 bfd_vma stop_offset;
1199 arelent ***relppp;
1200 arelent **relppend;
1202 struct objdump_disasm_info *aux;
1203 asection *section;
1204 int octets_per_line;
1205 bfd_boolean done_dot;
1206 int skip_addr_chars;
1207 bfd_vma addr_offset;
1208 int opb = info->octets_per_byte;
1210 aux = (struct objdump_disasm_info *) info->application_data;
1211 section = aux->sec;
1213 if (insns)
1214 octets_per_line = 4;
1215 else
1216 octets_per_line = 16;
1218 /* Figure out how many characters to skip at the start of an
1219 address, to make the disassembly look nicer. We discard leading
1220 zeroes in chunks of 4, ensuring that there is always a leading
1221 zero remaining. */
1222 skip_addr_chars = 0;
1223 if (! prefix_addresses)
1225 char buf[30];
1226 char *s;
1228 bfd_sprintf_vma
1229 (aux->abfd, buf,
1230 (section->vma
1231 + bfd_section_size (section->owner, section) / opb));
1232 s = buf;
1233 while (s[0] == '0' && s[1] == '0' && s[2] == '0' && s[3] == '0'
1234 && s[4] == '0')
1236 skip_addr_chars += 4;
1237 s += 4;
1241 info->insn_info_valid = 0;
1243 done_dot = FALSE;
1244 addr_offset = start_offset;
1245 while (addr_offset < stop_offset)
1247 bfd_vma z;
1248 int octets = 0;
1249 bfd_boolean need_nl = FALSE;
1251 /* If we see more than SKIP_ZEROES octets of zeroes, we just
1252 print `...'. */
1253 for (z = addr_offset * opb; z < stop_offset * opb; z++)
1254 if (data[z] != 0)
1255 break;
1256 if (! disassemble_zeroes
1257 && (info->insn_info_valid == 0
1258 || info->branch_delay_insns == 0)
1259 && (z - addr_offset * opb >= SKIP_ZEROES
1260 || (z == stop_offset * opb &&
1261 z - addr_offset * opb < SKIP_ZEROES_AT_END)))
1263 printf ("\t...\n");
1265 /* If there are more nonzero octets to follow, we only skip
1266 zeroes in multiples of 4, to try to avoid running over
1267 the start of an instruction which happens to start with
1268 zero. */
1269 if (z != stop_offset * opb)
1270 z = addr_offset * opb + ((z - addr_offset * opb) &~ 3);
1272 octets = z - addr_offset * opb;
1274 else
1276 char buf[50];
1277 SFILE sfile;
1278 int bpc = 0;
1279 int pb = 0;
1281 done_dot = FALSE;
1283 if (with_line_numbers || with_source_code)
1284 /* The line number tables will refer to unadjusted
1285 section VMAs, so we must undo any VMA modifications
1286 when calling show_line. */
1287 show_line (aux->abfd, section, addr_offset - adjust_section_vma);
1289 if (! prefix_addresses)
1291 char *s;
1293 bfd_sprintf_vma (aux->abfd, buf, section->vma + addr_offset);
1294 for (s = buf + skip_addr_chars; *s == '0'; s++)
1295 *s = ' ';
1296 if (*s == '\0')
1297 *--s = '0';
1298 printf ("%s:\t", buf + skip_addr_chars);
1300 else
1302 aux->require_sec = TRUE;
1303 objdump_print_address (section->vma + addr_offset, info);
1304 aux->require_sec = FALSE;
1305 putchar (' ');
1308 if (insns)
1310 sfile.size = 120;
1311 sfile.buffer = xmalloc (sfile.size);
1312 sfile.current = sfile.buffer;
1313 info->fprintf_func = (fprintf_ftype) objdump_sprintf;
1314 info->stream = (FILE *) &sfile;
1315 info->bytes_per_line = 0;
1316 info->bytes_per_chunk = 0;
1318 #ifdef DISASSEMBLER_NEEDS_RELOCS
1319 /* FIXME: This is wrong. It tests the number of octets
1320 in the last instruction, not the current one. */
1321 if (*relppp < relppend
1322 && (**relppp)->address >= addr_offset
1323 && (**relppp)->address <= addr_offset + octets / opb)
1324 info->flags = INSN_HAS_RELOC;
1325 else
1326 #endif
1327 info->flags = 0;
1329 octets = (*disassemble_fn) (section->vma + addr_offset, info);
1330 info->fprintf_func = (fprintf_ftype) fprintf;
1331 info->stream = stdout;
1332 if (info->bytes_per_line != 0)
1333 octets_per_line = info->bytes_per_line;
1334 if (octets < 0)
1336 if (sfile.current != sfile.buffer)
1337 printf ("%s\n", sfile.buffer);
1338 free (sfile.buffer);
1339 break;
1342 else
1344 bfd_vma j;
1346 octets = octets_per_line;
1347 if (addr_offset + octets / opb > stop_offset)
1348 octets = (stop_offset - addr_offset) * opb;
1350 for (j = addr_offset * opb; j < addr_offset * opb + octets; ++j)
1352 if (ISPRINT (data[j]))
1353 buf[j - addr_offset * opb] = data[j];
1354 else
1355 buf[j - addr_offset * opb] = '.';
1357 buf[j - addr_offset * opb] = '\0';
1360 if (prefix_addresses
1361 ? show_raw_insn > 0
1362 : show_raw_insn >= 0)
1364 bfd_vma j;
1366 /* If ! prefix_addresses and ! wide_output, we print
1367 octets_per_line octets per line. */
1368 pb = octets;
1369 if (pb > octets_per_line && ! prefix_addresses && ! wide_output)
1370 pb = octets_per_line;
1372 if (info->bytes_per_chunk)
1373 bpc = info->bytes_per_chunk;
1374 else
1375 bpc = 1;
1377 for (j = addr_offset * opb; j < addr_offset * opb + pb; j += bpc)
1379 int k;
1380 if (bpc > 1 && info->display_endian == BFD_ENDIAN_LITTLE)
1382 for (k = bpc - 1; k >= 0; k--)
1383 printf ("%02x", (unsigned) data[j + k]);
1384 putchar (' ');
1386 else
1388 for (k = 0; k < bpc; k++)
1389 printf ("%02x", (unsigned) data[j + k]);
1390 putchar (' ');
1394 for (; pb < octets_per_line; pb += bpc)
1396 int k;
1398 for (k = 0; k < bpc; k++)
1399 printf (" ");
1400 putchar (' ');
1403 /* Separate raw data from instruction by extra space. */
1404 if (insns)
1405 putchar ('\t');
1406 else
1407 printf (" ");
1410 if (! insns)
1411 printf ("%s", buf);
1412 else
1414 printf ("%s", sfile.buffer);
1415 free (sfile.buffer);
1418 if (prefix_addresses
1419 ? show_raw_insn > 0
1420 : show_raw_insn >= 0)
1422 while (pb < octets)
1424 bfd_vma j;
1425 char *s;
1427 putchar ('\n');
1428 j = addr_offset * opb + pb;
1430 bfd_sprintf_vma (aux->abfd, buf, section->vma + j / opb);
1431 for (s = buf + skip_addr_chars; *s == '0'; s++)
1432 *s = ' ';
1433 if (*s == '\0')
1434 *--s = '0';
1435 printf ("%s:\t", buf + skip_addr_chars);
1437 pb += octets_per_line;
1438 if (pb > octets)
1439 pb = octets;
1440 for (; j < addr_offset * opb + pb; j += bpc)
1442 int k;
1444 if (bpc > 1 && info->display_endian == BFD_ENDIAN_LITTLE)
1446 for (k = bpc - 1; k >= 0; k--)
1447 printf ("%02x", (unsigned) data[j + k]);
1448 putchar (' ');
1450 else
1452 for (k = 0; k < bpc; k++)
1453 printf ("%02x", (unsigned) data[j + k]);
1454 putchar (' ');
1460 if (!wide_output)
1461 putchar ('\n');
1462 else
1463 need_nl = TRUE;
1466 if ((section->flags & SEC_RELOC) != 0
1467 #ifndef DISASSEMBLER_NEEDS_RELOCS
1468 && dump_reloc_info
1469 #endif
1472 while ((*relppp) < relppend
1473 && ((**relppp)->address >= (bfd_vma) addr_offset
1474 && (**relppp)->address < (bfd_vma) addr_offset + octets / opb))
1475 #ifdef DISASSEMBLER_NEEDS_RELOCS
1476 if (! dump_reloc_info)
1477 ++(*relppp);
1478 else
1479 #endif
1481 arelent *q;
1483 q = **relppp;
1485 if (wide_output)
1486 putchar ('\t');
1487 else
1488 printf ("\t\t\t");
1490 objdump_print_value (section->vma + q->address, info, TRUE);
1492 printf (": %s\t", q->howto->name);
1494 if (q->sym_ptr_ptr == NULL || *q->sym_ptr_ptr == NULL)
1495 printf ("*unknown*");
1496 else
1498 const char *sym_name;
1500 sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
1501 if (sym_name != NULL && *sym_name != '\0')
1502 objdump_print_symname (aux->abfd, info, *q->sym_ptr_ptr);
1503 else
1505 asection *sym_sec;
1507 sym_sec = bfd_get_section (*q->sym_ptr_ptr);
1508 sym_name = bfd_get_section_name (aux->abfd, sym_sec);
1509 if (sym_name == NULL || *sym_name == '\0')
1510 sym_name = "*unknown*";
1511 printf ("%s", sym_name);
1515 if (q->addend)
1517 printf ("+0x");
1518 objdump_print_value (q->addend, info, TRUE);
1521 printf ("\n");
1522 need_nl = FALSE;
1523 ++(*relppp);
1527 if (need_nl)
1528 printf ("\n");
1530 addr_offset += octets / opb;
1534 /* Disassemble the contents of an object file. */
1536 static void
1537 disassemble_data (abfd)
1538 bfd *abfd;
1540 unsigned long addr_offset;
1541 disassembler_ftype disassemble_fn;
1542 struct disassemble_info disasm_info;
1543 struct objdump_disasm_info aux;
1544 asection *section;
1545 unsigned int opb;
1547 print_files = NULL;
1548 prev_functionname = NULL;
1549 prev_line = -1;
1551 /* We make a copy of syms to sort. We don't want to sort syms
1552 because that will screw up the relocs. */
1553 sorted_syms = (asymbol **) xmalloc (symcount * sizeof (asymbol *));
1554 memcpy (sorted_syms, syms, symcount * sizeof (asymbol *));
1556 sorted_symcount = remove_useless_symbols (sorted_syms, symcount);
1558 /* Sort the symbols into section and symbol order. */
1559 qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols);
1561 INIT_DISASSEMBLE_INFO (disasm_info, stdout, fprintf);
1563 disasm_info.application_data = (PTR) &aux;
1564 aux.abfd = abfd;
1565 aux.require_sec = FALSE;
1566 disasm_info.print_address_func = objdump_print_address;
1567 disasm_info.symbol_at_address_func = objdump_symbol_at_address;
1569 if (machine != (char *) NULL)
1571 const bfd_arch_info_type *info = bfd_scan_arch (machine);
1573 if (info == NULL)
1574 fatal (_("Can't use supplied machine %s"), machine);
1576 abfd->arch_info = info;
1579 if (endian != BFD_ENDIAN_UNKNOWN)
1581 struct bfd_target *xvec;
1583 xvec = (struct bfd_target *) xmalloc (sizeof (struct bfd_target));
1584 memcpy (xvec, abfd->xvec, sizeof (struct bfd_target));
1585 xvec->byteorder = endian;
1586 abfd->xvec = xvec;
1589 disassemble_fn = disassembler (abfd);
1590 if (!disassemble_fn)
1592 non_fatal (_("Can't disassemble for architecture %s\n"),
1593 bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
1594 exit_status = 1;
1595 return;
1598 opb = bfd_octets_per_byte (abfd);
1600 disasm_info.flavour = bfd_get_flavour (abfd);
1601 disasm_info.arch = bfd_get_arch (abfd);
1602 disasm_info.mach = bfd_get_mach (abfd);
1603 disasm_info.disassembler_options = disassembler_options;
1604 disasm_info.octets_per_byte = opb;
1606 if (bfd_big_endian (abfd))
1607 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_BIG;
1608 else if (bfd_little_endian (abfd))
1609 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_LITTLE;
1610 else
1611 /* ??? Aborting here seems too drastic. We could default to big or little
1612 instead. */
1613 disasm_info.endian = BFD_ENDIAN_UNKNOWN;
1615 for (section = abfd->sections;
1616 section != (asection *) NULL;
1617 section = section->next)
1619 bfd_byte *data = NULL;
1620 bfd_size_type datasize = 0;
1621 arelent **relbuf = NULL;
1622 arelent **relpp = NULL;
1623 arelent **relppend = NULL;
1624 unsigned long stop_offset;
1625 asymbol *sym = NULL;
1626 long place = 0;
1628 if ((section->flags & SEC_LOAD) == 0
1629 || (! disassemble_all
1630 && only == NULL
1631 && (section->flags & SEC_CODE) == 0))
1632 continue;
1634 if (only != NULL)
1636 size_t i;
1638 for (i = 0; i < only_used; i++)
1639 if (strcmp (only [i], section->name) == 0)
1640 break;
1642 if (i == only_used)
1643 continue;
1646 if ((section->flags & SEC_RELOC) != 0
1647 #ifndef DISASSEMBLER_NEEDS_RELOCS
1648 && dump_reloc_info
1649 #endif
1652 long relsize;
1654 relsize = bfd_get_reloc_upper_bound (abfd, section);
1655 if (relsize < 0)
1656 bfd_fatal (bfd_get_filename (abfd));
1658 if (relsize > 0)
1660 long relcount;
1662 relbuf = (arelent **) xmalloc (relsize);
1663 relcount = bfd_canonicalize_reloc (abfd, section, relbuf, syms);
1664 if (relcount < 0)
1665 bfd_fatal (bfd_get_filename (abfd));
1667 /* Sort the relocs by address. */
1668 qsort (relbuf, relcount, sizeof (arelent *), compare_relocs);
1670 relpp = relbuf;
1671 relppend = relpp + relcount;
1673 /* Skip over the relocs belonging to addresses below the
1674 start address. */
1675 if (start_address != (bfd_vma) -1)
1676 while (relpp < relppend
1677 && (*relpp)->address < start_address)
1678 ++relpp;
1682 printf (_("Disassembly of section %s:\n"), section->name);
1684 datasize = bfd_get_section_size_before_reloc (section);
1685 if (datasize == 0)
1686 continue;
1688 data = (bfd_byte *) xmalloc ((size_t) datasize);
1690 bfd_get_section_contents (abfd, section, data, 0, datasize);
1692 aux.sec = section;
1693 disasm_info.buffer = data;
1694 disasm_info.buffer_vma = section->vma;
1695 disasm_info.buffer_length = datasize;
1696 disasm_info.section = section;
1698 if (start_address == (bfd_vma) -1
1699 || start_address < disasm_info.buffer_vma)
1700 addr_offset = 0;
1701 else
1702 addr_offset = start_address - disasm_info.buffer_vma;
1704 if (stop_address == (bfd_vma) -1)
1705 stop_offset = datasize / opb;
1706 else
1708 if (stop_address < disasm_info.buffer_vma)
1709 stop_offset = 0;
1710 else
1711 stop_offset = stop_address - disasm_info.buffer_vma;
1712 if (stop_offset > disasm_info.buffer_length / opb)
1713 stop_offset = disasm_info.buffer_length / opb;
1716 sym = find_symbol_for_address (abfd, section, section->vma + addr_offset,
1717 TRUE, &place);
1719 while (addr_offset < stop_offset)
1721 asymbol *nextsym;
1722 unsigned long nextstop_offset;
1723 bfd_boolean insns;
1725 if (sym != NULL && bfd_asymbol_value (sym) <= section->vma + addr_offset)
1727 int x;
1729 for (x = place;
1730 (x < sorted_symcount
1731 && bfd_asymbol_value (sorted_syms[x]) <= section->vma + addr_offset);
1732 ++x)
1733 continue;
1735 disasm_info.symbols = & sorted_syms[place];
1736 disasm_info.num_symbols = x - place;
1738 else
1739 disasm_info.symbols = NULL;
1741 if (! prefix_addresses)
1743 (* disasm_info.fprintf_func) (disasm_info.stream, "\n");
1744 objdump_print_addr_with_sym (abfd, section, sym,
1745 section->vma + addr_offset,
1746 &disasm_info,
1747 FALSE);
1748 (* disasm_info.fprintf_func) (disasm_info.stream, ":\n");
1751 if (sym != NULL && bfd_asymbol_value (sym) > section->vma + addr_offset)
1752 nextsym = sym;
1753 else if (sym == NULL)
1754 nextsym = NULL;
1755 else
1757 /* Search forward for the next appropriate symbol in
1758 SECTION. Note that all the symbols are sorted
1759 together into one big array, and that some sections
1760 may have overlapping addresses. */
1761 while (place < sorted_symcount
1762 && (sorted_syms[place]->section != section
1763 || (bfd_asymbol_value (sorted_syms[place])
1764 <= bfd_asymbol_value (sym))))
1765 ++place;
1766 if (place >= sorted_symcount)
1767 nextsym = NULL;
1768 else
1769 nextsym = sorted_syms[place];
1772 if (sym != NULL && bfd_asymbol_value (sym) > section->vma + addr_offset)
1774 nextstop_offset = bfd_asymbol_value (sym) - section->vma;
1775 if (nextstop_offset > stop_offset)
1776 nextstop_offset = stop_offset;
1778 else if (nextsym == NULL)
1779 nextstop_offset = stop_offset;
1780 else
1782 nextstop_offset = bfd_asymbol_value (nextsym) - section->vma;
1783 if (nextstop_offset > stop_offset)
1784 nextstop_offset = stop_offset;
1787 /* If a symbol is explicitly marked as being an object
1788 rather than a function, just dump the bytes without
1789 disassembling them. */
1790 if (disassemble_all
1791 || sym == NULL
1792 || bfd_asymbol_value (sym) > section->vma + addr_offset
1793 || ((sym->flags & BSF_OBJECT) == 0
1794 && (strstr (bfd_asymbol_name (sym), "gnu_compiled")
1795 == NULL)
1796 && (strstr (bfd_asymbol_name (sym), "gcc2_compiled")
1797 == NULL))
1798 || (sym->flags & BSF_FUNCTION) != 0)
1799 insns = TRUE;
1800 else
1801 insns = FALSE;
1803 disassemble_bytes (&disasm_info, disassemble_fn, insns, data,
1804 addr_offset, nextstop_offset, &relpp, relppend);
1806 addr_offset = nextstop_offset;
1807 sym = nextsym;
1810 free (data);
1812 if (relbuf != NULL)
1813 free (relbuf);
1815 free (sorted_syms);
1818 /* Dump the stabs sections from an object file that has a section that
1819 uses Sun stabs encoding. */
1821 static void
1822 dump_stabs (abfd)
1823 bfd *abfd;
1825 dump_section_stabs (abfd, ".stab", ".stabstr");
1826 dump_section_stabs (abfd, ".stab.excl", ".stab.exclstr");
1827 dump_section_stabs (abfd, ".stab.index", ".stab.indexstr");
1828 dump_section_stabs (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
1831 /* Read ABFD's stabs section STABSECT_NAME into `stabs'
1832 and string table section STRSECT_NAME into `strtab'.
1833 If the section exists and was read, allocate the space and return TRUE.
1834 Otherwise return FALSE. */
1836 static bfd_boolean
1837 read_section_stabs (abfd, stabsect_name, strsect_name)
1838 bfd *abfd;
1839 const char *stabsect_name;
1840 const char *strsect_name;
1842 asection *stabsect, *stabstrsect;
1844 stabsect = bfd_get_section_by_name (abfd, stabsect_name);
1845 if (0 == stabsect)
1847 printf (_("No %s section present\n\n"), stabsect_name);
1848 return FALSE;
1851 stabstrsect = bfd_get_section_by_name (abfd, strsect_name);
1852 if (0 == stabstrsect)
1854 non_fatal (_("%s has no %s section"),
1855 bfd_get_filename (abfd), strsect_name);
1856 exit_status = 1;
1857 return FALSE;
1860 stab_size = bfd_section_size (abfd, stabsect);
1861 stabstr_size = bfd_section_size (abfd, stabstrsect);
1863 stabs = (bfd_byte *) xmalloc (stab_size);
1864 strtab = (char *) xmalloc (stabstr_size);
1866 if (! bfd_get_section_contents (abfd, stabsect, (PTR) stabs, 0, stab_size))
1868 non_fatal (_("Reading %s section of %s failed: %s"),
1869 stabsect_name, bfd_get_filename (abfd),
1870 bfd_errmsg (bfd_get_error ()));
1871 free (stabs);
1872 free (strtab);
1873 exit_status = 1;
1874 return FALSE;
1877 if (! bfd_get_section_contents (abfd, stabstrsect, (PTR) strtab, 0,
1878 stabstr_size))
1880 non_fatal (_("Reading %s section of %s failed: %s\n"),
1881 strsect_name, bfd_get_filename (abfd),
1882 bfd_errmsg (bfd_get_error ()));
1883 free (stabs);
1884 free (strtab);
1885 exit_status = 1;
1886 return FALSE;
1889 return TRUE;
1892 /* Stabs entries use a 12 byte format:
1893 4 byte string table index
1894 1 byte stab type
1895 1 byte stab other field
1896 2 byte stab desc field
1897 4 byte stab value
1898 FIXME: This will have to change for a 64 bit object format. */
1900 #define STRDXOFF (0)
1901 #define TYPEOFF (4)
1902 #define OTHEROFF (5)
1903 #define DESCOFF (6)
1904 #define VALOFF (8)
1905 #define STABSIZE (12)
1907 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
1908 using string table section STRSECT_NAME (in `strtab'). */
1910 static void
1911 print_section_stabs (abfd, stabsect_name, strsect_name)
1912 bfd *abfd;
1913 const char *stabsect_name;
1914 const char *strsect_name ATTRIBUTE_UNUSED;
1916 int i;
1917 unsigned file_string_table_offset = 0, next_file_string_table_offset = 0;
1918 bfd_byte *stabp, *stabs_end;
1920 stabp = stabs;
1921 stabs_end = stabp + stab_size;
1923 printf (_("Contents of %s section:\n\n"), stabsect_name);
1924 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
1926 /* Loop through all symbols and print them.
1928 We start the index at -1 because there is a dummy symbol on
1929 the front of stabs-in-{coff,elf} sections that supplies sizes. */
1930 for (i = -1; stabp < stabs_end; stabp += STABSIZE, i++)
1932 const char *name;
1933 unsigned long strx;
1934 unsigned char type, other;
1935 unsigned short desc;
1936 bfd_vma value;
1938 strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
1939 type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
1940 other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
1941 desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
1942 value = bfd_h_get_32 (abfd, stabp + VALOFF);
1944 printf ("\n%-6d ", i);
1945 /* Either print the stab name, or, if unnamed, print its number
1946 again (makes consistent formatting for tools like awk). */
1947 name = bfd_get_stab_name (type);
1948 if (name != NULL)
1949 printf ("%-6s", name);
1950 else if (type == N_UNDF)
1951 printf ("HdrSym");
1952 else
1953 printf ("%-6d", type);
1954 printf (" %-6d %-6d ", other, desc);
1955 bfd_printf_vma (abfd, value);
1956 printf (" %-6lu", strx);
1958 /* Symbols with type == 0 (N_UNDF) specify the length of the
1959 string table associated with this file. We use that info
1960 to know how to relocate the *next* file's string table indices. */
1961 if (type == N_UNDF)
1963 file_string_table_offset = next_file_string_table_offset;
1964 next_file_string_table_offset += value;
1966 else
1968 /* Using the (possibly updated) string table offset, print the
1969 string (if any) associated with this symbol. */
1970 if ((strx + file_string_table_offset) < stabstr_size)
1971 printf (" %s", &strtab[strx + file_string_table_offset]);
1972 else
1973 printf (" *");
1976 printf ("\n\n");
1979 static void
1980 dump_section_stabs (abfd, stabsect_name, strsect_name)
1981 bfd *abfd;
1982 char *stabsect_name;
1983 char *strsect_name;
1985 asection *s;
1987 /* Check for section names for which stabsect_name is a prefix, to
1988 handle .stab0, etc. */
1989 for (s = abfd->sections;
1990 s != NULL;
1991 s = s->next)
1993 int len;
1995 len = strlen (stabsect_name);
1997 /* If the prefix matches, and the files section name ends with a
1998 nul or a digit, then we match. I.e., we want either an exact
1999 match or a section followed by a number. */
2000 if (strncmp (stabsect_name, s->name, len) == 0
2001 && (s->name[len] == '\000'
2002 || ISDIGIT (s->name[len])))
2004 if (read_section_stabs (abfd, s->name, strsect_name))
2006 print_section_stabs (abfd, s->name, strsect_name);
2007 free (stabs);
2008 free (strtab);
2015 static void
2016 dump_bfd_header (abfd)
2017 bfd *abfd;
2019 char *comma = "";
2021 printf (_("architecture: %s, "),
2022 bfd_printable_arch_mach (bfd_get_arch (abfd),
2023 bfd_get_mach (abfd)));
2024 printf (_("flags 0x%08x:\n"), abfd->flags);
2026 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
2027 PF (HAS_RELOC, "HAS_RELOC");
2028 PF (EXEC_P, "EXEC_P");
2029 PF (HAS_LINENO, "HAS_LINENO");
2030 PF (HAS_DEBUG, "HAS_DEBUG");
2031 PF (HAS_SYMS, "HAS_SYMS");
2032 PF (HAS_LOCALS, "HAS_LOCALS");
2033 PF (DYNAMIC, "DYNAMIC");
2034 PF (WP_TEXT, "WP_TEXT");
2035 PF (D_PAGED, "D_PAGED");
2036 PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
2037 PF (HAS_LOAD_PAGE, "HAS_LOAD_PAGE");
2038 printf (_("\nstart address 0x"));
2039 bfd_printf_vma (abfd, abfd->start_address);
2040 printf ("\n");
2044 static void
2045 dump_bfd_private_header (abfd)
2046 bfd *abfd;
2048 bfd_print_private_bfd_data (abfd, stdout);
2051 /* Dump selected contents of ABFD. */
2053 static void
2054 dump_bfd (abfd)
2055 bfd *abfd;
2057 /* If we are adjusting section VMA's, change them all now. Changing
2058 the BFD information is a hack. However, we must do it, or
2059 bfd_find_nearest_line will not do the right thing. */
2060 if (adjust_section_vma != 0)
2062 asection *s;
2064 for (s = abfd->sections; s != NULL; s = s->next)
2066 s->vma += adjust_section_vma;
2067 s->lma += adjust_section_vma;
2071 printf (_("\n%s: file format %s\n"), bfd_get_filename (abfd),
2072 abfd->xvec->name);
2073 if (dump_ar_hdrs)
2074 print_arelt_descr (stdout, abfd, TRUE);
2075 if (dump_file_header)
2076 dump_bfd_header (abfd);
2077 if (dump_private_headers)
2078 dump_bfd_private_header (abfd);
2079 putchar ('\n');
2080 if (dump_section_headers)
2081 dump_headers (abfd);
2083 if (dump_symtab || dump_reloc_info || disassemble || dump_debugging)
2084 syms = slurp_symtab (abfd);
2085 if (dump_dynamic_symtab || dump_dynamic_reloc_info)
2086 dynsyms = slurp_dynamic_symtab (abfd);
2088 if (dump_symtab)
2089 dump_symbols (abfd, FALSE);
2090 if (dump_dynamic_symtab)
2091 dump_symbols (abfd, TRUE);
2092 if (dump_stab_section_info)
2093 dump_stabs (abfd);
2094 if (dump_reloc_info && ! disassemble)
2095 dump_relocs (abfd);
2096 if (dump_dynamic_reloc_info)
2097 dump_dynamic_relocs (abfd);
2098 if (dump_section_contents)
2099 dump_data (abfd);
2100 if (disassemble)
2101 disassemble_data (abfd);
2102 if (dump_debugging)
2104 PTR dhandle;
2106 dhandle = read_debugging_info (abfd, syms, symcount);
2107 if (dhandle != NULL)
2109 if (! print_debugging_info (stdout, dhandle))
2111 non_fatal (_("%s: printing debugging information failed"),
2112 bfd_get_filename (abfd));
2113 exit_status = 1;
2118 if (syms)
2120 free (syms);
2121 syms = NULL;
2124 if (dynsyms)
2126 free (dynsyms);
2127 dynsyms = NULL;
2131 static void
2132 display_bfd (abfd)
2133 bfd *abfd;
2135 char **matching;
2137 if (bfd_check_format_matches (abfd, bfd_object, &matching))
2139 dump_bfd (abfd);
2140 return;
2143 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
2145 nonfatal (bfd_get_filename (abfd));
2146 list_matching_formats (matching);
2147 free (matching);
2148 return;
2151 if (bfd_get_error () != bfd_error_file_not_recognized)
2153 nonfatal (bfd_get_filename (abfd));
2154 return;
2157 if (bfd_check_format_matches (abfd, bfd_core, &matching))
2159 dump_bfd (abfd);
2160 return;
2163 nonfatal (bfd_get_filename (abfd));
2165 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
2167 list_matching_formats (matching);
2168 free (matching);
2172 static void
2173 display_file (filename, target)
2174 char *filename;
2175 char *target;
2177 bfd *file, *arfile = (bfd *) NULL;
2179 file = bfd_openr (filename, target);
2180 if (file == NULL)
2182 nonfatal (filename);
2183 return;
2186 if (bfd_check_format (file, bfd_archive))
2188 bfd *last_arfile = NULL;
2190 printf (_("In archive %s:\n"), bfd_get_filename (file));
2191 for (;;)
2193 bfd_set_error (bfd_error_no_error);
2195 arfile = bfd_openr_next_archived_file (file, arfile);
2196 if (arfile == NULL)
2198 if (bfd_get_error () != bfd_error_no_more_archived_files)
2199 nonfatal (bfd_get_filename (file));
2200 break;
2203 display_bfd (arfile);
2205 if (last_arfile != NULL)
2206 bfd_close (last_arfile);
2207 last_arfile = arfile;
2210 if (last_arfile != NULL)
2211 bfd_close (last_arfile);
2213 else
2214 display_bfd (file);
2216 bfd_close (file);
2219 /* Actually display the various requested regions. */
2221 static void
2222 dump_data (abfd)
2223 bfd *abfd;
2225 asection *section;
2226 bfd_byte *data = 0;
2227 bfd_size_type datasize = 0;
2228 bfd_size_type addr_offset;
2229 bfd_size_type start_offset, stop_offset;
2230 unsigned int opb = bfd_octets_per_byte (abfd);
2232 for (section = abfd->sections; section != NULL; section =
2233 section->next)
2235 int onaline = 16;
2236 size_t i;
2238 for (i = 0; i < only_used; i++)
2239 if (strcmp (only [i], section->name) == 0)
2240 break;
2242 if (only_used == 0 || i != only_used)
2244 if (section->flags & SEC_HAS_CONTENTS)
2246 char buf[64];
2247 int count, width;
2249 printf (_("Contents of section %s:\n"), section->name);
2251 if (bfd_section_size (abfd, section) == 0)
2252 continue;
2253 data = (bfd_byte *) xmalloc ((size_t) bfd_section_size (abfd, section));
2254 datasize = bfd_section_size (abfd, section);
2257 bfd_get_section_contents (abfd, section, (PTR) data, 0, bfd_section_size (abfd, section));
2259 if (start_address == (bfd_vma) -1
2260 || start_address < section->vma)
2261 start_offset = 0;
2262 else
2263 start_offset = start_address - section->vma;
2264 if (stop_address == (bfd_vma) -1)
2265 stop_offset = bfd_section_size (abfd, section) / opb;
2266 else
2268 if (stop_address < section->vma)
2269 stop_offset = 0;
2270 else
2271 stop_offset = stop_address - section->vma;
2272 if (stop_offset > bfd_section_size (abfd, section) / opb)
2273 stop_offset = bfd_section_size (abfd, section) / opb;
2276 width = 4;
2278 bfd_sprintf_vma (abfd, buf, start_offset + section->vma);
2279 if (strlen (buf) >= sizeof (buf))
2280 abort ();
2281 count = 0;
2282 while (buf[count] == '0' && buf[count+1] != '\0')
2283 count++;
2284 count = strlen (buf) - count;
2285 if (count > width)
2286 width = count;
2288 bfd_sprintf_vma (abfd, buf, stop_offset + section->vma - 1);
2289 if (strlen (buf) >= sizeof (buf))
2290 abort ();
2291 count = 0;
2292 while (buf[count] == '0' && buf[count+1] != '\0')
2293 count++;
2294 count = strlen (buf) - count;
2295 if (count > width)
2296 width = count;
2298 for (addr_offset = start_offset;
2299 addr_offset < stop_offset; addr_offset += onaline / opb)
2301 bfd_size_type j;
2303 bfd_sprintf_vma (abfd, buf, (addr_offset + section->vma));
2304 count = strlen (buf);
2305 if ((size_t) count >= sizeof (buf))
2306 abort ();
2307 putchar (' ');
2308 while (count < width)
2310 putchar ('0');
2311 count++;
2313 fputs (buf + count - width, stdout);
2314 putchar (' ');
2316 for (j = addr_offset * opb;
2317 j < addr_offset * opb + onaline; j++)
2319 if (j < stop_offset * opb)
2320 printf ("%02x", (unsigned) (data[j]));
2321 else
2322 printf (" ");
2323 if ((j & 3) == 3)
2324 printf (" ");
2327 printf (" ");
2328 for (j = addr_offset * opb;
2329 j < addr_offset * opb + onaline; j++)
2331 if (j >= stop_offset * opb)
2332 printf (" ");
2333 else
2334 printf ("%c", ISPRINT (data[j]) ? data[j] : '.');
2336 putchar ('\n');
2338 free (data);
2344 /* Should perhaps share code and display with nm? */
2346 static void
2347 dump_symbols (abfd, dynamic)
2348 bfd *abfd ATTRIBUTE_UNUSED;
2349 bfd_boolean dynamic;
2351 asymbol **current;
2352 long max;
2353 long count;
2355 if (dynamic)
2357 current = dynsyms;
2358 max = dynsymcount;
2359 printf ("DYNAMIC SYMBOL TABLE:\n");
2361 else
2363 current = syms;
2364 max = symcount;
2365 printf ("SYMBOL TABLE:\n");
2368 if (max == 0)
2369 printf (_("no symbols\n"));
2371 for (count = 0; count < max; count++)
2373 if (*current)
2375 bfd *cur_bfd = bfd_asymbol_bfd (*current);
2377 if (cur_bfd != NULL)
2379 const char *name;
2380 char *alloc;
2382 name = (*current)->name;
2383 alloc = NULL;
2384 if (do_demangle && name != NULL && *name != '\0')
2386 /* If we want to demangle the name, we demangle it
2387 here, and temporarily clobber it while calling
2388 bfd_print_symbol. FIXME: This is a gross hack. */
2389 alloc = demangle (cur_bfd, name);
2390 (*current)->name = alloc;
2393 bfd_print_symbol (cur_bfd, stdout, *current,
2394 bfd_print_symbol_all);
2396 (*current)->name = name;
2397 if (alloc != NULL)
2398 free (alloc);
2400 printf ("\n");
2403 current++;
2405 printf ("\n");
2406 printf ("\n");
2409 static void
2410 dump_relocs (abfd)
2411 bfd *abfd;
2413 arelent **relpp;
2414 long relcount;
2415 asection *a;
2417 for (a = abfd->sections; a != (asection *) NULL; a = a->next)
2419 long relsize;
2421 if (bfd_is_abs_section (a))
2422 continue;
2423 if (bfd_is_und_section (a))
2424 continue;
2425 if (bfd_is_com_section (a))
2426 continue;
2428 if (only)
2430 size_t i;
2432 for (i = 0; i < only_used; i++)
2433 if (strcmp (only [i], a->name) == 0)
2434 break;
2436 if (i == only_used)
2437 continue;
2439 else if ((a->flags & SEC_RELOC) == 0)
2440 continue;
2442 relsize = bfd_get_reloc_upper_bound (abfd, a);
2443 if (relsize < 0)
2444 bfd_fatal (bfd_get_filename (abfd));
2446 printf ("RELOCATION RECORDS FOR [%s]:", a->name);
2448 if (relsize == 0)
2450 printf (" (none)\n\n");
2452 else
2454 relpp = (arelent **) xmalloc (relsize);
2455 relcount = bfd_canonicalize_reloc (abfd, a, relpp, syms);
2457 if (relcount < 0)
2458 bfd_fatal (bfd_get_filename (abfd));
2459 else if (relcount == 0)
2460 printf (" (none)\n\n");
2461 else
2463 printf ("\n");
2464 dump_reloc_set (abfd, a, relpp, relcount);
2465 printf ("\n\n");
2467 free (relpp);
2472 static void
2473 dump_dynamic_relocs (abfd)
2474 bfd *abfd;
2476 long relsize;
2477 arelent **relpp;
2478 long relcount;
2480 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
2481 if (relsize < 0)
2482 bfd_fatal (bfd_get_filename (abfd));
2484 printf ("DYNAMIC RELOCATION RECORDS");
2486 if (relsize == 0)
2487 printf (" (none)\n\n");
2488 else
2490 relpp = (arelent **) xmalloc (relsize);
2491 relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
2493 if (relcount < 0)
2494 bfd_fatal (bfd_get_filename (abfd));
2495 else if (relcount == 0)
2496 printf (" (none)\n\n");
2497 else
2499 printf ("\n");
2500 dump_reloc_set (abfd, (asection *) NULL, relpp, relcount);
2501 printf ("\n\n");
2503 free (relpp);
2507 static void
2508 dump_reloc_set (abfd, sec, relpp, relcount)
2509 bfd *abfd;
2510 asection *sec;
2511 arelent **relpp;
2512 long relcount;
2514 arelent **p;
2515 char *last_filename, *last_functionname;
2516 unsigned int last_line;
2518 /* Get column headers lined up reasonably. */
2520 static int width;
2522 if (width == 0)
2524 char buf[30];
2525 bfd_sprintf_vma (abfd, buf, (bfd_vma) -1);
2526 width = strlen (buf) - 7;
2528 printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
2531 last_filename = NULL;
2532 last_functionname = NULL;
2533 last_line = 0;
2535 for (p = relpp; relcount && *p != (arelent *) NULL; p++, relcount--)
2537 arelent *q = *p;
2538 const char *filename, *functionname;
2539 unsigned int line;
2540 const char *sym_name;
2541 const char *section_name;
2543 if (start_address != (bfd_vma) -1
2544 && q->address < start_address)
2545 continue;
2546 if (stop_address != (bfd_vma) -1
2547 && q->address > stop_address)
2548 continue;
2550 if (with_line_numbers
2551 && sec != NULL
2552 && bfd_find_nearest_line (abfd, sec, syms, q->address,
2553 &filename, &functionname, &line))
2555 if (functionname != NULL
2556 && (last_functionname == NULL
2557 || strcmp (functionname, last_functionname) != 0))
2559 printf ("%s():\n", functionname);
2560 if (last_functionname != NULL)
2561 free (last_functionname);
2562 last_functionname = xstrdup (functionname);
2565 if (line > 0
2566 && (line != last_line
2567 || (filename != NULL
2568 && last_filename != NULL
2569 && strcmp (filename, last_filename) != 0)))
2571 printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
2572 last_line = line;
2573 if (last_filename != NULL)
2574 free (last_filename);
2575 if (filename == NULL)
2576 last_filename = NULL;
2577 else
2578 last_filename = xstrdup (filename);
2582 if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
2584 sym_name = (*(q->sym_ptr_ptr))->name;
2585 section_name = (*(q->sym_ptr_ptr))->section->name;
2587 else
2589 sym_name = NULL;
2590 section_name = NULL;
2593 if (sym_name)
2595 bfd_printf_vma (abfd, q->address);
2596 if (q->howto->name)
2597 printf (" %-16s ", q->howto->name);
2598 else
2599 printf (" %-16d ", q->howto->type);
2600 objdump_print_symname (abfd, (struct disassemble_info *) NULL,
2601 *q->sym_ptr_ptr);
2603 else
2605 if (section_name == (const char *) NULL)
2606 section_name = "*unknown*";
2607 bfd_printf_vma (abfd, q->address);
2608 printf (" %-16s [%s]",
2609 q->howto->name,
2610 section_name);
2613 if (q->addend)
2615 printf ("+0x");
2616 bfd_printf_vma (abfd, q->addend);
2619 printf ("\n");
2623 int main PARAMS ((int, char **));
2626 main (argc, argv)
2627 int argc;
2628 char **argv;
2630 int c;
2631 char *target = default_target;
2632 bfd_boolean seenflag = FALSE;
2634 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
2635 setlocale (LC_MESSAGES, "");
2636 #endif
2637 #if defined (HAVE_SETLOCALE)
2638 setlocale (LC_CTYPE, "");
2639 #endif
2640 bindtextdomain (PACKAGE, LOCALEDIR);
2641 textdomain (PACKAGE);
2643 program_name = *argv;
2644 xmalloc_set_program_name (program_name);
2646 START_PROGRESS (program_name, 0);
2648 bfd_init ();
2649 set_default_bfd_target ();
2651 while ((c = getopt_long (argc, argv, "pib:m:M:VvCdDlfaHhrRtTxsSj:wE:zgG",
2652 long_options, (int *) 0))
2653 != EOF)
2655 switch (c)
2657 case 0:
2658 break; /* We've been given a long option. */
2659 case 'm':
2660 machine = optarg;
2661 break;
2662 case 'M':
2663 disassembler_options = optarg;
2664 break;
2665 case 'j':
2666 if (only == NULL)
2668 only_size = 8;
2669 only = (char **) xmalloc (only_size * sizeof (char *));
2671 else if (only_used == only_size)
2673 only_size += 8;
2674 only = (char **) xrealloc (only,
2675 only_size * sizeof (char *));
2677 only [only_used++] = optarg;
2678 break;
2679 case 'l':
2680 with_line_numbers = TRUE;
2681 break;
2682 case 'b':
2683 target = optarg;
2684 break;
2685 case 'C':
2686 do_demangle = TRUE;
2687 if (optarg != NULL)
2689 enum demangling_styles style;
2691 style = cplus_demangle_name_to_style (optarg);
2692 if (style == unknown_demangling)
2693 fatal (_("unknown demangling style `%s'"),
2694 optarg);
2696 cplus_demangle_set_style (style);
2698 break;
2699 case 'w':
2700 wide_output = TRUE;
2701 break;
2702 case OPTION_ADJUST_VMA:
2703 adjust_section_vma = parse_vma (optarg, "--adjust-vma");
2704 break;
2705 case OPTION_START_ADDRESS:
2706 start_address = parse_vma (optarg, "--start-address");
2707 break;
2708 case OPTION_STOP_ADDRESS:
2709 stop_address = parse_vma (optarg, "--stop-address");
2710 break;
2711 case 'E':
2712 if (strcmp (optarg, "B") == 0)
2713 endian = BFD_ENDIAN_BIG;
2714 else if (strcmp (optarg, "L") == 0)
2715 endian = BFD_ENDIAN_LITTLE;
2716 else
2718 non_fatal (_("unrecognized -E option"));
2719 usage (stderr, 1);
2721 break;
2722 case OPTION_ENDIAN:
2723 if (strncmp (optarg, "big", strlen (optarg)) == 0)
2724 endian = BFD_ENDIAN_BIG;
2725 else if (strncmp (optarg, "little", strlen (optarg)) == 0)
2726 endian = BFD_ENDIAN_LITTLE;
2727 else
2729 non_fatal (_("unrecognized --endian type `%s'"), optarg);
2730 usage (stderr, 1);
2732 break;
2734 case 'f':
2735 dump_file_header = TRUE;
2736 seenflag = TRUE;
2737 break;
2738 case 'i':
2739 formats_info = TRUE;
2740 seenflag = TRUE;
2741 break;
2742 case 'p':
2743 dump_private_headers = TRUE;
2744 seenflag = TRUE;
2745 break;
2746 case 'x':
2747 dump_private_headers = TRUE;
2748 dump_symtab = TRUE;
2749 dump_reloc_info = TRUE;
2750 dump_file_header = TRUE;
2751 dump_ar_hdrs = TRUE;
2752 dump_section_headers = TRUE;
2753 seenflag = TRUE;
2754 break;
2755 case 't':
2756 dump_symtab = TRUE;
2757 seenflag = TRUE;
2758 break;
2759 case 'T':
2760 dump_dynamic_symtab = TRUE;
2761 seenflag = TRUE;
2762 break;
2763 case 'd':
2764 disassemble = TRUE;
2765 seenflag = TRUE;
2766 break;
2767 case 'z':
2768 disassemble_zeroes = TRUE;
2769 break;
2770 case 'D':
2771 disassemble = TRUE;
2772 disassemble_all = TRUE;
2773 seenflag = TRUE;
2774 break;
2775 case 'S':
2776 disassemble = TRUE;
2777 with_source_code = TRUE;
2778 seenflag = TRUE;
2779 break;
2780 case 'g':
2781 dump_debugging = 1;
2782 seenflag = TRUE;
2783 break;
2784 case 'G':
2785 dump_stab_section_info = TRUE;
2786 seenflag = TRUE;
2787 break;
2788 case 's':
2789 dump_section_contents = TRUE;
2790 seenflag = TRUE;
2791 break;
2792 case 'r':
2793 dump_reloc_info = TRUE;
2794 seenflag = TRUE;
2795 break;
2796 case 'R':
2797 dump_dynamic_reloc_info = TRUE;
2798 seenflag = TRUE;
2799 break;
2800 case 'a':
2801 dump_ar_hdrs = TRUE;
2802 seenflag = TRUE;
2803 break;
2804 case 'h':
2805 dump_section_headers = TRUE;
2806 seenflag = TRUE;
2807 break;
2808 case 'H':
2809 usage (stdout, 0);
2810 seenflag = TRUE;
2811 case 'v':
2812 case 'V':
2813 show_version = TRUE;
2814 seenflag = TRUE;
2815 break;
2817 default:
2818 usage (stderr, 1);
2822 if (show_version)
2823 print_version ("objdump");
2825 if (!seenflag)
2826 usage (stderr, 2);
2828 if (formats_info)
2829 exit_status = display_info ();
2830 else
2832 if (optind == argc)
2833 display_file ("a.out", target);
2834 else
2835 for (; optind < argc;)
2836 display_file (argv[optind++], target);
2839 END_PROGRESS (program_name);
2841 return exit_status;