OSDN Git Service

*** empty log message ***
[pf3gnuchains/sourceware.git] / gdb / dwarf2read.c
index 0a8190d..5d498d5 100644 (file)
@@ -1,7 +1,7 @@
 /* DWARF 2 debugging format support for GDB.
 
    Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
-                 2004, 2005, 2006, 2007, 2008, 2009, 2010
+                 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
                  Free Software Foundation, Inc.
 
    Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
 #include "psympriv.h"
 #include "exceptions.h"
 #include "gdb_stat.h"
+#include "completer.h"
+#include "vec.h"
+#include "c-lang.h"
+#include "valprint.h"
 
 #include <fcntl.h>
 #include "gdb_string.h"
@@ -68,6 +72,9 @@
 #endif
 #endif
 
+typedef struct symbol *symbolp;
+DEF_VEC_P (symbolp);
+
 #if 0
 /* .debug_info header for a compilation unit
    Because of alignment constraints, this structure has padding and cannot
@@ -147,17 +154,14 @@ struct mapped_index
   const gdb_byte *address_table;
   /* Size of the address table data in bytes.  */
   offset_type address_table_size;
-  /* The hash table.  */
-  const offset_type *index_table;
+  /* The symbol table, implemented as a hash table.  */
+  const offset_type *symbol_table;
   /* Size in slots, each slot is 2 offset_types.  */
-  offset_type index_table_slots;
+  offset_type symbol_table_slots;
   /* A pointer to the constant pool.  */
   const char *constant_pool;
 };
 
-typedef struct dwarf2_per_cu_data *dwarf2_per_cu_data_ptr;
-DEF_VEC_P (dwarf2_per_cu_data_ptr);
-
 struct dwarf2_per_objfile
 {
   struct dwarf2_section_info info;
@@ -182,6 +186,12 @@ struct dwarf2_per_objfile
   /* The number of compilation units in ALL_COMP_UNITS.  */
   int n_comp_units;
 
+  /* The number of .debug_types-related CUs.  */
+  int n_type_comp_units;
+
+  /* The .debug_types-related CUs.  */
+  struct dwarf2_per_cu_data **type_comp_units;
+
   /* A chain of compilation units that are currently read in, so that
      they can be freed later.  */
   struct dwarf2_per_cu_data *read_in_chain;
@@ -194,11 +204,32 @@ struct dwarf2_per_objfile
      VMA of 0.  */
   int has_section_at_zero;
 
-  /* True if we are using the mapped index.  */
+  /* True if we are using the mapped index,
+     or we are faking it for OBJF_READNOW's sake.  */
   unsigned char using_index;
 
-  /* The mapped index.  */
+  /* The mapped index, or NULL if .gdb_index is missing or not being used.  */
   struct mapped_index *index_table;
+
+  /* When using index_table, this keeps track of all quick_file_names entries.
+     TUs can share line table entries with CUs or other TUs, and there can be
+     a lot more TUs than unique line tables, so we maintain a separate table
+     of all line table entries to support the sharing.  */
+  htab_t quick_file_names_table;
+
+  /* Set during partial symbol reading, to prevent queueing of full
+     symbols.  */
+  int reading_partial_symbols;
+
+  /* Table mapping type .debug_info DIE offsets to types.
+     This is NULL if not allocated yet.
+     It (currently) makes sense to allocate debug_types_type_hash lazily.
+     To keep things simple we allocate both lazily.  */
+  htab_t debug_info_type_hash;
+
+  /* Table mapping type .debug_types DIE offsets to types.
+     This is NULL if not allocated yet.  */
+  htab_t debug_types_type_hash;
 };
 
 static struct dwarf2_per_objfile *dwarf2_per_objfile;
@@ -222,7 +253,7 @@ static struct dwarf2_per_objfile *dwarf2_per_objfile;
 
 /* local data types */
 
-/* We hold several abbreviation tables in memory at the same time. */
+/* We hold several abbreviation tables in memory at the same time.  */
 #ifndef ABBREV_HASH_SIZE
 #define ABBREV_HASH_SIZE 121
 #endif
@@ -252,6 +283,29 @@ struct comp_unit_head
   unsigned int first_die_offset;
 };
 
+/* Type used for delaying computation of method physnames.
+   See comments for compute_delayed_physnames.  */
+struct delayed_method_info
+{
+  /* The type to which the method is attached, i.e., its parent class.  */
+  struct type *type;
+
+  /* The index of the method in the type's function fieldlists.  */
+  int fnfield_index;
+
+  /* The index of the method in the fieldlist.  */
+  int index;
+
+  /* The name of the DIE.  */
+  const char *name;
+
+  /*  The DIE associated with this method.  */
+  struct die_info *die;
+};
+
+typedef struct delayed_method_info delayed_method_info;
+DEF_VEC_O (delayed_method_info);
+
 /* Internal state when decoding a particular compilation unit.  */
 struct dwarf2_cu
 {
@@ -308,11 +362,6 @@ struct dwarf2_cu
   /* Backchain to our per_cu entry if the tree has been built.  */
   struct dwarf2_per_cu_data *per_cu;
 
-  /* Pointer to the die -> type map.  Although it is stored
-     permanently in per_cu, we copy it here to avoid double
-     indirection.  */
-  htab_t type_hash;
-
   /* How many compilation units ago was this CU last referenced?  */
   int last_used;
 
@@ -330,6 +379,10 @@ struct dwarf2_cu
   /* Header data from the line table, during full symbol processing.  */
   struct line_header *line_header;
 
+  /* A list of methods which need to have physnames computed
+     after all type information has been read.  */
+  VEC (delayed_method_info) *method_list;
+
   /* Mark used when releasing cached dies.  */
   unsigned int mark : 1;
 
@@ -344,32 +397,6 @@ struct dwarf2_cu
   unsigned int has_namespace_info : 1;
 };
 
-/* When using the index (and thus not using psymtabs), each CU has an
-   object of this type.  This is used to hold information needed by
-   the various "quick" methods.  */
-struct dwarf2_per_cu_quick_data
-{
-  /* The line table.  This can be NULL if there was no line table.  */
-  struct line_header *lines;
-
-  /* The file names from the line table.  */
-  const char **file_names;
-  /* The file names from the line table after being run through
-     gdb_realpath.  */
-  const char **full_names;
-
-  /* The corresponding symbol table.  This is NULL if symbols for this
-     CU have not yet been read.  */
-  struct symtab *symtab;
-
-  /* A temporary mark bit used when iterating over all CUs in
-     expand_symtabs_matching.  */
-  unsigned int mark : 1;
-
-  /* True if we've tried to read the line table.  */
-  unsigned int read_lines : 1;
-};
-
 /* Persistent data held for a compilation unit, even when not
    processing it.  We put a pointer to this structure in the
    read_symtab_private field of the psymtab.  If we encounter
@@ -404,12 +431,6 @@ struct dwarf2_per_cu_data
      of the CU cache it gets reset to NULL again.  */
   struct dwarf2_cu *cu;
 
-  /* If full symbols for this CU have been read in, then this field
-     holds a map of DIE offsets to types.  It isn't always possible
-     to reconstruct this information later, so we have to preserve
-     it.  */
-  htab_t type_hash;
-
   /* The corresponding objfile.  */
   struct objfile *objfile;
 
@@ -443,11 +464,11 @@ struct signatured_type
   struct dwarf2_per_cu_data per_cu;
 };
 
-/* Struct used to pass misc. parameters to read_die_and_children, et. al.
-   which are used for both .debug_info and .debug_types dies.
-   All parameters here are unchanging for the life of the call.
-   This struct exists to abstract away the constant parameters of
-   die reading.  */
+/* Struct used to pass misc. parameters to read_die_and_children, et
+   al.  which are used for both .debug_info and .debug_types dies.
+   All parameters here are unchanging for the life of the call.  This
+   struct exists to abstract away the constant parameters of die
+   reading.  */
 
 struct die_reader_specs
 {
@@ -510,7 +531,7 @@ struct line_header
 };
 
 /* When we construct a partial symbol table entry we only
-   need this much information. */
+   need this much information.  */
 struct partial_die_info
   {
     /* Offset of this DIE.  */
@@ -534,10 +555,19 @@ struct partial_die_info
     /* Flag set if the DIE has a byte_size attribute.  */
     unsigned int has_byte_size : 1;
 
+    /* Flag set if any of the DIE's children are template arguments.  */
+    unsigned int has_template_arguments : 1;
+
+    /* Flag set if fixup_partial_die has been called on this die.  */
+    unsigned int fixup_called : 1;
+
     /* The name of this DIE.  Normally the value of DW_AT_name, but
        sometimes a default name for unnamed DIEs.  */
     char *name;
 
+    /* The linkage name, if present.  */
+    const char *linkage_name;
+
     /* The scope to prepend to our children.  This is generally
        allocated on the comp_unit_obstack, so will disappear
        when this compilation unit leaves the cache.  */
@@ -552,6 +582,8 @@ struct partial_die_info
 
     /* Pointer into the info_buffer (or types_buffer) pointing at the target of
        DW_AT_sibling, if any.  */
+    /* NOTE: This member isn't strictly necessary, read_partial_die could
+       return DW_AT_sibling values to its caller load_partial_dies.  */
     gdb_byte *sibling;
 
     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
@@ -564,7 +596,7 @@ struct partial_die_info
     struct partial_die_info *die_parent, *die_child, *die_sibling;
   };
 
-/* This data structure holds the information of an abbrev. */
+/* This data structure holds the information of an abbrev.  */
 struct abbrev_info
   {
     unsigned int number;       /* number identifying abbrev */
@@ -581,7 +613,7 @@ struct attr_abbrev
     ENUM_BITFIELD(dwarf_form) form : 16;
   };
 
-/* Attributes have a name and a value */
+/* Attributes have a name and a value */
 struct attribute
   {
     ENUM_BITFIELD(dwarf_attribute) name : 16;
@@ -604,14 +636,18 @@ struct attribute
     u;
   };
 
-/* This data structure holds a complete die structure. */
+/* This data structure holds a complete die structure.  */
 struct die_info
   {
     /* DWARF-2 tag for this DIE.  */
     ENUM_BITFIELD(dwarf_tag) tag : 16;
 
     /* Number of attributes */
-    unsigned short num_attrs;
+    unsigned char num_attrs;
+
+    /* True if we're presently building the full type name for the
+       type derived from this DIE.  */
+    unsigned char building_fullname : 1;
 
     /* Abbrev number */
     unsigned int abbrev;
@@ -622,8 +658,7 @@ struct die_info
     /* The dies in a compilation unit form an n-ary tree.  PARENT
        points to this die's parent; CHILD points to the first child of
        this node; and all the children of a given node are chained
-       together via their SIBLING fields, terminated by a die whose
-       tag is zero.  */
+       together via their SIBLING fields.  */
     struct die_info *child;    /* Its first child, if any.  */
     struct die_info *sibling;  /* Its next sibling, if any.  */
     struct die_info *parent;   /* Its parent, if any.  */
@@ -642,7 +677,7 @@ struct function_range
   struct function_range *next;
 };
 
-/* Get at parts of an attribute structure */
+/* Get at parts of an attribute structure */
 
 #define DW_STRING(attr)    ((attr)->u.str)
 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
@@ -652,7 +687,7 @@ struct function_range
 #define DW_ADDR(attr)     ((attr)->u.addr)
 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
 
-/* Blocks are a bunch of untyped bytes. */
+/* Blocks are a bunch of untyped bytes.  */
 struct dwarf_block
   {
     unsigned int size;
@@ -678,7 +713,7 @@ static int bits_per_byte = 8;
    in an instance of a field_info structure, as defined below.  */
 struct field_info
   {
-    /* List of data member and baseclasses fields. */
+    /* List of data member and baseclasses fields.  */
     struct nextfield
       {
        struct nextfield *next;
@@ -752,13 +787,13 @@ static void
 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
                           struct cmd_list_element *c, const char *value)
 {
-  fprintf_filtered (file, _("\
-The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
+  fprintf_filtered (file, _("The upper bound on the age of cached "
+                           "dwarf2 compilation units is %s.\n"),
                    value);
 }
 
 
-/* Various complaints about symbol reading that don't abort the process */
+/* Various complaints about symbol reading that don't abort the process */
 
 static void
 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
@@ -778,7 +813,8 @@ static void
 dwarf2_debug_line_missing_end_sequence_complaint (void)
 {
   complaint (&symfile_complaints,
-            _(".debug_line section has line program sequence without an end"));
+            _(".debug_line section has line "
+              "program sequence without an end"));
 }
 
 static void
@@ -792,8 +828,8 @@ dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
                                              int arg3)
 {
   complaint (&symfile_complaints,
-            _("const value length mismatch for '%s', got %d, expected %d"), arg1,
-            arg2, arg3);
+            _("const value length mismatch for '%s', got %d, expected %d"),
+            arg1, arg2, arg3);
 }
 
 static void
@@ -807,7 +843,8 @@ static void
 dwarf2_macro_malformed_definition_complaint (const char *arg1)
 {
   complaint (&symfile_complaints,
-            _("macro debug info contains a malformed macro definition:\n`%s'"),
+            _("macro debug info contains a "
+              "malformed macro definition:\n`%s'"),
             arg1);
 }
 
@@ -815,7 +852,8 @@ static void
 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
 {
   complaint (&symfile_complaints,
-            _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
+            _("invalid attribute class or form for '%s' in '%s'"),
+            arg1, arg2);
 }
 
 /* local function prototypes */
@@ -825,10 +863,6 @@ static void dwarf2_locate_sections (bfd *, asection *, void *);
 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
                                            struct objfile *);
 
-static void dwarf2_build_include_psymtabs (struct dwarf2_cu *,
-                                           struct die_info *,
-                                           struct partial_symtab *);
-
 static void dwarf2_build_psymtabs_hard (struct objfile *);
 
 static void scan_partial_symbols (struct partial_die_info *,
@@ -919,7 +953,7 @@ static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
 
 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
 
-static char *read_string (bfd *, gdb_byte *, unsigned int *);
+static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
 
 static char *read_indirect_string (bfd *, gdb_byte *,
                                    const struct comp_unit_head *,
@@ -957,26 +991,34 @@ static struct line_header *(dwarf_decode_line_header
                             (unsigned int offset,
                              bfd *abfd, struct dwarf2_cu *cu));
 
-static void dwarf_decode_lines (struct line_header *, char *, bfd *,
+static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
                                struct dwarf2_cu *, struct partial_symtab *);
 
-static void dwarf2_start_subfile (char *, char *, char *);
+static void dwarf2_start_subfile (char *, const char *, const char *);
 
 static struct symbol *new_symbol (struct die_info *, struct type *,
                                  struct dwarf2_cu *);
 
+static struct symbol *new_symbol_full (struct die_info *, struct type *,
+                                      struct dwarf2_cu *, struct symbol *);
+
 static void dwarf2_const_value (struct attribute *, struct symbol *,
                                struct dwarf2_cu *);
 
-static void dwarf2_const_value_data (struct attribute *attr,
-                                    struct symbol *sym,
-                                    int bits);
+static void dwarf2_const_value_attr (struct attribute *attr,
+                                    struct type *type,
+                                    const char *name,
+                                    struct obstack *obstack,
+                                    struct dwarf2_cu *cu, long *value,
+                                    gdb_byte **bytes,
+                                    struct dwarf2_locexpr_baton **baton);
 
 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
 
 static int need_gnat_info (struct dwarf2_cu *);
 
-static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
+static struct type *die_descriptive_type (struct die_info *,
+                                         struct dwarf2_cu *);
 
 static void set_descriptive_type (struct type *, struct die_info *,
                                  struct dwarf2_cu *);
@@ -984,10 +1026,13 @@ static void set_descriptive_type (struct type *, struct die_info *,
 static struct type *die_containing_type (struct die_info *,
                                         struct dwarf2_cu *);
 
-static struct type *tag_type_to_type (struct die_info *, struct dwarf2_cu *);
+static struct type *lookup_die_type (struct die_info *, struct attribute *,
+                                    struct dwarf2_cu *);
 
 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
 
+static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
+
 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
 
 static char *typename_concat (struct obstack *obs, const char *prefix,
@@ -1027,7 +1072,8 @@ static void dwarf2_add_member_fn (struct field_info *,
                                  struct dwarf2_cu *);
 
 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
-                                            struct type *, struct dwarf2_cu *);
+                                            struct type *,
+                                            struct dwarf2_cu *);
 
 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
 
@@ -1080,6 +1126,10 @@ static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
 
 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
 
+static const char *dwarf2_full_name (char *name,
+                                    struct die_info *die,
+                                    struct dwarf2_cu *cu);
+
 static struct die_info *dwarf2_extension (struct die_info *die,
                                          struct dwarf2_cu **);
 
@@ -1157,6 +1207,10 @@ static int attr_form_is_section_offset (struct attribute *);
 
 static int attr_form_is_constant (struct attribute *);
 
+static void fill_in_loclist_baton (struct dwarf2_cu *cu,
+                                  struct dwarf2_loclist_baton *baton,
+                                  struct attribute *attr);
+
 static void dwarf2_symbol_mark_computed (struct attribute *attr,
                                         struct symbol *sym,
                                         struct dwarf2_cu *cu);
@@ -1177,7 +1231,11 @@ static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
   (unsigned int offset, struct objfile *objfile);
 
-static struct dwarf2_cu *alloc_one_comp_unit (struct objfile *objfile);
+static void init_one_comp_unit (struct dwarf2_cu *cu,
+                               struct objfile *objfile);
+
+static void prepare_one_comp_unit (struct dwarf2_cu *cu,
+                                  struct die_info *comp_unit_die);
 
 static void free_one_comp_unit (void *);
 
@@ -1192,6 +1250,8 @@ static struct type *set_die_type (struct die_info *, struct type *,
 
 static void create_all_comp_units (struct objfile *);
 
+static int create_debug_types_hash_table (struct objfile *objfile);
+
 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
                                 struct objfile *);
 
@@ -1204,6 +1264,9 @@ static void dwarf2_mark (struct dwarf2_cu *);
 
 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
 
+static struct type *get_die_type_at_offset (unsigned int,
+                                           struct dwarf2_per_cu_data *per_cu);
+
 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
 
 static void dwarf2_release_queue (void *dummy);
@@ -1229,6 +1292,8 @@ static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
 static void init_cu_die_reader (struct die_reader_specs *reader,
                                struct dwarf2_cu *cu);
 
+static htab_t allocate_signatured_type_table (struct objfile *objfile);
+
 #if WORDS_BIGENDIAN
 
 /* Convert VALUE between big- and little-endian.  */
@@ -1253,6 +1318,9 @@ byte_swap (offset_type value)
 /* The suffix for an index file.  */
 #define INDEX_SUFFIX ".gdb-index"
 
+static const char *dwarf2_physname (char *name, struct die_info *die,
+                                   struct dwarf2_cu *cu);
+
 /* Try to locate the sections we need for DWARF 2 debugging
    information and return true if we have enough to do something.  */
 
@@ -1385,7 +1453,8 @@ zlib_decompress_section (struct objfile *objfile, asection *sectp,
   int header_size = 12;
 
   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
-      || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
+      || bfd_bread (compressed_buffer,
+                   compressed_size, abfd) != compressed_size)
     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
            bfd_get_filename (abfd));
 
@@ -1440,6 +1509,14 @@ zlib_decompress_section (struct objfile *objfile, asection *sectp,
 #endif
 }
 
+/* A helper function that decides whether a section is empty.  */
+
+static int
+dwarf2_section_empty_p (struct dwarf2_section_info *info)
+{
+  return info->asection == NULL || info->size == 0;
+}
+
 /* Read the contents of the section SECTP from object file specified by
    OBJFILE, store info about the section into INFO.
    If the section is compressed, uncompress it before returning.  */
@@ -1458,7 +1535,7 @@ dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
   info->was_mmapped = 0;
   info->readin = 1;
 
-  if (info->asection == NULL || info->size == 0)
+  if (dwarf2_section_empty_p (info))
     return;
 
   /* Check if the file has a 4-byte header indicating compression.  */
@@ -1523,8 +1600,24 @@ dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
           bfd_get_filename (abfd));
 }
 
+/* A helper function that returns the size of a section in a safe way.
+   If you are positive that the section has been read before using the
+   size, then it is safe to refer to the dwarf2_section_info object's
+   "size" field directly.  In other cases, you must call this
+   function, because for compressed sections the size field is not set
+   correctly until the section has been read.  */
+
+static bfd_size_type
+dwarf2_section_size (struct objfile *objfile,
+                    struct dwarf2_section_info *info)
+{
+  if (!info->readin)
+    dwarf2_read_section (objfile, info);
+  return info->size;
+}
+
 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
-   SECTION_NAME. */
+   SECTION_NAME.  */
 
 void
 dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
@@ -1549,11 +1642,9 @@ dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
   else if (section_is_p (section_name, FRAME_SECTION))
     info = &data->frame;
   else
-    gdb_assert (0);
+    gdb_assert_not_reached ("unexpected section");
 
-  if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
-    /* We haven't read this section in yet.  Do it now.  */
-    dwarf2_read_section (objfile, info);
+  dwarf2_read_section (objfile, info);
 
   *sectp = info->asection;
   *bufp = info->buffer;
@@ -1561,9 +1652,106 @@ dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
 }
 
 \f
+/* DWARF quick_symbols_functions support.  */
+
+/* TUs can share .debug_line entries, and there can be a lot more TUs than
+   unique line tables, so we maintain a separate table of all .debug_line
+   derived entries to support the sharing.
+   All the quick functions need is the list of file names.  We discard the
+   line_header when we're done and don't need to record it here.  */
+struct quick_file_names
+{
+  /* The offset in .debug_line of the line table.  We hash on this.  */
+  unsigned int offset;
+
+  /* The number of entries in file_names, real_names.  */
+  unsigned int num_file_names;
+
+  /* The file names from the line table, after being run through
+     file_full_name.  */
+  const char **file_names;
+
+  /* The file names from the line table after being run through
+     gdb_realpath.  These are computed lazily.  */
+  const char **real_names;
+};
+
+/* When using the index (and thus not using psymtabs), each CU has an
+   object of this type.  This is used to hold information needed by
+   the various "quick" methods.  */
+struct dwarf2_per_cu_quick_data
+{
+  /* The file table.  This can be NULL if there was no file table
+     or it's currently not read in.
+     NOTE: This points into dwarf2_per_objfile->quick_file_names_table.  */
+  struct quick_file_names *file_names;
+
+  /* The corresponding symbol table.  This is NULL if symbols for this
+     CU have not yet been read.  */
+  struct symtab *symtab;
+
+  /* A temporary mark bit used when iterating over all CUs in
+     expand_symtabs_matching.  */
+  unsigned int mark : 1;
+
+  /* True if we've tried to read the file table and found there isn't one.
+     There will be no point in trying to read it again next time.  */
+  unsigned int no_file_data : 1;
+};
+
+/* Hash function for a quick_file_names.  */
+
+static hashval_t
+hash_file_name_entry (const void *e)
+{
+  const struct quick_file_names *file_data = e;
+
+  return file_data->offset;
+}
+
+/* Equality function for a quick_file_names.  */
+
+static int
+eq_file_name_entry (const void *a, const void *b)
+{
+  const struct quick_file_names *ea = a;
+  const struct quick_file_names *eb = b;
+
+  return ea->offset == eb->offset;
+}
+
+/* Delete function for a quick_file_names.  */
+
+static void
+delete_file_name_entry (void *e)
+{
+  struct quick_file_names *file_data = e;
+  int i;
+
+  for (i = 0; i < file_data->num_file_names; ++i)
+    {
+      xfree ((void*) file_data->file_names[i]);
+      if (file_data->real_names)
+       xfree ((void*) file_data->real_names[i]);
+    }
+
+  /* The space for the struct itself lives on objfile_obstack,
+     so we don't free it here.  */
+}
+
+/* Create a quick_file_names hash table.  */
+
+static htab_t
+create_quick_file_names_table (unsigned int nr_initial_entries)
+{
+  return htab_create_alloc (nr_initial_entries,
+                           hash_file_name_entry, eq_file_name_entry,
+                           delete_file_name_entry, xcalloc, xfree);
+}
 
 /* Read in the symbols for PER_CU.  OBJFILE is the objfile from which
    this CU came.  */
+
 static void
 dw2_do_instantiate_symtab (struct objfile *objfile,
                           struct dwarf2_per_cu_data *per_cu)
@@ -1591,6 +1779,7 @@ dw2_do_instantiate_symtab (struct objfile *objfile,
 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
    the objfile from which this CU came.  Returns the resulting symbol
    table.  */
+
 static struct symtab *
 dw2_instantiate_symtab (struct objfile *objfile,
                        struct dwarf2_per_cu_data *per_cu)
@@ -1605,9 +1794,23 @@ dw2_instantiate_symtab (struct objfile *objfile,
   return per_cu->v.quick->symtab;
 }
 
+/* Return the CU given its index.  */
+
+static struct dwarf2_per_cu_data *
+dw2_get_cu (int index)
+{
+  if (index >= dwarf2_per_objfile->n_comp_units)
+    {
+      index -= dwarf2_per_objfile->n_comp_units;
+      return dwarf2_per_objfile->type_comp_units[index];
+    }
+  return dwarf2_per_objfile->all_comp_units[index];
+}
+
 /* A helper function that knows how to read a 64-bit value in a way
    that doesn't make gdb die.  Returns 1 if the conversion went ok, 0
    otherwise.  */
+
 static int
 extract_cu_value (const char *bytes, ULONGEST *result)
 {
@@ -1630,12 +1833,12 @@ extract_cu_value (const char *bytes, ULONGEST *result)
 /* Read the CU list from the mapped index, and use it to create all
    the CU objects for this objfile.  Return 0 if something went wrong,
    1 if everything went ok.  */
+
 static int
-create_cus_from_index (struct objfile *objfile, struct mapped_index *index,
-                      const gdb_byte *cu_list, offset_type cu_list_elements)
+create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
+                      offset_type cu_list_elements)
 {
   offset_type i;
-  const char *entry;
 
   dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
   dwarf2_per_objfile->all_comp_units
@@ -1666,8 +1869,62 @@ create_cus_from_index (struct objfile *objfile, struct mapped_index *index,
   return 1;
 }
 
+/* Create the signatured type hash table from the index.  */
+
+static int
+create_signatured_type_table_from_index (struct objfile *objfile,
+                                        const gdb_byte *bytes,
+                                        offset_type elements)
+{
+  offset_type i;
+  htab_t sig_types_hash;
+
+  dwarf2_per_objfile->n_type_comp_units = elements / 3;
+  dwarf2_per_objfile->type_comp_units
+    = obstack_alloc (&objfile->objfile_obstack,
+                    dwarf2_per_objfile->n_type_comp_units
+                    * sizeof (struct dwarf2_per_cu_data *));
+
+  sig_types_hash = allocate_signatured_type_table (objfile);
+
+  for (i = 0; i < elements; i += 3)
+    {
+      struct signatured_type *type_sig;
+      ULONGEST offset, type_offset, signature;
+      void **slot;
+
+      if (!extract_cu_value (bytes, &offset)
+         || !extract_cu_value (bytes + 8, &type_offset))
+       return 0;
+      signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
+      bytes += 3 * 8;
+
+      type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
+                                struct signatured_type);
+      type_sig->signature = signature;
+      type_sig->offset = offset;
+      type_sig->type_offset = type_offset;
+      type_sig->per_cu.from_debug_types = 1;
+      type_sig->per_cu.offset = offset;
+      type_sig->per_cu.objfile = objfile;
+      type_sig->per_cu.v.quick
+       = OBSTACK_ZALLOC (&objfile->objfile_obstack,
+                         struct dwarf2_per_cu_quick_data);
+
+      slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
+      *slot = type_sig;
+
+      dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
+    }
+
+  dwarf2_per_objfile->signatured_types = sig_types_hash;
+
+  return 1;
+}
+
 /* Read the address map data from the mapped index, and use it to
    populate the objfile's psymtabs_addrmap.  */
+
 static void
 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
 {
@@ -1697,7 +1954,7 @@ create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
       iter += 4;
       
       addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
-                        dwarf2_per_objfile->all_comp_units[cu_index]);
+                        dw2_get_cu (cu_index));
     }
 
   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
@@ -1710,6 +1967,7 @@ create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
    maintain control over the implementation.  This is necessary
    because the hash function is tied to the format of the mapped index
    file.  */
+
 static hashval_t
 mapped_index_string_hash (const void *p)
 {
@@ -1726,6 +1984,7 @@ mapped_index_string_hash (const void *p)
 /* Find a slot in the mapped index INDEX for the object named NAME.
    If NAME is found, set *VEC_OUT to point to the CU vector in the
    constant pool and return 1.  If NAME cannot be found, return 0.  */
+
 static int
 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
                          offset_type **vec_out)
@@ -1733,82 +1992,120 @@ find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
   offset_type hash = mapped_index_string_hash (name);
   offset_type slot, step;
 
-  slot = hash & (index->index_table_slots - 1);
-  step = ((hash * 17) & (index->index_table_slots - 1)) | 1;
+  slot = hash & (index->symbol_table_slots - 1);
+  step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
 
   for (;;)
     {
       /* Convert a slot number to an offset into the table.  */
       offset_type i = 2 * slot;
       const char *str;
-      if (index->index_table[i] == 0 && index->index_table[i + 1] == 0)
+      if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
        return 0;
 
-      str = index->constant_pool + MAYBE_SWAP (index->index_table[i]);
+      str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
       if (!strcmp (name, str))
        {
          *vec_out = (offset_type *) (index->constant_pool
-                                     + MAYBE_SWAP (index->index_table[i + 1]));
+                                     + MAYBE_SWAP (index->symbol_table[i + 1]));
          return 1;
        }
 
-      slot = (slot + step) & (index->index_table_slots - 1);
+      slot = (slot + step) & (index->symbol_table_slots - 1);
     }
 }
 
 /* Read the index file.  If everything went ok, initialize the "quick"
    elements of all the CUs and return 1.  Otherwise, return 0.  */
+
 static int
 dwarf2_read_index (struct objfile *objfile)
 {
-  struct stat st, obstat;
-  int fd;
   char *addr;
   struct mapped_index *map;
-  offset_type val, *metadata;
-  char buf1[8], buf2[8];
+  offset_type *metadata;
   const gdb_byte *cu_list;
-  offset_type cu_list_elements;
+  const gdb_byte *types_list = NULL;
+  offset_type version, cu_list_elements;
+  offset_type types_list_elements = 0;
+  int i;
+
+  if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
+    return 0;
 
-  if (dwarf2_per_objfile->gdb_index.asection == NULL
-      || dwarf2_per_objfile->gdb_index.size == 0)
+  /* Older elfutils strip versions could keep the section in the main
+     executable while splitting it for the separate debug info file.  */
+  if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
+       & SEC_HAS_CONTENTS) == 0)
     return 0;
+
   dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
 
   addr = dwarf2_per_objfile->gdb_index.buffer;
   /* Version check.  */
-  if (MAYBE_SWAP (*(offset_type *) addr) != 1)
+  version = MAYBE_SWAP (*(offset_type *) addr);
+  /* Versions earlier than 3 emitted every copy of a psymbol.  This
+     causes the index to behave very poorly for certain requests.  Version 4
+     contained incomplete addrmap.  So, it seems better to just ignore such
+     indices.  */
+  if (version < 4)
+    return 0;
+  /* Indexes with higher version than the one supported by GDB may be no
+     longer backward compatible.  */
+  if (version > 4)
     return 0;
 
   map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
-  map->total_size = st.st_size;
+  map->total_size = dwarf2_per_objfile->gdb_index.size;
 
   metadata = (offset_type *) (addr + sizeof (offset_type));
-  cu_list = addr + MAYBE_SWAP (metadata[0]);
-  cu_list_elements = ((MAYBE_SWAP (metadata[1]) - MAYBE_SWAP (metadata[0]))
+
+  i = 0;
+  cu_list = addr + MAYBE_SWAP (metadata[i]);
+  cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
                      / 8);
-  map->address_table = addr + MAYBE_SWAP (metadata[1]);
-  map->address_table_size = (MAYBE_SWAP (metadata[2])
-                            - MAYBE_SWAP (metadata[1]));
-  map->index_table = (offset_type *) (addr + MAYBE_SWAP (metadata[2]));
-  map->index_table_slots = ((MAYBE_SWAP (metadata[3])
-                            - MAYBE_SWAP (metadata[2]))
-                           / (2 * sizeof (offset_type)));
-  map->constant_pool = addr + MAYBE_SWAP (metadata[3]);
-
-  if (!create_cus_from_index (objfile, map, cu_list, cu_list_elements))
+  ++i;
+
+  types_list = addr + MAYBE_SWAP (metadata[i]);
+  types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
+                         - MAYBE_SWAP (metadata[i]))
+                        / 8);
+  ++i;
+
+  map->address_table = addr + MAYBE_SWAP (metadata[i]);
+  map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
+                            - MAYBE_SWAP (metadata[i]));
+  ++i;
+
+  map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
+  map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
+                             - MAYBE_SWAP (metadata[i]))
+                            / (2 * sizeof (offset_type)));
+  ++i;
+
+  map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
+
+  if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
+    return 0;
+
+  if (types_list_elements
+      && !create_signatured_type_table_from_index (objfile, types_list,
+                                                  types_list_elements))
     return 0;
 
   create_addrmap_from_index (objfile, map);
 
   dwarf2_per_objfile->index_table = map;
   dwarf2_per_objfile->using_index = 1;
+  dwarf2_per_objfile->quick_file_names_table =
+    create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
 
   return 1;
 }
 
 /* A helper for the "quick" functions which sets the global
    dwarf2_per_objfile according to OBJFILE.  */
+
 static void
 dw2_setup (struct objfile *objfile)
 {
@@ -1818,35 +2115,43 @@ dw2_setup (struct objfile *objfile)
 
 /* A helper for the "quick" functions which attempts to read the line
    table for THIS_CU.  */
-static void
-dw2_require_line_header (struct objfile *objfile,
-                        struct dwarf2_per_cu_data *this_cu)
+
+static struct quick_file_names *
+dw2_get_file_names (struct objfile *objfile,
+                   struct dwarf2_per_cu_data *this_cu)
 {
   bfd *abfd = objfile->obfd;
-  struct line_header *lh = NULL;
+  struct line_header *lh;
   struct attribute *attr;
   struct cleanup *cleanups;
   struct die_info *comp_unit_die;
+  struct dwarf2_section_info* sec;
   gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
   int has_children, i;
   struct dwarf2_cu cu;
   unsigned int bytes_read, buffer_size;
   struct die_reader_specs reader_specs;
   char *name, *comp_dir;
+  void **slot;
+  struct quick_file_names *qfn;
+  unsigned int line_offset;
 
-  if (this_cu->v.quick->read_lines)
-    return;
-  this_cu->v.quick->read_lines = 1;
-
-  memset (&cu, 0, sizeof (cu));
-  cu.objfile = objfile;
-  obstack_init (&cu.comp_unit_obstack);
+  if (this_cu->v.quick->file_names != NULL)
+    return this_cu->v.quick->file_names;
+  /* If we know there is no line data, no point in looking again.  */
+  if (this_cu->v.quick->no_file_data)
+    return NULL;
 
+  init_one_comp_unit (&cu, objfile);
   cleanups = make_cleanup (free_stack_comp_unit, &cu);
 
-  dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
-  buffer_size = dwarf2_per_objfile->info.size;
-  buffer = dwarf2_per_objfile->info.buffer;
+  if (this_cu->from_debug_types)
+    sec = &dwarf2_per_objfile->types;
+  else
+    sec = &dwarf2_per_objfile->info;
+  dwarf2_read_section (objfile, sec);
+  buffer_size = sec->size;
+  buffer = sec->buffer;
   info_ptr = buffer + this_cu->offset;
   beg_of_comp_unit = info_ptr;
 
@@ -1867,84 +2172,116 @@ dw2_require_line_header (struct objfile *objfile,
   if (this_cu->from_debug_types)
     info_ptr += 8 /*signature*/ + cu.header.offset_size;
   init_cu_die_reader (&reader_specs, &cu);
-  info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
-                           &has_children);
+  read_full_die (&reader_specs, &comp_unit_die, info_ptr,
+                &has_children);
 
+  lh = NULL;
+  slot = NULL;
+  line_offset = 0;
   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
   if (attr)
     {
-      unsigned int line_offset = DW_UNSND (attr);
+      struct quick_file_names find_entry;
+
+      line_offset = DW_UNSND (attr);
+
+      /* We may have already read in this line header (TU line header sharing).
+        If we have we're done.  */
+      find_entry.offset = line_offset;
+      slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
+                            &find_entry, INSERT);
+      if (*slot != NULL)
+       {
+         do_cleanups (cleanups);
+         this_cu->v.quick->file_names = *slot;
+         return *slot;
+       }
+
       lh = dwarf_decode_line_header (line_offset, abfd, &cu);
     }
   if (lh == NULL)
     {
       do_cleanups (cleanups);
-      return;
+      this_cu->v.quick->no_file_data = 1;
+      return NULL;
     }
 
-  find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
+  qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
+  qfn->offset = line_offset;
+  gdb_assert (slot != NULL);
+  *slot = qfn;
 
-  this_cu->v.quick->lines = lh;
+  find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
 
-  this_cu->v.quick->file_names
-    = obstack_alloc (&objfile->objfile_obstack,
-                    lh->num_file_names * sizeof (char *));
+  qfn->num_file_names = lh->num_file_names;
+  qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
+                                  lh->num_file_names * sizeof (char *));
   for (i = 0; i < lh->num_file_names; ++i)
-    this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
+    qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
+  qfn->real_names = NULL;
 
+  free_line_header (lh);
   do_cleanups (cleanups);
+
+  this_cu->v.quick->file_names = qfn;
+  return qfn;
 }
 
 /* A helper for the "quick" functions which computes and caches the
-   real path for a given file name from the line table.
-   dw2_require_line_header must have been called before this is
-   invoked.  */
+   real path for a given file name from the line table.  */
+
 static const char *
-dw2_require_full_path (struct objfile *objfile,
-                      struct dwarf2_per_cu_data *cu,
-                      int index)
+dw2_get_real_path (struct objfile *objfile,
+                  struct quick_file_names *qfn, int index)
 {
-  if (!cu->v.quick->full_names)
-    cu->v.quick->full_names
-      = OBSTACK_CALLOC (&objfile->objfile_obstack,
-                       cu->v.quick->lines->num_file_names,
-                       sizeof (char *));
+  if (qfn->real_names == NULL)
+    qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
+                                     qfn->num_file_names, sizeof (char *));
 
-  if (!cu->v.quick->full_names[index])
-    cu->v.quick->full_names[index]
-      = gdb_realpath (cu->v.quick->file_names[index]);
+  if (qfn->real_names[index] == NULL)
+    qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
 
-  return cu->v.quick->full_names[index];
+  return qfn->real_names[index];
 }
 
 static struct symtab *
 dw2_find_last_source_symtab (struct objfile *objfile)
 {
   int index;
+
   dw2_setup (objfile);
   index = dwarf2_per_objfile->n_comp_units - 1;
-  return dw2_instantiate_symtab (objfile,
-                                dwarf2_per_objfile->all_comp_units[index]);
+  return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
 }
 
-static void
-dw2_forget_cached_source_info (struct objfile *objfile)
+/* Traversal function for dw2_forget_cached_source_info.  */
+
+static int
+dw2_free_cached_file_names (void **slot, void *info)
 {
-  int i;
+  struct quick_file_names *file_data = (struct quick_file_names *) *slot;
 
-  dw2_setup (objfile);
-  for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
+  if (file_data->real_names)
     {
-      struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
+      int i;
 
-      if (cu->v.quick->full_names)
+      for (i = 0; i < file_data->num_file_names; ++i)
        {
-         int j;
-
-         for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
-           xfree ((void *) cu->v.quick->full_names[j]);
+         xfree ((void*) file_data->real_names[i]);
+         file_data->real_names[i] = NULL;
        }
     }
+
+  return 1;
+}
+
+static void
+dw2_forget_cached_source_info (struct objfile *objfile)
+{
+  dw2_setup (objfile);
+
+  htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
+                         dw2_free_cached_file_names, NULL);
 }
 
 static int
@@ -1957,60 +2294,58 @@ dw2_lookup_symtab (struct objfile *objfile, const char *name,
   struct dwarf2_per_cu_data *base_cu = NULL;
 
   dw2_setup (objfile);
-  for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
+
+  for (i = 0; i < (dwarf2_per_objfile->n_comp_units
+                  + dwarf2_per_objfile->n_type_comp_units); ++i)
     {
       int j;
-      struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
+      struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
+      struct quick_file_names *file_data;
 
-      if (cu->v.quick->symtab)
+      if (per_cu->v.quick->symtab)
        continue;
 
-      dw2_require_line_header (objfile, cu);
-      if (!cu->v.quick->lines)
+      file_data = dw2_get_file_names (objfile, per_cu);
+      if (file_data == NULL)
        continue;
 
-      for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
+      for (j = 0; j < file_data->num_file_names; ++j)
        {
-         const char *this_name = cu->v.quick->file_names[j];
+         const char *this_name = file_data->file_names[j];
 
          if (FILENAME_CMP (name, this_name) == 0)
            {
-             *result = dw2_instantiate_symtab (objfile, cu);
+             *result = dw2_instantiate_symtab (objfile, per_cu);
              return 1;
            }
 
          if (check_basename && ! base_cu
              && FILENAME_CMP (lbasename (this_name), name) == 0)
-           base_cu = cu;
+           base_cu = per_cu;
 
          if (full_path != NULL)
            {
-             const char *this_full_name = dw2_require_full_path (objfile,
-                                                                 cu, j);
+             const char *this_real_name = dw2_get_real_path (objfile,
+                                                             file_data, j);
 
-             if (this_full_name
-                 && FILENAME_CMP (full_path, this_full_name) == 0)
+             if (this_real_name != NULL
+                 && FILENAME_CMP (full_path, this_real_name) == 0)
                {
-                 *result = dw2_instantiate_symtab (objfile, cu);
+                 *result = dw2_instantiate_symtab (objfile, per_cu);
                  return 1;
                }
            }
 
          if (real_path != NULL)
            {
-             const char *this_full_name = dw2_require_full_path (objfile,
-                                                                 cu, j);
+             const char *this_real_name = dw2_get_real_path (objfile,
+                                                             file_data, j);
 
-             if (this_full_name != NULL)
+             if (this_real_name != NULL
+                 && FILENAME_CMP (real_path, this_real_name) == 0)
                {
-                 char *rp = gdb_realpath (this_full_name);
-                 if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
-                   {
-                     xfree (rp);
-                     *result = dw2_instantiate_symtab (objfile, cu);
-                     return 1;
-                   }
-                 xfree (rp);
+                 *result = dw2_instantiate_symtab (objfile, per_cu);
+                 return 1;
                }
            }
        }
@@ -2036,11 +2371,13 @@ dw2_lookup_symbol (struct objfile *objfile, int block_index,
 
 /* A helper function that expands all symtabs that hold an object
    named NAME.  */
+
 static void
 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
 {
   dw2_setup (objfile);
 
+  /* index_table is NULL if OBJF_READNOW.  */
   if (dwarf2_per_objfile->index_table)
     {
       offset_type *vec;
@@ -2052,9 +2389,9 @@ dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
          for (i = 0; i < len; ++i)
            {
              offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
-             struct dwarf2_per_cu_data *cu;
-             cu = dwarf2_per_objfile->all_comp_units[cu_index];
-             dw2_instantiate_symtab (objfile, cu);
+             struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
+
+             dw2_instantiate_symtab (objfile, per_cu);
            }
        }
     }
@@ -2075,11 +2412,12 @@ dw2_print_stats (struct objfile *objfile)
 
   dw2_setup (objfile);
   count = 0;
-  for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
+  for (i = 0; i < (dwarf2_per_objfile->n_comp_units
+                  + dwarf2_per_objfile->n_type_comp_units); ++i)
     {
-      struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
+      struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
 
-      if (!cu->v.quick->symtab)
+      if (!per_cu->v.quick->symtab)
        ++count;
     }
   printf_filtered (_("  Number of unread CUs: %d\n"), count);
@@ -2111,11 +2449,13 @@ dw2_expand_all_symtabs (struct objfile *objfile)
   int i;
 
   dw2_setup (objfile);
-  for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
+
+  for (i = 0; i < (dwarf2_per_objfile->n_comp_units
+                  + dwarf2_per_objfile->n_type_comp_units); ++i)
     {
-      struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
+      struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
 
-      dw2_instantiate_symtab (objfile, cu);
+      dw2_instantiate_symtab (objfile, per_cu);
     }
 }
 
@@ -2126,24 +2466,31 @@ dw2_expand_symtabs_with_filename (struct objfile *objfile,
   int i;
 
   dw2_setup (objfile);
+
+  /* We don't need to consider type units here.
+     This is only called for examining code, e.g. expand_line_sal.
+     There can be an order of magnitude (or more) more type units
+     than comp units, and we avoid them if we can.  */
+
   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
     {
       int j;
-      struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
+      struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
+      struct quick_file_names *file_data;
 
-      if (cu->v.quick->symtab)
+      if (per_cu->v.quick->symtab)
        continue;
 
-      dw2_require_line_header (objfile, cu);
-      if (!cu->v.quick->lines)
+      file_data = dw2_get_file_names (objfile, per_cu);
+      if (file_data == NULL)
        continue;
 
-      for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
+      for (j = 0; j < file_data->num_file_names; ++j)
        {
-         const char *this_name = cu->v.quick->file_names[j];
-         if (strcmp (this_name, filename) == 0)
+         const char *this_name = file_data->file_names[j];
+         if (FILENAME_CMP (this_name, filename) == 0)
            {
-             dw2_instantiate_symtab (objfile, cu);
+             dw2_instantiate_symtab (objfile, per_cu);
              break;
            }
        }
@@ -2153,11 +2500,13 @@ dw2_expand_symtabs_with_filename (struct objfile *objfile,
 static const char *
 dw2_find_symbol_file (struct objfile *objfile, const char *name)
 {
-  struct dwarf2_per_cu_data *cu;
+  struct dwarf2_per_cu_data *per_cu;
   offset_type *vec;
+  struct quick_file_names *file_data;
 
   dw2_setup (objfile);
 
+  /* index_table is NULL if OBJF_READNOW.  */
   if (!dwarf2_per_objfile->index_table)
     return NULL;
 
@@ -2170,29 +2519,26 @@ dw2_find_symbol_file (struct objfile *objfile, const char *name)
      should be rewritten so that it doesn't require a custom hook.  It
      could just use the ordinary symbol tables.  */
   /* vec[0] is the length, which must always be >0.  */
-  cu = dwarf2_per_objfile->all_comp_units[MAYBE_SWAP (vec[1])];
+  per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
 
-  dw2_require_line_header (objfile, cu);
-  if (!cu->v.quick->lines)
+  file_data = dw2_get_file_names (objfile, per_cu);
+  if (file_data == NULL)
     return NULL;
 
-  return cu->v.quick->file_names[cu->v.quick->lines->num_file_names - 1];
+  return file_data->file_names[file_data->num_file_names - 1];
 }
 
 static void
-dw2_map_ada_symtabs (struct objfile *objfile,
-                    int (*wild_match) (const char *, int, const char *),
-                    int (*is_name_suffix) (const char *),
-                    void (*callback) (struct objfile *,
-                                      struct symtab *, void *),
-                    const char *name, int global,
-                    domain_enum namespace, int wild,
-                    void *data)
+dw2_map_matching_symbols (const char * name, domain_enum namespace,
+                         struct objfile *objfile, int global,
+                         int (*callback) (struct block *,
+                                          struct symbol *, void *),
+                         void *data, symbol_compare_ftype *match,
+                         symbol_compare_ftype *ordered_compare)
 {
-  /* For now, we don't support Ada, so this function can't be
-     reached.  */
-  internal_error (__FILE__, __LINE__,
-                 _("map_ada_symtabs called via index method"));
+  /* Currently unimplemented; used for Ada.  The function can be called if the
+     current language is Ada for a non-Ada objfile using GNU index.  As Ada
+     does not look for non-Ada symbols this function should just return.  */
 }
 
 static void
@@ -2204,63 +2550,66 @@ dw2_expand_symtabs_matching (struct objfile *objfile,
 {
   int i;
   offset_type iter;
+  struct mapped_index *index;
 
   dw2_setup (objfile);
+
+  /* index_table is NULL if OBJF_READNOW.  */
   if (!dwarf2_per_objfile->index_table)
     return;
+  index = dwarf2_per_objfile->index_table;
 
-  for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
+  for (i = 0; i < (dwarf2_per_objfile->n_comp_units
+                  + dwarf2_per_objfile->n_type_comp_units); ++i)
     {
       int j;
-      struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
+      struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
+      struct quick_file_names *file_data;
 
-      cu->v.quick->mark = 0;
-      if (cu->v.quick->symtab)
+      per_cu->v.quick->mark = 0;
+      if (per_cu->v.quick->symtab)
        continue;
 
-      dw2_require_line_header (objfile, cu);
-      if (!cu->v.quick->lines)
+      file_data = dw2_get_file_names (objfile, per_cu);
+      if (file_data == NULL)
        continue;
 
-      for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
+      for (j = 0; j < file_data->num_file_names; ++j)
        {
-         if (file_matcher (cu->v.quick->file_names[j], data))
+         if (file_matcher (file_data->file_names[j], data))
            {
-             cu->v.quick->mark = 1;
+             per_cu->v.quick->mark = 1;
              break;
            }
        }
     }
 
-  for (iter = 0;
-       iter < dwarf2_per_objfile->index_table->index_table_slots;
-       ++iter)
+  for (iter = 0; iter < index->symbol_table_slots; ++iter)
     {
       offset_type idx = 2 * iter;
       const char *name;
       offset_type *vec, vec_len, vec_idx;
 
-      if (dwarf2_per_objfile->index_table->index_table[idx] == 0
-         && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
+      if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
        continue;
 
-      name = (dwarf2_per_objfile->index_table->constant_pool
-             + dwarf2_per_objfile->index_table->index_table[idx]);
+      name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
 
       if (! (*name_matcher) (name, data))
        continue;
 
       /* The name was matched, now expand corresponding CUs that were
         marked.  */
-      vec = (offset_type *) (dwarf2_per_objfile->index_table->constant_pool
-                            + dwarf2_per_objfile->index_table->index_table[idx + 1]);
+      vec = (offset_type *) (index->constant_pool
+                            + MAYBE_SWAP (index->symbol_table[idx + 1]));
       vec_len = MAYBE_SWAP (vec[0]);
       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
        {
-         struct dwarf2_per_cu_data *cu
-           = dwarf2_per_objfile->all_comp_units[MAYBE_SWAP (vec[vec_idx + 1])];
-         if (cu->v.quick->mark)
-           dw2_instantiate_symtab (objfile, cu);
+         struct dwarf2_per_cu_data *per_cu;
+
+         per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
+         if (per_cu->v.quick->mark)
+           dw2_instantiate_symtab (objfile, per_cu);
        }
     }
 }
@@ -2284,7 +2633,7 @@ dw2_find_pc_sect_symtab (struct objfile *objfile,
     return NULL;
 
   if (warn_if_readin && data->v.quick->symtab)
-    warning (_("(Internal error: pc %s in read in CU, but not in symtab.)\n"),
+    warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
             paddress (get_objfile_arch (objfile), pc));
 
   return dw2_instantiate_symtab (objfile, data);
@@ -2296,25 +2645,25 @@ dw2_map_symbol_names (struct objfile *objfile,
                      void *data)
 {
   offset_type iter;
+  struct mapped_index *index;
+
   dw2_setup (objfile);
 
+  /* index_table is NULL if OBJF_READNOW.  */
   if (!dwarf2_per_objfile->index_table)
     return;
+  index = dwarf2_per_objfile->index_table;
 
-  for (iter = 0;
-       iter < dwarf2_per_objfile->index_table->index_table_slots;
-       ++iter)
+  for (iter = 0; iter < index->symbol_table_slots; ++iter)
     {
       offset_type idx = 2 * iter;
       const char *name;
       offset_type *vec, vec_len, vec_idx;
 
-      if (dwarf2_per_objfile->index_table->index_table[idx] == 0
-         && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
+      if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
        continue;
 
-      name = (dwarf2_per_objfile->index_table->constant_pool
-             + dwarf2_per_objfile->index_table->index_table[idx]);
+      name = (index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]));
 
       (*fun) (name, data);
     }
@@ -2328,22 +2677,26 @@ dw2_map_symbol_filenames (struct objfile *objfile,
   int i;
 
   dw2_setup (objfile);
-  for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
+
+  for (i = 0; i < (dwarf2_per_objfile->n_comp_units
+                  + dwarf2_per_objfile->n_type_comp_units); ++i)
     {
       int j;
-      struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
+      struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
+      struct quick_file_names *file_data;
 
-      if (cu->v.quick->symtab)
+      if (per_cu->v.quick->symtab)
        continue;
 
-      dw2_require_line_header (objfile, cu);
-      if (!cu->v.quick->lines)
+      file_data = dw2_get_file_names (objfile, per_cu);
+      if (file_data == NULL)
        continue;
 
-      for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
+      for (j = 0; j < file_data->num_file_names; ++j)
        {
-         const char *this_full_name = dw2_require_full_path (objfile, cu, j);
-         (*fun) (cu->v.quick->file_names[j], this_full_name, data);
+         const char *this_real_name = dw2_get_real_path (objfile, file_data,
+                                                         j);
+         (*fun) (file_data->file_names[j], this_real_name, data);
        }
     }
 }
@@ -2369,7 +2722,7 @@ const struct quick_symbol_functions dwarf2_gdb_index_functions =
   dw2_expand_all_symtabs,
   dw2_expand_symtabs_with_filename,
   dw2_find_symbol_file,
-  dw2_map_ada_symtabs,
+  dw2_map_matching_symbols,
   dw2_expand_symtabs_matching,
   dw2_find_pc_sect_symtab,
   dw2_map_symbol_names,
@@ -2392,13 +2745,17 @@ dwarf2_initialize_objfile (struct objfile *objfile)
 
       dwarf2_per_objfile->using_index = 1;
       create_all_comp_units (objfile);
+      create_debug_types_hash_table (objfile);
+      dwarf2_per_objfile->quick_file_names_table =
+       create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
 
-      for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
+      for (i = 0; i < (dwarf2_per_objfile->n_comp_units
+                      + dwarf2_per_objfile->n_type_comp_units); ++i)
        {
-         struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
+         struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
 
-         cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
-                                       struct dwarf2_per_cu_quick_data);
+         per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
+                                           struct dwarf2_per_cu_quick_data);
        }
 
       /* Return 1 so that gdb sees the "quick" functions.  However,
@@ -2410,7 +2767,6 @@ dwarf2_initialize_objfile (struct objfile *objfile)
   if (dwarf2_read_index (objfile))
     return 1;
 
-  dwarf2_build_psymtabs (objfile);
   return 0;
 }
 
@@ -2487,7 +2843,9 @@ partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
           "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
           bfd_get_filename (abfd));
 
-  if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
+  if (header->abbrev_offset
+      >= dwarf2_section_size (dwarf2_per_objfile->objfile,
+                             &dwarf2_per_objfile->abbrev))
     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
           "(offset 0x%lx + 6) [in module %s]"),
           (long) header->abbrev_offset,
@@ -2586,7 +2944,8 @@ dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
   if (lh == NULL)
     return;  /* No linetable, so no includes.  */
 
-  dwarf_decode_lines (lh, NULL, abfd, cu, pst);
+  /* NOTE: pst->dirname is DW_AT_comp_dir (if present).  */
+  dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
 
   free_line_header (lh);
 }
@@ -2609,6 +2968,34 @@ eq_type_signature (const void *item_lhs, const void *item_rhs)
   return lhs->signature == rhs->signature;
 }
 
+/* Allocate a hash table for signatured types.  */
+
+static htab_t
+allocate_signatured_type_table (struct objfile *objfile)
+{
+  return htab_create_alloc_ex (41,
+                              hash_type_signature,
+                              eq_type_signature,
+                              NULL,
+                              &objfile->objfile_obstack,
+                              hashtab_obstack_allocate,
+                              dummy_obstack_deallocate);
+}
+
+/* A helper function to add a signatured type CU to a list.  */
+
+static int
+add_signatured_type_cu_to_list (void **slot, void *datum)
+{
+  struct signatured_type *sigt = *slot;
+  struct dwarf2_per_cu_data ***datap = datum;
+
+  **datap = &sigt->per_cu;
+  ++*datap;
+
+  return 1;
+}
+
 /* Create the hash table of all entries in the .debug_types section.
    The result is zero if there is an error (e.g. missing .debug_types section),
    otherwise non-zero. */
@@ -2618,6 +3005,7 @@ create_debug_types_hash_table (struct objfile *objfile)
 {
   gdb_byte *info_ptr;
   htab_t types_htab;
+  struct dwarf2_per_cu_data **iter;
 
   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
   info_ptr = dwarf2_per_objfile->types.buffer;
@@ -2628,18 +3016,13 @@ create_debug_types_hash_table (struct objfile *objfile)
       return 0;
     }
 
-  types_htab = htab_create_alloc_ex (41,
-                                    hash_type_signature,
-                                    eq_type_signature,
-                                    NULL,
-                                    &objfile->objfile_obstack,
-                                    hashtab_obstack_allocate,
-                                    dummy_obstack_deallocate);
+  types_htab = allocate_signatured_type_table (objfile);
 
   if (dwarf2_die_debug)
     fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
 
-  while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
+  while (info_ptr < dwarf2_per_objfile->types.buffer
+        + dwarf2_per_objfile->types.size)
     {
       unsigned int offset;
       unsigned int offset_size;
@@ -2662,7 +3045,8 @@ create_debug_types_hash_table (struct objfile *objfile)
          > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
        {
          complaint (&symfile_complaints,
-                    _("debug type entry runs off end of `.debug_types' section, ignored"));
+                    _("debug type entry runs off end "
+                      "of `.debug_types' section, ignored"));
          break;
        }
 
@@ -2681,6 +3065,8 @@ create_debug_types_hash_table (struct objfile *objfile)
       type_sig->signature = signature;
       type_sig->offset = offset;
       type_sig->type_offset = type_offset;
+      type_sig->per_cu.objfile = objfile;
+      type_sig->per_cu.from_debug_types = 1;
 
       slot = htab_find_slot (types_htab, type_sig, INSERT);
       gdb_assert (slot != NULL);
@@ -2695,6 +3081,16 @@ create_debug_types_hash_table (struct objfile *objfile)
 
   dwarf2_per_objfile->signatured_types = types_htab;
 
+  dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
+  dwarf2_per_objfile->type_comp_units
+    = obstack_alloc (&objfile->objfile_obstack,
+                    dwarf2_per_objfile->n_type_comp_units
+                    * sizeof (struct dwarf2_per_cu_data *));
+  iter = &dwarf2_per_objfile->type_comp_units[0];
+  htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
+  gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
+             == dwarf2_per_objfile->n_type_comp_units);
+
   return 1;
 }
 
@@ -2794,11 +3190,9 @@ process_psymtab_comp_unit (struct objfile *objfile,
   struct attribute *attr;
   CORE_ADDR best_lowpc = 0, best_highpc = 0;
   struct die_reader_specs reader_specs;
+  const char *filename;
 
-  memset (&cu, 0, sizeof (cu));
-  cu.objfile = objfile;
-  obstack_init (&cu.comp_unit_obstack);
-
+  init_one_comp_unit (&cu, objfile);
   back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
 
   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
@@ -2852,17 +3246,16 @@ process_psymtab_comp_unit (struct objfile *objfile,
       return info_ptr;
     }
 
-  /* Set the language we're debugging.  */
-  attr = dwarf2_attr (comp_unit_die, DW_AT_language, &cu);
-  if (attr)
-    set_cu_language (DW_UNSND (attr), &cu);
-  else
-    set_cu_language (language_minimal, &cu);
+  prepare_one_comp_unit (&cu, comp_unit_die);
 
   /* Allocate a new partial symbol table structure.  */
   attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
+  if (attr == NULL || !DW_STRING (attr))
+    filename = "";
+  else
+    filename = DW_STRING (attr);
   pst = start_psymtab_common (objfile, objfile->section_offsets,
-                             (attr != NULL) ? DW_STRING (attr) : "",
+                             filename,
                              /* TEXTLOW and TEXTHIGH are set below.  */
                              0,
                              objfile->global_psymbols.next,
@@ -2876,7 +3269,7 @@ process_psymtab_comp_unit (struct objfile *objfile,
 
   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
 
-  /* Store the function that reads in the rest of the symbol table */
+  /* Store the function that reads in the rest of the symbol table */
   pst->read_symtab = dwarf2_psymtab_to_symtab;
 
   this_cu->v.psymtab = pst;
@@ -2896,7 +3289,7 @@ process_psymtab_comp_unit (struct objfile *objfile,
 
   /* Check if comp unit has_children.
      If so, read the rest of the partial symbols from this comp unit.
-     If not, there's no more debug_info for this comp unit. */
+     If not, there's no more debug_info for this comp unit.  */
   if (has_children)
     {
       struct partial_die_info *first_die;
@@ -2963,7 +3356,6 @@ process_type_comp_unit (void **slot, void *info)
   struct dwarf2_per_cu_data *this_cu;
 
   this_cu = &entry->per_cu;
-  this_cu->from_debug_types = 1;
 
   gdb_assert (dwarf2_per_objfile->types.readin);
   process_psymtab_comp_unit (objfile, this_cu,
@@ -3007,6 +3399,8 @@ dwarf2_build_psymtabs_hard (struct objfile *objfile)
   struct cleanup *back_to, *addrmap_cleanup;
   struct obstack temp_obstack;
 
+  dwarf2_per_objfile->reading_partial_symbols = 1;
+
   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
   info_ptr = dwarf2_per_objfile->info.buffer;
 
@@ -3044,7 +3438,8 @@ dwarf2_build_psymtabs_hard (struct objfile *objfile)
     {
       struct dwarf2_per_cu_data *this_cu;
 
-      this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
+      this_cu = dwarf2_find_comp_unit (info_ptr
+                                      - dwarf2_per_objfile->info.buffer,
                                       objfile);
 
       info_ptr = process_psymtab_comp_unit (objfile, this_cu,
@@ -3070,10 +3465,10 @@ load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
   gdb_byte *info_ptr, *beg_of_comp_unit;
   struct die_info *comp_unit_die;
   struct dwarf2_cu *cu;
-  struct cleanup *back_to;
-  struct attribute *attr;
+  struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
   int has_children;
   struct die_reader_specs reader_specs;
+  int read_cu = 0;
 
   gdb_assert (! this_cu->from_debug_types);
 
@@ -3081,47 +3476,65 @@ load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
   info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
   beg_of_comp_unit = info_ptr;
 
-  cu = alloc_one_comp_unit (objfile);
+  if (this_cu->cu == NULL)
+    {
+      cu = xmalloc (sizeof (*cu));
+      init_one_comp_unit (cu, objfile);
 
-  /* ??? Missing cleanup for CU?  */
+      read_cu = 1;
 
-  /* Link this compilation unit into the compilation unit tree.  */
-  this_cu->cu = cu;
-  cu->per_cu = this_cu;
-  cu->type_hash = this_cu->type_hash;
+      /* If an error occurs while loading, release our storage.  */
+      free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
 
-  info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
-                                         dwarf2_per_objfile->info.buffer,
-                                         dwarf2_per_objfile->info.size,
-                                         abfd);
+      info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
+                                             dwarf2_per_objfile->info.buffer,
+                                             dwarf2_per_objfile->info.size,
+                                             abfd);
 
-  /* Complete the cu_header.  */
-  cu->header.offset = this_cu->offset;
-  cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
+      /* Complete the cu_header.  */
+      cu->header.offset = this_cu->offset;
+      cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
+
+      /* Link this compilation unit into the compilation unit tree.  */
+      this_cu->cu = cu;
+      cu->per_cu = this_cu;
+
+      /* Link this CU into read_in_chain.  */
+      this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
+      dwarf2_per_objfile->read_in_chain = this_cu;
+    }
+  else
+    {
+      cu = this_cu->cu;
+      info_ptr += cu->header.first_die_offset;
+    }
 
   /* Read the abbrevs for this compilation unit into a table.  */
+  gdb_assert (cu->dwarf2_abbrevs == NULL);
   dwarf2_read_abbrevs (abfd, cu);
-  back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
+  free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
 
   /* Read the compilation unit die.  */
   init_cu_die_reader (&reader_specs, cu);
   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
                            &has_children);
 
-  /* Set the language we're debugging.  */
-  attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
-  if (attr)
-    set_cu_language (DW_UNSND (attr), cu);
-  else
-    set_cu_language (language_minimal, cu);
+  prepare_one_comp_unit (cu, comp_unit_die);
 
   /* Check if comp unit has_children.
      If so, read the rest of the partial symbols from this comp unit.
-     If not, there's no more debug_info for this comp unit. */
+     If not, there's no more debug_info for this comp unit.  */
   if (has_children)
     load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
 
-  do_cleanups (back_to);
+  do_cleanups (free_abbrevs_cleanup);
+
+  if (read_cu)
+    {
+      /* We've successfully allocated this compilation unit.  Let our
+        caller clean it up when finished with it.  */
+      discard_cleanups (free_cu_cleanup);
+    }
 }
 
 /* Create a list of all compilation units in OBJFILE.  We do this only
@@ -3145,7 +3558,8 @@ create_all_comp_units (struct objfile *objfile)
   all_comp_units = xmalloc (n_allocated
                            * sizeof (struct dwarf2_per_cu_data *));
 
-  while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
+  while (info_ptr < dwarf2_per_objfile->info.buffer
+        + dwarf2_per_objfile->info.size)
     {
       unsigned int length, initial_length_size;
       struct dwarf2_per_cu_data *this_cu;
@@ -3222,6 +3636,7 @@ scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
            case DW_TAG_subprogram:
              add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
              break;
+           case DW_TAG_constant:
            case DW_TAG_variable:
            case DW_TAG_typedef:
            case DW_TAG_union_type:
@@ -3338,7 +3753,8 @@ partial_die_parent_scope (struct partial_die_info *pdi,
       if (grandparent_scope == NULL)
        parent->scope = parent->name;
       else
-       parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
+       parent->scope = typename_concat (&cu->comp_unit_obstack,
+                                        grandparent_scope,
                                         parent->name, 0, cu);
     }
   else if (parent->tag == DW_TAG_enumerator)
@@ -3367,6 +3783,29 @@ partial_die_full_name (struct partial_die_info *pdi,
 {
   char *parent_scope;
 
+  /* If this is a template instantiation, we can not work out the
+     template arguments from partial DIEs.  So, unfortunately, we have
+     to go through the full DIEs.  At least any work we do building
+     types here will be reused if full symbols are loaded later.  */
+  if (pdi->has_template_arguments)
+    {
+      fixup_partial_die (pdi, cu);
+
+      if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
+       {
+         struct die_info *die;
+         struct attribute attr;
+         struct dwarf2_cu *ref_cu = cu;
+
+         attr.name = 0;
+         attr.form = DW_FORM_ref_addr;
+         attr.u.addr = pdi->offset;
+         die = follow_die_ref (NULL, &attr, &ref_cu);
+
+         return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
+       }
+    }
+
   parent_scope = partial_die_parent_scope (pdi, cu);
   if (parent_scope == NULL)
     return NULL;
@@ -3402,29 +3841,54 @@ add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
              of the global scope.  But in Ada, we want to be able to access
              nested procedures globally.  So all Ada subprograms are stored
              in the global scope.  */
-         /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
+         /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
             mst_text, objfile); */
-         psym = add_psymbol_to_list (actual_name, strlen (actual_name),
-                                     built_actual_name,
-                                     VAR_DOMAIN, LOC_BLOCK,
-                                     &objfile->global_psymbols,
-                                     0, pdi->lowpc + baseaddr,
-                                     cu->language, objfile);
+         add_psymbol_to_list (actual_name, strlen (actual_name),
+                              built_actual_name,
+                              VAR_DOMAIN, LOC_BLOCK,
+                              &objfile->global_psymbols,
+                              0, pdi->lowpc + baseaddr,
+                              cu->language, objfile);
        }
       else
        {
-         /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
+         /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
             mst_file_text, objfile); */
-         psym = add_psymbol_to_list (actual_name, strlen (actual_name),
-                                     built_actual_name,
-                                     VAR_DOMAIN, LOC_BLOCK,
-                                     &objfile->static_psymbols,
-                                     0, pdi->lowpc + baseaddr,
-                                     cu->language, objfile);
+         add_psymbol_to_list (actual_name, strlen (actual_name),
+                              built_actual_name,
+                              VAR_DOMAIN, LOC_BLOCK,
+                              &objfile->static_psymbols,
+                              0, pdi->lowpc + baseaddr,
+                              cu->language, objfile);
        }
       break;
+    case DW_TAG_constant:
+      {
+        struct psymbol_allocation_list *list;
+
+       if (pdi->is_external)
+         list = &objfile->global_psymbols;
+       else
+         list = &objfile->static_psymbols;
+       add_psymbol_to_list (actual_name, strlen (actual_name),
+                            built_actual_name, VAR_DOMAIN, LOC_STATIC,
+                            list, 0, 0, cu->language, objfile);
+      }
+      break;
     case DW_TAG_variable:
-      if (pdi->is_external)
+      if (pdi->locdesc)
+       addr = decode_locdesc (pdi->locdesc, cu);
+
+      if (pdi->locdesc
+         && addr == 0
+         && !dwarf2_per_objfile->has_section_at_zero)
+       {
+         /* A global or static variable may also have been stripped
+            out by the linker if unused, in which case its address
+            will be nullified; do not add such variables into partial
+            symbol table then.  */
+       }
+      else if (pdi->is_external)
        {
          /* Global Variable.
             Don't enter into the minimal symbol tables as there is
@@ -3439,34 +3903,31 @@ add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
             used by GDB, but it comes in handy for debugging partial symbol
             table building.  */
 
-         if (pdi->locdesc)
-           addr = decode_locdesc (pdi->locdesc, cu);
          if (pdi->locdesc || pdi->has_type)
-           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
-                                       built_actual_name,
-                                       VAR_DOMAIN, LOC_STATIC,
-                                       &objfile->global_psymbols,
-                                       0, addr + baseaddr,
-                                       cu->language, objfile);
+           add_psymbol_to_list (actual_name, strlen (actual_name),
+                                built_actual_name,
+                                VAR_DOMAIN, LOC_STATIC,
+                                &objfile->global_psymbols,
+                                0, addr + baseaddr,
+                                cu->language, objfile);
        }
       else
        {
-         /* Static Variable. Skip symbols without location descriptors.  */
+         /* Static Variable.  Skip symbols without location descriptors.  */
          if (pdi->locdesc == NULL)
            {
              if (built_actual_name)
                xfree (actual_name);
              return;
            }
-         addr = decode_locdesc (pdi->locdesc, cu);
-         /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
+         /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
             mst_file_data, objfile); */
-         psym = add_psymbol_to_list (actual_name, strlen (actual_name),
-                                     built_actual_name,
-                                     VAR_DOMAIN, LOC_STATIC,
-                                     &objfile->static_psymbols,
-                                     0, addr + baseaddr,
-                                     cu->language, objfile);
+         add_psymbol_to_list (actual_name, strlen (actual_name),
+                              built_actual_name,
+                              VAR_DOMAIN, LOC_STATIC,
+                              &objfile->static_psymbols,
+                              0, addr + baseaddr,
+                              cu->language, objfile);
        }
       break;
     case DW_TAG_typedef:
@@ -3624,40 +4085,6 @@ add_partial_subprogram (struct partial_die_info *pdi,
     }
 }
 
-/* See if we can figure out if the class lives in a namespace.  We do
-   this by looking for a member function; its demangled name will
-   contain namespace info, if there is any.  */
-
-static void
-guess_structure_name (struct partial_die_info *struct_pdi,
-                     struct dwarf2_cu *cu)
-{
-  if ((cu->language == language_cplus
-       || cu->language == language_java)
-      && cu->has_namespace_info == 0
-      && struct_pdi->has_children)
-    {
-      /* NOTE: carlton/2003-10-07: Getting the info this way changes
-        what template types look like, because the demangler
-        frequently doesn't give the same name as the debug info.  We
-        could fix this by only using the demangled name to get the
-        prefix (but see comment in read_structure_type).  */
-
-      struct partial_die_info *real_pdi;
-
-      /* If this DIE (this DIE's specification, if any) has a parent, then
-        we should not do this.  We'll prepend the parent's fully qualified
-         name when we create the partial symbol.  */
-
-      real_pdi = struct_pdi;
-      while (real_pdi->has_specification)
-       real_pdi = find_partial_die (real_pdi->spec_offset, cu);
-
-      if (real_pdi->die_parent != NULL)
-       return;
-    }
-}
-
 /* Read a partial die corresponding to an enumeration type.  */
 
 static void
@@ -3701,8 +4128,8 @@ peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
   if (!abbrev)
     {
-      error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
-                     bfd_get_filename (abfd));
+      error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
+            abbrev_number, bfd_get_filename (abfd));
     }
 
   return abbrev;
@@ -3751,7 +4178,8 @@ skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
          read_attribute (&attr, &abbrev->attrs[i],
                          abfd, info_ptr, cu);
          if (attr.form == DW_FORM_ref_addr)
-           complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
+           complaint (&symfile_complaints,
+                      _("ignoring absolute DW_AT_sibling"));
          else
            return buffer + dwarf2_get_ref_die_offset (&attr);
        }
@@ -3793,7 +4221,7 @@ skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
          info_ptr += 8;
          break;
        case DW_FORM_string:
-         read_string (abfd, info_ptr, &bytes_read);
+         read_direct_string (abfd, info_ptr, &bytes_read);
          info_ptr += bytes_read;
          break;
        case DW_FORM_sec_offset:
@@ -3827,7 +4255,8 @@ skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
          goto skip_attribute;
 
        default:
-         error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
+         error (_("Dwarf Error: Cannot handle %s "
+                  "in DWARF reader [in module %s]"),
                 dwarf_form_name (form),
                 bfd_get_filename (abfd));
        }
@@ -3872,13 +4301,15 @@ dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
     {
       if (pst->readin)
        {
-         warning (_("bug: psymtab for %s is already read in."), pst->filename);
+         warning (_("bug: psymtab for %s is already read in."),
+                  pst->filename);
        }
       else
        {
          if (info_verbose)
            {
-             printf_filtered (_("Reading in symbols for %s..."), pst->filename);
+             printf_filtered (_("Reading in symbols for %s..."),
+                              pst->filename);
              gdb_flush (gdb_stdout);
            }
 
@@ -3900,6 +4331,8 @@ dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
                = dpo_backlink->has_section_at_zero;
            }
 
+         dwarf2_per_objfile->reading_partial_symbols = 0;
+
          psymtab_to_symtab_1 (pst);
 
          /* Finish up the debug error message.  */
@@ -4003,7 +4436,7 @@ psymtab_to_symtab_1 (struct partial_symtab *pst)
             fputs_filtered ("and ", gdb_stdout);
             wrap_here ("");
             printf_filtered ("%s...", pst->dependencies[i]->filename);
-            wrap_here ("");     /* Flush output */
+            wrap_here ("");     /* Flush output */
             gdb_flush (gdb_stdout);
           }
         psymtab_to_symtab_1 (pst->dependencies[i]);
@@ -4025,14 +4458,16 @@ psymtab_to_symtab_1 (struct partial_symtab *pst)
 /* Load the DIEs associated with PER_CU into memory.  */
 
 static void
-load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
+load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
+                    struct objfile *objfile)
 {
   bfd *abfd = objfile->obfd;
   struct dwarf2_cu *cu;
   unsigned int offset;
   gdb_byte *info_ptr, *beg_of_comp_unit;
-  struct cleanup *back_to, *free_cu_cleanup;
+  struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
   struct attribute *attr;
+  int read_cu = 0;
 
   gdb_assert (! per_cu->from_debug_types);
 
@@ -4043,26 +4478,40 @@ load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
   info_ptr = dwarf2_per_objfile->info.buffer + offset;
   beg_of_comp_unit = info_ptr;
 
-  cu = alloc_one_comp_unit (objfile);
+  if (per_cu->cu == NULL)
+    {
+      cu = xmalloc (sizeof (*cu));
+      init_one_comp_unit (cu, objfile);
+
+      read_cu = 1;
 
-  /* If an error occurs while loading, release our storage.  */
-  free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
+      /* If an error occurs while loading, release our storage.  */
+      free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
 
-  /* Read in the comp_unit header.  */
-  info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
+      /* Read in the comp_unit header.  */
+      info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
 
-  /* Complete the cu_header.  */
-  cu->header.offset = offset;
-  cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
+      /* Complete the cu_header.  */
+      cu->header.offset = offset;
+      cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
 
-  /* Read the abbrevs for this compilation unit.  */
-  dwarf2_read_abbrevs (abfd, cu);
-  back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
+      /* Read the abbrevs for this compilation unit.  */
+      dwarf2_read_abbrevs (abfd, cu);
+      free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
 
-  /* Link this compilation unit into the compilation unit tree.  */
-  per_cu->cu = cu;
-  cu->per_cu = per_cu;
-  cu->type_hash = per_cu->type_hash;
+      /* Link this compilation unit into the compilation unit tree.  */
+      per_cu->cu = cu;
+      cu->per_cu = per_cu;
+
+      /* Link this CU into read_in_chain.  */
+      per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
+      dwarf2_per_objfile->read_in_chain = per_cu;
+    }
+  else
+    {
+      cu = per_cu->cu;
+      info_ptr += cu->header.first_die_offset;
+    }
 
   cu->dies = read_comp_unit (info_ptr, cu);
 
@@ -4070,11 +4519,7 @@ load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
      all objfiles needed for references have been loaded yet, and symbol
      table processing isn't initialized.  But we have to set the CU language,
      or we won't be able to build types correctly.  */
-  attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
-  if (attr)
-    set_cu_language (DW_UNSND (attr), cu);
-  else
-    set_cu_language (language_minimal, cu);
+  prepare_one_comp_unit (cu, cu->dies);
 
   /* Similarly, if we do not read the producer, we can not apply
      producer-specific interpretation.  */
@@ -4082,15 +4527,64 @@ load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
   if (attr)
     cu->producer = DW_STRING (attr);
 
-  /* Link this CU into read_in_chain.  */
-  per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
-  dwarf2_per_objfile->read_in_chain = per_cu;
+  if (read_cu)
+    {
+      do_cleanups (free_abbrevs_cleanup);
 
-  do_cleanups (back_to);
+      /* We've successfully allocated this compilation unit.  Let our
+        caller clean it up when finished with it.  */
+      discard_cleanups (free_cu_cleanup);
+    }
+}
 
-  /* We've successfully allocated this compilation unit.  Let our caller
-     clean it up when finished with it.  */
-  discard_cleanups (free_cu_cleanup);
+/* Add a DIE to the delayed physname list.  */
+
+static void
+add_to_method_list (struct type *type, int fnfield_index, int index,
+                   const char *name, struct die_info *die,
+                   struct dwarf2_cu *cu)
+{
+  struct delayed_method_info mi;
+  mi.type = type;
+  mi.fnfield_index = fnfield_index;
+  mi.index = index;
+  mi.name = name;
+  mi.die = die;
+  VEC_safe_push (delayed_method_info, cu->method_list, &mi);
+}
+
+/* A cleanup for freeing the delayed method list.  */
+
+static void
+free_delayed_list (void *ptr)
+{
+  struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
+  if (cu->method_list != NULL)
+    {
+      VEC_free (delayed_method_info, cu->method_list);
+      cu->method_list = NULL;
+    }
+}
+
+/* Compute the physnames of any methods on the CU's method list.
+
+   The computation of method physnames is delayed in order to avoid the
+   (bad) condition that one of the method's formal parameters is of an as yet
+   incomplete type.  */
+
+static void
+compute_delayed_physnames (struct dwarf2_cu *cu)
+{
+  int i;
+  struct delayed_method_info *mi;
+  for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
+    {
+      char *physname;
+      struct fn_fieldlist *fn_flp
+       = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
+      physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
+      fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
+    }
 }
 
 /* Generate full symbol information for PST and CU, whose DIEs have
@@ -4103,13 +4597,14 @@ process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
   struct objfile *objfile = per_cu->objfile;
   CORE_ADDR lowpc, highpc;
   struct symtab *symtab;
-  struct cleanup *back_to;
+  struct cleanup *back_to, *delayed_list_cleanup;
   CORE_ADDR baseaddr;
 
   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
 
   buildsym_init ();
   back_to = make_cleanup (really_free_pendings, NULL);
+  delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
 
   cu->list_in_scope = &file_symbols;
 
@@ -4118,6 +4613,12 @@ process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
   /* Do line number decoding in read_file_scope () */
   process_die (cu->dies, cu);
 
+  /* Now that we have processed all the DIEs in the CU, all the types 
+     should be complete, and it should now be safe to compute all of the
+     physnames.  */
+  compute_delayed_physnames (cu);
+  do_cleanups (delayed_list_cleanup);
+
   /* Some compilers don't define a DW_AT_high_pc attribute for the
      compilation unit.  If the DW_AT_high_pc is missing, synthesize
      it, by scanning the DIE's below the compilation unit.  */
@@ -4195,8 +4696,6 @@ process_die (struct die_info *die, struct dwarf2_cu *cu)
     case DW_TAG_base_type:
     case DW_TAG_subrange_type:
     case DW_TAG_typedef:
-    case DW_TAG_const_type:
-    case DW_TAG_volatile_type:
       /* Add a typedef symbol for the type definition, if it has a
          DW_AT_name.  */
       new_symbol (die, read_type_die (die, cu), cu);
@@ -4253,6 +4752,7 @@ die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
       return 1;
 
     case DW_TAG_variable:
+    case DW_TAG_constant:
       /* We only need to prefix "globally" visible variables.  These include
         any variable marked with DW_AT_external or any variable that
         lives in a namespace.  [Variables in anonymous namespaces
@@ -4285,6 +4785,17 @@ die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
     }
 }
 
+/* Retrieve the last character from a mem_file.  */
+
+static void
+do_ui_file_peek_last (void *object, const char *buffer, long length)
+{
+  char *last_char_p = (char *) object;
+
+  if (length > 0)
+    *last_char_p = buffer[length - 1];
+}
+
 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
    compute the physname for the object, which include a method's
    formal parameters (C++/Java) and return type (Java).
@@ -4342,7 +4853,133 @@ dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
              xfree (prefixed_name);
            }
          else
-           fputs_unfiltered (name ? name : "", buf);
+           fputs_unfiltered (name, buf);
+
+         /* Template parameters may be specified in the DIE's DW_AT_name, or
+            as children with DW_TAG_template_type_param or
+            DW_TAG_value_type_param.  If the latter, add them to the name
+            here.  If the name already has template parameters, then
+            skip this step; some versions of GCC emit both, and
+            it is more efficient to use the pre-computed name.
+
+            Something to keep in mind about this process: it is very
+            unlikely, or in some cases downright impossible, to produce
+            something that will match the mangled name of a function.
+            If the definition of the function has the same debug info,
+            we should be able to match up with it anyway.  But fallbacks
+            using the minimal symbol, for instance to find a method
+            implemented in a stripped copy of libstdc++, will not work.
+            If we do not have debug info for the definition, we will have to
+            match them up some other way.
+
+            When we do name matching there is a related problem with function
+            templates; two instantiated function templates are allowed to
+            differ only by their return types, which we do not add here.  */
+
+         if (cu->language == language_cplus && strchr (name, '<') == NULL)
+           {
+             struct attribute *attr;
+             struct die_info *child;
+             int first = 1;
+
+             die->building_fullname = 1;
+
+             for (child = die->child; child != NULL; child = child->sibling)
+               {
+                 struct type *type;
+                 long value;
+                 gdb_byte *bytes;
+                 struct dwarf2_locexpr_baton *baton;
+                 struct value *v;
+
+                 if (child->tag != DW_TAG_template_type_param
+                     && child->tag != DW_TAG_template_value_param)
+                   continue;
+
+                 if (first)
+                   {
+                     fputs_unfiltered ("<", buf);
+                     first = 0;
+                   }
+                 else
+                   fputs_unfiltered (", ", buf);
+
+                 attr = dwarf2_attr (child, DW_AT_type, cu);
+                 if (attr == NULL)
+                   {
+                     complaint (&symfile_complaints,
+                                _("template parameter missing DW_AT_type"));
+                     fputs_unfiltered ("UNKNOWN_TYPE", buf);
+                     continue;
+                   }
+                 type = die_type (child, cu);
+
+                 if (child->tag == DW_TAG_template_type_param)
+                   {
+                     c_print_type (type, "", buf, -1, 0);
+                     continue;
+                   }
+
+                 attr = dwarf2_attr (child, DW_AT_const_value, cu);
+                 if (attr == NULL)
+                   {
+                     complaint (&symfile_complaints,
+                                _("template parameter missing "
+                                  "DW_AT_const_value"));
+                     fputs_unfiltered ("UNKNOWN_VALUE", buf);
+                     continue;
+                   }
+
+                 dwarf2_const_value_attr (attr, type, name,
+                                          &cu->comp_unit_obstack, cu,
+                                          &value, &bytes, &baton);
+
+                 if (TYPE_NOSIGN (type))
+                   /* GDB prints characters as NUMBER 'CHAR'.  If that's
+                      changed, this can use value_print instead.  */
+                   c_printchar (value, type, buf);
+                 else
+                   {
+                     struct value_print_options opts;
+
+                     if (baton != NULL)
+                       v = dwarf2_evaluate_loc_desc (type, NULL,
+                                                     baton->data,
+                                                     baton->size,
+                                                     baton->per_cu);
+                     else if (bytes != NULL)
+                       {
+                         v = allocate_value (type);
+                         memcpy (value_contents_writeable (v), bytes,
+                                 TYPE_LENGTH (type));
+                       }
+                     else
+                       v = value_from_longest (type, value);
+
+                     /* Specify decimal so that we do not depend on
+                        the radix.  */
+                     get_formatted_print_options (&opts, 'd');
+                     opts.raw = 1;
+                     value_print (v, buf, &opts);
+                     release_value (v);
+                     value_free (v);
+                   }
+               }
+
+             die->building_fullname = 0;
+
+             if (!first)
+               {
+                 /* Close the argument list, with a space if necessary
+                    (nested templates).  */
+                 char last_char = '\0';
+                 ui_file_put (buf, do_ui_file_peek_last, &last_char);
+                 if (last_char == '>')
+                   fputs_unfiltered (" >", buf);
+                 else
+                   fputs_unfiltered (">", buf);
+               }
+           }
 
          /* For Java and C++ methods, append formal parameter type
             information, if PHYSNAME.  */
@@ -4358,16 +4995,23 @@ dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
              if (cu->language == language_java)
                {
                  /* For java, we must append the return type to method
-                    names. */
+                    names.  */
                  if (die->tag == DW_TAG_subprogram)
                    java_print_type (TYPE_TARGET_TYPE (type), "", buf,
                                     0, 0);
                }
              else if (cu->language == language_cplus)
                {
+                 /* Assume that an artificial first parameter is
+                    "this", but do not crash if it is not.  RealView
+                    marks unnamed (and thus unused) parameters as
+                    artificial; there is no way to differentiate
+                    the two cases.  */
                  if (TYPE_NFIELDS (type) > 0
                      && TYPE_FIELD_ARTIFICIAL (type, 0)
-                     && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
+                     && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
+                     && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
+                                                                       0))))
                    fputs_unfiltered (" const", buf);
                }
            }
@@ -4397,7 +5041,7 @@ dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
    not have a name.  NAME may either be from a previous call to
    dwarf2_name or NULL.
 
-   The output string will be canonicalized (if C++/Java). */
+   The output string will be canonicalized (if C++/Java).  */
 
 static const char *
 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
@@ -4524,6 +5168,8 @@ initialize_cu_func_list (struct dwarf2_cu *cu)
   cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
 }
 
+/* Cleanup function for read_file_scope.  */
+
 static void
 free_cu_line_header (void *arg)
 {
@@ -4573,6 +5219,8 @@ find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
     *name = "<unknown>";
 }
 
+/* Process DW_TAG_compile_unit.  */
+
 static void
 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
 {
@@ -4611,7 +5259,13 @@ read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
   if (attr)
     cu->producer = DW_STRING (attr);
 
-  /* We assume that we're processing GCC output. */
+  /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
+     standardised yet.  As a workaround for the language detection we fall
+     back to the DW_AT_producer string.  */
+  if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
+    cu->language = language_opencl;
+
+  /* We assume that we're processing GCC output.  */
   processing_gcc_compilation = 2;
 
   processing_has_namespace_info = 0;
@@ -4664,7 +5318,8 @@ read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
   do_cleanups (back_to);
 }
 
-/* For TUs we want to skip the first top level sibling if it's not the
+/* Process DW_TAG_type_unit.
+   For TUs we want to skip the first top level sibling if it's not the
    actual type being defined by this TU.  In this case the first top
    level sibling is there to provide context only.  */
 
@@ -4713,7 +5368,7 @@ read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
   if (attr)
     cu->producer = DW_STRING (attr);
 
-  /* We assume that we're processing GCC output. */
+  /* We assume that we're processing GCC output.  */
   processing_gcc_compilation = 2;
 
   processing_has_namespace_info = 0;
@@ -4776,8 +5431,9 @@ unsigned_int_compar (const void *ap, const void *bp)
 }
 
 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
-   Inherit only the children of the DW_AT_abstract_origin DIE not being already
-   referenced by DW_AT_abstract_origin from the children of the current DIE.  */
+   Inherit only the children of the DW_AT_abstract_origin DIE not being
+   already referenced by DW_AT_abstract_origin from the children of the
+   current DIE.  */
 
 static void
 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
@@ -4793,12 +5449,24 @@ inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
   struct die_info *origin_child_die;
   struct cleanup *cleanups;
   struct attribute *attr;
+  struct dwarf2_cu *origin_cu;
+  struct pending **origin_previous_list_in_scope;
 
   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
   if (!attr)
     return;
 
-  origin_die = follow_die_ref (die, attr, &cu);
+  /* Note that following die references may follow to a die in a
+     different cu.  */
+
+  origin_cu = cu;
+  origin_die = follow_die_ref (die, attr, &origin_cu);
+
+  /* We're inheriting ORIGIN's children into the scope we'd put DIE's
+     symbols in.  */
+  origin_previous_list_in_scope = origin_cu->list_in_scope;
+  origin_cu->list_in_scope = cu->list_in_scope;
+
   if (die->tag != origin_die->tag
       && !(die->tag == DW_TAG_inlined_subroutine
           && origin_die->tag == DW_TAG_subprogram))
@@ -4826,13 +5494,16 @@ inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
         but GCC versions at least through 4.4 generate this (GCC PR
         40573).  */
       struct die_info *child_origin_die = child_die;
+      struct dwarf2_cu *child_origin_cu = cu;
 
       while (1)
        {
-         attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin, cu);
+         attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
+                             child_origin_cu);
          if (attr == NULL)
            break;
-         child_origin_die = follow_die_ref (child_origin_die, attr, &cu);
+         child_origin_die = follow_die_ref (child_origin_die, attr,
+                                            &child_origin_cu);
        }
 
       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
@@ -4860,8 +5531,9 @@ inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
         unsigned_int_compar);
   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
     if (offsetp[-1] == *offsetp)
-      complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
-                                       "to DIE 0x%x as their abstract origin"),
+      complaint (&symfile_complaints,
+                _("Multiple children of DIE 0x%x refer "
+                  "to DIE 0x%x as their abstract origin"),
                 die->offset, *offsetp);
 
   offsetp = offsets;
@@ -4874,10 +5546,11 @@ inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
       if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
        {
          /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
-         process_die (origin_child_die, cu);
+         process_die (origin_child_die, origin_cu);
        }
       origin_child_die = sibling_die (origin_child_die);
     }
+  origin_cu->list_in_scope = origin_previous_list_in_scope;
 
   do_cleanups (cleanups);
 }
@@ -4895,6 +5568,8 @@ read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
   CORE_ADDR baseaddr;
   struct block *block;
   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
+  VEC (symbolp) *template_args = NULL;
+  struct template_symbol *templ_func = NULL;
 
   if (inlined_func)
     {
@@ -4929,7 +5604,8 @@ read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
       attr = dwarf2_attr (die, DW_AT_external, cu);
       if (!attr || !DW_UNSND (attr))
        complaint (&symfile_complaints,
-                  _("cannot get low and high bounds for subprogram DIE at %d"),
+                  _("cannot get low and high bounds "
+                    "for subprogram DIE at %d"),
                   die->offset);
       return;
     }
@@ -4940,8 +5616,23 @@ read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
   /* Record the function range for dwarf_decode_lines.  */
   add_to_cu_func_list (name, lowpc, highpc, cu);
 
+  /* If we have any template arguments, then we must allocate a
+     different sort of symbol.  */
+  for (child_die = die->child; child_die; child_die = sibling_die (child_die))
+    {
+      if (child_die->tag == DW_TAG_template_type_param
+         || child_die->tag == DW_TAG_template_value_param)
+       {
+         templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
+                                      struct template_symbol);
+         templ_func->base.is_cplus_template_function = 1;
+         break;
+       }
+    }
+
   new = push_context (0, lowpc);
-  new->name = new_symbol (die, read_type_die (die, cu), cu);
+  new->name = new_symbol_full (die, read_type_die (die, cu), cu,
+                              (struct symbol *) templ_func);
 
   /* If there is a location expression for DW_AT_frame_base, record
      it.  */
@@ -4965,7 +5656,16 @@ read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
       child_die = die->child;
       while (child_die && child_die->tag)
        {
-         process_die (child_die, cu);
+         if (child_die->tag == DW_TAG_template_type_param
+             || child_die->tag == DW_TAG_template_value_param)
+           {
+             struct symbol *arg = new_symbol (child_die, NULL, cu);
+
+             if (arg != NULL)
+               VEC_safe_push (symbolp, template_args, arg);
+           }
+         else
+           process_die (child_die, cu);
          child_die = sibling_die (child_die);
        }
     }
@@ -5011,6 +5711,22 @@ read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
   /* If we have address ranges, record them.  */
   dwarf2_record_block_ranges (die, block, baseaddr, cu);
 
+  /* Attach template arguments to function.  */
+  if (! VEC_empty (symbolp, template_args))
+    {
+      gdb_assert (templ_func != NULL);
+
+      templ_func->n_template_arguments = VEC_length (symbolp, template_args);
+      templ_func->template_arguments
+       = obstack_alloc (&objfile->objfile_obstack,
+                        (templ_func->n_template_arguments
+                         * sizeof (struct symbol *)));
+      memcpy (templ_func->template_arguments,
+             VEC_address (symbolp, template_args),
+             (templ_func->n_template_arguments * sizeof (struct symbol *)));
+      VEC_free (symbolp, template_args);
+    }
+
   /* In C++, we can have functions nested inside functions (e.g., when
      a function declares a class that has methods).  This means that
      when we finish processing a function scope, we may need to go
@@ -5173,12 +5889,25 @@ dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
          return 0;
        }
 
+      if (range_beginning > range_end)
+       {
+         /* Inverted range entries are invalid.  */
+         complaint (&symfile_complaints,
+                    _("Invalid .debug_ranges data (inverted range)"));
+         return 0;
+       }
+
+      /* Empty range entries have no effect.  */
+      if (range_beginning == range_end)
+       continue;
+
       range_beginning += base;
       range_end += base;
 
-      if (ranges_pst != NULL && range_beginning < range_end)
+      if (ranges_pst != NULL)
        addrmap_set_empty (objfile->psymtabs_addrmap,
-                          range_beginning + baseaddr, range_end - 1 + baseaddr,
+                          range_beginning + baseaddr,
+                          range_end - 1 + baseaddr,
                           ranges_pst);
 
       /* FIXME: This is recording everything as a low-high
@@ -5253,7 +5982,8 @@ dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
        }
     }
 
-  if (high < low)
+  /* read_partial_die has also the strict LOW < HIGH requirement.  */
+  if (high <= low)
     return 0;
 
   /* When using the GNU linker, .gnu.linkonce. sections are used to
@@ -5356,7 +6086,7 @@ get_scope_pc_bounds (struct die_info *die,
              }
            break;
          default:
-           /* Ignore. */
+           /* Ignore.  */
            break;
          }
 
@@ -5453,10 +6183,24 @@ dwarf2_record_block_ranges (struct die_info *die, struct block *block,
               if (!base_known)
                 {
                   complaint (&symfile_complaints,
-                             _("Invalid .debug_ranges data (no base address)"));
+                            _("Invalid .debug_ranges data "
+                              "(no base address)"));
                   return;
                 }
 
+             if (start > end)
+               {
+                 /* Inverted range entries are invalid.  */
+                 complaint (&symfile_complaints,
+                            _("Invalid .debug_ranges data "
+                              "(inverted range)"));
+                 return;
+               }
+
+             /* Empty range entries have no effect.  */
+             if (start == end)
+               continue;
+
               record_block_range (block,
                                   baseaddr + base + start,
                                   baseaddr + base + end - 1);
@@ -5685,7 +6429,6 @@ dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
                    struct dwarf2_cu *cu)
 {
   struct objfile *objfile = cu->objfile;
-  struct gdbarch *gdbarch = get_objfile_arch (objfile);
   struct typedef_field_list *new_field;
   struct attribute *attr;
   struct typedef_field *fp;
@@ -5757,9 +6500,9 @@ dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
     }
 
-  /* Copy the saved-up fields into the field vector.  Start from the head
-     of the list, adding to the tail of the field array, so that they end
-     up in the same order in the array in which they were added to the list.  */
+  /* Copy the saved-up fields into the field vector.  Start from the head of
+     the list, adding to the tail of the field array, so that they end up in
+     the same order in the array in which they were added to the list.  */
   while (nfields-- > 0)
     {
       struct nextfield *fieldp;
@@ -5806,7 +6549,7 @@ dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
            case DW_VIRTUALITY_virtual:
            case DW_VIRTUALITY_pure_virtual:
              if (cu->language == language_ada)
-               error ("unexpected virtuality in component of Ada type");
+               error (_("unexpected virtuality in component of Ada type"));
              SET_TYPE_FIELD_VIRTUAL (type, nfields);
              break;
            }
@@ -5826,21 +6569,17 @@ dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
   int i;
   struct fn_field *fnp;
   char *fieldname;
-  char *physname;
   struct nextfnfield *new_fnfield;
   struct type *this_type;
 
   if (cu->language == language_ada)
-    error ("unexpected member function in Ada type");
+    error (_("unexpected member function in Ada type"));
 
   /* Get name of member function.  */
   fieldname = dwarf2_name (die, cu);
   if (fieldname == NULL)
     return;
 
-  /* Get the mangled name.  */
-  physname = (char *) dwarf2_physname (fieldname, die, cu);
-
   /* Look up member function name in fieldlist.  */
   for (i = 0; i < fip->nfnfields; i++)
     {
@@ -5866,11 +6605,11 @@ dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
       flp->name = fieldname;
       flp->length = 0;
       flp->head = NULL;
-      fip->nfnfields++;
+      i = fip->nfnfields++;
     }
 
   /* Create a new member function field and chain it to the field list
-     entry. */
+     entry.  */
   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
   make_cleanup (xfree, new_fnfield);
   memset (new_fnfield, 0, sizeof (struct nextfnfield));
@@ -5880,9 +6619,19 @@ dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
 
   /* Fill in the member function field info.  */
   fnp = &new_fnfield->fnfield;
-  /* The name is already allocated along with this objfile, so we don't
-     need to duplicate it for the type.  */
-  fnp->physname = physname ? physname : "";
+
+  /* Delay processing of the physname until later.  */
+  if (cu->language == language_cplus || cu->language == language_java)
+    {
+      add_to_method_list (type, i, flp->length - 1, fieldname,
+                         die, cu);
+    }
+  else
+    {
+      char *physname = (char *) dwarf2_physname (fieldname, die, cu);
+      fnp->physname = physname ? physname : "";
+    }
+
   fnp->type = alloc_type (objfile);
   this_type = read_type_die (die, cu);
   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
@@ -5899,23 +6648,23 @@ dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
 
       /* Handle static member functions.
          Dwarf2 has no clean way to discern C++ static and non-static
-         member functions. G++ helps GDB by marking the first
-         parameter for non-static member functions (which is the
-         this pointer) as artificial. We obtain this information
-         from read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
+         member functions.  G++ helps GDB by marking the first
+         parameter for non-static member functions (which is the this
+         pointer) as artificial.  We obtain this information from
+         read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
        fnp->voffset = VOFFSET_STATIC;
     }
   else
     complaint (&symfile_complaints, _("member function type missing for '%s'"),
-              physname);
+              dwarf2_full_name (fieldname, die, cu));
 
   /* Get fcontext from DW_AT_containing_type if present.  */
   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
     fnp->fcontext = die_containing_type (die, cu);
 
-  /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
-     and is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
+  /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
+     is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
 
   /* Get accessibility.  */
   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
@@ -5995,7 +6744,8 @@ dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
        {
          /* GCC does this, as of 2008-08-25; PR debug/37237.  */
          complaint (&symfile_complaints,
-                    _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
+                    _("Member function \"%s\" (offset %d) is virtual "
+                      "but the vtable offset is not specified"),
                     fieldname, die->offset);
          ALLOCATE_CPLUS_STRUCT_TYPE (type);
          TYPE_CPLUS_DYNAMIC (type) = 1;
@@ -6014,7 +6764,7 @@ dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
   int i;
 
   if (cu->language == language_ada)
-    error ("unexpected member functions in Ada type");
+    error (_("unexpected member functions in Ada type"));
 
   ALLOCATE_CPLUS_STRUCT_TYPE (type);
   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
@@ -6104,11 +6854,12 @@ quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
 }
 
 /* Called when we find the DIE that starts a structure or union scope
-   (definition) to process all dies that define the members of the
-   structure or union.
+   (definition) to create a type for the structure or union.  Fill in
+   the type's name and general properties; the members will not be
+   processed until process_structure_type.
 
-   NOTE: we need to call struct_type regardless of whether or not the
-   DIE has an at_name attribute, since it might be an anonymous
+   NOTE: we need to call these functions regardless of whether or not the
+   DIE has a DW_AT_name attribute, since it might be an anonymous
    structure or union.  This gets the type entered into our set of
    user defined types.
 
@@ -6126,7 +6877,6 @@ read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
   struct type *type;
   struct attribute *attr;
   char *name;
-  struct cleanup *back_to;
 
   /* If the definition of this type lives in .debug_types, read that type.
      Don't follow DW_AT_specification though, that will take us back up
@@ -6142,11 +6892,12 @@ read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
         This is important, for example, because for c++ classes we need
         TYPE_NAME set which is only done by new_symbol.  Blech.  */
       type = read_type_die (type_die, type_cu);
+
+      /* TYPE_CU may not be the same as CU.
+        Ensure TYPE is recorded in CU's type_hash table.  */
       return set_die_type (die, type, cu);
     }
 
-  back_to = make_cleanup (null_cleanup, 0);
-
   type = alloc_type (objfile);
   INIT_CPLUS_SPECIFIC (type);
 
@@ -6156,7 +6907,14 @@ read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
       if (cu->language == language_cplus
          || cu->language == language_java)
        {
-         TYPE_TAG_NAME (type) = (char *) dwarf2_full_name (name, die, cu);
+         char *full_name = (char *) dwarf2_full_name (name, die, cu);
+
+         /* dwarf2_full_name might have already finished building the DIE's
+            type.  If so, there is no need to continue.  */
+         if (get_die_type (die, cu) != NULL)
+           return get_die_type (die, cu);
+
+         TYPE_TAG_NAME (type) = full_name;
          if (die->tag == DW_TAG_structure_type
              || die->tag == DW_TAG_class_type)
            TYPE_NAME (type) = TYPE_TAG_NAME (type);
@@ -6208,16 +6966,35 @@ read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
 
   /* We need to add the type field to the die immediately so we don't
      infinitely recurse when dealing with pointers to the structure
-     type within the structure itself. */
+     type within the structure itself.  */
   set_die_type (die, type, cu);
 
   /* set_die_type should be already done.  */
   set_descriptive_type (type, die, cu);
 
+  return type;
+}
+
+/* Finish creating a structure or union type, including filling in
+   its members and creating a symbol for it.  */
+
+static void
+process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
+{
+  struct objfile *objfile = cu->objfile;
+  struct die_info *child_die = die->child;
+  struct type *type;
+
+  type = get_die_type (die, cu);
+  if (type == NULL)
+    type = read_structure_type (die, cu);
+
   if (die->child != NULL && ! die_is_declaration (die, cu))
     {
       struct field_info fi;
       struct die_info *child_die;
+      VEC (symbolp) *template_args = NULL;
+      struct cleanup *back_to = make_cleanup (null_cleanup, 0);
 
       memset (&fi, 0, sizeof (struct field_info));
 
@@ -6237,7 +7014,7 @@ read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
            }
          else if (child_die->tag == DW_TAG_subprogram)
            {
-             /* C++ member function. */
+             /* C++ member function.  */
              dwarf2_add_member_fn (&fi, child_die, type, cu);
            }
          else if (child_die->tag == DW_TAG_inheritance)
@@ -6247,9 +7024,35 @@ read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
            }
          else if (child_die->tag == DW_TAG_typedef)
            dwarf2_add_typedef (&fi, child_die, cu);
+         else if (child_die->tag == DW_TAG_template_type_param
+                  || child_die->tag == DW_TAG_template_value_param)
+           {
+             struct symbol *arg = new_symbol (child_die, NULL, cu);
+
+             if (arg != NULL)
+               VEC_safe_push (symbolp, template_args, arg);
+           }
+
          child_die = sibling_die (child_die);
        }
 
+      /* Attach template arguments to type.  */
+      if (! VEC_empty (symbolp, template_args))
+       {
+         ALLOCATE_CPLUS_STRUCT_TYPE (type);
+         TYPE_N_TEMPLATE_ARGUMENTS (type)
+           = VEC_length (symbolp, template_args);
+         TYPE_TEMPLATE_ARGUMENTS (type)
+           = obstack_alloc (&objfile->objfile_obstack,
+                            (TYPE_N_TEMPLATE_ARGUMENTS (type)
+                             * sizeof (struct symbol *)));
+         memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
+                 VEC_address (symbolp, template_args),
+                 (TYPE_N_TEMPLATE_ARGUMENTS (type)
+                  * sizeof (struct symbol *)));
+         VEC_free (symbolp, template_args);
+       }
+
       /* Attach fields and member functions to the type.  */
       if (fi.nfields)
        dwarf2_attach_fields_to_type (&fi, type, cu);
@@ -6288,7 +7091,8 @@ read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
                  /* Complain if virtual function table field not found.  */
                  if (i < TYPE_N_BASECLASSES (t))
                    complaint (&symfile_complaints,
-                              _("virtual function table pointer not found when defining class '%s'"),
+                              _("virtual function table pointer "
+                                "not found when defining class '%s'"),
                               TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
                               "");
                }
@@ -6343,24 +7147,12 @@ read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
              *dest = *src;
            }
        }
+
+      do_cleanups (back_to);
     }
 
   quirk_gcc_member_function_pointer (type, cu->objfile);
 
-  do_cleanups (back_to);
-  return type;
-}
-
-static void
-process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
-{
-  struct die_info *child_die = die->child;
-  struct type *this_type;
-
-  this_type = get_die_type (die, cu);
-  if (this_type == NULL)
-    this_type = read_structure_type (die, cu);
-
   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
      snapshots) has been known to create a die giving a declaration
      for a class that has, as a child, a die giving a definition for a
@@ -6372,7 +7164,9 @@ process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
     {
       if (child_die->tag == DW_TAG_member
          || child_die->tag == DW_TAG_variable
-         || child_die->tag == DW_TAG_inheritance)
+         || child_die->tag == DW_TAG_inheritance
+         || child_die->tag == DW_TAG_template_value_param
+         || child_die->tag == DW_TAG_template_type_param)
        {
          /* Do nothing.  */
        }
@@ -6387,7 +7181,7 @@ process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
      attribute, and a declaration attribute.  */
   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
       || !die_is_declaration (die, cu))
-    new_symbol (die, this_type, cu);
+    new_symbol (die, type, cu);
 }
 
 /* Given a DW_AT_enumeration_type die, set its type.  We do not
@@ -6411,6 +7205,9 @@ read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
 
       type = read_type_die (type_die, type_cu);
+
+      /* TYPE_CU may not be the same as CU.
+        Ensure TYPE is recorded in CU's type_hash table.  */
       return set_die_type (die, type, cu);
     }
 
@@ -6451,21 +7248,21 @@ read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
 static void
 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
 {
-  struct die_info *child_die;
-  struct field *fields;
-  struct symbol *sym;
-  int num_fields;
-  int unsigned_enum = 1;
-  char *name;
   struct type *this_type;
 
-  num_fields = 0;
-  fields = NULL;
   this_type = get_die_type (die, cu);
   if (this_type == NULL)
     this_type = read_enumeration_type (die, cu);
+
   if (die->child != NULL)
     {
+      struct die_info *child_die;
+      struct symbol *sym;
+      struct field *fields = NULL;
+      int num_fields = 0;
+      int unsigned_enum = 1;
+      char *name;
+
       child_die = die->child;
       while (child_die && child_die->tag)
        {
@@ -6562,8 +7359,8 @@ read_array_type (struct die_info *die, struct dwarf2_cu *cu)
 
           if (child_type != NULL)
             {
-             /* The range type was succesfully read. Save it for
-                 the array type creation.  */
+             /* The range type was succesfully read.  Save it for the
+                 array type creation.  */
               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
                 {
                   range_types = (struct type **)
@@ -6604,13 +7401,27 @@ read_array_type (struct die_info *die, struct dwarf2_cu *cu)
      to functions.  */
   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
   if (attr)
-    make_vector_type (type);
+    make_vector_type (type);
+
+  /* The DIE may have DW_AT_byte_size set.  For example an OpenCL
+     implementation may choose to implement triple vectors using this
+     attribute.  */
+  attr = dwarf2_attr (die, DW_AT_byte_size, cu);
+  if (attr)
+    {
+      if (DW_UNSND (attr) >= TYPE_LENGTH (type))
+       TYPE_LENGTH (type) = DW_UNSND (attr);
+      else
+       complaint (&symfile_complaints,
+                  _("DW_AT_byte_size for array type smaller "
+                    "than the total size of elements"));
+    }
 
   name = dwarf2_name (die, cu);
   if (name)
     TYPE_NAME (type) = name;
 
-  /* Install the type in the die. */
+  /* Install the type in the die.  */
   set_die_type (die, type, cu);
 
   /* set_die_type should be already done.  */
@@ -6630,14 +7441,12 @@ read_array_order (struct die_info *die, struct dwarf2_cu *cu)
 
   if (attr) return DW_SND (attr);
 
-  /*
-    GNU F77 is a special case, as at 08/2004 array type info is the
-    opposite order to the dwarf2 specification, but data is still
-    laid out as per normal fortran.
+  /* GNU F77 is a special case, as at 08/2004 array type info is the
+     opposite order to the dwarf2 specification, but data is still
+     laid out as per normal fortran.
 
-    FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
-    version checking.
-  */
+     FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
+     version checking.  */
 
   if (cu->language == language_fortran
       && cu->producer && strstr (cu->producer, "GNU F77"))
@@ -6656,7 +7465,7 @@ read_array_order (struct die_info *die, struct dwarf2_cu *cu)
 }
 
 /* Extract all information from a DW_TAG_set_type DIE and put it in
-   the DIE's type field. */
+   the DIE's type field.  */
 
 static struct type *
 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
@@ -6693,7 +7502,7 @@ read_common_block (struct die_info *die, struct dwarf2_cu *cu)
   attr = dwarf2_attr (die, DW_AT_location, cu);
   if (attr)
     {
-      /* Support the .debug_loc offsets */
+      /* Support the .debug_loc offsets */
       if (attr_form_is_block (attr))
         {
           base = decode_locdesc (DW_BLOCK (attr), cu);
@@ -6715,7 +7524,7 @@ read_common_block (struct die_info *die, struct dwarf2_cu *cu)
        {
          sym = new_symbol (child_die, NULL, cu);
          attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
-         if (attr)
+         if (sym != NULL && attr != NULL)
            {
              CORE_ADDR byte_offset = 0;
 
@@ -6754,6 +7563,9 @@ read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
 
       ext_die = dwarf2_extension (die, &ext_cu);
       type = read_type_die (ext_die, ext_cu);
+
+      /* EXT_CU may not be the same as CU.
+        Ensure TYPE is recorded in CU's type_hash table.  */
       return set_die_type (die, type, cu);
     }
 
@@ -6781,7 +7593,6 @@ static void
 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
 {
   struct objfile *objfile = cu->objfile;
-  const char *name;
   int is_anonymous;
 
   /* Add a symbol associated to this if we haven't seen the namespace
@@ -6795,7 +7606,7 @@ read_namespace (struct die_info *die, struct dwarf2_cu *cu)
       type = read_type_die (die, cu);
       new_symbol (die, type, cu);
 
-      name = namespace_name (die, &is_anonymous, cu);
+      namespace_name (die, &is_anonymous, cu);
       if (is_anonymous)
        {
          const char *previous_prefix = determine_prefix (die, cu);
@@ -6830,7 +7641,8 @@ read_module_type (struct die_info *die, struct dwarf2_cu *cu)
 
   module_name = dwarf2_name (die, cu);
   if (!module_name)
-    complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
+    complaint (&symfile_complaints,
+              _("DW_TAG_module has no name, offset 0x%x"),
                die->offset);
   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
 
@@ -6936,7 +7748,8 @@ read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
        }
       else if (TYPE_LENGTH (type) != byte_size)
        {
-         complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
+         complaint (&symfile_complaints,
+                    _("invalid pointer size %d"), byte_size);
        }
       else
        {
@@ -7016,6 +7829,29 @@ read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
   if (cv_type)
     return cv_type;
 
+  /* In case the const qualifier is applied to an array type, the element type
+     is so qualified, not the array type (section 6.7.3 of C99).  */
+  if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
+    {
+      struct type *el_type, *inner_array;
+
+      base_type = copy_type (base_type);
+      inner_array = base_type;
+
+      while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
+       {
+         TYPE_TARGET_TYPE (inner_array) =
+           copy_type (TYPE_TARGET_TYPE (inner_array));
+         inner_array = TYPE_TARGET_TYPE (inner_array);
+       }
+
+      el_type = TYPE_TARGET_TYPE (inner_array);
+      TYPE_TARGET_TYPE (inner_array) =
+       make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
+
+      return set_die_type (die, base_type, cu);
+    }
+
   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
   return set_die_type (die, cv_type, cu);
 }
@@ -7057,7 +7893,7 @@ read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
     }
   else
     {
-      /* check for the DW_AT_byte_size attribute */
+      /* Check for the DW_AT_byte_size attribute.  */
       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
       if (attr)
         {
@@ -7085,14 +7921,13 @@ read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
    int b;
    };
 
-   ('funcp' generates a DW_TAG_subroutine_type DIE)
- */
+   ('funcp' generates a DW_TAG_subroutine_type DIE).  */
 
 static struct type *
 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
 {
-  struct type *type;           /* Type that this function returns */
-  struct type *ftype;          /* Function that returns above type */
+  struct type *type;           /* Type that this function returns */
+  struct type *ftype;          /* Function that returns above type */
   struct attribute *attr;
 
   type = die_type (die, cu);
@@ -7122,11 +7957,16 @@ read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
      the subroutine die.  Otherwise set the calling convention to
      the default value DW_CC_normal.  */
   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
-  TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
+  if (attr)
+    TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
+  else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
+    TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
+  else
+    TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
 
   /* We need to add the subroutine type to the die immediately so
      we don't infinitely recurse when dealing with parameters
-     declared as the same subroutine type. */
+     declared as the same subroutine type.  */
   set_die_type (die, ftype, cu);
 
   if (die->child != NULL)
@@ -7165,11 +8005,17 @@ read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
        {
          if (child_die->tag == DW_TAG_formal_parameter)
            {
-             /* Dwarf2 has no clean way to discern C++ static and non-static
-                member functions. G++ helps GDB by marking the first
-                parameter for non-static member functions (which is the
-                this pointer) as artificial. We pass this information
-                to dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.  */
+             struct type *arg_type;
+
+             /* DWARF version 2 has no clean way to discern C++
+                static and non-static member functions.  G++ helps
+                GDB by marking the first parameter for non-static
+                member functions (which is the this pointer) as
+                artificial.  We pass this information to
+                dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
+
+                DWARF version 3 added DW_AT_object_pointer, which GCC
+                4.5 does not yet generate.  */
              attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
              if (attr)
                TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
@@ -7187,7 +8033,40 @@ read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
                        TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
                    }
                }
-             TYPE_FIELD_TYPE (ftype, iparams) = die_type (child_die, cu);
+             arg_type = die_type (child_die, cu);
+
+             /* RealView does not mark THIS as const, which the testsuite
+                expects.  GCC marks THIS as const in method definitions,
+                but not in the class specifications (GCC PR 43053).  */
+             if (cu->language == language_cplus && !TYPE_CONST (arg_type)
+                 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
+               {
+                 int is_this = 0;
+                 struct dwarf2_cu *arg_cu = cu;
+                 const char *name = dwarf2_name (child_die, cu);
+
+                 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
+                 if (attr)
+                   {
+                     /* If the compiler emits this, use it.  */
+                     if (follow_die_ref (die, attr, &arg_cu) == child_die)
+                       is_this = 1;
+                   }
+                 else if (name && strcmp (name, "this") == 0)
+                   /* Function definitions will have the argument names.  */
+                   is_this = 1;
+                 else if (name == NULL && iparams == 0)
+                   /* Declarations may not have the names, so like
+                      elsewhere in GDB, assume an artificial first
+                      argument is "this".  */
+                   is_this = 1;
+
+                 if (is_this)
+                   arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
+                                            arg_type, 0);
+               }
+
+             TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
              iparams++;
            }
          child_die = sibling_die (child_die);
@@ -7309,7 +8188,6 @@ read_base_type (struct die_info *die, struct dwarf2_cu *cu)
 static struct type *
 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
 {
-  struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
   struct type *base_type;
   struct type *range_type;
   struct attribute *attr;
@@ -7369,6 +8247,11 @@ read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
          int count = dwarf2_get_attr_constant_value (attr, 1);
          high = low + count - 1;
        }
+      else
+       {
+         /* Unspecified array length.  */
+         high = low - 1;
+       }
     }
 
   /* Dwarf-2 specifications explicitly allows to create subrange types
@@ -7382,8 +8265,7 @@ read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
      For the following C code: `extern char gdb_int [];'
      GCC produces an empty range DIE.
      FIXME: muller/2010-05-28: Possible references to object for low bound,
-     high bound or count are not yet handled by this code.
-  */
+     high bound or count are not yet handled by this code.  */
   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
     {
       struct objfile *objfile = cu->objfile;
@@ -7425,6 +8307,10 @@ read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
   if (attr && attr->form == DW_FORM_block1)
     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
 
+  /* Ada expects an empty array on no boundary attributes.  */
+  if (attr == NULL && cu->language != language_ada)
+    TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
+
   name = dwarf2_name (die, cu);
   if (name)
     TYPE_NAME (range_type) = name;
@@ -7483,6 +8369,16 @@ static struct die_info *
 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
 {
   struct die_reader_specs reader_specs;
+  int read_abbrevs = 0;
+  struct cleanup *back_to = NULL;
+  struct die_info *die;
+
+  if (cu->dwarf2_abbrevs == NULL)
+    {
+      dwarf2_read_abbrevs (cu->objfile->obfd, cu);
+      back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
+      read_abbrevs = 1;
+    }
 
   gdb_assert (cu->die_hash == NULL);
   cu->die_hash
@@ -7496,7 +8392,12 @@ read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
 
   init_cu_die_reader (&reader_specs, cu);
 
-  return read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
+  die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
+
+  if (read_abbrevs)
+    do_cleanups (back_to);
+
+  return die;
 }
 
 /* Main entry point for reading a DIE and all children.
@@ -7665,7 +8566,7 @@ dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
   struct attr_abbrev *cur_attrs;
   unsigned int allocated_attrs;
 
-  /* Initialize dwarf2 abbrevs */
+  /* Initialize dwarf2 abbrevs */
   obstack_init (&cu->abbrev_obstack);
   cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
                                      (ABBREV_HASH_SIZE
@@ -7682,7 +8583,7 @@ dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
   allocated_attrs = ATTR_ALLOC_CHUNK;
   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
 
-  /* loop until we reach an abbrev number of 0 */
+  /* Loop until we reach an abbrev number of 0.  */
   while (abbrev_number)
     {
       cur_abbrev = dwarf_alloc_abbrev (cu);
@@ -7878,12 +8779,42 @@ load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
          continue;
        }
 
+      /* Check for template arguments.  We never save these; if
+        they're seen, we just mark the parent, and go on our way.  */
+      if (parent_die != NULL
+         && cu->language == language_cplus
+         && (abbrev->tag == DW_TAG_template_type_param
+             || abbrev->tag == DW_TAG_template_value_param))
+       {
+         parent_die->has_template_arguments = 1;
+
+         if (!load_all)
+           {
+             /* We don't need a partial DIE for the template argument.  */
+             info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
+                                      cu);
+             continue;
+           }
+       }
+
+      /* We only recurse into subprograms looking for template arguments.
+        Skip their other children.  */
+      if (!load_all
+         && cu->language == language_cplus
+         && parent_die != NULL
+         && parent_die->tag == DW_TAG_subprogram)
+       {
+         info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
+         continue;
+       }
+
       /* Check whether this DIE is interesting enough to save.  Normally
         we would not be interested in members here, but there may be
         later variables referencing them via DW_AT_specification (for
         static members).  */
       if (!load_all
          && !is_type_tag_for_partial (abbrev->tag)
+         && abbrev->tag != DW_TAG_constant
          && abbrev->tag != DW_TAG_enumerator
          && abbrev->tag != DW_TAG_subprogram
          && abbrev->tag != DW_TAG_lexical_block
@@ -7943,7 +8874,8 @@ load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
          && parent_die->has_specification == 0)
        {
          if (part_die->name == NULL)
-           complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
+           complaint (&symfile_complaints,
+                      _("malformed enumerator DIE ignored"));
          else if (building_psymtab)
            add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
                                 VAR_DOMAIN, LOC_CONST,
@@ -7994,6 +8926,7 @@ load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
         unit with load_all_dies set.  */
 
       if (load_all
+         || abbrev->tag == DW_TAG_constant
          || abbrev->tag == DW_TAG_subprogram
          || abbrev->tag == DW_TAG_variable
          || abbrev->tag == DW_TAG_namespace
@@ -8011,8 +8944,11 @@ load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
 
       /* For some DIEs we want to follow their children (if any).  For C
         we have no reason to follow the children of structures; for other
-        languages we have to, both so that we can get at method physnames
-        to infer fully qualified class names, and for DW_AT_specification.
+        languages we have to, so that we can get at method physnames
+        to infer fully qualified class names, for DW_AT_specification,
+        and for C++ template arguments.  For C++, we also look one level
+        inside functions to find template arguments (if the name of the
+        function does not already contain the template arguments).
 
         For Ada, we need to scan the children of subprograms and lexical
         blocks as well because Ada allows the definition of nested
@@ -8023,6 +8959,10 @@ load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
              || last_die->tag == DW_TAG_namespace
              || last_die->tag == DW_TAG_module
              || last_die->tag == DW_TAG_enumeration_type
+             || (cu->language == language_cplus
+                 && last_die->tag == DW_TAG_subprogram
+                 && (last_die->name == NULL
+                     || strchr (last_die->name, '<') == NULL))
              || (cu->language != language_c
                  && (last_die->tag == DW_TAG_class_type
                      || last_die->tag == DW_TAG_interface_type
@@ -8105,6 +9045,7 @@ read_partial_die (struct partial_die_info *part_die,
             one we see.  */
          if (cu->language == language_ada)
            part_die->name = DW_STRING (&attr);
+         part_die->linkage_name = DW_STRING (&attr);
          break;
        case DW_AT_low_pc:
          has_low_pc_attr = 1;
@@ -8115,7 +9056,7 @@ read_partial_die (struct partial_die_info *part_die,
          part_die->highpc = DW_ADDR (&attr);
          break;
        case DW_AT_location:
-          /* Support the .debug_loc offsets */
+          /* Support the .debug_loc offsets */
           if (attr_form_is_block (&attr))
             {
               part_die->locdesc = DW_BLOCK (&attr);
@@ -8149,7 +9090,8 @@ read_partial_die (struct partial_die_info *part_die,
          /* Ignore absolute siblings, they might point outside of
             the current compile unit.  */
          if (attr.form == DW_FORM_ref_addr)
-           complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
+           complaint (&symfile_complaints,
+                      _("ignoring absolute DW_AT_sibling"));
          else
            part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
          break;
@@ -8173,26 +9115,54 @@ read_partial_die (struct partial_die_info *part_die,
             practice.  */
          if (DW_UNSND (&attr) == DW_CC_program
              && cu->language == language_fortran)
-           set_main_name (part_die->name);
+           {
+             set_main_name (part_die->name);
+
+             /* As this DIE has a static linkage the name would be difficult
+                to look up later.  */
+             language_of_main = language_fortran;
+           }
          break;
        default:
          break;
        }
     }
 
-  /* When using the GNU linker, .gnu.linkonce. sections are used to
-     eliminate duplicate copies of functions and vtables and such.
-     The linker will arbitrarily choose one and discard the others.
-     The AT_*_pc values for such functions refer to local labels in
-     these sections.  If the section from that file was discarded, the
-     labels are not in the output, so the relocs get a value of 0.
-     If this is a discarded function, mark the pc bounds as invalid,
-     so that GDB will ignore it.  */
-  if (has_low_pc_attr && has_high_pc_attr
-      && part_die->lowpc < part_die->highpc
-      && (part_die->lowpc != 0
-         || dwarf2_per_objfile->has_section_at_zero))
-    part_die->has_pc_info = 1;
+  if (has_low_pc_attr && has_high_pc_attr)
+    {
+      /* When using the GNU linker, .gnu.linkonce. sections are used to
+        eliminate duplicate copies of functions and vtables and such.
+        The linker will arbitrarily choose one and discard the others.
+        The AT_*_pc values for such functions refer to local labels in
+        these sections.  If the section from that file was discarded, the
+        labels are not in the output, so the relocs get a value of 0.
+        If this is a discarded function, mark the pc bounds as invalid,
+        so that GDB will ignore it.  */
+      if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
+       {
+         struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
+
+         complaint (&symfile_complaints,
+                    _("DW_AT_low_pc %s is zero "
+                      "for DIE at 0x%x [in module %s]"),
+                    paddress (gdbarch, part_die->lowpc),
+                    part_die->offset, cu->objfile->name);
+       }
+      /* dwarf2_get_pc_bounds has also the strict low < high requirement.  */
+      else if (part_die->lowpc >= part_die->highpc)
+       {
+         struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
+
+         complaint (&symfile_complaints,
+                    _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
+                      "for DIE at 0x%x [in module %s]"),
+                    paddress (gdbarch, part_die->lowpc),
+                    paddress (gdbarch, part_die->highpc),
+                    part_die->offset, cu->objfile->name);
+       }
+      else
+       part_die->has_pc_info = 1;
+    }
 
   return info_ptr;
 }
@@ -8239,12 +9209,8 @@ find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
 
   per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
 
-  if (per_cu->cu == NULL)
-    {
-      load_partial_comp_unit (per_cu, cu->objfile);
-      per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
-      dwarf2_per_objfile->read_in_chain = per_cu;
-    }
+  if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
+    load_partial_comp_unit (per_cu, cu->objfile);
 
   per_cu->cu->last_used = 0;
   pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
@@ -8287,11 +9253,63 @@ find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
 
   if (pd == NULL)
     internal_error (__FILE__, __LINE__,
-                   _("could not find partial DIE 0x%x in cache [from module %s]\n"),
+                   _("could not find partial DIE 0x%x "
+                     "in cache [from module %s]\n"),
                    offset, bfd_get_filename (cu->objfile->obfd));
   return pd;
 }
 
+/* See if we can figure out if the class lives in a namespace.  We do
+   this by looking for a member function; its demangled name will
+   contain namespace info, if there is any.  */
+
+static void
+guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
+                                 struct dwarf2_cu *cu)
+{
+  /* NOTE: carlton/2003-10-07: Getting the info this way changes
+     what template types look like, because the demangler
+     frequently doesn't give the same name as the debug info.  We
+     could fix this by only using the demangled name to get the
+     prefix (but see comment in read_structure_type).  */
+
+  struct partial_die_info *real_pdi;
+  struct partial_die_info *child_pdi;
+
+  /* If this DIE (this DIE's specification, if any) has a parent, then
+     we should not do this.  We'll prepend the parent's fully qualified
+     name when we create the partial symbol.  */
+
+  real_pdi = struct_pdi;
+  while (real_pdi->has_specification)
+    real_pdi = find_partial_die (real_pdi->spec_offset, cu);
+
+  if (real_pdi->die_parent != NULL)
+    return;
+
+  for (child_pdi = struct_pdi->die_child;
+       child_pdi != NULL;
+       child_pdi = child_pdi->die_sibling)
+    {
+      if (child_pdi->tag == DW_TAG_subprogram
+         && child_pdi->linkage_name != NULL)
+       {
+         char *actual_class_name
+           = language_class_name_from_physname (cu->language_defn,
+                                                child_pdi->linkage_name);
+         if (actual_class_name != NULL)
+           {
+             struct_pdi->name
+               = obsavestring (actual_class_name,
+                               strlen (actual_class_name),
+                               &cu->objfile->objfile_obstack);
+             xfree (actual_class_name);
+           }
+         break;
+       }
+    }
+}
+
 /* Adjust PART_DIE before generating a symbol for it.  This function
    may set the is_external flag or change the DIE's name.  */
 
@@ -8299,6 +9317,12 @@ static void
 fixup_partial_die (struct partial_die_info *part_die,
                   struct dwarf2_cu *cu)
 {
+  /* Once we've fixed up a die, there's no point in doing so again.
+     This also avoids a memory leak if we were to call
+     guess_partial_die_structure_name multiple times.  */
+  if (part_die->fixup_called)
+    return;
+
   /* If we found a reference attribute and the DIE has no name, try
      to find a name in the referred to DIE.  */
 
@@ -8321,17 +9345,25 @@ fixup_partial_die (struct partial_die_info *part_die,
     }
 
   /* Set default names for some unnamed DIEs.  */
-  if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
-                                || part_die->tag == DW_TAG_class_type))
-    part_die->name = "(anonymous class)";
 
   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
     part_die->name = "(anonymous namespace)";
 
-  if (part_die->tag == DW_TAG_structure_type
-      || part_die->tag == DW_TAG_class_type
-      || part_die->tag == DW_TAG_union_type)
-    guess_structure_name (part_die, cu);
+  /* If there is no parent die to provide a namespace, and there are
+     children, see if we can determine the namespace from their linkage
+     name.
+     NOTE: We need to do this even if cu->has_namespace_info != 0.
+     gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  */
+  if (cu->language == language_cplus
+      && dwarf2_per_objfile->types.asection != NULL
+      && part_die->die_parent == NULL
+      && part_die->has_children
+      && (part_die->tag == DW_TAG_class_type
+         || part_die->tag == DW_TAG_structure_type
+         || part_die->tag == DW_TAG_union_type))
+    guess_partial_die_structure_name (part_die, cu);
+
+  part_die->fixup_called = 1;
 }
 
 /* Read an attribute value described by an attribute form.  */
@@ -8352,7 +9384,8 @@ read_attribute_value (struct attribute *attr, unsigned form,
       if (cu->header.version == 2)
        DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
       else
-       DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
+       DW_ADDR (attr) = read_offset (abfd, info_ptr,
+                                     &cu->header, &bytes_read);
       info_ptr += bytes_read;
       break;
     case DW_FORM_addr:
@@ -8392,7 +9425,7 @@ read_attribute_value (struct attribute *attr, unsigned form,
       info_ptr += bytes_read;
       break;
     case DW_FORM_string:
-      DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
+      DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
       DW_STRING_IS_CANONICAL (attr) = 0;
       info_ptr += bytes_read;
       break;
@@ -8508,7 +9541,7 @@ read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
   return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
 }
 
-/* read dwarf information from a buffer */
+/* Read dwarf information from a buffer.  */
 
 static unsigned int
 read_1_byte (bfd *abfd, gdb_byte *buf)
@@ -8593,7 +9626,8 @@ read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
          break;
        default:
          internal_error (__FILE__, __LINE__,
-                         _("read_address: bad switch, unsigned [in module %s]"),
+                         _("read_address: bad switch, "
+                           "unsigned [in module %s]"),
                          bfd_get_filename (abfd));
        }
     }
@@ -8740,7 +9774,7 @@ read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
 }
 
 static char *
-read_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
+read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
 {
   /* If the size of a host char is 8 bits, we can return a pointer
      to the string, otherwise we have to copy the string to a buffer
@@ -8771,8 +9805,9 @@ read_indirect_string (bfd *abfd, gdb_byte *buf,
     }
   if (str_offset >= dwarf2_per_objfile->str.size)
     {
-      error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
-                     bfd_get_filename (abfd));
+      error (_("DW_FORM_strp pointing outside of "
+              ".debug_str section [in module %s]"),
+            bfd_get_filename (abfd));
       return NULL;
     }
   gdb_assert (HOST_CHAR_BIT == 8);
@@ -8996,7 +10031,9 @@ die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
 }
 
 /* Free the line_header structure *LH, and any arrays and strings it
-   refers to.  */
+   refers to.
+   NOTE: This is also used as a "cleanup" function.  */
+
 static void
 free_line_header (struct line_header *lh)
 {
@@ -9015,8 +10052,8 @@ free_line_header (struct line_header *lh)
   xfree (lh);
 }
 
-
 /* Add an entry to LH's include directory table.  */
+
 static void
 add_include_dir (struct line_header *lh, char *include_dir)
 {
@@ -9038,8 +10075,8 @@ add_include_dir (struct line_header *lh, char *include_dir)
   lh->include_dirs[lh->num_include_dirs++] = include_dir;
 }
 
-
 /* Add an entry to LH's file name table.  */
+
 static void
 add_file_name (struct line_header *lh,
                char *name,
@@ -9073,7 +10110,6 @@ add_file_name (struct line_header *lh,
   fe->symtab = NULL;
 }
 
-
 /* Read the statement program header starting at OFFSET in
    .debug_line, according to the endianness of ABFD.  Return a pointer
    to a struct line_header, allocated using xmalloc.
@@ -9081,6 +10117,7 @@ add_file_name (struct line_header *lh,
    NOTE: the strings in the include directory and file name tables of
    the returned object point into debug_line_buffer, and must not be
    freed.  */
+
 static struct line_header *
 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
                          struct dwarf2_cu *cu)
@@ -9144,7 +10181,8 @@ dwarf_decode_line_header (unsigned int offset, bfd *abfd,
     {
       lh->maximum_ops_per_instruction = 1;
       complaint (&symfile_complaints,
-                _("invalid maximum_ops_per_instruction in `.debug_line' section"));
+                _("invalid maximum_ops_per_instruction "
+                  "in `.debug_line' section"));
     }
 
   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
@@ -9166,7 +10204,7 @@ dwarf_decode_line_header (unsigned int offset, bfd *abfd,
     }
 
   /* Read directory table.  */
-  while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
+  while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
     {
       line_ptr += bytes_read;
       add_include_dir (lh, cur_dir);
@@ -9174,7 +10212,7 @@ dwarf_decode_line_header (unsigned int offset, bfd *abfd,
   line_ptr += bytes_read;
 
   /* Read file name table.  */
-  while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
+  while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
     {
       unsigned int dir_index, mod_time, length;
 
@@ -9194,7 +10232,8 @@ dwarf_decode_line_header (unsigned int offset, bfd *abfd,
   if (line_ptr > (dwarf2_per_objfile->line.buffer
                  + dwarf2_per_objfile->line.size))
     complaint (&symfile_complaints,
-              _("line number info header doesn't fit in `.debug_line' section"));
+              _("line number info header doesn't "
+                "fit in `.debug_line' section"));
 
   discard_cleanups (back_to);
   return lh;
@@ -9246,26 +10285,109 @@ check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
   return fn->lowpc;
 }
 
+/* Subroutine of dwarf_decode_lines to simplify it.
+   Return the file name of the psymtab for included file FILE_INDEX
+   in line header LH of PST.
+   COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
+   If space for the result is malloc'd, it will be freed by a cleanup.
+   Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.  */
+
+static char *
+psymtab_include_file_name (const struct line_header *lh, int file_index,
+                          const struct partial_symtab *pst,
+                          const char *comp_dir)
+{
+  const struct file_entry fe = lh->file_names [file_index];
+  char *include_name = fe.name;
+  char *include_name_to_compare = include_name;
+  char *dir_name = NULL;
+  const char *pst_filename;
+  char *copied_name = NULL;
+  int file_is_pst;
+
+  if (fe.dir_index)
+    dir_name = lh->include_dirs[fe.dir_index - 1];
+
+  if (!IS_ABSOLUTE_PATH (include_name)
+      && (dir_name != NULL || comp_dir != NULL))
+    {
+      /* Avoid creating a duplicate psymtab for PST.
+        We do this by comparing INCLUDE_NAME and PST_FILENAME.
+        Before we do the comparison, however, we need to account
+        for DIR_NAME and COMP_DIR.
+        First prepend dir_name (if non-NULL).  If we still don't
+        have an absolute path prepend comp_dir (if non-NULL).
+        However, the directory we record in the include-file's
+        psymtab does not contain COMP_DIR (to match the
+        corresponding symtab(s)).
+
+        Example:
+
+        bash$ cd /tmp
+        bash$ gcc -g ./hello.c
+        include_name = "hello.c"
+        dir_name = "."
+        DW_AT_comp_dir = comp_dir = "/tmp"
+        DW_AT_name = "./hello.c"  */
+
+      if (dir_name != NULL)
+       {
+         include_name = concat (dir_name, SLASH_STRING,
+                                include_name, (char *)NULL);
+         include_name_to_compare = include_name;
+         make_cleanup (xfree, include_name);
+       }
+      if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
+       {
+         include_name_to_compare = concat (comp_dir, SLASH_STRING,
+                                           include_name, (char *)NULL);
+       }
+    }
+
+  pst_filename = pst->filename;
+  if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
+    {
+      copied_name = concat (pst->dirname, SLASH_STRING,
+                           pst_filename, (char *)NULL);
+      pst_filename = copied_name;
+    }
+
+  file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
+
+  if (include_name_to_compare != include_name)
+    xfree (include_name_to_compare);
+  if (copied_name != NULL)
+    xfree (copied_name);
+
+  if (file_is_pst)
+    return NULL;
+  return include_name;
+}
+
 /* Decode the Line Number Program (LNP) for the given line_header
    structure and CU.  The actual information extracted and the type
    of structures created from the LNP depends on the value of PST.
 
    1. If PST is NULL, then this procedure uses the data from the program
       to create all necessary symbol tables, and their linetables.
-      The compilation directory of the file is passed in COMP_DIR,
-      and must not be NULL.
 
    2. If PST is not NULL, this procedure reads the program to determine
       the list of files included by the unit represented by PST, and
-      builds all the associated partial symbol tables.  In this case,
-      the value of COMP_DIR is ignored, and can thus be NULL (the COMP_DIR
-      is not used to compute the full name of the symtab, and therefore
-      omitting it when building the partial symtab does not introduce
-      the potential for inconsistency - a partial symtab and its associated
-      symbtab having a different fullname -).  */
+      builds all the associated partial symbol tables.
+
+   COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
+   It is used for relative paths in the line table.
+   NOTE: When processing partial symtabs (pst != NULL),
+   comp_dir == pst->dirname.
+
+   NOTE: It is important that psymtabs have the same file name (via strcmp)
+   as the corresponding symtab.  Since COMP_DIR is not used in the name of the
+   symtab we don't use it in the name of the psymtabs we create.
+   E.g. expand_line_sal requires this when finding psymtabs to expand.
+   A good testcase for this is mb-inline.exp.  */
 
 static void
-dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
+dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
                    struct dwarf2_cu *cu, struct partial_symtab *pst)
 {
   gdb_byte *line_ptr, *extended_end;
@@ -9360,7 +10482,8 @@ dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
          else switch (op_code)
            {
            case DW_LNS_extended_op:
-             extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
+             extended_len = read_unsigned_leb128 (abfd, line_ptr,
+                                                  &bytes_read);
              line_ptr += bytes_read;
              extended_end = line_ptr + extended_len;
              extended_op = read_1_byte (abfd, line_ptr);
@@ -9381,7 +10504,8 @@ dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
                     char *cur_file;
                     unsigned int dir_index, mod_time, length;
 
-                    cur_file = read_string (abfd, line_ptr, &bytes_read);
+                    cur_file = read_direct_string (abfd, line_ptr,
+                                                  &bytes_read);
                     line_ptr += bytes_read;
                     dir_index =
                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
@@ -9493,7 +10617,7 @@ dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
               address increment value corresponding to special opcode
               255.  I.e., this value is scaled by the minimum
               instruction length since special opcode 255 would have
-              scaled the the increment.  */
+              scaled the increment.  */
            case DW_LNS_const_add_pc:
              {
                CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
@@ -9545,29 +10669,9 @@ dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
       for (file_index = 0; file_index < lh->num_file_names; file_index++)
         if (lh->file_names[file_index].included_p == 1)
           {
-            const struct file_entry fe = lh->file_names [file_index];
-            char *include_name = fe.name;
-            char *dir_name = NULL;
-            char *pst_filename = pst->filename;
-
-            if (fe.dir_index)
-              dir_name = lh->include_dirs[fe.dir_index - 1];
-
-            if (!IS_ABSOLUTE_PATH (include_name) && dir_name != NULL)
-              {
-                include_name = concat (dir_name, SLASH_STRING,
-                                      include_name, (char *)NULL);
-                make_cleanup (xfree, include_name);
-              }
-
-            if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
-              {
-                pst_filename = concat (pst->dirname, SLASH_STRING,
-                                      pst_filename, (char *)NULL);
-                make_cleanup (xfree, pst_filename);
-              }
-
-            if (strcmp (include_name, pst_filename) != 0)
+           char *include_name =
+             psymtab_include_file_name (lh, file_index, pst, comp_dir);
+           if (include_name != NULL)
               dwarf2_create_include_psymtab (include_name, pst, objfile);
           }
     }
@@ -9628,7 +10732,8 @@ dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
    subfile's name.  */
 
 static void
-dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
+dwarf2_start_subfile (char *filename, const char *dirname,
+                     const char *comp_dir)
 {
   char *fullname;
 
@@ -9713,10 +10818,13 @@ var_decode_location (struct attribute *attr, struct symbol *sym,
    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.
+   If SPACE is not NULL, use it to hold the new symbol.  If it is
+   NULL, allocate a new symbol on the objfile's obstack.  */
 
 static struct symbol *
-new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
+new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
+                struct symbol *space)
 {
   struct objfile *objfile = cu->objfile;
   struct symbol *sym = NULL;
@@ -9724,6 +10832,8 @@ new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
   struct attribute *attr = NULL;
   struct attribute *attr2 = NULL;
   CORE_ADDR baseaddr;
+  struct pending **list_to_add = NULL;
+
   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
 
   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
@@ -9732,23 +10842,26 @@ new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
   if (name)
     {
       const char *linkagename;
+      int suppress_add = 0;
 
-      sym = (struct symbol *) obstack_alloc (&objfile->objfile_obstack,
-                                            sizeof (struct symbol));
+      if (space)
+       sym = space;
+      else
+       sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
       OBJSTAT (objfile, n_syms++);
-      memset (sym, 0, sizeof (struct symbol));
 
       /* Cache this symbol's name and the name's demangled form (if any).  */
-      SYMBOL_LANGUAGE (sym) = cu->language;
+      SYMBOL_SET_LANGUAGE (sym, cu->language);
       linkagename = dwarf2_physname (name, die, cu);
       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
 
       /* Fortran does not have mangling standard and the mangling does differ
         between gfortran, iFort etc.  */
       if (cu->language == language_fortran
-          && sym->ginfo.language_specific.cplus_specific.demangled_name == NULL)
-       sym->ginfo.language_specific.cplus_specific.demangled_name
-         = (char *) dwarf2_full_name (name, die, cu);
+          && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
+       symbol_set_demangled_name (&(sym->ginfo),
+                                  (char *) dwarf2_full_name (name, die, cu),
+                                  NULL);
 
       /* Default assumptions.
          Use the passed type or decode it from the die.  */
@@ -9813,11 +10926,11 @@ new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
                  access them globally.  For instance, we want to be able
                  to break on a nested subprogram without having to
                  specify the context.  */
-             add_symbol_to_list (sym, &global_symbols);
+             list_to_add = &global_symbols;
            }
          else
            {
-             add_symbol_to_list (sym, cu->list_in_scope);
+             list_to_add = cu->list_in_scope;
            }
          break;
        case DW_TAG_inlined_subroutine:
@@ -9828,11 +10941,15 @@ new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
          /* Do not add the symbol to any lists.  It will be found via
             BLOCK_FUNCTION from the blockvector.  */
          break;
+       case DW_TAG_template_value_param:
+         suppress_add = 1;
+         /* Fall through.  */
+       case DW_TAG_constant:
        case DW_TAG_variable:
        case DW_TAG_member:
-         /* Compilation with minimal debug info may result in variables
-            with missing type entries. Change the misleading `void' type
-            to something sensible.  */
+         /* Compilation with minimal debug info may result in
+            variables with missing type entries.  Change the
+            misleading `void' type to something sensible.  */
          if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
            SYMBOL_TYPE (sym)
              = objfile_type (objfile)->nodebug_data_symbol;
@@ -9851,10 +10968,13 @@ new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
            {
              dwarf2_const_value (attr, sym, cu);
              attr2 = dwarf2_attr (die, DW_AT_external, cu);
-             if (attr2 && (DW_UNSND (attr2) != 0))
-               add_symbol_to_list (sym, &global_symbols);
-             else
-               add_symbol_to_list (sym, cu->list_in_scope);
+             if (!suppress_add)
+               {
+                 if (attr2 && (DW_UNSND (attr2) != 0))
+                   list_to_add = &global_symbols;
+                 else
+                   list_to_add = cu->list_in_scope;
+               }
              break;
            }
          attr = dwarf2_attr (die, DW_AT_location, cu);
@@ -9862,10 +10982,17 @@ new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
            {
              var_decode_location (attr, sym, cu);
              attr2 = dwarf2_attr (die, DW_AT_external, cu);
-             if (attr2 && (DW_UNSND (attr2) != 0))
+             if (SYMBOL_CLASS (sym) == LOC_STATIC
+                 && SYMBOL_VALUE_ADDRESS (sym) == 0
+                 && !dwarf2_per_objfile->has_section_at_zero)
+               {
+                 /* When a static variable is eliminated by the linker,
+                    the corresponding debug information is not stripped
+                    out, but the variable address is set to null;
+                    do not add such variables into symbol table.  */
+               }
+             else if (attr2 && (DW_UNSND (attr2) != 0))
                {
-                 struct pending **list_to_add;
-
                  /* Workaround gfortran PR debug/40040 - it uses
                     DW_AT_location for variables in -fPIC libraries which may
                     get overriden by other libraries/executable and get
@@ -9884,10 +11011,9 @@ new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
                     but it may be block-scoped.  */
                  list_to_add = (cu->list_in_scope == &file_symbols
                                 ? &global_symbols : cu->list_in_scope);
-                 add_symbol_to_list (sym, list_to_add);
                }
              else
-               add_symbol_to_list (sym, cu->list_in_scope);
+               list_to_add = cu->list_in_scope;
            }
          else
            {
@@ -9901,21 +11027,19 @@ new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
              if (attr2 && (DW_UNSND (attr2) != 0)
                  && dwarf2_attr (die, DW_AT_type, cu) != NULL)
                {
-                 struct pending **list_to_add;
-
                  /* A variable with DW_AT_external is never static, but it
                     may be block-scoped.  */
                  list_to_add = (cu->list_in_scope == &file_symbols
                                 ? &global_symbols : cu->list_in_scope);
 
                  SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
-                 add_symbol_to_list (sym, list_to_add);
                }
              else if (!die_is_declaration (die, cu))
                {
                  /* Use the default LOC_OPTIMIZED_OUT class.  */
                  gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
-                 add_symbol_to_list (sym, cu->list_in_scope);
+                 if (!suppress_add)
+                   list_to_add = cu->list_in_scope;
                }
            }
          break;
@@ -9947,13 +11071,16 @@ new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
              SYMBOL_TYPE (sym) = ref_type;
            }
 
-         add_symbol_to_list (sym, cu->list_in_scope);
+         list_to_add = cu->list_in_scope;
          break;
        case DW_TAG_unspecified_parameters:
          /* From varargs functions; gdb doesn't seem to have any
             interest in this information, so just ignore it for now.
             (FIXME?) */
          break;
+       case DW_TAG_template_type_param:
+         suppress_add = 1;
+         /* Fall through.  */
        case DW_TAG_class_type:
        case DW_TAG_interface_type:
        case DW_TAG_structure_type:
@@ -9972,42 +11099,40 @@ new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
               saves you.  See the OtherFileClass tests in
               gdb.c++/namespace.exp.  */
 
-           struct pending **list_to_add;
-
-           list_to_add = (cu->list_in_scope == &file_symbols
-                          && (cu->language == language_cplus
-                              || cu->language == language_java)
-                          ? &global_symbols : cu->list_in_scope);
-
-           add_symbol_to_list (sym, list_to_add);
-
-           /* The semantics of C++ state that "struct foo { ... }" also
-              defines a typedef for "foo".  A Java class declaration also
-              defines a typedef for the class.  */
-           if (cu->language == language_cplus
-               || cu->language == language_java
-               || cu->language == language_ada)
+           if (!suppress_add)
              {
-               /* The symbol's name is already allocated along with
-                  this objfile, so we don't need to duplicate it for
-                  the type.  */
-               if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
-                 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
+               list_to_add = (cu->list_in_scope == &file_symbols
+                              && (cu->language == language_cplus
+                                  || cu->language == language_java)
+                              ? &global_symbols : cu->list_in_scope);
+
+               /* The semantics of C++ state that "struct foo {
+                  ... }" also defines a typedef for "foo".  A Java
+                  class declaration also defines a typedef for the
+                  class.  */
+               if (cu->language == language_cplus
+                   || cu->language == language_java
+                   || cu->language == language_ada)
+                 {
+                   /* The symbol's name is already allocated along
+                      with this objfile, so we don't need to
+                      duplicate it for the type.  */
+                   if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
+                     TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
+                 }
              }
          }
          break;
        case DW_TAG_typedef:
          SYMBOL_CLASS (sym) = LOC_TYPEDEF;
          SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
-         add_symbol_to_list (sym, cu->list_in_scope);
+         list_to_add = cu->list_in_scope;
          break;
        case DW_TAG_base_type:
         case DW_TAG_subrange_type:
-        case DW_TAG_const_type:
-        case DW_TAG_volatile_type:
          SYMBOL_CLASS (sym) = LOC_TYPEDEF;
          SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
-         add_symbol_to_list (sym, cu->list_in_scope);
+         list_to_add = cu->list_in_scope;
          break;
        case DW_TAG_enumerator:
          attr = dwarf2_attr (die, DW_AT_const_value, cu);
@@ -10019,30 +11144,36 @@ new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
            /* NOTE: carlton/2003-11-10: See comment above in the
               DW_TAG_class_type, etc. block.  */
 
-           struct pending **list_to_add;
-
            list_to_add = (cu->list_in_scope == &file_symbols
                           && (cu->language == language_cplus
                               || cu->language == language_java)
                           ? &global_symbols : cu->list_in_scope);
-
-           add_symbol_to_list (sym, list_to_add);
          }
          break;
        case DW_TAG_namespace:
          SYMBOL_CLASS (sym) = LOC_TYPEDEF;
-         add_symbol_to_list (sym, &global_symbols);
+         list_to_add = &global_symbols;
          break;
        default:
          /* Not a tag we recognize.  Hopefully we aren't processing
             trash data, but since we must specifically ignore things
             we don't recognize, there is nothing else we should do at
-            this point. */
+            this point.  */
          complaint (&symfile_complaints, _("unsupported tag: '%s'"),
                     dwarf_tag_name (die->tag));
          break;
        }
 
+      if (suppress_add)
+       {
+         sym->hash_next = objfile->template_symbols;
+         objfile->template_symbols = sym;
+         list_to_add = NULL;
+       }
+
+      if (list_to_add != NULL)
+       add_symbol_to_list (sym, list_to_add);
+
       /* For the benefit of old versions of GCC, check for anonymous
         namespaces based on the demangled name.  */
       if (!processing_has_namespace_info
@@ -10052,58 +11183,105 @@ new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
   return (sym);
 }
 
-/* Copy constant value from an attribute to a symbol.  */
+/* A wrapper for new_symbol_full that always allocates a new symbol.  */
 
-static void
-dwarf2_const_value (struct attribute *attr, struct symbol *sym,
-                   struct dwarf2_cu *cu)
+static struct symbol *
+new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
+{
+  return new_symbol_full (die, type, cu, NULL);
+}
+
+/* Given an attr with a DW_FORM_dataN value in host byte order,
+   zero-extend it as appropriate for the symbol's type.  The DWARF
+   standard (v4) is not entirely clear about the meaning of using
+   DW_FORM_dataN for a constant with a signed type, where the type is
+   wider than the data.  The conclusion of a discussion on the DWARF
+   list was that this is unspecified.  We choose to always zero-extend
+   because that is the interpretation long in use by GCC.  */
+
+static gdb_byte *
+dwarf2_const_value_data (struct attribute *attr, struct type *type,
+                        const char *name, struct obstack *obstack,
+                        struct dwarf2_cu *cu, long *value, int bits)
 {
   struct objfile *objfile = cu->objfile;
-  struct comp_unit_head *cu_header = &cu->header;
   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
                                BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
+  LONGEST l = DW_UNSND (attr);
+
+  if (bits < sizeof (*value) * 8)
+    {
+      l &= ((LONGEST) 1 << bits) - 1;
+      *value = l;
+    }
+  else if (bits == sizeof (*value) * 8)
+    *value = l;
+  else
+    {
+      gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
+      store_unsigned_integer (bytes, bits / 8, byte_order, l);
+      return bytes;
+    }
+
+  return NULL;
+}
+
+/* Read a constant value from an attribute.  Either set *VALUE, or if
+   the value does not fit in *VALUE, set *BYTES - either already
+   allocated on the objfile obstack, or newly allocated on OBSTACK,
+   or, set *BATON, if we translated the constant to a location
+   expression.  */
+
+static void
+dwarf2_const_value_attr (struct attribute *attr, struct type *type,
+                        const char *name, struct obstack *obstack,
+                        struct dwarf2_cu *cu,
+                        long *value, gdb_byte **bytes,
+                        struct dwarf2_locexpr_baton **baton)
+{
+  struct objfile *objfile = cu->objfile;
+  struct comp_unit_head *cu_header = &cu->header;
   struct dwarf_block *blk;
+  enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
+                               BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
+
+  *value = 0;
+  *bytes = NULL;
+  *baton = NULL;
 
   switch (attr->form)
     {
     case DW_FORM_addr:
       {
-       struct dwarf2_locexpr_baton *baton;
        gdb_byte *data;
 
-       if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != cu_header->addr_size)
-         dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
+       if (TYPE_LENGTH (type) != cu_header->addr_size)
+         dwarf2_const_value_length_mismatch_complaint (name,
                                                        cu_header->addr_size,
-                                                       TYPE_LENGTH (SYMBOL_TYPE
-                                                                    (sym)));
+                                                       TYPE_LENGTH (type));
        /* Symbols of this form are reasonably rare, so we just
           piggyback on the existing location code rather than writing
           a new implementation of symbol_computed_ops.  */
-       baton = obstack_alloc (&objfile->objfile_obstack,
-                              sizeof (struct dwarf2_locexpr_baton));
-       baton->per_cu = cu->per_cu;
-       gdb_assert (baton->per_cu);
+       *baton = obstack_alloc (&objfile->objfile_obstack,
+                               sizeof (struct dwarf2_locexpr_baton));
+       (*baton)->per_cu = cu->per_cu;
+       gdb_assert ((*baton)->per_cu);
 
-       baton->size = 2 + cu_header->addr_size;
-       data = obstack_alloc (&objfile->objfile_obstack, baton->size);
-       baton->data = data;
+       (*baton)->size = 2 + cu_header->addr_size;
+       data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
+       (*baton)->data = data;
 
        data[0] = DW_OP_addr;
        store_unsigned_integer (&data[1], cu_header->addr_size,
                                byte_order, DW_ADDR (attr));
        data[cu_header->addr_size + 1] = DW_OP_stack_value;
-
-       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
-       SYMBOL_LOCATION_BATON (sym) = baton;
-       SYMBOL_CLASS (sym) = LOC_COMPUTED;
       }
       break;
     case DW_FORM_string:
     case DW_FORM_strp:
-      /* DW_STRING is already allocated on the obstack, point directly
-        to it.  */
-      SYMBOL_VALUE_BYTES (sym) = (gdb_byte *) DW_STRING (attr);
-      SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
+      /* DW_STRING is already allocated on the objfile obstack, point
+        directly to it.  */
+      *bytes = (gdb_byte *) DW_STRING (attr);
       break;
     case DW_FORM_block1:
     case DW_FORM_block2:
@@ -10111,15 +11289,10 @@ dwarf2_const_value (struct attribute *attr, struct symbol *sym,
     case DW_FORM_block:
     case DW_FORM_exprloc:
       blk = DW_BLOCK (attr);
-      if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
-       dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
-                                                     blk->size,
-                                                     TYPE_LENGTH (SYMBOL_TYPE
-                                                                  (sym)));
-      SYMBOL_VALUE_BYTES (sym) =
-       obstack_alloc (&objfile->objfile_obstack, blk->size);
-      memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
-      SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
+      if (TYPE_LENGTH (type) != blk->size)
+       dwarf2_const_value_length_mismatch_complaint (name, blk->size,
+                                                     TYPE_LENGTH (type));
+      *bytes = blk->data;
       break;
 
       /* The DW_AT_const_value attributes are supposed to carry the
@@ -10128,68 +11301,81 @@ dwarf2_const_value (struct attribute *attr, struct symbol *sym,
         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);
+      *bytes = dwarf2_const_value_data (attr, type, name,
+                                       obstack, cu, value, 8);
       break;
     case DW_FORM_data2:
-      dwarf2_const_value_data (attr, sym, 16);
+      *bytes = dwarf2_const_value_data (attr, type, name,
+                                       obstack, cu, value, 16);
       break;
     case DW_FORM_data4:
-      dwarf2_const_value_data (attr, sym, 32);
+      *bytes = dwarf2_const_value_data (attr, type, name,
+                                       obstack, cu, value, 32);
       break;
     case DW_FORM_data8:
-      dwarf2_const_value_data (attr, sym, 64);
+      *bytes = dwarf2_const_value_data (attr, type, name,
+                                       obstack, cu, value, 64);
       break;
 
     case DW_FORM_sdata:
-      SYMBOL_VALUE (sym) = DW_SND (attr);
-      SYMBOL_CLASS (sym) = LOC_CONST;
+      *value = DW_SND (attr);
       break;
 
     case DW_FORM_udata:
-      SYMBOL_VALUE (sym) = DW_UNSND (attr);
-      SYMBOL_CLASS (sym) = LOC_CONST;
+      *value = DW_UNSND (attr);
       break;
 
     default:
       complaint (&symfile_complaints,
                 _("unsupported const value attribute form: '%s'"),
                 dwarf_form_name (attr->form));
-      SYMBOL_VALUE (sym) = 0;
-      SYMBOL_CLASS (sym) = LOC_CONST;
+      *value = 0;
       break;
     }
 }
 
 
-/* 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.  */
+/* Copy constant value from an attribute to a symbol.  */
+
 static void
-dwarf2_const_value_data (struct attribute *attr,
-                        struct symbol *sym,
-                        int bits)
+dwarf2_const_value (struct attribute *attr, struct symbol *sym,
+                   struct dwarf2_cu *cu)
 {
-  LONGEST l = DW_UNSND (attr);
+  struct objfile *objfile = cu->objfile;
+  struct comp_unit_head *cu_header = &cu->header;
+  long value;
+  gdb_byte *bytes;
+  struct dwarf2_locexpr_baton *baton;
 
-  if (bits < sizeof (l) * 8)
+  dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
+                          SYMBOL_PRINT_NAME (sym),
+                          &objfile->objfile_obstack, cu,
+                          &value, &bytes, &baton);
+
+  if (baton != NULL)
     {
-      if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
-       l &= ((LONGEST) 1 << bits) - 1;
-      else
-       l = (l << (sizeof (l) * 8 - bits)) >> (sizeof (l) * 8 - bits);
+      SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
+      SYMBOL_LOCATION_BATON (sym) = baton;
+      SYMBOL_CLASS (sym) = LOC_COMPUTED;
+    }
+  else if (bytes != NULL)
+     {
+      SYMBOL_VALUE_BYTES (sym) = bytes;
+      SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
+    }
+  else
+    {
+      SYMBOL_VALUE (sym) = value;
+      SYMBOL_CLASS (sym) = LOC_CONST;
     }
-
-  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 *
 die_type (struct die_info *die, struct dwarf2_cu *cu)
 {
   struct attribute *type_attr;
-  struct die_info *type_die;
 
   type_attr = dwarf2_attr (die, DW_AT_type, cu);
   if (!type_attr)
@@ -10198,9 +11384,7 @@ die_type (struct die_info *die, struct dwarf2_cu *cu)
       return objfile_type (cu->objfile)->builtin_void;
     }
 
-  type_die = follow_die_ref_or_sig (die, type_attr, &cu);
-
-  return tag_type_to_type (type_die, cu);
+  return lookup_die_type (die, type_attr, cu);
 }
 
 /* True iff CU's producer generates GNAT Ada auxiliary information
@@ -10221,7 +11405,6 @@ need_gnat_info (struct dwarf2_cu *cu)
   return 0;
 }
 
-
 /* Return the auxiliary type of the die in question using its
    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
    attribute is not present.  */
@@ -10230,14 +11413,12 @@ static struct type *
 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
 {
   struct attribute *type_attr;
-  struct die_info *type_die;
 
   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
   if (!type_attr)
     return NULL;
 
-  type_die = follow_die_ref (die, type_attr, &cu);
-  return tag_type_to_type (type_die, cu);
+  return lookup_die_type (die, type_attr, cu);
 }
 
 /* If DIE has a descriptive_type attribute, then set the TYPE's
@@ -10263,24 +11444,72 @@ static struct type *
 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
 {
   struct attribute *type_attr;
-  struct die_info *type_die;
 
   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
   if (!type_attr)
     error (_("Dwarf Error: Problem turning containing type into gdb type "
             "[in module %s]"), cu->objfile->name);
 
-  type_die = follow_die_ref_or_sig (die, type_attr, &cu);
-  return tag_type_to_type (type_die, cu);
+  return lookup_die_type (die, type_attr, cu);
 }
 
+/* Look up the type of DIE in CU using its type attribute ATTR.
+   If there is no type substitute an error marker.  */
+
 static struct type *
-tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
+lookup_die_type (struct die_info *die, struct attribute *attr,
+                struct dwarf2_cu *cu)
 {
   struct type *this_type;
 
-  this_type = read_type_die (die, cu);
-  if (!this_type)
+  /* First see if we have it cached.  */
+
+  if (is_ref_attr (attr))
+    {
+      unsigned int offset = dwarf2_get_ref_die_offset (attr);
+
+      this_type = get_die_type_at_offset (offset, cu->per_cu);
+    }
+  else if (attr->form == DW_FORM_sig8)
+    {
+      struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
+      struct dwarf2_cu *sig_cu;
+      unsigned int offset;
+
+      /* sig_type will be NULL if the signatured type is missing from
+        the debug info.  */
+      if (sig_type == NULL)
+       error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
+                "at 0x%x [in module %s]"),
+              die->offset, cu->objfile->name);
+
+      gdb_assert (sig_type->per_cu.from_debug_types);
+      offset = sig_type->offset + sig_type->type_offset;
+      this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
+    }
+  else
+    {
+      dump_die_for_error (die);
+      error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
+            dwarf_attr_name (attr->name), cu->objfile->name);
+    }
+
+  /* If not cached we need to read it in.  */
+
+  if (this_type == NULL)
+    {
+      struct die_info *type_die;
+      struct dwarf2_cu *type_cu = cu;
+
+      type_die = follow_die_ref_or_sig (die, attr, &type_cu);
+      /* If the type is cached, we should have found it above.  */
+      gdb_assert (get_die_type (type_die, type_cu) == NULL);
+      this_type = read_type_die_1 (type_die, type_cu);
+    }
+
+  /* If we still don't have a type use an error marker.  */
+
+  if (this_type == NULL)
     {
       char *message, *saved;
 
@@ -10295,9 +11524,18 @@ tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
 
       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
     }
+
   return this_type;
 }
 
+/* Return the type in DIE, CU.
+   Returns NULL for invalid types.
+
+   This first does a lookup in the appropriate type_hash table,
+   and only reads the die in if necessary.
+
+   NOTE: This can be called when reading in partial or full symbols.  */
+
 static struct type *
 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
 {
@@ -10307,6 +11545,17 @@ read_type_die (struct die_info *die, struct dwarf2_cu *cu)
   if (this_type)
     return this_type;
 
+  return read_type_die_1 (die, cu);
+}
+
+/* Read the type in DIE, CU.
+   Returns NULL for invalid types.  */
+
+static struct type *
+read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
+{
+  struct type *this_type = NULL;
+
   switch (die->tag)
     {
     case DW_TAG_class_type:
@@ -10366,7 +11615,8 @@ read_type_die (struct die_info *die, struct dwarf2_cu *cu)
       this_type = read_module_type (die, cu);
       break;
     default:
-      complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
+      complaint (&symfile_complaints,
+                _("unexpected tag in read_type_die: '%s'"),
                 dwarf_tag_name (die->tag));
       break;
     }
@@ -10374,6 +11624,78 @@ read_type_die (struct die_info *die, struct dwarf2_cu *cu)
   return this_type;
 }
 
+/* See if we can figure out if the class lives in a namespace.  We do
+   this by looking for a member function; its demangled name will
+   contain namespace info, if there is any.
+   Return the computed name or NULL.
+   Space for the result is allocated on the objfile's obstack.
+   This is the full-die version of guess_partial_die_structure_name.
+   In this case we know DIE has no useful parent.  */
+
+static char *
+guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
+{
+  struct die_info *spec_die;
+  struct dwarf2_cu *spec_cu;
+  struct die_info *child;
+
+  spec_cu = cu;
+  spec_die = die_specification (die, &spec_cu);
+  if (spec_die != NULL)
+    {
+      die = spec_die;
+      cu = spec_cu;
+    }
+
+  for (child = die->child;
+       child != NULL;
+       child = child->sibling)
+    {
+      if (child->tag == DW_TAG_subprogram)
+       {
+         struct attribute *attr;
+
+         attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
+         if (attr == NULL)
+           attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
+         if (attr != NULL)
+           {
+             char *actual_name
+               = language_class_name_from_physname (cu->language_defn,
+                                                    DW_STRING (attr));
+             char *name = NULL;
+
+             if (actual_name != NULL)
+               {
+                 char *die_name = dwarf2_name (die, cu);
+
+                 if (die_name != NULL
+                     && strcmp (die_name, actual_name) != 0)
+                   {
+                     /* Strip off the class name from the full name.
+                        We want the prefix.  */
+                     int die_name_len = strlen (die_name);
+                     int actual_name_len = strlen (actual_name);
+
+                     /* Test for '::' as a sanity check.  */
+                     if (actual_name_len > die_name_len + 2
+                         && actual_name[actual_name_len
+                                        - die_name_len - 1] == ':')
+                       name =
+                         obsavestring (actual_name,
+                                       actual_name_len - die_name_len - 2,
+                                       &cu->objfile->objfile_obstack);
+                   }
+               }
+             xfree (actual_name);
+             return name;
+           }
+       }
+    }
+
+  return NULL;
+}
+
 /* Return the name of the namespace/class that DIE is defined within,
    or "" if we can't tell.  The caller should not xfree the result.
 
@@ -10432,6 +11754,48 @@ determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
 
   if (parent == NULL)
     return "";
+  else if (parent->building_fullname)
+    {
+      const char *name;
+      const char *parent_name;
+
+      /* It has been seen on RealView 2.2 built binaries,
+        DW_TAG_template_type_param types actually _defined_ as
+        children of the parent class:
+
+        enum E {};
+        template class <class Enum> Class{};
+        Class<enum E> class_e;
+
+         1: DW_TAG_class_type (Class)
+           2: DW_TAG_enumeration_type (E)
+             3: DW_TAG_enumerator (enum1:0)
+             3: DW_TAG_enumerator (enum2:1)
+             ...
+           2: DW_TAG_template_type_param
+              DW_AT_type  DW_FORM_ref_udata (E)
+
+        Besides being broken debug info, it can put GDB into an
+        infinite loop.  Consider:
+
+        When we're building the full name for Class<E>, we'll start
+        at Class, and go look over its template type parameters,
+        finding E.  We'll then try to build the full name of E, and
+        reach here.  We're now trying to build the full name of E,
+        and look over the parent DIE for containing scope.  In the
+        broken case, if we followed the parent DIE of E, we'd again
+        find Class, and once again go look at its template type
+        arguments, etc., etc.  Simply don't consider such parent die
+        as source-level parent of this die (it can't be, the language
+        doesn't allow it), and break the loop here.  */
+      name = dwarf2_name (die, cu);
+      parent_name = dwarf2_name (parent, cu);
+      complaint (&symfile_complaints,
+                _("template param type '%s' defined within parent '%s'"),
+                name ? name : "<unknown>",
+                parent_name ? parent_name : "<unknown>");
+      return "";
+    }
   else
     switch (parent->tag)
       {
@@ -10458,16 +11822,30 @@ determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
             members; no typedefs, no member functions, et cetera.
             So it does not need a prefix.  */
          return "";
+      case DW_TAG_compile_unit:
+       /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  Cope.  */
+       if (cu->language == language_cplus
+           && dwarf2_per_objfile->types.asection != NULL
+           && die->child != NULL
+           && (die->tag == DW_TAG_class_type
+               || die->tag == DW_TAG_structure_type
+               || die->tag == DW_TAG_union_type))
+         {
+           char *name = guess_full_die_structure_name (die, cu);
+           if (name != NULL)
+             return name;
+         }
+       return "";
       default:
        return determine_prefix (parent, cu);
       }
 }
 
-/* Return a newly-allocated string formed by concatenating PREFIX and
-   SUFFIX with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
-   simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null,
-   perform an obconcat, otherwise allocate storage for the result.  The CU argument
-   is used to determine the language and hence, the appropriate separator.  */
+/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
+   with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
+   simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null, perform
+   an obconcat, otherwise allocate storage for the result.  The CU argument is
+   used to determine the language and hence, the appropriate separator.  */
 
 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
 
@@ -10478,7 +11856,8 @@ typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
   const char *lead = "";
   const char *sep;
 
-  if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
+  if (suffix == NULL || suffix[0] == '\0'
+      || prefix == NULL || prefix[0] == '\0')
     sep = "";
   else if (cu->language == language_java)
     sep = ".";
@@ -10500,7 +11879,8 @@ typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
 
   if (obs == NULL)
     {
-      char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
+      char *retval
+       = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
 
       strcpy (retval, lead);
       strcat (retval, prefix);
@@ -11055,6 +12435,8 @@ dwarf_attr_name (unsigned attr)
       return "DW_AT_body_end";
     case DW_AT_GNU_vector:
       return "DW_AT_GNU_vector";
+    case DW_AT_GNU_odr_signature:
+      return "DW_AT_GNU_odr_signature";
     /* VMS extensions.  */
     case DW_AT_VMS_rtnbeg_pd_address:
       return "DW_AT_VMS_rtnbeg_pd_address";
@@ -11138,7 +12520,7 @@ dwarf_form_name (unsigned form)
 /* Convert a DWARF stack opcode into its string name.  */
 
 const char *
-dwarf_stack_op_name (unsigned op, int def)
+dwarf_stack_op_name (unsigned op)
 {
   switch (op)
     {
@@ -11457,8 +12839,10 @@ dwarf_stack_op_name (unsigned op, int def)
       return "DW_OP_GNU_push_tls_address";
     case DW_OP_GNU_uninit:
       return "DW_OP_GNU_uninit";
+    case DW_OP_GNU_implicit_pointer:
+      return "DW_OP_GNU_implicit_pointer";
     default:
-      return def ? "OP_<unknown>" : NULL;
+      return NULL;
     }
 }
 
@@ -11534,7 +12918,7 @@ dwarf_type_encoding_name (unsigned enc)
     }
 }
 
-/* Convert a DWARF call frame info operation to its string name. */
+/* Convert a DWARF call frame info operation to its string name.  */
 
 #if 0
 static char *
@@ -11652,7 +13036,8 @@ dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
        case DW_FORM_block4:
        case DW_FORM_block:
        case DW_FORM_block1:
-         fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
+         fprintf_unfiltered (f, "block: size %d",
+                             DW_BLOCK (&die->attrs[i])->size);
          break;
        case DW_FORM_exprloc:
          fprintf_unfiltered (f, "expression: size %u",
@@ -11701,9 +13086,10 @@ dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
          fprintf_unfiltered (f, "flag: TRUE");
          break;
        case DW_FORM_indirect:
-         /* the reader will have reduced the indirect form to
-            the "base form" so this form should not occur */
-         fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
+         /* The reader will have reduced the indirect form to
+            the "base form" so this form should not occur.  */
+         fprintf_unfiltered (f, 
+                             "unexpected attribute form: DW_FORM_indirect");
          break;
        default:
          fprintf_unfiltered (f, "unsupported attribute form: %d.",
@@ -11743,7 +13129,8 @@ dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
        }
       else
        {
-         fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
+         fprintf_unfiltered (f,
+                             " [not printed, max nesting level reached]\n");
        }
     }
 
@@ -11817,7 +13204,8 @@ dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
     return DW_UNSND (attr);
   else
     {
-      complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
+      complaint (&symfile_complaints,
+                _("Attribute value is not a constant (%s)"),
                  dwarf_form_name (attr->form));
       return default_value;
     }
@@ -11832,6 +13220,16 @@ static int
 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
                       struct dwarf2_per_cu_data *per_cu)
 {
+  /* We may arrive here during partial symbol reading, if we need full
+     DIEs to process an unusual case (e.g. template arguments).  Do
+     not queue PER_CU, just tell our caller to load its DIEs.  */
+  if (dwarf2_per_objfile->reading_partial_symbols)
+    {
+      if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
+       return 1;
+      return 0;
+    }
+
   /* Mark the dependence relation so that we don't flush PER_CU
      too early.  */
   dwarf2_add_dependence (this_cu, per_cu);
@@ -11879,8 +13277,9 @@ follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
 }
 
 /* Follow reference OFFSET.
-   On entry *REF_CU is the CU of source DIE referencing OFFSET.
-   On exit *REF_CU is the CU of the result.  */
+   On entry *REF_CU is the CU of the source die referencing OFFSET.
+   On exit *REF_CU is the CU of the result.
+   Returns NULL if OFFSET is invalid.  */
 
 static struct die_info *
 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
@@ -11890,6 +13289,8 @@ follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
 
   gdb_assert (cu->per_cu != NULL);
 
+  target_cu = cu;
+
   if (cu->per_cu->from_debug_types)
     {
       /* .debug_types CUs cannot reference anything outside their CU.
@@ -11897,7 +13298,6 @@ follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
         DW_FORM_sig8.  */
       if (! offset_in_cu_p (&cu->header, offset))
        return NULL;
-      target_cu = cu;
     }
   else if (! offset_in_cu_p (&cu->header, offset))
     {
@@ -11911,8 +13311,12 @@ follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
 
       target_cu = per_cu->cu;
     }
-  else
-    target_cu = cu;
+  else if (cu->dies == NULL)
+    {
+      /* We're loading full DIEs during partial symbol reading.  */
+      gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
+      load_full_comp_unit (cu->per_cu, cu->objfile);
+    }
 
   *ref_cu = target_cu;
   temp_die.offset = offset;
@@ -11945,13 +13349,17 @@ follow_die_ref (struct die_info *src_die, struct attribute *attr,
 
 struct dwarf2_locexpr_baton
 dwarf2_fetch_die_location_block (unsigned int offset,
-                                struct dwarf2_per_cu_data *per_cu)
+                                struct dwarf2_per_cu_data *per_cu,
+                                CORE_ADDR (*get_frame_pc) (void *baton),
+                                void *baton)
 {
   struct dwarf2_cu *cu = per_cu->cu;
   struct die_info *die;
   struct attribute *attr;
   struct dwarf2_locexpr_baton retval;
 
+  dw2_setup (per_cu->objfile);
+
   die = follow_die_offset (offset, &cu);
   if (!die)
     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
@@ -11965,6 +13373,18 @@ dwarf2_fetch_die_location_block (unsigned int offset,
       retval.data = NULL;
       retval.size = 0;
     }
+  else if (attr_form_is_section_offset (attr))
+    {
+      struct dwarf2_loclist_baton loclist_baton;
+      CORE_ADDR pc = (*get_frame_pc) (baton);
+      size_t size;
+
+      fill_in_loclist_baton (cu, &loclist_baton, attr);
+
+      retval.data = dwarf2_find_location_expression (&loclist_baton,
+                                                    &size, pc);
+      retval.size = size;
+    }
   else
     {
       if (!attr_form_is_block (attr))
@@ -12016,8 +13436,8 @@ follow_die_sig (struct die_info *src_die, struct attribute *attr,
       return die;
     }
 
-  error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
-        "at 0x%x [in module %s]"),
+  error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
+        "from DIE at 0x%x [in module %s]"),
         sig_type->type_offset, src_die->offset, objfile->name);
 }
 
@@ -12074,19 +13494,20 @@ static void
 read_signatured_type (struct objfile *objfile,
                      struct signatured_type *type_sig)
 {
-  gdb_byte *types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
+  gdb_byte *types_ptr;
   struct die_reader_specs reader_specs;
   struct dwarf2_cu *cu;
   ULONGEST signature;
   struct cleanup *back_to, *free_cu_cleanup;
-  struct attribute *attr;
+
+  dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
+  types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
 
   gdb_assert (type_sig->per_cu.cu == NULL);
 
-  cu = xmalloc (sizeof (struct dwarf2_cu));
-  memset (cu, 0, sizeof (struct dwarf2_cu));
-  obstack_init (&cu->comp_unit_obstack);
-  cu->objfile = objfile;
+  cu = xmalloc (sizeof (*cu));
+  init_one_comp_unit (cu, objfile);
+
   type_sig->per_cu.cu = cu;
   cu->per_cu = &type_sig->per_cu;
 
@@ -12118,11 +13539,7 @@ read_signatured_type (struct objfile *objfile,
      all objfiles needed for references have been loaded yet, and symbol
      table processing isn't initialized.  But we have to set the CU language,
      or we won't be able to build types correctly.  */
-  attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
-  if (attr)
-    set_cu_language (DW_UNSND (attr), cu);
-  else
-    set_cu_language (language_minimal, cu);
+  prepare_one_comp_unit (cu, cu->dies);
 
   do_cleanups (back_to);
 
@@ -12153,8 +13570,7 @@ read_signatured_type (struct objfile *objfile,
    callers will only want a very basic result and this can become a
    complaint.
 
-   Note that stack[0] is unused except as a default error return.
-   Note that stack overflow is not yet handled.  */
+   Note that stack[0] is unused except as a default error return.  */
 
 static CORE_ADDR
 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
@@ -12171,6 +13587,7 @@ decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
   i = 0;
   stacki = 0;
   stack[stacki] = 0;
+  stack[++stacki] = 0;
 
   while (i < size)
     {
@@ -12315,7 +13732,8 @@ decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
          break;
 
        case DW_OP_plus_uconst:
-         stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
+         stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
+                                                &bytes_read);
          i += bytes_read;
          break;
 
@@ -12348,10 +13766,35 @@ decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
          break;
 
        default:
-         complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
-                    dwarf_stack_op_name (op, 1));
+         {
+           const char *name = dwarf_stack_op_name (op);
+
+           if (name)
+             complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
+                        name);
+           else
+             complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
+                        op);
+         }
+
          return (stack[stacki]);
        }
+
+      /* Enforce maximum stack depth of SIZE-1 to avoid writing
+         outside of the allocated space.  Also enforce minimum>0.  */
+      if (stacki >= ARRAY_SIZE (stack) - 1)
+       {
+         complaint (&symfile_complaints,
+                    _("location description stack overflow"));
+         return 0;
+       }
+
+      if (stacki <= 0)
+       {
+         complaint (&symfile_complaints,
+                    _("location description stack underflow"));
+         return 0;
+       }
     }
   return (stack[stacki]);
 }
@@ -12396,7 +13839,6 @@ dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
 \f
 /* Macro support.  */
 
-
 /* Return the full name of file number I in *LH's file name table.
    Use COMP_DIR as the name of the current directory of the
    compilation.  The result is allocated using xmalloc; the caller is
@@ -12501,7 +13943,8 @@ consume_improper_spaces (const char *p, const char *body)
   if (*p == ' ')
     {
       complaint (&symfile_complaints,
-                _("macro definition contains spaces in formal argument list:\n`%s'"),
+                _("macro definition contains spaces "
+                  "in formal argument list:\n`%s'"),
                 body);
 
       while (*p == ' ')
@@ -12692,7 +14135,8 @@ dwarf_decode_macros (struct line_header *lh, unsigned int offset,
       if (mac_ptr >= mac_end)
         {
          /* Complaint is printed during the second pass as GDB will probably
-            stop the first pass earlier upon finding DW_MACINFO_start_file.  */
+            stop the first pass earlier upon finding
+            DW_MACINFO_start_file.  */
          break;
         }
 
@@ -12714,7 +14158,7 @@ dwarf_decode_macros (struct line_header *lh, unsigned int offset,
 
            read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
            mac_ptr += bytes_read;
-           read_string (abfd, mac_ptr, &bytes_read);
+           read_direct_string (abfd, mac_ptr, &bytes_read);
            mac_ptr += bytes_read;
          }
          break;
@@ -12729,8 +14173,8 @@ dwarf_decode_macros (struct line_header *lh, unsigned int offset,
            file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
            mac_ptr += bytes_read;
 
-           current_file = macro_start_file (file, line, current_file, comp_dir,
-                                            lh, cu->objfile);
+           current_file = macro_start_file (file, line, current_file,
+                                            comp_dir, lh, cu->objfile);
          }
          break;
 
@@ -12745,7 +14189,7 @@ dwarf_decode_macros (struct line_header *lh, unsigned int offset,
 
            read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
            mac_ptr += bytes_read;
-           read_string (abfd, mac_ptr, &bytes_read);
+           read_direct_string (abfd, mac_ptr, &bytes_read);
            mac_ptr += bytes_read;
          }
          break;
@@ -12800,7 +14244,7 @@ dwarf_decode_macros (struct line_header *lh, unsigned int offset,
 
             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
             mac_ptr += bytes_read;
-            body = read_string (abfd, mac_ptr, &bytes_read);
+            body = read_direct_string (abfd, mac_ptr, &bytes_read);
             mac_ptr += bytes_read;
 
             if (! current_file)
@@ -12816,7 +14260,8 @@ dwarf_decode_macros (struct line_header *lh, unsigned int offset,
                                 _("something-or-other"), line, body);
                break;
              }
-           if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
+           if ((line == 0 && !at_commandline)
+               || (line != 0 && at_commandline))
              complaint (&symfile_complaints,
                         _("debug info gives %s macro %s with %s line %d: %s"),
                         at_commandline ? _("command-line") : _("in-file"),
@@ -12844,7 +14289,8 @@ dwarf_decode_macros (struct line_header *lh, unsigned int offset,
             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
             mac_ptr += bytes_read;
 
-           if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
+           if ((line == 0 && !at_commandline)
+               || (line != 0 && at_commandline))
              complaint (&symfile_complaints,
                         _("debug info gives source %d included "
                           "from %s at %s line %d"),
@@ -12866,7 +14312,8 @@ dwarf_decode_macros (struct line_header *lh, unsigned int offset,
         case DW_MACINFO_end_file:
           if (! current_file)
            complaint (&symfile_complaints,
-                      _("macro debug info has an unmatched `close_file' directive"));
+                      _("macro debug info has an unmatched "
+                        "`close_file' directive"));
           else
             {
               current_file = current_file->included_by;
@@ -12891,7 +14338,8 @@ dwarf_decode_macros (struct line_header *lh, unsigned int offset,
                   next_type = read_1_byte (abfd, mac_ptr);
                   if (next_type != 0)
                    complaint (&symfile_complaints,
-                              _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
+                              _("no terminating 0-type entry for "
+                                "macros in `.debug_macinfo' section"));
 
                   return;
                 }
@@ -12902,11 +14350,10 @@ dwarf_decode_macros (struct line_header *lh, unsigned int offset,
           {
             unsigned int bytes_read;
             int constant;
-            char *string;
 
             constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
             mac_ptr += bytes_read;
-            string = read_string (abfd, mac_ptr, &bytes_read);
+            read_direct_string (abfd, mac_ptr, &bytes_read);
             mac_ptr += bytes_read;
 
             /* We don't recognize any vendor extensions.  */
@@ -12917,7 +14364,7 @@ dwarf_decode_macros (struct line_header *lh, unsigned int offset,
 }
 
 /* Check if the attribute's form is a DW_FORM_block*
-   if so return true else false. */
+   if so return true else false.  */
 static int
 attr_form_is_block (struct attribute *attr)
 {
@@ -12975,6 +14422,25 @@ attr_form_is_constant (struct attribute *attr)
     }
 }
 
+/* A helper function that fills in a dwarf2_loclist_baton.  */
+
+static void
+fill_in_loclist_baton (struct dwarf2_cu *cu,
+                      struct dwarf2_loclist_baton *baton,
+                      struct attribute *attr)
+{
+  dwarf2_read_section (dwarf2_per_objfile->objfile,
+                      &dwarf2_per_objfile->loc);
+
+  baton->per_cu = cu->per_cu;
+  gdb_assert (baton->per_cu);
+  /* We don't know how long the location list is, but make sure we
+     don't run off the edge of the section.  */
+  baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
+  baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
+  baton->base_address = cu->base_address;
+}
+
 static void
 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
                             struct dwarf2_cu *cu)
@@ -12983,26 +14449,20 @@ dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
       /* ".debug_loc" may not exist at all, or the offset may be outside
         the section.  If so, fall through to the complaint in the
         other branch.  */
-      && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
+      && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
+                                               &dwarf2_per_objfile->loc))
     {
       struct dwarf2_loclist_baton *baton;
 
       baton = obstack_alloc (&cu->objfile->objfile_obstack,
                             sizeof (struct dwarf2_loclist_baton));
-      baton->per_cu = cu->per_cu;
-      gdb_assert (baton->per_cu);
 
-      dwarf2_read_section (dwarf2_per_objfile->objfile,
-                          &dwarf2_per_objfile->loc);
+      fill_in_loclist_baton (cu, baton, attr);
 
-      /* We don't know how long the location list is, but make sure we
-        don't run off the edge of the section.  */
-      baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
-      baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
-      baton->base_address = cu->base_address;
       if (cu->base_known == 0)
        complaint (&symfile_complaints,
-                  _("Location list used without specifying the CU base address."));
+                  _("Location list used without "
+                    "specifying the CU base address."));
 
       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
       SYMBOL_LOCATION_BATON (sym) = baton;
@@ -13108,7 +14568,7 @@ dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
 CORE_ADDR
 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
 {
-  struct objfile *objfile = per_cu->psymtab->objfile;
+  struct objfile *objfile = per_cu->objfile;
 
   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
 }
@@ -13170,15 +14630,29 @@ dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
   return this_cu;
 }
 
-/* Malloc space for a dwarf2_cu for OBJFILE and initialize it.  */
+/* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space.  */
 
-static struct dwarf2_cu *
-alloc_one_comp_unit (struct objfile *objfile)
+static void
+init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
 {
-  struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
+  memset (cu, 0, sizeof (*cu));
   cu->objfile = objfile;
   obstack_init (&cu->comp_unit_obstack);
-  return cu;
+}
+
+/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE.  */
+
+static void
+prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
+{
+  struct attribute *attr;
+
+  /* Set the language we're debugging.  */
+  attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
+  if (attr)
+    set_cu_language (DW_UNSND (attr), cu);
+  else
+    set_cu_language (language_minimal, cu);
 }
 
 /* Release one cached compilation unit, CU.  We unlink it from the tree
@@ -13330,29 +14804,8 @@ dwarf2_free_objfile (struct objfile *objfile)
   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
   free_cached_comp_units (NULL);
 
-  if (dwarf2_per_objfile->using_index)
-    {
-      int i;
-
-      for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
-       {
-         int j;
-         struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
-
-         if (!cu->v.quick->lines)
-           continue;
-
-         for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
-           {
-             if (cu->v.quick->file_names)
-               xfree ((void *) cu->v.quick->file_names[j]);
-             if (cu->v.quick->full_names)
-               xfree ((void *) cu->v.quick->full_names[j]);
-           }
-
-         free_line_header (cu->v.quick->lines);
-       }
-    }
+  if (dwarf2_per_objfile->quick_file_names_table)
+    htab_delete (dwarf2_per_objfile->quick_file_names_table);
 
   /* Everything else should be on the objfile obstack.  */
 }
@@ -13402,7 +14855,7 @@ offset_and_type_eq (const void *item_lhs, const void *item_rhs)
    Therefore caller should follow these rules:
      * Try to fetch any prerequisite types we may need to build this DIE type
        before building the type and calling set_die_type.
-     * After building typer call set_die_type for current DIE as soon as
+     * After building type call set_die_type for current DIE as soon as
        possible before fetching more types to complete the current type.
      * Make the type as complete as possible before fetching more types.  */
 
@@ -13410,6 +14863,8 @@ static struct type *
 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
 {
   struct dwarf2_offset_and_type **slot, ofs;
+  struct objfile *objfile = cu->objfile;
+  htab_t *type_hash_ptr;
 
   /* For Ada types, make sure that the gnat-specific data is always
      initialized (if not already set).  There are a few types where
@@ -13424,46 +14879,54 @@ set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
       && !HAVE_GNAT_AUX_INFO (type))
     INIT_GNAT_SPECIFIC (type);
 
-  if (cu->type_hash == NULL)
+  if (cu->per_cu->from_debug_types)
+    type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
+  else
+    type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
+
+  if (*type_hash_ptr == NULL)
     {
-      gdb_assert (cu->per_cu != NULL);
-      cu->per_cu->type_hash
-       = htab_create_alloc_ex (cu->header.length / 24,
+      *type_hash_ptr
+       = htab_create_alloc_ex (127,
                                offset_and_type_hash,
                                offset_and_type_eq,
                                NULL,
-                               &cu->objfile->objfile_obstack,
+                               &objfile->objfile_obstack,
                                hashtab_obstack_allocate,
                                dummy_obstack_deallocate);
-      cu->type_hash = cu->per_cu->type_hash;
     }
 
   ofs.offset = die->offset;
   ofs.type = type;
   slot = (struct dwarf2_offset_and_type **)
-    htab_find_slot_with_hash (cu->type_hash, &ofs, ofs.offset, INSERT);
+    htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
   if (*slot)
     complaint (&symfile_complaints,
               _("A problem internal to GDB: DIE 0x%x has type already set"),
               die->offset);
-  *slot = obstack_alloc (&cu->objfile->objfile_obstack, sizeof (**slot));
+  *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
   **slot = ofs;
   return type;
 }
 
-/* Find the type for DIE in CU's type_hash, or return NULL if DIE does
-   not have a saved type.  */
+/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
+   table, or return NULL if the die does not have a saved type.  */
 
 static struct type *
-get_die_type (struct die_info *die, struct dwarf2_cu *cu)
+get_die_type_at_offset (unsigned int offset,
+                       struct dwarf2_per_cu_data *per_cu)
 {
   struct dwarf2_offset_and_type *slot, ofs;
-  htab_t type_hash = cu->type_hash;
+  htab_t type_hash;
 
+  if (per_cu->from_debug_types)
+    type_hash = dwarf2_per_objfile->debug_types_type_hash;
+  else
+    type_hash = dwarf2_per_objfile->debug_info_type_hash;
   if (type_hash == NULL)
     return NULL;
 
-  ofs.offset = die->offset;
+  ofs.offset = offset;
   slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
   if (slot)
     return slot->type;
@@ -13471,6 +14934,15 @@ get_die_type (struct die_info *die, struct dwarf2_cu *cu)
     return NULL;
 }
 
+/* Look up the type for DIE in the appropriate type_hash table,
+   or return NULL if DIE does not have a saved type.  */
+
+static struct type *
+get_die_type (struct die_info *die, struct dwarf2_cu *cu)
+{
+  return get_die_type_at_offset (die->offset, cu->per_cu);
+}
+
 /* Add a dependence relationship from CU to REF_PER_CU.  */
 
 static void
@@ -13587,7 +15059,7 @@ munmap_section_buffer (struct dwarf2_section_info *info)
       gdb_assert (munmap ((void *) map_begin, map_length) == 0);
 #else
       /* Without HAVE_MMAP, we should never be here to begin with.  */
-      gdb_assert (0);
+      gdb_assert_not_reached ("no mmap support");
 #endif
     }
 }
@@ -13599,19 +15071,23 @@ dwarf2_per_objfile_free (struct objfile *objfile, void *d)
 {
   struct dwarf2_per_objfile *data = d;
 
+  /* This is sorted according to the order they're defined in to make it easier
+     to keep in sync.  */
   munmap_section_buffer (&data->info);
   munmap_section_buffer (&data->abbrev);
   munmap_section_buffer (&data->line);
-  munmap_section_buffer (&data->str);
+  munmap_section_buffer (&data->loc);
   munmap_section_buffer (&data->macinfo);
+  munmap_section_buffer (&data->str);
   munmap_section_buffer (&data->ranges);
-  munmap_section_buffer (&data->loc);
+  munmap_section_buffer (&data->types);
   munmap_section_buffer (&data->frame);
   munmap_section_buffer (&data->eh_frame);
   munmap_section_buffer (&data->gdb_index);
 }
 
 \f
+/* The "save gdb-index" command.  */
 
 /* The contents of the hash table we create when building the string
    table.  */
@@ -13622,6 +15098,7 @@ struct strtab_entry
 };
 
 /* Hash function for a strtab_entry.  */
+
 static hashval_t
 hash_strtab_entry (const void *e)
 {
@@ -13630,6 +15107,7 @@ hash_strtab_entry (const void *e)
 }
 
 /* Equality function for a strtab_entry.  */
+
 static int
 eq_strtab_entry (const void *a, const void *b)
 {
@@ -13639,6 +15117,7 @@ eq_strtab_entry (const void *a, const void *b)
 }
 
 /* Create a strtab_entry hash table.  */
+
 static htab_t
 create_strtab (void)
 {
@@ -13648,6 +15127,7 @@ create_strtab (void)
 
 /* Add a string to the constant pool.  Return the string's offset in
    host order.  */
+
 static offset_type
 add_string (htab_t table, struct obstack *cpool, const char *str)
 {
@@ -13691,6 +15171,7 @@ struct mapped_symtab
 };
 
 /* Hash function for a symtab_index_entry.  */
+
 static hashval_t
 hash_symtab_entry (const void *e)
 {
@@ -13702,6 +15183,7 @@ hash_symtab_entry (const void *e)
 }
 
 /* Equality function for a symtab_index_entry.  */
+
 static int
 eq_symtab_entry (const void *a, const void *b)
 {
@@ -13716,6 +15198,7 @@ eq_symtab_entry (const void *a, const void *b)
 }
 
 /* Destroy a symtab_index_entry.  */
+
 static void
 delete_symtab_entry (void *p)
 {
@@ -13725,14 +15208,16 @@ delete_symtab_entry (void *p)
 }
 
 /* Create a hash table holding symtab_index_entry objects.  */
+
 static htab_t
-create_index_table (void)
+create_symbol_hash_table (void)
 {
   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
                            delete_symtab_entry, xcalloc, xfree);
 }
 
 /* Create a new mapped symtab object.  */
+
 static struct mapped_symtab *
 create_mapped_symtab (void)
 {
@@ -13744,6 +15229,7 @@ create_mapped_symtab (void)
 }
 
 /* Destroy a mapped_symtab.  */
+
 static void
 cleanup_mapped_symtab (void *p)
 {
@@ -13756,6 +15242,7 @@ cleanup_mapped_symtab (void *p)
 
 /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
    the slot.  */
+
 static struct symtab_index_entry **
 find_slot (struct mapped_symtab *symtab, const char *name)
 {
@@ -13773,6 +15260,7 @@ find_slot (struct mapped_symtab *symtab, const char *name)
 }
 
 /* Expand SYMTAB's hash table.  */
+
 static void
 hash_expand (struct mapped_symtab *symtab)
 {
@@ -13798,6 +15286,7 @@ hash_expand (struct mapped_symtab *symtab)
 
 /* Add an entry to SYMTAB.  NAME is the name of the symbol.  CU_INDEX
    is the index of the CU in which the symbol appears.  */
+
 static void
 add_index_entry (struct mapped_symtab *symtab, const char *name,
                 offset_type cu_index)
@@ -13823,13 +15312,14 @@ add_index_entry (struct mapped_symtab *symtab, const char *name,
 }
 
 /* Add a vector of indices to the constant pool.  */
+
 static offset_type
-add_indices_to_cpool (htab_t index_table, struct obstack *cpool,
+add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
                      struct symtab_index_entry *entry)
 {
   void **slot;
 
-  slot = htab_find_slot (index_table, entry, INSERT);
+  slot = htab_find_slot (symbol_hash_table, entry, INSERT);
   if (!*slot)
     {
       offset_type len = VEC_length (offset_type, entry->cu_indices);
@@ -13860,22 +15350,24 @@ add_indices_to_cpool (htab_t index_table, struct obstack *cpool,
 
 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
    constant pool entries going into the obstack CPOOL.  */
+
 static void
 write_hash_table (struct mapped_symtab *symtab,
                  struct obstack *output, struct obstack *cpool)
 {
   offset_type i;
-  htab_t index_table;
+  htab_t symbol_hash_table;
   htab_t str_table;
 
-  index_table = create_index_table ();
+  symbol_hash_table = create_symbol_hash_table ();
   str_table = create_strtab ();
+
   /* We add all the index vectors to the constant pool first, to
      ensure alignment is ok.  */
   for (i = 0; i < symtab->size; ++i)
     {
       if (symtab->data[i])
-       add_indices_to_cpool (index_table, cpool, symtab->data[i]);
+       add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
     }
 
   /* Now write out the hash table.  */
@@ -13904,48 +15396,176 @@ write_hash_table (struct mapped_symtab *symtab,
     }
 
   htab_delete (str_table);
-  htab_delete (index_table);
+  htab_delete (symbol_hash_table);
+}
+
+/* Struct to map psymtab to CU index in the index file.  */
+struct psymtab_cu_index_map
+{
+  struct partial_symtab *psymtab;
+  unsigned int cu_index;
+};
+
+static hashval_t
+hash_psymtab_cu_index (const void *item)
+{
+  const struct psymtab_cu_index_map *map = item;
+
+  return htab_hash_pointer (map->psymtab);
+}
+
+static int
+eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
+{
+  const struct psymtab_cu_index_map *lhs = item_lhs;
+  const struct psymtab_cu_index_map *rhs = item_rhs;
+
+  return lhs->psymtab == rhs->psymtab;
 }
 
-/* Write an address entry to ADDR_OBSTACK.  The addresses are taken
-   from PST; CU_INDEX is the index of the CU in the vector of all
-   CUs.  */
+/* Helper struct for building the address table.  */
+struct addrmap_index_data
+{
+  struct objfile *objfile;
+  struct obstack *addr_obstack;
+  htab_t cu_index_htab;
+
+  /* Non-zero if the previous_* fields are valid.
+     We can't write an entry until we see the next entry (since it is only then
+     that we know the end of the entry).  */
+  int previous_valid;
+  /* Index of the CU in the table of all CUs in the index file.  */
+  unsigned int previous_cu_index;
+  /* Start address of the CU.  */
+  CORE_ADDR previous_cu_start;
+};
+
+/* Write an address entry to OBSTACK.  */
+
 static void
-add_address_entry (struct objfile *objfile,
-                  struct obstack *addr_obstack, struct partial_symtab *pst,
-                  unsigned int cu_index)
+add_address_entry (struct objfile *objfile, struct obstack *obstack,
+                  CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
 {
-  offset_type offset;
+  offset_type cu_index_to_write;
   char addr[8];
   CORE_ADDR baseaddr;
 
   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
 
-  store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
-  obstack_grow (addr_obstack, addr, 8);
-  store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
-  obstack_grow (addr_obstack, addr, 8);
-  offset = MAYBE_SWAP (cu_index);
-  obstack_grow (addr_obstack, &offset, sizeof (offset_type));
+  store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
+  obstack_grow (obstack, addr, 8);
+  store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
+  obstack_grow (obstack, addr, 8);
+  cu_index_to_write = MAYBE_SWAP (cu_index);
+  obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
+}
+
+/* Worker function for traversing an addrmap to build the address table.  */
+
+static int
+add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
+{
+  struct addrmap_index_data *data = datap;
+  struct partial_symtab *pst = obj;
+  offset_type cu_index;
+  void **slot;
+
+  if (data->previous_valid)
+    add_address_entry (data->objfile, data->addr_obstack,
+                      data->previous_cu_start, start_addr,
+                      data->previous_cu_index);
+
+  data->previous_cu_start = start_addr;
+  if (pst != NULL)
+    {
+      struct psymtab_cu_index_map find_map, *map;
+      find_map.psymtab = pst;
+      map = htab_find (data->cu_index_htab, &find_map);
+      gdb_assert (map != NULL);
+      data->previous_cu_index = map->cu_index;
+      data->previous_valid = 1;
+    }
+  else
+      data->previous_valid = 0;
+
+  return 0;
+}
+
+/* Write OBJFILE's address map to OBSTACK.
+   CU_INDEX_HTAB is used to map addrmap entries to their CU indices
+   in the index file.  */
+
+static void
+write_address_map (struct objfile *objfile, struct obstack *obstack,
+                  htab_t cu_index_htab)
+{
+  struct addrmap_index_data addrmap_index_data;
+
+  /* When writing the address table, we have to cope with the fact that
+     the addrmap iterator only provides the start of a region; we have to
+     wait until the next invocation to get the start of the next region.  */
+
+  addrmap_index_data.objfile = objfile;
+  addrmap_index_data.addr_obstack = obstack;
+  addrmap_index_data.cu_index_htab = cu_index_htab;
+  addrmap_index_data.previous_valid = 0;
+
+  addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
+                  &addrmap_index_data);
+
+  /* It's highly unlikely the last entry (end address = 0xff...ff)
+     is valid, but we should still handle it.
+     The end address is recorded as the start of the next region, but that
+     doesn't work here.  To cope we pass 0xff...ff, this is a rare situation
+     anyway.  */
+  if (addrmap_index_data.previous_valid)
+    add_address_entry (objfile, obstack,
+                      addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
+                      addrmap_index_data.previous_cu_index);
 }
 
 /* Add a list of partial symbols to SYMTAB.  */
+
 static void
 write_psymbols (struct mapped_symtab *symtab,
+               htab_t psyms_seen,
                struct partial_symbol **psymp,
                int count,
-               offset_type cu_index)
+               offset_type cu_index,
+               int is_static)
 {
   for (; count-- > 0; ++psymp)
     {
+      void **slot, *lookup;
+
       if (SYMBOL_LANGUAGE (*psymp) == language_ada)
        error (_("Ada is not currently supported by the index"));
-      add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
+
+      /* We only want to add a given psymbol once.  However, we also
+        want to account for whether it is global or static.  So, we
+        may add it twice, using slightly different values.  */
+      if (is_static)
+       {
+         uintptr_t val = 1 | (uintptr_t) *psymp;
+
+         lookup = (void *) val;
+       }
+      else
+       lookup = *psymp;
+
+      /* Only add a given psymbol once.  */
+      slot = htab_find_slot (psyms_seen, lookup, INSERT);
+      if (!*slot)
+       {
+         *slot = lookup;
+         add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
+       }
     }
 }
 
 /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
    exception if there is an error.  */
+
 static void
 write_obstack (FILE *file, struct obstack *obstack)
 {
@@ -13956,6 +15576,7 @@ write_obstack (FILE *file, struct obstack *obstack)
 }
 
 /* Unlink a file if the argument is not NULL.  */
+
 static void
 unlink_if_set (void *p)
 {
@@ -13964,19 +15585,79 @@ unlink_if_set (void *p)
     unlink (*filename);
 }
 
+/* A helper struct used when iterating over debug_types.  */
+struct signatured_type_index_data
+{
+  struct objfile *objfile;
+  struct mapped_symtab *symtab;
+  struct obstack *types_list;
+  htab_t psyms_seen;
+  int cu_index;
+};
+
+/* A helper function that writes a single signatured_type to an
+   obstack.  */
+
+static int
+write_one_signatured_type (void **slot, void *d)
+{
+  struct signatured_type_index_data *info = d;
+  struct signatured_type *entry = (struct signatured_type *) *slot;
+  struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
+  struct partial_symtab *psymtab = per_cu->v.psymtab;
+  gdb_byte val[8];
+
+  write_psymbols (info->symtab,
+                 info->psyms_seen,
+                 info->objfile->global_psymbols.list
+                 + psymtab->globals_offset,
+                 psymtab->n_global_syms, info->cu_index,
+                 0);
+  write_psymbols (info->symtab,
+                 info->psyms_seen,
+                 info->objfile->static_psymbols.list
+                 + psymtab->statics_offset,
+                 psymtab->n_static_syms, info->cu_index,
+                 1);
+
+  store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
+  obstack_grow (info->types_list, val, 8);
+  store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
+  obstack_grow (info->types_list, val, 8);
+  store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
+  obstack_grow (info->types_list, val, 8);
+
+  ++info->cu_index;
+
+  return 1;
+}
+
+/* A cleanup function for an htab_t.  */
+
+static void
+cleanup_htab (void *arg)
+{
+  htab_delete (arg);
+}
+
 /* Create an index file for OBJFILE in the directory DIR.  */
+
 static void
 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
 {
   struct cleanup *cleanup;
   char *filename, *cleanup_filename;
-  struct obstack contents, addr_obstack, constant_pool, symtab_obstack, cu_list;
+  struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
+  struct obstack cu_list, types_cu_list;
   int i;
   FILE *out_file;
   struct mapped_symtab *symtab;
   offset_type val, size_of_contents, total_len;
   struct stat st;
   char buf[8];
+  htab_t psyms_seen;
+  htab_t cu_index_htab;
+  struct psymtab_cu_index_map *psymtab_cu_index_map;
 
   if (!objfile->psymtabs)
     return;
@@ -13984,7 +15665,7 @@ write_psymtabs_to_index (struct objfile *objfile, const char *dir)
     error (_("Cannot use an index to create the index"));
 
   if (stat (objfile->name, &st) < 0)
-    perror_with_name (_("Could not stat"));
+    perror_with_name (objfile->name);
 
   filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
                     INDEX_SUFFIX, (char *) NULL);
@@ -14006,27 +15687,81 @@ write_psymtabs_to_index (struct objfile *objfile, const char *dir)
   obstack_init (&cu_list);
   make_cleanup_obstack_free (&cu_list);
 
+  obstack_init (&types_cu_list);
+  make_cleanup_obstack_free (&types_cu_list);
+
+  psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
+                                 NULL, xcalloc, xfree);
+  make_cleanup (cleanup_htab, psyms_seen);
+
+  /* While we're scanning CU's create a table that maps a psymtab pointer
+     (which is what addrmap records) to its index (which is what is recorded
+     in the index file).  This will later be needed to write the address
+     table.  */
+  cu_index_htab = htab_create_alloc (100,
+                                    hash_psymtab_cu_index,
+                                    eq_psymtab_cu_index,
+                                    NULL, xcalloc, xfree);
+  make_cleanup (cleanup_htab, cu_index_htab);
+  psymtab_cu_index_map = (struct psymtab_cu_index_map *)
+    xmalloc (sizeof (struct psymtab_cu_index_map)
+            * dwarf2_per_objfile->n_comp_units);
+  make_cleanup (xfree, psymtab_cu_index_map);
+
+  /* The CU list is already sorted, so we don't need to do additional
+     work here.  Also, the debug_types entries do not appear in
+     all_comp_units, but only in their own hash table.  */
   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
     {
-      struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
-      struct partial_symtab *psymtab = cu->v.psymtab;
+      struct dwarf2_per_cu_data *per_cu
+       = dwarf2_per_objfile->all_comp_units[i];
+      struct partial_symtab *psymtab = per_cu->v.psymtab;
       gdb_byte val[8];
+      struct psymtab_cu_index_map *map;
+      void **slot;
 
       write_psymbols (symtab,
+                     psyms_seen,
                      objfile->global_psymbols.list + psymtab->globals_offset,
-                     psymtab->n_global_syms, i);
+                     psymtab->n_global_syms, i,
+                     0);
       write_psymbols (symtab,
+                     psyms_seen,
                      objfile->static_psymbols.list + psymtab->statics_offset,
-                     psymtab->n_static_syms, i);
+                     psymtab->n_static_syms, i,
+                     1);
 
-      add_address_entry (objfile, &addr_obstack, psymtab, i);
+      map = &psymtab_cu_index_map[i];
+      map->psymtab = psymtab;
+      map->cu_index = i;
+      slot = htab_find_slot (cu_index_htab, map, INSERT);
+      gdb_assert (slot != NULL);
+      gdb_assert (*slot == NULL);
+      *slot = map;
 
-      store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->offset);
+      store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
       obstack_grow (&cu_list, val, 8);
-      store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->length);
+      store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
       obstack_grow (&cu_list, val, 8);
     }
 
+  /* Dump the address map.  */
+  write_address_map (objfile, &addr_obstack, cu_index_htab);
+
+  /* Write out the .debug_type entries, if any.  */
+  if (dwarf2_per_objfile->signatured_types)
+    {
+      struct signatured_type_index_data sig_data;
+
+      sig_data.objfile = objfile;
+      sig_data.symtab = symtab;
+      sig_data.types_list = &types_cu_list;
+      sig_data.psyms_seen = psyms_seen;
+      sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
+      htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
+                             write_one_signatured_type, &sig_data);
+    }
+
   obstack_init (&constant_pool);
   make_cleanup_obstack_free (&constant_pool);
   obstack_init (&symtab_obstack);
@@ -14035,11 +15770,11 @@ write_psymtabs_to_index (struct objfile *objfile, const char *dir)
 
   obstack_init (&contents);
   make_cleanup_obstack_free (&contents);
-  size_of_contents = 5 * sizeof (offset_type);
+  size_of_contents = 6 * sizeof (offset_type);
   total_len = size_of_contents;
 
   /* The version number.  */
-  val = MAYBE_SWAP (1);
+  val = MAYBE_SWAP (4);
   obstack_grow (&contents, &val, sizeof (val));
 
   /* The offset of the CU list from the start of the file.  */
@@ -14047,6 +15782,11 @@ write_psymtabs_to_index (struct objfile *objfile, const char *dir)
   obstack_grow (&contents, &val, sizeof (val));
   total_len += obstack_object_size (&cu_list);
 
+  /* The offset of the types CU list from the start of the file.  */
+  val = MAYBE_SWAP (total_len);
+  obstack_grow (&contents, &val, sizeof (val));
+  total_len += obstack_object_size (&types_cu_list);
+
   /* The offset of the address table from the start of the file.  */
   val = MAYBE_SWAP (total_len);
   obstack_grow (&contents, &val, sizeof (val));
@@ -14066,6 +15806,7 @@ write_psymtabs_to_index (struct objfile *objfile, const char *dir)
 
   write_obstack (out_file, &contents);
   write_obstack (out_file, &cu_list);
+  write_obstack (out_file, &types_cu_list);
   write_obstack (out_file, &addr_obstack);
   write_obstack (out_file, &symtab_obstack);
   write_obstack (out_file, &constant_pool);
@@ -14090,26 +15831,40 @@ write_psymtabs_to_index (struct objfile *objfile, const char *dir)
 
    1. The file header.  This is a sequence of values, of offset_type
    unless otherwise noted:
-   [0] The version number.  Currently 1.
+
+   [0] The version number, currently 4.  Versions 1, 2 and 3 are
+   obsolete.
    [1] The offset, from the start of the file, of the CU list.
-   [2] The offset, from the start of the file, of the address section.
-   [3] The offset, from the start of the file, of the symbol table.
-   [4] The offset, from the start of the file, of the constant pool.
+   [2] The offset, from the start of the file, of the types CU list.
+   Note that this section can be empty, in which case this offset will
+   be equal to the next offset.
+   [3] The offset, from the start of the file, of the address section.
+   [4] The offset, from the start of the file, of the symbol table.
+   [5] The offset, from the start of the file, of the constant pool.
 
    2. The CU list.  This is a sequence of pairs of 64-bit
-   little-endian values.  The first element in each pair is the offset
-   of a CU in the .debug_info section.  The second element in each
-   pair is the length of that CU.  References to a CU elsewhere in the
-   map are done using a CU index, which is just the 0-based index into
-   this table.
-
-   3. The address section.  The address section consists of a sequence
+   little-endian values, sorted by the CU offset.  The first element
+   in each pair is the offset of a CU in the .debug_info section.  The
+   second element in each pair is the length of that CU.  References
+   to a CU elsewhere in the map are done using a CU index, which is
+   just the 0-based index into this table.  Note that if there are
+   type CUs, then conceptually CUs and type CUs form a single list for
+   the purposes of CU indices.
+
+   3. The types CU list.  This is a sequence of triplets of 64-bit
+   little-endian values.  In a triplet, the first value is the CU
+   offset, the second value is the type offset in the CU, and the
+   third value is the type signature.  The types CU list is not
+   sorted.
+
+   4. The address section.  The address section consists of a sequence
    of address entries.  Each address entry has three elements.
    [0] The low address.  This is a 64-bit little-endian value.
    [1] The high address.  This is a 64-bit little-endian value.
+       Like DW_AT_high_pc, the value is one byte beyond the end.
    [2] The CU index.  This is an offset_type value.
 
-   4. The symbol table.  This is a hash table.  The size of the hash
+   5. The symbol table.  This is a hash table.  The size of the hash
    table is always a power of 2.  The initial hash and the step are
    currently defined by the `find_slot' function.
 
@@ -14131,16 +15886,17 @@ write_psymtabs_to_index (struct objfile *objfile, const char *dir)
    element in the hash table is used to indicate which CUs define the
    symbol.
 
-   5. The constant pool.  This is simply a bunch of bytes.  It is
+   6. The constant pool.  This is simply a bunch of bytes.  It is
    organized so that alignment is correct: CU vectors are stored
    first, followed by strings.  */
+
 static void
 save_gdb_index_command (char *arg, int from_tty)
 {
   struct objfile *objfile;
 
   if (!arg || !*arg)
-    error (_("usage: maintenance save-gdb-index DIRECTORY"));
+    error (_("usage: save gdb-index DIRECTORY"));
 
   ALL_OBJFILES (objfile)
   {
@@ -14175,8 +15931,9 @@ static void
 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
                                struct cmd_list_element *c, const char *value)
 {
-  fprintf_filtered (file, _("\
-Whether to always disassemble DWARF expressions is %s.\n"),
+  fprintf_filtered (file,
+                   _("Whether to always disassemble "
+                     "DWARF expressions is %s.\n"),
                    value);
 }
 
@@ -14185,6 +15942,8 @@ void _initialize_dwarf2_read (void);
 void
 _initialize_dwarf2_read (void)
 {
+  struct cmd_list_element *c;
+
   dwarf2_objfile_data_key
     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
 
@@ -14233,7 +15992,10 @@ The value is the maximum depth to print."),
                            NULL,
                            &setdebuglist, &showdebuglist);
 
-  add_cmd ("gdb-index", class_files, save_gdb_index_command,
-          _("Save a .gdb-index file"),
-          &save_cmdlist);
+  c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
+              _("\
+Save a gdb-index file.\n\
+Usage: save gdb-index DIRECTORY"),
+              &save_cmdlist);
+  set_cmd_completer (c, filename_completer);
 }