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