* objdump.c (disassemble_all): New global variable.
[external/binutils.git] / binutils / objdump.c
1 /* objdump.c -- dump information about an object file.
2    Copyright 1990, 1991, 1992, 1993, 1994 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, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include "bfd.h"
21 #include "sysdep.h"
22 #include "getopt.h"
23 #include "bucomm.h"
24 #include <stdio.h>
25 #include <ctype.h>
26 #include "dis-asm.h"
27 #include "libiberty.h"
28
29 /* Internal headers for the ELF .stab-dump code - sorry.  */
30 #define BYTES_IN_WORD   32
31 #include "aout/aout64.h"
32
33 #ifndef FPRINTF_ALREADY_DECLARED
34 extern int fprintf PARAMS ((FILE *, CONST char *, ...));
35 #endif
36
37 char *default_target = NULL;    /* default at runtime */
38
39 extern char *program_version;
40
41 int show_version = 0;           /* show the version number */
42 int dump_section_contents;      /* -s */
43 int dump_section_headers;       /* -h */
44 boolean dump_file_header;       /* -f */
45 int dump_symtab;                /* -t */
46 int dump_dynamic_symtab;        /* -T */
47 int dump_reloc_info;            /* -r */
48 int dump_dynamic_reloc_info;    /* -R */
49 int dump_ar_hdrs;               /* -a */
50 int with_line_numbers;          /* -l */
51 int dump_stab_section_info;     /* --stabs */
52 boolean disassemble;            /* -d */
53 boolean disassemble_all;        /* -D */
54 boolean formats_info;           /* -i */
55 char *only;                     /* -j secname */
56
57 /* Extra info to pass to the disassembler address printing function.  */
58 struct objdump_disasm_info {
59   bfd *abfd;
60   asection *sec;
61 };
62
63 /* Architecture to disassemble for, or default if NULL.  */
64 char *machine = (char *) NULL;
65
66 /* The symbol table.  */
67 asymbol **syms;
68
69 /* Number of symbols in `syms'.  */
70 long symcount = 0;
71
72 /* The dynamic symbol table.  */
73 asymbol **dynsyms;
74
75 /* Number of symbols in `dynsyms'.  */
76 long dynsymcount = 0;
77
78 /* Forward declarations.  */
79
80 static void
81 display_file PARAMS ((char *filename, char *target));
82
83 static void
84 dump_data PARAMS ((bfd *abfd));
85
86 static void
87 dump_relocs PARAMS ((bfd *abfd));
88
89 static void
90 dump_dynamic_relocs PARAMS ((bfd * abfd));
91
92 static void
93 dump_reloc_set PARAMS ((bfd *, arelent **, long));
94
95 static void
96 dump_symbols PARAMS ((bfd *abfd, boolean dynamic));
97
98 static void
99 display_bfd PARAMS ((bfd *abfd));
100
101 static void
102 objdump_print_address PARAMS ((bfd_vma, struct disassemble_info *));
103 \f
104 void
105 usage (stream, status)
106      FILE *stream;
107      int status;
108 {
109   fprintf (stream, "\
110 Usage: %s [-ahifdDrRtTxsl] [-b bfdname] [-m machine] [-j section-name]\n\
111        [--archive-headers] [--target=bfdname] [--disassemble]\n\
112        [--disassemble-all] [--file-headers] [--section-headers] [--headers]\n\
113        [--info] [--section=section-name] [--line-numbers]\n\
114        [--architecture=machine] [--reloc] [--full-contents] [--stabs]\n\
115        [--syms] [--all-headers] [--dynamic-syms] [--dynamic-reloc]\n\
116        [--version] [--help] objfile...\n\
117 at least one option besides -l (--line-numbers) must be given\n",
118            program_name);
119   exit (status);
120 }
121
122 static struct option long_options[]=
123 {
124   {"all-headers", no_argument, NULL, 'x'},
125   {"architecture", required_argument, NULL, 'm'},
126   {"archive-headers", no_argument, NULL, 'a'},
127   {"disassemble", no_argument, NULL, 'd'},
128   {"disassemble-all", no_argument, NULL, 'D'},
129   {"dynamic-reloc", no_argument, NULL, 'R'},
130   {"dynamic-syms", no_argument, NULL, 'T'},
131   {"file-headers", no_argument, NULL, 'f'},
132   {"full-contents", no_argument, NULL, 's'},
133   {"headers", no_argument, NULL, 'h'},
134   {"help", no_argument, NULL, 'H'},
135   {"info", no_argument, NULL, 'i'},
136   {"line-numbers", no_argument, NULL, 'l'},
137   {"reloc", no_argument, NULL, 'r'},
138   {"section", required_argument, NULL, 'j'},
139   {"section-headers", no_argument, NULL, 'h'},
140   {"stabs", no_argument, &dump_stab_section_info, 1},
141   {"syms", no_argument, NULL, 't'},
142   {"target", required_argument, NULL, 'b'},
143   {"version", no_argument, &show_version,    1},
144   {0, no_argument, 0, 0}
145 };
146 \f
147 static void
148 dump_section_header (abfd, section, ignored)
149      bfd *abfd;
150      asection *section;
151      PTR ignored;
152 {
153   char *comma = "";
154
155 #define PF(x,y) \
156   if (section->flags & x) {  printf("%s%s",comma,y); comma = ", "; }
157
158
159   printf ("SECTION %d [%s]\t: size %08x",
160           section->index,
161           section->name,
162           (unsigned) bfd_get_section_size_before_reloc (section));
163   printf (" vma ");
164   printf_vma (section->vma);
165   printf (" align 2**%u\n ",
166           section->alignment_power);
167   PF (SEC_ALLOC, "ALLOC");
168   PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
169   PF (SEC_CONSTRUCTOR_TEXT, "CONSTRUCTOR TEXT");
170   PF (SEC_CONSTRUCTOR_DATA, "CONSTRUCTOR DATA");
171   PF (SEC_CONSTRUCTOR_BSS, "CONSTRUCTOR BSS");
172   PF (SEC_LOAD, "LOAD");
173   PF (SEC_RELOC, "RELOC");
174 #ifdef SEC_BALIGN
175   PF (SEC_BALIGN, "BALIGN");
176 #endif
177   PF (SEC_READONLY, "READONLY");
178   PF (SEC_CODE, "CODE");
179   PF (SEC_DATA, "DATA");
180   PF (SEC_ROM, "ROM");
181   PF (SEC_DEBUGGING, "DEBUGGING");
182   PF (SEC_NEVER_LOAD, "NEVER_LOAD");
183   printf ("\n");
184 #undef PF
185 }
186
187 static void
188 dump_headers (abfd)
189      bfd *abfd;
190 {
191   bfd_map_over_sections (abfd, dump_section_header, (PTR) NULL);
192 }
193 \f
194 static asymbol **
195 slurp_symtab (abfd)
196      bfd *abfd;
197 {
198   asymbol **sy = (asymbol **) NULL;
199   long storage;
200
201   if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
202     {
203       printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd));
204       return NULL;
205     }
206
207   storage = bfd_get_symtab_upper_bound (abfd);
208   if (storage < 0)
209     bfd_fatal (bfd_get_filename (abfd));
210
211   if (storage)
212     {
213       sy = (asymbol **) xmalloc (storage);
214     }
215   symcount = bfd_canonicalize_symtab (abfd, sy);
216   if (symcount < 0)
217     bfd_fatal (bfd_get_filename (abfd));
218   if (symcount == 0)
219     fprintf (stderr, "%s: %s: No symbols\n",
220              program_name, bfd_get_filename (abfd));
221   return sy;
222 }
223
224 /* Read in the dynamic symbols.  */
225
226 static asymbol **
227 slurp_dynamic_symtab (abfd)
228      bfd *abfd;
229 {
230   asymbol **sy = (asymbol **) NULL;
231   long storage;
232
233   storage = bfd_get_dynamic_symtab_upper_bound (abfd);
234   if (storage < 0)
235     {
236       if (!(bfd_get_file_flags (abfd) & DYNAMIC))
237         {
238           fprintf (stderr, "%s: %s: not a dynamic object\n",
239                    program_name, bfd_get_filename (abfd));
240           return NULL;
241         }
242
243       bfd_fatal (bfd_get_filename (abfd));
244     }
245
246   if (storage)
247     {
248       sy = (asymbol **) xmalloc (storage);
249     }
250   dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
251   if (dynsymcount < 0)
252     bfd_fatal (bfd_get_filename (abfd));
253   if (dynsymcount == 0)
254     fprintf (stderr, "%s: %s: No dynamic symbols\n",
255              program_name, bfd_get_filename (abfd));
256   return sy;
257 }
258
259 /* Filter out (in place) symbols that are useless for disassembly.
260    COUNT is the number of elements in SYMBOLS.
261    Return the number of useful symbols. */
262
263 long
264 remove_useless_symbols (symbols, count)
265      asymbol **symbols;
266      long count;
267 {
268   register asymbol **in_ptr = symbols, **out_ptr = symbols;
269
270   while (--count >= 0)
271     {
272       asymbol *sym = *in_ptr++;
273
274       if (sym->name == NULL || sym->name[0] == '\0')
275         continue;
276       if (sym->flags & (BSF_DEBUGGING))
277         continue;
278       if (bfd_is_und_section (sym->section)
279           || bfd_is_com_section (sym->section))
280         continue;
281
282       *out_ptr++ = sym;
283     }
284   return out_ptr - symbols;
285 }
286
287 /* Sort symbols into value order. */
288
289 static int 
290 compare_symbols (ap, bp)
291      const PTR ap;
292      const PTR bp;
293 {
294   const asymbol *a = *(const asymbol **)ap;
295   const asymbol *b = *(const asymbol **)bp;
296
297   if (a->value > b->value)
298     return 1;
299   else if (a->value < b->value)
300     return -1;
301
302   if (a->section > b->section)
303     return 1;
304   else if (a->section < b->section)
305     return -1;
306   return 0;
307 }
308
309 /* Sort relocs into address order.  */
310
311 static int
312 compare_relocs (ap, bp)
313      const PTR ap;
314      const PTR bp;
315 {
316   const arelent *a = *(const arelent **)ap;
317   const arelent *b = *(const arelent **)bp;
318
319   if (a->address > b->address)
320     return 1;
321   else if (a->address < b->address)
322     return -1;
323
324   return compare_symbols ((const PTR) a->sym_ptr_ptr,
325                           (const PTR) b->sym_ptr_ptr);
326 }
327
328 /* Print VMA symbolically to INFO if possible.  */
329
330 static void
331 objdump_print_address (vma, info)
332      bfd_vma vma;
333      struct disassemble_info *info;
334 {
335   /* @@ For relocateable files, should filter out symbols belonging to
336      the wrong section.  Unfortunately, not enough information is supplied
337      to this routine to determine the correct section in all cases.  */
338   /* @@ Would it speed things up to cache the last two symbols returned,
339      and maybe their address ranges?  For many processors, only one memory
340      operand can be present at a time, so the 2-entry cache wouldn't be
341      constantly churned by code doing heavy memory accesses.  */
342
343   /* Indices in `syms'.  */
344   long min = 0;
345   long max = symcount;
346   long thisplace;
347
348   bfd_signed_vma vardiff;
349
350   fprintf_vma (info->stream, vma);
351
352   if (symcount < 1)
353     return;
354
355   /* Perform a binary search looking for the closest symbol to the
356      required value.  We are searching the range (min, max].  */
357   while (min + 1 < max)
358     {
359       asymbol *sym;
360
361       thisplace = (max + min) / 2;
362       sym = syms[thisplace];
363
364       vardiff = sym->value - vma;
365
366       if (vardiff > 0)
367         max = thisplace;
368       else if (vardiff < 0)
369         min = thisplace;
370       else
371         {
372           min = thisplace;
373           break;
374         }
375     }
376
377   /* The symbol we want is now in min, the low end of the range we
378      were searching.  */
379   thisplace = min;
380
381   {
382     /* If this symbol isn't global, search for one with the same value
383        that is.  */
384     bfd_vma val = syms[thisplace]->value;
385     long i;
386     if (syms[thisplace]->flags & (BSF_LOCAL|BSF_DEBUGGING))
387       for (i = thisplace - 1; i >= 0; i--)
388         {
389           if (syms[i]->value == val
390               && (!(syms[i]->flags & (BSF_LOCAL|BSF_DEBUGGING))
391                   || ((syms[thisplace]->flags & BSF_DEBUGGING)
392                       && !(syms[i]->flags & BSF_DEBUGGING))))
393             {
394               thisplace = i;
395               break;
396             }
397         }
398     if (syms[thisplace]->flags & (BSF_LOCAL|BSF_DEBUGGING))
399       for (i = thisplace + 1; i < symcount; i++)
400         {
401           if (syms[i]->value == val
402               && (!(syms[i]->flags & (BSF_LOCAL|BSF_DEBUGGING))
403                   || ((syms[thisplace]->flags & BSF_DEBUGGING)
404                       && !(syms[i]->flags & BSF_DEBUGGING))))
405             {
406               thisplace = i;
407               break;
408             }
409         }
410   }
411   {
412     /* If the file is relocateable, and the symbol could be from this
413        section, prefer a symbol from this section over symbols from
414        others, even if the other symbol's value might be closer.
415        
416        Note that this may be wrong for some symbol references if the
417        sections have overlapping memory ranges, but in that case there's
418        no way to tell what's desired without looking at the relocation
419        table.  */
420     struct objdump_disasm_info *aux;
421     long i;
422
423     aux = (struct objdump_disasm_info *) info->application_data;
424     if ((aux->abfd->flags & HAS_RELOC)
425         && vma >= bfd_get_section_vma (aux->abfd, aux->sec)
426         && vma < (bfd_get_section_vma (aux->abfd, aux->sec)
427                   + bfd_get_section_size_before_reloc (aux->sec))
428         && syms[thisplace]->section != aux->sec)
429       {
430         for (i = thisplace + 1; i < symcount; i++)
431           {
432             if (syms[i]->value != syms[thisplace]->value)
433               break;
434           }
435         --i;
436         for (; i >= 0; i--)
437           {
438             if (syms[i]->section == aux->sec)
439               {
440                 thisplace = i;
441                 break;
442               }
443           }
444       }
445   }
446   fprintf (info->stream, " <%s", syms[thisplace]->name);
447   if (syms[thisplace]->value > vma)
448     {
449       char buf[30], *p = buf;
450       sprintf_vma (buf, syms[thisplace]->value - vma);
451       while (*p == '0')
452         p++;
453       fprintf (info->stream, "-%s", p);
454     }
455   else if (vma > syms[thisplace]->value)
456     {
457       char buf[30], *p = buf;
458       sprintf_vma (buf, vma - syms[thisplace]->value);
459       while (*p == '0')
460         p++;
461       fprintf (info->stream, "+%s", p);
462     }
463   fprintf (info->stream, ">");
464 }
465
466 void
467 disassemble_data (abfd)
468      bfd *abfd;
469 {
470   long i;
471   unsigned int (*print) () = 0; /* Old style */
472   disassembler_ftype disassemble_fn = 0; /* New style */
473   struct disassemble_info disasm_info;
474   struct objdump_disasm_info aux;
475
476   int prevline = 0;
477   char *prev_function = NULL;
478
479   asection *section;
480
481   boolean done_dot = false;
482
483   /* If we are dumping relocation information, read the relocs for
484      each section we are going to disassemble.  We must do this before
485      we sort the symbols.  */
486   if (dump_reloc_info)
487     {
488       for (section = abfd->sections;
489            section != (asection *) NULL;
490            section = section->next)
491         {
492           long relsize;
493           arelent **relpp;
494
495           if ((section->flags & SEC_LOAD) == 0
496               || (! disassemble_all
497                   && only == NULL
498                   && (section->flags & SEC_CODE) == 0))
499             continue;
500           if (only != (char *) NULL && strcmp (only, section->name) != 0)
501             continue;
502           if ((section->flags & SEC_RELOC) == 0)
503             continue;
504
505           /* We store the reloc information in the reloc_count and
506              orelocation fields.  */
507
508           relsize = bfd_get_reloc_upper_bound (abfd, section);
509           if (relsize < 0)
510             bfd_fatal (bfd_get_filename (abfd));
511
512           if (relsize == 0)
513             section->reloc_count = 0;
514           else
515             {
516               long relcount;
517
518               relpp = (arelent **) xmalloc (relsize);
519               relcount = bfd_canonicalize_reloc (abfd, section, relpp, syms);
520               if (relcount < 0)
521                 bfd_fatal (bfd_get_filename (abfd));
522               section->reloc_count = relcount;
523               section->orelocation = relpp;
524             }
525         }
526     }
527
528   /* Replace symbol section relative values with abs values.  */
529   for (i = 0; i < symcount; i++)
530     {
531       syms[i]->value += syms[i]->section->vma;
532     }
533
534   symcount = remove_useless_symbols (syms, symcount);
535
536   /* Sort the symbols into section and symbol order */
537   qsort (syms, symcount, sizeof (asymbol *), compare_symbols);
538
539   INIT_DISASSEMBLE_INFO(disasm_info, stdout);
540   disasm_info.application_data = (PTR) &aux;
541   aux.abfd = abfd;
542   disasm_info.print_address_func = objdump_print_address;
543
544   if (machine != (char *) NULL)
545     {
546       bfd_arch_info_type *info = bfd_scan_arch (machine);
547       if (info == NULL)
548         {
549           fprintf (stderr, "%s: Can't use supplied machine %s\n",
550                    program_name,
551                    machine);
552           exit (1);
553         }
554       abfd->arch_info = info;
555     }
556
557   /* See if we can disassemble using bfd.  */
558
559   if (abfd->arch_info->disassemble)
560     {
561       print = abfd->arch_info->disassemble;
562     }
563   else
564     {
565       disassemble_fn = disassembler (abfd);
566       if (!disassemble_fn)
567         {
568           fprintf (stderr, "%s: Can't disassemble for architecture %s\n",
569                    program_name,
570                    bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
571           exit (1);
572         }
573     }
574
575   for (section = abfd->sections;
576        section != (asection *) NULL;
577        section = section->next)
578     {
579       bfd_byte *data = NULL;
580       bfd_size_type datasize = 0;
581       arelent **relpp = NULL;
582       arelent **relppend = NULL;
583
584       if ((section->flags & SEC_LOAD) == 0
585           || (! disassemble_all
586               && only == NULL
587               && (section->flags & SEC_CODE) == 0))
588         continue;
589       if (only != (char *) NULL && strcmp (only, section->name) != 0)
590         continue;
591
592       if (dump_reloc_info
593           && (section->flags & SEC_RELOC) != 0)
594         {
595           /* Sort the relocs by address.  */
596           qsort (section->orelocation, section->reloc_count,
597                  sizeof (arelent *), compare_relocs);
598           relpp = section->orelocation;
599           relppend = relpp + section->reloc_count;
600         }
601
602       printf ("Disassembly of section %s:\n", section->name);
603
604       datasize = bfd_get_section_size_before_reloc (section);
605       if (datasize == 0)
606         continue;
607
608       data = (bfd_byte *) xmalloc ((size_t) datasize);
609
610       bfd_get_section_contents (abfd, section, data, 0, datasize);
611
612       aux.sec = section;
613       disasm_info.buffer = data;
614       disasm_info.buffer_vma = section->vma;
615       disasm_info.buffer_length = datasize;
616       i = 0;
617       while (i < disasm_info.buffer_length)
618         {
619           int bytes;
620
621           if (data[i] == 0 && data[i + 1] == 0 && data[i + 2] == 0 &&
622               data[i + 3] == 0)
623             {
624               if (done_dot == false)
625                 {
626                   printf ("...\n");
627                   done_dot = true;
628                 }
629               bytes = 4;
630             }
631           else
632             {
633               done_dot = false;
634               if (with_line_numbers)
635                 {
636                   CONST char *filename;
637                   CONST char *functionname;
638                   unsigned int line;
639
640                   if (bfd_find_nearest_line (abfd,
641                                              section,
642                                              syms,
643                                              section->vma + i,
644                                              &filename,
645                                              &functionname,
646                                              &line))
647                     {
648                       if (functionname
649                           && *functionname != '\0'
650                           && (prev_function == NULL
651                               || strcmp (functionname, prev_function) != 0))
652                         {
653                           printf ("%s():\n", functionname);
654                           if (prev_function != NULL)
655                             free (prev_function);
656                           prev_function = xmalloc (strlen (functionname) + 1);
657                           strcpy (prev_function, functionname);
658                         }
659                       if (!filename)
660                         filename = "???";
661                       if (line && line != prevline)
662                         {
663                           printf ("%s:%u\n", filename, line);
664                           prevline = line;
665                         }
666                     }
667                 }
668               objdump_print_address (section->vma + i, &disasm_info);
669               putchar (' ');
670
671               if (disassemble_fn)
672                 {
673                   /* New style */
674                   bytes = (*disassemble_fn) (section->vma + i, &disasm_info);
675                   if (bytes < 0)
676                     break;
677                 }
678               else
679                 {
680                   /* Old style */
681                   bytes = print (section->vma + i, data + i, stdout);
682                 }
683               putchar ('\n');
684             }
685
686           if (dump_reloc_info
687               && (section->flags & SEC_RELOC) != 0)
688             {
689               while (relpp < relppend
690                      && ((*relpp)->address >= i
691                          && (*relpp)->address < i + bytes))
692                 {
693                   arelent *q;
694                   const char *sym_name;
695
696                   q = *relpp;
697
698                   printf ("\t\tRELOC: ");
699
700                   printf_vma (section->vma + q->address);
701
702                   printf (" %s ", q->howto->name);
703
704                   if (q->sym_ptr_ptr != NULL
705                       && *q->sym_ptr_ptr != NULL)
706                     {
707                       sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
708                       if (sym_name == NULL || *sym_name == '\0')
709                         {
710                           asection *sym_sec;
711
712                           sym_sec = bfd_get_section (*q->sym_ptr_ptr);
713                           sym_name = bfd_get_section_name (abfd, sym_sec);
714                           if (sym_name == NULL || *sym_name == '\0')
715                             sym_name = "*unknown*";
716                         }
717                     }
718
719                   printf ("%s", sym_name);
720
721                   if (q->addend)
722                     {
723                       printf ("+0x");
724                       printf_vma (q->addend);
725                     }
726
727                   printf ("\n");
728
729                   ++relpp;
730                 }
731             }
732
733           i += bytes;
734         }
735       free (data);
736     }
737 }
738 \f
739
740 /* Define a table of stab values and print-strings.  We wish the initializer
741    could be a direct-mapped table, but instead we build one the first
742    time we need it.  */
743
744 char **stab_name;
745
746 struct stab_print {
747   int value;
748   char *string;
749 };
750
751 struct stab_print stab_print[] = {
752 #define __define_stab(NAME, CODE, STRING) {CODE, STRING},
753 #include "aout/stab.def"
754 #undef __define_stab
755   {0, ""}
756 };
757
758 void dump_section_stabs PARAMS ((bfd *abfd, char *stabsect_name,
759                                  char *strsect_name));
760
761 /* Dump the stabs sections from an object file that has a section that
762    uses Sun stabs encoding.  It has to use some hooks into BFD because
763    string table sections are not normally visible to BFD callers.  */
764
765 void
766 dump_stabs (abfd)
767      bfd *abfd;
768 {
769   /* Allocate and initialize stab name array if first time.  */
770   if (stab_name == NULL) 
771     {
772       int i;
773
774       stab_name = (char **) xmalloc (256 * sizeof(char *));
775       /* Clear the array. */
776       for (i = 0; i < 256; i++)
777         stab_name[i] = NULL;
778       /* Fill in the defined stabs. */
779       for (i = 0; *stab_print[i].string; i++)
780         stab_name[stab_print[i].value] = stab_print[i].string;
781     }
782
783   dump_section_stabs (abfd, ".stab", ".stabstr");
784   dump_section_stabs (abfd, ".stab.excl", ".stab.exclstr");
785   dump_section_stabs (abfd, ".stab.index", ".stab.indexstr");
786   dump_section_stabs (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
787 }
788
789 static struct internal_nlist *stabs;
790 static bfd_size_type stab_size;
791
792 static char *strtab;
793 static bfd_size_type stabstr_size;
794
795 /* Read ABFD's stabs section STABSECT_NAME into `stabs'
796    and string table section STRSECT_NAME into `strtab'.
797    If the section exists and was read, allocate the space and return true.
798    Otherwise return false.  */
799
800 boolean
801 read_section_stabs (abfd, stabsect_name, strsect_name)
802      bfd *abfd;
803      char *stabsect_name;
804      char *strsect_name;
805 {
806   asection *stabsect, *stabstrsect;
807
808   stabsect = bfd_get_section_by_name (abfd, stabsect_name);
809   if (0 == stabsect)
810     {
811       printf ("No %s section present\n\n", stabsect_name);
812       return false;
813     }
814
815   stabstrsect = bfd_get_section_by_name (abfd, strsect_name);
816   if (0 == stabstrsect)
817     {
818       fprintf (stderr, "%s: %s has no %s section\n", program_name,
819                bfd_get_filename (abfd), strsect_name);
820       return false;
821     }
822  
823   stab_size    = bfd_section_size (abfd, stabsect);
824   stabstr_size = bfd_section_size (abfd, stabstrsect);
825
826   stabs  = (struct internal_nlist *) xmalloc (stab_size);
827   strtab = (char *) xmalloc (stabstr_size);
828   
829   if (! bfd_get_section_contents (abfd, stabsect, (PTR) stabs, 0, stab_size))
830     {
831       fprintf (stderr, "%s: Reading %s section of %s failed: %s\n",
832                program_name, stabsect_name, bfd_get_filename (abfd),
833                bfd_errmsg (bfd_get_error ()));
834       free (stabs);
835       free (strtab);
836       return false;
837     }
838
839   if (! bfd_get_section_contents (abfd, stabstrsect, (PTR) strtab, 0,
840                                   stabstr_size))
841     {
842       fprintf (stderr, "%s: Reading %s section of %s failed: %s\n",
843                program_name, strsect_name, bfd_get_filename (abfd),
844                bfd_errmsg (bfd_get_error ()));
845       free (stabs);
846       free (strtab);
847       return false;
848     }
849
850   return true;
851 }
852
853 #define SWAP_SYMBOL(symp, abfd) \
854 { \
855     (symp)->n_strx = bfd_h_get_32(abfd,                 \
856                                   (unsigned char *)&(symp)->n_strx);    \
857     (symp)->n_desc = bfd_h_get_16 (abfd,                        \
858                                    (unsigned char *)&(symp)->n_desc);   \
859     (symp)->n_value = bfd_h_get_32 (abfd,                       \
860                                     (unsigned char *)&(symp)->n_value); \
861 }
862
863 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
864    using string table section STRSECT_NAME (in `strtab').  */
865
866 void
867 print_section_stabs (abfd, stabsect_name, strsect_name)
868      bfd *abfd;
869      char *stabsect_name;
870      char *strsect_name;
871 {
872   int i;
873   unsigned file_string_table_offset = 0, next_file_string_table_offset = 0;
874   struct internal_nlist *stabp = stabs,
875   *stabs_end = (struct internal_nlist *) (stab_size + (char *) stabs);
876
877   printf ("Contents of %s section:\n\n", stabsect_name);
878   printf ("Symnum n_type n_othr n_desc n_value  n_strx String\n");
879
880   /* Loop through all symbols and print them.
881
882      We start the index at -1 because there is a dummy symbol on
883      the front of stabs-in-{coff,elf} sections that supplies sizes.  */
884
885   for (i = -1; stabp < stabs_end; stabp++, i++)
886     {
887       SWAP_SYMBOL (stabp, abfd);
888       printf ("\n%-6d ", i);
889       /* Either print the stab name, or, if unnamed, print its number
890          again (makes consistent formatting for tools like awk). */
891       if (stab_name[stabp->n_type])
892         printf ("%-6s", stab_name[stabp->n_type]);
893       else if (stabp->n_type == N_UNDF)
894         printf ("HdrSym");
895       else
896         printf ("%-6d", stabp->n_type);
897       printf (" %-6d %-6d ", stabp->n_other, stabp->n_desc);
898       printf_vma (stabp->n_value);
899       printf (" %-6lu", stabp->n_strx);
900
901       /* Symbols with type == 0 (N_UNDF) specify the length of the
902          string table associated with this file.  We use that info
903          to know how to relocate the *next* file's string table indices.  */
904
905       if (stabp->n_type == N_UNDF)
906         {
907           file_string_table_offset = next_file_string_table_offset;
908           next_file_string_table_offset += stabp->n_value;
909         }
910       else
911         {
912           /* Using the (possibly updated) string table offset, print the
913              string (if any) associated with this symbol.  */
914
915           if ((stabp->n_strx + file_string_table_offset) < stabstr_size)
916             printf (" %s", &strtab[stabp->n_strx + file_string_table_offset]);
917           else
918             printf (" *");
919         }
920     }
921   printf ("\n\n");
922 }
923
924 void
925 dump_section_stabs (abfd, stabsect_name, strsect_name)
926      bfd *abfd;
927      char *stabsect_name;
928      char *strsect_name;
929 {
930   if (read_section_stabs (abfd, stabsect_name, strsect_name))
931     {
932       print_section_stabs (abfd, stabsect_name, strsect_name);
933       free (stabs);
934       free (strtab);
935     }
936 }
937 \f
938 static void
939 dump_bfd_header (abfd)
940      bfd *abfd;
941 {
942   char *comma = "";
943
944   printf ("architecture: %s, ",
945           bfd_printable_arch_mach (bfd_get_arch (abfd),
946                                    bfd_get_mach (abfd)));
947   printf ("flags 0x%08x:\n", abfd->flags);
948
949 #define PF(x, y)    if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
950   PF (HAS_RELOC, "HAS_RELOC");
951   PF (EXEC_P, "EXEC_P");
952   PF (HAS_LINENO, "HAS_LINENO");
953   PF (HAS_DEBUG, "HAS_DEBUG");
954   PF (HAS_SYMS, "HAS_SYMS");
955   PF (HAS_LOCALS, "HAS_LOCALS");
956   PF (DYNAMIC, "DYNAMIC");
957   PF (WP_TEXT, "WP_TEXT");
958   PF (D_PAGED, "D_PAGED");
959   PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
960   printf ("\nstart address 0x");
961   printf_vma (abfd->start_address);
962 }
963
964 static void
965 display_bfd (abfd)
966      bfd *abfd;
967 {
968   char **matching;
969
970   if (!bfd_check_format_matches (abfd, bfd_object, &matching))
971     {
972       bfd_nonfatal (bfd_get_filename (abfd));
973       if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
974         {
975           list_matching_formats (matching);
976           free (matching);
977         }
978       return;
979     }
980
981   printf ("\n%s:     file format %s\n", bfd_get_filename (abfd),
982           abfd->xvec->name);
983   if (dump_ar_hdrs)
984     print_arelt_descr (stdout, abfd, true);
985   if (dump_file_header)
986     dump_bfd_header (abfd);
987   putchar ('\n');
988   if (dump_section_headers)
989     dump_headers (abfd);
990   if (dump_symtab || dump_reloc_info || disassemble)
991     {
992       syms = slurp_symtab (abfd);
993     }
994   if (dump_dynamic_symtab || dump_dynamic_reloc_info)
995     {
996       dynsyms = slurp_dynamic_symtab (abfd);
997     }
998   if (dump_symtab)
999     dump_symbols (abfd, false);
1000   if (dump_dynamic_symtab)
1001     dump_symbols (abfd, true);
1002   if (dump_stab_section_info)
1003     dump_stabs (abfd);
1004   if (dump_reloc_info && ! disassemble)
1005     dump_relocs (abfd);
1006   if (dump_dynamic_reloc_info)
1007     dump_dynamic_relocs (abfd);
1008   if (dump_section_contents)
1009     dump_data (abfd);
1010   /* Note that disassemble_data re-orders the syms table, but that is
1011      safe - as long as it is done last! */
1012   if (disassemble)
1013     disassemble_data (abfd);
1014 }
1015
1016 static void
1017 display_file (filename, target)
1018      char *filename;
1019      char *target;
1020 {
1021   bfd *file, *arfile = (bfd *) NULL;
1022
1023   file = bfd_openr (filename, target);
1024   if (file == NULL)
1025     {
1026       bfd_nonfatal (filename);
1027       return;
1028     }
1029
1030   if (bfd_check_format (file, bfd_archive) == true)
1031     {
1032       bfd *last_arfile = NULL;
1033
1034       printf ("In archive %s:\n", bfd_get_filename (file));
1035       for (;;)
1036         {
1037           bfd_set_error (bfd_error_no_error);
1038
1039           arfile = bfd_openr_next_archived_file (file, arfile);
1040           if (arfile == NULL)
1041             {
1042               if (bfd_get_error () != bfd_error_no_more_archived_files)
1043                 {
1044                   bfd_nonfatal (bfd_get_filename (file));
1045                 }
1046               break;
1047             }
1048
1049           display_bfd (arfile);
1050
1051           if (last_arfile != NULL)
1052             bfd_close (last_arfile);
1053           last_arfile = arfile;
1054         }
1055
1056       if (last_arfile != NULL)
1057         bfd_close (last_arfile);
1058     }
1059   else
1060     display_bfd (file);
1061
1062   bfd_close (file);
1063 }
1064 \f
1065 /* Actually display the various requested regions */
1066
1067 static void
1068 dump_data (abfd)
1069      bfd *abfd;
1070 {
1071   asection *section;
1072   bfd_byte *data = 0;
1073   bfd_size_type datasize = 0;
1074   bfd_size_type i;
1075
1076   for (section = abfd->sections; section != NULL; section =
1077        section->next)
1078     {
1079       int onaline = 16;
1080
1081       if (only == (char *) NULL ||
1082           strcmp (only, section->name) == 0)
1083         {
1084           if (section->flags & SEC_HAS_CONTENTS)
1085             {
1086               printf ("Contents of section %s:\n", section->name);
1087
1088               if (bfd_section_size (abfd, section) == 0)
1089                 continue;
1090               data = (bfd_byte *) xmalloc ((size_t) bfd_section_size (abfd, section));
1091               datasize = bfd_section_size (abfd, section);
1092
1093
1094               bfd_get_section_contents (abfd, section, (PTR) data, 0, bfd_section_size (abfd, section));
1095
1096               for (i = 0; i < bfd_section_size (abfd, section); i += onaline)
1097                 {
1098                   bfd_size_type j;
1099
1100                   printf (" %04lx ", (unsigned long int) (i + section->vma));
1101                   for (j = i; j < i + onaline; j++)
1102                     {
1103                       if (j < bfd_section_size (abfd, section))
1104                         printf ("%02x", (unsigned) (data[j]));
1105                       else
1106                         printf ("  ");
1107                       if ((j & 3) == 3)
1108                         printf (" ");
1109                     }
1110
1111                   printf (" ");
1112                   for (j = i; j < i + onaline; j++)
1113                     {
1114                       if (j >= bfd_section_size (abfd, section))
1115                         printf (" ");
1116                       else
1117                         printf ("%c", isprint (data[j]) ? data[j] : '.');
1118                     }
1119                   putchar ('\n');
1120                 }
1121               free (data);
1122             }
1123         }
1124     }
1125 }
1126
1127 /* Should perhaps share code and display with nm? */
1128 static void
1129 dump_symbols (abfd, dynamic)
1130      bfd *abfd;
1131      boolean dynamic;
1132 {
1133   asymbol **current;
1134   long max;
1135   long count;
1136
1137   if (dynamic)
1138     {
1139       current = dynsyms;
1140       max = dynsymcount;
1141       if (max == 0)
1142         return;
1143       printf ("DYNAMIC SYMBOL TABLE:\n");
1144     }
1145   else
1146     {
1147       current = syms;
1148       max = symcount;
1149       if (max == 0)
1150         return;
1151       printf ("SYMBOL TABLE:\n");
1152     }
1153
1154   for (count = 0; count < max; count++)
1155     {
1156       if (*current)
1157         {
1158           bfd *cur_bfd = bfd_asymbol_bfd(*current);
1159           if (cur_bfd)
1160             {
1161               bfd_print_symbol (cur_bfd,
1162                                 stdout,
1163                                 *current, bfd_print_symbol_all);
1164               printf ("\n");
1165             }
1166         }
1167       current++;
1168     }
1169   printf ("\n");
1170   printf ("\n");
1171 }
1172
1173 static void
1174 dump_relocs (abfd)
1175      bfd *abfd;
1176 {
1177   arelent **relpp;
1178   long relcount;
1179   asection *a;
1180
1181   for (a = abfd->sections; a != (asection *) NULL; a = a->next)
1182     {
1183       long relsize;
1184
1185       if (bfd_is_abs_section (a))
1186         continue;
1187       if (bfd_is_und_section (a))
1188         continue;
1189       if (bfd_is_com_section (a))
1190         continue;
1191
1192       if (only)
1193         {
1194           if (strcmp (only, a->name))
1195             continue;
1196         }
1197       else if ((a->flags & SEC_RELOC) == 0)
1198         continue;
1199
1200       printf ("RELOCATION RECORDS FOR [%s]:", a->name);
1201
1202       relsize = bfd_get_reloc_upper_bound (abfd, a);
1203       if (relsize < 0)
1204         bfd_fatal (bfd_get_filename (abfd));
1205
1206       if (relsize == 0)
1207         {
1208           printf (" (none)\n\n");
1209         }
1210       else
1211         {
1212           relpp = (arelent **) xmalloc (relsize);
1213           /* Note that this must be done *before* we sort the syms table. */
1214           relcount = bfd_canonicalize_reloc (abfd, a, relpp, syms);
1215           if (relcount < 0)
1216             bfd_fatal (bfd_get_filename (abfd));
1217           else if (relcount == 0)
1218             {
1219               printf (" (none)\n\n");
1220             }
1221           else
1222             {
1223               printf ("\n");
1224               dump_reloc_set (abfd, relpp, relcount);
1225               printf ("\n\n");
1226             }
1227           free (relpp);
1228         }
1229     }
1230 }
1231
1232 static void
1233 dump_dynamic_relocs (abfd)
1234      bfd *abfd;
1235 {
1236   long relsize;
1237   arelent **relpp;
1238   long relcount;
1239
1240   printf ("DYNAMIC RELOCATION RECORDS");
1241
1242   relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
1243   if (relsize < 0)
1244     bfd_fatal (bfd_get_filename (abfd));
1245
1246   if (relsize == 0)
1247     {
1248       printf (" (none)\n\n");
1249     }
1250   else
1251     {
1252       relpp = (arelent **) xmalloc (relsize);
1253       relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
1254       if (relcount < 0)
1255         bfd_fatal (bfd_get_filename (abfd));
1256       else if (relcount == 0)
1257         {
1258           printf (" (none)\n\n");
1259         }
1260       else
1261         {
1262           printf ("\n");
1263           dump_reloc_set (abfd, relpp, relcount);
1264           printf ("\n\n");
1265         }
1266       free (relpp);
1267     }
1268 }
1269
1270 static void
1271 dump_reloc_set (abfd, relpp, relcount)
1272      bfd *abfd;
1273      arelent **relpp;
1274      long relcount;
1275 {
1276   arelent **p;
1277
1278   /* Get column headers lined up reasonably.  */
1279   {
1280     static int width;
1281     if (width == 0)
1282       {
1283         char buf[30];
1284         sprintf_vma (buf, (bfd_vma) -1);
1285         width = strlen (buf) - 7;
1286       }
1287     printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
1288   }
1289
1290   for (p = relpp; relcount && *p != (arelent *) NULL; p++, relcount--)
1291     {
1292       arelent *q = *p;
1293       CONST char *sym_name;
1294       CONST char *section_name;
1295
1296       if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
1297         {
1298           sym_name = (*(q->sym_ptr_ptr))->name;
1299           section_name = (*(q->sym_ptr_ptr))->section->name;
1300         }
1301       else
1302         {
1303           sym_name = NULL;
1304           section_name = NULL;
1305         }
1306       if (sym_name)
1307         {
1308           printf_vma (q->address);
1309           printf (" %-16s  %s",
1310                   q->howto->name,
1311                   sym_name);
1312         }
1313       else
1314         {
1315           if (section_name == (CONST char *) NULL)
1316             section_name = "*unknown*";
1317           printf_vma (q->address);
1318           printf (" %-16s  [%s]",
1319                   q->howto->name,
1320                   section_name);
1321         }
1322       if (q->addend)
1323         {
1324           printf ("+0x");
1325           printf_vma (q->addend);
1326         }
1327       printf ("\n");
1328     }
1329 }
1330 \f
1331 /* The length of the longest architecture name + 1.  */
1332 #define LONGEST_ARCH sizeof("rs6000:6000")
1333
1334 /* List the targets that BFD is configured to support, each followed
1335    by its endianness and the architectures it supports.  */
1336
1337 static void
1338 display_target_list ()
1339 {
1340   extern char *tmpnam ();
1341   extern bfd_target *bfd_target_vector[];
1342   char *dummy_name;
1343   int t;
1344
1345   dummy_name = tmpnam ((char *) NULL);
1346   for (t = 0; bfd_target_vector[t]; t++)
1347     {
1348       bfd_target *p = bfd_target_vector[t];
1349       bfd *abfd = bfd_openw (dummy_name, p->name);
1350       int a;
1351
1352       printf ("%s\n (header %s, data %s)\n", p->name,
1353               p->header_byteorder_big_p ? "big endian" : "little endian",
1354               p->byteorder_big_p ? "big endian" : "little endian");
1355
1356       if (abfd == NULL)
1357         {
1358           bfd_nonfatal (dummy_name);
1359           continue;
1360         }
1361
1362       if (! bfd_set_format (abfd, bfd_object))
1363         {
1364           if (bfd_get_error () != bfd_error_invalid_operation)
1365             bfd_nonfatal (p->name);
1366           continue;
1367         }
1368
1369       for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
1370         if (bfd_set_arch_mach (abfd, (enum bfd_architecture) a, 0))
1371           printf ("  %s\n",
1372                   bfd_printable_arch_mach ((enum bfd_architecture) a, 0));
1373     }
1374   unlink (dummy_name);
1375 }
1376
1377 /* Print a table showing which architectures are supported for entries
1378    FIRST through LAST-1 of bfd_target_vector (targets across,
1379    architectures down).  */
1380
1381 static void
1382 display_info_table (first, last)
1383      int first;
1384      int last;
1385 {
1386   extern bfd_target *bfd_target_vector[];
1387   extern char *tmpnam ();
1388   int t, a;
1389   char *dummy_name;
1390
1391   /* Print heading of target names.  */
1392   printf ("\n%*s", (int) LONGEST_ARCH, " ");
1393   for (t = first; t < last && bfd_target_vector[t]; t++)
1394     printf ("%s ", bfd_target_vector[t]->name);
1395   putchar ('\n');
1396
1397   dummy_name = tmpnam ((char *) NULL);
1398   for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
1399     if (strcmp (bfd_printable_arch_mach (a, 0), "UNKNOWN!") != 0)
1400       {
1401         printf ("%*s ", (int) LONGEST_ARCH - 1,
1402                 bfd_printable_arch_mach (a, 0));
1403         for (t = first; t < last && bfd_target_vector[t]; t++)
1404           {
1405             bfd_target *p = bfd_target_vector[t];
1406             boolean ok = true;
1407             bfd *abfd = bfd_openw (dummy_name, p->name);
1408
1409             if (abfd == NULL)
1410               {
1411                 bfd_nonfatal (p->name);
1412                 ok = false;
1413               }
1414
1415             if (ok)
1416               {
1417                 if (! bfd_set_format (abfd, bfd_object))
1418                   {
1419                     if (bfd_get_error () != bfd_error_invalid_operation)
1420                       bfd_nonfatal (p->name);
1421                     ok = false;
1422                   }
1423               }
1424
1425             if (ok)
1426               {
1427                 if (! bfd_set_arch_mach (abfd, a, 0))
1428                   ok = false;
1429               }
1430
1431             if (ok)
1432               printf ("%s ", p->name);
1433             else
1434               {
1435                 int l = strlen (p->name);
1436                 while (l--)
1437                   putchar ('-');
1438                 putchar (' ');
1439               }
1440           }
1441         putchar ('\n');
1442       }
1443   unlink (dummy_name);
1444 }
1445
1446 /* Print tables of all the target-architecture combinations that
1447    BFD has been configured to support.  */
1448
1449 static void
1450 display_target_tables ()
1451 {
1452   int t, columns;
1453   extern bfd_target *bfd_target_vector[];
1454   char *colum;
1455   extern char *getenv ();
1456
1457   columns = 0;
1458   colum = getenv ("COLUMNS");
1459   if (colum != NULL)
1460     columns = atoi (colum);
1461   if (columns == 0)
1462     columns = 80;
1463
1464   t = 0;
1465   while (bfd_target_vector[t] != NULL)
1466     {
1467       int oldt = t, wid;
1468
1469       wid = LONGEST_ARCH + strlen (bfd_target_vector[t]->name) + 1;
1470       ++t;
1471       while (wid < columns && bfd_target_vector[t] != NULL)
1472         {
1473           int newwid;
1474
1475           newwid = wid + strlen (bfd_target_vector[t]->name) + 1;
1476           if (newwid >= columns)
1477             break;
1478           wid = newwid;
1479           ++t;
1480         }
1481       display_info_table (oldt, t);
1482     }
1483 }
1484
1485 static void
1486 display_info ()
1487 {
1488   printf ("BFD header file version %s\n", BFD_VERSION);
1489   display_target_list ();
1490   display_target_tables ();
1491 }
1492
1493 int
1494 main (argc, argv)
1495      int argc;
1496      char **argv;
1497 {
1498   int c;
1499   char *target = default_target;
1500   boolean seenflag = false;
1501
1502   program_name = *argv;
1503   xmalloc_set_program_name (program_name);
1504
1505   bfd_init ();
1506
1507   while ((c = getopt_long (argc, argv, "ib:m:VdDlfahrRtTxsj:", long_options,
1508                            (int *) 0))
1509          != EOF)
1510     {
1511       seenflag = true;
1512       switch (c)
1513         {
1514         case 0:
1515           break;                /* we've been given a long option */
1516         case 'm':
1517           machine = optarg;
1518           break;
1519         case 'j':
1520           only = optarg;
1521           break;
1522         case 'l':
1523           with_line_numbers = 1;
1524           break;
1525         case 'b':
1526           target = optarg;
1527           break;
1528         case 'f':
1529           dump_file_header = true;
1530           break;
1531         case 'i':
1532           formats_info = true;
1533           break;
1534         case 'x':
1535           dump_symtab = 1;
1536           dump_reloc_info = 1;
1537           dump_file_header = true;
1538           dump_ar_hdrs = 1;
1539           dump_section_headers = 1;
1540           break;
1541         case 't':
1542           dump_symtab = 1;
1543           break;
1544         case 'T':
1545           dump_dynamic_symtab = 1;
1546           break;
1547         case 'd':
1548           disassemble = true;
1549           break;
1550         case 'D':
1551           disassemble = disassemble_all = true;
1552           break;
1553         case 's':
1554           dump_section_contents = 1;
1555           break;
1556         case 'r':
1557           dump_reloc_info = 1;
1558           break;
1559         case 'R':
1560           dump_dynamic_reloc_info = 1;
1561           break;
1562         case 'a':
1563           dump_ar_hdrs = 1;
1564           break;
1565         case 'h':
1566           dump_section_headers = 1;
1567           break;
1568         case 'H':
1569           usage (stdout, 0);
1570         case 'V':
1571           show_version = 1;
1572           break;
1573         default:
1574           usage (stderr, 1);
1575         }
1576     }
1577
1578   if (show_version)
1579     {
1580       printf ("GNU %s version %s\n", program_name, program_version);
1581       exit (0);
1582     }
1583
1584   if (seenflag == false)
1585     usage (stderr, 1);
1586
1587   if (formats_info)
1588     {
1589       display_info ();
1590     }
1591   else
1592     {
1593       if (optind == argc)
1594         display_file ("a.out", target);
1595       else
1596         for (; optind < argc;)
1597           display_file (argv[optind++], target);
1598     }
1599   return 0;
1600 }