20000-05-18 H.J. Lu (hjl@gnu.org)
[binutils-gdb.git] / gdb / dwarf2read.c
index 8f0274b447a49878e7e73cc1bd4bf11028a1ed76..9212e389ffc5d4490dcd1c2da3820f3a4ae6e0c8 100644 (file)
@@ -27,7 +27,6 @@
 
 #include "defs.h"
 #include "bfd.h"
-#include "elf-bfd.h"
 #include "symtab.h"
 #include "gdbtypes.h"
 #include "symfile.h"
@@ -546,15 +545,6 @@ static struct complaint dwarf2_unsupported_const_value_attr =
    whatever scope is currently getting read. */
 static int address_size;
 
-/* Some elf32 object file formats while linked for a 32 bit address
-   space contain debug information that has assumed 64 bit
-   addresses. Eg 64 bit MIPS target produced by GCC/GAS/LD where the
-   symbol table contains 32bit address values while its .debug_info
-   section contains 64 bit address values.
-   ADDRESS_SIGNIFICANT_SIZE specifies the number significant bits in
-   the ADDRESS_SIZE bytes read from the file */
-static int address_significant_size;
-
 /* Externals references.  */
 extern int info_verbose;       /* From main.c; nonzero => verbose */
 
@@ -563,14 +553,10 @@ extern int info_verbose;  /* From main.c; nonzero => verbose */
 static void dwarf2_locate_sections PARAMS ((bfd *, asection *, PTR));
 
 #if 0
-static void dwarf2_build_psymtabs_easy PARAMS ((struct objfile *,
-                                               struct section_offsets *,
-                                               int));
+static void dwarf2_build_psymtabs_easy PARAMS ((struct objfile *, int));
 #endif
 
-static void dwarf2_build_psymtabs_hard PARAMS ((struct objfile *,
-                                               struct section_offsets *,
-                                               int));
+static void dwarf2_build_psymtabs_hard PARAMS ((struct objfile *, int));
 
 static char *scan_partial_symbols PARAMS ((char *, struct objfile *,
                                           CORE_ADDR *, CORE_ADDR *));
@@ -625,6 +611,8 @@ static void set_cu_language PARAMS ((unsigned int));
 static struct attribute *dwarf_attr PARAMS ((struct die_info *,
                                             unsigned int));
 
+static int die_is_declaration (struct die_info *);
+
 static void dwarf_decode_lines PARAMS ((unsigned int, char *, bfd *));
 
 static void dwarf2_start_subfile PARAMS ((char *, char *));
@@ -635,6 +623,10 @@ static struct symbol *new_symbol PARAMS ((struct die_info *, struct type *,
 static void dwarf2_const_value PARAMS ((struct attribute *, struct symbol *,
                                        struct objfile *));
 
+static void dwarf2_const_value_data (struct attribute *attr,
+                                    struct symbol *sym,
+                                    int bits);
+
 static struct type *die_type PARAMS ((struct die_info *, struct objfile *));
 
 static struct type *die_containing_type PARAMS ((struct die_info *,
@@ -671,8 +663,6 @@ static void dwarf2_attach_fields_to_type PARAMS ((struct field_info *,
                                                  struct type *,
                                                  struct objfile *));
 
-static char *skip_member_fn_name PARAMS ((char *));
-
 static void dwarf2_add_member_fn PARAMS ((struct field_info *,
                                          struct die_info *, struct type *,
                                          struct objfile * objfile));
@@ -840,9 +830,8 @@ dwarf2_locate_sections (ignore_abfd, sectp, ignore_ptr)
 /* Build a partial symbol table.  */
 
 void
-dwarf2_build_psymtabs (objfile, section_offsets, mainline)
+dwarf2_build_psymtabs (objfile, mainline)
      struct objfile *objfile;
-     struct section_offsets *section_offsets;
      int mainline;
 {
 
@@ -867,17 +856,17 @@ dwarf2_build_psymtabs (objfile, section_offsets, mainline)
 #if 0
   if (dwarf_aranges_offset && dwarf_pubnames_offset)
     {
-      /* Things are significanlty easier if we have .debug_aranges and
+      /* Things are significantly easier if we have .debug_aranges and
          .debug_pubnames sections */
 
-      dwarf2_build_psymtabs_easy (objfile, section_offsets, mainline);
+      dwarf2_build_psymtabs_easy (objfile, mainline);
     }
   else
 #endif
     /* only test this case for now */
     {
       /* In this case we have to work a bit harder */
-      dwarf2_build_psymtabs_hard (objfile, section_offsets, mainline);
+      dwarf2_build_psymtabs_hard (objfile, mainline);
     }
 }
 
@@ -886,9 +875,8 @@ dwarf2_build_psymtabs (objfile, section_offsets, mainline)
    .debug_pubnames and .debug_aranges sections.  */
 
 static void
-dwarf2_build_psymtabs_easy (objfile, section_offsets, mainline)
+dwarf2_build_psymtabs_easy (objfile, mainline)
      struct objfile *objfile;
-     struct section_offsets *section_offsets;
      int mainline;
 {
   bfd *abfd = objfile->obfd;
@@ -923,9 +911,8 @@ dwarf2_build_psymtabs_easy (objfile, section_offsets, mainline)
    .debug_info and .debug_abbrev sections.  */
 
 static void
-dwarf2_build_psymtabs_hard (objfile, section_offsets, mainline)
+dwarf2_build_psymtabs_hard (objfile, mainline)
      struct objfile *objfile;
-     struct section_offsets *section_offsets;
      int mainline;
 {
   /* Instead of reading this into a big buffer, we should probably use
@@ -940,9 +927,6 @@ dwarf2_build_psymtabs_hard (objfile, section_offsets, mainline)
   int comp_unit_has_pc_info;
   CORE_ADDR lowpc, highpc;
 
-  /* Number of bytes of any addresses that are signficant */
-  address_significant_size = get_elf_backend_data (abfd)->s->arch_size / 8;
-
   info_ptr = dwarf_info_buffer;
   abbrev_ptr = dwarf_abbrev_buffer;
 
@@ -983,13 +967,6 @@ dwarf2_build_psymtabs_hard (objfile, section_offsets, mainline)
                 (long) (beg_of_comp_unit - dwarf_info_buffer));
          return;
        }
-      if (address_size < address_significant_size)
-       {
-         error ("Dwarf Error: bad address size (%ld) in compilation unit header (offset 0x%lx + 11).",
-                (long) cu_header.addr_size,
-                (long) (beg_of_comp_unit - dwarf_info_buffer));
-       }
-
       /* Read the abbrevs for this compilation unit into a table */
       dwarf2_read_abbrevs (abfd, cu_header.abbrev_offset);
       make_cleanup (dwarf2_empty_abbrev_table, NULL);
@@ -1002,8 +979,8 @@ dwarf2_build_psymtabs_hard (objfile, section_offsets, mainline)
       set_cu_language (comp_unit_die.language);
 
       /* Allocate a new partial symbol table structure */
-      pst = start_psymtab_common (objfile, section_offsets,
-                              comp_unit_die.name ? comp_unit_die.name : "",
+      pst = start_psymtab_common (objfile, objfile->section_offsets,
+                                 comp_unit_die.name ? comp_unit_die.name : "",
                                  comp_unit_die.lowpc,
                                  objfile->global_psymbols.next,
                                  objfile->static_psymbols.next);
@@ -1016,7 +993,7 @@ dwarf2_build_psymtabs_hard (objfile, section_offsets, mainline)
       DWARF_ABBREV_BUFFER (pst) = dwarf_abbrev_buffer;
       DWARF_ABBREV_SIZE (pst) = dwarf_abbrev_size;
       DWARF_LINE_BUFFER (pst) = dwarf_line_buffer;
-      baseaddr = ANOFFSET (section_offsets, 0);
+      baseaddr = ANOFFSET (objfile->section_offsets, 0);
 
       /* Store the function that reads in the rest of the symbol table */
       pst->read_symtab = dwarf2_psymtab_to_symtab;
@@ -1076,7 +1053,7 @@ scan_partial_symbols (info_ptr, objfile, lowpc, highpc)
   int nesting_level = 1;
   int has_pc_info;
 
-  *lowpc = ((CORE_ADDR) - 1);
+  *lowpc = ((CORE_ADDR) -1);
   *highpc = ((CORE_ADDR) 0);
 
   while (nesting_level)
@@ -1156,7 +1133,7 @@ scan_partial_symbols (info_ptr, objfile, lowpc, highpc)
 
   /* If we didn't find a lowpc, set it to highpc to avoid complaints
      from `maint check'.  */
-  if (*lowpc == ((CORE_ADDR) - 1))
+  if (*lowpc == ((CORE_ADDR) -1))
     *lowpc = *highpc;
   return info_ptr;
 }
@@ -1327,7 +1304,7 @@ psymtab_to_symtab_1 (pst)
   back_to = make_cleanup (dwarf2_free_tmp_obstack, NULL);
 
   buildsym_init ();
-  make_cleanup ((make_cleanup_func) really_free_pendings, NULL);
+  make_cleanup (really_free_pendings, NULL);
 
   /* read in the comp_unit header  */
   cu_header.length = read_4_bytes (abfd, info_ptr);
@@ -1468,7 +1445,7 @@ read_file_scope (die, objfile)
      struct objfile *objfile;
 {
   unsigned int line_offset = 0;
-  CORE_ADDR lowpc = ((CORE_ADDR) - 1);
+  CORE_ADDR lowpc = ((CORE_ADDR) -1);
   CORE_ADDR highpc = ((CORE_ADDR) 0);
   struct attribute *attr;
   char *name = "<unknown>";
@@ -1500,7 +1477,7 @@ read_file_scope (die, objfile)
 
   /* If we didn't find a lowpc, set it to highpc to avoid complaints
      from finish_block.  */
-  if (lowpc == ((CORE_ADDR) - 1))
+  if (lowpc == ((CORE_ADDR) -1))
     lowpc = highpc;
   lowpc += baseaddr;
   highpc += baseaddr;
@@ -1604,12 +1581,6 @@ read_func_scope (die, objfile)
       objfile->ei.entry_func_highpc = highpc;
     }
 
-  if (STREQ (name, "main"))    /* FIXME: hardwired name */
-    {
-      objfile->ei.main_func_lowpc = lowpc;
-      objfile->ei.main_func_highpc = highpc;
-    }
-
   /* Decode DW_AT_frame_base location descriptor if present, keep result
      for DW_OP_fbreg operands in decode_locdesc.  */
   frame_base_reg = -1;
@@ -1861,23 +1832,17 @@ dwarf2_add_field (fip, die, objfile)
   else if (die->tag == DW_TAG_variable)
     {
       char *physname;
-      char *cp;
 
       /* C++ static member.
-         Get physical name, extract field name from physical name.  */
-      physname = dwarf2_linkage_name (die);
-      if (physname == NULL)
+        Get name of field.  */
+      attr = dwarf_attr (die, DW_AT_name);
+      if (attr && DW_STRING (attr))
+       fieldname = DW_STRING (attr);
+      else
        return;
 
-      cp = physname;
-      while (*cp && !is_cplus_marker (*cp))
-       cp++;
-      if (*cp)
-       fieldname = cp + 1;
-      if (*fieldname == '\0')
-       {
-         complain (&dwarf2_bad_static_member_name, physname);
-       }
+      /* Get physical name.  */
+      physname = dwarf2_linkage_name (die);
 
       SET_FIELD_PHYSNAME (*fp, obsavestring (physname, strlen (physname),
                                             &objfile->type_obstack));
@@ -1987,37 +1952,6 @@ dwarf2_attach_fields_to_type (fip, type, objfile)
     }
 }
 
-/* Skip to the end of a member function name in a mangled name.  */
-
-static char *
-skip_member_fn_name (physname)
-     char *physname;
-{
-  char *endname = physname;
-
-  /* Skip over leading underscores.  */
-  while (*endname == '_')
-    endname++;
-
-  /* Find two succesive underscores.  */
-  do
-    endname = strchr (endname, '_');
-  while (endname != NULL && *++endname != '_');
-
-  if (endname == NULL)
-    {
-      complain (&dwarf2_bad_member_name_complaint, physname);
-      endname = physname;
-    }
-  else
-    {
-      /* Take care of trailing underscores.  */
-      if (endname[1] != '_')
-       endname--;
-    }
-  return endname;
-}
-
 /* Add a member function to the proper fieldlist.  */
 
 static void
@@ -2035,46 +1969,15 @@ dwarf2_add_member_fn (fip, die, type, objfile)
   char *physname;
   struct nextfnfield *new_fnfield;
 
-  /* Extract member function name from mangled name.  */
-  physname = dwarf2_linkage_name (die);
-  if (physname == NULL)
-    return;
-  if ((physname[0] == '_' && physname[1] == '_'
-       && strchr ("0123456789Qt", physname[2]))
-      || DESTRUCTOR_PREFIX_P (physname))
-    {
-      /* Constructor and destructor field names are set to the name
-         of the class, but without template parameter lists.
-         The name might be missing for anonymous aggregates.  */
-      if (TYPE_TAG_NAME (type))
-       {
-         char *p = strchr (TYPE_TAG_NAME (type), '<');
-
-         if (p == NULL)
-           fieldname = TYPE_TAG_NAME (type);
-         else
-           fieldname = obsavestring (TYPE_TAG_NAME (type),
-                                     p - TYPE_TAG_NAME (type),
-                                     &objfile->type_obstack);
-       }
-      else
-       {
-         char *anon_name = "";
-         fieldname = obsavestring (anon_name, strlen (anon_name),
-                                   &objfile->type_obstack);
-       }
-    }
+  /* Get name of member function.  */
+  attr = dwarf_attr (die, DW_AT_name);
+  if (attr && DW_STRING (attr))
+    fieldname = DW_STRING (attr);
   else
-    {
-      char *endname = skip_member_fn_name (physname);
+    return;
 
-      /* Ignore member function if we were unable not extract the member
-         function name.  */
-      if (endname == physname)
-       return;
-      fieldname = obsavestring (physname, endname - physname,
-                               &objfile->type_obstack);
-    }
+  /* Get the mangled name.  */
+  physname = dwarf2_linkage_name (die);
 
   /* Look up member function name in fieldlist.  */
   for (i = 0; i < fip->nfnfields; i++)
@@ -2095,8 +1998,7 @@ dwarf2_add_member_fn (fip, die, type, objfile)
                      (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
                      * sizeof (struct fnfieldlist));
          if (fip->nfnfields == 0)
-           make_cleanup ((make_cleanup_func) free_current_contents,
-                         &fip->fnfieldlists);
+           make_cleanup (free_current_contents, &fip->fnfieldlists);
        }
       flp = &fip->fnfieldlists[fip->nfnfields];
       flp->name = fieldname;
@@ -2281,7 +2183,7 @@ read_structure_scope (die, objfile)
      type within the structure itself. */
   die->type = type;
 
-  if (die->has_children)
+  if (die->has_children && ! die_is_declaration (die))
     {
       struct field_info fi;
       struct die_info *child_die;
@@ -2603,8 +2505,7 @@ read_array_type (die, objfile)
                xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
                          * sizeof (struct type *));
              if (ndim == 0)
-               make_cleanup ((make_cleanup_func) free_current_contents,
-                             &range_types);
+               make_cleanup (free_current_contents, &range_types);
            }
          range_types[ndim++] = create_range_type (NULL, index_type, low, high);
        }
@@ -3564,6 +3465,9 @@ read_address (abfd, buf)
 
   switch (address_size)
     {
+    case 2:
+      retval = bfd_get_16 (abfd, (bfd_byte *) buf);
+      break;
     case 4:
       retval = bfd_get_32 (abfd, (bfd_byte *) buf);
       break;
@@ -3574,17 +3478,8 @@ read_address (abfd, buf)
       /* *THE* alternative is 8, right? */
       abort ();
     }
-  /* If the address being read is larger than the address that is
-     applicable for the object file format then mask it down to the
-     correct size.  Take care to avoid unnecessary shift or shift
-     overflow */
-  if (address_size > address_significant_size
-      && address_significant_size < sizeof (CORE_ADDR))
-    {
-      CORE_ADDR mask = ((CORE_ADDR) 0) - 1;
-      retval &= ~(mask << (address_significant_size * 8));
-    }
-  return retval;
+
+ return retval;
 }
 
 static char *
@@ -3779,6 +3674,13 @@ dwarf_attr (die, name)
   return NULL;
 }
 
+static int
+die_is_declaration (struct die_info *die)
+{
+  return (dwarf_attr (die, DW_AT_declaration)
+         && ! dwarf_attr (die, DW_AT_specification));
+}
+
 /* Decode the line number information for the compilation unit whose
    line number info is at OFFSET in the .debug_line section.
    The compilation directory of the file is passed in COMP_DIR.  */
@@ -3856,8 +3758,7 @@ dwarf_decode_lines (offset, comp_dir, abfd)
   line_ptr += 1;
   lh.standard_opcode_lengths = (unsigned char *)
     xmalloc (lh.opcode_base * sizeof (unsigned char));
-  back_to = make_cleanup ((make_cleanup_func) free_current_contents,
-                         &lh.standard_opcode_lengths);
+  back_to = make_cleanup (free_current_contents, &lh.standard_opcode_lengths);
 
   lh.standard_opcode_lengths[0] = 1;
   for (i = 1; i < lh.opcode_base; ++i)
@@ -3876,7 +3777,7 @@ dwarf_decode_lines (offset, comp_dir, abfd)
            xrealloc (dirs.dirs,
                      (dirs.num_dirs + DIR_ALLOC_CHUNK) * sizeof (char *));
          if (dirs.num_dirs == 0)
-           make_cleanup ((make_cleanup_func) free_current_contents, &dirs.dirs);
+           make_cleanup (free_current_contents, &dirs.dirs);
        }
       dirs.dirs[dirs.num_dirs++] = cur_dir;
     }
@@ -3893,8 +3794,7 @@ dwarf_decode_lines (offset, comp_dir, abfd)
                      (files.num_files + FILE_ALLOC_CHUNK)
                      * sizeof (struct fileinfo));
          if (files.num_files == 0)
-           make_cleanup ((make_cleanup_func) free_current_contents,
-                         &files.files);
+           make_cleanup (free_current_contents, &files.files);
        }
       files.files[files.num_files].name = cur_file;
       files.files[files.num_files].dir =
@@ -3969,8 +3869,7 @@ dwarf_decode_lines (offset, comp_dir, abfd)
                                  (files.num_files + FILE_ALLOC_CHUNK)
                                  * sizeof (struct fileinfo));
                      if (files.num_files == 0)
-                       make_cleanup ((make_cleanup_func) free_current_contents,
-                                     &files.files);
+                       make_cleanup (free_current_contents, &files.files);
                    }
                  files.files[files.num_files].name = cur_file;
                  files.files[files.num_files].dir =
@@ -4023,8 +3922,14 @@ dwarf_decode_lines (offset, comp_dir, abfd)
            case DW_LNS_set_basic_block:
              basic_block = 1;
              break;
+           /* Add to the address register of the state machine the
+              address increment value corresponding to special opcode
+              255.  Ie, this value is scaled by the minimum instruction
+              length since special opcode 255 would have scaled the
+              the increment.  */
            case DW_LNS_const_add_pc:
-             address += (255 - lh.opcode_base) / lh.line_range;
+             address += (lh.minimum_instruction_length
+                         * ((255 - lh.opcode_base) / lh.line_range));
              break;
            case DW_LNS_fixed_advance_pc:
              address += read_2_bytes (abfd, line_ptr);
@@ -4096,8 +4001,7 @@ dwarf2_start_subfile (filename, dirname)
    to make a symbol table entry for it, and if so, create a new entry
    and return a pointer to it.
    If TYPE is NULL, determine symbol type from the die, otherwise
-   used the passed type.
- */
+   used the passed type.  */
 
 static struct symbol *
 new_symbol (die, type, objfile)
@@ -4384,15 +4288,35 @@ dwarf2_const_value (attr, sym, objfile)
       memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
       break;
+
+      /* The DW_AT_const_value attributes are supposed to carry the
+        symbol's value "represented as it would be on the target
+        architecture."  By the time we get here, it's already been
+        converted to host endianness, so we just need to sign- or
+        zero-extend it as appropriate.  */
+    case DW_FORM_data1:
+      dwarf2_const_value_data (attr, sym, 8);
+      break;
     case DW_FORM_data2:
+      dwarf2_const_value_data (attr, sym, 16);
+      break;
     case DW_FORM_data4:
+      dwarf2_const_value_data (attr, sym, 32);
+      break;
     case DW_FORM_data8:
-    case DW_FORM_data1:
+      dwarf2_const_value_data (attr, sym, 64);
+      break;
+
     case DW_FORM_sdata:
+      SYMBOL_VALUE (sym) = DW_SND (attr);
+      SYMBOL_CLASS (sym) = LOC_CONST;
+      break;
+
     case DW_FORM_udata:
       SYMBOL_VALUE (sym) = DW_UNSND (attr);
       SYMBOL_CLASS (sym) = LOC_CONST;
       break;
+
     default:
       complain (&dwarf2_unsupported_const_value_attr,
                dwarf_form_name (attr->form));
@@ -4402,6 +4326,29 @@ dwarf2_const_value (attr, sym, objfile)
     }
 }
 
+
+/* Given an attr with a DW_FORM_dataN value in host byte order, sign-
+   or zero-extend it as appropriate for the symbol's type.  */
+static void
+dwarf2_const_value_data (struct attribute *attr,
+                        struct symbol *sym,
+                        int bits)
+{
+  LONGEST l = DW_UNSND (attr);
+
+  if (bits < sizeof (l) * 8)
+    {
+      if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
+       l &= ((LONGEST) 1 << bits) - 1;
+      else
+       l = (l << (sizeof (l) * 8 - bits)) >> (sizeof (l) * 8 - bits);
+    }
+
+  SYMBOL_VALUE (sym) = l;
+  SYMBOL_CLASS (sym) = LOC_CONST;
+}
+
+
 /* Return the type of the die in question using its DW_AT_type attribute.  */
 
 static struct type *
@@ -5780,6 +5727,14 @@ decode_locdesc (blk, objfile)
          i += bytes_read;
          break;
 
+       case DW_OP_bregx:
+         offreg = 1;
+         basereg = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
+         i += bytes_read;
+         stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
+         i += bytes_read;
+         break;
+
        case DW_OP_fbreg:
          stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
          i += bytes_read;