1 //===- InputFiles.cpp -----------------------------------------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "InputFiles.h"
11 #include "Error.h"
12 #include "InputSection.h"
13 #include "Symbols.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/CodeGen/Analysis.h"
16 #include "llvm/IR/LLVMContext.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/Object/IRObjectFile.h"
19 #include "llvm/Support/raw_ostream.h"
20 
21 using namespace llvm;
22 using namespace llvm::ELF;
23 using namespace llvm::object;
24 using namespace llvm::sys::fs;
25 
26 using namespace lld;
27 using namespace lld::elf;
28 
29 template <class ELFT>
30 static ELFFile<ELFT> createELFObj(MemoryBufferRef MB) {
31   std::error_code EC;
32   ELFFile<ELFT> F(MB.getBuffer(), EC);
33   check(EC);
34   return F;
35 }
36 
37 template <class ELFT>
38 ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB)
39     : InputFile(K, MB), ELFObj(createELFObj<ELFT>(MB)) {}
40 
41 template <class ELFT>
42 ELFKind ELFFileBase<ELFT>::getELFKind() {
43   if (ELFT::TargetEndianness == support::little)
44     return ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind;
45   return ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind;
46 }
47 
48 template <class ELFT>
49 typename ELFT::SymRange ELFFileBase<ELFT>::getElfSymbols(bool OnlyGlobals) {
50   if (!Symtab)
51     return Elf_Sym_Range(nullptr, nullptr);
52   Elf_Sym_Range Syms = ELFObj.symbols(Symtab);
53   uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
54   uint32_t FirstNonLocal = Symtab->sh_info;
55   if (FirstNonLocal > NumSymbols)
56     fatal("invalid sh_info in symbol table");
57 
58   if (OnlyGlobals)
59     return makeArrayRef(Syms.begin() + FirstNonLocal, Syms.end());
60   return makeArrayRef(Syms.begin(), Syms.end());
61 }
62 
63 template <class ELFT>
64 uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
65   uint32_t I = Sym.st_shndx;
66   if (I == ELF::SHN_XINDEX)
67     return ELFObj.getExtendedSymbolTableIndex(&Sym, Symtab, SymtabSHNDX);
68   if (I >= ELF::SHN_LORESERVE)
69     return 0;
70   return I;
71 }
72 
73 template <class ELFT> void ELFFileBase<ELFT>::initStringTable() {
74   if (!Symtab)
75     return;
76   StringTable = check(ELFObj.getStringTableForSymtab(*Symtab));
77 }
78 
79 template <class ELFT>
80 elf::ObjectFile<ELFT>::ObjectFile(MemoryBufferRef M)
81     : ELFFileBase<ELFT>(Base::ObjectKind, M) {}
82 
83 template <class ELFT>
84 ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getNonLocalSymbols() {
85   if (!this->Symtab)
86     return this->SymbolBodies;
87   uint32_t FirstNonLocal = this->Symtab->sh_info;
88   return makeArrayRef(this->SymbolBodies).slice(FirstNonLocal);
89 }
90 
91 template <class ELFT>
92 ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getLocalSymbols() {
93   if (!this->Symtab)
94     return this->SymbolBodies;
95   uint32_t FirstNonLocal = this->Symtab->sh_info;
96   return makeArrayRef(this->SymbolBodies).slice(1, FirstNonLocal - 1);
97 }
98 
99 template <class ELFT>
100 ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getSymbols() {
101   if (!this->Symtab)
102     return this->SymbolBodies;
103   return makeArrayRef(this->SymbolBodies).slice(1);
104 }
105 
106 template <class ELFT> uint32_t elf::ObjectFile<ELFT>::getMipsGp0() const {
107   if (MipsReginfo)
108     return MipsReginfo->Reginfo->ri_gp_value;
109   return 0;
110 }
111 
112 template <class ELFT>
113 void elf::ObjectFile<ELFT>::parse(DenseSet<StringRef> &ComdatGroups) {
114   // Read section and symbol tables.
115   initializeSections(ComdatGroups);
116   initializeSymbols();
117 }
118 
119 // Sections with SHT_GROUP and comdat bits define comdat section groups.
120 // They are identified and deduplicated by group name. This function
121 // returns a group name.
122 template <class ELFT>
123 StringRef elf::ObjectFile<ELFT>::getShtGroupSignature(const Elf_Shdr &Sec) {
124   const ELFFile<ELFT> &Obj = this->ELFObj;
125   uint32_t SymtabdSectionIndex = Sec.sh_link;
126   const Elf_Shdr *SymtabSec = check(Obj.getSection(SymtabdSectionIndex));
127   uint32_t SymIndex = Sec.sh_info;
128   const Elf_Sym *Sym = Obj.getSymbol(SymtabSec, SymIndex);
129   StringRef StringTable = check(Obj.getStringTableForSymtab(*SymtabSec));
130   return check(Sym->getName(StringTable));
131 }
132 
133 template <class ELFT>
134 ArrayRef<typename elf::ObjectFile<ELFT>::Elf_Word>
135 elf::ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
136   const ELFFile<ELFT> &Obj = this->ELFObj;
137   ArrayRef<Elf_Word> Entries =
138       check(Obj.template getSectionContentsAsArray<Elf_Word>(&Sec));
139   if (Entries.empty() || Entries[0] != GRP_COMDAT)
140     fatal("unsupported SHT_GROUP format");
141   return Entries.slice(1);
142 }
143 
144 template <class ELFT> static bool shouldMerge(const typename ELFT::Shdr &Sec) {
145   typedef typename ELFT::uint uintX_t;
146   uintX_t Flags = Sec.sh_flags;
147   if (!(Flags & SHF_MERGE))
148     return false;
149   if (Flags & SHF_WRITE)
150     fatal("writable SHF_MERGE sections are not supported");
151   uintX_t EntSize = Sec.sh_entsize;
152   if (!EntSize || Sec.sh_size % EntSize)
153     fatal("SHF_MERGE section size must be a multiple of sh_entsize");
154 
155   // Don't try to merge if the aligment is larger than the sh_entsize and this
156   // is not SHF_STRINGS.
157   //
158   // Since this is not a SHF_STRINGS, we would need to pad after every entity.
159   // It would be equivalent for the producer of the .o to just set a larger
160   // sh_entsize.
161   if (Flags & SHF_STRINGS)
162     return true;
163 
164   if (Sec.sh_addralign > EntSize)
165     return false;
166 
167   return true;
168 }
169 
170 template <class ELFT>
171 void elf::ObjectFile<ELFT>::initializeSections(
172     DenseSet<StringRef> &ComdatGroups) {
173   uint64_t Size = this->ELFObj.getNumSections();
174   Sections.resize(Size);
175   unsigned I = -1;
176   const ELFFile<ELFT> &Obj = this->ELFObj;
177   for (const Elf_Shdr &Sec : Obj.sections()) {
178     ++I;
179     if (Sections[I] == &InputSection<ELFT>::Discarded)
180       continue;
181 
182     switch (Sec.sh_type) {
183     case SHT_GROUP:
184       Sections[I] = &InputSection<ELFT>::Discarded;
185       if (ComdatGroups.insert(getShtGroupSignature(Sec)).second)
186         continue;
187       for (uint32_t SecIndex : getShtGroupEntries(Sec)) {
188         if (SecIndex >= Size)
189           fatal("invalid section index in group");
190         Sections[SecIndex] = &InputSection<ELFT>::Discarded;
191       }
192       break;
193     case SHT_SYMTAB:
194       this->Symtab = &Sec;
195       break;
196     case SHT_SYMTAB_SHNDX:
197       this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec));
198       break;
199     case SHT_STRTAB:
200     case SHT_NULL:
201       break;
202     case SHT_RELA:
203     case SHT_REL: {
204       // This section contains relocation information.
205       // If -r is given, we do not interpret or apply relocation
206       // but just copy relocation sections to output.
207       if (Config->Relocatable) {
208         Sections[I] = new (IAlloc.Allocate()) InputSection<ELFT>(this, &Sec);
209         break;
210       }
211 
212       // Find the relocation target section and associate this
213       // section with it.
214       InputSectionBase<ELFT> *Target = getRelocTarget(Sec);
215       if (!Target)
216         break;
217       if (auto *S = dyn_cast<InputSection<ELFT>>(Target)) {
218         S->RelocSections.push_back(&Sec);
219         break;
220       }
221       if (auto *S = dyn_cast<EHInputSection<ELFT>>(Target)) {
222         if (S->RelocSection)
223           fatal("multiple relocation sections to .eh_frame are not supported");
224         S->RelocSection = &Sec;
225         break;
226       }
227       fatal("relocations pointing to SHF_MERGE are not supported");
228     }
229     default:
230       Sections[I] = createInputSection(Sec);
231     }
232   }
233 }
234 
235 template <class ELFT>
236 InputSectionBase<ELFT> *
237 elf::ObjectFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
238   uint32_t Idx = Sec.sh_info;
239   if (Idx >= Sections.size())
240     fatal("invalid relocated section index");
241   InputSectionBase<ELFT> *Target = Sections[Idx];
242 
243   // Strictly speaking, a relocation section must be included in the
244   // group of the section it relocates. However, LLVM 3.3 and earlier
245   // would fail to do so, so we gracefully handle that case.
246   if (Target == &InputSection<ELFT>::Discarded)
247     return nullptr;
248 
249   if (!Target)
250     fatal("unsupported relocation reference");
251   return Target;
252 }
253 
254 template <class ELFT>
255 InputSectionBase<ELFT> *
256 elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
257   StringRef Name = check(this->ELFObj.getSectionName(&Sec));
258 
259   // .note.GNU-stack is a marker section to control the presence of
260   // PT_GNU_STACK segment in outputs. Since the presence of the segment
261   // is controlled only by the command line option (-z execstack) in LLD,
262   // .note.GNU-stack is ignored.
263   if (Name == ".note.GNU-stack")
264     return &InputSection<ELFT>::Discarded;
265 
266   if (Name == ".note.GNU-split-stack") {
267     error("objects using splitstacks are not supported");
268     return &InputSection<ELFT>::Discarded;
269   }
270 
271   if (Config->StripDebug && Name.startswith(".debug"))
272     return &InputSection<ELFT>::Discarded;
273 
274   // A MIPS object file has a special section that contains register
275   // usage info, which needs to be handled by the linker specially.
276   if (Config->EMachine == EM_MIPS && Name == ".reginfo") {
277     MipsReginfo.reset(new MipsReginfoInputSection<ELFT>(this, &Sec));
278     return MipsReginfo.get();
279   }
280 
281   // We dont need special handling of .eh_frame sections if relocatable
282   // output was choosen. Proccess them as usual input sections.
283   if (!Config->Relocatable && Name == ".eh_frame")
284     return new (EHAlloc.Allocate()) EHInputSection<ELFT>(this, &Sec);
285   if (shouldMerge<ELFT>(Sec))
286     return new (MAlloc.Allocate()) MergeInputSection<ELFT>(this, &Sec);
287   return new (IAlloc.Allocate()) InputSection<ELFT>(this, &Sec);
288 }
289 
290 template <class ELFT> void elf::ObjectFile<ELFT>::initializeSymbols() {
291   this->initStringTable();
292   Elf_Sym_Range Syms = this->getElfSymbols(false);
293   uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
294   SymbolBodies.reserve(NumSymbols);
295   for (const Elf_Sym &Sym : Syms)
296     SymbolBodies.push_back(createSymbolBody(&Sym));
297 }
298 
299 template <class ELFT>
300 InputSectionBase<ELFT> *
301 elf::ObjectFile<ELFT>::getSection(const Elf_Sym &Sym) const {
302   uint32_t Index = this->getSectionIndex(Sym);
303   if (Index == 0)
304     return nullptr;
305   if (Index >= Sections.size() || !Sections[Index])
306     fatal("invalid section index");
307   InputSectionBase<ELFT> *S = Sections[Index];
308   if (S == &InputSectionBase<ELFT>::Discarded)
309     return S;
310   return S->Repl;
311 }
312 
313 template <class ELFT>
314 SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
315   unsigned char Binding = Sym->getBinding();
316   InputSectionBase<ELFT> *Sec = getSection(*Sym);
317   if (Binding == STB_LOCAL) {
318     if (Sym->st_shndx == SHN_UNDEF)
319       return new (Alloc) UndefinedElf<ELFT>(*Sym);
320     return new (Alloc) DefinedRegular<ELFT>(*Sym, Sec);
321   }
322 
323   StringRef Name = check(Sym->getName(this->StringTable));
324 
325   switch (Sym->st_shndx) {
326   case SHN_UNDEF:
327     return new (Alloc) UndefinedElf<ELFT>(Name, *Sym);
328   case SHN_COMMON:
329     return new (Alloc) DefinedCommon(Name, Sym->st_size, Sym->st_value, Binding,
330                                      Sym->st_other, Sym->getType());
331   }
332 
333   switch (Binding) {
334   default:
335     fatal("unexpected binding");
336   case STB_GLOBAL:
337   case STB_WEAK:
338   case STB_GNU_UNIQUE:
339     if (Sec == &InputSection<ELFT>::Discarded)
340       return new (Alloc) UndefinedElf<ELFT>(Name, *Sym);
341     return new (Alloc) DefinedRegular<ELFT>(Name, *Sym, Sec);
342   }
343 }
344 
345 void ArchiveFile::parse() {
346   File = check(Archive::create(MB), "failed to parse archive");
347 
348   // Allocate a buffer for Lazy objects.
349   size_t NumSyms = File->getNumberOfSymbols();
350   LazySymbols.reserve(NumSyms);
351 
352   // Read the symbol table to construct Lazy objects.
353   for (const Archive::Symbol &Sym : File->symbols())
354     LazySymbols.emplace_back(this, Sym);
355 }
356 
357 // Returns a buffer pointing to a member file containing a given symbol.
358 MemoryBufferRef ArchiveFile::getMember(const Archive::Symbol *Sym) {
359   Archive::Child C =
360       check(Sym->getMember(),
361             "could not get the member for symbol " + Sym->getName());
362 
363   if (!Seen.insert(C.getChildOffset()).second)
364     return MemoryBufferRef();
365 
366   return check(C.getMemoryBufferRef(),
367                "could not get the buffer for the member defining symbol " +
368                    Sym->getName());
369 }
370 
371 template <class ELFT>
372 SharedFile<ELFT>::SharedFile(MemoryBufferRef M)
373     : ELFFileBase<ELFT>(Base::SharedKind, M), AsNeeded(Config->AsNeeded) {}
374 
375 template <class ELFT>
376 const typename ELFT::Shdr *
377 SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const {
378   uint32_t Index = this->getSectionIndex(Sym);
379   if (Index == 0)
380     return nullptr;
381   return check(this->ELFObj.getSection(Index));
382 }
383 
384 // Partially parse the shared object file so that we can call
385 // getSoName on this object.
386 template <class ELFT> void SharedFile<ELFT>::parseSoName() {
387   typedef typename ELFT::Dyn Elf_Dyn;
388   typedef typename ELFT::uint uintX_t;
389   const Elf_Shdr *DynamicSec = nullptr;
390 
391   const ELFFile<ELFT> Obj = this->ELFObj;
392   for (const Elf_Shdr &Sec : Obj.sections()) {
393     switch (Sec.sh_type) {
394     default:
395       continue;
396     case SHT_DYNSYM:
397       this->Symtab = &Sec;
398       break;
399     case SHT_DYNAMIC:
400       DynamicSec = &Sec;
401       break;
402     case SHT_SYMTAB_SHNDX:
403       this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec));
404       break;
405     }
406   }
407 
408   this->initStringTable();
409   SoName = this->getName();
410 
411   if (!DynamicSec)
412     return;
413   auto *Begin =
414       reinterpret_cast<const Elf_Dyn *>(Obj.base() + DynamicSec->sh_offset);
415   const Elf_Dyn *End = Begin + DynamicSec->sh_size / sizeof(Elf_Dyn);
416 
417   for (const Elf_Dyn &Dyn : make_range(Begin, End)) {
418     if (Dyn.d_tag == DT_SONAME) {
419       uintX_t Val = Dyn.getVal();
420       if (Val >= this->StringTable.size())
421         fatal("invalid DT_SONAME entry");
422       SoName = StringRef(this->StringTable.data() + Val);
423       return;
424     }
425   }
426 }
427 
428 // Fully parse the shared object file. This must be called after parseSoName().
429 template <class ELFT> void SharedFile<ELFT>::parseRest() {
430   Elf_Sym_Range Syms = this->getElfSymbols(true);
431   uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
432   SymbolBodies.reserve(NumSymbols);
433   for (const Elf_Sym &Sym : Syms) {
434     StringRef Name = check(Sym.getName(this->StringTable));
435     if (Sym.isUndefined())
436       Undefs.push_back(Name);
437     else
438       SymbolBodies.emplace_back(this, Name, Sym);
439   }
440 }
441 
442 BitcodeFile::BitcodeFile(MemoryBufferRef M) : InputFile(BitcodeKind, M) {}
443 
444 bool BitcodeFile::classof(const InputFile *F) {
445   return F->kind() == BitcodeKind;
446 }
447 
448 static uint8_t getGvVisibility(const GlobalValue *GV) {
449   switch (GV->getVisibility()) {
450   case GlobalValue::DefaultVisibility:
451     return STV_DEFAULT;
452   case GlobalValue::HiddenVisibility:
453     return STV_HIDDEN;
454   case GlobalValue::ProtectedVisibility:
455     return STV_PROTECTED;
456   }
457   llvm_unreachable("unknown visibility");
458 }
459 
460 SymbolBody *
461 BitcodeFile::createSymbolBody(const DenseSet<const Comdat *> &KeptComdats,
462                               const IRObjectFile &Obj,
463                               const BasicSymbolRef &Sym) {
464   const GlobalValue *GV = Obj.getSymbolGV(Sym.getRawDataRefImpl());
465   if (GV)
466     if (const Comdat *C = GV->getComdat())
467       if (!KeptComdats.count(C))
468         return nullptr;
469 
470   uint32_t Flags = Sym.getFlags();
471   uint8_t Visibility;
472   if (GV)
473     Visibility = getGvVisibility(GV);
474   else
475     // FIXME: Set SF_Hidden flag correctly for module asm symbols, and expose
476     // protected visibility.
477     Visibility = STV_DEFAULT;
478 
479   SmallString<64> Name;
480   raw_svector_ostream OS(Name);
481   Sym.printName(OS);
482   StringRef NameRef = Saver.save(StringRef(Name));
483 
484   const Module &M = Obj.getModule();
485   SymbolBody *Body;
486   bool IsWeak = Flags & BasicSymbolRef::SF_Weak;
487   if (Flags & BasicSymbolRef::SF_Undefined) {
488     Body = new (Alloc) UndefinedBitcode(NameRef, IsWeak, Visibility);
489   } else if (Flags & BasicSymbolRef::SF_Common) {
490     // FIXME: Set SF_Common flag correctly for module asm symbols, and expose
491     // size and alignment.
492     assert(GV);
493     const DataLayout &DL = M.getDataLayout();
494     uint64_t Size = DL.getTypeAllocSize(GV->getValueType());
495     Body = new (Alloc)
496         DefinedCommon(NameRef, Size, GV->getAlignment(),
497                       IsWeak ? STB_WEAK : STB_GLOBAL, Visibility, /*Type*/ 0);
498   } else {
499     Body = new (Alloc) DefinedBitcode(NameRef, IsWeak, Visibility);
500   }
501   // FIXME: Expose a thread-local flag for module asm symbols.
502   if (GV) {
503     if (GV->isThreadLocal())
504       Body->Type = STT_TLS;
505     Body->CanOmitFromDynSym = canBeOmittedFromSymbolTable(GV);
506   }
507   return Body;
508 }
509 
510 bool BitcodeFile::shouldSkip(const BasicSymbolRef &Sym) {
511   uint32_t Flags = Sym.getFlags();
512   if (!(Flags & BasicSymbolRef::SF_Global))
513     return true;
514   if (Flags & BasicSymbolRef::SF_FormatSpecific)
515     return true;
516   return false;
517 }
518 
519 void BitcodeFile::parse(DenseSet<StringRef> &ComdatGroups) {
520   LLVMContext Context;
521   std::unique_ptr<IRObjectFile> Obj = check(IRObjectFile::create(MB, Context));
522   const Module &M = Obj->getModule();
523 
524   DenseSet<const Comdat *> KeptComdats;
525   for (const auto &P : M.getComdatSymbolTable()) {
526     StringRef N = Saver.save(P.first());
527     if (ComdatGroups.insert(N).second)
528       KeptComdats.insert(&P.second);
529   }
530 
531   for (const BasicSymbolRef &Sym : Obj->symbols())
532     if (!shouldSkip(Sym))
533       SymbolBodies.push_back(createSymbolBody(KeptComdats, *Obj, Sym));
534 }
535 
536 template <typename T>
537 static std::unique_ptr<InputFile> createELFFileAux(MemoryBufferRef MB) {
538   std::unique_ptr<T> Ret = llvm::make_unique<T>(MB);
539 
540   if (!Config->FirstElf)
541     Config->FirstElf = Ret.get();
542 
543   if (Config->EKind == ELFNoneKind) {
544     Config->EKind = Ret->getELFKind();
545     Config->EMachine = Ret->getEMachine();
546   }
547 
548   return std::move(Ret);
549 }
550 
551 template <template <class> class T>
552 static std::unique_ptr<InputFile> createELFFile(MemoryBufferRef MB) {
553   unsigned char Size;
554   unsigned char Endian;
555   std::tie(Size, Endian) = getElfArchType(MB.getBuffer());
556   if (Endian != ELFDATA2LSB && Endian != ELFDATA2MSB)
557     fatal("invalid data encoding: " + MB.getBufferIdentifier());
558 
559   if (Size == ELFCLASS32) {
560     if (Endian == ELFDATA2LSB)
561       return createELFFileAux<T<ELF32LE>>(MB);
562     return createELFFileAux<T<ELF32BE>>(MB);
563   }
564   if (Size == ELFCLASS64) {
565     if (Endian == ELFDATA2LSB)
566       return createELFFileAux<T<ELF64LE>>(MB);
567     return createELFFileAux<T<ELF64BE>>(MB);
568   }
569   fatal("invalid file class: " + MB.getBufferIdentifier());
570 }
571 
572 static bool isBitcode(MemoryBufferRef MB) {
573   using namespace sys::fs;
574   return identify_magic(MB.getBuffer()) == file_magic::bitcode;
575 }
576 
577 std::unique_ptr<InputFile> elf::createObjectFile(MemoryBufferRef MB,
578                                                  StringRef ArchiveName) {
579   std::unique_ptr<InputFile> F;
580   if (isBitcode(MB))
581     F.reset(new BitcodeFile(MB));
582   else
583     F = createELFFile<ObjectFile>(MB);
584   F->ArchiveName = ArchiveName;
585   return F;
586 }
587 
588 std::unique_ptr<InputFile> elf::createSharedFile(MemoryBufferRef MB) {
589   return createELFFile<SharedFile>(MB);
590 }
591 
592 void LazyObjectFile::parse() {
593   for (StringRef Sym : getSymbols())
594     LazySymbols.emplace_back(Sym, this->MB);
595 }
596 
597 template <class ELFT> std::vector<StringRef> LazyObjectFile::getElfSymbols() {
598   typedef typename ELFT::Shdr Elf_Shdr;
599   typedef typename ELFT::Sym Elf_Sym;
600   typedef typename ELFT::SymRange Elf_Sym_Range;
601 
602   const ELFFile<ELFT> Obj = createELFObj<ELFT>(this->MB);
603   for (const Elf_Shdr &Sec : Obj.sections()) {
604     if (Sec.sh_type != SHT_SYMTAB)
605       continue;
606     Elf_Sym_Range Syms = Obj.symbols(&Sec);
607     uint32_t FirstNonLocal = Sec.sh_info;
608     StringRef StringTable = check(Obj.getStringTableForSymtab(Sec));
609     std::vector<StringRef> V;
610     for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal))
611       if (Sym.st_shndx != SHN_UNDEF)
612         V.push_back(check(Sym.getName(StringTable)));
613     return V;
614   }
615   return {};
616 }
617 
618 std::vector<StringRef> LazyObjectFile::getBitcodeSymbols() {
619   LLVMContext Context;
620   std::unique_ptr<IRObjectFile> Obj =
621       check(IRObjectFile::create(this->MB, Context));
622   std::vector<StringRef> V;
623   for (const BasicSymbolRef &Sym : Obj->symbols()) {
624     if (BitcodeFile::shouldSkip(Sym))
625       continue;
626     if (Sym.getFlags() & BasicSymbolRef::SF_Undefined)
627       continue;
628     SmallString<64> Name;
629     raw_svector_ostream OS(Name);
630     Sym.printName(OS);
631     V.push_back(Saver.save(StringRef(Name)));
632   }
633   return V;
634 }
635 
636 // Returns a vector of globally-visible defined symbol names.
637 std::vector<StringRef> LazyObjectFile::getSymbols() {
638   if (isBitcode(this->MB))
639     return getBitcodeSymbols();
640 
641   unsigned char Size;
642   unsigned char Endian;
643   std::tie(Size, Endian) = getElfArchType(this->MB.getBuffer());
644   if (Size == ELFCLASS32) {
645     if (Endian == ELFDATA2LSB)
646       return getElfSymbols<ELF32LE>();
647     return getElfSymbols<ELF32BE>();
648   }
649   if (Endian == ELFDATA2LSB)
650     return getElfSymbols<ELF64LE>();
651   return getElfSymbols<ELF64BE>();
652 }
653 
654 template class elf::ELFFileBase<ELF32LE>;
655 template class elf::ELFFileBase<ELF32BE>;
656 template class elf::ELFFileBase<ELF64LE>;
657 template class elf::ELFFileBase<ELF64BE>;
658 
659 template class elf::ObjectFile<ELF32LE>;
660 template class elf::ObjectFile<ELF32BE>;
661 template class elf::ObjectFile<ELF64LE>;
662 template class elf::ObjectFile<ELF64BE>;
663 
664 template class elf::SharedFile<ELF32LE>;
665 template class elf::SharedFile<ELF32BE>;
666 template class elf::SharedFile<ELF64LE>;
667 template class elf::SharedFile<ELF64BE>;
668