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 "Driver.h"
12 #include "Error.h"
13 #include "InputSection.h"
14 #include "LinkerScript.h"
15 #include "SymbolTable.h"
16 #include "Symbols.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/Bitcode/ReaderWriter.h"
19 #include "llvm/CodeGen/Analysis.h"
20 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
21 #include "llvm/IR/LLVMContext.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/LTO/LTO.h"
24 #include "llvm/MC/StringTableBuilder.h"
25 #include "llvm/Support/Path.h"
26 #include "llvm/Support/raw_ostream.h"
27 
28 using namespace llvm;
29 using namespace llvm::ELF;
30 using namespace llvm::object;
31 using namespace llvm::sys::fs;
32 
33 using namespace lld;
34 using namespace lld::elf;
35 
36 std::vector<InputFile *> InputFile::Pool;
37 
38 template <class ELFT> DIHelper<ELFT>::DIHelper(elf::InputFile *F) {
39   Expected<std::unique_ptr<object::ObjectFile>> Obj =
40       object::ObjectFile::createObjectFile(F->MB);
41   if (!Obj)
42     return;
43 
44   DWARFContextInMemory Dwarf(*Obj.get());
45   DwarfLine.reset(new DWARFDebugLine(&Dwarf.getLineSection().Relocs));
46   DataExtractor LineData(Dwarf.getLineSection().Data,
47                          ELFT::TargetEndianness == support::little,
48                          ELFT::Is64Bits ? 8 : 4);
49   // The second parameter is offset in .debug_line section
50   // for compilation unit (CU) of interest. We have only one
51   // CU (object file), so offset is always 0.
52   DwarfLine->getOrParseLineTable(LineData, 0);
53 }
54 
55 template <class ELFT> DIHelper<ELFT>::~DIHelper() {}
56 
57 template <class ELFT> std::string DIHelper<ELFT>::getLineInfo(uintX_t Offset) {
58   if (!DwarfLine)
59     return "";
60 
61   DILineInfo LineInfo;
62   DILineInfoSpecifier Spec;
63   // The offset to CU is 0 (see DIHelper constructor).
64   const DWARFDebugLine::LineTable *LineTbl = DwarfLine->getLineTable(0);
65   if (!LineTbl)
66     return "";
67   LineTbl->getFileLineInfoForAddress(Offset, nullptr, Spec.FLIKind, LineInfo);
68   return LineInfo.Line != 0
69              ? LineInfo.FileName + " (" + std::to_string(LineInfo.Line) + ")"
70              : "";
71 }
72 
73 // Deletes all InputFile instances created so far.
74 void InputFile::freePool() {
75   // Files are freed in reverse order so that files created
76   // from other files (e.g. object files extracted from archives)
77   // are freed in the proper order.
78   for (int I = Pool.size() - 1; I >= 0; --I)
79     delete Pool[I];
80 }
81 
82 // Returns "(internal)", "foo.a(bar.o)" or "baz.o".
83 std::string elf::getFilename(const InputFile *F) {
84   if (!F)
85     return "(internal)";
86   if (!F->ArchiveName.empty())
87     return (F->ArchiveName + "(" + F->getName() + ")").str();
88   return F->getName();
89 }
90 
91 template <class ELFT> static ELFFile<ELFT> createELFObj(MemoryBufferRef MB) {
92   std::error_code EC;
93   ELFFile<ELFT> F(MB.getBuffer(), EC);
94   if (EC)
95     fatal(EC, "failed to read " + MB.getBufferIdentifier());
96   return F;
97 }
98 
99 template <class ELFT> static ELFKind getELFKind() {
100   if (ELFT::TargetEndianness == support::little)
101     return ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind;
102   return ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind;
103 }
104 
105 template <class ELFT>
106 ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB)
107     : InputFile(K, MB), ELFObj(createELFObj<ELFT>(MB)) {
108   EKind = getELFKind<ELFT>();
109   EMachine = ELFObj.getHeader()->e_machine;
110   OSABI = ELFObj.getHeader()->e_ident[llvm::ELF::EI_OSABI];
111 }
112 
113 template <class ELFT>
114 typename ELFT::SymRange ELFFileBase<ELFT>::getElfSymbols(bool OnlyGlobals) {
115   if (!Symtab)
116     return Elf_Sym_Range(nullptr, nullptr);
117   Elf_Sym_Range Syms = ELFObj.symbols(Symtab);
118   uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
119   uint32_t FirstNonLocal = Symtab->sh_info;
120   if (FirstNonLocal == 0 || FirstNonLocal > NumSymbols)
121     fatal(getFilename(this) + ": invalid sh_info in symbol table");
122 
123   if (OnlyGlobals)
124     return makeArrayRef(Syms.begin() + FirstNonLocal, Syms.end());
125   return makeArrayRef(Syms.begin(), Syms.end());
126 }
127 
128 template <class ELFT>
129 uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
130   uint32_t I = Sym.st_shndx;
131   if (I == ELF::SHN_XINDEX)
132     return ELFObj.getExtendedSymbolTableIndex(&Sym, Symtab, SymtabSHNDX);
133   if (I >= ELF::SHN_LORESERVE)
134     return 0;
135   return I;
136 }
137 
138 template <class ELFT> void ELFFileBase<ELFT>::initStringTable() {
139   if (!Symtab)
140     return;
141   StringTable = check(ELFObj.getStringTableForSymtab(*Symtab));
142 }
143 
144 template <class ELFT>
145 elf::ObjectFile<ELFT>::ObjectFile(BumpPtrAllocator &Alloc, MemoryBufferRef M)
146     : ELFFileBase<ELFT>(Base::ObjectKind, M), Alloc(Alloc) {}
147 
148 template <class ELFT>
149 ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getNonLocalSymbols() {
150   if (!this->Symtab)
151     return this->SymbolBodies;
152   uint32_t FirstNonLocal = this->Symtab->sh_info;
153   return makeArrayRef(this->SymbolBodies).slice(FirstNonLocal);
154 }
155 
156 template <class ELFT>
157 ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getLocalSymbols() {
158   if (!this->Symtab)
159     return this->SymbolBodies;
160   uint32_t FirstNonLocal = this->Symtab->sh_info;
161   return makeArrayRef(this->SymbolBodies).slice(1, FirstNonLocal - 1);
162 }
163 
164 template <class ELFT>
165 ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getSymbols() {
166   if (!this->Symtab)
167     return this->SymbolBodies;
168   return makeArrayRef(this->SymbolBodies).slice(1);
169 }
170 
171 template <class ELFT> DIHelper<ELFT> *elf::ObjectFile<ELFT>::getDIHelper() {
172   if (!DIH)
173     DIH.reset(new DIHelper<ELFT>(this));
174 
175   return DIH.get();
176 }
177 
178 template <class ELFT> uint32_t elf::ObjectFile<ELFT>::getMipsGp0() const {
179   if (ELFT::Is64Bits && MipsOptions && MipsOptions->Reginfo)
180     return MipsOptions->Reginfo->ri_gp_value;
181   if (!ELFT::Is64Bits && MipsReginfo && MipsReginfo->Reginfo)
182     return MipsReginfo->Reginfo->ri_gp_value;
183   return 0;
184 }
185 
186 template <class ELFT>
187 void elf::ObjectFile<ELFT>::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
188   // Read section and symbol tables.
189   initializeSections(ComdatGroups);
190   initializeSymbols();
191   if (Config->GcSections && Config->EMachine == EM_ARM)
192     initializeReverseDependencies();
193 }
194 
195 // Sections with SHT_GROUP and comdat bits define comdat section groups.
196 // They are identified and deduplicated by group name. This function
197 // returns a group name.
198 template <class ELFT>
199 StringRef elf::ObjectFile<ELFT>::getShtGroupSignature(const Elf_Shdr &Sec) {
200   const ELFFile<ELFT> &Obj = this->ELFObj;
201   const Elf_Shdr *Symtab = check(Obj.getSection(Sec.sh_link));
202   const Elf_Sym *Sym = Obj.getSymbol(Symtab, Sec.sh_info);
203   StringRef Strtab = check(Obj.getStringTableForSymtab(*Symtab));
204   return check(Sym->getName(Strtab));
205 }
206 
207 template <class ELFT>
208 ArrayRef<typename elf::ObjectFile<ELFT>::Elf_Word>
209 elf::ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
210   const ELFFile<ELFT> &Obj = this->ELFObj;
211   ArrayRef<Elf_Word> Entries =
212       check(Obj.template getSectionContentsAsArray<Elf_Word>(&Sec));
213   if (Entries.empty() || Entries[0] != GRP_COMDAT)
214     fatal(getFilename(this) + ": unsupported SHT_GROUP format");
215   return Entries.slice(1);
216 }
217 
218 template <class ELFT>
219 bool elf::ObjectFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) {
220   // We don't merge sections if -O0 (default is -O1). This makes sometimes
221   // the linker significantly faster, although the output will be bigger.
222   if (Config->Optimize == 0)
223     return false;
224 
225   // Do not merge sections if generating a relocatable object. It makes
226   // the code simpler because we do not need to update relocation addends
227   // to reflect changes introduced by merging. Instead of that we write
228   // such "merge" sections into separate OutputSections and keep SHF_MERGE
229   // / SHF_STRINGS flags and sh_entsize value to be able to perform merging
230   // later during a final linking.
231   if (Config->Relocatable)
232     return false;
233 
234   // A mergeable section with size 0 is useless because they don't have
235   // any data to merge. A mergeable string section with size 0 can be
236   // argued as invalid because it doesn't end with a null character.
237   // We'll avoid a mess by handling them as if they were non-mergeable.
238   if (Sec.sh_size == 0)
239     return false;
240 
241   // Check for sh_entsize. The ELF spec is not clear about the zero
242   // sh_entsize. It says that "the member [sh_entsize] contains 0 if
243   // the section does not hold a table of fixed-size entries". We know
244   // that Rust 1.13 produces a string mergeable section with a zero
245   // sh_entsize. Here we just accept it rather than being picky about it.
246   uintX_t EntSize = Sec.sh_entsize;
247   if (EntSize == 0)
248     return false;
249   if (Sec.sh_size % EntSize)
250     fatal(getFilename(this) +
251           ": SHF_MERGE section size must be a multiple of sh_entsize");
252 
253   uintX_t Flags = Sec.sh_flags;
254   if (!(Flags & SHF_MERGE))
255     return false;
256   if (Flags & SHF_WRITE)
257     fatal(getFilename(this) + ": writable SHF_MERGE section is not supported");
258 
259   // Don't try to merge if the alignment is larger than the sh_entsize and this
260   // is not SHF_STRINGS.
261   //
262   // Since this is not a SHF_STRINGS, we would need to pad after every entity.
263   // It would be equivalent for the producer of the .o to just set a larger
264   // sh_entsize.
265   if (Flags & SHF_STRINGS)
266     return true;
267 
268   return Sec.sh_addralign <= EntSize;
269 }
270 
271 template <class ELFT>
272 void elf::ObjectFile<ELFT>::initializeSections(
273     DenseSet<CachedHashStringRef> &ComdatGroups) {
274   uint64_t Size = this->ELFObj.getNumSections();
275   Sections.resize(Size);
276   unsigned I = -1;
277   const ELFFile<ELFT> &Obj = this->ELFObj;
278   for (const Elf_Shdr &Sec : Obj.sections()) {
279     ++I;
280     if (Sections[I] == &InputSection<ELFT>::Discarded)
281       continue;
282 
283     // SHF_EXCLUDE'ed sections are discarded by the linker. However,
284     // if -r is given, we'll let the final link discard such sections.
285     // This is compatible with GNU.
286     if ((Sec.sh_flags & SHF_EXCLUDE) && !Config->Relocatable) {
287       Sections[I] = &InputSection<ELFT>::Discarded;
288       continue;
289     }
290 
291     switch (Sec.sh_type) {
292     case SHT_GROUP:
293       Sections[I] = &InputSection<ELFT>::Discarded;
294       if (ComdatGroups.insert(CachedHashStringRef(getShtGroupSignature(Sec)))
295               .second)
296         continue;
297       for (uint32_t SecIndex : getShtGroupEntries(Sec)) {
298         if (SecIndex >= Size)
299           fatal(getFilename(this) + ": invalid section index in group: " +
300                 Twine(SecIndex));
301         Sections[SecIndex] = &InputSection<ELFT>::Discarded;
302       }
303       break;
304     case SHT_SYMTAB:
305       this->Symtab = &Sec;
306       break;
307     case SHT_SYMTAB_SHNDX:
308       this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec));
309       break;
310     case SHT_STRTAB:
311     case SHT_NULL:
312       break;
313     default:
314       Sections[I] = createInputSection(Sec);
315     }
316   }
317 }
318 
319 // .ARM.exidx sections have a reverse dependency on the InputSection they
320 // have a SHF_LINK_ORDER dependency, this is identified by the sh_link.
321 template <class ELFT>
322 void elf::ObjectFile<ELFT>::initializeReverseDependencies() {
323   unsigned I = -1;
324   for (const Elf_Shdr &Sec : this->ELFObj.sections()) {
325     ++I;
326     if ((Sections[I] == &InputSection<ELFT>::Discarded) ||
327         !(Sec.sh_flags & SHF_LINK_ORDER))
328       continue;
329     if (Sec.sh_link >= Sections.size())
330       fatal(getFilename(this) + ": invalid sh_link index: " +
331             Twine(Sec.sh_link));
332     auto *IS = cast<InputSection<ELFT>>(Sections[Sec.sh_link]);
333     IS->DependentSection = Sections[I];
334   }
335 }
336 
337 template <class ELFT>
338 InputSectionBase<ELFT> *
339 elf::ObjectFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
340   uint32_t Idx = Sec.sh_info;
341   if (Idx >= Sections.size())
342     fatal(getFilename(this) + ": invalid relocated section index: " +
343           Twine(Idx));
344   InputSectionBase<ELFT> *Target = Sections[Idx];
345 
346   // Strictly speaking, a relocation section must be included in the
347   // group of the section it relocates. However, LLVM 3.3 and earlier
348   // would fail to do so, so we gracefully handle that case.
349   if (Target == &InputSection<ELFT>::Discarded)
350     return nullptr;
351 
352   if (!Target)
353     fatal(getFilename(this) + ": unsupported relocation reference");
354   return Target;
355 }
356 
357 template <class ELFT>
358 InputSectionBase<ELFT> *
359 elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
360   StringRef Name = check(this->ELFObj.getSectionName(&Sec));
361 
362   switch (Sec.sh_type) {
363   case SHT_ARM_ATTRIBUTES:
364     // FIXME: ARM meta-data section. At present attributes are ignored,
365     // they can be used to reason about object compatibility.
366     return &InputSection<ELFT>::Discarded;
367   case SHT_MIPS_REGINFO:
368     if (MipsReginfo)
369       fatal(getFilename(this) +
370             ": multiple SHT_MIPS_REGINFO sections are not allowed");
371     MipsReginfo.reset(new MipsReginfoInputSection<ELFT>(this, &Sec, Name));
372     return MipsReginfo.get();
373   case SHT_MIPS_OPTIONS:
374     if (MipsOptions)
375       fatal(getFilename(this) +
376             ": multiple SHT_MIPS_OPTIONS sections are not allowed");
377     MipsOptions.reset(new MipsOptionsInputSection<ELFT>(this, &Sec, Name));
378     return MipsOptions.get();
379   case SHT_MIPS_ABIFLAGS:
380     if (MipsAbiFlags)
381       fatal(getFilename(this) +
382             ": multiple SHT_MIPS_ABIFLAGS sections are not allowed");
383     MipsAbiFlags.reset(new MipsAbiFlagsInputSection<ELFT>(this, &Sec, Name));
384     return MipsAbiFlags.get();
385   case SHT_RELA:
386   case SHT_REL: {
387     // This section contains relocation information.
388     // If -r is given, we do not interpret or apply relocation
389     // but just copy relocation sections to output.
390     if (Config->Relocatable)
391       return new (GAlloc<ELFT>::IAlloc.Allocate())
392           InputSection<ELFT>(this, &Sec, Name);
393 
394     // Find the relocation target section and associate this
395     // section with it.
396     InputSectionBase<ELFT> *Target = getRelocTarget(Sec);
397     if (!Target)
398       return nullptr;
399     if (auto *S = dyn_cast<InputSection<ELFT>>(Target)) {
400       S->RelocSections.push_back(&Sec);
401       return nullptr;
402     }
403     if (auto *S = dyn_cast<EhInputSection<ELFT>>(Target)) {
404       if (S->RelocSection)
405         fatal(getFilename(this) +
406               ": multiple relocation sections to .eh_frame are not supported");
407       S->RelocSection = &Sec;
408       return nullptr;
409     }
410     fatal(getFilename(this) +
411           ": relocations pointing to SHF_MERGE are not supported");
412   }
413   }
414 
415   // .note.GNU-stack is a marker section to control the presence of
416   // PT_GNU_STACK segment in outputs. Since the presence of the segment
417   // is controlled only by the command line option (-z execstack) in LLD,
418   // .note.GNU-stack is ignored.
419   if (Name == ".note.GNU-stack")
420     return &InputSection<ELFT>::Discarded;
421 
422   if (Name == ".note.GNU-split-stack") {
423     error("objects using splitstacks are not supported");
424     return &InputSection<ELFT>::Discarded;
425   }
426 
427   if (Config->Strip != StripPolicy::None && Name.startswith(".debug"))
428     return &InputSection<ELFT>::Discarded;
429 
430   // The linker merges EH (exception handling) frames and creates a
431   // .eh_frame_hdr section for runtime. So we handle them with a special
432   // class. For relocatable outputs, they are just passed through.
433   if (Name == ".eh_frame" && !Config->Relocatable)
434     return new (GAlloc<ELFT>::EHAlloc.Allocate())
435         EhInputSection<ELFT>(this, &Sec, Name);
436 
437   if (shouldMerge(Sec))
438     return new (GAlloc<ELFT>::MAlloc.Allocate())
439         MergeInputSection<ELFT>(this, &Sec, Name);
440   return new (GAlloc<ELFT>::IAlloc.Allocate())
441       InputSection<ELFT>(this, &Sec, Name);
442 }
443 
444 template <class ELFT> void elf::ObjectFile<ELFT>::initializeSymbols() {
445   this->initStringTable();
446   Elf_Sym_Range Syms = this->getElfSymbols(false);
447   uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
448   SymbolBodies.reserve(NumSymbols);
449   for (const Elf_Sym &Sym : Syms)
450     SymbolBodies.push_back(createSymbolBody(&Sym));
451 }
452 
453 template <class ELFT>
454 InputSectionBase<ELFT> *
455 elf::ObjectFile<ELFT>::getSection(const Elf_Sym &Sym) const {
456   uint32_t Index = this->getSectionIndex(Sym);
457   if (Index >= Sections.size())
458     fatal(getFilename(this) + ": invalid section index: " + Twine(Index));
459   InputSectionBase<ELFT> *S = Sections[Index];
460 
461   // We found that GNU assembler 2.17.50 [FreeBSD] 2007-07-03
462   // could generate broken objects. STT_SECTION symbols can be
463   // associated with SHT_REL[A]/SHT_SYMTAB/SHT_STRTAB sections.
464   // In this case it is fine for section to be null here as we
465   // do not allocate sections of these types.
466   if (!S) {
467     if (Index == 0 || Sym.getType() == STT_SECTION)
468       return nullptr;
469     fatal(getFilename(this) + ": invalid section index: " + Twine(Index));
470   }
471 
472   if (S == &InputSectionBase<ELFT>::Discarded)
473     return S;
474   return S->Repl;
475 }
476 
477 template <class ELFT>
478 SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
479   int Binding = Sym->getBinding();
480   InputSectionBase<ELFT> *Sec = getSection(*Sym);
481   if (Binding == STB_LOCAL) {
482     if (Sym->getType() == STT_FILE)
483       SourceFile = check(Sym->getName(this->StringTable));
484     if (Sym->st_shndx == SHN_UNDEF)
485       return new (this->Alloc)
486           Undefined(Sym->st_name, Sym->st_other, Sym->getType(), this);
487     return new (this->Alloc) DefinedRegular<ELFT>(*Sym, Sec);
488   }
489 
490   StringRef Name = check(Sym->getName(this->StringTable));
491 
492   switch (Sym->st_shndx) {
493   case SHN_UNDEF:
494     return elf::Symtab<ELFT>::X->addUndefined(Name, Binding, Sym->st_other,
495                                               Sym->getType(),
496                                               /*CanOmitFromDynSym*/ false, this)
497         ->body();
498   case SHN_COMMON:
499     if (Sym->st_value == 0 || Sym->st_value >= UINT32_MAX)
500       fatal(getFilename(this) + ": common symbol '" + Name +
501             "' has invalid alignment: " + Twine(Sym->st_value));
502     return elf::Symtab<ELFT>::X->addCommon(Name, Sym->st_size, Sym->st_value,
503                                            Binding, Sym->st_other,
504                                            Sym->getType(), this)
505         ->body();
506   }
507 
508   switch (Binding) {
509   default:
510     fatal(getFilename(this) + ": unexpected binding: " + Twine(Binding));
511   case STB_GLOBAL:
512   case STB_WEAK:
513   case STB_GNU_UNIQUE:
514     if (Sec == &InputSection<ELFT>::Discarded)
515       return elf::Symtab<ELFT>::X->addUndefined(Name, Binding, Sym->st_other,
516                                                 Sym->getType(),
517                                                 /*CanOmitFromDynSym*/ false,
518                                                 this)
519           ->body();
520     return elf::Symtab<ELFT>::X->addRegular(Name, *Sym, Sec)->body();
521   }
522 }
523 
524 template <class ELFT> void ArchiveFile::parse() {
525   File = check(Archive::create(MB), "failed to parse archive");
526 
527   // Read the symbol table to construct Lazy objects.
528   for (const Archive::Symbol &Sym : File->symbols())
529     Symtab<ELFT>::X->addLazyArchive(this, Sym);
530 }
531 
532 // Returns a buffer pointing to a member file containing a given symbol.
533 std::pair<MemoryBufferRef, uint64_t>
534 ArchiveFile::getMember(const Archive::Symbol *Sym) {
535   Archive::Child C =
536       check(Sym->getMember(),
537             "could not get the member for symbol " + Sym->getName());
538 
539   if (!Seen.insert(C.getChildOffset()).second)
540     return {MemoryBufferRef(), 0};
541 
542   MemoryBufferRef Ret =
543       check(C.getMemoryBufferRef(),
544             "could not get the buffer for the member defining symbol " +
545                 Sym->getName());
546 
547   if (C.getParent()->isThin() && Driver->Cpio)
548     Driver->Cpio->append(relativeToRoot(check(C.getFullName())),
549                          Ret.getBuffer());
550   if (C.getParent()->isThin())
551     return {Ret, 0};
552   return {Ret, C.getChildOffset()};
553 }
554 
555 template <class ELFT>
556 SharedFile<ELFT>::SharedFile(BumpPtrAllocator &Alloc, MemoryBufferRef M)
557     : ELFFileBase<ELFT>(Base::SharedKind, M), AsNeeded(Config->AsNeeded) {}
558 
559 template <class ELFT>
560 const typename ELFT::Shdr *
561 SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const {
562   uint32_t Index = this->getSectionIndex(Sym);
563   if (Index == 0)
564     return nullptr;
565   return check(this->ELFObj.getSection(Index));
566 }
567 
568 // Partially parse the shared object file so that we can call
569 // getSoName on this object.
570 template <class ELFT> void SharedFile<ELFT>::parseSoName() {
571   typedef typename ELFT::Dyn Elf_Dyn;
572   typedef typename ELFT::uint uintX_t;
573   const Elf_Shdr *DynamicSec = nullptr;
574 
575   const ELFFile<ELFT> Obj = this->ELFObj;
576   for (const Elf_Shdr &Sec : Obj.sections()) {
577     switch (Sec.sh_type) {
578     default:
579       continue;
580     case SHT_DYNSYM:
581       this->Symtab = &Sec;
582       break;
583     case SHT_DYNAMIC:
584       DynamicSec = &Sec;
585       break;
586     case SHT_SYMTAB_SHNDX:
587       this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec));
588       break;
589     case SHT_GNU_versym:
590       this->VersymSec = &Sec;
591       break;
592     case SHT_GNU_verdef:
593       this->VerdefSec = &Sec;
594       break;
595     }
596   }
597 
598   this->initStringTable();
599 
600   // DSOs are identified by soname, and they usually contain
601   // DT_SONAME tag in their header. But if they are missing,
602   // filenames are used as default sonames.
603   SoName = sys::path::filename(this->getName());
604 
605   if (!DynamicSec)
606     return;
607 
608   ArrayRef<Elf_Dyn> Arr =
609       check(Obj.template getSectionContentsAsArray<Elf_Dyn>(DynamicSec),
610             getFilename(this) + ": getSectionContentsAsArray failed");
611   for (const Elf_Dyn &Dyn : Arr) {
612     if (Dyn.d_tag == DT_SONAME) {
613       uintX_t Val = Dyn.getVal();
614       if (Val >= this->StringTable.size())
615         fatal(getFilename(this) + ": invalid DT_SONAME entry");
616       SoName = StringRef(this->StringTable.data() + Val);
617       return;
618     }
619   }
620 }
621 
622 // Parse the version definitions in the object file if present. Returns a vector
623 // whose nth element contains a pointer to the Elf_Verdef for version identifier
624 // n. Version identifiers that are not definitions map to nullptr. The array
625 // always has at least length 1.
626 template <class ELFT>
627 std::vector<const typename ELFT::Verdef *>
628 SharedFile<ELFT>::parseVerdefs(const Elf_Versym *&Versym) {
629   std::vector<const Elf_Verdef *> Verdefs(1);
630   // We only need to process symbol versions for this DSO if it has both a
631   // versym and a verdef section, which indicates that the DSO contains symbol
632   // version definitions.
633   if (!VersymSec || !VerdefSec)
634     return Verdefs;
635 
636   // The location of the first global versym entry.
637   Versym = reinterpret_cast<const Elf_Versym *>(this->ELFObj.base() +
638                                                 VersymSec->sh_offset) +
639            this->Symtab->sh_info;
640 
641   // We cannot determine the largest verdef identifier without inspecting
642   // every Elf_Verdef, but both bfd and gold assign verdef identifiers
643   // sequentially starting from 1, so we predict that the largest identifier
644   // will be VerdefCount.
645   unsigned VerdefCount = VerdefSec->sh_info;
646   Verdefs.resize(VerdefCount + 1);
647 
648   // Build the Verdefs array by following the chain of Elf_Verdef objects
649   // from the start of the .gnu.version_d section.
650   const uint8_t *Verdef = this->ELFObj.base() + VerdefSec->sh_offset;
651   for (unsigned I = 0; I != VerdefCount; ++I) {
652     auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef);
653     Verdef += CurVerdef->vd_next;
654     unsigned VerdefIndex = CurVerdef->vd_ndx;
655     if (Verdefs.size() <= VerdefIndex)
656       Verdefs.resize(VerdefIndex + 1);
657     Verdefs[VerdefIndex] = CurVerdef;
658   }
659 
660   return Verdefs;
661 }
662 
663 // Fully parse the shared object file. This must be called after parseSoName().
664 template <class ELFT> void SharedFile<ELFT>::parseRest() {
665   // Create mapping from version identifiers to Elf_Verdef entries.
666   const Elf_Versym *Versym = nullptr;
667   std::vector<const Elf_Verdef *> Verdefs = parseVerdefs(Versym);
668 
669   Elf_Sym_Range Syms = this->getElfSymbols(true);
670   for (const Elf_Sym &Sym : Syms) {
671     unsigned VersymIndex = 0;
672     if (Versym) {
673       VersymIndex = Versym->vs_index;
674       ++Versym;
675     }
676 
677     StringRef Name = check(Sym.getName(this->StringTable));
678     if (Sym.isUndefined()) {
679       Undefs.push_back(Name);
680       continue;
681     }
682 
683     if (Versym) {
684       // Ignore local symbols and non-default versions.
685       if (VersymIndex == VER_NDX_LOCAL || (VersymIndex & VERSYM_HIDDEN))
686         continue;
687     }
688 
689     const Elf_Verdef *V =
690         VersymIndex == VER_NDX_GLOBAL ? nullptr : Verdefs[VersymIndex];
691     elf::Symtab<ELFT>::X->addShared(this, Name, Sym, V);
692   }
693 }
694 
695 static ELFKind getBitcodeELFKind(MemoryBufferRef MB) {
696   Triple T(getBitcodeTargetTriple(MB, Driver->Context));
697   if (T.isLittleEndian())
698     return T.isArch64Bit() ? ELF64LEKind : ELF32LEKind;
699   return T.isArch64Bit() ? ELF64BEKind : ELF32BEKind;
700 }
701 
702 static uint8_t getBitcodeMachineKind(MemoryBufferRef MB) {
703   Triple T(getBitcodeTargetTriple(MB, Driver->Context));
704   switch (T.getArch()) {
705   case Triple::aarch64:
706     return EM_AARCH64;
707   case Triple::arm:
708     return EM_ARM;
709   case Triple::mips:
710   case Triple::mipsel:
711   case Triple::mips64:
712   case Triple::mips64el:
713     return EM_MIPS;
714   case Triple::ppc:
715     return EM_PPC;
716   case Triple::ppc64:
717     return EM_PPC64;
718   case Triple::x86:
719     return T.isOSIAMCU() ? EM_IAMCU : EM_386;
720   case Triple::x86_64:
721     return EM_X86_64;
722   default:
723     fatal(MB.getBufferIdentifier() +
724           ": could not infer e_machine from bitcode target triple " + T.str());
725   }
726 }
727 
728 BitcodeFile::BitcodeFile(MemoryBufferRef MB) : InputFile(BitcodeKind, MB) {
729   EKind = getBitcodeELFKind(MB);
730   EMachine = getBitcodeMachineKind(MB);
731 }
732 
733 static uint8_t mapVisibility(GlobalValue::VisibilityTypes GvVisibility) {
734   switch (GvVisibility) {
735   case GlobalValue::DefaultVisibility:
736     return STV_DEFAULT;
737   case GlobalValue::HiddenVisibility:
738     return STV_HIDDEN;
739   case GlobalValue::ProtectedVisibility:
740     return STV_PROTECTED;
741   }
742   llvm_unreachable("unknown visibility");
743 }
744 
745 template <class ELFT>
746 static Symbol *createBitcodeSymbol(const std::vector<bool> &KeptComdats,
747                                    const lto::InputFile::Symbol &ObjSym,
748                                    StringSaver &Saver, BitcodeFile *F) {
749   StringRef NameRef = Saver.save(ObjSym.getName());
750   uint32_t Flags = ObjSym.getFlags();
751   uint32_t Binding = (Flags & BasicSymbolRef::SF_Weak) ? STB_WEAK : STB_GLOBAL;
752 
753   uint8_t Type = ObjSym.isTLS() ? STT_TLS : STT_NOTYPE;
754   uint8_t Visibility = mapVisibility(ObjSym.getVisibility());
755   bool CanOmitFromDynSym = ObjSym.canBeOmittedFromSymbolTable();
756 
757   int C = check(ObjSym.getComdatIndex());
758   if (C != -1 && !KeptComdats[C])
759     return Symtab<ELFT>::X->addUndefined(NameRef, Binding, Visibility, Type,
760                                          CanOmitFromDynSym, F);
761 
762   if (Flags & BasicSymbolRef::SF_Undefined)
763     return Symtab<ELFT>::X->addUndefined(NameRef, Binding, Visibility, Type,
764                                          CanOmitFromDynSym, F);
765 
766   if (Flags & BasicSymbolRef::SF_Common)
767     return Symtab<ELFT>::X->addCommon(NameRef, ObjSym.getCommonSize(),
768                                       ObjSym.getCommonAlignment(), Binding,
769                                       Visibility, STT_OBJECT, F);
770 
771   return Symtab<ELFT>::X->addBitcode(NameRef, Binding, Visibility, Type,
772                                      CanOmitFromDynSym, F);
773 }
774 
775 template <class ELFT>
776 void BitcodeFile::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
777 
778   // Here we pass a new MemoryBufferRef which is identified by ArchiveName
779   // (the fully resolved path of the archive) + member name + offset of the
780   // member in the archive.
781   // ThinLTO uses the MemoryBufferRef identifier to access its internal
782   // data structures and if two archives define two members with the same name,
783   // this causes a collision which result in only one of the objects being
784   // taken into consideration at LTO time (which very likely causes undefined
785   // symbols later in the link stage).
786   Obj = check(lto::InputFile::create(MemoryBufferRef(
787       MB.getBuffer(), Saver.save(ArchiveName + MB.getBufferIdentifier() +
788                                  utostr(OffsetInArchive)))));
789 
790   std::vector<bool> KeptComdats;
791   for (StringRef S : Obj->getComdatTable()) {
792     StringRef N = Saver.save(S);
793     KeptComdats.push_back(ComdatGroups.insert(CachedHashStringRef(N)).second);
794   }
795 
796   for (const lto::InputFile::Symbol &ObjSym : Obj->symbols())
797     Symbols.push_back(
798         createBitcodeSymbol<ELFT>(KeptComdats, ObjSym, Saver, this));
799 }
800 
801 template <template <class> class T>
802 static InputFile *createELFFile(BumpPtrAllocator &Alloc, MemoryBufferRef MB) {
803   unsigned char Size;
804   unsigned char Endian;
805   std::tie(Size, Endian) = getElfArchType(MB.getBuffer());
806   if (Endian != ELFDATA2LSB && Endian != ELFDATA2MSB)
807     fatal("invalid data encoding: " + MB.getBufferIdentifier());
808 
809   InputFile *Obj;
810   if (Size == ELFCLASS32 && Endian == ELFDATA2LSB)
811     Obj = new T<ELF32LE>(Alloc, MB);
812   else if (Size == ELFCLASS32 && Endian == ELFDATA2MSB)
813     Obj = new T<ELF32BE>(Alloc, MB);
814   else if (Size == ELFCLASS64 && Endian == ELFDATA2LSB)
815     Obj = new T<ELF64LE>(Alloc, MB);
816   else if (Size == ELFCLASS64 && Endian == ELFDATA2MSB)
817     Obj = new T<ELF64BE>(Alloc, MB);
818   else
819     fatal("invalid file class: " + MB.getBufferIdentifier());
820 
821   if (!Config->FirstElf)
822     Config->FirstElf = Obj;
823   return Obj;
824 }
825 
826 template <class ELFT> void BinaryFile::parse() {
827   StringRef Buf = MB.getBuffer();
828   ArrayRef<uint8_t> Data =
829       makeArrayRef<uint8_t>((const uint8_t *)Buf.data(), Buf.size());
830 
831   std::string Filename = MB.getBufferIdentifier();
832   std::transform(Filename.begin(), Filename.end(), Filename.begin(),
833                  [](char C) { return isalnum(C) ? C : '_'; });
834   Filename = "_binary_" + Filename;
835   StringRef StartName = Saver.save(Twine(Filename) + "_start");
836   StringRef EndName = Saver.save(Twine(Filename) + "_end");
837   StringRef SizeName = Saver.save(Twine(Filename) + "_size");
838 
839   auto *Section =
840       new InputSection<ELFT>(SHF_ALLOC, SHT_PROGBITS, 8, Data, ".data");
841   Sections.push_back(Section);
842 
843   elf::Symtab<ELFT>::X->addRegular(StartName, STV_DEFAULT, Section, STB_GLOBAL,
844                                    STT_OBJECT, 0);
845   elf::Symtab<ELFT>::X->addRegular(EndName, STV_DEFAULT, Section, STB_GLOBAL,
846                                    STT_OBJECT, Data.size());
847   elf::Symtab<ELFT>::X->addRegular(SizeName, STV_DEFAULT, nullptr, STB_GLOBAL,
848                                    STT_OBJECT, Data.size());
849 }
850 
851 static bool isBitcode(MemoryBufferRef MB) {
852   using namespace sys::fs;
853   return identify_magic(MB.getBuffer()) == file_magic::bitcode;
854 }
855 
856 InputFile *elf::createObjectFile(BumpPtrAllocator &Alloc, MemoryBufferRef MB,
857                                  StringRef ArchiveName,
858                                  uint64_t OffsetInArchive) {
859   InputFile *F = isBitcode(MB) ? new BitcodeFile(MB)
860                                : createELFFile<ObjectFile>(Alloc, MB);
861   F->ArchiveName = ArchiveName;
862   F->OffsetInArchive = OffsetInArchive;
863   return F;
864 }
865 
866 InputFile *elf::createSharedFile(BumpPtrAllocator &Alloc, MemoryBufferRef MB) {
867   return createELFFile<SharedFile>(Alloc, MB);
868 }
869 
870 MemoryBufferRef LazyObjectFile::getBuffer() {
871   if (Seen)
872     return MemoryBufferRef();
873   Seen = true;
874   return MB;
875 }
876 
877 template <class ELFT> void LazyObjectFile::parse() {
878   for (StringRef Sym : getSymbols())
879     Symtab<ELFT>::X->addLazyObject(Sym, *this);
880 }
881 
882 template <class ELFT> std::vector<StringRef> LazyObjectFile::getElfSymbols() {
883   typedef typename ELFT::Shdr Elf_Shdr;
884   typedef typename ELFT::Sym Elf_Sym;
885   typedef typename ELFT::SymRange Elf_Sym_Range;
886 
887   const ELFFile<ELFT> Obj = createELFObj<ELFT>(this->MB);
888   for (const Elf_Shdr &Sec : Obj.sections()) {
889     if (Sec.sh_type != SHT_SYMTAB)
890       continue;
891     Elf_Sym_Range Syms = Obj.symbols(&Sec);
892     uint32_t FirstNonLocal = Sec.sh_info;
893     StringRef StringTable = check(Obj.getStringTableForSymtab(Sec));
894     std::vector<StringRef> V;
895     for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal))
896       if (Sym.st_shndx != SHN_UNDEF)
897         V.push_back(check(Sym.getName(StringTable)));
898     return V;
899   }
900   return {};
901 }
902 
903 std::vector<StringRef> LazyObjectFile::getBitcodeSymbols() {
904   std::unique_ptr<lto::InputFile> Obj = check(lto::InputFile::create(this->MB));
905   std::vector<StringRef> V;
906   for (const lto::InputFile::Symbol &Sym : Obj->symbols())
907     if (!(Sym.getFlags() & BasicSymbolRef::SF_Undefined))
908       V.push_back(Saver.save(Sym.getName()));
909   return V;
910 }
911 
912 // Returns a vector of globally-visible defined symbol names.
913 std::vector<StringRef> LazyObjectFile::getSymbols() {
914   if (isBitcode(this->MB))
915     return getBitcodeSymbols();
916 
917   unsigned char Size;
918   unsigned char Endian;
919   std::tie(Size, Endian) = getElfArchType(this->MB.getBuffer());
920   if (Size == ELFCLASS32) {
921     if (Endian == ELFDATA2LSB)
922       return getElfSymbols<ELF32LE>();
923     return getElfSymbols<ELF32BE>();
924   }
925   if (Endian == ELFDATA2LSB)
926     return getElfSymbols<ELF64LE>();
927   return getElfSymbols<ELF64BE>();
928 }
929 
930 template void ArchiveFile::parse<ELF32LE>();
931 template void ArchiveFile::parse<ELF32BE>();
932 template void ArchiveFile::parse<ELF64LE>();
933 template void ArchiveFile::parse<ELF64BE>();
934 
935 template void BitcodeFile::parse<ELF32LE>(DenseSet<CachedHashStringRef> &);
936 template void BitcodeFile::parse<ELF32BE>(DenseSet<CachedHashStringRef> &);
937 template void BitcodeFile::parse<ELF64LE>(DenseSet<CachedHashStringRef> &);
938 template void BitcodeFile::parse<ELF64BE>(DenseSet<CachedHashStringRef> &);
939 
940 template void LazyObjectFile::parse<ELF32LE>();
941 template void LazyObjectFile::parse<ELF32BE>();
942 template void LazyObjectFile::parse<ELF64LE>();
943 template void LazyObjectFile::parse<ELF64BE>();
944 
945 template class elf::ELFFileBase<ELF32LE>;
946 template class elf::ELFFileBase<ELF32BE>;
947 template class elf::ELFFileBase<ELF64LE>;
948 template class elf::ELFFileBase<ELF64BE>;
949 
950 template class elf::ObjectFile<ELF32LE>;
951 template class elf::ObjectFile<ELF32BE>;
952 template class elf::ObjectFile<ELF64LE>;
953 template class elf::ObjectFile<ELF64BE>;
954 
955 template class elf::SharedFile<ELF32LE>;
956 template class elf::SharedFile<ELF32BE>;
957 template class elf::SharedFile<ELF64LE>;
958 template class elf::SharedFile<ELF64BE>;
959 
960 template void BinaryFile::parse<ELF32LE>();
961 template void BinaryFile::parse<ELF32BE>();
962 template void BinaryFile::parse<ELF64LE>();
963 template void BinaryFile::parse<ELF64BE>();
964 
965 template class elf::DIHelper<ELF32LE>;
966 template class elf::DIHelper<ELF32BE>;
967 template class elf::DIHelper<ELF64LE>;
968 template class elf::DIHelper<ELF64BE>;
969