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