+2020-05-27  Simon Marchi  <simon.marchi@polymtl.ca>
+
+       * dwarf2/read.h (struct dwarf2_per_cu_data) <m_header,
+       m_header_read_in>: New fields.
+       <get_header>: New method.
+       * dwarf2/read.c (per_cu_header_read_in): Remove.
+       (dwarf2_per_cu_data::get_header): New.
+       (dwarf2_per_cu_data::addr_size): Update.
+       (dwarf2_per_cu_data::offset_size): Update.
+       (dwarf2_per_cu_data::ref_addr_size): Update.
+
 2020-05-27  Simon Marchi  <simon.marchi@polymtl.ca>
 
        * dwarf2/read.c (load_cu): Return dwarf2_cu.
 
     }
 }
 
-/* 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.  */
+/* See read.h.  */
 
-static const struct comp_unit_head *
-per_cu_header_read_in (struct comp_unit_head *cu_headerp,
-                      const struct dwarf2_per_cu_data *per_cu)
+const comp_unit_head *
+dwarf2_per_cu_data::get_header () const
 {
-  const gdb_byte *info_ptr;
-
-  if (per_cu->cu)
-    return &per_cu->cu->header;
+  if (!m_header_read_in)
+    {
+      const gdb_byte *info_ptr
+       = this->section->buffer + to_underlying (this->sect_off);
 
-  info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
+      memset (&m_header, 0, sizeof (m_header));
 
-  memset (cu_headerp, 0, sizeof (*cu_headerp));
-  read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
-                      rcuh_kind::COMPILE);
+      read_comp_unit_head (&m_header, info_ptr, this->section,
+                          rcuh_kind::COMPILE);
+    }
 
-  return cu_headerp;
+  return &m_header;
 }
 
 /* See read.h.  */
 int
 dwarf2_per_cu_data::addr_size () const
 {
-  struct comp_unit_head cu_header_local;
-  const struct comp_unit_head *cu_headerp;
-
-  cu_headerp = per_cu_header_read_in (&cu_header_local, this);
-
-  return cu_headerp->addr_size;
+  return this->get_header ()->addr_size;
 }
 
 /* See read.h.  */
 int
 dwarf2_per_cu_data::offset_size () const
 {
-  struct comp_unit_head cu_header_local;
-  const struct comp_unit_head *cu_headerp;
-
-  cu_headerp = per_cu_header_read_in (&cu_header_local, this);
-
-  return cu_headerp->offset_size;
+  return this->get_header ()->offset_size;
 }
 
 /* See read.h.  */
 int
 dwarf2_per_cu_data::ref_addr_size () const
 {
-  struct comp_unit_head cu_header_local;
-  const struct comp_unit_head *cu_headerp;
-
-  cu_headerp = per_cu_header_read_in (&cu_header_local, this);
+  const comp_unit_head *header = this->get_header ();
 
-  if (cu_headerp->version == 2)
-    return cu_headerp->addr_size;
+  if (header->version == 2)
+    return header->addr_size;
   else
-    return cu_headerp->offset_size;
+    return header->offset_size;
 }
 
 /* See read.h.  */
 
 
 #include <queue>
 #include <unordered_map>
+#include "dwarf2/comp-unit.h"
 #include "dwarf2/index-cache.h"
 #include "dwarf2/section.h"
 #include "filename-seen-cache.h"
   /* Backlink to the owner of this.  */
   dwarf2_per_bfd *per_bfd;
 
+  /* DWARF header of this CU.  Note that dwarf2_cu reads its own version of the
+     header, which may differ from this one, since it may pass rcuh_kind::TYPE
+     to read_comp_unit_head, whereas for dwarf2_per_cu_data we always pass
+     rcuh_kind::COMPILE.
+
+     Don't access this field directly, use the get_header method instead.  It
+     should be private, but we can't make it private at the moment.  */
+  mutable comp_unit_head m_header;
+
+  /* True if HEADER has been read in.
+
+     Don't access this field directly.  It should be private, but we can't make
+     it private at the moment.  */
+  mutable bool m_header_read_in;
+
   /* When dwarf2_per_bfd::using_index is true, the 'quick' field
      is active.  Otherwise, the 'psymtab' field is active.  */
   union
     imported_symtabs = nullptr;
   }
 
+  /* Get the header of this per_cu, reading it if necessary.  */
+  const comp_unit_head *get_header () const;
+
   /* Return the address size given in the compilation unit header for
      this CU.  */
   int addr_size () const;