Index: ELF/DWARF.h
===================================================================
--- ELF/DWARF.h
+++ ELF/DWARF.h
@@ -41,19 +41,19 @@
     return LineSection;
   }
 
+  const llvm::DWARFSection &getGnuPubNamesSection() const override {
+    return GnuPubNamesSection;
+  }
+
+  const llvm::DWARFSection &getGnuPubTypesSection() const override {
+    return GnuPubTypesSection;
+  }
+
   StringRef getFileName() const override { return ""; }
   StringRef getAbbrevSection() const override { return AbbrevSection; }
   StringRef getStringSection() const override { return StrSection; }
   StringRef getLineStringSection() const override { return LineStringSection; }
 
-  StringRef getGnuPubNamesSection() const override {
-    return GnuPubNamesSection;
-  }
-
-  StringRef getGnuPubTypesSection() const override {
-    return GnuPubTypesSection;
-  }
-
   bool isLittleEndian() const override {
     return ELFT::TargetEndianness == llvm::support::little;
   }
@@ -67,13 +67,13 @@
                                                uint64_t Pos,
                                                ArrayRef<RelTy> Rels) const;
 
+  LLDDWARFSection GnuPubNamesSection;
+  LLDDWARFSection GnuPubTypesSection;
   LLDDWARFSection InfoSection;
   LLDDWARFSection RangeSection;
   LLDDWARFSection LineSection;
 
   StringRef AbbrevSection;
-  StringRef GnuPubNamesSection;
-  StringRef GnuPubTypesSection;
   StringRef StrSection;
   StringRef LineStringSection;
 };
Index: ELF/DWARF.cpp
===================================================================
--- ELF/DWARF.cpp
+++ ELF/DWARF.cpp
@@ -31,11 +31,14 @@
     if (!Sec)
       continue;
 
-    if (LLDDWARFSection *M = StringSwitch<LLDDWARFSection *>(Sec->Name)
-                                 .Case(".debug_info", &InfoSection)
-                                 .Case(".debug_ranges", &RangeSection)
-                                 .Case(".debug_line", &LineSection)
-                                 .Default(nullptr)) {
+    if (LLDDWARFSection *M =
+            StringSwitch<LLDDWARFSection *>(Sec->Name)
+                .Case(".debug_gnu_pubnames", &GnuPubNamesSection)
+                .Case(".debug_gnu_pubtypes", &GnuPubTypesSection)
+                .Case(".debug_info", &InfoSection)
+                .Case(".debug_ranges", &RangeSection)
+                .Case(".debug_line", &LineSection)
+                .Default(nullptr)) {
       M->Data = toStringRef(Sec->data());
       M->Sec = Sec;
       continue;
@@ -43,10 +46,6 @@
 
     if (Sec->Name == ".debug_abbrev")
       AbbrevSection = toStringRef(Sec->data());
-    else if (Sec->Name == ".debug_gnu_pubnames")
-      GnuPubNamesSection = toStringRef(Sec->data());
-    else if (Sec->Name == ".debug_gnu_pubtypes")
-      GnuPubTypesSection = toStringRef(Sec->data());
     else if (Sec->Name == ".debug_str")
       StrSection = toStringRef(Sec->data());
     else if (Sec->Name == ".debug_line_str")
Index: ELF/SyntheticSections.cpp
===================================================================
--- ELF/SyntheticSections.cpp
+++ ELF/SyntheticSections.cpp
@@ -2412,14 +2412,16 @@
   return Ret;
 }
 
+template <class ELFT>
 static std::vector<GdbIndexSection::NameTypeEntry>
 readPubNamesAndTypes(DWARFContext &Dwarf, uint32_t Idx) {
-  StringRef Sec1 = Dwarf.getDWARFObj().getGnuPubNamesSection();
-  StringRef Sec2 = Dwarf.getDWARFObj().getGnuPubTypesSection();
+  auto &Obj = static_cast<const LLDDwarfObj<ELFT> &>(Dwarf.getDWARFObj());
+  const DWARFSection &PubNames = Obj.getGnuPubNamesSection();
+  const DWARFSection &PubTypes = Obj.getGnuPubTypesSection();
 
   std::vector<GdbIndexSection::NameTypeEntry> Ret;
-  for (StringRef Sec : {Sec1, Sec2}) {
-    DWARFDebugPubTable Table(Sec, Config->IsLE, true);
+  for (const DWARFSection *Pub : {&PubNames, &PubTypes}) {
+    DWARFDebugPubTable Table(Obj, *Pub, Config->IsLE, true);
     for (const DWARFDebugPubTable::Set &Set : Table.getData())
       for (const DWARFDebugPubTable::Entry &Ent : Set.Entries)
         Ret.push_back({{Ent.Name, computeGdbHash(Ent.Name)},
@@ -2517,7 +2519,7 @@
     Chunks[I].Sec = Sections[I];
     Chunks[I].CompilationUnits = readCuList(Dwarf);
     Chunks[I].AddressAreas = readAddressAreas(Dwarf, Sections[I]);
-    NameTypes[I] = readPubNamesAndTypes(Dwarf, I);
+    NameTypes[I] = readPubNamesAndTypes<ELFT>(Dwarf, I);
   });
 
   auto *Ret = make<GdbIndexSection>();