]> Git Repo - binutils.git/blob - binutils/objdump.c
Add new option --show-raw-insn.
[binutils.git] / binutils / objdump.c
1 /* objdump.c -- dump information about an object file.
2    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 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 /* 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 show_raw_insn;              /* --show-raw-insn */
56 int dump_stab_section_info;     /* --stabs */
57 boolean disassemble;            /* -d */
58 boolean disassemble_all;        /* -D */
59 boolean formats_info;           /* -i */
60 char *only;                     /* -j secname */
61 int wide_output;                /* -w */
62 bfd_vma start_address = (bfd_vma) -1; /* --start-address */
63 bfd_vma stop_address = (bfd_vma) -1;  /* --stop-address */
64 int dump_debugging;             /* --debugging */
65
66 /* Extra info to pass to the disassembler address printing function.  */
67 struct objdump_disasm_info {
68   bfd *abfd;
69   asection *sec;
70   boolean require_sec;
71 };
72
73 /* Architecture to disassemble for, or default if NULL.  */
74 char *machine = (char *) NULL;
75
76 /* The symbol table.  */
77 asymbol **syms;
78
79 /* Number of symbols in `syms'.  */
80 long symcount = 0;
81
82 /* The sorted symbol table.  */
83 asymbol **sorted_syms;
84
85 /* Number of symbols in `sorted_syms'.  */
86 long sorted_symcount = 0;
87
88 /* The dynamic symbol table.  */
89 asymbol **dynsyms;
90
91 /* Number of symbols in `dynsyms'.  */
92 long dynsymcount = 0;
93
94 /* Forward declarations.  */
95
96 static void
97 display_file PARAMS ((char *filename, char *target));
98
99 static void
100 dump_data PARAMS ((bfd *abfd));
101
102 static void
103 dump_relocs PARAMS ((bfd *abfd));
104
105 static void
106 dump_dynamic_relocs PARAMS ((bfd * abfd));
107
108 static void
109 dump_reloc_set PARAMS ((bfd *, arelent **, long));
110
111 static void
112 dump_symbols PARAMS ((bfd *abfd, boolean dynamic));
113
114 static void
115 display_bfd PARAMS ((bfd *abfd));
116
117 static void
118 objdump_print_value PARAMS ((bfd_vma, FILE *));
119
120 static void
121 objdump_print_address PARAMS ((bfd_vma, struct disassemble_info *));
122
123 static void
124 show_line PARAMS ((bfd *, asection *, bfd_vma));
125
126 static const char *
127 endian_string PARAMS ((enum bfd_endian));
128 \f
129 void
130 usage (stream, status)
131      FILE *stream;
132      int status;
133 {
134   fprintf (stream, "\
135 Usage: %s [-ahifdDprRtTxsSlw] [-b bfdname] [-m machine] [-j section-name]\n\
136        [--archive-headers] [--target=bfdname] [--debugging] [--disassemble]\n\
137        [--disassemble-all] [--file-headers] [--section-headers] [--headers]\n\
138        [--info] [--section=section-name] [--line-numbers] [--source]\n",
139            program_name);
140   fprintf (stream, "\
141        [--architecture=machine] [--reloc] [--full-contents] [--stabs]\n\
142        [--syms] [--all-headers] [--dynamic-syms] [--dynamic-reloc]\n\
143        [--wide] [--version] [--help] [--private-headers]\n\
144        [--start-address=addr] [--stop-address=addr]\n\
145        [--show-raw-insn] objfile...\n\
146 at least one option besides -l (--line-numbers) must be given\n");
147   list_supported_targets (program_name, stream);
148   exit (status);
149 }
150
151 /* 150 isn't special; it's just an arbitrary non-ASCII char value.  */
152
153 #define OPTION_START_ADDRESS (150)
154 #define OPTION_STOP_ADDRESS (OPTION_START_ADDRESS + 1)
155
156 static struct option long_options[]=
157 {
158   {"all-headers", no_argument, NULL, 'x'},
159   {"private-headers", no_argument, NULL, 'p'},
160   {"architecture", required_argument, NULL, 'm'},
161   {"archive-headers", no_argument, NULL, 'a'},
162   {"debugging", no_argument, &dump_debugging, 1},
163   {"disassemble", no_argument, NULL, 'd'},
164   {"disassemble-all", no_argument, NULL, 'D'},
165   {"dynamic-reloc", no_argument, NULL, 'R'},
166   {"dynamic-syms", no_argument, NULL, 'T'},
167   {"file-headers", no_argument, NULL, 'f'},
168   {"full-contents", no_argument, NULL, 's'},
169   {"headers", no_argument, NULL, 'h'},
170   {"help", no_argument, NULL, 'H'},
171   {"info", no_argument, NULL, 'i'},
172   {"line-numbers", no_argument, NULL, 'l'},
173   {"reloc", no_argument, NULL, 'r'},
174   {"section", required_argument, NULL, 'j'},
175   {"section-headers", no_argument, NULL, 'h'},
176   {"show-raw-insn", no_argument, &show_raw_insn, 1},
177   {"source", no_argument, NULL, 'S'},
178   {"stabs", no_argument, &dump_stab_section_info, 1},
179   {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
180   {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
181   {"syms", no_argument, NULL, 't'},
182   {"target", required_argument, NULL, 'b'},
183   {"version", no_argument, &show_version, 1},
184   {"wide", no_argument, &wide_output, 'w'},
185   {0, no_argument, 0, 0}
186 };
187 \f
188 static void
189 dump_section_header (abfd, section, ignored)
190      bfd *abfd;
191      asection *section;
192      PTR ignored;
193 {
194   char *comma = "";
195
196 #define PF(x,y) \
197   if (section->flags & x) {  printf("%s%s",comma,y); comma = ", "; }
198
199
200   printf ("SECTION %d [%s]\t: size %08x",
201           section->index,
202           section->name,
203           (unsigned) bfd_get_section_size_before_reloc (section));
204   printf (" vma ");
205   printf_vma (section->vma);
206   printf (" lma ");
207   printf_vma (section->lma);
208   printf (" align 2**%u%s ",
209           section->alignment_power, (wide_output) ? "" : "\n");
210   PF (SEC_ALLOC, "ALLOC");
211   PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
212   PF (SEC_CONSTRUCTOR_TEXT, "CONSTRUCTOR TEXT");
213   PF (SEC_CONSTRUCTOR_DATA, "CONSTRUCTOR DATA");
214   PF (SEC_CONSTRUCTOR_BSS, "CONSTRUCTOR BSS");
215   PF (SEC_LOAD, "LOAD");
216   PF (SEC_RELOC, "RELOC");
217 #ifdef SEC_BALIGN
218   PF (SEC_BALIGN, "BALIGN");
219 #endif
220   PF (SEC_READONLY, "READONLY");
221   PF (SEC_CODE, "CODE");
222   PF (SEC_DATA, "DATA");
223   PF (SEC_ROM, "ROM");
224   PF (SEC_DEBUGGING, "DEBUGGING");
225   PF (SEC_NEVER_LOAD, "NEVER_LOAD");
226   PF (SEC_EXCLUDE, "EXCLUDE");
227   PF (SEC_SORT_ENTRIES, "SORT ENTRIES");
228   printf ("\n");
229 #undef PF
230 }
231
232 static void
233 dump_headers (abfd)
234      bfd *abfd;
235 {
236   bfd_map_over_sections (abfd, dump_section_header, (PTR) NULL);
237 }
238 \f
239 static asymbol **
240 slurp_symtab (abfd)
241      bfd *abfd;
242 {
243   asymbol **sy = (asymbol **) NULL;
244   long storage;
245
246   if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
247     {
248       printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd));
249       symcount = 0;
250       return NULL;
251     }
252
253   storage = bfd_get_symtab_upper_bound (abfd);
254   if (storage < 0)
255     bfd_fatal (bfd_get_filename (abfd));
256
257   if (storage)
258     {
259       sy = (asymbol **) xmalloc (storage);
260     }
261   symcount = bfd_canonicalize_symtab (abfd, sy);
262   if (symcount < 0)
263     bfd_fatal (bfd_get_filename (abfd));
264   if (symcount == 0)
265     fprintf (stderr, "%s: %s: No symbols\n",
266              program_name, bfd_get_filename (abfd));
267   return sy;
268 }
269
270 /* Read in the dynamic symbols.  */
271
272 static asymbol **
273 slurp_dynamic_symtab (abfd)
274      bfd *abfd;
275 {
276   asymbol **sy = (asymbol **) NULL;
277   long storage;
278
279   storage = bfd_get_dynamic_symtab_upper_bound (abfd);
280   if (storage < 0)
281     {
282       if (!(bfd_get_file_flags (abfd) & DYNAMIC))
283         {
284           fprintf (stderr, "%s: %s: not a dynamic object\n",
285                    program_name, bfd_get_filename (abfd));
286           dynsymcount = 0;
287           return NULL;
288         }
289
290       bfd_fatal (bfd_get_filename (abfd));
291     }
292
293   if (storage)
294     {
295       sy = (asymbol **) xmalloc (storage);
296     }
297   dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
298   if (dynsymcount < 0)
299     bfd_fatal (bfd_get_filename (abfd));
300   if (dynsymcount == 0)
301     fprintf (stderr, "%s: %s: No dynamic symbols\n",
302              program_name, bfd_get_filename (abfd));
303   return sy;
304 }
305
306 /* Filter out (in place) symbols that are useless for disassembly.
307    COUNT is the number of elements in SYMBOLS.
308    Return the number of useful symbols. */
309
310 long
311 remove_useless_symbols (symbols, count)
312      asymbol **symbols;
313      long count;
314 {
315   register asymbol **in_ptr = symbols, **out_ptr = symbols;
316
317   while (--count >= 0)
318     {
319       asymbol *sym = *in_ptr++;
320
321       if (sym->name == NULL || sym->name[0] == '\0')
322         continue;
323       if (sym->flags & (BSF_DEBUGGING))
324         continue;
325       if (bfd_is_und_section (sym->section)
326           || bfd_is_com_section (sym->section))
327         continue;
328
329       *out_ptr++ = sym;
330     }
331   return out_ptr - symbols;
332 }
333
334 /* Sort symbols into value order.  */
335
336 static int 
337 compare_symbols (ap, bp)
338      const PTR ap;
339      const PTR bp;
340 {
341   const asymbol *a = *(const asymbol **)ap;
342   const asymbol *b = *(const asymbol **)bp;
343   const char *an, *bn;
344   size_t anl, bnl;
345   boolean af, bf;
346   flagword aflags, bflags;
347
348   if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
349     return 1;
350   else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
351     return -1;
352
353   if (a->section > b->section)
354     return 1;
355   else if (a->section < b->section)
356     return -1;
357
358   an = bfd_asymbol_name (a);
359   bn = bfd_asymbol_name (b);
360   anl = strlen (an);
361   bnl = strlen (bn);
362
363   /* The symbols gnu_compiled and gcc2_compiled convey no real
364      information, so put them after other symbols with the same value.  */
365
366   af = (strstr (an, "gnu_compiled") != NULL
367         || strstr (an, "gcc2_compiled") != NULL);
368   bf = (strstr (bn, "gnu_compiled") != NULL
369         || strstr (bn, "gcc2_compiled") != NULL);
370
371   if (af && ! bf)
372     return 1;
373   if (! af && bf)
374     return -1;
375
376   /* We use a heuristic for the file name, to try to sort it after
377      more useful symbols.  It may not work on non Unix systems, but it
378      doesn't really matter; the only difference is precisely which
379      symbol names get printed.  */
380
381 #define file_symbol(s, sn, snl)                 \
382   (((s)->flags & BSF_FILE) != 0                 \
383    || ((sn)[(snl) - 2] == '.'                   \
384        && ((sn)[(snl) - 1] == 'o'               \
385            || (sn)[(snl) - 1] == 'a')))
386
387   af = file_symbol (a, an, anl);
388   bf = file_symbol (b, bn, bnl);
389
390   if (af && ! bf)
391     return 1;
392   if (! af && bf)
393     return -1;
394
395   /* Finally, try to sort global symbols before local symbols before
396      debugging symbols.  */
397
398   aflags = a->flags;
399   bflags = b->flags;
400
401   if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING))
402     {
403       if ((aflags & BSF_DEBUGGING) != 0)
404         return 1;
405       else
406         return -1;
407     }
408   if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL))
409     {
410       if ((aflags & BSF_LOCAL) != 0)
411         return 1;
412       else
413         return -1;
414     }
415
416   return 0;
417 }
418
419 /* Sort relocs into address order.  */
420
421 static int
422 compare_relocs (ap, bp)
423      const PTR ap;
424      const PTR bp;
425 {
426   const arelent *a = *(const arelent **)ap;
427   const arelent *b = *(const arelent **)bp;
428
429   if (a->address > b->address)
430     return 1;
431   else if (a->address < b->address)
432     return -1;
433
434   /* So that associated relocations tied to the same address show up
435      in the correct order, we don't do any further sorting.  */
436   if (a > b)
437     return 1;
438   else if (a < b)
439     return -1;
440   else
441     return 0;
442 }
443
444 /* Print VMA to STREAM with no leading zeroes.  */
445
446 static void
447 objdump_print_value (vma, stream)
448      bfd_vma vma;
449      FILE *stream;
450 {
451   char buf[30];
452   char *p;
453
454   sprintf_vma (buf, vma);
455   for (p = buf; *p == '0'; ++p)
456     ;
457   fprintf (stream, "%s", p);
458 }
459
460 /* Print VMA symbolically to INFO if possible.  */
461
462 static void
463 objdump_print_address (vma, info)
464      bfd_vma vma;
465      struct disassemble_info *info;
466 {
467   /* @@ Would it speed things up to cache the last two symbols returned,
468      and maybe their address ranges?  For many processors, only one memory
469      operand can be present at a time, so the 2-entry cache wouldn't be
470      constantly churned by code doing heavy memory accesses.  */
471
472   /* Indices in `sorted_syms'.  */
473   long min = 0;
474   long max = sorted_symcount;
475   long thisplace;
476
477   fprintf_vma (info->stream, vma);
478
479   if (sorted_symcount < 1)
480     return;
481
482   /* Perform a binary search looking for the closest symbol to the
483      required value.  We are searching the range (min, max].  */
484   while (min + 1 < max)
485     {
486       asymbol *sym;
487
488       thisplace = (max + min) / 2;
489       sym = sorted_syms[thisplace];
490
491       if (bfd_asymbol_value (sym) > vma)
492         max = thisplace;
493       else if (bfd_asymbol_value (sym) < vma)
494         min = thisplace;
495       else
496         {
497           min = thisplace;
498           break;
499         }
500     }
501
502   /* The symbol we want is now in min, the low end of the range we
503      were searching.  If there are several symbols with the same
504      value, we want the first one.  */
505   thisplace = min;
506   while (thisplace > 0
507          && (bfd_asymbol_value (sorted_syms[thisplace])
508              == bfd_asymbol_value (sorted_syms[thisplace - 1])))
509     --thisplace;
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   if (show_raw_insn)
838     disasm_info.flags |= DISASM_RAW_INSN_FLAG;
839
840   if (machine != (char *) NULL)
841     {
842       const bfd_arch_info_type *info = bfd_scan_arch (machine);
843       if (info == NULL)
844         {
845           fprintf (stderr, "%s: Can't use supplied machine %s\n",
846                    program_name,
847                    machine);
848           exit (1);
849         }
850       abfd->arch_info = info;
851     }
852
853   disassemble_fn = disassembler (abfd);
854   if (!disassemble_fn)
855     {
856       fprintf (stderr, "%s: Can't disassemble for architecture %s\n",
857                program_name,
858                bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
859       exit (1);
860     }
861
862   disasm_info.arch = bfd_get_arch (abfd);
863   disasm_info.mach = bfd_get_mach (abfd);
864   if (bfd_big_endian (abfd))
865     disasm_info.endian = BFD_ENDIAN_BIG;
866   else
867     disasm_info.endian = BFD_ENDIAN_LITTLE;
868
869   for (section = abfd->sections;
870        section != (asection *) NULL;
871        section = section->next)
872     {
873       bfd_byte *data = NULL;
874       bfd_size_type datasize = 0;
875       arelent **relbuf = NULL;
876       arelent **relpp = NULL;
877       arelent **relppend = NULL;
878       long stop;
879
880       if ((section->flags & SEC_LOAD) == 0
881           || (! disassemble_all
882               && only == NULL
883               && (section->flags & SEC_CODE) == 0))
884         continue;
885       if (only != (char *) NULL && strcmp (only, section->name) != 0)
886         continue;
887
888       if (dump_reloc_info
889           && (section->flags & SEC_RELOC) != 0)
890         {
891           long relsize;
892
893           relsize = bfd_get_reloc_upper_bound (abfd, section);
894           if (relsize < 0)
895             bfd_fatal (bfd_get_filename (abfd));
896
897           if (relsize > 0)
898             {
899               long relcount;
900
901               relbuf = (arelent **) xmalloc (relsize);
902               relcount = bfd_canonicalize_reloc (abfd, section, relbuf, syms);
903               if (relcount < 0)
904                 bfd_fatal (bfd_get_filename (abfd));
905
906               /* Sort the relocs by address.  */
907               qsort (relbuf, relcount, sizeof (arelent *), compare_relocs);
908
909               relpp = relbuf;
910               relppend = relpp + relcount;
911             }
912         }
913
914       printf ("Disassembly of section %s:\n", section->name);
915
916       datasize = bfd_get_section_size_before_reloc (section);
917       if (datasize == 0)
918         continue;
919
920       data = (bfd_byte *) xmalloc ((size_t) datasize);
921
922       bfd_get_section_contents (abfd, section, data, 0, datasize);
923
924       aux.sec = section;
925       disasm_info.buffer = data;
926       disasm_info.buffer_vma = section->vma;
927       disasm_info.buffer_length = datasize;
928       if (start_address == (bfd_vma) -1
929           || start_address < disasm_info.buffer_vma)
930         i = 0;
931       else
932         i = start_address - disasm_info.buffer_vma;
933       if (stop_address == (bfd_vma) -1)
934         stop = datasize;
935       else
936         {
937           if (stop_address < disasm_info.buffer_vma)
938             stop = 0;
939           else
940             stop = stop_address - disasm_info.buffer_vma;
941           if (stop > disasm_info.buffer_length)
942             stop = disasm_info.buffer_length;
943         }
944       while (i < stop)
945         {
946           int bytes;
947           boolean need_nl = false;
948
949           if (data[i] == 0 && data[i + 1] == 0 && data[i + 2] == 0 &&
950               data[i + 3] == 0)
951             {
952               if (done_dot == false)
953                 {
954                   printf ("...\n");
955                   done_dot = true;
956                 }
957               bytes = 4;
958             }
959           else
960             {
961               done_dot = false;
962               if (with_line_numbers || with_source_code)
963                 show_line (abfd, section, i);
964               aux.require_sec = true;
965               objdump_print_address (section->vma + i, &disasm_info);
966               aux.require_sec = false;
967               putchar (' ');
968
969               bytes = (*disassemble_fn) (section->vma + i, &disasm_info);
970               if (bytes < 0)
971                 break;
972
973               if (!wide_output)
974                 putchar ('\n');
975               else
976                 need_nl = true;
977             }
978
979           if (dump_reloc_info
980               && (section->flags & SEC_RELOC) != 0)
981             {
982               while (relpp < relppend
983                      && ((*relpp)->address >= (bfd_vma) i
984                          && (*relpp)->address < (bfd_vma) i + bytes))
985                 {
986                   arelent *q;
987                   const char *sym_name;
988
989                   q = *relpp;
990
991                   printf ("\t\tRELOC: ");
992
993                   printf_vma (section->vma + q->address);
994
995                   printf (" %s ", q->howto->name);
996
997                   if (q->sym_ptr_ptr != NULL
998                       && *q->sym_ptr_ptr != NULL)
999                     {
1000                       sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
1001                       if (sym_name == NULL || *sym_name == '\0')
1002                         {
1003                           asection *sym_sec;
1004
1005                           sym_sec = bfd_get_section (*q->sym_ptr_ptr);
1006                           sym_name = bfd_get_section_name (abfd, sym_sec);
1007                           if (sym_name == NULL || *sym_name == '\0')
1008                             sym_name = "*unknown*";
1009                         }
1010                     }
1011
1012                   printf ("%s", sym_name);
1013
1014                   if (q->addend)
1015                     {
1016                       printf ("+0x");
1017                       printf_vma (q->addend);
1018                     }
1019
1020                   printf ("\n");
1021                   need_nl = false;
1022                   ++relpp;
1023                 }
1024             }
1025
1026           if (need_nl)
1027             printf ("\n");
1028
1029           i += bytes;
1030         }
1031
1032       free (data);
1033       if (relbuf != NULL)
1034         free (relbuf);
1035     }
1036   free (sorted_syms);
1037 }
1038 \f
1039
1040 /* Define a table of stab values and print-strings.  We wish the initializer
1041    could be a direct-mapped table, but instead we build one the first
1042    time we need it.  */
1043
1044 char **stab_name;
1045
1046 struct stab_print {
1047   int value;
1048   char *string;
1049 };
1050
1051 struct stab_print stab_print[] = {
1052 #define __define_stab(NAME, CODE, STRING) {CODE, STRING},
1053 #include "aout/stab.def"
1054 #undef __define_stab
1055   {0, ""}
1056 };
1057
1058 void dump_section_stabs PARAMS ((bfd *abfd, char *stabsect_name,
1059                                  char *strsect_name));
1060
1061 /* Dump the stabs sections from an object file that has a section that
1062    uses Sun stabs encoding.  It has to use some hooks into BFD because
1063    string table sections are not normally visible to BFD callers.  */
1064
1065 void
1066 dump_stabs (abfd)
1067      bfd *abfd;
1068 {
1069   /* Allocate and initialize stab name array if first time.  */
1070   if (stab_name == NULL) 
1071     {
1072       int i;
1073
1074       stab_name = (char **) xmalloc (256 * sizeof(char *));
1075       /* Clear the array. */
1076       for (i = 0; i < 256; i++)
1077         stab_name[i] = NULL;
1078       /* Fill in the defined stabs. */
1079       for (i = 0; *stab_print[i].string; i++)
1080         stab_name[stab_print[i].value] = stab_print[i].string;
1081     }
1082
1083   dump_section_stabs (abfd, ".stab", ".stabstr");
1084   dump_section_stabs (abfd, ".stab.excl", ".stab.exclstr");
1085   dump_section_stabs (abfd, ".stab.index", ".stab.indexstr");
1086   dump_section_stabs (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
1087 }
1088
1089 static struct internal_nlist *stabs;
1090 static bfd_size_type stab_size;
1091
1092 static char *strtab;
1093 static bfd_size_type stabstr_size;
1094
1095 /* Read ABFD's stabs section STABSECT_NAME into `stabs'
1096    and string table section STRSECT_NAME into `strtab'.
1097    If the section exists and was read, allocate the space and return true.
1098    Otherwise return false.  */
1099
1100 boolean
1101 read_section_stabs (abfd, stabsect_name, strsect_name)
1102      bfd *abfd;
1103      char *stabsect_name;
1104      char *strsect_name;
1105 {
1106   asection *stabsect, *stabstrsect;
1107
1108   stabsect = bfd_get_section_by_name (abfd, stabsect_name);
1109   if (0 == stabsect)
1110     {
1111       printf ("No %s section present\n\n", stabsect_name);
1112       return false;
1113     }
1114
1115   stabstrsect = bfd_get_section_by_name (abfd, strsect_name);
1116   if (0 == stabstrsect)
1117     {
1118       fprintf (stderr, "%s: %s has no %s section\n", program_name,
1119                bfd_get_filename (abfd), strsect_name);
1120       return false;
1121     }
1122  
1123   stab_size    = bfd_section_size (abfd, stabsect);
1124   stabstr_size = bfd_section_size (abfd, stabstrsect);
1125
1126   stabs  = (struct internal_nlist *) xmalloc (stab_size);
1127   strtab = (char *) xmalloc (stabstr_size);
1128   
1129   if (! bfd_get_section_contents (abfd, stabsect, (PTR) stabs, 0, stab_size))
1130     {
1131       fprintf (stderr, "%s: Reading %s section of %s failed: %s\n",
1132                program_name, stabsect_name, bfd_get_filename (abfd),
1133                bfd_errmsg (bfd_get_error ()));
1134       free (stabs);
1135       free (strtab);
1136       return false;
1137     }
1138
1139   if (! bfd_get_section_contents (abfd, stabstrsect, (PTR) strtab, 0,
1140                                   stabstr_size))
1141     {
1142       fprintf (stderr, "%s: Reading %s section of %s failed: %s\n",
1143                program_name, strsect_name, bfd_get_filename (abfd),
1144                bfd_errmsg (bfd_get_error ()));
1145       free (stabs);
1146       free (strtab);
1147       return false;
1148     }
1149
1150   return true;
1151 }
1152
1153 #define SWAP_SYMBOL(symp, abfd) \
1154 { \
1155     (symp)->n_strx = bfd_h_get_32(abfd,                 \
1156                                   (unsigned char *)&(symp)->n_strx);    \
1157     (symp)->n_desc = bfd_h_get_16 (abfd,                        \
1158                                    (unsigned char *)&(symp)->n_desc);   \
1159     (symp)->n_value = bfd_h_get_32 (abfd,                       \
1160                                     (unsigned char *)&(symp)->n_value); \
1161 }
1162
1163 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
1164    using string table section STRSECT_NAME (in `strtab').  */
1165
1166 void
1167 print_section_stabs (abfd, stabsect_name, strsect_name)
1168      bfd *abfd;
1169      char *stabsect_name;
1170      char *strsect_name;
1171 {
1172   int i;
1173   unsigned file_string_table_offset = 0, next_file_string_table_offset = 0;
1174   struct internal_nlist *stabp = stabs,
1175   *stabs_end = (struct internal_nlist *) (stab_size + (char *) stabs);
1176
1177   printf ("Contents of %s section:\n\n", stabsect_name);
1178   printf ("Symnum n_type n_othr n_desc n_value  n_strx String\n");
1179
1180   /* Loop through all symbols and print them.
1181
1182      We start the index at -1 because there is a dummy symbol on
1183      the front of stabs-in-{coff,elf} sections that supplies sizes.  */
1184
1185   for (i = -1; stabp < stabs_end; stabp++, i++)
1186     {
1187       SWAP_SYMBOL (stabp, abfd);
1188       printf ("\n%-6d ", i);
1189       /* Either print the stab name, or, if unnamed, print its number
1190          again (makes consistent formatting for tools like awk). */
1191       if (stab_name[stabp->n_type])
1192         printf ("%-6s", stab_name[stabp->n_type]);
1193       else if (stabp->n_type == N_UNDF)
1194         printf ("HdrSym");
1195       else
1196         printf ("%-6d", stabp->n_type);
1197       printf (" %-6d %-6d ", stabp->n_other, stabp->n_desc);
1198       printf_vma (stabp->n_value);
1199       printf (" %-6lu", stabp->n_strx);
1200
1201       /* Symbols with type == 0 (N_UNDF) specify the length of the
1202          string table associated with this file.  We use that info
1203          to know how to relocate the *next* file's string table indices.  */
1204
1205       if (stabp->n_type == N_UNDF)
1206         {
1207           file_string_table_offset = next_file_string_table_offset;
1208           next_file_string_table_offset += stabp->n_value;
1209         }
1210       else
1211         {
1212           /* Using the (possibly updated) string table offset, print the
1213              string (if any) associated with this symbol.  */
1214
1215           if ((stabp->n_strx + file_string_table_offset) < stabstr_size)
1216             printf (" %s", &strtab[stabp->n_strx + file_string_table_offset]);
1217           else
1218             printf (" *");
1219         }
1220     }
1221   printf ("\n\n");
1222 }
1223
1224 void
1225 dump_section_stabs (abfd, stabsect_name, strsect_name)
1226      bfd *abfd;
1227      char *stabsect_name;
1228      char *strsect_name;
1229 {
1230   asection *s;
1231
1232   /* Check for section names for which stabsect_name is a prefix, to
1233      handle .stab0, etc.  */
1234   for (s = abfd->sections;
1235        s != NULL;
1236        s = s->next)
1237     {
1238       if (strncmp (stabsect_name, s->name, strlen (stabsect_name)) == 0
1239           && strncmp (strsect_name, s->name, strlen (strsect_name)) != 0)
1240         {
1241           if (read_section_stabs (abfd, s->name, strsect_name))
1242             {
1243               print_section_stabs (abfd, s->name, strsect_name);
1244               free (stabs);
1245               free (strtab);
1246             }
1247         }
1248     }
1249 }
1250 \f
1251 static void
1252 dump_bfd_header (abfd)
1253      bfd *abfd;
1254 {
1255   char *comma = "";
1256
1257   printf ("architecture: %s, ",
1258           bfd_printable_arch_mach (bfd_get_arch (abfd),
1259                                    bfd_get_mach (abfd)));
1260   printf ("flags 0x%08x:\n", abfd->flags);
1261
1262 #define PF(x, y)    if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
1263   PF (HAS_RELOC, "HAS_RELOC");
1264   PF (EXEC_P, "EXEC_P");
1265   PF (HAS_LINENO, "HAS_LINENO");
1266   PF (HAS_DEBUG, "HAS_DEBUG");
1267   PF (HAS_SYMS, "HAS_SYMS");
1268   PF (HAS_LOCALS, "HAS_LOCALS");
1269   PF (DYNAMIC, "DYNAMIC");
1270   PF (WP_TEXT, "WP_TEXT");
1271   PF (D_PAGED, "D_PAGED");
1272   PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
1273   printf ("\nstart address 0x");
1274   printf_vma (abfd->start_address);
1275 }
1276 \f
1277 static void
1278 dump_bfd_private_header (abfd)
1279 bfd *abfd;
1280 {
1281   bfd_print_private_bfd_data (abfd, stdout);
1282 }
1283 static void
1284 display_bfd (abfd)
1285      bfd *abfd;
1286 {
1287   char **matching;
1288
1289   if (!bfd_check_format_matches (abfd, bfd_object, &matching))
1290     {
1291       bfd_nonfatal (bfd_get_filename (abfd));
1292       if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1293         {
1294           list_matching_formats (matching);
1295           free (matching);
1296         }
1297       return;
1298     }
1299
1300   printf ("\n%s:     file format %s\n", bfd_get_filename (abfd),
1301           abfd->xvec->name);
1302   if (dump_ar_hdrs)
1303     print_arelt_descr (stdout, abfd, true);
1304   if (dump_file_header)
1305     dump_bfd_header (abfd);
1306   if (dump_private_headers)
1307     dump_bfd_private_header (abfd);
1308   putchar ('\n');
1309   if (dump_section_headers)
1310     dump_headers (abfd);
1311   if (dump_symtab || dump_reloc_info || disassemble || dump_debugging)
1312     {
1313       syms = slurp_symtab (abfd);
1314     }
1315   if (dump_dynamic_symtab || dump_dynamic_reloc_info)
1316     {
1317       dynsyms = slurp_dynamic_symtab (abfd);
1318     }
1319   if (dump_symtab)
1320     dump_symbols (abfd, false);
1321   if (dump_dynamic_symtab)
1322     dump_symbols (abfd, true);
1323   if (dump_stab_section_info)
1324     dump_stabs (abfd);
1325   if (dump_reloc_info && ! disassemble)
1326     dump_relocs (abfd);
1327   if (dump_dynamic_reloc_info)
1328     dump_dynamic_relocs (abfd);
1329   if (dump_section_contents)
1330     dump_data (abfd);
1331   if (disassemble)
1332     disassemble_data (abfd);
1333   if (dump_debugging)
1334     {
1335       PTR dhandle;
1336
1337       dhandle = read_debugging_info (abfd, syms, symcount);
1338       if (dhandle != NULL)
1339         {
1340           if (! print_debugging_info (stdout, dhandle))
1341             fprintf (stderr, "%s: printing debugging information failed\n",
1342                      bfd_get_filename (abfd));
1343         }
1344     }
1345   if (syms)
1346     {
1347       free (syms);
1348       syms = NULL;
1349     }
1350   if (dynsyms)
1351     {
1352       free (dynsyms);
1353       dynsyms = NULL;
1354     }
1355 }
1356
1357 static void
1358 display_file (filename, target)
1359      char *filename;
1360      char *target;
1361 {
1362   bfd *file, *arfile = (bfd *) NULL;
1363
1364   file = bfd_openr (filename, target);
1365   if (file == NULL)
1366     {
1367       bfd_nonfatal (filename);
1368       return;
1369     }
1370
1371   if (bfd_check_format (file, bfd_archive) == true)
1372     {
1373       bfd *last_arfile = NULL;
1374
1375       printf ("In archive %s:\n", bfd_get_filename (file));
1376       for (;;)
1377         {
1378           bfd_set_error (bfd_error_no_error);
1379
1380           arfile = bfd_openr_next_archived_file (file, arfile);
1381           if (arfile == NULL)
1382             {
1383               if (bfd_get_error () != bfd_error_no_more_archived_files)
1384                 {
1385                   bfd_nonfatal (bfd_get_filename (file));
1386                 }
1387               break;
1388             }
1389
1390           display_bfd (arfile);
1391
1392           if (last_arfile != NULL)
1393             bfd_close (last_arfile);
1394           last_arfile = arfile;
1395         }
1396
1397       if (last_arfile != NULL)
1398         bfd_close (last_arfile);
1399     }
1400   else
1401     display_bfd (file);
1402
1403   bfd_close (file);
1404 }
1405 \f
1406 /* Actually display the various requested regions */
1407
1408 static void
1409 dump_data (abfd)
1410      bfd *abfd;
1411 {
1412   asection *section;
1413   bfd_byte *data = 0;
1414   bfd_size_type datasize = 0;
1415   bfd_size_type i;
1416   bfd_size_type start, stop;
1417
1418   for (section = abfd->sections; section != NULL; section =
1419        section->next)
1420     {
1421       int onaline = 16;
1422
1423       if (only == (char *) NULL ||
1424           strcmp (only, section->name) == 0)
1425         {
1426           if (section->flags & SEC_HAS_CONTENTS)
1427             {
1428               printf ("Contents of section %s:\n", section->name);
1429
1430               if (bfd_section_size (abfd, section) == 0)
1431                 continue;
1432               data = (bfd_byte *) xmalloc ((size_t) bfd_section_size (abfd, section));
1433               datasize = bfd_section_size (abfd, section);
1434
1435
1436               bfd_get_section_contents (abfd, section, (PTR) data, 0, bfd_section_size (abfd, section));
1437
1438               if (start_address == (bfd_vma) -1
1439                   || start_address < section->vma)
1440                 start = 0;
1441               else
1442                 start = start_address - section->vma;
1443               if (stop_address == (bfd_vma) -1)
1444                 stop = bfd_section_size (abfd, section);
1445               else
1446                 {
1447                   if (stop_address < section->vma)
1448                     stop = 0;
1449                   else
1450                     stop = stop_address - section->vma;
1451                   if (stop > bfd_section_size (abfd, section))
1452                     stop = bfd_section_size (abfd, section);
1453                 }
1454               for (i = start; i < stop; i += onaline)
1455                 {
1456                   bfd_size_type j;
1457
1458                   printf (" %04lx ", (unsigned long int) (i + section->vma));
1459                   for (j = i; j < i + onaline; j++)
1460                     {
1461                       if (j < stop)
1462                         printf ("%02x", (unsigned) (data[j]));
1463                       else
1464                         printf ("  ");
1465                       if ((j & 3) == 3)
1466                         printf (" ");
1467                     }
1468
1469                   printf (" ");
1470                   for (j = i; j < i + onaline; j++)
1471                     {
1472                       if (j >= stop)
1473                         printf (" ");
1474                       else
1475                         printf ("%c", isprint (data[j]) ? data[j] : '.');
1476                     }
1477                   putchar ('\n');
1478                 }
1479               free (data);
1480             }
1481         }
1482     }
1483 }
1484
1485 /* Should perhaps share code and display with nm? */
1486 static void
1487 dump_symbols (abfd, dynamic)
1488      bfd *abfd;
1489      boolean dynamic;
1490 {
1491   asymbol **current;
1492   long max;
1493   long count;
1494
1495   if (dynamic)
1496     {
1497       current = dynsyms;
1498       max = dynsymcount;
1499       if (max == 0)
1500         return;
1501       printf ("DYNAMIC SYMBOL TABLE:\n");
1502     }
1503   else
1504     {
1505       current = syms;
1506       max = symcount;
1507       if (max == 0)
1508         return;
1509       printf ("SYMBOL TABLE:\n");
1510     }
1511
1512   for (count = 0; count < max; count++)
1513     {
1514       if (*current)
1515         {
1516           bfd *cur_bfd = bfd_asymbol_bfd(*current);
1517           if (cur_bfd)
1518             {
1519               bfd_print_symbol (cur_bfd,
1520                                 stdout,
1521                                 *current, bfd_print_symbol_all);
1522               printf ("\n");
1523             }
1524         }
1525       current++;
1526     }
1527   printf ("\n");
1528   printf ("\n");
1529 }
1530
1531 static void
1532 dump_relocs (abfd)
1533      bfd *abfd;
1534 {
1535   arelent **relpp;
1536   long relcount;
1537   asection *a;
1538
1539   for (a = abfd->sections; a != (asection *) NULL; a = a->next)
1540     {
1541       long relsize;
1542
1543       if (bfd_is_abs_section (a))
1544         continue;
1545       if (bfd_is_und_section (a))
1546         continue;
1547       if (bfd_is_com_section (a))
1548         continue;
1549
1550       if (only)
1551         {
1552           if (strcmp (only, a->name))
1553             continue;
1554         }
1555       else if ((a->flags & SEC_RELOC) == 0)
1556         continue;
1557
1558       relsize = bfd_get_reloc_upper_bound (abfd, a);
1559       if (relsize < 0)
1560         bfd_fatal (bfd_get_filename (abfd));
1561
1562       printf ("RELOCATION RECORDS FOR [%s]:", a->name);
1563
1564       if (relsize == 0)
1565         {
1566           printf (" (none)\n\n");
1567         }
1568       else
1569         {
1570           relpp = (arelent **) xmalloc (relsize);
1571           relcount = bfd_canonicalize_reloc (abfd, a, relpp, syms);
1572           if (relcount < 0)
1573             bfd_fatal (bfd_get_filename (abfd));
1574           else if (relcount == 0)
1575             {
1576               printf (" (none)\n\n");
1577             }
1578           else
1579             {
1580               printf ("\n");
1581               dump_reloc_set (abfd, relpp, relcount);
1582               printf ("\n\n");
1583             }
1584           free (relpp);
1585         }
1586     }
1587 }
1588
1589 static void
1590 dump_dynamic_relocs (abfd)
1591      bfd *abfd;
1592 {
1593   long relsize;
1594   arelent **relpp;
1595   long relcount;
1596
1597   relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
1598   if (relsize < 0)
1599     bfd_fatal (bfd_get_filename (abfd));
1600
1601   printf ("DYNAMIC RELOCATION RECORDS");
1602
1603   if (relsize == 0)
1604     {
1605       printf (" (none)\n\n");
1606     }
1607   else
1608     {
1609       relpp = (arelent **) xmalloc (relsize);
1610       relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
1611       if (relcount < 0)
1612         bfd_fatal (bfd_get_filename (abfd));
1613       else if (relcount == 0)
1614         {
1615           printf (" (none)\n\n");
1616         }
1617       else
1618         {
1619           printf ("\n");
1620           dump_reloc_set (abfd, relpp, relcount);
1621           printf ("\n\n");
1622         }
1623       free (relpp);
1624     }
1625 }
1626
1627 static void
1628 dump_reloc_set (abfd, relpp, relcount)
1629      bfd *abfd;
1630      arelent **relpp;
1631      long relcount;
1632 {
1633   arelent **p;
1634
1635   /* Get column headers lined up reasonably.  */
1636   {
1637     static int width;
1638     if (width == 0)
1639       {
1640         char buf[30];
1641         sprintf_vma (buf, (bfd_vma) -1);
1642         width = strlen (buf) - 7;
1643       }
1644     printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
1645   }
1646
1647   for (p = relpp; relcount && *p != (arelent *) NULL; p++, relcount--)
1648     {
1649       arelent *q = *p;
1650       CONST char *sym_name;
1651       CONST char *section_name;
1652
1653       if (start_address != (bfd_vma) -1
1654           && q->address < start_address)
1655         continue;
1656       if (stop_address != (bfd_vma) -1
1657           && q->address > stop_address)
1658         continue;
1659
1660       if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
1661         {
1662           sym_name = (*(q->sym_ptr_ptr))->name;
1663           section_name = (*(q->sym_ptr_ptr))->section->name;
1664         }
1665       else
1666         {
1667           sym_name = NULL;
1668           section_name = NULL;
1669         }
1670       if (sym_name)
1671         {
1672           printf_vma (q->address);
1673           printf (" %-16s  %s",
1674                   q->howto->name,
1675                   sym_name);
1676         }
1677       else
1678         {
1679           if (section_name == (CONST char *) NULL)
1680             section_name = "*unknown*";
1681           printf_vma (q->address);
1682           printf (" %-16s  [%s]",
1683                   q->howto->name,
1684                   section_name);
1685         }
1686       if (q->addend)
1687         {
1688           printf ("+0x");
1689           printf_vma (q->addend);
1690         }
1691       printf ("\n");
1692     }
1693 }
1694 \f
1695 /* The length of the longest architecture name + 1.  */
1696 #define LONGEST_ARCH sizeof("rs6000:6000")
1697
1698 #ifndef L_tmpnam
1699 #define L_tmpnam 25
1700 #endif
1701
1702 static const char *
1703 endian_string (endian)
1704      enum bfd_endian endian;
1705 {
1706   if (endian == BFD_ENDIAN_BIG)
1707     return "big endian";
1708   else if (endian == BFD_ENDIAN_LITTLE)
1709     return "little endian";
1710   else
1711     return "endianness unknown";
1712 }
1713
1714 /* List the targets that BFD is configured to support, each followed
1715    by its endianness and the architectures it supports.  */
1716
1717 static void
1718 display_target_list ()
1719 {
1720   extern char *tmpnam ();
1721   extern bfd_target *bfd_target_vector[];
1722   char tmparg[L_tmpnam];
1723   char *dummy_name;
1724   int t;
1725
1726   dummy_name = tmpnam (tmparg);
1727   for (t = 0; bfd_target_vector[t]; t++)
1728     {
1729       bfd_target *p = bfd_target_vector[t];
1730       bfd *abfd = bfd_openw (dummy_name, p->name);
1731       int a;
1732
1733       printf ("%s\n (header %s, data %s)\n", p->name,
1734               endian_string (p->header_byteorder),
1735               endian_string (p->byteorder));
1736
1737       if (abfd == NULL)
1738         {
1739           bfd_nonfatal (dummy_name);
1740           continue;
1741         }
1742
1743       if (! bfd_set_format (abfd, bfd_object))
1744         {
1745           if (bfd_get_error () != bfd_error_invalid_operation)
1746             bfd_nonfatal (p->name);
1747           continue;
1748         }
1749
1750       for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
1751         if (bfd_set_arch_mach (abfd, (enum bfd_architecture) a, 0))
1752           printf ("  %s\n",
1753                   bfd_printable_arch_mach ((enum bfd_architecture) a, 0));
1754     }
1755   unlink (dummy_name);
1756 }
1757
1758 /* Print a table showing which architectures are supported for entries
1759    FIRST through LAST-1 of bfd_target_vector (targets across,
1760    architectures down).  */
1761
1762 static void
1763 display_info_table (first, last)
1764      int first;
1765      int last;
1766 {
1767   extern bfd_target *bfd_target_vector[];
1768   extern char *tmpnam ();
1769   char tmparg[L_tmpnam];
1770   int t, a;
1771   char *dummy_name;
1772
1773   /* Print heading of target names.  */
1774   printf ("\n%*s", (int) LONGEST_ARCH, " ");
1775   for (t = first; t < last && bfd_target_vector[t]; t++)
1776     printf ("%s ", bfd_target_vector[t]->name);
1777   putchar ('\n');
1778
1779   dummy_name = tmpnam (tmparg);
1780   for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
1781     if (strcmp (bfd_printable_arch_mach (a, 0), "UNKNOWN!") != 0)
1782       {
1783         printf ("%*s ", (int) LONGEST_ARCH - 1,
1784                 bfd_printable_arch_mach (a, 0));
1785         for (t = first; t < last && bfd_target_vector[t]; t++)
1786           {
1787             bfd_target *p = bfd_target_vector[t];
1788             boolean ok = true;
1789             bfd *abfd = bfd_openw (dummy_name, p->name);
1790
1791             if (abfd == NULL)
1792               {
1793                 bfd_nonfatal (p->name);
1794                 ok = false;
1795               }
1796
1797             if (ok)
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                     ok = false;
1804                   }
1805               }
1806
1807             if (ok)
1808               {
1809                 if (! bfd_set_arch_mach (abfd, a, 0))
1810                   ok = false;
1811               }
1812
1813             if (ok)
1814               printf ("%s ", p->name);
1815             else
1816               {
1817                 int l = strlen (p->name);
1818                 while (l--)
1819                   putchar ('-');
1820                 putchar (' ');
1821               }
1822           }
1823         putchar ('\n');
1824       }
1825   unlink (dummy_name);
1826 }
1827
1828 /* Print tables of all the target-architecture combinations that
1829    BFD has been configured to support.  */
1830
1831 static void
1832 display_target_tables ()
1833 {
1834   int t, columns;
1835   extern bfd_target *bfd_target_vector[];
1836   char *colum;
1837   extern char *getenv ();
1838
1839   columns = 0;
1840   colum = getenv ("COLUMNS");
1841   if (colum != NULL)
1842     columns = atoi (colum);
1843   if (columns == 0)
1844     columns = 80;
1845
1846   t = 0;
1847   while (bfd_target_vector[t] != NULL)
1848     {
1849       int oldt = t, wid;
1850
1851       wid = LONGEST_ARCH + strlen (bfd_target_vector[t]->name) + 1;
1852       ++t;
1853       while (wid < columns && bfd_target_vector[t] != NULL)
1854         {
1855           int newwid;
1856
1857           newwid = wid + strlen (bfd_target_vector[t]->name) + 1;
1858           if (newwid >= columns)
1859             break;
1860           wid = newwid;
1861           ++t;
1862         }
1863       display_info_table (oldt, t);
1864     }
1865 }
1866
1867 static void
1868 display_info ()
1869 {
1870   printf ("BFD header file version %s\n", BFD_VERSION);
1871   display_target_list ();
1872   display_target_tables ();
1873 }
1874
1875 int
1876 main (argc, argv)
1877      int argc;
1878      char **argv;
1879 {
1880   int c;
1881   char *target = default_target;
1882   boolean seenflag = false;
1883
1884   program_name = *argv;
1885   xmalloc_set_program_name (program_name);
1886
1887   START_PROGRESS (program_name, 0);
1888
1889   bfd_init ();
1890
1891   while ((c = getopt_long (argc, argv, "pib:m:VdDlfahrRtTxsSj:w", long_options,
1892                            (int *) 0))
1893          != EOF)
1894     {
1895       if (c != 'l' && c != OPTION_START_ADDRESS && c != OPTION_STOP_ADDRESS)
1896         seenflag = true;
1897       switch (c)
1898         {
1899         case 0:
1900           break;                /* we've been given a long option */
1901         case 'm':
1902           machine = optarg;
1903           break;
1904         case 'j':
1905           only = optarg;
1906           break;
1907         case 'l':
1908           with_line_numbers = 1;
1909           break;
1910         case 'b':
1911           target = optarg;
1912           break;
1913         case 'f':
1914           dump_file_header = true;
1915           break;
1916         case 'i':
1917           formats_info = true;
1918           break;
1919         case 'p':
1920           dump_private_headers = 1;
1921           break;
1922         case 'x':
1923           dump_private_headers = 1;
1924           dump_symtab = 1;
1925           dump_reloc_info = 1;
1926           dump_file_header = true;
1927           dump_ar_hdrs = 1;
1928           dump_section_headers = 1;
1929           break;
1930         case 't':
1931           dump_symtab = 1;
1932           break;
1933         case 'T':
1934           dump_dynamic_symtab = 1;
1935           break;
1936         case 'd':
1937           disassemble = true;
1938           break;
1939         case 'D':
1940           disassemble = disassemble_all = true;
1941           break;
1942         case 'S':
1943           disassemble = true;
1944           with_source_code = true;
1945           break;
1946         case 's':
1947           dump_section_contents = 1;
1948           break;
1949         case 'r':
1950           dump_reloc_info = 1;
1951           break;
1952         case 'R':
1953           dump_dynamic_reloc_info = 1;
1954           break;
1955         case 'a':
1956           dump_ar_hdrs = 1;
1957           break;
1958         case 'h':
1959           dump_section_headers = 1;
1960           break;
1961         case 'H':
1962           usage (stdout, 0);
1963         case 'V':
1964           show_version = 1;
1965           break;
1966         case 'w':
1967           wide_output = 1;
1968           break;
1969         case OPTION_START_ADDRESS:
1970           start_address = parse_vma (optarg, "--start-address");
1971           break;
1972         case OPTION_STOP_ADDRESS:
1973           stop_address = parse_vma (optarg, "--stop-address");
1974           break;
1975         default:
1976           usage (stderr, 1);
1977         }
1978     }
1979
1980   if (show_version)
1981     {
1982       printf ("GNU %s version %s\n", program_name, program_version);
1983       exit (0);
1984     }
1985
1986   if (seenflag == false)
1987     usage (stderr, 1);
1988
1989   if (formats_info)
1990     {
1991       display_info ();
1992     }
1993   else
1994     {
1995       if (optind == argc)
1996         display_file ("a.out", target);
1997       else
1998         for (; optind < argc;)
1999           display_file (argv[optind++], target);
2000     }
2001
2002   END_PROGRESS (program_name);
2003
2004   return 0;
2005 }
This page took 0.138559 seconds and 4 git commands to generate.