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