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