* objdump.c (disassemble_data): Handle unknown endianness.
[deliverable/binutils-gdb.git] / binutils / objdump.c
index e531f20637fbad897d88ed4ae96006c8402bcf25..19b1243985c29f559a39deb09559497872970a2b 100644 (file)
@@ -1,5 +1,5 @@
 /* objdump.c -- dump information about an object file.
-   Copyright 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
+   Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
 
 This file is part of GNU Binutils.
 
@@ -15,23 +15,25 @@ GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
-Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
+Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
 #include "bfd.h"
-#include "sysdep.h"
 #include "getopt.h"
+#include "progress.h"
 #include "bucomm.h"
-#include <stdio.h>
 #include <ctype.h>
 #include "dis-asm.h"
 #include "libiberty.h"
+#include "debug.h"
+#include "budbg.h"
 
 /* Internal headers for the ELF .stab-dump code - sorry.  */
 #define        BYTES_IN_WORD   32
 #include "aout/aout64.h"
 
-#ifndef FPRINTF_ALREADY_DECLARED
-extern int fprintf PARAMS ((FILE *, CONST char *, ...));
+#ifdef NEED_DECLARATION_FPRINTF
+/* This is needed by INIT_DISASSEMBLE_INFO.  */
+extern int fprintf ();
 #endif
 
 char *default_target = NULL;   /* default at runtime */
@@ -47,17 +49,25 @@ int dump_dynamic_symtab;    /* -T */
 int dump_reloc_info;           /* -r */
 int dump_dynamic_reloc_info;   /* -R */
 int dump_ar_hdrs;              /* -a */
+int dump_private_headers;      /* -p */
 int with_line_numbers;         /* -l */
+boolean with_source_code;      /* -S */
+int show_raw_insn;             /* --show-raw-insn */
 int dump_stab_section_info;    /* --stabs */
 boolean disassemble;           /* -d */
 boolean disassemble_all;       /* -D */
 boolean formats_info;          /* -i */
 char *only;                    /* -j secname */
+int wide_output;               /* -w */
+bfd_vma start_address = (bfd_vma) -1; /* --start-address */
+bfd_vma stop_address = (bfd_vma) -1;  /* --stop-address */
+int dump_debugging;            /* --debugging */
 
 /* Extra info to pass to the disassembler address printing function.  */
 struct objdump_disasm_info {
   bfd *abfd;
   asection *sec;
+  boolean require_sec;
 };
 
 /* Architecture to disassemble for, or default if NULL.  */
@@ -69,6 +79,12 @@ asymbol **syms;
 /* Number of symbols in `syms'.  */
 long symcount = 0;
 
+/* The sorted symbol table.  */
+asymbol **sorted_syms;
+
+/* Number of symbols in `sorted_syms'.  */
+long sorted_symcount = 0;
+
 /* The dynamic symbol table.  */
 asymbol **dynsyms;
 
@@ -98,8 +114,17 @@ dump_symbols PARAMS ((bfd *abfd, boolean dynamic));
 static void
 display_bfd PARAMS ((bfd *abfd));
 
+static void
+objdump_print_value PARAMS ((bfd_vma, FILE *));
+
 static void
 objdump_print_address PARAMS ((bfd_vma, struct disassemble_info *));
+
+static void
+show_line PARAMS ((bfd *, asection *, bfd_vma));
+
+static const char *
+endian_string PARAMS ((enum bfd_endian));
 \f
 void
 usage (stream, status)
@@ -107,23 +132,34 @@ usage (stream, status)
      int status;
 {
   fprintf (stream, "\
-Usage: %s [-ahifdDrRtTxsl] [-b bfdname] [-m machine] [-j section-name]\n\
-       [--archive-headers] [--target=bfdname] [--disassemble]\n\
+Usage: %s [-ahifdDprRtTxsSlw] [-b bfdname] [-m machine] [-j section-name]\n\
+       [--archive-headers] [--target=bfdname] [--debugging] [--disassemble]\n\
        [--disassemble-all] [--file-headers] [--section-headers] [--headers]\n\
-       [--info] [--section=section-name] [--line-numbers]\n\
+       [--info] [--section=section-name] [--line-numbers] [--source]\n",
+          program_name);
+  fprintf (stream, "\
        [--architecture=machine] [--reloc] [--full-contents] [--stabs]\n\
        [--syms] [--all-headers] [--dynamic-syms] [--dynamic-reloc]\n\
-       [--version] [--help] objfile...\n\
-at least one option besides -l (--line-numbers) must be given\n",
-          program_name);
+       [--wide] [--version] [--help] [--private-headers]\n\
+       [--start-address=addr] [--stop-address=addr]\n\
+       [--show-raw-insn] objfile...\n\
+at least one option besides -l (--line-numbers) must be given\n");
+  list_supported_targets (program_name, stream);
   exit (status);
 }
 
+/* 150 isn't special; it's just an arbitrary non-ASCII char value.  */
+
+#define OPTION_START_ADDRESS (150)
+#define OPTION_STOP_ADDRESS (OPTION_START_ADDRESS + 1)
+
 static struct option long_options[]=
 {
   {"all-headers", no_argument, NULL, 'x'},
+  {"private-headers", no_argument, NULL, 'p'},
   {"architecture", required_argument, NULL, 'm'},
   {"archive-headers", no_argument, NULL, 'a'},
+  {"debugging", no_argument, &dump_debugging, 1},
   {"disassemble", no_argument, NULL, 'd'},
   {"disassemble-all", no_argument, NULL, 'D'},
   {"dynamic-reloc", no_argument, NULL, 'R'},
@@ -137,10 +173,15 @@ static struct option long_options[]=
   {"reloc", no_argument, NULL, 'r'},
   {"section", required_argument, NULL, 'j'},
   {"section-headers", no_argument, NULL, 'h'},
+  {"show-raw-insn", no_argument, &show_raw_insn, 1},
+  {"source", no_argument, NULL, 'S'},
   {"stabs", no_argument, &dump_stab_section_info, 1},
+  {"start-address", required_argument, NULL, OPTION_START_ADDRESS},
+  {"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
   {"syms", no_argument, NULL, 't'},
   {"target", required_argument, NULL, 'b'},
-  {"version", no_argument, &show_version,    1},
+  {"version", no_argument, &show_version, 1},
+  {"wide", no_argument, &wide_output, 'w'},
   {0, no_argument, 0, 0}
 };
 \f
@@ -162,8 +203,10 @@ dump_section_header (abfd, section, ignored)
          (unsigned) bfd_get_section_size_before_reloc (section));
   printf (" vma ");
   printf_vma (section->vma);
-  printf (" align 2**%u\n ",
-         section->alignment_power);
+  printf (" lma ");
+  printf_vma (section->lma);
+  printf (" align 2**%u%s ",
+         section->alignment_power, (wide_output) ? "" : "\n");
   PF (SEC_ALLOC, "ALLOC");
   PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
   PF (SEC_CONSTRUCTOR_TEXT, "CONSTRUCTOR TEXT");
@@ -180,6 +223,8 @@ dump_section_header (abfd, section, ignored)
   PF (SEC_ROM, "ROM");
   PF (SEC_DEBUGGING, "DEBUGGING");
   PF (SEC_NEVER_LOAD, "NEVER_LOAD");
+  PF (SEC_EXCLUDE, "EXCLUDE");
+  PF (SEC_SORT_ENTRIES, "SORT ENTRIES");
   printf ("\n");
 #undef PF
 }
@@ -201,6 +246,7 @@ slurp_symtab (abfd)
   if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
     {
       printf ("No symbols in \"%s\".\n", bfd_get_filename (abfd));
+      symcount = 0;
       return NULL;
     }
 
@@ -237,6 +283,7 @@ slurp_dynamic_symtab (abfd)
        {
          fprintf (stderr, "%s: %s: not a dynamic object\n",
                   program_name, bfd_get_filename (abfd));
+         dynsymcount = 0;
          return NULL;
        }
 
@@ -284,7 +331,7 @@ remove_useless_symbols (symbols, count)
   return out_ptr - symbols;
 }
 
-/* Sort symbols into value order. */
+/* Sort symbols into value order.  */
 
 static int 
 compare_symbols (ap, bp)
@@ -293,16 +340,79 @@ compare_symbols (ap, bp)
 {
   const asymbol *a = *(const asymbol **)ap;
   const asymbol *b = *(const asymbol **)bp;
+  const char *an, *bn;
+  size_t anl, bnl;
+  boolean af, bf;
+  flagword aflags, bflags;
 
-  if (a->value > b->value)
+  if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
     return 1;
-  else if (a->value < b->value)
+  else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
     return -1;
 
   if (a->section > b->section)
     return 1;
   else if (a->section < b->section)
     return -1;
+
+  an = bfd_asymbol_name (a);
+  bn = bfd_asymbol_name (b);
+  anl = strlen (an);
+  bnl = strlen (bn);
+
+  /* The symbols gnu_compiled and gcc2_compiled convey no real
+     information, so put them after other symbols with the same value.  */
+
+  af = (strstr (an, "gnu_compiled") != NULL
+       || strstr (an, "gcc2_compiled") != NULL);
+  bf = (strstr (bn, "gnu_compiled") != NULL
+       || strstr (bn, "gcc2_compiled") != NULL);
+
+  if (af && ! bf)
+    return 1;
+  if (! af && bf)
+    return -1;
+
+  /* We use a heuristic for the file name, to try to sort it after
+     more useful symbols.  It may not work on non Unix systems, but it
+     doesn't really matter; the only difference is precisely which
+     symbol names get printed.  */
+
+#define file_symbol(s, sn, snl)                        \
+  (((s)->flags & BSF_FILE) != 0                        \
+   || ((sn)[(snl) - 2] == '.'                  \
+       && ((sn)[(snl) - 1] == 'o'              \
+          || (sn)[(snl) - 1] == 'a')))
+
+  af = file_symbol (a, an, anl);
+  bf = file_symbol (b, bn, bnl);
+
+  if (af && ! bf)
+    return 1;
+  if (! af && bf)
+    return -1;
+
+  /* Finally, try to sort global symbols before local symbols before
+     debugging symbols.  */
+
+  aflags = a->flags;
+  bflags = b->flags;
+
+  if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING))
+    {
+      if ((aflags & BSF_DEBUGGING) != 0)
+       return 1;
+      else
+       return -1;
+    }
+  if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL))
+    {
+      if ((aflags & BSF_LOCAL) != 0)
+       return 1;
+      else
+       return -1;
+    }
+
   return 0;
 }
 
@@ -321,8 +431,30 @@ compare_relocs (ap, bp)
   else if (a->address < b->address)
     return -1;
 
-  return compare_symbols ((const PTR) a->sym_ptr_ptr,
-                         (const PTR) b->sym_ptr_ptr);
+  /* So that associated relocations tied to the same address show up
+     in the correct order, we don't do any further sorting.  */
+  if (a > b)
+    return 1;
+  else if (a < b)
+    return -1;
+  else
+    return 0;
+}
+
+/* Print VMA to STREAM with no leading zeroes.  */
+
+static void
+objdump_print_value (vma, stream)
+     bfd_vma vma;
+     FILE *stream;
+{
+  char buf[30];
+  char *p;
+
+  sprintf_vma (buf, vma);
+  for (p = buf; *p == '0'; ++p)
+    ;
+  fprintf (stream, "%s", p);
 }
 
 /* Print VMA symbolically to INFO if possible.  */
@@ -332,24 +464,19 @@ objdump_print_address (vma, info)
      bfd_vma vma;
      struct disassemble_info *info;
 {
-  /* @@ For relocateable files, should filter out symbols belonging to
-     the wrong section.  Unfortunately, not enough information is supplied
-     to this routine to determine the correct section in all cases.  */
   /* @@ Would it speed things up to cache the last two symbols returned,
      and maybe their address ranges?  For many processors, only one memory
      operand can be present at a time, so the 2-entry cache wouldn't be
      constantly churned by code doing heavy memory accesses.  */
 
-  /* Indices in `syms'.  */
+  /* Indices in `sorted_syms'.  */
   long min = 0;
-  long max = symcount;
+  long max = sorted_symcount;
   long thisplace;
 
-  bfd_signed_vma vardiff;
-
   fprintf_vma (info->stream, vma);
 
-  if (symcount < 1)
+  if (sorted_symcount < 1)
     return;
 
   /* Perform a binary search looking for the closest symbol to the
@@ -359,13 +486,11 @@ objdump_print_address (vma, info)
       asymbol *sym;
 
       thisplace = (max + min) / 2;
-      sym = syms[thisplace];
-
-      vardiff = sym->value - vma;
+      sym = sorted_syms[thisplace];
 
-      if (vardiff > 0)
+      if (bfd_asymbol_value (sym) > vma)
        max = thisplace;
-      else if (vardiff < 0)
+      else if (bfd_asymbol_value (sym) < vma)
        min = thisplace;
       else
        {
@@ -375,39 +500,14 @@ objdump_print_address (vma, info)
     }
 
   /* The symbol we want is now in min, the low end of the range we
-     were searching.  */
+     were searching.  If there are several symbols with the same
+     value, we want the first one.  */
   thisplace = min;
+  while (thisplace > 0
+        && (bfd_asymbol_value (sorted_syms[thisplace])
+            == bfd_asymbol_value (sorted_syms[thisplace - 1])))
+    --thisplace;
 
-  {
-    /* If this symbol isn't global, search for one with the same value
-       that is.  */
-    bfd_vma val = syms[thisplace]->value;
-    long i;
-    if (syms[thisplace]->flags & (BSF_LOCAL|BSF_DEBUGGING))
-      for (i = thisplace - 1; i >= 0; i--)
-       {
-         if (syms[i]->value == val
-             && (!(syms[i]->flags & (BSF_LOCAL|BSF_DEBUGGING))
-                 || ((syms[thisplace]->flags & BSF_DEBUGGING)
-                     && !(syms[i]->flags & BSF_DEBUGGING))))
-           {
-             thisplace = i;
-             break;
-           }
-       }
-    if (syms[thisplace]->flags & (BSF_LOCAL|BSF_DEBUGGING))
-      for (i = thisplace + 1; i < symcount; i++)
-       {
-         if (syms[i]->value == val
-             && (!(syms[i]->flags & (BSF_LOCAL|BSF_DEBUGGING))
-                 || ((syms[thisplace]->flags & BSF_DEBUGGING)
-                     && !(syms[i]->flags & BSF_DEBUGGING))))
-           {
-             thisplace = i;
-             break;
-           }
-       }
-  }
   {
     /* If the file is relocateable, and the symbol could be from this
        section, prefer a symbol from this section over symbols from
@@ -421,129 +521,325 @@ objdump_print_address (vma, info)
     long i;
 
     aux = (struct objdump_disasm_info *) info->application_data;
-    if ((aux->abfd->flags & HAS_RELOC)
-       && vma >= bfd_get_section_vma (aux->abfd, aux->sec)
-       && vma < (bfd_get_section_vma (aux->abfd, aux->sec)
-                 + bfd_get_section_size_before_reloc (aux->sec))
-       && syms[thisplace]->section != aux->sec)
+    if (sorted_syms[thisplace]->section != aux->sec
+       && (aux->require_sec
+           || ((aux->abfd->flags & HAS_RELOC) != 0
+               && vma >= bfd_get_section_vma (aux->abfd, aux->sec)
+               && vma < (bfd_get_section_vma (aux->abfd, aux->sec)
+                         + bfd_section_size (aux->abfd, aux->sec)))))
       {
-       for (i = thisplace + 1; i < symcount; i++)
+       for (i = thisplace + 1; i < sorted_symcount; i++)
          {
-           if (syms[i]->value != syms[thisplace]->value)
+           if (bfd_asymbol_value (sorted_syms[i])
+               != bfd_asymbol_value (sorted_syms[thisplace]))
              break;
          }
        --i;
        for (; i >= 0; i--)
          {
-           if (syms[i]->section == aux->sec)
+           if (sorted_syms[i]->section == aux->sec
+               && (i == 0
+                   || sorted_syms[i - 1]->section != aux->sec
+                   || (bfd_asymbol_value (sorted_syms[i])
+                       != bfd_asymbol_value (sorted_syms[i - 1]))))
              {
                thisplace = i;
                break;
              }
          }
+
+       if (sorted_syms[thisplace]->section != aux->sec)
+         {
+           /* We didn't find a good symbol with a smaller value.
+               Look for one with a larger value.  */
+           for (i = thisplace + 1; i < sorted_symcount; i++)
+             {
+               if (sorted_syms[i]->section == aux->sec)
+                 {
+                   thisplace = i;
+                   break;
+                 }
+             }
+         }
+
+       if (sorted_syms[thisplace]->section != aux->sec
+           && (aux->require_sec
+               || ((aux->abfd->flags & HAS_RELOC) != 0
+                   && vma >= bfd_get_section_vma (aux->abfd, aux->sec)
+                   && vma < (bfd_get_section_vma (aux->abfd, aux->sec)
+                             + bfd_section_size (aux->abfd, aux->sec)))))
+         {
+           bfd_vma secaddr;
+
+           fprintf (info->stream, " <%s",
+                    bfd_get_section_name (aux->abfd, aux->sec));
+           secaddr = bfd_get_section_vma (aux->abfd, aux->sec);
+           if (vma < secaddr)
+             {
+               fprintf (info->stream, "-");
+               objdump_print_value (secaddr - vma, info->stream);
+             }
+           else if (vma > secaddr)
+             {
+               fprintf (info->stream, "+");
+               objdump_print_value (vma - secaddr, info->stream);
+             }
+           fprintf (info->stream, ">");
+           return;
+         }
       }
   }
-  fprintf (info->stream, " <%s", syms[thisplace]->name);
-  if (syms[thisplace]->value > vma)
+
+  fprintf (info->stream, " <%s", sorted_syms[thisplace]->name);
+  if (bfd_asymbol_value (sorted_syms[thisplace]) > vma)
     {
-      char buf[30], *p = buf;
-      sprintf_vma (buf, syms[thisplace]->value - vma);
-      while (*p == '0')
-       p++;
-      fprintf (info->stream, "-%s", p);
+      fprintf (info->stream, "-");
+      objdump_print_value (bfd_asymbol_value (sorted_syms[thisplace]) - vma,
+                          info->stream);
     }
-  else if (vma > syms[thisplace]->value)
+  else if (vma > bfd_asymbol_value (sorted_syms[thisplace]))
     {
-      char buf[30], *p = buf;
-      sprintf_vma (buf, vma - syms[thisplace]->value);
-      while (*p == '0')
-       p++;
-      fprintf (info->stream, "+%s", p);
+      fprintf (info->stream, "+");
+      objdump_print_value (vma - bfd_asymbol_value (sorted_syms[thisplace]),
+                          info->stream);
     }
   fprintf (info->stream, ">");
 }
 
-void
-disassemble_data (abfd)
+/* Hold the last function name and the last line number we displayed
+   in a disassembly.  */
+
+static char *prev_functionname;
+static unsigned int prev_line;
+
+/* We keep a list of all files that we have seen when doing a
+   dissassembly with source, so that we know how much of the file to
+   display.  This can be important for inlined functions.  */
+
+struct print_file_list
+{
+  struct print_file_list *next;
+  char *filename;
+  unsigned int line;
+  FILE *f;
+};
+
+static struct print_file_list *print_files;
+
+/* The number of preceding context lines to show when we start
+   displaying a file for the first time.  */
+
+#define SHOW_PRECEDING_CONTEXT_LINES (5)
+
+/* Skip ahead to a given line in a file, optionally printing each
+   line.  */
+
+static void
+skip_to_line PARAMS ((struct print_file_list *, unsigned int, boolean));
+
+static void
+skip_to_line (p, line, show)
+     struct print_file_list *p;
+     unsigned int line;
+     boolean show;
+{
+  while (p->line < line)
+    {
+      char buf[100];
+
+      if (fgets (buf, sizeof buf, p->f) == NULL)
+       {
+         fclose (p->f);
+         p->f = NULL;
+         break;
+       }
+
+      if (show)
+       printf ("%s", buf);
+
+      if (strchr (buf, '\n') != NULL)
+       ++p->line;
+    }
+}  
+
+/* Show the line number, or the source line, in a dissassembly
+   listing.  */
+
+static void
+show_line (abfd, section, off)
      bfd *abfd;
+     asection *section;
+     bfd_vma off;
 {
-  long i;
-  unsigned int (*print) () = 0; /* Old style */
-  disassembler_ftype disassemble_fn = 0; /* New style */
-  struct disassemble_info disasm_info;
-  struct objdump_disasm_info aux;
+  CONST char *filename;
+  CONST char *functionname;
+  unsigned int line;
 
-  int prevline = 0;
-  char *prev_function = NULL;
+  if (! with_line_numbers && ! with_source_code)
+    return;
 
-  asection *section;
+  if (! bfd_find_nearest_line (abfd, section, syms, off, &filename,
+                              &functionname, &line))
+    return;
 
-  boolean done_dot = false;
+  if (filename != NULL && *filename == '\0')
+    filename = NULL;
+  if (functionname != NULL && *functionname == '\0')
+    functionname = NULL;
 
-  /* If we are dumping relocation information, read the relocs for
-     each section we are going to disassemble.  We must do this before
-     we sort the symbols.  */
-  if (dump_reloc_info)
+  if (with_line_numbers)
     {
-      for (section = abfd->sections;
-          section != (asection *) NULL;
-          section = section->next)
+      if (functionname != NULL
+         && (prev_functionname == NULL
+             || strcmp (functionname, prev_functionname) != 0))
+       printf ("%s():\n", functionname);
+      if (line > 0 && line != prev_line)
+       printf ("%s:%u\n", filename == NULL ? "???" : filename, line);
+    }
+
+  if (with_source_code
+      && filename != NULL
+      && line > 0)
+    {
+      struct print_file_list **pp, *p;
+
+      for (pp = &print_files; *pp != NULL; pp = &(*pp)->next)
+       if (strcmp ((*pp)->filename, filename) == 0)
+         break;
+      p = *pp;
+
+      if (p != NULL)
        {
-         long relsize;
-         arelent **relpp;
+         if (p != print_files)
+           {
+             int l;
+
+             /* We have reencountered a file name which we saw
+                earlier.  This implies that either we are dumping out
+                code from an included file, or the same file was
+                linked in more than once.  There are two common cases
+                of an included file: inline functions in a header
+                file, and a bison or flex skeleton file.  In the
+                former case we want to just start printing (but we
+                back up a few lines to give context); in the latter
+                case we want to continue from where we left off.  I
+                can't think of a good way to distinguish the cases,
+                so I used a heuristic based on the file name.  */
+             if (strcmp (p->filename + strlen (p->filename) - 2, ".h") != 0)
+               l = p->line;
+             else
+               {
+                 l = line - SHOW_PRECEDING_CONTEXT_LINES;
+                 if (l <= 0)
+                   l = 1;
+               }
 
-         if ((section->flags & SEC_LOAD) == 0
-             || (! disassemble_all
-                 && only == NULL
-                 && (section->flags & SEC_CODE) == 0))
-           continue;
-         if (only != (char *) NULL && strcmp (only, section->name) != 0)
-           continue;
-         if ((section->flags & SEC_RELOC) == 0)
-           continue;
+             if (p->f == NULL)
+               {
+                 p->f = fopen (p->filename, "r");
+                 p->line = 0;
+               }
+             if (p->f != NULL)
+               skip_to_line (p, l, false);
 
-         /* We store the reloc information in the reloc_count and
-             orelocation fields.  */
+             if (print_files->f != NULL)
+               {
+                 fclose (print_files->f);
+                 print_files->f = NULL;
+               }
+           }
 
-         relsize = bfd_get_reloc_upper_bound (abfd, section);
-         if (relsize < 0)
-           bfd_fatal (bfd_get_filename (abfd));
+         if (p->f != NULL)
+           {
+             skip_to_line (p, line, true);
+             *pp = p->next;
+             p->next = print_files;
+             print_files = p;
+           }
+       }
+      else
+       {
+         FILE *f;
 
-         if (relsize == 0)
-           section->reloc_count = 0;
-         else
+         f = fopen (filename, "r");
+         if (f != NULL)
            {
-             long relcount;
+             int l;
 
-             relpp = (arelent **) xmalloc (relsize);
-             relcount = bfd_canonicalize_reloc (abfd, section, relpp, syms);
-             if (relcount < 0)
-               bfd_fatal (bfd_get_filename (abfd));
-             section->reloc_count = relcount;
-             section->orelocation = relpp;
+             p = ((struct print_file_list *)
+                  xmalloc (sizeof (struct print_file_list)));
+             p->filename = xmalloc (strlen (filename) + 1);
+             strcpy (p->filename, filename);
+             p->line = 0;
+             p->f = f;
+
+             if (print_files != NULL && print_files->f != NULL)
+               {
+                 fclose (print_files->f);
+                 print_files->f = NULL;
+               }
+             p->next = print_files;
+             print_files = p;
+
+             l = line - SHOW_PRECEDING_CONTEXT_LINES;
+             if (l <= 0)
+               l = 1;
+             skip_to_line (p, l, false);
+             if (p->f != NULL)
+               skip_to_line (p, line, true);
            }
        }
     }
 
-  /* Replace symbol section relative values with abs values.  */
-  for (i = 0; i < symcount; i++)
+  if (functionname != NULL
+      && (prev_functionname == NULL
+         || strcmp (functionname, prev_functionname) != 0))
     {
-      syms[i]->value += syms[i]->section->vma;
+      if (prev_functionname != NULL)
+       free (prev_functionname);
+      prev_functionname = xmalloc (strlen (functionname) + 1);
+      strcpy (prev_functionname, functionname);
     }
 
-  symcount = remove_useless_symbols (syms, symcount);
+  if (line > 0 && line != prev_line)
+    prev_line = line;
+}
+
+void
+disassemble_data (abfd)
+     bfd *abfd;
+{
+  long i;
+  disassembler_ftype disassemble_fn = 0; /* New style */
+  struct disassemble_info disasm_info;
+  struct objdump_disasm_info aux;
+  asection *section;
+  boolean done_dot = false;
+
+  print_files = NULL;
+  prev_functionname = NULL;
+  prev_line = -1;
+
+  /* We make a copy of syms to sort.  We don't want to sort syms
+     because that will screw up the relocs.  */
+  sorted_syms = (asymbol **) xmalloc (symcount * sizeof (asymbol *));
+  memcpy (sorted_syms, syms, symcount * sizeof (asymbol *));
+
+  sorted_symcount = remove_useless_symbols (sorted_syms, symcount);
 
   /* Sort the symbols into section and symbol order */
-  qsort (syms, symcount, sizeof (asymbol *), compare_symbols);
+  qsort (sorted_syms, sorted_symcount, sizeof (asymbol *), compare_symbols);
 
   INIT_DISASSEMBLE_INFO(disasm_info, stdout);
   disasm_info.application_data = (PTR) &aux;
   aux.abfd = abfd;
   disasm_info.print_address_func = objdump_print_address;
+  if (show_raw_insn)
+    disasm_info.flags |= DISASM_RAW_INSN_FLAG;
 
   if (machine != (char *) NULL)
     {
-      bfd_arch_info_type *info = bfd_scan_arch (machine);
+      const bfd_arch_info_type *info = bfd_scan_arch (machine);
       if (info == NULL)
        {
          fprintf (stderr, "%s: Can't use supplied machine %s\n",
@@ -554,23 +850,25 @@ disassemble_data (abfd)
       abfd->arch_info = info;
     }
 
-  /* See if we can disassemble using bfd.  */
-
-  if (abfd->arch_info->disassemble)
+  disassemble_fn = disassembler (abfd);
+  if (!disassemble_fn)
     {
-      print = abfd->arch_info->disassemble;
+      fprintf (stderr, "%s: Can't disassemble for architecture %s\n",
+              program_name,
+              bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
+      exit (1);
     }
+
+  disasm_info.arch = bfd_get_arch (abfd);
+  disasm_info.mach = bfd_get_mach (abfd);
+  if (bfd_big_endian (abfd))
+    disasm_info.endian = BFD_ENDIAN_BIG;
+  else if (bfd_little_endian (abfd))
+    disasm_info.endian = BFD_ENDIAN_LITTLE;
   else
-    {
-      disassemble_fn = disassembler (abfd);
-      if (!disassemble_fn)
-       {
-         fprintf (stderr, "%s: Can't disassemble for architecture %s\n",
-                  program_name,
-                  bfd_printable_arch_mach (bfd_get_arch (abfd), 0));
-         exit (1);
-       }
-    }
+    /* ??? Aborting here seems too drastic.  We could default to big or little
+       instead.  */
+    disasm_info.endian = BFD_ENDIAN_UNKNOWN;
 
   for (section = abfd->sections;
        section != (asection *) NULL;
@@ -578,8 +876,10 @@ disassemble_data (abfd)
     {
       bfd_byte *data = NULL;
       bfd_size_type datasize = 0;
+      arelent **relbuf = NULL;
       arelent **relpp = NULL;
       arelent **relppend = NULL;
+      long stop;
 
       if ((section->flags & SEC_LOAD) == 0
          || (! disassemble_all
@@ -592,11 +892,27 @@ disassemble_data (abfd)
       if (dump_reloc_info
          && (section->flags & SEC_RELOC) != 0)
        {
-         /* Sort the relocs by address.  */
-         qsort (section->orelocation, section->reloc_count,
-                sizeof (arelent *), compare_relocs);
-         relpp = section->orelocation;
-         relppend = relpp + section->reloc_count;
+         long relsize;
+
+         relsize = bfd_get_reloc_upper_bound (abfd, section);
+         if (relsize < 0)
+           bfd_fatal (bfd_get_filename (abfd));
+
+         if (relsize > 0)
+           {
+             long relcount;
+
+             relbuf = (arelent **) xmalloc (relsize);
+             relcount = bfd_canonicalize_reloc (abfd, section, relbuf, syms);
+             if (relcount < 0)
+               bfd_fatal (bfd_get_filename (abfd));
+
+             /* Sort the relocs by address.  */
+             qsort (relbuf, relcount, sizeof (arelent *), compare_relocs);
+
+             relpp = relbuf;
+             relppend = relpp + relcount;
+           }
        }
 
       printf ("Disassembly of section %s:\n", section->name);
@@ -613,10 +929,26 @@ disassemble_data (abfd)
       disasm_info.buffer = data;
       disasm_info.buffer_vma = section->vma;
       disasm_info.buffer_length = datasize;
-      i = 0;
-      while (i < disasm_info.buffer_length)
+      if (start_address == (bfd_vma) -1
+         || start_address < disasm_info.buffer_vma)
+       i = 0;
+      else
+       i = start_address - disasm_info.buffer_vma;
+      if (stop_address == (bfd_vma) -1)
+       stop = datasize;
+      else
+       {
+         if (stop_address < disasm_info.buffer_vma)
+           stop = 0;
+         else
+           stop = stop_address - disasm_info.buffer_vma;
+         if (stop > disasm_info.buffer_length)
+           stop = disasm_info.buffer_length;
+       }
+      while (i < stop)
        {
          int bytes;
+         boolean need_nl = false;
 
          if (data[i] == 0 && data[i + 1] == 0 && data[i + 2] == 0 &&
              data[i + 3] == 0)
@@ -631,64 +963,29 @@ disassemble_data (abfd)
          else
            {
              done_dot = false;
-             if (with_line_numbers)
-               {
-                 CONST char *filename;
-                 CONST char *functionname;
-                 unsigned int line;
-
-                 if (bfd_find_nearest_line (abfd,
-                                            section,
-                                            syms,
-                                            section->vma + i,
-                                            &filename,
-                                            &functionname,
-                                            &line))
-                   {
-                     if (functionname
-                         && *functionname != '\0'
-                         && (prev_function == NULL
-                             || strcmp (functionname, prev_function) != 0))
-                       {
-                         printf ("%s():\n", functionname);
-                         if (prev_function != NULL)
-                           free (prev_function);
-                         prev_function = xmalloc (strlen (functionname) + 1);
-                         strcpy (prev_function, functionname);
-                       }
-                     if (!filename)
-                       filename = "???";
-                     if (line && line != prevline)
-                       {
-                         printf ("%s:%u\n", filename, line);
-                         prevline = line;
-                       }
-                   }
-               }
+             if (with_line_numbers || with_source_code)
+               show_line (abfd, section, i);
+             aux.require_sec = true;
              objdump_print_address (section->vma + i, &disasm_info);
+             aux.require_sec = false;
              putchar (' ');
 
-             if (disassemble_fn)
-               {
-                 /* New style */
-                 bytes = (*disassemble_fn) (section->vma + i, &disasm_info);
-                 if (bytes < 0)
-                   break;
-               }
+             bytes = (*disassemble_fn) (section->vma + i, &disasm_info);
+             if (bytes < 0)
+               break;
+
+             if (!wide_output)
+               putchar ('\n');
              else
-               {
-                 /* Old style */
-                 bytes = print (section->vma + i, data + i, stdout);
-               }
-             putchar ('\n');
+               need_nl = true;
            }
 
          if (dump_reloc_info
              && (section->flags & SEC_RELOC) != 0)
            {
              while (relpp < relppend
-                    && ((*relpp)->address >= i
-                        && (*relpp)->address < i + bytes))
+                    && ((*relpp)->address >= (bfd_vma) i
+                        && (*relpp)->address < (bfd_vma) i + bytes))
                {
                  arelent *q;
                  const char *sym_name;
@@ -725,15 +1022,22 @@ disassemble_data (abfd)
                    }
 
                  printf ("\n");
-
+                 need_nl = false;
                  ++relpp;
                }
            }
 
+         if (need_nl)
+           printf ("\n");
+
          i += bytes;
        }
+
       free (data);
+      if (relbuf != NULL)
+       free (relbuf);
     }
+  free (sorted_syms);
 }
 \f
 
@@ -927,11 +1231,24 @@ dump_section_stabs (abfd, stabsect_name, strsect_name)
      char *stabsect_name;
      char *strsect_name;
 {
-  if (read_section_stabs (abfd, stabsect_name, strsect_name))
+  asection *s;
+
+  /* Check for section names for which stabsect_name is a prefix, to
+     handle .stab0, etc.  */
+  for (s = abfd->sections;
+       s != NULL;
+       s = s->next)
     {
-      print_section_stabs (abfd, stabsect_name, strsect_name);
-      free (stabs);
-      free (strtab);
+      if (strncmp (stabsect_name, s->name, strlen (stabsect_name)) == 0
+         && strncmp (strsect_name, s->name, strlen (strsect_name)) != 0)
+       {
+         if (read_section_stabs (abfd, s->name, strsect_name))
+           {
+             print_section_stabs (abfd, s->name, strsect_name);
+             free (stabs);
+             free (strtab);
+           }
+       }
     }
 }
 \f
@@ -960,7 +1277,13 @@ dump_bfd_header (abfd)
   printf ("\nstart address 0x");
   printf_vma (abfd->start_address);
 }
-
+\f
+static void
+dump_bfd_private_header (abfd)
+bfd *abfd;
+{
+  bfd_print_private_bfd_data (abfd, stdout);
+}
 static void
 display_bfd (abfd)
      bfd *abfd;
@@ -984,10 +1307,12 @@ display_bfd (abfd)
     print_arelt_descr (stdout, abfd, true);
   if (dump_file_header)
     dump_bfd_header (abfd);
+  if (dump_private_headers)
+    dump_bfd_private_header (abfd);
   putchar ('\n');
   if (dump_section_headers)
     dump_headers (abfd);
-  if (dump_symtab || dump_reloc_info || disassemble)
+  if (dump_symtab || dump_reloc_info || disassemble || dump_debugging)
     {
       syms = slurp_symtab (abfd);
     }
@@ -1007,10 +1332,30 @@ display_bfd (abfd)
     dump_dynamic_relocs (abfd);
   if (dump_section_contents)
     dump_data (abfd);
-  /* Note that disassemble_data re-orders the syms table, but that is
-     safe - as long as it is done last! */
   if (disassemble)
     disassemble_data (abfd);
+  if (dump_debugging)
+    {
+      PTR dhandle;
+
+      dhandle = read_debugging_info (abfd, syms, symcount);
+      if (dhandle != NULL)
+       {
+         if (! print_debugging_info (stdout, dhandle))
+           fprintf (stderr, "%s: printing debugging information failed\n",
+                    bfd_get_filename (abfd));
+       }
+    }
+  if (syms)
+    {
+      free (syms);
+      syms = NULL;
+    }
+  if (dynsyms)
+    {
+      free (dynsyms);
+      dynsyms = NULL;
+    }
 }
 
 static void
@@ -1072,6 +1417,7 @@ dump_data (abfd)
   bfd_byte *data = 0;
   bfd_size_type datasize = 0;
   bfd_size_type i;
+  bfd_size_type start, stop;
 
   for (section = abfd->sections; section != NULL; section =
        section->next)
@@ -1093,14 +1439,30 @@ dump_data (abfd)
 
              bfd_get_section_contents (abfd, section, (PTR) data, 0, bfd_section_size (abfd, section));
 
-             for (i = 0; i < bfd_section_size (abfd, section); i += onaline)
+             if (start_address == (bfd_vma) -1
+                 || start_address < section->vma)
+               start = 0;
+             else
+               start = start_address - section->vma;
+             if (stop_address == (bfd_vma) -1)
+               stop = bfd_section_size (abfd, section);
+             else
+               {
+                 if (stop_address < section->vma)
+                   stop = 0;
+                 else
+                   stop = stop_address - section->vma;
+                 if (stop > bfd_section_size (abfd, section))
+                   stop = bfd_section_size (abfd, section);
+               }
+             for (i = start; i < stop; i += onaline)
                {
                  bfd_size_type j;
 
                  printf (" %04lx ", (unsigned long int) (i + section->vma));
                  for (j = i; j < i + onaline; j++)
                    {
-                     if (j < bfd_section_size (abfd, section))
+                     if (j < stop)
                        printf ("%02x", (unsigned) (data[j]));
                      else
                        printf ("  ");
@@ -1111,7 +1473,7 @@ dump_data (abfd)
                  printf (" ");
                  for (j = i; j < i + onaline; j++)
                    {
-                     if (j >= bfd_section_size (abfd, section))
+                     if (j >= stop)
                        printf (" ");
                      else
                        printf ("%c", isprint (data[j]) ? data[j] : '.');
@@ -1197,12 +1559,12 @@ dump_relocs (abfd)
       else if ((a->flags & SEC_RELOC) == 0)
        continue;
 
-      printf ("RELOCATION RECORDS FOR [%s]:", a->name);
-
       relsize = bfd_get_reloc_upper_bound (abfd, a);
       if (relsize < 0)
        bfd_fatal (bfd_get_filename (abfd));
 
+      printf ("RELOCATION RECORDS FOR [%s]:", a->name);
+
       if (relsize == 0)
        {
          printf (" (none)\n\n");
@@ -1210,7 +1572,6 @@ dump_relocs (abfd)
       else
        {
          relpp = (arelent **) xmalloc (relsize);
-         /* Note that this must be done *before* we sort the syms table. */
          relcount = bfd_canonicalize_reloc (abfd, a, relpp, syms);
          if (relcount < 0)
            bfd_fatal (bfd_get_filename (abfd));
@@ -1237,12 +1598,12 @@ dump_dynamic_relocs (abfd)
   arelent **relpp;
   long relcount;
 
-  printf ("DYNAMIC RELOCATION RECORDS");
-
   relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
   if (relsize < 0)
     bfd_fatal (bfd_get_filename (abfd));
 
+  printf ("DYNAMIC RELOCATION RECORDS");
+
   if (relsize == 0)
     {
       printf (" (none)\n\n");
@@ -1293,6 +1654,13 @@ dump_reloc_set (abfd, relpp, relcount)
       CONST char *sym_name;
       CONST char *section_name;
 
+      if (start_address != (bfd_vma) -1
+         && q->address < start_address)
+       continue;
+      if (stop_address != (bfd_vma) -1
+         && q->address > stop_address)
+       continue;
+
       if (q->sym_ptr_ptr && *q->sym_ptr_ptr)
        {
          sym_name = (*(q->sym_ptr_ptr))->name;
@@ -1331,6 +1699,22 @@ dump_reloc_set (abfd, relpp, relcount)
 /* The length of the longest architecture name + 1.  */
 #define LONGEST_ARCH sizeof("rs6000:6000")
 
+#ifndef L_tmpnam
+#define L_tmpnam 25
+#endif
+
+static const char *
+endian_string (endian)
+     enum bfd_endian endian;
+{
+  if (endian == BFD_ENDIAN_BIG)
+    return "big endian";
+  else if (endian == BFD_ENDIAN_LITTLE)
+    return "little endian";
+  else
+    return "endianness unknown";
+}
+
 /* List the targets that BFD is configured to support, each followed
    by its endianness and the architectures it supports.  */
 
@@ -1339,10 +1723,11 @@ display_target_list ()
 {
   extern char *tmpnam ();
   extern bfd_target *bfd_target_vector[];
+  char tmparg[L_tmpnam];
   char *dummy_name;
   int t;
 
-  dummy_name = tmpnam ((char *) NULL);
+  dummy_name = tmpnam (tmparg);
   for (t = 0; bfd_target_vector[t]; t++)
     {
       bfd_target *p = bfd_target_vector[t];
@@ -1350,8 +1735,8 @@ display_target_list ()
       int a;
 
       printf ("%s\n (header %s, data %s)\n", p->name,
-             p->header_byteorder_big_p ? "big endian" : "little endian",
-             p->byteorder_big_p ? "big endian" : "little endian");
+             endian_string (p->header_byteorder),
+             endian_string (p->byteorder));
 
       if (abfd == NULL)
        {
@@ -1385,6 +1770,7 @@ display_info_table (first, last)
 {
   extern bfd_target *bfd_target_vector[];
   extern char *tmpnam ();
+  char tmparg[L_tmpnam];
   int t, a;
   char *dummy_name;
 
@@ -1394,7 +1780,7 @@ display_info_table (first, last)
     printf ("%s ", bfd_target_vector[t]->name);
   putchar ('\n');
 
-  dummy_name = tmpnam ((char *) NULL);
+  dummy_name = tmpnam (tmparg);
   for (a = (int) bfd_arch_obscure + 1; a < (int) bfd_arch_last; a++)
     if (strcmp (bfd_printable_arch_mach (a, 0), "UNKNOWN!") != 0)
       {
@@ -1502,13 +1888,16 @@ main (argc, argv)
   program_name = *argv;
   xmalloc_set_program_name (program_name);
 
+  START_PROGRESS (program_name, 0);
+
   bfd_init ();
 
-  while ((c = getopt_long (argc, argv, "ib:m:VdDlfahrRtTxsj:", long_options,
+  while ((c = getopt_long (argc, argv, "pib:m:VdDlfahrRtTxsSj:w", long_options,
                           (int *) 0))
         != EOF)
     {
-      seenflag = true;
+      if (c != 'l' && c != OPTION_START_ADDRESS && c != OPTION_STOP_ADDRESS)
+       seenflag = true;
       switch (c)
        {
        case 0:
@@ -1531,7 +1920,11 @@ main (argc, argv)
        case 'i':
          formats_info = true;
          break;
+       case 'p':
+         dump_private_headers = 1;
+         break;
        case 'x':
+         dump_private_headers = 1;
          dump_symtab = 1;
          dump_reloc_info = 1;
          dump_file_header = true;
@@ -1550,6 +1943,10 @@ main (argc, argv)
        case 'D':
          disassemble = disassemble_all = true;
          break;
+       case 'S':
+         disassemble = true;
+         with_source_code = true;
+         break;
        case 's':
          dump_section_contents = 1;
          break;
@@ -1570,6 +1967,15 @@ main (argc, argv)
        case 'V':
          show_version = 1;
          break;
+       case 'w':
+         wide_output = 1;
+         break;
+       case OPTION_START_ADDRESS:
+         start_address = parse_vma (optarg, "--start-address");
+         break;
+       case OPTION_STOP_ADDRESS:
+         stop_address = parse_vma (optarg, "--stop-address");
+         break;
        default:
          usage (stderr, 1);
        }
@@ -1596,5 +2002,8 @@ main (argc, argv)
        for (; optind < argc;)
          display_file (argv[optind++], target);
     }
+
+  END_PROGRESS (program_name);
+
   return 0;
 }
This page took 0.044426 seconds and 4 git commands to generate.