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