* objdump.c (dump_section_stabs): Fix test for stabs sections
[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 extern char *program_version;
48
49 static int show_version = 0;            /* show the version number */
50 static int dump_section_contents;       /* -s */
51 static int dump_section_headers;        /* -h */
52 static boolean dump_file_header;        /* -f */
53 static int dump_symtab;                 /* -t */
54 static int dump_dynamic_symtab;         /* -T */
55 static int dump_reloc_info;             /* -r */
56 static int dump_dynamic_reloc_info;     /* -R */
57 static int dump_ar_hdrs;                /* -a */
58 static int dump_private_headers;        /* -p */
59 static int with_line_numbers;           /* -l */
60 static boolean with_source_code;        /* -S */
61 static int show_raw_insn;               /* --show-raw-insn */
62 static int dump_stab_section_info;      /* --stabs */
63 static boolean disassemble;             /* -d */
64 static boolean disassemble_all;         /* -D */
65 static boolean formats_info;            /* -i */
66 static char *only;                      /* -j secname */
67 static int wide_output;                 /* -w */
68 static bfd_vma start_address = (bfd_vma) -1; /* --start-address */
69 static bfd_vma stop_address = (bfd_vma) -1;  /* --stop-address */
70 static 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 static char *machine = (char *) NULL;
81
82 /* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN.  */
83 static enum bfd_endian endian = BFD_ENDIAN_UNKNOWN;
84
85 /* The symbol table.  */
86 static asymbol **syms;
87
88 /* Number of symbols in `syms'.  */
89 static long symcount = 0;
90
91 /* The sorted symbol table.  */
92 static asymbol **sorted_syms;
93
94 /* Number of symbols in `sorted_syms'.  */
95 static long sorted_symcount = 0;
96
97 /* The dynamic symbol table.  */
98 static asymbol **dynsyms;
99
100 /* Number of symbols in `dynsyms'.  */
101 static long dynsymcount = 0;
102
103 /* Forward declarations.  */
104
105 static void
106 display_file PARAMS ((char *filename, char *target));
107
108 static void
109 dump_data PARAMS ((bfd *abfd));
110
111 static void
112 dump_relocs PARAMS ((bfd *abfd));
113
114 static void
115 dump_dynamic_relocs PARAMS ((bfd * abfd));
116
117 static void
118 dump_reloc_set PARAMS ((bfd *, asection *, arelent **, long));
119
120 static void
121 dump_symbols PARAMS ((bfd *abfd, boolean dynamic));
122
123 static void
124 display_bfd PARAMS ((bfd *abfd));
125
126 static void
127 objdump_print_value PARAMS ((bfd_vma, struct disassemble_info *));
128
129 static void
130 objdump_print_address PARAMS ((bfd_vma, struct disassemble_info *));
131
132 static void
133 show_line PARAMS ((bfd *, asection *, bfd_vma));
134
135 static const char *
136 endian_string PARAMS ((enum bfd_endian));
137 \f
138 static void
139 usage (stream, status)
140      FILE *stream;
141      int status;
142 {
143   fprintf (stream, "\
144 Usage: %s [-ahifdDprRtTxsSlw] [-b bfdname] [-m machine] [-j section-name]\n\
145        [--archive-headers] [--target=bfdname] [--debugging] [--disassemble]\n\
146        [--disassemble-all] [--file-headers] [--section-headers] [--headers]\n\
147        [--info] [--section=section-name] [--line-numbers] [--source]\n",
148            program_name);
149   fprintf (stream, "\
150        [--architecture=machine] [--reloc] [--full-contents] [--stabs]\n\
151        [--syms] [--all-headers] [--dynamic-syms] [--dynamic-reloc]\n\
152        [--wide] [--version] [--help] [--private-headers]\n\
153        [--start-address=addr] [--stop-address=addr]\n\
154        [--show-raw-insn] [-EB|-EL] [--endian={big|little}] objfile...\n\
155 at least one option besides -l (--line-numbers) must be given\n");
156   list_supported_targets (program_name, stream);
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.arch = bfd_get_arch (abfd);
978   disasm_info.mach = bfd_get_mach (abfd);
979   if (bfd_big_endian (abfd))
980     disasm_info.endian = BFD_ENDIAN_BIG;
981   else if (bfd_little_endian (abfd))
982     disasm_info.endian = BFD_ENDIAN_LITTLE;
983   else
984     /* ??? Aborting here seems too drastic.  We could default to big or little
985        instead.  */
986     disasm_info.endian = BFD_ENDIAN_UNKNOWN;
987
988   for (section = abfd->sections;
989        section != (asection *) NULL;
990        section = section->next)
991     {
992       bfd_byte *data = NULL;
993       bfd_size_type datasize = 0;
994       arelent **relbuf = NULL;
995       arelent **relpp = NULL;
996       arelent **relppend = NULL;
997       long stop;
998
999       if ((section->flags & SEC_LOAD) == 0
1000           || (! disassemble_all
1001               && only == NULL
1002               && (section->flags & SEC_CODE) == 0))
1003         continue;
1004       if (only != (char *) NULL && strcmp (only, section->name) != 0)
1005         continue;
1006
1007       if (dump_reloc_info
1008           && (section->flags & SEC_RELOC) != 0)
1009         {
1010           long relsize;
1011
1012           relsize = bfd_get_reloc_upper_bound (abfd, section);
1013           if (relsize < 0)
1014             bfd_fatal (bfd_get_filename (abfd));
1015
1016           if (relsize > 0)
1017             {
1018               long relcount;
1019
1020               relbuf = (arelent **) xmalloc (relsize);
1021               relcount = bfd_canonicalize_reloc (abfd, section, relbuf, syms);
1022               if (relcount < 0)
1023                 bfd_fatal (bfd_get_filename (abfd));
1024
1025               /* Sort the relocs by address.  */
1026               qsort (relbuf, relcount, sizeof (arelent *), compare_relocs);
1027
1028               relpp = relbuf;
1029               relppend = relpp + relcount;
1030             }
1031         }
1032
1033       printf ("Disassembly of section %s:\n", section->name);
1034
1035       datasize = bfd_get_section_size_before_reloc (section);
1036       if (datasize == 0)
1037         continue;
1038
1039       data = (bfd_byte *) xmalloc ((size_t) datasize);
1040
1041       bfd_get_section_contents (abfd, section, data, 0, datasize);
1042
1043       aux.sec = section;
1044       disasm_info.buffer = data;
1045       disasm_info.buffer_vma = section->vma;
1046       disasm_info.buffer_length = datasize;
1047       if (start_address == (bfd_vma) -1
1048           || start_address < disasm_info.buffer_vma)
1049         i = 0;
1050       else
1051         i = start_address - disasm_info.buffer_vma;
1052       if (stop_address == (bfd_vma) -1)
1053         stop = datasize;
1054       else
1055         {
1056           if (stop_address < disasm_info.buffer_vma)
1057             stop = 0;
1058           else
1059             stop = stop_address - disasm_info.buffer_vma;
1060           if (stop > disasm_info.buffer_length)
1061             stop = disasm_info.buffer_length;
1062         }
1063       while (i < stop)
1064         {
1065           int bytes;
1066           boolean need_nl = false;
1067
1068           if (data[i] == 0
1069               && (i + 1 >= stop
1070                   || (data[i + 1] == 0
1071                       && (i + 2 >= stop
1072                           || (data[i + 2] == 0
1073                               && (i + 3 >= stop
1074                                   || data[i + 3] == 0))))))
1075             {
1076               if (done_dot == false)
1077                 {
1078                   printf ("...\n");
1079                   done_dot = true;
1080                 }
1081               bytes = 4;
1082             }
1083           else
1084             {
1085               done_dot = false;
1086               if (with_line_numbers || with_source_code)
1087                 show_line (abfd, section, i);
1088               aux.require_sec = true;
1089               objdump_print_address (section->vma + i, &disasm_info);
1090               aux.require_sec = false;
1091               putchar (' ');
1092
1093               sfile.buffer = sfile.current = buf;
1094               disasm_info.fprintf_func = (fprintf_ftype) objdump_sprintf;
1095               disasm_info.stream = (FILE *) &sfile;
1096               bytes = (*disassemble_fn) (section->vma + i, &disasm_info);
1097               disasm_info.fprintf_func = (fprintf_ftype) fprintf;
1098               disasm_info.stream = stdout;
1099               if (bytes < 0)
1100                 break;
1101
1102               if (show_raw_insn)
1103                 {
1104                   long j;
1105                   for (j = i; j < i + bytes; ++j)
1106                     {
1107                       printf ("%02x", (unsigned) data[j]);
1108                       putchar (' ');
1109                     }
1110                   /* Separate raw data from instruction by extra space.  */
1111                   putchar (' ');
1112                 }
1113
1114               printf ("%s", sfile.buffer);
1115
1116               if (!wide_output)
1117                 putchar ('\n');
1118               else
1119                 need_nl = true;
1120             }
1121
1122           if (dump_reloc_info
1123               && (section->flags & SEC_RELOC) != 0)
1124             {
1125               while (relpp < relppend
1126                      && ((*relpp)->address >= (bfd_vma) i
1127                          && (*relpp)->address < (bfd_vma) i + bytes))
1128                 {
1129                   arelent *q;
1130                   const char *sym_name;
1131
1132                   q = *relpp;
1133
1134                   printf ("\t\tRELOC: ");
1135
1136                   printf_vma (section->vma + q->address);
1137
1138                   printf (" %s ", q->howto->name);
1139
1140                   if (q->sym_ptr_ptr != NULL
1141                       && *q->sym_ptr_ptr != NULL)
1142                     {
1143                       sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
1144                       if (sym_name == NULL || *sym_name == '\0')
1145                         {
1146                           asection *sym_sec;
1147
1148                           sym_sec = bfd_get_section (*q->sym_ptr_ptr);
1149                           sym_name = bfd_get_section_name (abfd, sym_sec);
1150                           if (sym_name == NULL || *sym_name == '\0')
1151                             sym_name = "*unknown*";
1152                         }
1153                     }
1154                   else
1155                     sym_name = "*unknown*";
1156
1157                   printf ("%s", sym_name);
1158
1159                   if (q->addend)
1160                     {
1161                       printf ("+0x");
1162                       printf_vma (q->addend);
1163                     }
1164
1165                   printf ("\n");
1166                   need_nl = false;
1167                   ++relpp;
1168                 }
1169             }
1170
1171           if (need_nl)
1172             printf ("\n");
1173
1174           i += bytes;
1175         }
1176
1177       free (data);
1178       if (relbuf != NULL)
1179         free (relbuf);
1180     }
1181   free (sorted_syms);
1182 }
1183 \f
1184
1185 /* Define a table of stab values and print-strings.  We wish the initializer
1186    could be a direct-mapped table, but instead we build one the first
1187    time we need it.  */
1188
1189 static void dump_section_stabs PARAMS ((bfd *abfd, char *stabsect_name,
1190                                         char *strsect_name));
1191
1192 /* Dump the stabs sections from an object file that has a section that
1193    uses Sun stabs encoding.  */
1194
1195 static void
1196 dump_stabs (abfd)
1197      bfd *abfd;
1198 {
1199   dump_section_stabs (abfd, ".stab", ".stabstr");
1200   dump_section_stabs (abfd, ".stab.excl", ".stab.exclstr");
1201   dump_section_stabs (abfd, ".stab.index", ".stab.indexstr");
1202   dump_section_stabs (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
1203 }
1204
1205 static bfd_byte *stabs;
1206 static bfd_size_type stab_size;
1207
1208 static char *strtab;
1209 static bfd_size_type stabstr_size;
1210
1211 /* Read ABFD's stabs section STABSECT_NAME into `stabs'
1212    and string table section STRSECT_NAME into `strtab'.
1213    If the section exists and was read, allocate the space and return true.
1214    Otherwise return false.  */
1215
1216 static boolean
1217 read_section_stabs (abfd, stabsect_name, strsect_name)
1218      bfd *abfd;
1219      char *stabsect_name;
1220      char *strsect_name;
1221 {
1222   asection *stabsect, *stabstrsect;
1223
1224   stabsect = bfd_get_section_by_name (abfd, stabsect_name);
1225   if (0 == stabsect)
1226     {
1227       printf ("No %s section present\n\n", stabsect_name);
1228       return false;
1229     }
1230
1231   stabstrsect = bfd_get_section_by_name (abfd, strsect_name);
1232   if (0 == stabstrsect)
1233     {
1234       fprintf (stderr, "%s: %s has no %s section\n", program_name,
1235                bfd_get_filename (abfd), strsect_name);
1236       return false;
1237     }
1238  
1239   stab_size    = bfd_section_size (abfd, stabsect);
1240   stabstr_size = bfd_section_size (abfd, stabstrsect);
1241
1242   stabs  = (bfd_byte *) xmalloc (stab_size);
1243   strtab = (char *) xmalloc (stabstr_size);
1244   
1245   if (! bfd_get_section_contents (abfd, stabsect, (PTR) stabs, 0, stab_size))
1246     {
1247       fprintf (stderr, "%s: Reading %s section of %s failed: %s\n",
1248                program_name, stabsect_name, bfd_get_filename (abfd),
1249                bfd_errmsg (bfd_get_error ()));
1250       free (stabs);
1251       free (strtab);
1252       return false;
1253     }
1254
1255   if (! bfd_get_section_contents (abfd, stabstrsect, (PTR) strtab, 0,
1256                                   stabstr_size))
1257     {
1258       fprintf (stderr, "%s: Reading %s section of %s failed: %s\n",
1259                program_name, strsect_name, bfd_get_filename (abfd),
1260                bfd_errmsg (bfd_get_error ()));
1261       free (stabs);
1262       free (strtab);
1263       return false;
1264     }
1265
1266   return true;
1267 }
1268
1269 /* Stabs entries use a 12 byte format:
1270      4 byte string table index
1271      1 byte stab type
1272      1 byte stab other field
1273      2 byte stab desc field
1274      4 byte stab value
1275    FIXME: This will have to change for a 64 bit object format.  */
1276
1277 #define STRDXOFF (0)
1278 #define TYPEOFF (4)
1279 #define OTHEROFF (5)
1280 #define DESCOFF (6)
1281 #define VALOFF (8)
1282 #define STABSIZE (12)
1283
1284 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
1285    using string table section STRSECT_NAME (in `strtab').  */
1286
1287 static void
1288 print_section_stabs (abfd, stabsect_name, strsect_name)
1289      bfd *abfd;
1290      char *stabsect_name;
1291      char *strsect_name;
1292 {
1293   int i;
1294   unsigned file_string_table_offset = 0, next_file_string_table_offset = 0;
1295   bfd_byte *stabp, *stabs_end;
1296
1297   stabp = stabs;
1298   stabs_end = stabp + stab_size;
1299
1300   printf ("Contents of %s section:\n\n", stabsect_name);
1301   printf ("Symnum n_type n_othr n_desc n_value  n_strx String\n");
1302
1303   /* Loop through all symbols and print them.
1304
1305      We start the index at -1 because there is a dummy symbol on
1306      the front of stabs-in-{coff,elf} sections that supplies sizes.  */
1307
1308   for (i = -1; stabp < stabs_end; stabp += STABSIZE, i++)
1309     {
1310       const char *name;
1311       unsigned long strx;
1312       unsigned char type, other;
1313       unsigned short desc;
1314       bfd_vma value;
1315
1316       strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
1317       type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
1318       other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
1319       desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
1320       value = bfd_h_get_32 (abfd, stabp + VALOFF);
1321
1322       printf ("\n%-6d ", i);
1323       /* Either print the stab name, or, if unnamed, print its number
1324          again (makes consistent formatting for tools like awk). */
1325       name = bfd_get_stab_name (type);
1326       if (name != NULL)
1327         printf ("%-6s", name);
1328       else if (type == N_UNDF)
1329         printf ("HdrSym");
1330       else
1331         printf ("%-6d", type);
1332       printf (" %-6d %-6d ", other, desc);
1333       printf_vma (value);
1334       printf (" %-6lu", strx);
1335
1336       /* Symbols with type == 0 (N_UNDF) specify the length of the
1337          string table associated with this file.  We use that info
1338          to know how to relocate the *next* file's string table indices.  */
1339
1340       if (type == N_UNDF)
1341         {
1342           file_string_table_offset = next_file_string_table_offset;
1343           next_file_string_table_offset += value;
1344         }
1345       else
1346         {
1347           /* Using the (possibly updated) string table offset, print the
1348              string (if any) associated with this symbol.  */
1349
1350           if ((strx + file_string_table_offset) < stabstr_size)
1351             printf (" %s", &strtab[strx + file_string_table_offset]);
1352           else
1353             printf (" *");
1354         }
1355     }
1356   printf ("\n\n");
1357 }
1358
1359 static void
1360 dump_section_stabs (abfd, stabsect_name, strsect_name)
1361      bfd *abfd;
1362      char *stabsect_name;
1363      char *strsect_name;
1364 {
1365   asection *s;
1366
1367   /* Check for section names for which stabsect_name is a prefix, to
1368      handle .stab0, etc.  */
1369   for (s = abfd->sections;
1370        s != NULL;
1371        s = s->next)
1372     {
1373       int len;
1374
1375       len = strlen (stabsect_name);
1376
1377 /* If the prefix matches, and the files section name ends with a nul or a digit,
1378    then we match.  Ie: we want either an exact match or a a section followed by 
1379    a number.  */
1380       if (strncmp (stabsect_name, s->name, len) == 0
1381           && (s->name[len] == '\000' || isdigit (s->name[len])))
1382         {
1383           if (read_section_stabs (abfd, s->name, strsect_name))
1384             {
1385               print_section_stabs (abfd, s->name, strsect_name);
1386               free (stabs);
1387               free (strtab);
1388             }
1389         }
1390     }
1391 }
1392 \f
1393 static void
1394 dump_bfd_header (abfd)
1395      bfd *abfd;
1396 {
1397   char *comma = "";
1398
1399   printf ("architecture: %s, ",
1400           bfd_printable_arch_mach (bfd_get_arch (abfd),
1401                                    bfd_get_mach (abfd)));
1402   printf ("flags 0x%08x:\n", abfd->flags);
1403
1404 #define PF(x, y)    if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
1405   PF (HAS_RELOC, "HAS_RELOC");
1406   PF (EXEC_P, "EXEC_P");
1407   PF (HAS_LINENO, "HAS_LINENO");
1408   PF (HAS_DEBUG, "HAS_DEBUG");
1409   PF (HAS_SYMS, "HAS_SYMS");
1410   PF (HAS_LOCALS, "HAS_LOCALS");
1411   PF (DYNAMIC, "DYNAMIC");
1412   PF (WP_TEXT, "WP_TEXT");
1413   PF (D_PAGED, "D_PAGED");
1414   PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
1415   printf ("\nstart address 0x");
1416   printf_vma (abfd->start_address);
1417   printf ("\n");
1418 }
1419 \f
1420 static void
1421 dump_bfd_private_header (abfd)
1422 bfd *abfd;
1423 {
1424   bfd_print_private_bfd_data (abfd, stdout);
1425 }
1426
1427 static void
1428 display_bfd (abfd)
1429      bfd *abfd;
1430 {
1431   char **matching;
1432
1433   if (!bfd_check_format_matches (abfd, bfd_object, &matching))
1434     {
1435       bfd_nonfatal (bfd_get_filename (abfd));
1436       if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1437         {
1438           list_matching_formats (matching);
1439           free (matching);
1440         }
1441       return;
1442     }
1443
1444   printf ("\n%s:     file format %s\n", bfd_get_filename (abfd),
1445           abfd->xvec->name);
1446   if (dump_ar_hdrs)
1447     print_arelt_descr (stdout, abfd, true);
1448   if (dump_file_header)
1449     dump_bfd_header (abfd);
1450   if (dump_private_headers)
1451     dump_bfd_private_header (abfd);
1452   putchar ('\n');
1453   if (dump_section_headers)
1454     dump_headers (abfd);
1455   if (dump_symtab || dump_reloc_info || disassemble || dump_debugging)
1456     {
1457       syms = slurp_symtab (abfd);
1458     }
1459   if (dump_dynamic_symtab || dump_dynamic_reloc_info)
1460     {
1461       dynsyms = slurp_dynamic_symtab (abfd);
1462     }
1463   if (dump_symtab)
1464     dump_symbols (abfd, false);
1465   if (dump_dynamic_symtab)
1466     dump_symbols (abfd, true);
1467   if (dump_stab_section_info)
1468     dump_stabs (abfd);
1469   if (dump_reloc_info && ! disassemble)
1470     dump_relocs (abfd);
1471   if (dump_dynamic_reloc_info)
1472     dump_dynamic_relocs (abfd);
1473   if (dump_section_contents)
1474     dump_data (abfd);
1475   if (disassemble)
1476     disassemble_data (abfd);
1477   if (dump_debugging)
1478     {
1479       PTR dhandle;
1480
1481       dhandle = read_debugging_info (abfd, syms, symcount);
1482       if (dhandle != NULL)
1483         {
1484           if (! print_debugging_info (stdout, dhandle))
1485             fprintf (stderr, "%s: printing debugging information failed\n",
1486                      bfd_get_filename (abfd));
1487         }
1488     }
1489   if (syms)
1490     {
1491       free (syms);
1492       syms = NULL;
1493     }
1494   if (dynsyms)
1495     {
1496       free (dynsyms);
1497       dynsyms = NULL;
1498     }
1499 }
1500
1501 static void
1502 display_file (filename, target)
1503      char *filename;
1504      char *target;
1505 {
1506   bfd *file, *arfile = (bfd *) NULL;
1507
1508   file = bfd_openr (filename, target);
1509   if (file == NULL)
1510     {
1511       bfd_nonfatal (filename);
1512       return;
1513     }
1514
1515   if (bfd_check_format (file, bfd_archive) == true)
1516     {
1517       bfd *last_arfile = NULL;
1518
1519       printf ("In archive %s:\n", bfd_get_filename (file));
1520       for (;;)
1521         {
1522           bfd_set_error (bfd_error_no_error);
1523
1524           arfile = bfd_openr_next_archived_file (file, arfile);
1525           if (arfile == NULL)
1526             {
1527               if (bfd_get_error () != bfd_error_no_more_archived_files)
1528                 {
1529                   bfd_nonfatal (bfd_get_filename (file));
1530                 }
1531               break;
1532             }
1533
1534           display_bfd (arfile);
1535
1536           if (last_arfile != NULL)
1537             bfd_close (last_arfile);
1538           last_arfile = arfile;
1539         }
1540
1541       if (last_arfile != NULL)
1542         bfd_close (last_arfile);
1543     }
1544   else
1545     display_bfd (file);
1546
1547   bfd_close (file);
1548 }
1549 \f
1550 /* Actually display the various requested regions */
1551
1552 static void
1553 dump_data (abfd)
1554      bfd *abfd;
1555 {
1556   asection *section;
1557   bfd_byte *data = 0;
1558   bfd_size_type datasize = 0;
1559   bfd_size_type i;
1560   bfd_size_type start, stop;
1561
1562   for (section = abfd->sections; section != NULL; section =
1563        section->next)
1564     {
1565       int onaline = 16;
1566
1567       if (only == (char *) NULL ||
1568           strcmp (only, section->name) == 0)
1569         {
1570           if (section->flags & SEC_HAS_CONTENTS)
1571             {
1572               printf ("Contents of section %s:\n", section->name);
1573
1574               if (bfd_section_size (abfd, section) == 0)
1575                 continue;
1576               data = (bfd_byte *) xmalloc ((size_t) bfd_section_size (abfd, section));
1577               datasize = bfd_section_size (abfd, section);
1578
1579
1580               bfd_get_section_contents (abfd, section, (PTR) data, 0, bfd_section_size (abfd, section));
1581
1582               if (start_address == (bfd_vma) -1
1583                   || start_address < section->vma)
1584                 start = 0;
1585               else
1586                 start = start_address - section->vma;
1587               if (stop_address == (bfd_vma) -1)
1588                 stop = bfd_section_size (abfd, section);
1589               else
1590                 {
1591                   if (stop_address < section->vma)
1592                     stop = 0;
1593                   else
1594                     stop = stop_address - section->vma;
1595                   if (stop > bfd_section_size (abfd, section))
1596                     stop = bfd_section_size (abfd, section);
1597                 }
1598               for (i = start; i < stop; i += onaline)
1599                 {
1600                   bfd_size_type j;
1601
1602                   printf (" %04lx ", (unsigned long int) (i + section->vma));
1603                   for (j = i; j < i + onaline; j++)
1604                     {
1605                       if (j < stop)
1606                         printf ("%02x", (unsigned) (data[j]));
1607                       else
1608                         printf ("  ");
1609                       if ((j & 3) == 3)
1610                         printf (" ");
1611                     }
1612
1613                   printf (" ");
1614                   for (j = i; j < i + onaline; j++)
1615                     {
1616                       if (j >= stop)
1617                         printf (" ");
1618                       else
1619                         printf ("%c", isprint (data[j]) ? data[j] : '.');
1620                     }
1621                   putchar ('\n');
1622                 }
1623               free (data);
1624             }
1625         }
1626     }
1627 }
1628
1629 /* Should perhaps share code and display with nm? */
1630 static void
1631 dump_symbols (abfd, dynamic)
1632      bfd *abfd;
1633      boolean dynamic;
1634 {
1635   asymbol **current;
1636   long max;
1637   long count;
1638
1639   if (dynamic)
1640     {
1641       current = dynsyms;
1642       max = dynsymcount;
1643       if (max == 0)
1644         return;
1645       printf ("DYNAMIC SYMBOL TABLE:\n");
1646     }
1647   else
1648     {
1649       current = syms;
1650       max = symcount;
1651       if (max == 0)
1652         return;
1653       printf ("SYMBOL TABLE:\n");
1654     }
1655
1656   for (count = 0; count < max; count++)
1657     {
1658       if (*current)
1659         {
1660           bfd *cur_bfd = bfd_asymbol_bfd(*current);
1661           if (cur_bfd)
1662             {
1663               bfd_print_symbol (cur_bfd,
1664                                 stdout,
1665                                 *current, bfd_print_symbol_all);
1666               printf ("\n");
1667             }
1668         }
1669       current++;
1670     }
1671   printf ("\n");
1672   printf ("\n");
1673 }
1674
1675 static void
1676 dump_relocs (abfd)
1677      bfd *abfd;
1678 {
1679   arelent **relpp;
1680   long relcount;
1681   asection *a;
1682
1683   for (a = abfd->sections; a != (asection *) NULL; a = a->next)
1684     {
1685       long relsize;
1686
1687       if (bfd_is_abs_section (a))
1688         continue;
1689       if (bfd_is_und_section (a))
1690         continue;
1691       if (bfd_is_com_section (a))
1692         continue;
1693
1694       if (only)
1695         {
1696           if (strcmp (only, a->name))
1697             continue;
1698         }
1699       else if ((a->flags & SEC_RELOC) == 0)
1700         continue;
1701
1702       relsize = bfd_get_reloc_upper_bound (abfd, a);
1703       if (relsize < 0)
1704         bfd_fatal (bfd_get_filename (abfd));
1705
1706       printf ("RELOCATION RECORDS FOR [%s]:", a->name);
1707
1708       if (relsize == 0)
1709         {
1710           printf (" (none)\n\n");
1711         }
1712       else
1713         {
1714           relpp = (arelent **) xmalloc (relsize);
1715           relcount = bfd_canonicalize_reloc (abfd, a, relpp, syms);
1716           if (relcount < 0)
1717             bfd_fatal (bfd_get_filename (abfd));
1718           else if (relcount == 0)
1719             {
1720               printf (" (none)\n\n");
1721             }
1722           else
1723             {
1724               printf ("\n");
1725               dump_reloc_set (abfd, a, relpp, relcount);
1726               printf ("\n\n");
1727             }
1728           free (relpp);
1729         }
1730     }
1731 }
1732
1733 static void
1734 dump_dynamic_relocs (abfd)
1735      bfd *abfd;
1736 {
1737   long relsize;
1738   arelent **relpp;
1739   long relcount;
1740
1741   relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
1742   if (relsize < 0)
1743     bfd_fatal (bfd_get_filename (abfd));
1744
1745   printf ("DYNAMIC RELOCATION RECORDS");
1746
1747   if (relsize == 0)
1748     {
1749       printf (" (none)\n\n");
1750     }
1751   else
1752     {
1753       relpp = (arelent **) xmalloc (relsize);
1754       relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
1755       if (relcount < 0)
1756         bfd_fatal (bfd_get_filename (abfd));
1757       else if (relcount == 0)
1758         {
1759           printf (" (none)\n\n");
1760         }
1761       else
1762         {
1763           printf ("\n");
1764           dump_reloc_set (abfd, (asection *) NULL, relpp, relcount);
1765           printf ("\n\n");
1766         }
1767       free (relpp);
1768     }
1769 }
1770
1771 static void
1772 dump_reloc_set (abfd, sec, relpp, relcount)
1773      bfd *abfd;
1774      asection *sec;
1775      arelent **relpp;
1776      long relcount;
1777 {
1778   arelent **p;
1779   char *last_filename, *last_functionname;
1780   unsigned int last_line;
1781
1782   /* Get column headers lined up reasonably.  */
1783   {
1784     static int width;
1785     if (width == 0)
1786       {
1787         char buf[30];
1788         sprintf_vma (buf, (bfd_vma) -1);
1789         width = strlen (buf) - 7;
1790       }
1791     printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
1792   }
1793
1794   last_filename = NULL;
1795   last_functionname = NULL;
1796   last_line = 0;
1797
1798   for (p = relpp; relcount && *p != (arelent *) NULL; p++, relcount--)
1799     {
1800       arelent *q = *p;
1801       const char *filename, *functionname;
1802       unsigned int line;
1803       const char *sym_name;
1804       const char *section_name;
1805
1806       if (start_address != (bfd_vma) -1
1807           && q->address < start_address)
1808         continue;
1809       if (stop_address != (bfd_vma) -1
1810           && q->address > stop_address)
1811         continue;
1812
1813       if (with_line_numbers
1814           && sec != NULL
1815           && bfd_find_nearest_line (abfd, sec, syms, q->address,
1816                                     &filename, &functionname, &line))
1817         {
1818           if (functionname != NULL
1819               && (last_functionname == NULL
1820                   || strcmp (functionname, last_functionname) != 0))
1821             {
1822               printf ("%s():\n", functionname);
1823               if (last_functionname != NULL)
1824                 free (last_functionname);
1825               last_functionname = xstrdup (functionname);
1826             }
1827           if (line > 0
1828               && (line != last_line
1829                   || (filename != NULL
1830                       && last_filename != NULL
1831                       && strcmp (filename, last_filename) != 0)))
1832             {
1833               printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
1834               last_line = line;
1835               if (last_filename != NULL)
1836                 free (last_filename);
1837               if (filename == NULL)
1838                 last_filename = NULL;
1839               else
1840                 last_filename = xstrdup (filename);
1841             }
1842         }
1843
1844       if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
1845         {
1846           sym_name = (*(q->sym_ptr_ptr))->name;
1847           section_name = (*(q->sym_ptr_ptr))->section->name;
1848         }
1849       else
1850         {
1851           sym_name = NULL;
1852           section_name = NULL;
1853         }
1854       if (sym_name)
1855         {
1856           printf_vma (q->address);
1857           printf (" %-16s  %s",
1858                   q->howto->name,
1859                   sym_name);
1860         }
1861       else
1862         {
1863           if (section_name == (CONST char *) NULL)
1864             section_name = "*unknown*";
1865           printf_vma (q->address);
1866           printf (" %-16s  [%s]",
1867                   q->howto->name,
1868                   section_name);
1869         }
1870       if (q->addend)
1871         {
1872           printf ("+0x");
1873           printf_vma (q->addend);
1874         }
1875       printf ("\n");
1876     }
1877 }
1878 \f
1879 /* The length of the longest architecture name + 1.  */
1880 #define LONGEST_ARCH sizeof("rs6000:6000")
1881
1882 #ifndef L_tmpnam
1883 #define L_tmpnam 25
1884 #endif
1885
1886 static const char *
1887 endian_string (endian)
1888      enum bfd_endian endian;
1889 {
1890   if (endian == BFD_ENDIAN_BIG)
1891     return "big endian";
1892   else if (endian == BFD_ENDIAN_LITTLE)
1893     return "little endian";
1894   else
1895     return "endianness unknown";
1896 }
1897
1898 /* List the targets that BFD is configured to support, each followed
1899    by its endianness and the architectures it supports.  */
1900
1901 static void
1902 display_target_list ()
1903 {
1904   extern char *tmpnam ();
1905   extern bfd_target *bfd_target_vector[];
1906   char tmparg[L_tmpnam];
1907   char *dummy_name;
1908   int t;
1909
1910   dummy_name = tmpnam (tmparg);
1911   for (t = 0; bfd_target_vector[t]; t++)
1912     {
1913       bfd_target *p = bfd_target_vector[t];
1914       bfd *abfd = bfd_openw (dummy_name, p->name);
1915       int a;
1916
1917       printf ("%s\n (header %s, data %s)\n", p->name,
1918               endian_string (p->header_byteorder),
1919               endian_string (p->byteorder));
1920
1921       if (abfd == NULL)
1922         {
1923           bfd_nonfatal (dummy_name);
1924           continue;
1925         }
1926
1927       if (! bfd_set_format (abfd, bfd_object))
1928         {
1929           if (bfd_get_error () != bfd_error_invalid_operation)
1930             bfd_nonfatal (p->name);
1931           continue;
1932         }
1933
1934       for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
1935         if (bfd_set_arch_mach (abfd, (enum bfd_architecture) a, 0))
1936           printf ("  %s\n",
1937                   bfd_printable_arch_mach ((enum bfd_architecture) a, 0));
1938     }
1939   unlink (dummy_name);
1940 }
1941
1942 /* Print a table showing which architectures are supported for entries
1943    FIRST through LAST-1 of bfd_target_vector (targets across,
1944    architectures down).  */
1945
1946 static void
1947 display_info_table (first, last)
1948      int first;
1949      int last;
1950 {
1951   extern bfd_target *bfd_target_vector[];
1952   extern char *tmpnam ();
1953   char tmparg[L_tmpnam];
1954   int t, a;
1955   char *dummy_name;
1956
1957   /* Print heading of target names.  */
1958   printf ("\n%*s", (int) LONGEST_ARCH, " ");
1959   for (t = first; t < last && bfd_target_vector[t]; t++)
1960     printf ("%s ", bfd_target_vector[t]->name);
1961   putchar ('\n');
1962
1963   dummy_name = tmpnam (tmparg);
1964   for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
1965     if (strcmp (bfd_printable_arch_mach (a, 0), "UNKNOWN!") != 0)
1966       {
1967         printf ("%*s ", (int) LONGEST_ARCH - 1,
1968                 bfd_printable_arch_mach (a, 0));
1969         for (t = first; t < last && bfd_target_vector[t]; t++)
1970           {
1971             bfd_target *p = bfd_target_vector[t];
1972             boolean ok = true;
1973             bfd *abfd = bfd_openw (dummy_name, p->name);
1974
1975             if (abfd == NULL)
1976               {
1977                 bfd_nonfatal (p->name);
1978                 ok = false;
1979               }
1980
1981             if (ok)
1982               {
1983                 if (! bfd_set_format (abfd, bfd_object))
1984                   {
1985                     if (bfd_get_error () != bfd_error_invalid_operation)
1986                       bfd_nonfatal (p->name);
1987                     ok = false;
1988                   }
1989               }
1990
1991             if (ok)
1992               {
1993                 if (! bfd_set_arch_mach (abfd, a, 0))
1994                   ok = false;
1995               }
1996
1997             if (ok)
1998               printf ("%s ", p->name);
1999             else
2000               {
2001                 int l = strlen (p->name);
2002                 while (l--)
2003                   putchar ('-');
2004                 putchar (' ');
2005               }
2006           }
2007         putchar ('\n');
2008       }
2009   unlink (dummy_name);
2010 }
2011
2012 /* Print tables of all the target-architecture combinations that
2013    BFD has been configured to support.  */
2014
2015 static void
2016 display_target_tables ()
2017 {
2018   int t, columns;
2019   extern bfd_target *bfd_target_vector[];
2020   char *colum;
2021   extern char *getenv ();
2022
2023   columns = 0;
2024   colum = getenv ("COLUMNS");
2025   if (colum != NULL)
2026     columns = atoi (colum);
2027   if (columns == 0)
2028     columns = 80;
2029
2030   t = 0;
2031   while (bfd_target_vector[t] != NULL)
2032     {
2033       int oldt = t, wid;
2034
2035       wid = LONGEST_ARCH + strlen (bfd_target_vector[t]->name) + 1;
2036       ++t;
2037       while (wid < columns && bfd_target_vector[t] != NULL)
2038         {
2039           int newwid;
2040
2041           newwid = wid + strlen (bfd_target_vector[t]->name) + 1;
2042           if (newwid >= columns)
2043             break;
2044           wid = newwid;
2045           ++t;
2046         }
2047       display_info_table (oldt, t);
2048     }
2049 }
2050
2051 static void
2052 display_info ()
2053 {
2054   printf ("BFD header file version %s\n", BFD_VERSION);
2055   display_target_list ();
2056   display_target_tables ();
2057 }
2058
2059 int
2060 main (argc, argv)
2061      int argc;
2062      char **argv;
2063 {
2064   int c;
2065   char *target = default_target;
2066   boolean seenflag = false;
2067
2068   program_name = *argv;
2069   xmalloc_set_program_name (program_name);
2070
2071   START_PROGRESS (program_name, 0);
2072
2073   bfd_init ();
2074
2075   while ((c = getopt_long (argc, argv, "pib:m:VdDlfahrRtTxsSj:wE:",
2076                            long_options, (int *) 0))
2077          != EOF)
2078     {
2079       if (c != 'l' && c != OPTION_START_ADDRESS && c != OPTION_STOP_ADDRESS)
2080         seenflag = true;
2081       switch (c)
2082         {
2083         case 0:
2084           break;                /* we've been given a long option */
2085         case 'm':
2086           machine = optarg;
2087           break;
2088         case 'j':
2089           only = optarg;
2090           break;
2091         case 'l':
2092           with_line_numbers = 1;
2093           break;
2094         case 'b':
2095           target = optarg;
2096           break;
2097         case 'f':
2098           dump_file_header = true;
2099           break;
2100         case 'i':
2101           formats_info = true;
2102           break;
2103         case 'p':
2104           dump_private_headers = 1;
2105           break;
2106         case 'x':
2107           dump_private_headers = 1;
2108           dump_symtab = 1;
2109           dump_reloc_info = 1;
2110           dump_file_header = true;
2111           dump_ar_hdrs = 1;
2112           dump_section_headers = 1;
2113           break;
2114         case 't':
2115           dump_symtab = 1;
2116           break;
2117         case 'T':
2118           dump_dynamic_symtab = 1;
2119           break;
2120         case 'd':
2121           disassemble = true;
2122           break;
2123         case 'D':
2124           disassemble = disassemble_all = true;
2125           break;
2126         case 'S':
2127           disassemble = true;
2128           with_source_code = true;
2129           break;
2130         case 's':
2131           dump_section_contents = 1;
2132           break;
2133         case 'r':
2134           dump_reloc_info = 1;
2135           break;
2136         case 'R':
2137           dump_dynamic_reloc_info = 1;
2138           break;
2139         case 'a':
2140           dump_ar_hdrs = 1;
2141           break;
2142         case 'h':
2143           dump_section_headers = 1;
2144           break;
2145         case 'H':
2146           usage (stdout, 0);
2147         case 'V':
2148           show_version = 1;
2149           break;
2150         case 'w':
2151           wide_output = 1;
2152           break;
2153         case OPTION_START_ADDRESS:
2154           start_address = parse_vma (optarg, "--start-address");
2155           break;
2156         case OPTION_STOP_ADDRESS:
2157           stop_address = parse_vma (optarg, "--stop-address");
2158           break;
2159         case 'E':
2160           if (strcmp (optarg, "B") == 0)
2161             endian = BFD_ENDIAN_BIG;
2162           else if (strcmp (optarg, "L") == 0)
2163             endian = BFD_ENDIAN_LITTLE;
2164           else
2165             {
2166               fprintf (stderr, "%s: unrecognized -E option\n", program_name);
2167               usage (stderr, 1);
2168             }
2169           break;
2170         case OPTION_ENDIAN:
2171           if (strncmp (optarg, "big", strlen (optarg)) == 0)
2172             endian = BFD_ENDIAN_BIG;
2173           else if (strncmp (optarg, "little", strlen (optarg)) == 0)
2174             endian = BFD_ENDIAN_LITTLE;
2175           else
2176             {
2177               fprintf (stderr, "%s: unrecognized --endian type `%s'\n",
2178                       program_name, optarg);
2179               usage (stderr, 1);
2180             }
2181           break;
2182         default:
2183           usage (stderr, 1);
2184         }
2185     }
2186
2187   if (show_version)
2188     {
2189       printf ("GNU %s version %s\n", program_name, program_version);
2190       exit (0);
2191     }
2192
2193   if (seenflag == false)
2194     usage (stderr, 1);
2195
2196   if (formats_info)
2197     {
2198       display_info ();
2199     }
2200   else
2201     {
2202       if (optind == argc)
2203         display_file ("a.out", target);
2204       else
2205         for (; optind < argc;)
2206           display_file (argv[optind++], target);
2207     }
2208
2209   END_PROGRESS (program_name);
2210
2211   return 0;
2212 }