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