]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - gdb/dwarf2/read.c
Make load_cu return the loaded dwarf2_cu
[thirdparty/binutils-gdb.git] / gdb / dwarf2 / read.c
index 260fb54ce863043d4e65943ba4601e38436d648b..a27c351e1cc5eda0ae510c2913fbcf0c21a5b9c3 100644 (file)
@@ -614,7 +614,9 @@ struct stmt_list_hash
 };
 
 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
-   an object of this type.  */
+   an object of this type.  This contains elements of type unit groups
+   that can be shared across objfiles.  The non-shareable parts are in
+   type_unit_group_unshareable.  */
 
 struct type_unit_group
 {
@@ -629,23 +631,8 @@ struct type_unit_group
      and is deleted afterwards and not used again.  */
   std::vector<signatured_type *> *tus;
 
-  /* The compunit symtab.
-     Type units in a group needn't all be defined in the same source file,
-     so we create an essentially anonymous symtab as the compunit symtab.  */
-  struct compunit_symtab *compunit_symtab;
-
   /* The data used to construct the hash key.  */
   struct stmt_list_hash hash;
-
-  /* The symbol tables for this TU (obtained from the files listed in
-     DW_AT_stmt_list).
-     WARNING: The order of entries here must match the order of entries
-     in the line header.  After the first TU using this type_unit_group, the
-     line header for the subsequent TUs is recreated from this.  This is done
-     because we need to use the same symtabs for each TU using the same
-     DW_AT_stmt_list value.  Also note that symtabs may be repeated here,
-     there's no guarantee the line header doesn't have duplicate entries.  */
-  struct symtab **symtabs;
 };
 
 /* These sections are what may appear in a (real or virtual) DWO file.  */
@@ -1568,7 +1555,8 @@ static void prepare_one_comp_unit (struct dwarf2_cu *cu,
 
 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
 
-static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
+static void free_one_cached_comp_unit (dwarf2_per_cu_data *target_per_cu,
+                                      dwarf2_per_objfile *per_objfile);
 
 static struct type *set_die_type (struct die_info *, struct type *,
                                  struct dwarf2_cu *);
@@ -1582,12 +1570,10 @@ static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
                                 bool skip_partial,
                                 enum language pretend_language);
 
-static void process_full_comp_unit (dwarf2_per_cu_data *per_cu,
-                                   dwarf2_per_objfile *per_objfile,
+static void process_full_comp_unit (dwarf2_cu *cu,
                                    enum language pretend_language);
 
-static void process_full_type_unit (dwarf2_per_cu_data *per_cu,
-                                   dwarf2_per_objfile *per_objfile,
+static void process_full_type_unit (dwarf2_cu *cu,
                                    enum language pretend_language);
 
 static void dwarf2_add_dependence (struct dwarf2_cu *,
@@ -1598,11 +1584,13 @@ 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 (sect_offset,
-                                           struct dwarf2_per_cu_data *);
+                                           dwarf2_per_cu_data *per_cu,
+                                           dwarf2_per_objfile *per_objfile);
 
 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
 
-static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
+static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
+                            dwarf2_per_objfile *per_objfile,
                             enum language pretend_language);
 
 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
@@ -1642,7 +1630,7 @@ dwarf2_queue_item::~dwarf2_queue_item ()
   if (per_cu->queued)
     {
       if (per_cu->cu != NULL)
-       free_one_cached_comp_unit (per_cu);
+       free_one_cached_comp_unit (per_cu, per_objfile);
       per_cu->queued = 0;
     }
 }
@@ -1688,7 +1676,7 @@ static struct dwo_unit *lookup_dwo_comp_unit
 static struct dwo_unit *lookup_dwo_type_unit
   (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
 
-static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
+static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
 
 /* A unique pointer to a dwo_file.  */
 
@@ -2347,7 +2335,7 @@ create_quick_file_names_table (unsigned int nr_initial_entries)
    function is unrelated to symtabs, symtab would have to be created afterwards.
    You should call age_cached_comp_units after processing the CU.  */
 
-static void
+static dwarf2_cu *
 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
         bool skip_partial)
 {
@@ -2356,10 +2344,12 @@ load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
   else
     load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
 
-  if (per_cu->cu == NULL)
-    return;  /* Dummy CU.  */
+  if (per_cu->cu == nullptr)
+    return nullptr;  /* Dummy CU.  */
 
   dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
+
+  return per_cu->cu;
 }
 
 /* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE.  */
@@ -2381,20 +2371,20 @@ dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
 
   if (!dwarf2_per_objfile->symtab_set_p (per_cu))
     {
-      queue_comp_unit (per_cu, language_minimal);
-      load_cu (per_cu, dwarf2_per_objfile, skip_partial);
+      queue_comp_unit (per_cu, dwarf2_per_objfile, language_minimal);
+      dwarf2_cu *cu = load_cu (per_cu, dwarf2_per_objfile, skip_partial);
 
       /* If we just loaded a CU from a DWO, and we're working with an index
         that may badly handle TUs, load all the TUs in that DWO as well.
         http://sourceware.org/bugzilla/show_bug.cgi?id=15021  */
       if (!per_cu->is_debug_types
-         && per_cu->cu != NULL
-         && per_cu->cu->dwo_unit != NULL
+         && cu != NULL
+         && cu->dwo_unit != NULL
          && dwarf2_per_objfile->per_bfd->index_table != NULL
          && dwarf2_per_objfile->per_bfd->index_table->version <= 7
          /* DWP files aren't supported yet.  */
          && get_dwp_file (dwarf2_per_objfile) == NULL)
-       queue_and_load_all_dwo_tus (per_cu);
+       queue_and_load_all_dwo_tus (cu);
     }
 
   process_queue (dwarf2_per_objfile);
@@ -2484,21 +2474,20 @@ dwarf2_per_bfd::allocate_signatured_type ()
   return result;
 }
 
-/* Return a new dwarf2_per_cu_data allocated on the dwarf2_per_objfile
+/* Return a new dwarf2_per_cu_data allocated on the per-bfd
    obstack, and constructed with the specified field values.  */
 
 static dwarf2_per_cu_data *
-create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
-                          struct dwarf2_section_info *section,
-                          int is_dwz,
-                          sect_offset sect_off, ULONGEST length)
+create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
+                          struct dwarf2_section_info *section,
+                          int is_dwz,
+                          sect_offset sect_off, ULONGEST length)
 {
-  dwarf2_per_cu_data *the_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
+  dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
   the_cu->sect_off = sect_off;
   the_cu->length = length;
-  the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
   the_cu->section = section;
-  the_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
+  the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
                                    struct dwarf2_per_cu_quick_data);
   the_cu->is_dwz = is_dwz;
   return the_cu;
@@ -2508,7 +2497,7 @@ create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
    CUs.  */
 
 static void
-create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
+create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
                            const gdb_byte *cu_list, offset_type n_elements,
                            struct dwarf2_section_info *section,
                            int is_dwz)
@@ -2523,32 +2512,31 @@ create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
       cu_list += 2 * 8;
 
       dwarf2_per_cu_data *per_cu
-       = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
-                                    sect_off, length);
-      dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
+       = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
+                                    length);
+      per_bfd->all_comp_units.push_back (per_cu);
     }
 }
 
 /* Read the CU list from the mapped index, and use it to create all
-   the CU objects for this objfile.  */
+   the CU objects for PER_BFD.  */
 
 static void
-create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
+create_cus_from_index (dwarf2_per_bfd *per_bfd,
                       const gdb_byte *cu_list, offset_type cu_list_elements,
                       const gdb_byte *dwz_list, offset_type dwz_elements)
 {
-  gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
-  dwarf2_per_objfile->per_bfd->all_comp_units.reserve
-    ((cu_list_elements + dwz_elements) / 2);
+  gdb_assert (per_bfd->all_comp_units.empty ());
+  per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
 
-  create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
-                             &dwarf2_per_objfile->per_bfd->info, 0);
+  create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
+                             &per_bfd->info, 0);
 
   if (dwz_elements == 0)
     return;
 
-  dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
-  create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
+  dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
+  create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
                              &dwz->info, 1);
 }
 
@@ -2556,13 +2544,11 @@ create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
 
 static void
 create_signatured_type_table_from_index
-  (struct dwarf2_per_objfile *dwarf2_per_objfile,
-   struct dwarf2_section_info *section,
-   const gdb_byte *bytes,
-   offset_type elements)
+  (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
+   const gdb_byte *bytes, offset_type elements)
 {
-  gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
-  dwarf2_per_objfile->per_bfd->all_type_units.reserve (elements / 3);
+  gdb_assert (per_bfd->all_type_units.empty ());
+  per_bfd->all_type_units.reserve (elements / 3);
 
   htab_up sig_types_hash = allocate_signatured_type_table ();
 
@@ -2582,24 +2568,23 @@ create_signatured_type_table_from_index
       signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
       bytes += 3 * 8;
 
-      sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
+      sig_type = per_bfd->allocate_signatured_type ();
       sig_type->signature = signature;
       sig_type->type_offset_in_tu = type_offset_in_tu;
       sig_type->per_cu.is_debug_types = 1;
       sig_type->per_cu.section = section;
       sig_type->per_cu.sect_off = sect_off;
-      sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
       sig_type->per_cu.v.quick
-       = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
+       = OBSTACK_ZALLOC (&per_bfd->obstack,
                          struct dwarf2_per_cu_quick_data);
 
       slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
       *slot = sig_type;
 
-      dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
+      per_bfd->all_type_units.push_back (sig_type);
     }
 
-  dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
+  per_bfd->signatured_types = std::move (sig_types_hash);
 }
 
 /* Create the signatured type hash table from .debug_names.  */
@@ -2644,7 +2629,6 @@ create_signatured_type_table_from_debug_names
       sig_type->per_cu.is_debug_types = 1;
       sig_type->per_cu.section = section;
       sig_type->per_cu.sect_off = sect_off;
-      sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
       sig_type->per_cu.v.quick
        = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
                          struct dwarf2_per_cu_quick_data);
@@ -3119,7 +3103,7 @@ dwarf2_read_gdb_index
        }
     }
 
-  create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
+  create_cus_from_index (dwarf2_per_objfile->per_bfd, cu_list, cu_list_elements,
                         dwz_list, dwz_list_elements);
 
   if (types_list_elements)
@@ -3131,8 +3115,9 @@ dwarf2_read_gdb_index
 
       dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
 
-      create_signatured_type_table_from_index (dwarf2_per_objfile, section,
-                                              types_list, types_list_elements);
+      create_signatured_type_table_from_index (dwarf2_per_objfile->per_bfd,
+                                              section, types_list,
+                                              types_list_elements);
     }
 
   create_addrmap_from_index (dwarf2_per_objfile, map.get ());
@@ -5130,7 +5115,7 @@ read_debug_names_from_section (struct objfile *objfile,
    list.  */
 
 static void
-create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
+create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
                                  const mapped_debug_names &map,
                                  dwarf2_section_info &section,
                                  bool is_dwz)
@@ -5149,9 +5134,8 @@ create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
           the next CU as end of this CU.  We create the CUs here with length 0,
           and in cutu_reader::cutu_reader we'll fill in the actual length.  */
        dwarf2_per_cu_data *per_cu
-         = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
-                                      sect_off, 0);
-       dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
+         = create_cu_from_index_list (per_bfd, &section, is_dwz, sect_off, 0);
+       per_bfd->all_comp_units.push_back (per_cu);
       }
     }
 
@@ -5173,9 +5157,9 @@ create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
        {
          const ULONGEST length = sect_off_next - sect_off_prev;
          dwarf2_per_cu_data *per_cu
-           = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
+           = create_cu_from_index_list (per_bfd, &section, is_dwz,
                                         sect_off_prev, length);
-         dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
+         per_bfd->all_comp_units.push_back (per_cu);
        }
       sect_off_prev = sect_off_next;
     }
@@ -5185,22 +5169,21 @@ create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
    the CU objects for this dwarf2_per_objfile.  */
 
 static void
-create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
+create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
                             const mapped_debug_names &map,
                             const mapped_debug_names &dwz_map)
 {
-  gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
-  dwarf2_per_objfile->per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
+  gdb_assert (per_bfd->all_comp_units.empty ());
+  per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
 
-  create_cus_from_debug_names_list (dwarf2_per_objfile, map,
-                                   dwarf2_per_objfile->per_bfd->info,
+  create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
                                    false /* is_dwz */);
 
   if (dwz_map.cu_count == 0)
     return;
 
-  dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
-  create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
+  dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
+  create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
                                    true /* is_dwz */);
 }
 
@@ -5239,7 +5222,7 @@ dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
        }
     }
 
-  create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
+  create_cus_from_debug_names (dwarf2_per_objfile->per_bfd, *map, dwz_map);
 
   if (map->tu_count != 0)
     {
@@ -6334,7 +6317,6 @@ create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
          sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
          sig_type->signature = header.signature;
          sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
-         sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
          sig_type->per_cu.is_debug_types = 1;
          sig_type->per_cu.section = section;
          sig_type->per_cu.sect_off = sect_off;
@@ -6500,7 +6482,6 @@ fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
   sig_entry->per_cu.sect_off = dwo_entry->sect_off;
   sig_entry->per_cu.length = dwo_entry->length;
   sig_entry->per_cu.reading_dwo_directly = 1;
-  sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
   sig_entry->per_cu.per_bfd = per_bfd;
   sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
   sig_entry->dwo_unit = dwo_entry;
@@ -7316,7 +7297,6 @@ create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
   tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
                             struct type_unit_group);
   per_cu = &tu_group->per_cu;
-  per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
   per_cu->per_bfd = per_bfd;
 
   if (per_bfd->using_index)
@@ -7579,7 +7559,7 @@ process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
      read in the compilation unit (see load_partial_dies).
      This problem could be avoided, but the benefit is unclear.  */
   if (this_cu->cu != NULL)
-    free_one_cached_comp_unit (this_cu);
+    free_one_cached_comp_unit (this_cu, per_objfile);
 
   cutu_reader reader (this_cu, per_objfile, NULL, 0, false);
 
@@ -7611,7 +7591,7 @@ process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
   this_cu->lang = this_cu->cu->language;
 
   /* Age out any secondary CUs.  */
-  age_cached_comp_units (this_cu->dwarf2_per_objfile);
+  age_cached_comp_units (per_objfile);
 }
 
 /* Reader function for build_type_psymtabs.  */
@@ -8066,7 +8046,6 @@ read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
       this_cu->sect_off = sect_off;
       this_cu->length = cu_header.length + cu_header.initial_length_size;
       this_cu->is_dwz = is_dwz;
-      this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
       this_cu->section = section;
 
       dwarf2_per_objfile->per_bfd->all_comp_units.push_back (this_cu);
@@ -8937,11 +8916,12 @@ dwarf2_psymtab::read_symtab (struct objfile *objfile)
 /* Add PER_CU to the queue.  */
 
 static void
-queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
+queue_comp_unit (dwarf2_per_cu_data *per_cu,
+                dwarf2_per_objfile *per_objfile,
                 enum language pretend_language)
 {
   per_cu->queued = 1;
-  per_cu->per_bfd->queue.emplace (per_cu, pretend_language);
+  per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
 }
 
 /* If PER_CU is not yet queued, add it to the queue.
@@ -8955,7 +8935,8 @@ queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
 
 static int
 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
-                      struct dwarf2_per_cu_data *per_cu,
+                      dwarf2_per_cu_data *per_cu,
+                      dwarf2_per_objfile *per_objfile,
                       enum language pretend_language)
 {
   /* We may arrive here during partial symbol reading, if we need full
@@ -8986,7 +8967,7 @@ maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
     }
 
   /* Add it to the queue.  */
-  queue_comp_unit (per_cu, pretend_language);
+  queue_comp_unit (per_cu, per_objfile,  pretend_language);
 
   return 1;
 }
@@ -9040,11 +9021,9 @@ process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
            fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
 
          if (per_cu->is_debug_types)
-           process_full_type_unit (per_cu, dwarf2_per_objfile,
-                                   item.pretend_language);
+           process_full_type_unit (per_cu->cu, item.pretend_language);
          else
-           process_full_comp_unit (per_cu, dwarf2_per_objfile,
-                                   item.pretend_language);
+           process_full_comp_unit (per_cu->cu, item.pretend_language);
 
          if (dwarf_read_debug >= debug_print_threshold)
            fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
@@ -9630,6 +9609,40 @@ rust_union_quirks (struct dwarf2_cu *cu)
   cu->rust_unions.clear ();
 }
 
+/* See read.h.  */
+
+type_unit_group_unshareable *
+dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
+{
+  auto iter = this->m_type_units.find (tu_group);
+  if (iter != this->m_type_units.end ())
+    return iter->second.get ();
+
+  type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
+  type_unit_group_unshareable *result = uniq.get ();
+  this->m_type_units[tu_group] = std::move (uniq);
+  return result;
+}
+
+struct type *
+dwarf2_per_objfile::get_type_for_signatured_type
+  (signatured_type *sig_type) const
+{
+  auto iter = this->m_type_map.find (sig_type);
+  if (iter == this->m_type_map.end ())
+    return nullptr;
+
+  return iter->second;
+}
+
+void dwarf2_per_objfile::set_type_for_signatured_type
+  (signatured_type *sig_type, struct type *type)
+{
+  gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
+
+  this->m_type_map[sig_type] = type;
+}
+
 /* A helper function for computing the list of all symbol tables
    included by PER_CU.  */
 
@@ -9718,7 +9731,7 @@ compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
       /* Now we have a transitive closure of all the included symtabs.  */
       len = result_symtabs.size ();
       cust->includes
-       = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
+       = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
                     struct compunit_symtab *, len + 1);
       memcpy (cust->includes, result_symtabs.data (),
              len * sizeof (compunit_symtab *));
@@ -9744,15 +9757,13 @@ process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
   dwarf2_per_objfile->per_bfd->just_read_cus.clear ();
 }
 
-/* Generate full symbol information for PER_CU, whose DIEs have
+/* Generate full symbol information for CU, whose DIEs have
    already been loaded into memory.  */
 
 static void
-process_full_comp_unit (dwarf2_per_cu_data *per_cu,
-                       dwarf2_per_objfile *dwarf2_per_objfile,
-                       enum language pretend_language)
+process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
 {
-  struct dwarf2_cu *cu = per_cu->cu;
+  dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
   struct objfile *objfile = dwarf2_per_objfile->objfile;
   struct gdbarch *gdbarch = objfile->arch ();
   CORE_ADDR lowpc, highpc;
@@ -9835,30 +9846,29 @@ process_full_comp_unit (dwarf2_per_cu_data *per_cu,
       cust->call_site_htab = cu->call_site_htab;
     }
 
-  dwarf2_per_objfile->set_symtab (per_cu, cust);
+  dwarf2_per_objfile->set_symtab (cu->per_cu, cust);
 
   /* Push it for inclusion processing later.  */
-  dwarf2_per_objfile->per_bfd->just_read_cus.push_back (per_cu);
+  dwarf2_per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
 
   /* Not needed any more.  */
   cu->reset_builder ();
 }
 
-/* Generate full symbol information for type unit PER_CU, whose DIEs have
+/* Generate full symbol information for type unit CU, whose DIEs have
    already been loaded into memory.  */
 
 static void
-process_full_type_unit (dwarf2_per_cu_data *per_cu,
-                       dwarf2_per_objfile *dwarf2_per_objfile,
+process_full_type_unit (dwarf2_cu *cu,
                        enum language pretend_language)
 {
-  struct dwarf2_cu *cu = per_cu->cu;
+  dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
   struct objfile *objfile = dwarf2_per_objfile->objfile;
   struct compunit_symtab *cust;
   struct signatured_type *sig_type;
 
-  gdb_assert (per_cu->is_debug_types);
-  sig_type = (struct signatured_type *) per_cu;
+  gdb_assert (cu->per_cu->is_debug_types);
+  sig_type = (struct signatured_type *) cu->per_cu;
 
   /* Clear the list here in case something was left over.  */
   cu->method_list.clear ();
@@ -9885,11 +9895,13 @@ process_full_type_unit (dwarf2_per_cu_data *per_cu,
      If this is the first TU to use this symtab, complete the construction
      of it with end_expandable_symtab.  Otherwise, complete the addition of
      this TU's symbols to the existing symtab.  */
-  if (sig_type->type_unit_group->compunit_symtab == NULL)
+  type_unit_group_unshareable *tug_unshare =
+    dwarf2_per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
+  if (tug_unshare->compunit_symtab == NULL)
     {
       buildsym_compunit *builder = cu->get_builder ();
       cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
-      sig_type->type_unit_group->compunit_symtab = cust;
+      tug_unshare->compunit_symtab = cust;
 
       if (cust != NULL)
        {
@@ -9905,10 +9917,10 @@ process_full_type_unit (dwarf2_per_cu_data *per_cu,
   else
     {
       cu->get_builder ()->augment_type_symtab ();
-      cust = sig_type->type_unit_group->compunit_symtab;
+      cust = tug_unshare->compunit_symtab;
     }
 
-  dwarf2_per_objfile->set_symtab (per_cu, cust);
+  dwarf2_per_objfile->set_symtab (cu->per_cu, cust);
 
   /* Not needed any more.  */
   cu->reset_builder ();
@@ -9947,7 +9959,7 @@ process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
        return;
 
       /* If necessary, add it to the queue and load its DIEs.  */
-      if (maybe_queue_comp_unit (cu, per_cu, cu->language))
+      if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
        load_full_comp_unit (per_cu, per_objfile, false, cu->language);
 
       cu->per_cu->imported_symtabs_push (per_cu);
@@ -10354,7 +10366,8 @@ dwarf2_compute_name (const char *name,
                        v = dwarf2_evaluate_loc_desc (type, NULL,
                                                      baton->data,
                                                      baton->size,
-                                                     baton->per_cu);
+                                                     baton->per_cu,
+                                                     baton->per_objfile);
                      else if (bytes != NULL)
                        {
                          v = allocate_value (type);
@@ -10589,7 +10602,7 @@ read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
          struct type *type;
          sect_offset sect_off = attr->get_ref_die_offset ();
 
-         type = get_die_type_at_offset (sect_off, cu->per_cu);
+         type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
          if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
            {
              /* This declaration is a global namespace alias.  Add
@@ -11043,7 +11056,9 @@ dwarf2_cu::setup_type_unit_groups (struct die_info *die)
      do it again, we could fake it and just recreate the part we need
      (file name,index -> symtab mapping).  If data shows this optimization
      is useful we can do it then.  */
-  first_time = tu_group->compunit_symtab == NULL;
+  type_unit_group_unshareable *tug_unshare
+    = per_objfile->get_type_unit_group_unshareable (tu_group);
+  first_time = tug_unshare->compunit_symtab == NULL;
 
   /* We have to handle the case of both a missing DW_AT_stmt_list or bad
      debug info.  */
@@ -11059,9 +11074,9 @@ dwarf2_cu::setup_type_unit_groups (struct die_info *die)
        start_symtab ("", NULL, 0);
       else
        {
-         gdb_assert (tu_group->symtabs == NULL);
+         gdb_assert (tug_unshare->symtabs == NULL);
          gdb_assert (m_builder == nullptr);
-         struct compunit_symtab *cust = tu_group->compunit_symtab;
+         struct compunit_symtab *cust = tug_unshare->compunit_symtab;
          m_builder.reset (new struct buildsym_compunit
                           (COMPUNIT_OBJFILE (cust), "",
                            COMPUNIT_DIRNAME (cust),
@@ -11084,7 +11099,7 @@ dwarf2_cu::setup_type_unit_groups (struct die_info *die)
         process_full_type_unit still needs to know if this is the first
         time.  */
 
-      tu_group->symtabs
+      tug_unshare->symtabs
        = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
                     struct symtab *, line_header->file_names_size ());
 
@@ -11107,13 +11122,13 @@ dwarf2_cu::setup_type_unit_groups (struct die_info *die)
            }
 
          fe.symtab = b->get_current_subfile ()->symtab;
-         tu_group->symtabs[i] = fe.symtab;
+         tug_unshare->symtabs[i] = fe.symtab;
        }
     }
   else
     {
       gdb_assert (m_builder == nullptr);
-      struct compunit_symtab *cust = tu_group->compunit_symtab;
+      struct compunit_symtab *cust = tug_unshare->compunit_symtab;
       m_builder.reset (new struct buildsym_compunit
                       (COMPUNIT_OBJFILE (cust), "",
                        COMPUNIT_DIRNAME (cust),
@@ -11125,7 +11140,7 @@ dwarf2_cu::setup_type_unit_groups (struct die_info *die)
       for (i = 0; i < file_names.size (); ++i)
        {
          file_entry &fe = file_names[i];
-         fe.symtab = tu_group->symtabs[i];
+         fe.symtab = tug_unshare->symtabs[i];
        }
     }
 
@@ -11343,7 +11358,6 @@ create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
       sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
 
       memset (&per_cu, 0, sizeof (per_cu));
-      per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
       per_cu.per_bfd = per_bfd;
       per_cu.is_debug_types = 0;
       per_cu.sect_off = sect_offset (info_ptr - section.buffer);
@@ -12860,7 +12874,7 @@ queue_and_load_dwo_tu (void **slot, void *info)
       /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
         a real dependency of PER_CU on SIG_TYPE.  That is detected later
         while processing PER_CU.  */
-      if (maybe_queue_comp_unit (NULL, sig_cu, cu->language))
+      if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
        load_full_type_unit (sig_cu, cu->per_objfile);
       cu->per_cu->imported_symtabs_push (sig_cu);
     }
@@ -12868,28 +12882,27 @@ queue_and_load_dwo_tu (void **slot, void *info)
   return 1;
 }
 
-/* Queue all TUs contained in the DWO of PER_CU to be read in.
+/* Queue all TUs contained in the DWO of CU to be read in.
    The DWO may have the only definition of the type, though it may not be
    referenced anywhere in PER_CU.  Thus we have to load *all* its TUs.
    http://sourceware.org/bugzilla/show_bug.cgi?id=15021  */
 
 static void
-queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
+queue_and_load_all_dwo_tus (dwarf2_cu *cu)
 {
   struct dwo_unit *dwo_unit;
   struct dwo_file *dwo_file;
 
-  gdb_assert (!per_cu->is_debug_types);
-  gdb_assert (per_cu->cu != NULL);
-  gdb_assert (get_dwp_file (per_cu->cu->per_objfile) == NULL);
+  gdb_assert (cu != nullptr);
+  gdb_assert (!cu->per_cu->is_debug_types);
+  gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
 
-  dwo_unit = per_cu->cu->dwo_unit;
+  dwo_unit = cu->dwo_unit;
   gdb_assert (dwo_unit != NULL);
 
   dwo_file = dwo_unit->dwo_file;
   if (dwo_file->tus != NULL)
-    htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
-                           per_cu->cu);
+    htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
 }
 
 /* Read in various DIEs.  */
@@ -13498,6 +13511,7 @@ read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
               sect_offset_str (die->sect_off), objfile_name (objfile));
 
   call_site->per_cu = cu->per_cu;
+  call_site->per_objfile = per_objfile;
 
   for (child_die = die->child;
        child_die && child_die->tag;
@@ -20586,7 +20600,7 @@ dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
   gdb_assert (m_builder == nullptr);
 
   m_builder.reset (new struct buildsym_compunit
-                  (per_cu->dwarf2_per_objfile->objfile,
+                  (this->per_objfile->objfile,
                    name, comp_dir, language, low_pc));
 
   list_in_scope = get_builder ()->get_file_symbols ();
@@ -21384,13 +21398,14 @@ lookup_die_type (struct die_info *die, const struct attribute *attr,
 
       per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
                                                 dwarf2_per_objfile);
-      this_type = get_die_type_at_offset (sect_off, per_cu);
+      this_type = get_die_type_at_offset (sect_off, per_cu, dwarf2_per_objfile);
     }
   else if (attr->form_is_ref ())
     {
       sect_offset sect_off = attr->get_ref_die_offset ();
 
-      this_type = get_die_type_at_offset (sect_off, cu->per_cu);
+      this_type = get_die_type_at_offset (sect_off, cu->per_cu,
+                                         dwarf2_per_objfile);
     }
   else if (attr->form == DW_FORM_ref_sig8)
     {
@@ -22245,7 +22260,7 @@ follow_die_offset (sect_offset sect_off, int offset_in_dwz,
                                                 dwarf2_per_objfile);
 
       /* If necessary, add it to the queue and load its DIEs.  */
-      if (maybe_queue_comp_unit (cu, per_cu, cu->language))
+      if (maybe_queue_comp_unit (cu, per_cu, dwarf2_per_objfile, cu->language))
        load_full_comp_unit (per_cu, dwarf2_per_objfile, false, cu->language);
 
       target_cu = per_cu->cu;
@@ -22303,16 +22318,16 @@ dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
                               CORE_ADDR (*get_frame_pc) (void *baton),
                               void *baton, bool resolve_abstract_p)
 {
-  struct dwarf2_cu *cu;
   struct die_info *die;
   struct attribute *attr;
   struct dwarf2_locexpr_baton retval;
   struct objfile *objfile = dwarf2_per_objfile->objfile;
 
-  if (per_cu->cu == NULL)
-    load_cu (per_cu, dwarf2_per_objfile, false);
-  cu = per_cu->cu;
-  if (cu == NULL)
+  dwarf2_cu *cu = per_cu->cu;
+  if (cu == nullptr)
+    cu = load_cu (per_cu, dwarf2_per_objfile, false);
+
+  if (cu == nullptr)
     {
       /* We shouldn't get here for a dummy CU, but don't crash on the user.
         Instead just throw an error, not much else we can do.  */
@@ -22441,7 +22456,6 @@ dwarf2_fetch_constant_bytes (sect_offset sect_off,
                             obstack *obstack,
                             LONGEST *len)
 {
-  struct dwarf2_cu *cu;
   struct die_info *die;
   struct attribute *attr;
   const gdb_byte *result = NULL;
@@ -22450,10 +22464,11 @@ dwarf2_fetch_constant_bytes (sect_offset sect_off,
   enum bfd_endian byte_order;
   struct objfile *objfile = per_objfile->objfile;
 
-  if (per_cu->cu == NULL)
-    load_cu (per_cu, per_objfile, false);
-  cu = per_cu->cu;
-  if (cu == NULL)
+  dwarf2_cu *cu = per_cu->cu;
+  if (cu == nullptr)
+    cu = load_cu (per_cu, per_objfile, false);
+
+  if (cu == nullptr)
     {
       /* We shouldn't get here for a dummy CU, but don't crash on the user.
         Instead just throw an error, not much else we can do.  */
@@ -22570,14 +22585,14 @@ dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
                                dwarf2_per_cu_data *per_cu,
                                dwarf2_per_objfile *per_objfile)
 {
-  struct dwarf2_cu *cu;
   struct die_info *die;
 
-  if (per_cu->cu == NULL)
-    load_cu (per_cu, per_objfile, false);
-  cu = per_cu->cu;
-  if (!cu)
-    return NULL;
+  dwarf2_cu *cu = per_cu->cu;
+  if (cu == nullptr)
+    cu = load_cu (per_cu, per_objfile, false);
+
+  if (cu == nullptr)
+    return nullptr;
 
   die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
   if (!die)
@@ -22590,10 +22605,11 @@ dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
 
 struct type *
 dwarf2_get_die_type (cu_offset die_offset,
-                    struct dwarf2_per_cu_data *per_cu)
+                    dwarf2_per_cu_data *per_cu,
+                    dwarf2_per_objfile *per_objfile)
 {
   sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
-  return get_die_type_at_offset (die_offset_sect, per_cu);
+  return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
 }
 
 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
@@ -22608,6 +22624,8 @@ follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
   struct die_info temp_die;
   struct dwarf2_cu *sig_cu, *cu = *ref_cu;
   struct die_info *die;
+  dwarf2_per_objfile *dwarf2_per_objfile = (*ref_cu)->per_objfile;
+
 
   /* While it might be nice to assert sig_type->type == NULL here,
      we can get here for DW_AT_imported_declaration where we need
@@ -22615,8 +22633,9 @@ follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
 
   /* If necessary, add it to the queue and load its DIEs.  */
 
-  if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
-    read_signatured_type (sig_type, (*ref_cu)->per_objfile);
+  if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, dwarf2_per_objfile,
+                            language_minimal))
+    read_signatured_type (sig_type, dwarf2_per_objfile);
 
   sig_cu = sig_type->per_cu.cu;
   gdb_assert (sig_cu != NULL);
@@ -22626,8 +22645,6 @@ follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
                                                 to_underlying (temp_die.sect_off));
   if (die)
     {
-      struct dwarf2_per_objfile *dwarf2_per_objfile = (*ref_cu)->per_objfile;
-
       /* For .gdb_index version 7 keep track of included TUs.
         http://sourceware.org/bugzilla/show_bug.cgi?id=15021.  */
       if (dwarf2_per_objfile->per_bfd->index_table != NULL
@@ -22712,8 +22729,9 @@ get_signatured_type (struct die_info *die, ULONGEST signature,
     }
 
   /* If we already know the type we're done.  */
-  if (sig_type->type != NULL)
-    return sig_type->type;
+  type = dwarf2_per_objfile->get_type_for_signatured_type (sig_type);
+  if (type != nullptr)
+    return type;
 
   type_cu = cu;
   type_die = follow_die_sig_1 (die, sig_type, &type_cu);
@@ -22740,7 +22758,8 @@ get_signatured_type (struct die_info *die, ULONGEST signature,
                 objfile_name (dwarf2_per_objfile->objfile));
       type = build_error_marker_type (cu, die);
     }
-  sig_type->type = type;
+
+  dwarf2_per_objfile->set_type_for_signatured_type (sig_type, type);
 
   return type;
 }
@@ -23316,21 +23335,6 @@ dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
     }
 }
 
-/* See read.h.  */
-
-struct objfile *
-dwarf2_per_cu_data::objfile () const
-{
-  struct objfile *objfile = dwarf2_per_objfile->objfile;
-
-  /* Return the master objfile, so that we can report and look up the
-     correct file containing this variable.  */
-  if (objfile->separate_debug_objfile_backlink)
-    objfile = objfile->separate_debug_objfile_backlink;
-
-  return objfile;
-}
-
 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
    (CU_HEADERP is unused in such case) or prepare a temporary copy at
    CU_HEADERP first.  */
@@ -23620,11 +23624,10 @@ age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
 /* Remove a single compilation unit from the cache.  */
 
 static void
-free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
+free_one_cached_comp_unit (dwarf2_per_cu_data *target_per_cu,
+                          dwarf2_per_objfile *dwarf2_per_objfile)
 {
   struct dwarf2_per_cu_data *per_cu, **last_chain;
-  struct dwarf2_per_objfile *dwarf2_per_objfile
-    = target_per_cu->dwarf2_per_objfile;
 
   per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
   last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
@@ -23798,10 +23801,10 @@ set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
 
 static struct type *
 get_die_type_at_offset (sect_offset sect_off,
-                       struct dwarf2_per_cu_data *per_cu)
+                       dwarf2_per_cu_data *per_cu,
+                       dwarf2_per_objfile *dwarf2_per_objfile)
 {
   struct dwarf2_per_cu_offset_and_type *slot, ofs;
-  struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
 
   if (dwarf2_per_objfile->die_type_hash == NULL)
     return NULL;
@@ -23822,7 +23825,7 @@ get_die_type_at_offset (sect_offset sect_off,
 static struct type *
 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
 {
-  return get_die_type_at_offset (die->sect_off, cu->per_cu);
+  return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
 }
 
 /* Add a dependence relationship from CU to REF_PER_CU.  */