Remove "member" from verbose output of "-dv" in order to conform to POSIX
[binutils.git] / binutils / objdump.c
blob8c16aa2a6e915b2157a842020d86061fa976d127
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 /* Objdump overview.
24 Objdump displays information about one or more object files, either on
25 their own, or inside libraries. It is commonly used as a disassembler,
26 but it can also display information about file headers, symbol tables,
27 relocations, debugging directives and more.
29 The flow of execution is as follows:
31 1. Command line arguments are checked for control switches and the
32 information to be displayed is selected.
34 2. Any remaining arguments are assumed to be object files, and they are
35 processed in order by display_bfd(). If the file is an archive each
36 of its elements is processed in turn.
38 3. The file's target architecture and binary file format are determined
39 by bfd_check_format(). If they are recognised, then dump_bfd() is
40 called.
42 4. dump_bfd() in turn calls separate functions to display the requested
43 item(s) of information(s). For example disassemble_data() is called if
44 a disassmebly has been requested.
46 When disassembling the code loops through blocks of instructions bounded
47 by symbols, calling disassemble_bytes() on each block. The actual
48 disassembling is done by the libopcodes library, via a function pointer
49 supplied by the disassembler() function. */
51 #include "bfd.h"
52 #include "bfdver.h"
53 #include "progress.h"
54 #include "bucomm.h"
55 #include "budemang.h"
56 #include "getopt.h"
57 #include "safe-ctype.h"
58 #include "dis-asm.h"
59 #include "libiberty.h"
60 #include "demangle.h"
61 #include "debug.h"
62 #include "budbg.h"
64 /* Internal headers for the ELF .stab-dump code - sorry. */
65 #define BYTES_IN_WORD 32
66 #include "aout/aout64.h"
68 #ifdef NEED_DECLARATION_FPRINTF
69 /* This is needed by init_disassemble_info(). */
70 extern int fprintf (FILE *, const char *, ...);
71 #endif
73 /* Exit status. */
74 static int exit_status = 0;
76 static char *default_target = NULL; /* Default at runtime. */
78 /* The following variables are set based on arguments passed on command line. */
79 static int show_version = 0; /* Show the version number. */
80 static int dump_section_contents; /* -s */
81 static int dump_section_headers; /* -h */
82 static bfd_boolean dump_file_header; /* -f */
83 static int dump_symtab; /* -t */
84 static int dump_dynamic_symtab; /* -T */
85 static int dump_reloc_info; /* -r */
86 static int dump_dynamic_reloc_info; /* -R */
87 static int dump_ar_hdrs; /* -a */
88 static int dump_private_headers; /* -p */
89 static int prefix_addresses; /* --prefix-addresses */
90 static int with_line_numbers; /* -l */
91 static bfd_boolean with_source_code; /* -S */
92 static int show_raw_insn; /* --show-raw-insn */
93 static int dump_stab_section_info; /* --stabs */
94 static int do_demangle; /* -C, --demangle */
95 static bfd_boolean disassemble; /* -d */
96 static bfd_boolean disassemble_all; /* -D */
97 static int disassemble_zeroes; /* --disassemble-zeroes */
98 static bfd_boolean formats_info; /* -i */
99 static int wide_output; /* -w */
100 static bfd_vma start_address = (bfd_vma) -1; /* --start-address */
101 static bfd_vma stop_address = (bfd_vma) -1; /* --stop-address */
102 static int dump_debugging; /* --debugging */
103 static int dump_debugging_tags; /* --debugging-tags */
104 static bfd_vma adjust_section_vma = 0; /* --adjust-vma */
105 static int file_start_context = 0; /* --file-start-context */
107 /* Pointer to an array of section names provided by
108 one or more "-j secname" command line options. */
109 static char **only;
110 /* The total number of slots in the only[] array. */
111 static size_t only_size = 0;
112 /* The number of occupied slots in the only[] array. */
113 static size_t only_used = 0;
115 /* Variables for handling include file path table. */
116 static const char **include_paths;
117 static int include_path_count;
119 /* Extra info to pass to the section disassembler and address printing function. */
120 struct objdump_disasm_info
122 bfd * abfd;
123 asection * sec;
124 bfd_boolean require_sec;
125 arelent ** dynrelbuf;
126 long dynrelcount;
127 disassembler_ftype disassemble_fn;
130 /* Architecture to disassemble for, or default if NULL. */
131 static char *machine = NULL;
133 /* Target specific options to the disassembler. */
134 static char *disassembler_options = NULL;
136 /* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */
137 static enum bfd_endian endian = BFD_ENDIAN_UNKNOWN;
139 /* The symbol table. */
140 static asymbol **syms;
142 /* Number of symbols in `syms'. */
143 static long symcount = 0;
145 /* The sorted symbol table. */
146 static asymbol **sorted_syms;
148 /* Number of symbols in `sorted_syms'. */
149 static long sorted_symcount = 0;
151 /* The dynamic symbol table. */
152 static asymbol **dynsyms;
154 /* Number of symbols in `dynsyms'. */
155 static long dynsymcount = 0;
157 static bfd_byte *stabs;
158 static bfd_size_type stab_size;
160 static char *strtab;
161 static bfd_size_type stabstr_size;
163 static void
164 usage (FILE *stream, int status)
166 fprintf (stream, _("Usage: %s <option(s)> <file(s)>\n"), program_name);
167 fprintf (stream, _(" Display information from object <file(s)>.\n"));
168 fprintf (stream, _(" At least one of the following switches must be given:\n"));
169 fprintf (stream, _("\
170 -a, --archive-headers Display archive header information\n\
171 -f, --file-headers Display the contents of the overall file header\n\
172 -p, --private-headers Display object format specific file header contents\n\
173 -h, --[section-]headers Display the contents of the section headers\n\
174 -x, --all-headers Display the contents of all headers\n\
175 -d, --disassemble Display assembler contents of executable sections\n\
176 -D, --disassemble-all Display assembler contents of all sections\n\
177 -S, --source Intermix source code with disassembly\n\
178 -s, --full-contents Display the full contents of all sections requested\n\
179 -g, --debugging Display debug information in object file\n\
180 -e, --debugging-tags Display debug information using ctags style\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 -I, --include=DIR Add DIR to search list for source files\n\
202 -l, --line-numbers Include line numbers and filenames in output\n\
203 -C, --demangle[=STYLE] Decode mangled/processed symbol names\n\
204 The STYLE, if specified, can be `auto', `gnu',\n\
205 `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
206 or `gnat'\n\
207 -w, --wide Format output for more than 80 columns\n\
208 -z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling\n\
209 --start-address=ADDR Only process data whose address is >= ADDR\n\
210 --stop-address=ADDR Only process data whose address is <= ADDR\n\
211 --prefix-addresses Print complete address alongside disassembly\n\
212 --[no-]show-raw-insn Display hex alongside symbolic disassembly\n\
213 --adjust-vma=OFFSET Add OFFSET to all displayed section addresses\n\
214 \n"));
215 list_supported_targets (program_name, stream);
216 list_supported_architectures (program_name, stream);
218 disassembler_usage (stream);
220 if (status == 0)
221 fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
222 exit (status);
225 /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
226 enum option_values
228 OPTION_ENDIAN=150,
229 OPTION_START_ADDRESS,
230 OPTION_STOP_ADDRESS,
231 OPTION_ADJUST_VMA
234 static struct option long_options[]=
236 {"adjust-vma", required_argument, NULL, OPTION_ADJUST_VMA},
237 {"all-headers", no_argument, NULL, 'x'},
238 {"private-headers", no_argument, NULL, 'p'},
239 {"architecture", required_argument, NULL, 'm'},
240 {"archive-headers", no_argument, NULL, 'a'},
241 {"debugging", no_argument, NULL, 'g'},
242 {"debugging-tags", no_argument, NULL, 'e'},
243 {"demangle", optional_argument, NULL, 'C'},
244 {"disassemble", no_argument, NULL, 'd'},
245 {"disassemble-all", no_argument, NULL, 'D'},
246 {"disassembler-options", required_argument, NULL, 'M'},
247 {"disassemble-zeroes", no_argument, NULL, 'z'},
248 {"dynamic-reloc", no_argument, NULL, 'R'},
249 {"dynamic-syms", no_argument, NULL, 'T'},
250 {"endian", required_argument, NULL, OPTION_ENDIAN},
251 {"file-headers", no_argument, NULL, 'f'},
252 {"file-start-context", no_argument, &file_start_context, 1},
253 {"full-contents", no_argument, NULL, 's'},
254 {"headers", no_argument, NULL, 'h'},
255 {"help", no_argument, NULL, 'H'},
256 {"info", no_argument, NULL, 'i'},
257 {"line-numbers", no_argument, NULL, 'l'},
258 {"no-show-raw-insn", no_argument, &show_raw_insn, -1},
259 {"prefix-addresses", no_argument, &prefix_addresses, 1},
260 {"reloc", no_argument, NULL, 'r'},
261 {"section", required_argument, NULL, 'j'},
262 {"section-headers", no_argument, NULL, 'h'},
263 {"show-raw-insn", no_argument, &show_raw_insn, 1},
264 {"source", no_argument, NULL, 'S'},
265 {"include", required_argument, NULL, 'I'},
266 {"stabs", no_argument, NULL, 'G'},
267 {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
268 {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
269 {"syms", no_argument, NULL, 't'},
270 {"target", required_argument, NULL, 'b'},
271 {"version", no_argument, NULL, 'V'},
272 {"wide", no_argument, NULL, 'w'},
273 {0, no_argument, 0, 0}
276 static void
277 nonfatal (const char *msg)
279 bfd_nonfatal (msg);
280 exit_status = 1;
283 static void
284 dump_section_header (bfd *abfd ATTRIBUTE_UNUSED, asection *section,
285 void *ignored ATTRIBUTE_UNUSED)
287 char *comma = "";
288 unsigned int opb = bfd_octets_per_byte (abfd);
290 printf ("%3d %-13s %08lx ", section->index,
291 bfd_get_section_name (abfd, section),
292 (unsigned long) bfd_section_size (abfd, section) / opb);
293 bfd_printf_vma (abfd, bfd_get_section_vma (abfd, section));
294 printf (" ");
295 bfd_printf_vma (abfd, section->lma);
296 printf (" %08lx 2**%u", (unsigned long) section->filepos,
297 bfd_get_section_alignment (abfd, section));
298 if (! wide_output)
299 printf ("\n ");
300 printf (" ");
302 #define PF(x, y) \
303 if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
305 PF (SEC_HAS_CONTENTS, "CONTENTS");
306 PF (SEC_ALLOC, "ALLOC");
307 PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
308 PF (SEC_LOAD, "LOAD");
309 PF (SEC_RELOC, "RELOC");
310 PF (SEC_READONLY, "READONLY");
311 PF (SEC_CODE, "CODE");
312 PF (SEC_DATA, "DATA");
313 PF (SEC_ROM, "ROM");
314 PF (SEC_DEBUGGING, "DEBUGGING");
315 PF (SEC_NEVER_LOAD, "NEVER_LOAD");
316 PF (SEC_EXCLUDE, "EXCLUDE");
317 PF (SEC_SORT_ENTRIES, "SORT_ENTRIES");
318 PF (SEC_BLOCK, "BLOCK");
319 PF (SEC_CLINK, "CLINK");
320 PF (SEC_SMALL_DATA, "SMALL_DATA");
321 PF (SEC_SHARED, "SHARED");
322 PF (SEC_ARCH_BIT_0, "ARCH_BIT_0");
323 PF (SEC_THREAD_LOCAL, "THREAD_LOCAL");
325 if ((section->flags & SEC_LINK_ONCE) != 0)
327 const char *ls;
329 switch (section->flags & SEC_LINK_DUPLICATES)
331 default:
332 abort ();
333 case SEC_LINK_DUPLICATES_DISCARD:
334 ls = "LINK_ONCE_DISCARD";
335 break;
336 case SEC_LINK_DUPLICATES_ONE_ONLY:
337 ls = "LINK_ONCE_ONE_ONLY";
338 break;
339 case SEC_LINK_DUPLICATES_SAME_SIZE:
340 ls = "LINK_ONCE_SAME_SIZE";
341 break;
342 case SEC_LINK_DUPLICATES_SAME_CONTENTS:
343 ls = "LINK_ONCE_SAME_CONTENTS";
344 break;
346 printf ("%s%s", comma, ls);
348 if (section->comdat != NULL)
349 printf (" (COMDAT %s %ld)", section->comdat->name,
350 section->comdat->symbol);
352 comma = ", ";
355 printf ("\n");
356 #undef PF
359 static void
360 dump_headers (bfd *abfd)
362 printf (_("Sections:\n"));
364 #ifndef BFD64
365 printf (_("Idx Name Size VMA LMA File off Algn"));
366 #else
367 /* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */
368 if (bfd_get_arch_size (abfd) == 32)
369 printf (_("Idx Name Size VMA LMA File off Algn"));
370 else
371 printf (_("Idx Name Size VMA LMA File off Algn"));
372 #endif
374 if (wide_output)
375 printf (_(" Flags"));
376 if (abfd->flags & HAS_LOAD_PAGE)
377 printf (_(" Pg"));
378 printf ("\n");
380 bfd_map_over_sections (abfd, dump_section_header, NULL);
383 static asymbol **
384 slurp_symtab (bfd *abfd)
386 asymbol **sy = NULL;
387 long storage;
389 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
391 symcount = 0;
392 return NULL;
395 storage = bfd_get_symtab_upper_bound (abfd);
396 if (storage < 0)
397 bfd_fatal (bfd_get_filename (abfd));
398 if (storage)
399 sy = xmalloc (storage);
401 symcount = bfd_canonicalize_symtab (abfd, sy);
402 if (symcount < 0)
403 bfd_fatal (bfd_get_filename (abfd));
404 return sy;
407 /* Read in the dynamic symbols. */
409 static asymbol **
410 slurp_dynamic_symtab (bfd *abfd)
412 asymbol **sy = 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 = 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 (asymbol **symbols, long count)
443 asymbol **in_ptr = symbols, **out_ptr = symbols;
445 while (--count >= 0)
447 asymbol *sym = *in_ptr++;
449 if (sym->name == NULL || sym->name[0] == '\0')
450 continue;
451 if (sym->flags & (BSF_DEBUGGING))
452 continue;
453 if (bfd_is_und_section (sym->section)
454 || bfd_is_com_section (sym->section))
455 continue;
457 *out_ptr++ = sym;
459 return out_ptr - symbols;
462 /* Sort symbols into value order. */
464 static int
465 compare_symbols (const void *ap, const void *bp)
467 const asymbol *a = * (const asymbol **) ap;
468 const asymbol *b = * (const asymbol **) bp;
469 const char *an;
470 const char *bn;
471 size_t anl;
472 size_t bnl;
473 bfd_boolean af;
474 bfd_boolean bf;
475 flagword aflags;
476 flagword 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. */
495 af = (strstr (an, "gnu_compiled") != NULL
496 || strstr (an, "gcc2_compiled") != NULL);
497 bf = (strstr (bn, "gnu_compiled") != NULL
498 || strstr (bn, "gcc2_compiled") != NULL);
500 if (af && ! bf)
501 return 1;
502 if (! af && bf)
503 return -1;
505 /* We use a heuristic for the file name, to try to sort it after
506 more useful symbols. It may not work on non Unix systems, but it
507 doesn't really matter; the only difference is precisely which
508 symbol names get printed. */
510 #define file_symbol(s, sn, snl) \
511 (((s)->flags & BSF_FILE) != 0 \
512 || ((sn)[(snl) - 2] == '.' \
513 && ((sn)[(snl) - 1] == 'o' \
514 || (sn)[(snl) - 1] == 'a')))
516 af = file_symbol (a, an, anl);
517 bf = file_symbol (b, bn, bnl);
519 if (af && ! bf)
520 return 1;
521 if (! af && bf)
522 return -1;
524 /* Try to sort global symbols before local symbols before function
525 symbols before debugging symbols. */
527 aflags = a->flags;
528 bflags = b->flags;
530 if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING))
532 if ((aflags & BSF_DEBUGGING) != 0)
533 return 1;
534 else
535 return -1;
537 if ((aflags & BSF_FUNCTION) != (bflags & BSF_FUNCTION))
539 if ((aflags & BSF_FUNCTION) != 0)
540 return -1;
541 else
542 return 1;
544 if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL))
546 if ((aflags & BSF_LOCAL) != 0)
547 return 1;
548 else
549 return -1;
551 if ((aflags & BSF_GLOBAL) != (bflags & BSF_GLOBAL))
553 if ((aflags & BSF_GLOBAL) != 0)
554 return -1;
555 else
556 return 1;
559 /* Symbols that start with '.' might be section names, so sort them
560 after symbols that don't start with '.'. */
561 if (an[0] == '.' && bn[0] != '.')
562 return 1;
563 if (an[0] != '.' && bn[0] == '.')
564 return -1;
566 /* Finally, if we can't distinguish them in any other way, try to
567 get consistent results by sorting the symbols by name. */
568 return strcmp (an, bn);
571 /* Sort relocs into address order. */
573 static int
574 compare_relocs (const void *ap, const void *bp)
576 const arelent *a = * (const arelent **) ap;
577 const arelent *b = * (const arelent **) bp;
579 if (a->address > b->address)
580 return 1;
581 else if (a->address < b->address)
582 return -1;
584 /* So that associated relocations tied to the same address show up
585 in the correct order, we don't do any further sorting. */
586 if (a > b)
587 return 1;
588 else if (a < b)
589 return -1;
590 else
591 return 0;
594 /* Print an address (VMA) to the output stream in INFO.
595 If SKIP_ZEROES is TRUE, omit leading zeroes. */
597 static void
598 objdump_print_value (bfd_vma vma, struct disassemble_info *info,
599 bfd_boolean skip_zeroes)
601 char buf[30];
602 char *p;
603 struct objdump_disasm_info *aux
604 = (struct objdump_disasm_info *) info->application_data;
606 bfd_sprintf_vma (aux->abfd, buf, vma);
607 if (! skip_zeroes)
608 p = buf;
609 else
611 for (p = buf; *p == '0'; ++p)
613 if (*p == '\0')
614 --p;
616 (*info->fprintf_func) (info->stream, "%s", p);
619 /* Print the name of a symbol. */
621 static void
622 objdump_print_symname (bfd *abfd, struct disassemble_info *info,
623 asymbol *sym)
625 char *alloc;
626 const char *name;
628 alloc = NULL;
629 name = bfd_asymbol_name (sym);
630 if (do_demangle && name[0] != '\0')
632 /* Demangle the name. */
633 alloc = demangle (abfd, name);
634 name = alloc;
637 if (info != NULL)
638 (*info->fprintf_func) (info->stream, "%s", name);
639 else
640 printf ("%s", name);
642 if (alloc != NULL)
643 free (alloc);
646 /* Locate a symbol given a bfd and a section (from INFO->application_data),
647 and a VMA. If INFO->application_data->require_sec is TRUE, then always
648 require the symbol to be in the section. Returns NULL if there is no
649 suitable symbol. If PLACE is not NULL, then *PLACE is set to the index
650 of the symbol in sorted_syms. */
652 static asymbol *
653 find_symbol_for_address (bfd_vma vma, struct disassemble_info *info, long *place)
655 /* @@ Would it speed things up to cache the last two symbols returned,
656 and maybe their address ranges? For many processors, only one memory
657 operand can be present at a time, so the 2-entry cache wouldn't be
658 constantly churned by code doing heavy memory accesses. */
660 /* Indices in `sorted_syms'. */
661 long min = 0;
662 long max = sorted_symcount;
663 long thisplace;
664 struct objdump_disasm_info * aux = (struct objdump_disasm_info *) info->application_data;
665 bfd * abfd = aux->abfd;
666 asection * sec = aux->sec;
667 unsigned int opb = bfd_octets_per_byte (abfd);
669 if (sorted_symcount < 1)
670 return NULL;
672 /* Perform a binary search looking for the closest symbol to the
673 required value. We are searching the range (min, max]. */
674 while (min + 1 < max)
676 asymbol *sym;
678 thisplace = (max + min) / 2;
679 sym = sorted_syms[thisplace];
681 if (bfd_asymbol_value (sym) > vma)
682 max = thisplace;
683 else if (bfd_asymbol_value (sym) < vma)
684 min = thisplace;
685 else
687 min = thisplace;
688 break;
692 /* The symbol we want is now in min, the low end of the range we
693 were searching. If there are several symbols with the same
694 value, we want the first one. */
695 thisplace = min;
696 while (thisplace > 0
697 && (bfd_asymbol_value (sorted_syms[thisplace])
698 == bfd_asymbol_value (sorted_syms[thisplace - 1])))
699 --thisplace;
701 /* If the file is relocatable, and the symbol could be from this
702 section, prefer a symbol from this section over symbols from
703 others, even if the other symbol's value might be closer.
705 Note that this may be wrong for some symbol references if the
706 sections have overlapping memory ranges, but in that case there's
707 no way to tell what's desired without looking at the relocation
708 table. */
709 if (sorted_syms[thisplace]->section != sec
710 && (aux->require_sec
711 || ((abfd->flags & HAS_RELOC) != 0
712 && vma >= bfd_get_section_vma (abfd, sec)
713 && vma < (bfd_get_section_vma (abfd, sec)
714 + bfd_section_size (abfd, sec) / opb))))
716 long i;
718 for (i = thisplace + 1; i < sorted_symcount; i++)
720 if (bfd_asymbol_value (sorted_syms[i])
721 != bfd_asymbol_value (sorted_syms[thisplace]))
722 break;
725 --i;
727 for (; i >= 0; i--)
729 if (sorted_syms[i]->section == sec
730 && (i == 0
731 || sorted_syms[i - 1]->section != sec
732 || (bfd_asymbol_value (sorted_syms[i])
733 != bfd_asymbol_value (sorted_syms[i - 1]))))
735 thisplace = i;
736 break;
740 if (sorted_syms[thisplace]->section != sec)
742 /* We didn't find a good symbol with a smaller value.
743 Look for one with a larger value. */
744 for (i = thisplace + 1; i < sorted_symcount; i++)
746 if (sorted_syms[i]->section == sec)
748 thisplace = i;
749 break;
754 if (sorted_syms[thisplace]->section != sec
755 && (aux->require_sec
756 || ((abfd->flags & HAS_RELOC) != 0
757 && vma >= bfd_get_section_vma (abfd, sec)
758 && vma < (bfd_get_section_vma (abfd, sec)
759 + bfd_section_size (abfd, sec)))))
760 /* There is no suitable symbol. */
761 return NULL;
764 /* Give the target a chance to reject the symbol. */
765 while (! info->symbol_is_valid (sorted_syms [thisplace], info))
767 ++ thisplace;
768 if (thisplace >= sorted_symcount
769 || bfd_asymbol_value (sorted_syms [thisplace]) > vma)
770 return NULL;
773 if (place != NULL)
774 *place = thisplace;
776 return sorted_syms[thisplace];
779 /* Print an address and the offset to the nearest symbol. */
781 static void
782 objdump_print_addr_with_sym (bfd *abfd, asection *sec, asymbol *sym,
783 bfd_vma vma, struct disassemble_info *info,
784 bfd_boolean skip_zeroes)
786 objdump_print_value (vma, info, skip_zeroes);
788 if (sym == NULL)
790 bfd_vma secaddr;
792 (*info->fprintf_func) (info->stream, " <%s",
793 bfd_get_section_name (abfd, sec));
794 secaddr = bfd_get_section_vma (abfd, sec);
795 if (vma < secaddr)
797 (*info->fprintf_func) (info->stream, "-0x");
798 objdump_print_value (secaddr - vma, info, TRUE);
800 else if (vma > secaddr)
802 (*info->fprintf_func) (info->stream, "+0x");
803 objdump_print_value (vma - secaddr, info, TRUE);
805 (*info->fprintf_func) (info->stream, ">");
807 else
809 (*info->fprintf_func) (info->stream, " <");
810 objdump_print_symname (abfd, info, sym);
811 if (bfd_asymbol_value (sym) > vma)
813 (*info->fprintf_func) (info->stream, "-0x");
814 objdump_print_value (bfd_asymbol_value (sym) - vma, info, TRUE);
816 else if (vma > bfd_asymbol_value (sym))
818 (*info->fprintf_func) (info->stream, "+0x");
819 objdump_print_value (vma - bfd_asymbol_value (sym), info, TRUE);
821 (*info->fprintf_func) (info->stream, ">");
825 /* Print an address (VMA), symbolically if possible.
826 If SKIP_ZEROES is TRUE, don't output leading zeroes. */
828 static void
829 objdump_print_addr (bfd_vma vma, struct disassemble_info *info,
830 bfd_boolean skip_zeroes)
832 struct objdump_disasm_info * aux = (struct objdump_disasm_info *) info->application_data;
833 asymbol *sym;
835 if (sorted_symcount < 1)
837 (*info->fprintf_func) (info->stream, "0x");
838 objdump_print_value (vma, info, skip_zeroes);
839 return;
842 sym = find_symbol_for_address (vma, info, NULL);
843 objdump_print_addr_with_sym (aux->abfd, aux->sec, sym, vma, info,
844 skip_zeroes);
847 /* Print VMA to INFO. This function is passed to the disassembler
848 routine. */
850 static void
851 objdump_print_address (bfd_vma vma, struct disassemble_info *info)
853 objdump_print_addr (vma, info, ! prefix_addresses);
856 /* Determine of the given address has a symbol associated with it. */
858 static int
859 objdump_symbol_at_address (bfd_vma vma, struct disassemble_info * info)
861 asymbol * sym;
863 sym = find_symbol_for_address (vma, info, NULL);
865 return (sym != NULL && (bfd_asymbol_value (sym) == vma));
868 /* Hold the last function name and the last line number we displayed
869 in a disassembly. */
871 static char *prev_functionname;
872 static unsigned int prev_line;
874 /* We keep a list of all files that we have seen when doing a
875 disassembly with source, so that we know how much of the file to
876 display. This can be important for inlined functions. */
878 struct print_file_list
880 struct print_file_list *next;
881 const char *filename;
882 const char *modname;
883 unsigned int line;
884 FILE *f;
887 static struct print_file_list *print_files;
889 /* The number of preceding context lines to show when we start
890 displaying a file for the first time. */
892 #define SHOW_PRECEDING_CONTEXT_LINES (5)
894 /* Tries to open MODNAME, and if successful adds a node to print_files
895 linked list and returns that node. Returns NULL on failure. */
897 static struct print_file_list *
898 try_print_file_open (const char *origname, const char *modname)
900 struct print_file_list *p;
901 FILE *f;
903 f = fopen (modname, "r");
904 if (f == NULL)
905 return NULL;
907 if (print_files != NULL && print_files->f != NULL)
909 fclose (print_files->f);
910 print_files->f = NULL;
913 p = xmalloc (sizeof (struct print_file_list));
914 p->filename = origname;
915 p->modname = modname;
916 p->line = 0;
917 p->f = f;
918 p->next = print_files;
919 print_files = p;
920 return p;
923 /* If the the source file, as described in the symtab, is not found
924 try to locate it in one of the paths specified with -I
925 If found, add location to print_files linked list. */
927 static struct print_file_list *
928 update_source_path (const char *filename)
930 struct print_file_list *p;
931 const char *fname;
932 int i;
934 if (filename == NULL)
935 return NULL;
937 p = try_print_file_open (filename, filename);
938 if (p != NULL)
939 return p;
941 if (include_path_count == 0)
942 return NULL;
944 /* Get the name of the file. */
945 fname = strrchr (filename, '/');
946 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
948 /* We could have a mixed forward/back slash case. */
949 char *backslash = strrchr (filename, '\\');
950 if (fname == NULL || (backslash != NULL && backslash > fname))
951 fname = backslash;
952 if (fname == NULL && filename[0] != '\0' && filename[1] == ':')
953 fname = filename + 1;
955 #endif
956 if (fname == NULL)
957 fname = filename;
958 else
959 ++fname;
961 /* If file exists under a new path, we need to add it to the list
962 so that show_line knows about it. */
963 for (i = 0; i < include_path_count; i++)
965 char *modname = concat (include_paths[i], "/", fname, (const char *) 0);
967 p = try_print_file_open (filename, modname);
968 if (p)
969 return p;
971 free (modname);
974 return NULL;
977 /* Skip ahead to a given line in a file, optionally printing each
978 line. */
980 static void
981 skip_to_line (struct print_file_list *p, unsigned int line,
982 bfd_boolean show)
984 while (p->line < line)
986 char buf[100];
988 if (fgets (buf, sizeof buf, p->f) == NULL)
990 fclose (p->f);
991 p->f = NULL;
992 break;
995 if (show)
996 printf ("%s", buf);
998 if (strchr (buf, '\n') != NULL)
999 ++p->line;
1003 /* Show the line number, or the source line, in a disassembly
1004 listing. */
1006 static void
1007 show_line (bfd *abfd, asection *section, bfd_vma addr_offset)
1009 const char *filename;
1010 const char *functionname;
1011 unsigned int line;
1013 if (! with_line_numbers && ! with_source_code)
1014 return;
1016 if (! bfd_find_nearest_line (abfd, section, syms, addr_offset, &filename,
1017 &functionname, &line))
1018 return;
1020 if (filename != NULL && *filename == '\0')
1021 filename = NULL;
1022 if (functionname != NULL && *functionname == '\0')
1023 functionname = NULL;
1025 if (with_line_numbers)
1027 if (functionname != NULL
1028 && (prev_functionname == NULL
1029 || strcmp (functionname, prev_functionname) != 0))
1030 printf ("%s():\n", functionname);
1031 if (line > 0 && line != prev_line)
1032 printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
1035 if (with_source_code
1036 && filename != NULL
1037 && line > 0)
1039 struct print_file_list **pp, *p;
1041 for (pp = &print_files; *pp != NULL; pp = &(*pp)->next)
1042 if (strcmp ((*pp)->filename, filename) == 0)
1043 break;
1044 p = *pp;
1046 if (p != NULL)
1048 if (p != print_files)
1050 int l;
1052 /* We have reencountered a file name which we saw
1053 earlier. This implies that either we are dumping out
1054 code from an included file, or the same file was
1055 linked in more than once. There are two common cases
1056 of an included file: inline functions in a header
1057 file, and a bison or flex skeleton file. In the
1058 former case we want to just start printing (but we
1059 back up a few lines to give context); in the latter
1060 case we want to continue from where we left off. I
1061 can't think of a good way to distinguish the cases,
1062 so I used a heuristic based on the file name. */
1063 if (strcmp (p->filename + strlen (p->filename) - 2, ".h") != 0)
1064 l = p->line;
1065 else
1067 l = line - SHOW_PRECEDING_CONTEXT_LINES;
1068 if (l < 0)
1069 l = 0;
1072 if (p->f == NULL)
1074 p->f = fopen (p->modname, "r");
1075 p->line = 0;
1077 if (p->f != NULL)
1078 skip_to_line (p, l, FALSE);
1080 if (print_files->f != NULL)
1082 fclose (print_files->f);
1083 print_files->f = NULL;
1087 if (p->f != NULL)
1089 skip_to_line (p, line, TRUE);
1090 *pp = p->next;
1091 p->next = print_files;
1092 print_files = p;
1095 else
1097 p = update_source_path (filename);
1099 if (p != NULL)
1101 int l;
1103 if (file_start_context)
1104 l = 0;
1105 else
1106 l = line - SHOW_PRECEDING_CONTEXT_LINES;
1107 if (l < 0)
1108 l = 0;
1109 skip_to_line (p, l, FALSE);
1110 if (p->f != NULL)
1111 skip_to_line (p, line, TRUE);
1116 if (functionname != NULL
1117 && (prev_functionname == NULL
1118 || strcmp (functionname, prev_functionname) != 0))
1120 if (prev_functionname != NULL)
1121 free (prev_functionname);
1122 prev_functionname = xmalloc (strlen (functionname) + 1);
1123 strcpy (prev_functionname, functionname);
1126 if (line > 0 && line != prev_line)
1127 prev_line = line;
1130 /* Pseudo FILE object for strings. */
1131 typedef struct
1133 char *buffer;
1134 size_t size;
1135 char *current;
1136 } SFILE;
1138 /* sprintf to a "stream". */
1140 static int
1141 objdump_sprintf (SFILE *f, const char *format, ...)
1143 char *buf;
1144 size_t n;
1145 va_list args;
1147 va_start (args, format);
1149 vasprintf (&buf, format, args);
1151 if (buf == NULL)
1153 va_end (args);
1154 fatal (_("Out of virtual memory"));
1157 n = strlen (buf);
1159 while ((size_t) ((f->buffer + f->size) - f->current) < n + 1)
1161 size_t curroff;
1163 curroff = f->current - f->buffer;
1164 f->size *= 2;
1165 f->buffer = xrealloc (f->buffer, f->size);
1166 f->current = f->buffer + curroff;
1169 memcpy (f->current, buf, n);
1170 f->current += n;
1171 f->current[0] = '\0';
1173 free (buf);
1175 va_end (args);
1176 return n;
1179 /* Returns TRUE if the specified section should be dumped. */
1181 static bfd_boolean
1182 process_section_p (asection * section)
1184 size_t i;
1186 if (only == NULL)
1187 return TRUE;
1189 for (i = 0; i < only_used; i++)
1190 if (strcmp (only [i], section->name) == 0)
1191 return TRUE;
1193 return FALSE;
1197 /* The number of zeroes we want to see before we start skipping them.
1198 The number is arbitrarily chosen. */
1200 #ifndef SKIP_ZEROES
1201 #define SKIP_ZEROES (8)
1202 #endif
1204 /* The number of zeroes to skip at the end of a section. If the
1205 number of zeroes at the end is between SKIP_ZEROES_AT_END and
1206 SKIP_ZEROES, they will be disassembled. If there are fewer than
1207 SKIP_ZEROES_AT_END, they will be skipped. This is a heuristic
1208 attempt to avoid disassembling zeroes inserted by section
1209 alignment. */
1211 #ifndef SKIP_ZEROES_AT_END
1212 #define SKIP_ZEROES_AT_END (3)
1213 #endif
1215 /* Disassemble some data in memory between given values. */
1217 static void
1218 disassemble_bytes (struct disassemble_info * info,
1219 disassembler_ftype disassemble_fn,
1220 bfd_boolean insns,
1221 bfd_byte * data,
1222 bfd_vma start_offset,
1223 bfd_vma stop_offset,
1224 bfd_vma rel_offset,
1225 arelent *** relppp,
1226 arelent ** relppend)
1228 struct objdump_disasm_info *aux;
1229 asection *section;
1230 int octets_per_line;
1231 bfd_boolean done_dot;
1232 int skip_addr_chars;
1233 bfd_vma addr_offset;
1234 int opb = info->octets_per_byte;
1236 aux = (struct objdump_disasm_info *) info->application_data;
1237 section = aux->sec;
1239 if (insns)
1240 octets_per_line = 4;
1241 else
1242 octets_per_line = 16;
1244 /* Figure out how many characters to skip at the start of an
1245 address, to make the disassembly look nicer. We discard leading
1246 zeroes in chunks of 4, ensuring that there is always a leading
1247 zero remaining. */
1248 skip_addr_chars = 0;
1249 if (! prefix_addresses)
1251 char buf[30];
1252 char *s;
1254 bfd_sprintf_vma
1255 (aux->abfd, buf,
1256 (section->vma
1257 + bfd_section_size (section->owner, section) / opb));
1258 s = buf;
1259 while (s[0] == '0' && s[1] == '0' && s[2] == '0' && s[3] == '0'
1260 && s[4] == '0')
1262 skip_addr_chars += 4;
1263 s += 4;
1267 info->insn_info_valid = 0;
1269 done_dot = FALSE;
1270 addr_offset = start_offset;
1271 while (addr_offset < stop_offset)
1273 bfd_vma z;
1274 int octets = 0;
1275 bfd_boolean need_nl = FALSE;
1277 /* If we see more than SKIP_ZEROES octets of zeroes, we just
1278 print `...'. */
1279 for (z = addr_offset * opb; z < stop_offset * opb; z++)
1280 if (data[z] != 0)
1281 break;
1282 if (! disassemble_zeroes
1283 && (info->insn_info_valid == 0
1284 || info->branch_delay_insns == 0)
1285 && (z - addr_offset * opb >= SKIP_ZEROES
1286 || (z == stop_offset * opb &&
1287 z - addr_offset * opb < SKIP_ZEROES_AT_END)))
1289 printf ("\t...\n");
1291 /* If there are more nonzero octets to follow, we only skip
1292 zeroes in multiples of 4, to try to avoid running over
1293 the start of an instruction which happens to start with
1294 zero. */
1295 if (z != stop_offset * opb)
1296 z = addr_offset * opb + ((z - addr_offset * opb) &~ 3);
1298 octets = z - addr_offset * opb;
1300 else
1302 char buf[50];
1303 SFILE sfile;
1304 int bpc = 0;
1305 int pb = 0;
1307 done_dot = FALSE;
1309 if (with_line_numbers || with_source_code)
1310 /* The line number tables will refer to unadjusted
1311 section VMAs, so we must undo any VMA modifications
1312 when calling show_line. */
1313 show_line (aux->abfd, section, addr_offset - adjust_section_vma);
1315 if (! prefix_addresses)
1317 char *s;
1319 bfd_sprintf_vma (aux->abfd, buf, section->vma + addr_offset);
1320 for (s = buf + skip_addr_chars; *s == '0'; s++)
1321 *s = ' ';
1322 if (*s == '\0')
1323 *--s = '0';
1324 printf ("%s:\t", buf + skip_addr_chars);
1326 else
1328 aux->require_sec = TRUE;
1329 objdump_print_address (section->vma + addr_offset, info);
1330 aux->require_sec = FALSE;
1331 putchar (' ');
1334 if (insns)
1336 sfile.size = 120;
1337 sfile.buffer = xmalloc (sfile.size);
1338 sfile.current = sfile.buffer;
1339 info->fprintf_func = (fprintf_ftype) objdump_sprintf;
1340 info->stream = (FILE *) &sfile;
1341 info->bytes_per_line = 0;
1342 info->bytes_per_chunk = 0;
1344 #ifdef DISASSEMBLER_NEEDS_RELOCS
1345 /* FIXME: This is wrong. It tests the number of octets
1346 in the last instruction, not the current one. */
1347 if (*relppp < relppend
1348 && (**relppp)->address >= rel_offset + addr_offset
1349 && ((**relppp)->address
1350 < rel_offset + addr_offset + octets / opb))
1351 info->flags = INSN_HAS_RELOC;
1352 else
1353 #endif
1354 info->flags = 0;
1356 octets = (*disassemble_fn) (section->vma + addr_offset, info);
1357 info->fprintf_func = (fprintf_ftype) fprintf;
1358 info->stream = stdout;
1359 if (info->bytes_per_line != 0)
1360 octets_per_line = info->bytes_per_line;
1361 if (octets < 0)
1363 if (sfile.current != sfile.buffer)
1364 printf ("%s\n", sfile.buffer);
1365 free (sfile.buffer);
1366 break;
1369 else
1371 bfd_vma j;
1373 octets = octets_per_line;
1374 if (addr_offset + octets / opb > stop_offset)
1375 octets = (stop_offset - addr_offset) * opb;
1377 for (j = addr_offset * opb; j < addr_offset * opb + octets; ++j)
1379 if (ISPRINT (data[j]))
1380 buf[j - addr_offset * opb] = data[j];
1381 else
1382 buf[j - addr_offset * opb] = '.';
1384 buf[j - addr_offset * opb] = '\0';
1387 if (prefix_addresses
1388 ? show_raw_insn > 0
1389 : show_raw_insn >= 0)
1391 bfd_vma j;
1393 /* If ! prefix_addresses and ! wide_output, we print
1394 octets_per_line octets per line. */
1395 pb = octets;
1396 if (pb > octets_per_line && ! prefix_addresses && ! wide_output)
1397 pb = octets_per_line;
1399 if (info->bytes_per_chunk)
1400 bpc = info->bytes_per_chunk;
1401 else
1402 bpc = 1;
1404 for (j = addr_offset * opb; j < addr_offset * opb + pb; j += bpc)
1406 int k;
1407 if (bpc > 1 && info->display_endian == BFD_ENDIAN_LITTLE)
1409 for (k = bpc - 1; k >= 0; k--)
1410 printf ("%02x", (unsigned) data[j + k]);
1411 putchar (' ');
1413 else
1415 for (k = 0; k < bpc; k++)
1416 printf ("%02x", (unsigned) data[j + k]);
1417 putchar (' ');
1421 for (; pb < octets_per_line; pb += bpc)
1423 int k;
1425 for (k = 0; k < bpc; k++)
1426 printf (" ");
1427 putchar (' ');
1430 /* Separate raw data from instruction by extra space. */
1431 if (insns)
1432 putchar ('\t');
1433 else
1434 printf (" ");
1437 if (! insns)
1438 printf ("%s", buf);
1439 else
1441 printf ("%s", sfile.buffer);
1442 free (sfile.buffer);
1445 if (prefix_addresses
1446 ? show_raw_insn > 0
1447 : show_raw_insn >= 0)
1449 while (pb < octets)
1451 bfd_vma j;
1452 char *s;
1454 putchar ('\n');
1455 j = addr_offset * opb + pb;
1457 bfd_sprintf_vma (aux->abfd, buf, section->vma + j / opb);
1458 for (s = buf + skip_addr_chars; *s == '0'; s++)
1459 *s = ' ';
1460 if (*s == '\0')
1461 *--s = '0';
1462 printf ("%s:\t", buf + skip_addr_chars);
1464 pb += octets_per_line;
1465 if (pb > octets)
1466 pb = octets;
1467 for (; j < addr_offset * opb + pb; j += bpc)
1469 int k;
1471 if (bpc > 1 && info->display_endian == BFD_ENDIAN_LITTLE)
1473 for (k = bpc - 1; k >= 0; k--)
1474 printf ("%02x", (unsigned) data[j + k]);
1475 putchar (' ');
1477 else
1479 for (k = 0; k < bpc; k++)
1480 printf ("%02x", (unsigned) data[j + k]);
1481 putchar (' ');
1487 if (!wide_output)
1488 putchar ('\n');
1489 else
1490 need_nl = TRUE;
1493 while ((*relppp) < relppend
1494 && (**relppp)->address < rel_offset + addr_offset + octets / opb)
1496 if (dump_reloc_info || dump_dynamic_reloc_info)
1498 arelent *q;
1500 q = **relppp;
1502 if (wide_output)
1503 putchar ('\t');
1504 else
1505 printf ("\t\t\t");
1507 objdump_print_value (section->vma + q->address, info, TRUE);
1509 printf (": %s\t", q->howto->name);
1511 if (q->sym_ptr_ptr == NULL || *q->sym_ptr_ptr == NULL)
1512 printf ("*unknown*");
1513 else
1515 const char *sym_name;
1517 sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
1518 if (sym_name != NULL && *sym_name != '\0')
1519 objdump_print_symname (aux->abfd, info, *q->sym_ptr_ptr);
1520 else
1522 asection *sym_sec;
1524 sym_sec = bfd_get_section (*q->sym_ptr_ptr);
1525 sym_name = bfd_get_section_name (aux->abfd, sym_sec);
1526 if (sym_name == NULL || *sym_name == '\0')
1527 sym_name = "*unknown*";
1528 printf ("%s", sym_name);
1532 if (q->addend)
1534 printf ("+0x");
1535 objdump_print_value (q->addend, info, TRUE);
1538 printf ("\n");
1539 need_nl = FALSE;
1541 ++(*relppp);
1544 if (need_nl)
1545 printf ("\n");
1547 addr_offset += octets / opb;
1551 static void
1552 disassemble_section (bfd *abfd, asection *section, void *info)
1554 struct disassemble_info * pinfo = (struct disassemble_info *) info;
1555 struct objdump_disasm_info * paux = (struct objdump_disasm_info *) pinfo->application_data;
1556 unsigned int opb = pinfo->octets_per_byte;
1557 bfd_byte * data = NULL;
1558 bfd_size_type datasize = 0;
1559 arelent ** rel_pp = NULL;
1560 arelent ** rel_ppstart = NULL;
1561 arelent ** rel_ppend;
1562 unsigned long stop_offset;
1563 asymbol * sym = NULL;
1564 long place = 0;
1565 long rel_count;
1566 bfd_vma rel_offset;
1567 unsigned long addr_offset;
1569 /* Sections that do not contain machine
1570 code are not normally disassembled. */
1571 if (! disassemble_all
1572 && only == NULL
1573 && (section->flags & SEC_CODE) == 0)
1574 return;
1576 if (! process_section_p (section))
1577 return;
1579 datasize = bfd_get_section_size_before_reloc (section);
1580 if (datasize == 0)
1581 return;
1583 /* Decide which set of relocs to use. Load them if necessary. */
1584 if (paux->dynrelbuf)
1586 rel_pp = paux->dynrelbuf;
1587 rel_count = paux->dynrelcount;
1588 /* Dynamic reloc addresses are absolute, non-dynamic are section
1589 relative. REL_OFFSET specifies the reloc address corresponding
1590 to the start of this section. */
1591 rel_offset = pinfo->buffer_vma;
1593 else
1595 rel_count = 0;
1596 rel_pp = NULL;
1597 rel_offset = 0;
1599 if ((section->flags & SEC_RELOC) != 0
1600 #ifndef DISASSEMBLER_NEEDS_RELOCS
1601 && dump_reloc_info
1602 #endif
1605 long relsize;
1607 relsize = bfd_get_reloc_upper_bound (abfd, section);
1608 if (relsize < 0)
1609 bfd_fatal (bfd_get_filename (abfd));
1611 if (relsize > 0)
1613 rel_ppstart = rel_pp = xmalloc (relsize);
1614 rel_count = bfd_canonicalize_reloc (abfd, section, rel_pp, syms);
1615 if (rel_count < 0)
1616 bfd_fatal (bfd_get_filename (abfd));
1618 /* Sort the relocs by address. */
1619 qsort (rel_pp, rel_count, sizeof (arelent *), compare_relocs);
1624 rel_ppend = rel_pp + rel_count;
1626 data = xmalloc (datasize);
1628 bfd_get_section_contents (abfd, section, data, 0, datasize);
1630 paux->sec = section;
1631 paux->require_sec = TRUE;
1632 pinfo->buffer = data;
1633 pinfo->buffer_vma = section->vma;
1634 pinfo->buffer_length = datasize;
1635 pinfo->section = section;
1637 if (start_address == (bfd_vma) -1
1638 || start_address < pinfo->buffer_vma)
1639 addr_offset = 0;
1640 else
1641 addr_offset = start_address - pinfo->buffer_vma;
1643 if (stop_address == (bfd_vma) -1)
1644 stop_offset = datasize / opb;
1645 else
1647 if (stop_address < pinfo->buffer_vma)
1648 stop_offset = 0;
1649 else
1650 stop_offset = stop_address - pinfo->buffer_vma;
1651 if (stop_offset > pinfo->buffer_length / opb)
1652 stop_offset = pinfo->buffer_length / opb;
1655 /* Skip over the relocs belonging to addresses below the
1656 start address. */
1657 while (rel_pp < rel_ppend
1658 && (*rel_pp)->address < rel_offset + addr_offset)
1659 ++rel_pp;
1661 printf (_("Disassembly of section %s:\n"), section->name);
1663 /* Find the nearest symbol forwards from our current position. */
1664 sym = find_symbol_for_address (section->vma + addr_offset, info, &place);
1666 /* Disassemble a block of instructions up to the address associated with
1667 the symbol we have just found. Then print the symbol and find the
1668 next symbol on. Repeat until we have disassembled the entire section
1669 or we have reached the end of the address range we are interested in. */
1670 while (addr_offset < stop_offset)
1672 bfd_vma addr;
1673 asymbol *nextsym;
1674 unsigned long nextstop_offset;
1675 bfd_boolean insns;
1677 addr = section->vma + addr_offset;
1679 if (sym != NULL && bfd_asymbol_value (sym) <= addr)
1681 int x;
1683 for (x = place;
1684 (x < sorted_symcount
1685 && (bfd_asymbol_value (sorted_syms[x]) <= addr));
1686 ++x)
1687 continue;
1689 pinfo->symbols = sorted_syms + place;
1690 pinfo->num_symbols = x - place;
1692 else
1694 pinfo->symbols = NULL;
1695 pinfo->num_symbols = 0;
1698 if (! prefix_addresses)
1700 pinfo->fprintf_func (pinfo->stream, "\n");
1701 objdump_print_addr_with_sym (abfd, section, sym, addr,
1702 pinfo, FALSE);
1703 pinfo->fprintf_func (pinfo->stream, ":\n");
1706 if (sym != NULL && bfd_asymbol_value (sym) > addr)
1707 nextsym = sym;
1708 else if (sym == NULL)
1709 nextsym = NULL;
1710 else
1712 #define is_valid_next_sym(SYM) \
1713 ((SYM)->section == section \
1714 && (bfd_asymbol_value (SYM) > bfd_asymbol_value (sym)) \
1715 && pinfo->symbol_is_valid (SYM, pinfo))
1717 /* Search forward for the next appropriate symbol in
1718 SECTION. Note that all the symbols are sorted
1719 together into one big array, and that some sections
1720 may have overlapping addresses. */
1721 while (place < sorted_symcount
1722 && ! is_valid_next_sym (sorted_syms [place]))
1723 ++place;
1725 if (place >= sorted_symcount)
1726 nextsym = NULL;
1727 else
1728 nextsym = sorted_syms[place];
1731 if (sym != NULL && bfd_asymbol_value (sym) > addr)
1732 nextstop_offset = bfd_asymbol_value (sym) - section->vma;
1733 else if (nextsym == NULL)
1734 nextstop_offset = stop_offset;
1735 else
1736 nextstop_offset = bfd_asymbol_value (nextsym) - section->vma;
1738 if (nextstop_offset > stop_offset)
1739 nextstop_offset = stop_offset;
1741 /* If a symbol is explicitly marked as being an object
1742 rather than a function, just dump the bytes without
1743 disassembling them. */
1744 if (disassemble_all
1745 || sym == NULL
1746 || bfd_asymbol_value (sym) > addr
1747 || ((sym->flags & BSF_OBJECT) == 0
1748 && (strstr (bfd_asymbol_name (sym), "gnu_compiled")
1749 == NULL)
1750 && (strstr (bfd_asymbol_name (sym), "gcc2_compiled")
1751 == NULL))
1752 || (sym->flags & BSF_FUNCTION) != 0)
1753 insns = TRUE;
1754 else
1755 insns = FALSE;
1757 disassemble_bytes (pinfo, paux->disassemble_fn, insns, data,
1758 addr_offset, nextstop_offset,
1759 rel_offset, &rel_pp, rel_ppend);
1761 addr_offset = nextstop_offset;
1762 sym = nextsym;
1765 free (data);
1767 if (rel_ppstart != NULL)
1768 free (rel_ppstart);
1771 /* Disassemble the contents of an object file. */
1773 static void
1774 disassemble_data (bfd *abfd)
1776 struct disassemble_info disasm_info;
1777 struct objdump_disasm_info aux;
1779 print_files = NULL;
1780 prev_functionname = NULL;
1781 prev_line = -1;
1783 /* We make a copy of syms to sort. We don't want to sort syms
1784 because that will screw up the relocs. */
1785 sorted_syms = xmalloc (symcount * sizeof (asymbol *));
1786 memcpy (sorted_syms, syms, symcount * sizeof (asymbol *));
1788 sorted_symcount = remove_useless_symbols (sorted_syms, symcount);
1790 /* Sort the symbols into section and symbol order. */
1791 qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols);
1793 init_disassemble_info (&disasm_info, stdout, (fprintf_ftype) fprintf);
1795 disasm_info.application_data = (void *) &aux;
1796 aux.abfd = abfd;
1797 aux.require_sec = FALSE;
1798 aux.dynrelbuf = NULL;
1799 aux.dynrelcount = 0;
1801 disasm_info.print_address_func = objdump_print_address;
1802 disasm_info.symbol_at_address_func = objdump_symbol_at_address;
1804 if (machine != NULL)
1806 const bfd_arch_info_type *info = bfd_scan_arch (machine);
1808 if (info == NULL)
1809 fatal (_("Can't use supplied machine %s"), machine);
1811 abfd->arch_info = info;
1814 if (endian != BFD_ENDIAN_UNKNOWN)
1816 struct bfd_target *xvec;
1818 xvec = xmalloc (sizeof (struct bfd_target));
1819 memcpy (xvec, abfd->xvec, sizeof (struct bfd_target));
1820 xvec->byteorder = endian;
1821 abfd->xvec = xvec;
1824 /* Use libopcodes to locate a suitable disassembler. */
1825 aux.disassemble_fn = disassembler (abfd);
1826 if (!aux.disassemble_fn)
1828 non_fatal (_("Can't disassemble for architecture %s\n"),
1829 bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
1830 exit_status = 1;
1831 return;
1834 disasm_info.flavour = bfd_get_flavour (abfd);
1835 disasm_info.arch = bfd_get_arch (abfd);
1836 disasm_info.mach = bfd_get_mach (abfd);
1837 disasm_info.disassembler_options = disassembler_options;
1838 disasm_info.octets_per_byte = bfd_octets_per_byte (abfd);
1840 if (bfd_big_endian (abfd))
1841 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_BIG;
1842 else if (bfd_little_endian (abfd))
1843 disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_LITTLE;
1844 else
1845 /* ??? Aborting here seems too drastic. We could default to big or little
1846 instead. */
1847 disasm_info.endian = BFD_ENDIAN_UNKNOWN;
1849 /* Allow the target to customize the info structure. */
1850 disassemble_init_for_target (abfd, & disasm_info);
1852 /* Pre-load the dynamic relocs if we are going
1853 to be dumping them along with the disassembly. */
1854 if (dump_dynamic_reloc_info)
1856 long relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
1858 if (relsize < 0)
1859 bfd_fatal (bfd_get_filename (abfd));
1861 if (relsize > 0)
1863 aux.dynrelbuf = xmalloc (relsize);
1864 aux.dynrelcount = bfd_canonicalize_dynamic_reloc (abfd, aux.dynrelbuf, dynsyms);
1865 if (aux.dynrelcount < 0)
1866 bfd_fatal (bfd_get_filename (abfd));
1868 /* Sort the relocs by address. */
1869 qsort (aux.dynrelbuf, aux.dynrelcount, sizeof (arelent *), compare_relocs);
1873 bfd_map_over_sections (abfd, disassemble_section, & disasm_info);
1875 if (aux.dynrelbuf != NULL)
1876 free (aux.dynrelbuf);
1877 free (sorted_syms);
1880 /* Read ABFD's stabs section STABSECT_NAME, and return a pointer to
1881 it. Return NULL on failure. */
1883 static char *
1884 read_section_stabs (bfd *abfd, const char *sect_name, bfd_size_type *size_ptr)
1886 asection *stabsect;
1887 bfd_size_type size;
1888 char *contents;
1890 stabsect = bfd_get_section_by_name (abfd, sect_name);
1891 if (stabsect == NULL)
1893 printf (_("No %s section present\n\n"), sect_name);
1894 return FALSE;
1897 size = bfd_section_size (abfd, stabsect);
1898 contents = xmalloc (size);
1900 if (! bfd_get_section_contents (abfd, stabsect, contents, 0, size))
1902 non_fatal (_("Reading %s section of %s failed: %s"),
1903 sect_name, bfd_get_filename (abfd),
1904 bfd_errmsg (bfd_get_error ()));
1905 free (contents);
1906 exit_status = 1;
1907 return NULL;
1910 *size_ptr = size;
1912 return contents;
1915 /* Stabs entries use a 12 byte format:
1916 4 byte string table index
1917 1 byte stab type
1918 1 byte stab other field
1919 2 byte stab desc field
1920 4 byte stab value
1921 FIXME: This will have to change for a 64 bit object format. */
1923 #define STRDXOFF (0)
1924 #define TYPEOFF (4)
1925 #define OTHEROFF (5)
1926 #define DESCOFF (6)
1927 #define VALOFF (8)
1928 #define STABSIZE (12)
1930 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
1931 using string table section STRSECT_NAME (in `strtab'). */
1933 static void
1934 print_section_stabs (bfd *abfd, const char *stabsect_name, unsigned *string_offset_ptr)
1936 int i;
1937 unsigned file_string_table_offset = 0;
1938 unsigned next_file_string_table_offset = *string_offset_ptr;
1939 bfd_byte *stabp, *stabs_end;
1941 stabp = stabs;
1942 stabs_end = stabp + stab_size;
1944 printf (_("Contents of %s section:\n\n"), stabsect_name);
1945 printf ("Symnum n_type n_othr n_desc n_value n_strx String\n");
1947 /* Loop through all symbols and print them.
1949 We start the index at -1 because there is a dummy symbol on
1950 the front of stabs-in-{coff,elf} sections that supplies sizes. */
1951 for (i = -1; stabp < stabs_end; stabp += STABSIZE, i++)
1953 const char *name;
1954 unsigned long strx;
1955 unsigned char type, other;
1956 unsigned short desc;
1957 bfd_vma value;
1959 strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
1960 type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
1961 other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
1962 desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
1963 value = bfd_h_get_32 (abfd, stabp + VALOFF);
1965 printf ("\n%-6d ", i);
1966 /* Either print the stab name, or, if unnamed, print its number
1967 again (makes consistent formatting for tools like awk). */
1968 name = bfd_get_stab_name (type);
1969 if (name != NULL)
1970 printf ("%-6s", name);
1971 else if (type == N_UNDF)
1972 printf ("HdrSym");
1973 else
1974 printf ("%-6d", type);
1975 printf (" %-6d %-6d ", other, desc);
1976 bfd_printf_vma (abfd, value);
1977 printf (" %-6lu", strx);
1979 /* Symbols with type == 0 (N_UNDF) specify the length of the
1980 string table associated with this file. We use that info
1981 to know how to relocate the *next* file's string table indices. */
1982 if (type == N_UNDF)
1984 file_string_table_offset = next_file_string_table_offset;
1985 next_file_string_table_offset += value;
1987 else
1989 /* Using the (possibly updated) string table offset, print the
1990 string (if any) associated with this symbol. */
1991 if ((strx + file_string_table_offset) < stabstr_size)
1992 printf (" %s", &strtab[strx + file_string_table_offset]);
1993 else
1994 printf (" *");
1997 printf ("\n\n");
1998 *string_offset_ptr = next_file_string_table_offset;
2001 typedef struct
2003 const char * section_name;
2004 const char * string_section_name;
2005 unsigned string_offset;
2007 stab_section_names;
2009 static void
2010 find_stabs_section (bfd *abfd, asection *section, void *names)
2012 int len;
2013 stab_section_names * sought = (stab_section_names *) names;
2015 /* Check for section names for which stabsect_name is a prefix, to
2016 handle .stab.N, etc. */
2017 len = strlen (sought->section_name);
2019 /* If the prefix matches, and the files section name ends with a
2020 nul or a digit, then we match. I.e., we want either an exact
2021 match or a section followed by a number. */
2022 if (strncmp (sought->section_name, section->name, len) == 0
2023 && (section->name[len] == 0
2024 || (section->name[len] == '.' && ISDIGIT (section->name[len + 1]))))
2026 if (strtab == NULL)
2027 strtab = read_section_stabs (abfd, sought->string_section_name,
2028 &stabstr_size);
2030 if (strtab)
2032 stabs = read_section_stabs (abfd, section->name, &stab_size);
2033 if (stabs)
2034 print_section_stabs (abfd, section->name, &sought->string_offset);
2039 static void
2040 dump_stabs_section (bfd *abfd, char *stabsect_name, char *strsect_name)
2042 stab_section_names s;
2044 s.section_name = stabsect_name;
2045 s.string_section_name = strsect_name;
2046 s.string_offset = 0;
2048 bfd_map_over_sections (abfd, find_stabs_section, & s);
2050 free (strtab);
2051 strtab = NULL;
2054 /* Dump the any sections containing stabs debugging information. */
2056 static void
2057 dump_stabs (bfd *abfd)
2059 dump_stabs_section (abfd, ".stab", ".stabstr");
2060 dump_stabs_section (abfd, ".stab.excl", ".stab.exclstr");
2061 dump_stabs_section (abfd, ".stab.index", ".stab.indexstr");
2062 dump_stabs_section (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
2065 static void
2066 dump_bfd_header (bfd *abfd)
2068 char *comma = "";
2070 printf (_("architecture: %s, "),
2071 bfd_printable_arch_mach (bfd_get_arch (abfd),
2072 bfd_get_mach (abfd)));
2073 printf (_("flags 0x%08x:\n"), abfd->flags);
2075 #define PF(x, y) if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
2076 PF (HAS_RELOC, "HAS_RELOC");
2077 PF (EXEC_P, "EXEC_P");
2078 PF (HAS_LINENO, "HAS_LINENO");
2079 PF (HAS_DEBUG, "HAS_DEBUG");
2080 PF (HAS_SYMS, "HAS_SYMS");
2081 PF (HAS_LOCALS, "HAS_LOCALS");
2082 PF (DYNAMIC, "DYNAMIC");
2083 PF (WP_TEXT, "WP_TEXT");
2084 PF (D_PAGED, "D_PAGED");
2085 PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
2086 PF (HAS_LOAD_PAGE, "HAS_LOAD_PAGE");
2087 printf (_("\nstart address 0x"));
2088 bfd_printf_vma (abfd, abfd->start_address);
2089 printf ("\n");
2093 static void
2094 dump_bfd_private_header (bfd *abfd)
2096 bfd_print_private_bfd_data (abfd, stdout);
2100 /* Display a section in hexadecimal format with associated characters.
2101 Each line prefixed by the zero padded address. */
2103 static void
2104 dump_section (bfd *abfd, asection *section, void *dummy ATTRIBUTE_UNUSED)
2106 bfd_byte *data = 0;
2107 bfd_size_type datasize;
2108 bfd_size_type addr_offset;
2109 bfd_size_type start_offset;
2110 bfd_size_type stop_offset;
2111 unsigned int opb = bfd_octets_per_byte (abfd);
2112 /* Bytes per line. */
2113 const int onaline = 16;
2114 char buf[64];
2115 int count;
2116 int width;
2118 if ((section->flags & SEC_HAS_CONTENTS) == 0)
2119 return;
2121 if (! process_section_p (section))
2122 return;
2124 if ((datasize = bfd_section_size (abfd, section)) == 0)
2125 return;
2127 printf (_("Contents of section %s:\n"), section->name);
2129 data = xmalloc (datasize);
2131 bfd_get_section_contents (abfd, section, data, 0, datasize);
2133 /* Compute the address range to display. */
2134 if (start_address == (bfd_vma) -1
2135 || start_address < section->vma)
2136 start_offset = 0;
2137 else
2138 start_offset = start_address - section->vma;
2140 if (stop_address == (bfd_vma) -1)
2141 stop_offset = datasize / opb;
2142 else
2144 if (stop_address < section->vma)
2145 stop_offset = 0;
2146 else
2147 stop_offset = stop_address - section->vma;
2149 if (stop_offset > datasize / opb)
2150 stop_offset = datasize / opb;
2153 width = 4;
2155 bfd_sprintf_vma (abfd, buf, start_offset + section->vma);
2156 if (strlen (buf) >= sizeof (buf))
2157 abort ();
2159 count = 0;
2160 while (buf[count] == '0' && buf[count+1] != '\0')
2161 count++;
2162 count = strlen (buf) - count;
2163 if (count > width)
2164 width = count;
2166 bfd_sprintf_vma (abfd, buf, stop_offset + section->vma - 1);
2167 if (strlen (buf) >= sizeof (buf))
2168 abort ();
2170 count = 0;
2171 while (buf[count] == '0' && buf[count+1] != '\0')
2172 count++;
2173 count = strlen (buf) - count;
2174 if (count > width)
2175 width = count;
2177 for (addr_offset = start_offset;
2178 addr_offset < stop_offset; addr_offset += onaline / opb)
2180 bfd_size_type j;
2182 bfd_sprintf_vma (abfd, buf, (addr_offset + section->vma));
2183 count = strlen (buf);
2184 if ((size_t) count >= sizeof (buf))
2185 abort ();
2187 putchar (' ');
2188 while (count < width)
2190 putchar ('0');
2191 count++;
2193 fputs (buf + count - width, stdout);
2194 putchar (' ');
2196 for (j = addr_offset * opb;
2197 j < addr_offset * opb + onaline; j++)
2199 if (j < stop_offset * opb)
2200 printf ("%02x", (unsigned) (data[j]));
2201 else
2202 printf (" ");
2203 if ((j & 3) == 3)
2204 printf (" ");
2207 printf (" ");
2208 for (j = addr_offset * opb;
2209 j < addr_offset * opb + onaline; j++)
2211 if (j >= stop_offset * opb)
2212 printf (" ");
2213 else
2214 printf ("%c", ISPRINT (data[j]) ? data[j] : '.');
2216 putchar ('\n');
2218 free (data);
2221 /* Actually display the various requested regions. */
2223 static void
2224 dump_data (bfd *abfd)
2226 bfd_map_over_sections (abfd, dump_section, NULL);
2229 /* Should perhaps share code and display with nm? */
2231 static void
2232 dump_symbols (bfd *abfd ATTRIBUTE_UNUSED, bfd_boolean dynamic)
2234 asymbol **current;
2235 long max;
2236 long count;
2238 if (dynamic)
2240 current = dynsyms;
2241 max = dynsymcount;
2242 printf ("DYNAMIC SYMBOL TABLE:\n");
2244 else
2246 current = syms;
2247 max = symcount;
2248 printf ("SYMBOL TABLE:\n");
2251 if (max == 0)
2252 printf (_("no symbols\n"));
2254 for (count = 0; count < max; count++)
2256 bfd *cur_bfd;
2258 if (*current == NULL)
2259 printf (_("no information for the %ld'th symbol"), count);
2261 else if ((cur_bfd = bfd_asymbol_bfd (*current)) == NULL)
2262 printf (_("could not determine the type of the %ld'th symbol"),
2263 count);
2265 else
2267 const char *name = (*current)->name;
2269 if (do_demangle && name != NULL && *name != '\0')
2271 char *alloc;
2273 /* If we want to demangle the name, we demangle it
2274 here, and temporarily clobber it while calling
2275 bfd_print_symbol. FIXME: This is a gross hack. */
2276 alloc = demangle (cur_bfd, name);
2277 (*current)->name = alloc;
2278 bfd_print_symbol (cur_bfd, stdout, *current,
2279 bfd_print_symbol_all);
2280 (*current)->name = name;
2281 free (alloc);
2283 else
2284 bfd_print_symbol (cur_bfd, stdout, *current,
2285 bfd_print_symbol_all);
2288 printf ("\n");
2289 current++;
2291 printf ("\n\n");
2294 static void
2295 dump_reloc_set (bfd *abfd, asection *sec, arelent **relpp, long relcount)
2297 arelent **p;
2298 char *last_filename, *last_functionname;
2299 unsigned int last_line;
2301 /* Get column headers lined up reasonably. */
2303 static int width;
2305 if (width == 0)
2307 char buf[30];
2309 bfd_sprintf_vma (abfd, buf, (bfd_vma) -1);
2310 width = strlen (buf) - 7;
2312 printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
2315 last_filename = NULL;
2316 last_functionname = NULL;
2317 last_line = 0;
2319 for (p = relpp; relcount && *p != NULL; p++, relcount--)
2321 arelent *q = *p;
2322 const char *filename, *functionname;
2323 unsigned int line;
2324 const char *sym_name;
2325 const char *section_name;
2327 if (start_address != (bfd_vma) -1
2328 && q->address < start_address)
2329 continue;
2330 if (stop_address != (bfd_vma) -1
2331 && q->address > stop_address)
2332 continue;
2334 if (with_line_numbers
2335 && sec != NULL
2336 && bfd_find_nearest_line (abfd, sec, syms, q->address,
2337 &filename, &functionname, &line))
2339 if (functionname != NULL
2340 && (last_functionname == NULL
2341 || strcmp (functionname, last_functionname) != 0))
2343 printf ("%s():\n", functionname);
2344 if (last_functionname != NULL)
2345 free (last_functionname);
2346 last_functionname = xstrdup (functionname);
2349 if (line > 0
2350 && (line != last_line
2351 || (filename != NULL
2352 && last_filename != NULL
2353 && strcmp (filename, last_filename) != 0)))
2355 printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
2356 last_line = line;
2357 if (last_filename != NULL)
2358 free (last_filename);
2359 if (filename == NULL)
2360 last_filename = NULL;
2361 else
2362 last_filename = xstrdup (filename);
2366 if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
2368 sym_name = (*(q->sym_ptr_ptr))->name;
2369 section_name = (*(q->sym_ptr_ptr))->section->name;
2371 else
2373 sym_name = NULL;
2374 section_name = NULL;
2377 if (sym_name)
2379 bfd_printf_vma (abfd, q->address);
2380 if (q->howto->name)
2381 printf (" %-16s ", q->howto->name);
2382 else
2383 printf (" %-16d ", q->howto->type);
2384 objdump_print_symname (abfd, NULL, *q->sym_ptr_ptr);
2386 else
2388 if (section_name == NULL)
2389 section_name = "*unknown*";
2390 bfd_printf_vma (abfd, q->address);
2391 printf (" %-16s [%s]",
2392 q->howto->name,
2393 section_name);
2396 if (q->addend)
2398 printf ("+0x");
2399 bfd_printf_vma (abfd, q->addend);
2402 printf ("\n");
2406 static void
2407 dump_relocs_in_section (bfd *abfd, asection *section, void *dummy ATTRIBUTE_UNUSED)
2409 arelent **relpp;
2410 long relcount;
2411 long relsize;
2413 if ( bfd_is_abs_section (section)
2414 || bfd_is_und_section (section)
2415 || bfd_is_com_section (section)
2416 || (! process_section_p (section))
2417 || ((section->flags & SEC_RELOC) == 0))
2418 return;
2420 relsize = bfd_get_reloc_upper_bound (abfd, section);
2421 if (relsize < 0)
2422 bfd_fatal (bfd_get_filename (abfd));
2424 printf ("RELOCATION RECORDS FOR [%s]:", section->name);
2426 if (relsize == 0)
2428 printf (" (none)\n\n");
2429 return;
2432 relpp = xmalloc (relsize);
2433 relcount = bfd_canonicalize_reloc (abfd, section, relpp, syms);
2435 if (relcount < 0)
2436 bfd_fatal (bfd_get_filename (abfd));
2437 else if (relcount == 0)
2438 printf (" (none)\n\n");
2439 else
2441 printf ("\n");
2442 dump_reloc_set (abfd, section, relpp, relcount);
2443 printf ("\n\n");
2445 free (relpp);
2448 static void
2449 dump_relocs (bfd *abfd)
2451 bfd_map_over_sections (abfd, dump_relocs_in_section, NULL);
2454 static void
2455 dump_dynamic_relocs (bfd *abfd)
2457 long relsize;
2458 arelent **relpp;
2459 long relcount;
2461 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
2462 if (relsize < 0)
2463 bfd_fatal (bfd_get_filename (abfd));
2465 printf ("DYNAMIC RELOCATION RECORDS");
2467 if (relsize == 0)
2468 printf (" (none)\n\n");
2469 else
2471 relpp = xmalloc (relsize);
2472 relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
2474 if (relcount < 0)
2475 bfd_fatal (bfd_get_filename (abfd));
2476 else if (relcount == 0)
2477 printf (" (none)\n\n");
2478 else
2480 printf ("\n");
2481 dump_reloc_set (abfd, NULL, relpp, relcount);
2482 printf ("\n\n");
2484 free (relpp);
2488 /* Creates a table of paths, to search for source files. */
2490 static void
2491 add_include_path (const char *path)
2493 if (path[0] == 0)
2494 return;
2495 include_path_count++;
2496 include_paths = xrealloc (include_paths,
2497 include_path_count * sizeof (*include_paths));
2498 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2499 if (path[1] == ':' && path[2] == 0)
2500 path = concat (path, ".", (const char *) 0);
2501 #endif
2502 include_paths[include_path_count - 1] = path;
2505 static void
2506 adjust_addresses (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *dummy ATTRIBUTE_UNUSED)
2508 section->vma += adjust_section_vma;
2509 section->lma += adjust_section_vma;
2512 /* Dump selected contents of ABFD. */
2514 static void
2515 dump_bfd (bfd *abfd)
2517 /* If we are adjusting section VMA's, change them all now. Changing
2518 the BFD information is a hack. However, we must do it, or
2519 bfd_find_nearest_line will not do the right thing. */
2520 if (adjust_section_vma != 0)
2521 bfd_map_over_sections (abfd, adjust_addresses, NULL);
2523 if (! dump_debugging_tags)
2524 printf (_("\n%s: file format %s\n"), bfd_get_filename (abfd),
2525 abfd->xvec->name);
2526 if (dump_ar_hdrs)
2527 print_arelt_descr (stdout, abfd, TRUE);
2528 if (dump_file_header)
2529 dump_bfd_header (abfd);
2530 if (dump_private_headers)
2531 dump_bfd_private_header (abfd);
2532 if (! dump_debugging_tags)
2533 putchar ('\n');
2534 if (dump_section_headers)
2535 dump_headers (abfd);
2537 if (dump_symtab || dump_reloc_info || disassemble || dump_debugging)
2538 syms = slurp_symtab (abfd);
2539 if (dump_dynamic_symtab || dump_dynamic_reloc_info)
2540 dynsyms = slurp_dynamic_symtab (abfd);
2542 if (dump_symtab)
2543 dump_symbols (abfd, FALSE);
2544 if (dump_dynamic_symtab)
2545 dump_symbols (abfd, TRUE);
2546 if (dump_stab_section_info)
2547 dump_stabs (abfd);
2548 if (dump_reloc_info && ! disassemble)
2549 dump_relocs (abfd);
2550 if (dump_dynamic_reloc_info && ! disassemble)
2551 dump_dynamic_relocs (abfd);
2552 if (dump_section_contents)
2553 dump_data (abfd);
2554 if (disassemble)
2555 disassemble_data (abfd);
2557 if (dump_debugging)
2559 void *dhandle;
2561 dhandle = read_debugging_info (abfd, syms, symcount);
2562 if (dhandle != NULL)
2564 if (! print_debugging_info (stdout, dhandle, abfd, syms, demangle,
2565 dump_debugging_tags ? TRUE : FALSE))
2567 non_fatal (_("%s: printing debugging information failed"),
2568 bfd_get_filename (abfd));
2569 exit_status = 1;
2574 if (syms)
2576 free (syms);
2577 syms = NULL;
2580 if (dynsyms)
2582 free (dynsyms);
2583 dynsyms = NULL;
2587 static void
2588 display_bfd (bfd *abfd)
2590 char **matching;
2592 if (bfd_check_format_matches (abfd, bfd_object, &matching))
2594 dump_bfd (abfd);
2595 return;
2598 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
2600 nonfatal (bfd_get_filename (abfd));
2601 list_matching_formats (matching);
2602 free (matching);
2603 return;
2606 if (bfd_get_error () != bfd_error_file_not_recognized)
2608 nonfatal (bfd_get_filename (abfd));
2609 return;
2612 if (bfd_check_format_matches (abfd, bfd_core, &matching))
2614 dump_bfd (abfd);
2615 return;
2618 nonfatal (bfd_get_filename (abfd));
2620 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
2622 list_matching_formats (matching);
2623 free (matching);
2627 static void
2628 display_file (char *filename, char *target)
2630 bfd *file;
2631 bfd *arfile = NULL;
2633 if (get_file_size (filename) < 1)
2634 return;
2636 file = bfd_openr (filename, target);
2637 if (file == NULL)
2639 nonfatal (filename);
2640 return;
2643 /* If the file is an archive, process all of its elements. */
2644 if (bfd_check_format (file, bfd_archive))
2646 bfd *last_arfile = NULL;
2648 printf (_("In archive %s:\n"), bfd_get_filename (file));
2649 for (;;)
2651 bfd_set_error (bfd_error_no_error);
2653 arfile = bfd_openr_next_archived_file (file, arfile);
2654 if (arfile == NULL)
2656 if (bfd_get_error () != bfd_error_no_more_archived_files)
2657 nonfatal (bfd_get_filename (file));
2658 break;
2661 display_bfd (arfile);
2663 if (last_arfile != NULL)
2664 bfd_close (last_arfile);
2665 last_arfile = arfile;
2668 if (last_arfile != NULL)
2669 bfd_close (last_arfile);
2671 else
2672 display_bfd (file);
2674 bfd_close (file);
2678 main (int argc, char **argv)
2680 int c;
2681 char *target = default_target;
2682 bfd_boolean seenflag = FALSE;
2684 #if defined (HAVE_SETLOCALE)
2685 #if defined (HAVE_LC_MESSAGES)
2686 setlocale (LC_MESSAGES, "");
2687 #endif
2688 setlocale (LC_CTYPE, "");
2689 #endif
2691 bindtextdomain (PACKAGE, LOCALEDIR);
2692 textdomain (PACKAGE);
2694 program_name = *argv;
2695 xmalloc_set_program_name (program_name);
2697 START_PROGRESS (program_name, 0);
2699 bfd_init ();
2700 set_default_bfd_target ();
2702 while ((c = getopt_long (argc, argv, "pib:m:M:VvCdDlfaHhrRtTxsSI:j:wE:zgeG",
2703 long_options, (int *) 0))
2704 != EOF)
2706 switch (c)
2708 case 0:
2709 break; /* We've been given a long option. */
2710 case 'm':
2711 machine = optarg;
2712 break;
2713 case 'M':
2714 if (disassembler_options)
2715 /* Ignore potential memory leak for now. */
2716 disassembler_options = concat (disassembler_options, ",",
2717 optarg, NULL);
2718 else
2719 disassembler_options = optarg;
2720 break;
2721 case 'j':
2722 if (only_used == only_size)
2724 only_size += 8;
2725 only = xrealloc (only, only_size * sizeof (char *));
2727 only [only_used++] = optarg;
2728 break;
2729 case 'l':
2730 with_line_numbers = TRUE;
2731 break;
2732 case 'b':
2733 target = optarg;
2734 break;
2735 case 'C':
2736 do_demangle = TRUE;
2737 if (optarg != NULL)
2739 enum demangling_styles style;
2741 style = cplus_demangle_name_to_style (optarg);
2742 if (style == unknown_demangling)
2743 fatal (_("unknown demangling style `%s'"),
2744 optarg);
2746 cplus_demangle_set_style (style);
2748 break;
2749 case 'w':
2750 wide_output = TRUE;
2751 break;
2752 case OPTION_ADJUST_VMA:
2753 adjust_section_vma = parse_vma (optarg, "--adjust-vma");
2754 break;
2755 case OPTION_START_ADDRESS:
2756 start_address = parse_vma (optarg, "--start-address");
2757 break;
2758 case OPTION_STOP_ADDRESS:
2759 stop_address = parse_vma (optarg, "--stop-address");
2760 break;
2761 case 'E':
2762 if (strcmp (optarg, "B") == 0)
2763 endian = BFD_ENDIAN_BIG;
2764 else if (strcmp (optarg, "L") == 0)
2765 endian = BFD_ENDIAN_LITTLE;
2766 else
2768 non_fatal (_("unrecognized -E option"));
2769 usage (stderr, 1);
2771 break;
2772 case OPTION_ENDIAN:
2773 if (strncmp (optarg, "big", strlen (optarg)) == 0)
2774 endian = BFD_ENDIAN_BIG;
2775 else if (strncmp (optarg, "little", strlen (optarg)) == 0)
2776 endian = BFD_ENDIAN_LITTLE;
2777 else
2779 non_fatal (_("unrecognized --endian type `%s'"), optarg);
2780 usage (stderr, 1);
2782 break;
2784 case 'f':
2785 dump_file_header = TRUE;
2786 seenflag = TRUE;
2787 break;
2788 case 'i':
2789 formats_info = TRUE;
2790 seenflag = TRUE;
2791 break;
2792 case 'I':
2793 add_include_path (optarg);
2794 break;
2795 case 'p':
2796 dump_private_headers = TRUE;
2797 seenflag = TRUE;
2798 break;
2799 case 'x':
2800 dump_private_headers = TRUE;
2801 dump_symtab = TRUE;
2802 dump_reloc_info = TRUE;
2803 dump_file_header = TRUE;
2804 dump_ar_hdrs = TRUE;
2805 dump_section_headers = TRUE;
2806 seenflag = TRUE;
2807 break;
2808 case 't':
2809 dump_symtab = TRUE;
2810 seenflag = TRUE;
2811 break;
2812 case 'T':
2813 dump_dynamic_symtab = TRUE;
2814 seenflag = TRUE;
2815 break;
2816 case 'd':
2817 disassemble = TRUE;
2818 seenflag = TRUE;
2819 break;
2820 case 'z':
2821 disassemble_zeroes = TRUE;
2822 break;
2823 case 'D':
2824 disassemble = TRUE;
2825 disassemble_all = TRUE;
2826 seenflag = TRUE;
2827 break;
2828 case 'S':
2829 disassemble = TRUE;
2830 with_source_code = TRUE;
2831 seenflag = TRUE;
2832 break;
2833 case 'g':
2834 dump_debugging = 1;
2835 seenflag = TRUE;
2836 break;
2837 case 'e':
2838 dump_debugging = 1;
2839 dump_debugging_tags = 1;
2840 do_demangle = TRUE;
2841 seenflag = TRUE;
2842 break;
2843 case 'G':
2844 dump_stab_section_info = TRUE;
2845 seenflag = TRUE;
2846 break;
2847 case 's':
2848 dump_section_contents = TRUE;
2849 seenflag = TRUE;
2850 break;
2851 case 'r':
2852 dump_reloc_info = TRUE;
2853 seenflag = TRUE;
2854 break;
2855 case 'R':
2856 dump_dynamic_reloc_info = TRUE;
2857 seenflag = TRUE;
2858 break;
2859 case 'a':
2860 dump_ar_hdrs = TRUE;
2861 seenflag = TRUE;
2862 break;
2863 case 'h':
2864 dump_section_headers = TRUE;
2865 seenflag = TRUE;
2866 break;
2867 case 'H':
2868 usage (stdout, 0);
2869 seenflag = TRUE;
2870 case 'v':
2871 case 'V':
2872 show_version = TRUE;
2873 seenflag = TRUE;
2874 break;
2876 default:
2877 usage (stderr, 1);
2881 if (show_version)
2882 print_version ("objdump");
2884 if (!seenflag)
2885 usage (stderr, 2);
2887 if (formats_info)
2888 exit_status = display_info ();
2889 else
2891 if (optind == argc)
2892 display_file ("a.out", target);
2893 else
2894 for (; optind < argc;)
2895 display_file (argv[optind++], target);
2898 END_PROGRESS (program_name);
2900 return exit_status;