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