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