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