]> Git Repo - binutils.git/blob - binutils/objdump.c
* COPYING: Update FSF address.
[binutils.git] / binutils / objdump.c
1 /* objdump.c -- dump information about an object file.
2    Copyright 1990, 91, 92, 93, 94, 95, 96, 1997 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 "demangle.h"
28 #include "debug.h"
29 #include "budbg.h"
30
31 #ifdef ANSI_PROTOTYPES
32 #include <stdarg.h>
33 #else
34 #include <varargs.h>
35 #endif
36
37 /* Internal headers for the ELF .stab-dump code - sorry.  */
38 #define BYTES_IN_WORD   32
39 #include "aout/aout64.h"
40
41 #ifdef NEED_DECLARATION_FPRINTF
42 /* This is needed by INIT_DISASSEMBLE_INFO.  */
43 extern int fprintf PARAMS ((FILE *, const char *, ...));
44 #endif
45
46 static char *default_target = NULL;     /* default at runtime */
47
48 static int show_version = 0;            /* show the version number */
49 static int dump_section_contents;       /* -s */
50 static int dump_section_headers;        /* -h */
51 static boolean dump_file_header;        /* -f */
52 static int dump_symtab;                 /* -t */
53 static int dump_dynamic_symtab;         /* -T */
54 static int dump_reloc_info;             /* -r */
55 static int dump_dynamic_reloc_info;     /* -R */
56 static int dump_ar_hdrs;                /* -a */
57 static int dump_private_headers;        /* -p */
58 static int prefix_addresses;            /* --prefix-addresses */
59 static int with_line_numbers;           /* -l */
60 static boolean with_source_code;        /* -S */
61 static int show_raw_insn;               /* --show-raw-insn */
62 static int dump_stab_section_info;      /* --stabs */
63 static int do_demangle;                 /* -C, --demangle */
64 static boolean disassemble;             /* -d */
65 static boolean disassemble_all;         /* -D */
66 static int disassemble_zeroes;          /* --disassemble-zeroes */
67 static boolean formats_info;            /* -i */
68 static char *only;                      /* -j secname */
69 static int wide_output;                 /* -w */
70 static bfd_vma start_address = (bfd_vma) -1; /* --start-address */
71 static bfd_vma stop_address = (bfd_vma) -1;  /* --stop-address */
72 static int dump_debugging;              /* --debugging */
73 static bfd_vma adjust_section_vma = 0;  /* --adjust-vma */
74
75 /* Extra info to pass to the disassembler address printing function.  */
76 struct objdump_disasm_info {
77   bfd *abfd;
78   asection *sec;
79   boolean require_sec;
80 };
81
82 /* Architecture to disassemble for, or default if NULL.  */
83 static char *machine = (char *) NULL;
84
85 /* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN.  */
86 static enum bfd_endian endian = BFD_ENDIAN_UNKNOWN;
87
88 /* The symbol table.  */
89 static asymbol **syms;
90
91 /* Number of symbols in `syms'.  */
92 static long symcount = 0;
93
94 /* The sorted symbol table.  */
95 static asymbol **sorted_syms;
96
97 /* Number of symbols in `sorted_syms'.  */
98 static long sorted_symcount = 0;
99
100 /* The dynamic symbol table.  */
101 static asymbol **dynsyms;
102
103 /* Number of symbols in `dynsyms'.  */
104 static long dynsymcount = 0;
105
106 /* Forward declarations.  */
107
108 static void
109 display_file PARAMS ((char *filename, char *target));
110
111 static void
112 dump_data PARAMS ((bfd *abfd));
113
114 static void
115 dump_relocs PARAMS ((bfd *abfd));
116
117 static void
118 dump_dynamic_relocs PARAMS ((bfd * abfd));
119
120 static void
121 dump_reloc_set PARAMS ((bfd *, asection *, arelent **, long));
122
123 static void
124 dump_symbols PARAMS ((bfd *abfd, boolean dynamic));
125
126 static void
127 display_bfd PARAMS ((bfd *abfd));
128
129 static void
130 objdump_print_value PARAMS ((bfd_vma, struct disassemble_info *, boolean));
131
132 static void
133 objdump_print_symname PARAMS ((bfd *, struct disassemble_info *, asymbol *));
134
135 static asymbol *
136 find_symbol_for_address PARAMS ((bfd *, asection *, bfd_vma, boolean, long *));
137
138 static void
139 objdump_print_addr_with_sym PARAMS ((bfd *, asection *, asymbol *, bfd_vma,
140                                      struct disassemble_info *, boolean));
141
142 static void
143 objdump_print_addr PARAMS ((bfd_vma, struct disassemble_info *, boolean));
144
145 static void
146 objdump_print_address PARAMS ((bfd_vma, struct disassemble_info *));
147
148 static void
149 show_line PARAMS ((bfd *, asection *, bfd_vma));
150
151 static void
152 disassemble_bytes PARAMS ((struct disassemble_info *, disassembler_ftype,
153                            boolean, bfd_byte *, long, long, arelent ***,
154                            arelent **));
155
156 static void
157 disassemble_data PARAMS ((bfd *));
158
159 static const char *
160 endian_string PARAMS ((enum bfd_endian));
161 \f
162 static void
163 usage (stream, status)
164      FILE *stream;
165      int status;
166 {
167   fprintf (stream, "\
168 Usage: %s [-ahifCdDprRtTxsSlw] [-b bfdname] [-m machine] [-j section-name]\n\
169        [--archive-headers] [--target=bfdname] [--debugging] [--disassemble]\n\
170        [--disassemble-all] [--disassemble-zeroes] [--file-headers]\n\
171        [--section-headers] [--headers]\n\
172        [--info] [--section=section-name] [--line-numbers] [--source]\n",
173            program_name);
174   fprintf (stream, "\
175        [--architecture=machine] [--reloc] [--full-contents] [--stabs]\n\
176        [--syms] [--all-headers] [--dynamic-syms] [--dynamic-reloc]\n\
177        [--wide] [--version] [--help] [--private-headers]\n\
178        [--start-address=addr] [--stop-address=addr]\n\
179        [--prefix-addresses] [--[no-]show-raw-insn] [--demangle]\n\
180        [--adjust-vma=offset] [-EB|-EL] [--endian={big|little}] objfile...\n\
181 at least one option besides -l (--line-numbers) must be given\n");
182   list_supported_targets (program_name, stream);
183   if (status == 0)
184     fprintf (stream, "Report bugs to [email protected]\n");
185   exit (status);
186 }
187
188 /* 150 isn't special; it's just an arbitrary non-ASCII char value.  */
189
190 #define OPTION_ENDIAN (150)
191 #define OPTION_START_ADDRESS (OPTION_ENDIAN + 1)
192 #define OPTION_STOP_ADDRESS (OPTION_START_ADDRESS + 1)
193 #define OPTION_ADJUST_VMA (OPTION_STOP_ADDRESS + 1)
194
195 static struct option long_options[]=
196 {
197   {"adjust-vma", required_argument, NULL, OPTION_ADJUST_VMA},
198   {"all-headers", no_argument, NULL, 'x'},
199   {"private-headers", no_argument, NULL, 'p'},
200   {"architecture", required_argument, NULL, 'm'},
201   {"archive-headers", no_argument, NULL, 'a'},
202   {"debugging", no_argument, &dump_debugging, 1},
203   {"demangle", no_argument, &do_demangle, 1},
204   {"disassemble", no_argument, NULL, 'd'},
205   {"disassemble-all", no_argument, NULL, 'D'},
206   {"disassemble-zeroes", no_argument, &disassemble_zeroes, 1},
207   {"dynamic-reloc", no_argument, NULL, 'R'},
208   {"dynamic-syms", no_argument, NULL, 'T'},
209   {"endian", required_argument, NULL, OPTION_ENDIAN},
210   {"file-headers", no_argument, NULL, 'f'},
211   {"full-contents", no_argument, NULL, 's'},
212   {"headers", no_argument, NULL, 'h'},
213   {"help", no_argument, NULL, 'H'},
214   {"info", no_argument, NULL, 'i'},
215   {"line-numbers", no_argument, NULL, 'l'},
216   {"no-show-raw-insn", no_argument, &show_raw_insn, -1},
217   {"prefix-addresses", no_argument, &prefix_addresses, 1},
218   {"reloc", no_argument, NULL, 'r'},
219   {"section", required_argument, NULL, 'j'},
220   {"section-headers", no_argument, NULL, 'h'},
221   {"show-raw-insn", no_argument, &show_raw_insn, 1},
222   {"source", no_argument, NULL, 'S'},
223   {"stabs", no_argument, &dump_stab_section_info, 1},
224   {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
225   {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
226   {"syms", no_argument, NULL, 't'},
227   {"target", required_argument, NULL, 'b'},
228   {"version", no_argument, &show_version, 1},
229   {"wide", no_argument, &wide_output, 'w'},
230   {0, no_argument, 0, 0}
231 };
232 \f
233 static void
234 dump_section_header (abfd, section, ignored)
235      bfd *abfd;
236      asection *section;
237      PTR ignored;
238 {
239   char *comma = "";
240
241   printf ("%3d %-13s %08lx  ", section->index,
242           bfd_get_section_name (abfd, section),
243           (unsigned long) bfd_section_size (abfd, section));
244   printf_vma (bfd_get_section_vma (abfd, section));
245   printf ("  ");
246   printf_vma (section->lma);
247   printf ("  %08lx  2**%u", section->filepos,
248           bfd_get_section_alignment (abfd, section));
249   if (! wide_output)
250     printf ("\n                ");
251   printf ("  ");
252
253 #define PF(x, y) \
254   if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
255
256   PF (SEC_HAS_CONTENTS, "CONTENTS");
257   PF (SEC_ALLOC, "ALLOC");
258   PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
259   PF (SEC_CONSTRUCTOR_TEXT, "CONSTRUCTOR TEXT");
260   PF (SEC_CONSTRUCTOR_DATA, "CONSTRUCTOR DATA");
261   PF (SEC_CONSTRUCTOR_BSS, "CONSTRUCTOR BSS");
262   PF (SEC_LOAD, "LOAD");
263   PF (SEC_RELOC, "RELOC");
264 #ifdef SEC_BALIGN
265   PF (SEC_BALIGN, "BALIGN");
266 #endif
267   PF (SEC_READONLY, "READONLY");
268   PF (SEC_CODE, "CODE");
269   PF (SEC_DATA, "DATA");
270   PF (SEC_ROM, "ROM");
271   PF (SEC_DEBUGGING, "DEBUGGING");
272   PF (SEC_NEVER_LOAD, "NEVER_LOAD");
273   PF (SEC_EXCLUDE, "EXCLUDE");
274   PF (SEC_SORT_ENTRIES, "SORT_ENTRIES");
275
276   if ((section->flags & SEC_LINK_ONCE) != 0)
277     {
278       const char *ls;
279
280       switch (section->flags & SEC_LINK_DUPLICATES)
281         {
282         default:
283           abort ();
284         case SEC_LINK_DUPLICATES_DISCARD:
285           ls = "LINK_ONCE_DISCARD";
286           break;
287         case SEC_LINK_DUPLICATES_ONE_ONLY:
288           ls = "LINK_ONCE_ONE_ONLY";
289           break;
290         case SEC_LINK_DUPLICATES_SAME_SIZE:
291           ls = "LINK_ONCE_SAME_SIZE";
292           break;
293         case SEC_LINK_DUPLICATES_SAME_CONTENTS:
294           ls = "LINK_ONCE_SAME_CONTENTS";
295           break;
296         }
297       printf ("%s%s", comma, ls);
298       comma = ", ";
299     }
300
301   printf ("\n");
302 #undef PF
303 }
304
305 static void
306 dump_headers (abfd)
307      bfd *abfd;
308 {
309   printf ("Sections:\n");
310 #ifndef BFD64
311   printf ("Idx Name          Size      VMA       LMA       File off  Algn\n");
312 #else
313   printf ("Idx Name          Size      VMA               LMA               File off  Algn\n");
314 #endif
315   bfd_map_over_sections (abfd, dump_section_header, (PTR) NULL);
316 }
317 \f
318 static asymbol **
319 slurp_symtab (abfd)
320      bfd *abfd;
321 {
322   asymbol **sy = (asymbol **) NULL;
323   long storage;
324
325   if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
326     {
327       printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd));
328       symcount = 0;
329       return NULL;
330     }
331
332   storage = bfd_get_symtab_upper_bound (abfd);
333   if (storage < 0)
334     bfd_fatal (bfd_get_filename (abfd));
335
336   if (storage)
337     {
338       sy = (asymbol **) xmalloc (storage);
339     }
340   symcount = bfd_canonicalize_symtab (abfd, sy);
341   if (symcount < 0)
342     bfd_fatal (bfd_get_filename (abfd));
343   if (symcount == 0)
344     fprintf (stderr, "%s: %s: No symbols\n",
345              program_name, bfd_get_filename (abfd));
346   return sy;
347 }
348
349 /* Read in the dynamic symbols.  */
350
351 static asymbol **
352 slurp_dynamic_symtab (abfd)
353      bfd *abfd;
354 {
355   asymbol **sy = (asymbol **) NULL;
356   long storage;
357
358   storage = bfd_get_dynamic_symtab_upper_bound (abfd);
359   if (storage < 0)
360     {
361       if (!(bfd_get_file_flags (abfd) & DYNAMIC))
362         {
363           fprintf (stderr, "%s: %s: not a dynamic object\n",
364                    program_name, bfd_get_filename (abfd));
365           dynsymcount = 0;
366           return NULL;
367         }
368
369       bfd_fatal (bfd_get_filename (abfd));
370     }
371
372   if (storage)
373     {
374       sy = (asymbol **) xmalloc (storage);
375     }
376   dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
377   if (dynsymcount < 0)
378     bfd_fatal (bfd_get_filename (abfd));
379   if (dynsymcount == 0)
380     fprintf (stderr, "%s: %s: No dynamic symbols\n",
381              program_name, bfd_get_filename (abfd));
382   return sy;
383 }
384
385 /* Filter out (in place) symbols that are useless for disassembly.
386    COUNT is the number of elements in SYMBOLS.
387    Return the number of useful symbols. */
388
389 static long
390 remove_useless_symbols (symbols, count)
391      asymbol **symbols;
392      long count;
393 {
394   register asymbol **in_ptr = symbols, **out_ptr = symbols;
395
396   while (--count >= 0)
397     {
398       asymbol *sym = *in_ptr++;
399
400       if (sym->name == NULL || sym->name[0] == '\0')
401         continue;
402       if (sym->flags & (BSF_DEBUGGING))
403         continue;
404       if (bfd_is_und_section (sym->section)
405           || bfd_is_com_section (sym->section))
406         continue;
407
408       *out_ptr++ = sym;
409     }
410   return out_ptr - symbols;
411 }
412
413 /* Sort symbols into value order.  */
414
415 static int 
416 compare_symbols (ap, bp)
417      const PTR ap;
418      const PTR bp;
419 {
420   const asymbol *a = *(const asymbol **)ap;
421   const asymbol *b = *(const asymbol **)bp;
422   const char *an, *bn;
423   size_t anl, bnl;
424   boolean af, bf;
425   flagword aflags, bflags;
426
427   if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
428     return 1;
429   else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
430     return -1;
431
432   if (a->section > b->section)
433     return 1;
434   else if (a->section < b->section)
435     return -1;
436
437   an = bfd_asymbol_name (a);
438   bn = bfd_asymbol_name (b);
439   anl = strlen (an);
440   bnl = strlen (bn);
441
442   /* The symbols gnu_compiled and gcc2_compiled convey no real
443      information, so put them after other symbols with the same value.  */
444
445   af = (strstr (an, "gnu_compiled") != NULL
446         || strstr (an, "gcc2_compiled") != NULL);
447   bf = (strstr (bn, "gnu_compiled") != NULL
448         || strstr (bn, "gcc2_compiled") != NULL);
449
450   if (af && ! bf)
451     return 1;
452   if (! af && bf)
453     return -1;
454
455   /* We use a heuristic for the file name, to try to sort it after
456      more useful symbols.  It may not work on non Unix systems, but it
457      doesn't really matter; the only difference is precisely which
458      symbol names get printed.  */
459
460 #define file_symbol(s, sn, snl)                 \
461   (((s)->flags & BSF_FILE) != 0                 \
462    || ((sn)[(snl) - 2] == '.'                   \
463        && ((sn)[(snl) - 1] == 'o'               \
464            || (sn)[(snl) - 1] == 'a')))
465
466   af = file_symbol (a, an, anl);
467   bf = file_symbol (b, bn, bnl);
468
469   if (af && ! bf)
470     return 1;
471   if (! af && bf)
472     return -1;
473
474   /* Try to sort global symbols before local symbols before function
475      symbols before debugging symbols.  */
476
477   aflags = a->flags;
478   bflags = b->flags;
479
480   if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING))
481     {
482       if ((aflags & BSF_DEBUGGING) != 0)
483         return 1;
484       else
485         return -1;
486     }
487   if ((aflags & BSF_FUNCTION) != (bflags & BSF_FUNCTION))
488     {
489       if ((aflags & BSF_FUNCTION) != 0)
490         return -1;
491       else
492         return 1;
493     }
494   if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL))
495     {
496       if ((aflags & BSF_LOCAL) != 0)
497         return 1;
498       else
499         return -1;
500     }
501   if ((aflags & BSF_GLOBAL) != (bflags & BSF_GLOBAL))
502     {
503       if ((aflags & BSF_GLOBAL) != 0)
504         return -1;
505       else
506         return 1;
507     }
508
509   /* Symbols that start with '.' might be section names, so sort them
510      after symbols that don't start with '.'.  */
511   if (an[0] == '.' && bn[0] != '.')
512     return 1;
513   if (an[0] != '.' && bn[0] == '.')
514     return -1;
515
516   /* Finally, if we can't distinguish them in any other way, try to
517      get consistent results by sorting the symbols by name.  */
518   return strcmp (an, bn);
519 }
520
521 /* Sort relocs into address order.  */
522
523 static int
524 compare_relocs (ap, bp)
525      const PTR ap;
526      const PTR bp;
527 {
528   const arelent *a = *(const arelent **)ap;
529   const arelent *b = *(const arelent **)bp;
530
531   if (a->address > b->address)
532     return 1;
533   else if (a->address < b->address)
534     return -1;
535
536   /* So that associated relocations tied to the same address show up
537      in the correct order, we don't do any further sorting.  */
538   if (a > b)
539     return 1;
540   else if (a < b)
541     return -1;
542   else
543     return 0;
544 }
545
546 /* Print VMA to STREAM.  If SKIP_ZEROES is true, omit leading zeroes.  */
547
548 static void
549 objdump_print_value (vma, info, skip_zeroes)
550      bfd_vma vma;
551      struct disassemble_info *info;
552      boolean skip_zeroes;
553 {
554   char buf[30];
555   char *p;
556
557   sprintf_vma (buf, vma);
558   if (! skip_zeroes)
559     p = buf;
560   else
561     {
562       for (p = buf; *p == '0'; ++p)
563         ;
564       if (*p == '\0')
565         --p;
566     }
567   (*info->fprintf_func) (info->stream, "%s", p);
568 }
569
570 /* Print the name of a symbol.  */
571
572 static void
573 objdump_print_symname (abfd, info, sym)
574      bfd *abfd;
575      struct disassemble_info *info;
576      asymbol *sym;
577 {
578   char *alloc;
579   const char *name;
580   const char *print;
581
582   alloc = NULL;
583   name = bfd_asymbol_name (sym);
584   if (! do_demangle || name[0] == '\0')
585     print = name;
586   else
587     {
588       /* Demangle the name.  */
589       if (bfd_get_symbol_leading_char (abfd) == name[0])
590         ++name;
591
592       alloc = cplus_demangle (name, DMGL_ANSI | DMGL_PARAMS);
593       if (alloc == NULL)
594         print = name;
595       else
596         print = alloc;
597     }
598
599   if (info != NULL)
600     (*info->fprintf_func) (info->stream, "%s", print);
601   else
602     printf ("%s", print);
603
604   if (alloc != NULL)
605     free (alloc);
606 }
607
608 /* Locate a symbol given a bfd, a section, and a VMA.  If REQUIRE_SEC
609    is true, then always require the symbol to be in the section.  This
610    returns NULL if there is no suitable symbol.  If PLACE is not NULL,
611    then *PLACE is set to the index of the symbol in sorted_syms.  */
612
613 static asymbol *
614 find_symbol_for_address (abfd, sec, vma, require_sec, place)
615      bfd *abfd;
616      asection *sec;
617      bfd_vma vma;
618      boolean require_sec;
619      long *place;
620 {
621   /* @@ Would it speed things up to cache the last two symbols returned,
622      and maybe their address ranges?  For many processors, only one memory
623      operand can be present at a time, so the 2-entry cache wouldn't be
624      constantly churned by code doing heavy memory accesses.  */
625
626   /* Indices in `sorted_syms'.  */
627   long min = 0;
628   long max = sorted_symcount;
629   long thisplace;
630
631   if (sorted_symcount < 1)
632     return NULL;
633
634   /* Perform a binary search looking for the closest symbol to the
635      required value.  We are searching the range (min, max].  */
636   while (min + 1 < max)
637     {
638       asymbol *sym;
639
640       thisplace = (max + min) / 2;
641       sym = sorted_syms[thisplace];
642
643       if (bfd_asymbol_value (sym) > vma)
644         max = thisplace;
645       else if (bfd_asymbol_value (sym) < vma)
646         min = thisplace;
647       else
648         {
649           min = thisplace;
650           break;
651         }
652     }
653
654   /* The symbol we want is now in min, the low end of the range we
655      were searching.  If there are several symbols with the same
656      value, we want the first one.  */
657   thisplace = min;
658   while (thisplace > 0
659          && (bfd_asymbol_value (sorted_syms[thisplace])
660              == bfd_asymbol_value (sorted_syms[thisplace - 1])))
661     --thisplace;
662
663   /* If the file is relocateable, and the symbol could be from this
664      section, prefer a symbol from this section over symbols from
665      others, even if the other symbol's value might be closer.
666        
667      Note that this may be wrong for some symbol references if the
668      sections have overlapping memory ranges, but in that case there's
669      no way to tell what's desired without looking at the relocation
670      table.  */
671
672   if (sorted_syms[thisplace]->section != sec
673       && (require_sec
674           || ((abfd->flags & HAS_RELOC) != 0
675               && vma >= bfd_get_section_vma (abfd, sec)
676               && vma < (bfd_get_section_vma (abfd, sec)
677                         + bfd_section_size (abfd, sec)))))
678     {
679       long i;
680
681       for (i = thisplace + 1; i < sorted_symcount; i++)
682         {
683           if (bfd_asymbol_value (sorted_syms[i])
684               != bfd_asymbol_value (sorted_syms[thisplace]))
685             break;
686         }
687       --i;
688       for (; i >= 0; i--)
689         {
690           if (sorted_syms[i]->section == sec
691               && (i == 0
692                   || sorted_syms[i - 1]->section != sec
693                   || (bfd_asymbol_value (sorted_syms[i])
694                       != bfd_asymbol_value (sorted_syms[i - 1]))))
695             {
696               thisplace = i;
697               break;
698             }
699         }
700
701       if (sorted_syms[thisplace]->section != sec)
702         {
703           /* We didn't find a good symbol with a smaller value.
704              Look for one with a larger value.  */
705           for (i = thisplace + 1; i < sorted_symcount; i++)
706             {
707               if (sorted_syms[i]->section == sec)
708                 {
709                   thisplace = i;
710                   break;
711                 }
712             }
713         }
714
715       if (sorted_syms[thisplace]->section != sec
716           && (require_sec
717               || ((abfd->flags & HAS_RELOC) != 0
718                   && vma >= bfd_get_section_vma (abfd, sec)
719                   && vma < (bfd_get_section_vma (abfd, sec)
720                             + bfd_section_size (abfd, sec)))))
721         {
722           /* There is no suitable symbol.  */
723           return NULL;
724         }
725     }
726
727   if (place != NULL)
728     *place = thisplace;
729
730   return sorted_syms[thisplace];
731 }
732
733 /* Print an address to INFO symbolically.  */
734
735 static void
736 objdump_print_addr_with_sym (abfd, sec, sym, vma, info, skip_zeroes)
737      bfd *abfd;
738      asection *sec;
739      asymbol *sym;
740      bfd_vma vma;
741      struct disassemble_info *info;
742      boolean skip_zeroes;
743 {
744   objdump_print_value (vma, info, skip_zeroes);
745
746   if (sym == NULL)
747     {
748       bfd_vma secaddr;
749
750       (*info->fprintf_func) (info->stream, " <%s",
751                              bfd_get_section_name (abfd, sec));
752       secaddr = bfd_get_section_vma (abfd, sec);
753       if (vma < secaddr)
754         {
755           (*info->fprintf_func) (info->stream, "-");
756           objdump_print_value (secaddr - vma, info, true);
757         }
758       else if (vma > secaddr)
759         {
760           (*info->fprintf_func) (info->stream, "+");
761           objdump_print_value (vma - secaddr, info, true);
762         }
763       (*info->fprintf_func) (info->stream, ">");
764     }
765   else
766     {
767       (*info->fprintf_func) (info->stream, " <");
768       objdump_print_symname (abfd, info, sym);
769       if (bfd_asymbol_value (sym) > vma)
770         {
771           (*info->fprintf_func) (info->stream, "-");
772           objdump_print_value (bfd_asymbol_value (sym) - vma, info, true);
773         }
774       else if (vma > bfd_asymbol_value (sym))
775         {
776           (*info->fprintf_func) (info->stream, "+");
777           objdump_print_value (vma - bfd_asymbol_value (sym), info, true);
778         }
779       (*info->fprintf_func) (info->stream, ">");
780     }
781 }
782
783 /* Print VMA to INFO, symbolically if possible.  If SKIP_ZEROES is
784    true, don't output leading zeroes.  */
785
786 static void
787 objdump_print_addr (vma, info, skip_zeroes)
788      bfd_vma vma;
789      struct disassemble_info *info;
790      boolean skip_zeroes;
791 {
792   struct objdump_disasm_info *aux;
793   asymbol *sym;
794
795   if (sorted_symcount < 1)
796     {
797       objdump_print_value (vma, info, skip_zeroes);
798       return;
799     }
800
801   aux = (struct objdump_disasm_info *) info->application_data;
802   sym = find_symbol_for_address (aux->abfd, aux->sec, vma, aux->require_sec,
803                                  (long *) NULL);
804   objdump_print_addr_with_sym (aux->abfd, aux->sec, sym, vma, info,
805                                skip_zeroes);
806 }
807
808 /* Print VMA to INFO.  This function is passed to the disassembler
809    routine.  */
810
811 static void
812 objdump_print_address (vma, info)
813      bfd_vma vma;
814      struct disassemble_info *info;
815 {
816   objdump_print_addr (vma, info, ! prefix_addresses);
817 }
818
819 /* Hold the last function name and the last line number we displayed
820    in a disassembly.  */
821
822 static char *prev_functionname;
823 static unsigned int prev_line;
824
825 /* We keep a list of all files that we have seen when doing a
826    dissassembly with source, so that we know how much of the file to
827    display.  This can be important for inlined functions.  */
828
829 struct print_file_list
830 {
831   struct print_file_list *next;
832   char *filename;
833   unsigned int line;
834   FILE *f;
835 };
836
837 static struct print_file_list *print_files;
838
839 /* The number of preceding context lines to show when we start
840    displaying a file for the first time.  */
841
842 #define SHOW_PRECEDING_CONTEXT_LINES (5)
843
844 /* Skip ahead to a given line in a file, optionally printing each
845    line.  */
846
847 static void
848 skip_to_line PARAMS ((struct print_file_list *, unsigned int, boolean));
849
850 static void
851 skip_to_line (p, line, show)
852      struct print_file_list *p;
853      unsigned int line;
854      boolean show;
855 {
856   while (p->line < line)
857     {
858       char buf[100];
859
860       if (fgets (buf, sizeof buf, p->f) == NULL)
861         {
862           fclose (p->f);
863           p->f = NULL;
864           break;
865         }
866
867       if (show)
868         printf ("%s", buf);
869
870       if (strchr (buf, '\n') != NULL)
871         ++p->line;
872     }
873 }  
874
875 /* Show the line number, or the source line, in a dissassembly
876    listing.  */
877
878 static void
879 show_line (abfd, section, off)
880      bfd *abfd;
881      asection *section;
882      bfd_vma off;
883 {
884   CONST char *filename;
885   CONST char *functionname;
886   unsigned int line;
887
888   if (! with_line_numbers && ! with_source_code)
889     return;
890
891   if (! bfd_find_nearest_line (abfd, section, syms, off, &filename,
892                                &functionname, &line))
893     return;
894
895   if (filename != NULL && *filename == '\0')
896     filename = NULL;
897   if (functionname != NULL && *functionname == '\0')
898     functionname = NULL;
899
900   if (with_line_numbers)
901     {
902       if (functionname != NULL
903           && (prev_functionname == NULL
904               || strcmp (functionname, prev_functionname) != 0))
905         printf ("%s():\n", functionname);
906       if (line > 0 && line != prev_line)
907         printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
908     }
909
910   if (with_source_code
911       && filename != NULL
912       && line > 0)
913     {
914       struct print_file_list **pp, *p;
915
916       for (pp = &print_files; *pp != NULL; pp = &(*pp)->next)
917         if (strcmp ((*pp)->filename, filename) == 0)
918           break;
919       p = *pp;
920
921       if (p != NULL)
922         {
923           if (p != print_files)
924             {
925               int l;
926
927               /* We have reencountered a file name which we saw
928                  earlier.  This implies that either we are dumping out
929                  code from an included file, or the same file was
930                  linked in more than once.  There are two common cases
931                  of an included file: inline functions in a header
932                  file, and a bison or flex skeleton file.  In the
933                  former case we want to just start printing (but we
934                  back up a few lines to give context); in the latter
935                  case we want to continue from where we left off.  I
936                  can't think of a good way to distinguish the cases,
937                  so I used a heuristic based on the file name.  */
938               if (strcmp (p->filename + strlen (p->filename) - 2, ".h") != 0)
939                 l = p->line;
940               else
941                 {
942                   l = line - SHOW_PRECEDING_CONTEXT_LINES;
943                   if (l <= 0)
944                     l = 1;
945                 }
946
947               if (p->f == NULL)
948                 {
949                   p->f = fopen (p->filename, "r");
950                   p->line = 0;
951                 }
952               if (p->f != NULL)
953                 skip_to_line (p, l, false);
954
955               if (print_files->f != NULL)
956                 {
957                   fclose (print_files->f);
958                   print_files->f = NULL;
959                 }
960             }
961
962           if (p->f != NULL)
963             {
964               skip_to_line (p, line, true);
965               *pp = p->next;
966               p->next = print_files;
967               print_files = p;
968             }
969         }
970       else
971         {
972           FILE *f;
973
974           f = fopen (filename, "r");
975           if (f != NULL)
976             {
977               int l;
978
979               p = ((struct print_file_list *)
980                    xmalloc (sizeof (struct print_file_list)));
981               p->filename = xmalloc (strlen (filename) + 1);
982               strcpy (p->filename, filename);
983               p->line = 0;
984               p->f = f;
985
986               if (print_files != NULL && print_files->f != NULL)
987                 {
988                   fclose (print_files->f);
989                   print_files->f = NULL;
990                 }
991               p->next = print_files;
992               print_files = p;
993
994               l = line - SHOW_PRECEDING_CONTEXT_LINES;
995               if (l <= 0)
996                 l = 1;
997               skip_to_line (p, l, false);
998               if (p->f != NULL)
999                 skip_to_line (p, line, true);
1000             }
1001         }
1002     }
1003
1004   if (functionname != NULL
1005       && (prev_functionname == NULL
1006           || strcmp (functionname, prev_functionname) != 0))
1007     {
1008       if (prev_functionname != NULL)
1009         free (prev_functionname);
1010       prev_functionname = xmalloc (strlen (functionname) + 1);
1011       strcpy (prev_functionname, functionname);
1012     }
1013
1014   if (line > 0 && line != prev_line)
1015     prev_line = line;
1016 }
1017
1018 /* Pseudo FILE object for strings.  */
1019 typedef struct {
1020   char *buffer;
1021   char *current;
1022 } SFILE;
1023
1024 /* sprintf to a "stream" */
1025
1026 #ifdef ANSI_PROTOTYPES
1027 static int
1028 objdump_sprintf (SFILE *f, const char *format, ...)
1029 {
1030   int n;
1031   va_list args;
1032
1033   va_start (args, format);
1034   vsprintf (f->current, format, args);
1035   f->current += n = strlen (f->current);
1036   va_end (args);
1037   return n;
1038 }
1039 #else
1040 static int
1041 objdump_sprintf (va_alist)
1042      va_dcl
1043 {
1044   int n;
1045   SFILE *f;
1046   const char *format;
1047   va_list args;
1048
1049   va_start (args);
1050   f = va_arg (args, SFILE *);
1051   format = va_arg (args, const char *);
1052   vsprintf (f->current, format, args);
1053   f->current += n = strlen (f->current);
1054   va_end (args);
1055   return n;
1056 }
1057 #endif
1058
1059 /* The number of zeroes we want to see before we start skipping them.
1060    The number is arbitrarily chosen.  */
1061
1062 #define SKIP_ZEROES (8)
1063
1064 /* The number of zeroes to skip at the end of a section.  If the
1065    number of zeroes at the end is between SKIP_ZEROES_AT_END and
1066    SKIP_ZEROES, they will be disassembled.  If there are fewer than
1067    SKIP_ZEROES_AT_END, they will be skipped.  This is a heuristic
1068    attempt to avoid disassembling zeroes inserted by section
1069    alignment.  */
1070
1071 #define SKIP_ZEROES_AT_END (3)
1072
1073 /* Disassemble some data in memory between given values.  */
1074
1075 static void
1076 disassemble_bytes (info, disassemble_fn, insns, data, start, stop, relppp,
1077                    relppend)
1078      struct disassemble_info *info;
1079      disassembler_ftype disassemble_fn;
1080      boolean insns;
1081      bfd_byte *data;
1082      long start;
1083      long stop;
1084      arelent ***relppp;
1085      arelent **relppend;
1086 {
1087   struct objdump_disasm_info *aux;
1088   asection *section;
1089   int bytes_per_line;
1090   boolean done_dot;
1091   int skip_addr_chars;
1092   long i;
1093
1094   aux = (struct objdump_disasm_info *) info->application_data;
1095   section = aux->sec;
1096
1097   if (insns)
1098     bytes_per_line = 4;
1099   else
1100     bytes_per_line = 16;
1101
1102   /* Figure out how many characters to skip at the start of an
1103      address, to make the disassembly look nicer.  We discard leading
1104      zeroes in chunks of 4, ensuring that there is always a leading
1105      zero remaining.  */
1106   skip_addr_chars = 0;
1107   if (! prefix_addresses)
1108     {
1109       char buf[30];
1110       char *s;
1111
1112       sprintf_vma (buf,
1113                    section->vma + bfd_section_size (section->owner, section));
1114       s = buf;
1115       while (s[0] == '0' && s[1] == '0' && s[2] == '0' && s[3] == '0'
1116              && s[4] == '0')
1117         {
1118           skip_addr_chars += 4;
1119           s += 4;
1120         }
1121     }
1122
1123   info->insn_info_valid = 0;
1124
1125   done_dot = false;
1126   i = start;
1127   while (i < stop)
1128     {
1129       long z;
1130       int bytes;
1131       boolean need_nl = false;
1132
1133       /* If we see more than SKIP_ZEROES bytes of zeroes, we just
1134          print `...'.  */
1135       for (z = i; z < stop; z++)
1136         if (data[z] != 0)
1137           break;
1138       if (! disassemble_zeroes
1139           && (info->insn_info_valid == 0
1140               || info->branch_delay_insns == 0)
1141           && (z - i >= SKIP_ZEROES
1142               || (z == stop && z - i < SKIP_ZEROES_AT_END)))
1143         {
1144           printf ("\t...\n");
1145
1146           /* If there are more nonzero bytes to follow, we only skip
1147              zeroes in multiples of 4, to try to avoid running over
1148              the start of an instruction which happens to start with
1149              zero.  */
1150           if (z != stop)
1151             z = i + ((z - i) &~ 3);
1152
1153           bytes = z - i;
1154         }
1155       else
1156         {
1157           char buf[1000];
1158           SFILE sfile;
1159           int bpc, pb = 0;
1160
1161           done_dot = false;
1162
1163           if (with_line_numbers || with_source_code)
1164             show_line (aux->abfd, section, i);
1165
1166           if (! prefix_addresses)
1167             {
1168               char *s;
1169
1170               sprintf_vma (buf, section->vma + i);
1171               for (s = buf + skip_addr_chars; *s == '0'; s++)
1172                 *s = ' ';
1173               if (*s == '\0')
1174                 *--s = '0';
1175               printf ("%s:\t", buf + skip_addr_chars);
1176             }
1177           else
1178             {
1179               aux->require_sec = true;
1180               objdump_print_address (section->vma + i, info);
1181               aux->require_sec = false;
1182               putchar (' ');
1183             }
1184
1185           if (insns)
1186             {
1187               sfile.buffer = sfile.current = buf;
1188               info->fprintf_func = (fprintf_ftype) objdump_sprintf;
1189               info->stream = (FILE *) &sfile;
1190               info->bytes_per_line = 0;
1191               info->bytes_per_chunk = 0;
1192               bytes = (*disassemble_fn) (section->vma + i, info);
1193               info->fprintf_func = (fprintf_ftype) fprintf;
1194               info->stream = stdout;
1195               if (info->bytes_per_line != 0)
1196                 bytes_per_line = info->bytes_per_line;
1197               if (bytes < 0)
1198                 break;
1199             }
1200           else
1201             {
1202               long j;
1203
1204               bytes = bytes_per_line;
1205               if (i + bytes > stop)
1206                 bytes = stop - i;
1207
1208               for (j = i; j < i + bytes; ++j)
1209                 {
1210                   if (isprint (data[j]))
1211                     buf[j - i] = data[j];
1212                   else
1213                     buf[j - i] = '.';
1214                 }
1215               buf[j - i] = '\0';
1216             }
1217
1218           if (prefix_addresses
1219               ? show_raw_insn > 0
1220               : show_raw_insn >= 0)
1221             {
1222               long j;
1223
1224               /* If ! prefix_addresses and ! wide_output, we print
1225                  bytes_per_line bytes per line.  */
1226               pb = bytes;
1227               if (pb > bytes_per_line && ! prefix_addresses && ! wide_output)
1228                 pb = bytes_per_line;
1229
1230               if (info->bytes_per_chunk)
1231                 bpc = info->bytes_per_chunk;
1232               else
1233                 bpc = 1;
1234
1235               for (j = i; j < i + pb; j += bpc)
1236                 {
1237                   int k;
1238                   if (bpc > 1 && info->display_endian == BFD_ENDIAN_LITTLE)
1239                     {
1240                       for (k = bpc - 1; k >= 0; k--)
1241                         printf ("%02x", (unsigned) data[j + k]);
1242                       putchar (' ');
1243                     }
1244                   else
1245                     {
1246                       for (k = 0; k < bpc; k++)
1247                         printf ("%02x", (unsigned) data[j + k]);
1248                       putchar (' ');
1249                     }
1250                 }
1251
1252               for (; pb < bytes_per_line; pb += bpc)
1253                 {
1254                   int k;
1255
1256                   for (k = 0; k < bpc; k++)
1257                     printf ("  ");
1258                   putchar (' ');
1259                 }
1260
1261               /* Separate raw data from instruction by extra space.  */
1262               if (insns)
1263                 putchar ('\t');
1264               else
1265                 printf ("    ");
1266             }
1267
1268           printf ("%s", buf);
1269
1270           if (prefix_addresses
1271               ? show_raw_insn > 0
1272               : show_raw_insn >= 0)
1273             {
1274               while (pb < bytes)
1275                 {
1276                   long j;
1277                   char *s;
1278
1279                   putchar ('\n');
1280                   j = i + pb;
1281
1282                   sprintf_vma (buf, section->vma + j);
1283                   for (s = buf + skip_addr_chars; *s == '0'; s++)
1284                     *s = ' ';
1285                   if (*s == '\0')
1286                     *--s = '0';
1287                   printf ("%s:\t", buf + skip_addr_chars);
1288
1289                   pb += bytes_per_line;
1290                   if (pb > bytes)
1291                     pb = bytes;
1292                   for (; j < i + pb; j += bpc)
1293                     {
1294                       int k;
1295
1296                       if (bpc > 1 && info->display_endian == BFD_ENDIAN_LITTLE)
1297                         {
1298                           for (k = bpc - 1; k >= 0; k--)
1299                             printf ("%02x", (unsigned) data[j + k]);
1300                           putchar (' ');
1301                         }
1302                       else
1303                         {
1304                           for (k = 0; k < bpc; k++)
1305                             printf ("%02x", (unsigned) data[j + k]);
1306                           putchar (' ');
1307                         }
1308                     }
1309                 }
1310             }
1311
1312           if (!wide_output)
1313             putchar ('\n');
1314           else
1315             need_nl = true;
1316         }
1317
1318       if (dump_reloc_info
1319           && (section->flags & SEC_RELOC) != 0)
1320         {
1321           while ((*relppp) < relppend
1322                  && ((**relppp)->address >= (bfd_vma) i
1323                      && (**relppp)->address < (bfd_vma) i + bytes))
1324             {
1325               arelent *q;
1326
1327               q = **relppp;
1328
1329               if (wide_output)
1330                 putchar ('\t');
1331               else
1332                 printf ("\t\t\t");
1333
1334               objdump_print_value (section->vma + q->address, info, true);
1335
1336               printf (": %s\t", q->howto->name);
1337
1338               if (q->sym_ptr_ptr == NULL || *q->sym_ptr_ptr == NULL)
1339                 printf ("*unknown*");
1340               else
1341                 {
1342                   const char *sym_name;
1343
1344                   sym_name = bfd_asymbol_name (*q->sym_ptr_ptr);
1345                   if (sym_name != NULL && *sym_name != '\0')
1346                     objdump_print_symname (aux->abfd, info, *q->sym_ptr_ptr);
1347                   else
1348                     {
1349                       asection *sym_sec;
1350
1351                       sym_sec = bfd_get_section (*q->sym_ptr_ptr);
1352                       sym_name = bfd_get_section_name (aux->abfd, sym_sec);
1353                       if (sym_name == NULL || *sym_name == '\0')
1354                         sym_name = "*unknown*";
1355                       printf ("%s", sym_name);
1356                     }
1357                 }
1358
1359               if (q->addend)
1360                 {
1361                   printf ("+0x");
1362                   objdump_print_value (q->addend, info, true);
1363                 }
1364
1365               printf ("\n");
1366               need_nl = false;
1367               ++(*relppp);
1368             }
1369         }
1370
1371       if (need_nl)
1372         printf ("\n");
1373
1374       i += bytes;
1375     }
1376 }
1377
1378 /* Disassemble the contents of an object file.  */
1379
1380 static void
1381 disassemble_data (abfd)
1382      bfd *abfd;
1383 {
1384   long i;
1385   disassembler_ftype disassemble_fn;
1386   struct disassemble_info disasm_info;
1387   struct objdump_disasm_info aux;
1388   asection *section;
1389
1390   print_files = NULL;
1391   prev_functionname = NULL;
1392   prev_line = -1;
1393
1394   /* We make a copy of syms to sort.  We don't want to sort syms
1395      because that will screw up the relocs.  */
1396   sorted_syms = (asymbol **) xmalloc (symcount * sizeof (asymbol *));
1397   memcpy (sorted_syms, syms, symcount * sizeof (asymbol *));
1398
1399   sorted_symcount = remove_useless_symbols (sorted_syms, symcount);
1400
1401   /* Sort the symbols into section and symbol order */
1402   qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols);
1403
1404   INIT_DISASSEMBLE_INFO(disasm_info, stdout, fprintf);
1405   disasm_info.application_data = (PTR) &aux;
1406   aux.abfd = abfd;
1407   aux.require_sec = false;
1408   disasm_info.print_address_func = objdump_print_address;
1409
1410   if (machine != (char *) NULL)
1411     {
1412       const bfd_arch_info_type *info = bfd_scan_arch (machine);
1413       if (info == NULL)
1414         {
1415           fprintf (stderr, "%s: Can't use supplied machine %s\n",
1416                    program_name,
1417                    machine);
1418           exit (1);
1419         }
1420       abfd->arch_info = info;
1421     }
1422
1423   if (endian != BFD_ENDIAN_UNKNOWN)
1424     {
1425       struct bfd_target *xvec;
1426
1427       xvec = (struct bfd_target *) xmalloc (sizeof (struct bfd_target));
1428       memcpy (xvec, abfd->xvec, sizeof (struct bfd_target));
1429       xvec->byteorder = endian;
1430       abfd->xvec = xvec;
1431     }
1432
1433   disassemble_fn = disassembler (abfd);
1434   if (!disassemble_fn)
1435     {
1436       fprintf (stderr, "%s: Can't disassemble for architecture %s\n",
1437                program_name,
1438                bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
1439       exit (1);
1440     }
1441
1442   disasm_info.flavour = bfd_get_flavour (abfd);
1443   disasm_info.arch = bfd_get_arch (abfd);
1444   disasm_info.mach = bfd_get_mach (abfd);
1445   if (bfd_big_endian (abfd))
1446     disasm_info.endian = BFD_ENDIAN_BIG;
1447   else if (bfd_little_endian (abfd))
1448     disasm_info.endian = BFD_ENDIAN_LITTLE;
1449   else
1450     /* ??? Aborting here seems too drastic.  We could default to big or little
1451        instead.  */
1452     disasm_info.endian = BFD_ENDIAN_UNKNOWN;
1453
1454   for (section = abfd->sections;
1455        section != (asection *) NULL;
1456        section = section->next)
1457     {
1458       bfd_byte *data = NULL;
1459       bfd_size_type datasize = 0;
1460       arelent **relbuf = NULL;
1461       arelent **relpp = NULL;
1462       arelent **relppend = NULL;
1463       long stop;
1464
1465       if ((section->flags & SEC_LOAD) == 0
1466           || (! disassemble_all
1467               && only == NULL
1468               && (section->flags & SEC_CODE) == 0))
1469         continue;
1470       if (only != (char *) NULL && strcmp (only, section->name) != 0)
1471         continue;
1472
1473       if (dump_reloc_info
1474           && (section->flags & SEC_RELOC) != 0)
1475         {
1476           long relsize;
1477
1478           relsize = bfd_get_reloc_upper_bound (abfd, section);
1479           if (relsize < 0)
1480             bfd_fatal (bfd_get_filename (abfd));
1481
1482           if (relsize > 0)
1483             {
1484               long relcount;
1485
1486               relbuf = (arelent **) xmalloc (relsize);
1487               relcount = bfd_canonicalize_reloc (abfd, section, relbuf, syms);
1488               if (relcount < 0)
1489                 bfd_fatal (bfd_get_filename (abfd));
1490
1491               /* Sort the relocs by address.  */
1492               qsort (relbuf, relcount, sizeof (arelent *), compare_relocs);
1493
1494               relpp = relbuf;
1495               relppend = relpp + relcount;
1496             }
1497         }
1498
1499       printf ("Disassembly of section %s:\n", section->name);
1500
1501       datasize = bfd_get_section_size_before_reloc (section);
1502       if (datasize == 0)
1503         continue;
1504
1505       data = (bfd_byte *) xmalloc ((size_t) datasize);
1506
1507       bfd_get_section_contents (abfd, section, data, 0, datasize);
1508
1509       aux.sec = section;
1510       disasm_info.buffer = data;
1511       disasm_info.buffer_vma = section->vma;
1512       disasm_info.buffer_length = datasize;
1513       if (start_address == (bfd_vma) -1
1514           || start_address < disasm_info.buffer_vma)
1515         i = 0;
1516       else
1517         i = start_address - disasm_info.buffer_vma;
1518       if (stop_address == (bfd_vma) -1)
1519         stop = datasize;
1520       else
1521         {
1522           if (stop_address < disasm_info.buffer_vma)
1523             stop = 0;
1524           else
1525             stop = stop_address - disasm_info.buffer_vma;
1526           if (stop > disasm_info.buffer_length)
1527             stop = disasm_info.buffer_length;
1528         }
1529
1530       if (prefix_addresses)
1531         disassemble_bytes (&disasm_info, disassemble_fn, true, data, i, stop,
1532                            &relpp, relppend);
1533       else
1534         {
1535           asymbol *sym;
1536           long place;
1537
1538           sym = find_symbol_for_address (abfd, section, section->vma + i,
1539                                          true, &place);
1540           ++place;
1541           while (i < stop)
1542             {
1543               asymbol *nextsym;
1544               long nextstop;
1545               boolean insns;
1546
1547               if (sym != NULL && bfd_asymbol_value (sym) <= section->vma + i)
1548                 disasm_info.symbol = sym;
1549               else
1550                 disasm_info.symbol = NULL;
1551
1552               printf ("\n");
1553               objdump_print_addr_with_sym (abfd, section, sym,
1554                                            section->vma + i,
1555                                            &disasm_info,
1556                                            false);
1557               printf (":\n");
1558
1559               if (sym != NULL && bfd_asymbol_value (sym) > section->vma + i)
1560                 nextsym = sym;
1561               else if (sym == NULL)
1562                 nextsym = NULL;
1563               else
1564                 {
1565                   while (place < sorted_symcount
1566                          && (sorted_syms[place]->section != section
1567                              || (bfd_asymbol_value (sorted_syms[place])
1568                                  <= bfd_asymbol_value (sym))))
1569                     ++place;
1570                   if (place >= sorted_symcount)
1571                     nextsym = NULL;
1572                   else
1573                     nextsym = sorted_syms[place];
1574                 }
1575
1576               if (sym != NULL && bfd_asymbol_value (sym) > section->vma + i)
1577                 {
1578                   nextstop = bfd_asymbol_value (sym) - section->vma;
1579                   if (nextstop > stop)
1580                     nextstop = stop;
1581                 }
1582               else if (nextsym == NULL)
1583                 nextstop = stop;
1584               else
1585                 {
1586                   nextstop = bfd_asymbol_value (nextsym) - section->vma;
1587                   if (nextstop > stop)
1588                     nextstop = stop;
1589                 }
1590
1591               /* If a symbol is explicitly marked as being an object
1592                  rather than a function, just dump the bytes without
1593                  disassembling them.  */
1594               if (disassemble_all
1595                   || sym == NULL
1596                   || bfd_asymbol_value (sym) > section->vma + i
1597                   || ((sym->flags & BSF_OBJECT) == 0
1598                       && (strstr (bfd_asymbol_name (sym), "gnu_compiled")
1599                           == NULL)
1600                       && (strstr (bfd_asymbol_name (sym), "gcc2_compiled")
1601                           == NULL))
1602                   || (sym->flags & BSF_FUNCTION) != 0)
1603                 insns = true;
1604               else
1605                 insns = false;
1606
1607               disassemble_bytes (&disasm_info, disassemble_fn, insns, data, i,
1608                                  nextstop, &relpp, relppend);
1609
1610               i = nextstop;
1611               sym = nextsym;
1612             }
1613         }
1614
1615       free (data);
1616       if (relbuf != NULL)
1617         free (relbuf);
1618     }
1619   free (sorted_syms);
1620 }
1621 \f
1622
1623 /* Define a table of stab values and print-strings.  We wish the initializer
1624    could be a direct-mapped table, but instead we build one the first
1625    time we need it.  */
1626
1627 static void dump_section_stabs PARAMS ((bfd *abfd, char *stabsect_name,
1628                                         char *strsect_name));
1629
1630 /* Dump the stabs sections from an object file that has a section that
1631    uses Sun stabs encoding.  */
1632
1633 static void
1634 dump_stabs (abfd)
1635      bfd *abfd;
1636 {
1637   dump_section_stabs (abfd, ".stab", ".stabstr");
1638   dump_section_stabs (abfd, ".stab.excl", ".stab.exclstr");
1639   dump_section_stabs (abfd, ".stab.index", ".stab.indexstr");
1640   dump_section_stabs (abfd, "$GDB_SYMBOLS$", "$GDB_STRINGS$");
1641 }
1642
1643 static bfd_byte *stabs;
1644 static bfd_size_type stab_size;
1645
1646 static char *strtab;
1647 static bfd_size_type stabstr_size;
1648
1649 /* Read ABFD's stabs section STABSECT_NAME into `stabs'
1650    and string table section STRSECT_NAME into `strtab'.
1651    If the section exists and was read, allocate the space and return true.
1652    Otherwise return false.  */
1653
1654 static boolean
1655 read_section_stabs (abfd, stabsect_name, strsect_name)
1656      bfd *abfd;
1657      char *stabsect_name;
1658      char *strsect_name;
1659 {
1660   asection *stabsect, *stabstrsect;
1661
1662   stabsect = bfd_get_section_by_name (abfd, stabsect_name);
1663   if (0 == stabsect)
1664     {
1665       printf ("No %s section present\n\n", stabsect_name);
1666       return false;
1667     }
1668
1669   stabstrsect = bfd_get_section_by_name (abfd, strsect_name);
1670   if (0 == stabstrsect)
1671     {
1672       fprintf (stderr, "%s: %s has no %s section\n", program_name,
1673                bfd_get_filename (abfd), strsect_name);
1674       return false;
1675     }
1676  
1677   stab_size    = bfd_section_size (abfd, stabsect);
1678   stabstr_size = bfd_section_size (abfd, stabstrsect);
1679
1680   stabs  = (bfd_byte *) xmalloc (stab_size);
1681   strtab = (char *) xmalloc (stabstr_size);
1682   
1683   if (! bfd_get_section_contents (abfd, stabsect, (PTR) stabs, 0, stab_size))
1684     {
1685       fprintf (stderr, "%s: Reading %s section of %s failed: %s\n",
1686                program_name, stabsect_name, bfd_get_filename (abfd),
1687                bfd_errmsg (bfd_get_error ()));
1688       free (stabs);
1689       free (strtab);
1690       return false;
1691     }
1692
1693   if (! bfd_get_section_contents (abfd, stabstrsect, (PTR) strtab, 0,
1694                                   stabstr_size))
1695     {
1696       fprintf (stderr, "%s: Reading %s section of %s failed: %s\n",
1697                program_name, strsect_name, bfd_get_filename (abfd),
1698                bfd_errmsg (bfd_get_error ()));
1699       free (stabs);
1700       free (strtab);
1701       return false;
1702     }
1703
1704   return true;
1705 }
1706
1707 /* Stabs entries use a 12 byte format:
1708      4 byte string table index
1709      1 byte stab type
1710      1 byte stab other field
1711      2 byte stab desc field
1712      4 byte stab value
1713    FIXME: This will have to change for a 64 bit object format.  */
1714
1715 #define STRDXOFF (0)
1716 #define TYPEOFF (4)
1717 #define OTHEROFF (5)
1718 #define DESCOFF (6)
1719 #define VALOFF (8)
1720 #define STABSIZE (12)
1721
1722 /* Print ABFD's stabs section STABSECT_NAME (in `stabs'),
1723    using string table section STRSECT_NAME (in `strtab').  */
1724
1725 static void
1726 print_section_stabs (abfd, stabsect_name, strsect_name)
1727      bfd *abfd;
1728      char *stabsect_name;
1729      char *strsect_name;
1730 {
1731   int i;
1732   unsigned file_string_table_offset = 0, next_file_string_table_offset = 0;
1733   bfd_byte *stabp, *stabs_end;
1734
1735   stabp = stabs;
1736   stabs_end = stabp + stab_size;
1737
1738   printf ("Contents of %s section:\n\n", stabsect_name);
1739   printf ("Symnum n_type n_othr n_desc n_value  n_strx String\n");
1740
1741   /* Loop through all symbols and print them.
1742
1743      We start the index at -1 because there is a dummy symbol on
1744      the front of stabs-in-{coff,elf} sections that supplies sizes.  */
1745
1746   for (i = -1; stabp < stabs_end; stabp += STABSIZE, i++)
1747     {
1748       const char *name;
1749       unsigned long strx;
1750       unsigned char type, other;
1751       unsigned short desc;
1752       bfd_vma value;
1753
1754       strx = bfd_h_get_32 (abfd, stabp + STRDXOFF);
1755       type = bfd_h_get_8 (abfd, stabp + TYPEOFF);
1756       other = bfd_h_get_8 (abfd, stabp + OTHEROFF);
1757       desc = bfd_h_get_16 (abfd, stabp + DESCOFF);
1758       value = bfd_h_get_32 (abfd, stabp + VALOFF);
1759
1760       printf ("\n%-6d ", i);
1761       /* Either print the stab name, or, if unnamed, print its number
1762          again (makes consistent formatting for tools like awk). */
1763       name = bfd_get_stab_name (type);
1764       if (name != NULL)
1765         printf ("%-6s", name);
1766       else if (type == N_UNDF)
1767         printf ("HdrSym");
1768       else
1769         printf ("%-6d", type);
1770       printf (" %-6d %-6d ", other, desc);
1771       printf_vma (value);
1772       printf (" %-6lu", strx);
1773
1774       /* Symbols with type == 0 (N_UNDF) specify the length of the
1775          string table associated with this file.  We use that info
1776          to know how to relocate the *next* file's string table indices.  */
1777
1778       if (type == N_UNDF)
1779         {
1780           file_string_table_offset = next_file_string_table_offset;
1781           next_file_string_table_offset += value;
1782         }
1783       else
1784         {
1785           /* Using the (possibly updated) string table offset, print the
1786              string (if any) associated with this symbol.  */
1787
1788           if ((strx + file_string_table_offset) < stabstr_size)
1789             printf (" %s", &strtab[strx + file_string_table_offset]);
1790           else
1791             printf (" *");
1792         }
1793     }
1794   printf ("\n\n");
1795 }
1796
1797 static void
1798 dump_section_stabs (abfd, stabsect_name, strsect_name)
1799      bfd *abfd;
1800      char *stabsect_name;
1801      char *strsect_name;
1802 {
1803   asection *s;
1804
1805   /* Check for section names for which stabsect_name is a prefix, to
1806      handle .stab0, etc.  */
1807   for (s = abfd->sections;
1808        s != NULL;
1809        s = s->next)
1810     {
1811       int len;
1812
1813       len = strlen (stabsect_name);
1814
1815 /* If the prefix matches, and the files section name ends with a nul or a digit,
1816    then we match.  Ie: we want either an exact match or a a section followed by 
1817    a number.  */
1818       if (strncmp (stabsect_name, s->name, len) == 0
1819           && (s->name[len] == '\000' || isdigit (s->name[len])))
1820         {
1821           if (read_section_stabs (abfd, s->name, strsect_name))
1822             {
1823               print_section_stabs (abfd, s->name, strsect_name);
1824               free (stabs);
1825               free (strtab);
1826             }
1827         }
1828     }
1829 }
1830 \f
1831 static void
1832 dump_bfd_header (abfd)
1833      bfd *abfd;
1834 {
1835   char *comma = "";
1836
1837   printf ("architecture: %s, ",
1838           bfd_printable_arch_mach (bfd_get_arch (abfd),
1839                                    bfd_get_mach (abfd)));
1840   printf ("flags 0x%08x:\n", abfd->flags);
1841
1842 #define PF(x, y)    if (abfd->flags & x) {printf("%s%s", comma, y); comma=", ";}
1843   PF (HAS_RELOC, "HAS_RELOC");
1844   PF (EXEC_P, "EXEC_P");
1845   PF (HAS_LINENO, "HAS_LINENO");
1846   PF (HAS_DEBUG, "HAS_DEBUG");
1847   PF (HAS_SYMS, "HAS_SYMS");
1848   PF (HAS_LOCALS, "HAS_LOCALS");
1849   PF (DYNAMIC, "DYNAMIC");
1850   PF (WP_TEXT, "WP_TEXT");
1851   PF (D_PAGED, "D_PAGED");
1852   PF (BFD_IS_RELAXABLE, "BFD_IS_RELAXABLE");
1853   printf ("\nstart address 0x");
1854   printf_vma (abfd->start_address);
1855   printf ("\n");
1856 }
1857 \f
1858 static void
1859 dump_bfd_private_header (abfd)
1860 bfd *abfd;
1861 {
1862   bfd_print_private_bfd_data (abfd, stdout);
1863 }
1864
1865 static void
1866 display_bfd (abfd)
1867      bfd *abfd;
1868 {
1869   char **matching;
1870
1871   if (!bfd_check_format_matches (abfd, bfd_object, &matching))
1872     {
1873       bfd_nonfatal (bfd_get_filename (abfd));
1874       if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1875         {
1876           list_matching_formats (matching);
1877           free (matching);
1878         }
1879       return;
1880     }
1881
1882   /* If we are adjusting section VMA's, change them all now.  Changing
1883      the BFD information is a hack.  However, we must do it, or
1884      bfd_find_nearest_line will not do the right thing.  */
1885   if (adjust_section_vma != 0)
1886     {
1887       asection *s;
1888
1889       for (s = abfd->sections; s != NULL; s = s->next)
1890         {
1891           s->vma += adjust_section_vma;
1892           s->lma += adjust_section_vma;
1893         }
1894     }
1895
1896   printf ("\n%s:     file format %s\n", bfd_get_filename (abfd),
1897           abfd->xvec->name);
1898   if (dump_ar_hdrs)
1899     print_arelt_descr (stdout, abfd, true);
1900   if (dump_file_header)
1901     dump_bfd_header (abfd);
1902   if (dump_private_headers)
1903     dump_bfd_private_header (abfd);
1904   putchar ('\n');
1905   if (dump_section_headers)
1906     dump_headers (abfd);
1907   if (dump_symtab || dump_reloc_info || disassemble || dump_debugging)
1908     {
1909       syms = slurp_symtab (abfd);
1910     }
1911   if (dump_dynamic_symtab || dump_dynamic_reloc_info)
1912     {
1913       dynsyms = slurp_dynamic_symtab (abfd);
1914     }
1915   if (dump_symtab)
1916     dump_symbols (abfd, false);
1917   if (dump_dynamic_symtab)
1918     dump_symbols (abfd, true);
1919   if (dump_stab_section_info)
1920     dump_stabs (abfd);
1921   if (dump_reloc_info && ! disassemble)
1922     dump_relocs (abfd);
1923   if (dump_dynamic_reloc_info)
1924     dump_dynamic_relocs (abfd);
1925   if (dump_section_contents)
1926     dump_data (abfd);
1927   if (disassemble)
1928     disassemble_data (abfd);
1929   if (dump_debugging)
1930     {
1931       PTR dhandle;
1932
1933       dhandle = read_debugging_info (abfd, syms, symcount);
1934       if (dhandle != NULL)
1935         {
1936           if (! print_debugging_info (stdout, dhandle))
1937             fprintf (stderr, "%s: printing debugging information failed\n",
1938                      bfd_get_filename (abfd));
1939         }
1940     }
1941   if (syms)
1942     {
1943       free (syms);
1944       syms = NULL;
1945     }
1946   if (dynsyms)
1947     {
1948       free (dynsyms);
1949       dynsyms = NULL;
1950     }
1951 }
1952
1953 static void
1954 display_file (filename, target)
1955      char *filename;
1956      char *target;
1957 {
1958   bfd *file, *arfile = (bfd *) NULL;
1959
1960   file = bfd_openr (filename, target);
1961   if (file == NULL)
1962     {
1963       bfd_nonfatal (filename);
1964       return;
1965     }
1966
1967   if (bfd_check_format (file, bfd_archive) == true)
1968     {
1969       bfd *last_arfile = NULL;
1970
1971       printf ("In archive %s:\n", bfd_get_filename (file));
1972       for (;;)
1973         {
1974           bfd_set_error (bfd_error_no_error);
1975
1976           arfile = bfd_openr_next_archived_file (file, arfile);
1977           if (arfile == NULL)
1978             {
1979               if (bfd_get_error () != bfd_error_no_more_archived_files)
1980                 {
1981                   bfd_nonfatal (bfd_get_filename (file));
1982                 }
1983               break;
1984             }
1985
1986           display_bfd (arfile);
1987
1988           if (last_arfile != NULL)
1989             bfd_close (last_arfile);
1990           last_arfile = arfile;
1991         }
1992
1993       if (last_arfile != NULL)
1994         bfd_close (last_arfile);
1995     }
1996   else
1997     display_bfd (file);
1998
1999   bfd_close (file);
2000 }
2001 \f
2002 /* Actually display the various requested regions */
2003
2004 static void
2005 dump_data (abfd)
2006      bfd *abfd;
2007 {
2008   asection *section;
2009   bfd_byte *data = 0;
2010   bfd_size_type datasize = 0;
2011   bfd_size_type i;
2012   bfd_size_type start, stop;
2013
2014   for (section = abfd->sections; section != NULL; section =
2015        section->next)
2016     {
2017       int onaline = 16;
2018
2019       if (only == (char *) NULL ||
2020           strcmp (only, section->name) == 0)
2021         {
2022           if (section->flags & SEC_HAS_CONTENTS)
2023             {
2024               printf ("Contents of section %s:\n", section->name);
2025
2026               if (bfd_section_size (abfd, section) == 0)
2027                 continue;
2028               data = (bfd_byte *) xmalloc ((size_t) bfd_section_size (abfd, section));
2029               datasize = bfd_section_size (abfd, section);
2030
2031
2032               bfd_get_section_contents (abfd, section, (PTR) data, 0, bfd_section_size (abfd, section));
2033
2034               if (start_address == (bfd_vma) -1
2035                   || start_address < section->vma)
2036                 start = 0;
2037               else
2038                 start = start_address - section->vma;
2039               if (stop_address == (bfd_vma) -1)
2040                 stop = bfd_section_size (abfd, section);
2041               else
2042                 {
2043                   if (stop_address < section->vma)
2044                     stop = 0;
2045                   else
2046                     stop = stop_address - section->vma;
2047                   if (stop > bfd_section_size (abfd, section))
2048                     stop = bfd_section_size (abfd, section);
2049                 }
2050               for (i = start; i < stop; i += onaline)
2051                 {
2052                   bfd_size_type j;
2053
2054                   printf (" %04lx ", (unsigned long int) (i + section->vma));
2055                   for (j = i; j < i + onaline; j++)
2056                     {
2057                       if (j < stop)
2058                         printf ("%02x", (unsigned) (data[j]));
2059                       else
2060                         printf ("  ");
2061                       if ((j & 3) == 3)
2062                         printf (" ");
2063                     }
2064
2065                   printf (" ");
2066                   for (j = i; j < i + onaline; j++)
2067                     {
2068                       if (j >= stop)
2069                         printf (" ");
2070                       else
2071                         printf ("%c", isprint (data[j]) ? data[j] : '.');
2072                     }
2073                   putchar ('\n');
2074                 }
2075               free (data);
2076             }
2077         }
2078     }
2079 }
2080
2081 /* Should perhaps share code and display with nm? */
2082 static void
2083 dump_symbols (abfd, dynamic)
2084      bfd *abfd;
2085      boolean dynamic;
2086 {
2087   asymbol **current;
2088   long max;
2089   long count;
2090
2091   if (dynamic)
2092     {
2093       current = dynsyms;
2094       max = dynsymcount;
2095       if (max == 0)
2096         return;
2097       printf ("DYNAMIC SYMBOL TABLE:\n");
2098     }
2099   else
2100     {
2101       current = syms;
2102       max = symcount;
2103       if (max == 0)
2104         return;
2105       printf ("SYMBOL TABLE:\n");
2106     }
2107
2108   for (count = 0; count < max; count++)
2109     {
2110       if (*current)
2111         {
2112           bfd *cur_bfd = bfd_asymbol_bfd (*current);
2113
2114           if (cur_bfd != NULL)
2115             {
2116               const char *name;
2117               char *alloc;
2118
2119               name = bfd_asymbol_name (*current);
2120               alloc = NULL;
2121               if (do_demangle && name != NULL && *name != '\0')
2122                 {
2123                   const char *n;
2124
2125                   /* If we want to demangle the name, we demangle it
2126                      here, and temporarily clobber it while calling
2127                      bfd_print_symbol.  FIXME: This is a gross hack.  */
2128
2129                   n = name;
2130                   if (bfd_get_symbol_leading_char (cur_bfd) == *n)
2131                     ++n;
2132                   alloc = cplus_demangle (n, DMGL_ANSI | DMGL_PARAMS);
2133                   if (alloc != NULL)
2134                     (*current)->name = alloc;
2135                   else
2136                     (*current)->name = n;
2137                 }
2138
2139               bfd_print_symbol (cur_bfd, stdout, *current,
2140                                 bfd_print_symbol_all);
2141
2142               (*current)->name = name;
2143               if (alloc != NULL)
2144                 free (alloc);
2145
2146               printf ("\n");
2147             }
2148         }
2149       current++;
2150     }
2151   printf ("\n");
2152   printf ("\n");
2153 }
2154
2155 static void
2156 dump_relocs (abfd)
2157      bfd *abfd;
2158 {
2159   arelent **relpp;
2160   long relcount;
2161   asection *a;
2162
2163   for (a = abfd->sections; a != (asection *) NULL; a = a->next)
2164     {
2165       long relsize;
2166
2167       if (bfd_is_abs_section (a))
2168         continue;
2169       if (bfd_is_und_section (a))
2170         continue;
2171       if (bfd_is_com_section (a))
2172         continue;
2173
2174       if (only)
2175         {
2176           if (strcmp (only, a->name))
2177             continue;
2178         }
2179       else if ((a->flags & SEC_RELOC) == 0)
2180         continue;
2181
2182       relsize = bfd_get_reloc_upper_bound (abfd, a);
2183       if (relsize < 0)
2184         bfd_fatal (bfd_get_filename (abfd));
2185
2186       printf ("RELOCATION RECORDS FOR [%s]:", a->name);
2187
2188       if (relsize == 0)
2189         {
2190           printf (" (none)\n\n");
2191         }
2192       else
2193         {
2194           relpp = (arelent **) xmalloc (relsize);
2195           relcount = bfd_canonicalize_reloc (abfd, a, relpp, syms);
2196           if (relcount < 0)
2197             bfd_fatal (bfd_get_filename (abfd));
2198           else if (relcount == 0)
2199             {
2200               printf (" (none)\n\n");
2201             }
2202           else
2203             {
2204               printf ("\n");
2205               dump_reloc_set (abfd, a, relpp, relcount);
2206               printf ("\n\n");
2207             }
2208           free (relpp);
2209         }
2210     }
2211 }
2212
2213 static void
2214 dump_dynamic_relocs (abfd)
2215      bfd *abfd;
2216 {
2217   long relsize;
2218   arelent **relpp;
2219   long relcount;
2220
2221   relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
2222   if (relsize < 0)
2223     bfd_fatal (bfd_get_filename (abfd));
2224
2225   printf ("DYNAMIC RELOCATION RECORDS");
2226
2227   if (relsize == 0)
2228     {
2229       printf (" (none)\n\n");
2230     }
2231   else
2232     {
2233       relpp = (arelent **) xmalloc (relsize);
2234       relcount = bfd_canonicalize_dynamic_reloc (abfd, relpp, dynsyms);
2235       if (relcount < 0)
2236         bfd_fatal (bfd_get_filename (abfd));
2237       else if (relcount == 0)
2238         {
2239           printf (" (none)\n\n");
2240         }
2241       else
2242         {
2243           printf ("\n");
2244           dump_reloc_set (abfd, (asection *) NULL, relpp, relcount);
2245           printf ("\n\n");
2246         }
2247       free (relpp);
2248     }
2249 }
2250
2251 static void
2252 dump_reloc_set (abfd, sec, relpp, relcount)
2253      bfd *abfd;
2254      asection *sec;
2255      arelent **relpp;
2256      long relcount;
2257 {
2258   arelent **p;
2259   char *last_filename, *last_functionname;
2260   unsigned int last_line;
2261
2262   /* Get column headers lined up reasonably.  */
2263   {
2264     static int width;
2265     if (width == 0)
2266       {
2267         char buf[30];
2268         sprintf_vma (buf, (bfd_vma) -1);
2269         width = strlen (buf) - 7;
2270       }
2271     printf ("OFFSET %*s TYPE %*s VALUE \n", width, "", 12, "");
2272   }
2273
2274   last_filename = NULL;
2275   last_functionname = NULL;
2276   last_line = 0;
2277
2278   for (p = relpp; relcount && *p != (arelent *) NULL; p++, relcount--)
2279     {
2280       arelent *q = *p;
2281       const char *filename, *functionname;
2282       unsigned int line;
2283       const char *sym_name;
2284       const char *section_name;
2285
2286       if (start_address != (bfd_vma) -1
2287           && q->address < start_address)
2288         continue;
2289       if (stop_address != (bfd_vma) -1
2290           && q->address > stop_address)
2291         continue;
2292
2293       if (with_line_numbers
2294           && sec != NULL
2295           && bfd_find_nearest_line (abfd, sec, syms, q->address,
2296                                     &filename, &functionname, &line))
2297         {
2298           if (functionname != NULL
2299               && (last_functionname == NULL
2300                   || strcmp (functionname, last_functionname) != 0))
2301             {
2302               printf ("%s():\n", functionname);
2303               if (last_functionname != NULL)
2304                 free (last_functionname);
2305               last_functionname = xstrdup (functionname);
2306             }
2307           if (line > 0
2308               && (line != last_line
2309                   || (filename != NULL
2310                       && last_filename != NULL
2311                       && strcmp (filename, last_filename) != 0)))
2312             {
2313               printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
2314               last_line = line;
2315               if (last_filename != NULL)
2316                 free (last_filename);
2317               if (filename == NULL)
2318                 last_filename = NULL;
2319               else
2320                 last_filename = xstrdup (filename);
2321             }
2322         }
2323
2324       if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
2325         {
2326           sym_name = (*(q->sym_ptr_ptr))->name;
2327           section_name = (*(q->sym_ptr_ptr))->section->name;
2328         }
2329       else
2330         {
2331           sym_name = NULL;
2332           section_name = NULL;
2333         }
2334       if (sym_name)
2335         {
2336           printf_vma (q->address);
2337           printf (" %-16s  ", q->howto->name);
2338           objdump_print_symname (abfd, (struct disassemble_info *) NULL,
2339                                  *q->sym_ptr_ptr);
2340         }
2341       else
2342         {
2343           if (section_name == (CONST char *) NULL)
2344             section_name = "*unknown*";
2345           printf_vma (q->address);
2346           printf (" %-16s  [%s]",
2347                   q->howto->name,
2348                   section_name);
2349         }
2350       if (q->addend)
2351         {
2352           printf ("+0x");
2353           printf_vma (q->addend);
2354         }
2355       printf ("\n");
2356     }
2357 }
2358 \f
2359 /* The length of the longest architecture name + 1.  */
2360 #define LONGEST_ARCH sizeof("rs6000:6000")
2361
2362 static const char *
2363 endian_string (endian)
2364      enum bfd_endian endian;
2365 {
2366   if (endian == BFD_ENDIAN_BIG)
2367     return "big endian";
2368   else if (endian == BFD_ENDIAN_LITTLE)
2369     return "little endian";
2370   else
2371     return "endianness unknown";
2372 }
2373
2374 /* List the targets that BFD is configured to support, each followed
2375    by its endianness and the architectures it supports.  */
2376
2377 static void
2378 display_target_list ()
2379 {
2380   extern bfd_target *bfd_target_vector[];
2381   char *dummy_name;
2382   int t;
2383
2384   dummy_name = choose_temp_base ();
2385   for (t = 0; bfd_target_vector[t]; t++)
2386     {
2387       bfd_target *p = bfd_target_vector[t];
2388       bfd *abfd = bfd_openw (dummy_name, p->name);
2389       int a;
2390
2391       printf ("%s\n (header %s, data %s)\n", p->name,
2392               endian_string (p->header_byteorder),
2393               endian_string (p->byteorder));
2394
2395       if (abfd == NULL)
2396         {
2397           bfd_nonfatal (dummy_name);
2398           continue;
2399         }
2400
2401       if (! bfd_set_format (abfd, bfd_object))
2402         {
2403           if (bfd_get_error () != bfd_error_invalid_operation)
2404             bfd_nonfatal (p->name);
2405           continue;
2406         }
2407
2408       for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
2409         if (bfd_set_arch_mach (abfd, (enum bfd_architecture) a, 0))
2410           printf ("  %s\n",
2411                   bfd_printable_arch_mach ((enum bfd_architecture) a, 0));
2412     }
2413   unlink (dummy_name);
2414   free (dummy_name);
2415 }
2416
2417 /* Print a table showing which architectures are supported for entries
2418    FIRST through LAST-1 of bfd_target_vector (targets across,
2419    architectures down).  */
2420
2421 static void
2422 display_info_table (first, last)
2423      int first;
2424      int last;
2425 {
2426   extern bfd_target *bfd_target_vector[];
2427   int t, a;
2428   char *dummy_name;
2429
2430   /* Print heading of target names.  */
2431   printf ("\n%*s", (int) LONGEST_ARCH, " ");
2432   for (t = first; t < last && bfd_target_vector[t]; t++)
2433     printf ("%s ", bfd_target_vector[t]->name);
2434   putchar ('\n');
2435
2436   dummy_name = choose_temp_base ();
2437   for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
2438     if (strcmp (bfd_printable_arch_mach (a, 0), "UNKNOWN!") != 0)
2439       {
2440         printf ("%*s ", (int) LONGEST_ARCH - 1,
2441                 bfd_printable_arch_mach (a, 0));
2442         for (t = first; t < last && bfd_target_vector[t]; t++)
2443           {
2444             bfd_target *p = bfd_target_vector[t];
2445             boolean ok = true;
2446             bfd *abfd = bfd_openw (dummy_name, p->name);
2447
2448             if (abfd == NULL)
2449               {
2450                 bfd_nonfatal (p->name);
2451                 ok = false;
2452               }
2453
2454             if (ok)
2455               {
2456                 if (! bfd_set_format (abfd, bfd_object))
2457                   {
2458                     if (bfd_get_error () != bfd_error_invalid_operation)
2459                       bfd_nonfatal (p->name);
2460                     ok = false;
2461                   }
2462               }
2463
2464             if (ok)
2465               {
2466                 if (! bfd_set_arch_mach (abfd, a, 0))
2467                   ok = false;
2468               }
2469
2470             if (ok)
2471               printf ("%s ", p->name);
2472             else
2473               {
2474                 int l = strlen (p->name);
2475                 while (l--)
2476                   putchar ('-');
2477                 putchar (' ');
2478               }
2479           }
2480         putchar ('\n');
2481       }
2482   unlink (dummy_name);
2483   free (dummy_name);
2484 }
2485
2486 /* Print tables of all the target-architecture combinations that
2487    BFD has been configured to support.  */
2488
2489 static void
2490 display_target_tables ()
2491 {
2492   int t, columns;
2493   extern bfd_target *bfd_target_vector[];
2494   char *colum;
2495   extern char *getenv ();
2496
2497   columns = 0;
2498   colum = getenv ("COLUMNS");
2499   if (colum != NULL)
2500     columns = atoi (colum);
2501   if (columns == 0)
2502     columns = 80;
2503
2504   t = 0;
2505   while (bfd_target_vector[t] != NULL)
2506     {
2507       int oldt = t, wid;
2508
2509       wid = LONGEST_ARCH + strlen (bfd_target_vector[t]->name) + 1;
2510       ++t;
2511       while (wid < columns && bfd_target_vector[t] != NULL)
2512         {
2513           int newwid;
2514
2515           newwid = wid + strlen (bfd_target_vector[t]->name) + 1;
2516           if (newwid >= columns)
2517             break;
2518           wid = newwid;
2519           ++t;
2520         }
2521       display_info_table (oldt, t);
2522     }
2523 }
2524
2525 static void
2526 display_info ()
2527 {
2528   printf ("BFD header file version %s\n", BFD_VERSION);
2529   display_target_list ();
2530   display_target_tables ();
2531 }
2532
2533 int
2534 main (argc, argv)
2535      int argc;
2536      char **argv;
2537 {
2538   int c;
2539   char *target = default_target;
2540   boolean seenflag = false;
2541
2542   program_name = *argv;
2543   xmalloc_set_program_name (program_name);
2544
2545   START_PROGRESS (program_name, 0);
2546
2547   bfd_init ();
2548   set_default_bfd_target ();
2549
2550   while ((c = getopt_long (argc, argv, "pib:m:VCdDlfahrRtTxsSj:wE:",
2551                            long_options, (int *) 0))
2552          != EOF)
2553     {
2554       if (c != 'l' && c != OPTION_START_ADDRESS && c != OPTION_STOP_ADDRESS)
2555         seenflag = true;
2556       switch (c)
2557         {
2558         case 0:
2559           break;                /* we've been given a long option */
2560         case 'm':
2561           machine = optarg;
2562           break;
2563         case 'j':
2564           only = optarg;
2565           break;
2566         case 'l':
2567           with_line_numbers = 1;
2568           break;
2569         case 'b':
2570           target = optarg;
2571           break;
2572         case 'f':
2573           dump_file_header = true;
2574           break;
2575         case 'i':
2576           formats_info = true;
2577           break;
2578         case 'p':
2579           dump_private_headers = 1;
2580           break;
2581         case 'x':
2582           dump_private_headers = 1;
2583           dump_symtab = 1;
2584           dump_reloc_info = 1;
2585           dump_file_header = true;
2586           dump_ar_hdrs = 1;
2587           dump_section_headers = 1;
2588           break;
2589         case 't':
2590           dump_symtab = 1;
2591           break;
2592         case 'T':
2593           dump_dynamic_symtab = 1;
2594           break;
2595         case 'C':
2596           do_demangle = 1;
2597           break;
2598         case 'd':
2599           disassemble = true;
2600           break;
2601         case 'D':
2602           disassemble = disassemble_all = true;
2603           break;
2604         case 'S':
2605           disassemble = true;
2606           with_source_code = true;
2607           break;
2608         case 's':
2609           dump_section_contents = 1;
2610           break;
2611         case 'r':
2612           dump_reloc_info = 1;
2613           break;
2614         case 'R':
2615           dump_dynamic_reloc_info = 1;
2616           break;
2617         case 'a':
2618           dump_ar_hdrs = 1;
2619           break;
2620         case 'h':
2621           dump_section_headers = 1;
2622           break;
2623         case 'H':
2624           usage (stdout, 0);
2625         case 'V':
2626           show_version = 1;
2627           break;
2628         case 'w':
2629           wide_output = 1;
2630           break;
2631         case OPTION_ADJUST_VMA:
2632           adjust_section_vma = parse_vma (optarg, "--adjust-vma");
2633           break;
2634         case OPTION_START_ADDRESS:
2635           start_address = parse_vma (optarg, "--start-address");
2636           break;
2637         case OPTION_STOP_ADDRESS:
2638           stop_address = parse_vma (optarg, "--stop-address");
2639           break;
2640         case 'E':
2641           if (strcmp (optarg, "B") == 0)
2642             endian = BFD_ENDIAN_BIG;
2643           else if (strcmp (optarg, "L") == 0)
2644             endian = BFD_ENDIAN_LITTLE;
2645           else
2646             {
2647               fprintf (stderr, "%s: unrecognized -E option\n", program_name);
2648               usage (stderr, 1);
2649             }
2650           break;
2651         case OPTION_ENDIAN:
2652           if (strncmp (optarg, "big", strlen (optarg)) == 0)
2653             endian = BFD_ENDIAN_BIG;
2654           else if (strncmp (optarg, "little", strlen (optarg)) == 0)
2655             endian = BFD_ENDIAN_LITTLE;
2656           else
2657             {
2658               fprintf (stderr, "%s: unrecognized --endian type `%s'\n",
2659                       program_name, optarg);
2660               usage (stderr, 1);
2661             }
2662           break;
2663         default:
2664           usage (stderr, 1);
2665         }
2666     }
2667
2668   if (show_version)
2669     print_version ("objdump");
2670
2671   if (seenflag == false)
2672     usage (stderr, 1);
2673
2674   if (formats_info)
2675     {
2676       display_info ();
2677     }
2678   else
2679     {
2680       if (optind == argc)
2681         display_file ("a.out", target);
2682       else
2683         for (; optind < argc;)
2684           display_file (argv[optind++], target);
2685     }
2686
2687   END_PROGRESS (program_name);
2688
2689   return 0;
2690 }
This page took 0.180984 seconds and 4 git commands to generate.