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