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 "InputSection.h"
12 #include "LinkerScript.h"
13 #include "SymbolTable.h"
14 #include "Symbols.h"
15 #include "SyntheticSections.h"
16 #include "lld/Common/ErrorHandler.h"
17 #include "lld/Common/Memory.h"
18 #include "llvm/ADT/STLExtras.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/Object/ELFObjectFile.h"
26 #include "llvm/Support/ARMAttributeParser.h"
27 #include "llvm/Support/ARMBuildAttributes.h"
28 #include "llvm/Support/Path.h"
29 #include "llvm/Support/TarWriter.h"
30 #include "llvm/Support/raw_ostream.h"
31 
32 using namespace llvm;
33 using namespace llvm::ELF;
34 using namespace llvm::object;
35 using namespace llvm::sys::fs;
36 
37 using namespace lld;
38 using namespace lld::elf;
39 
40 std::vector<BinaryFile *> elf::BinaryFiles;
41 std::vector<BitcodeFile *> elf::BitcodeFiles;
42 std::vector<InputFile *> elf::ObjectFiles;
43 std::vector<InputFile *> elf::SharedFiles;
44 
45 TarWriter *elf::Tar;
46 
47 InputFile::InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
48 
49 Optional<MemoryBufferRef> elf::readFile(StringRef Path) {
50   // The --chroot option changes our virtual root directory.
51   // This is useful when you are dealing with files created by --reproduce.
52   if (!Config->Chroot.empty() && Path.startswith("/"))
53     Path = Saver.save(Config->Chroot + Path);
54 
55   log(Path);
56 
57   auto MBOrErr = MemoryBuffer::getFile(Path);
58   if (auto EC = MBOrErr.getError()) {
59     error("cannot open " + Path + ": " + EC.message());
60     return None;
61   }
62 
63   std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
64   MemoryBufferRef MBRef = MB->getMemBufferRef();
65   make<std::unique_ptr<MemoryBuffer>>(std::move(MB)); // take MB ownership
66 
67   if (Tar)
68     Tar->append(relativeToRoot(Path), MBRef.getBuffer());
69   return MBRef;
70 }
71 
72 template <class ELFT> void ObjFile<ELFT>::initializeDwarf() {
73   DWARFContext Dwarf(make_unique<LLDDwarfObj<ELFT>>(this));
74   const DWARFObject &Obj = Dwarf.getDWARFObj();
75   DwarfLine.reset(new DWARFDebugLine);
76   DWARFDataExtractor LineData(Obj, Obj.getLineSection(), Config->IsLE,
77                               Config->Wordsize);
78 
79   // The second parameter is offset in .debug_line section
80   // for compilation unit (CU) of interest. We have only one
81   // CU (object file), so offset is always 0.
82   // FIXME: Provide the associated DWARFUnit if there is one.  DWARF v5
83   // needs it in order to find indirect strings.
84   const DWARFDebugLine::LineTable *LT =
85       DwarfLine->getOrParseLineTable(LineData, 0, nullptr);
86 
87   // Return if there is no debug information about CU available.
88   if (!Dwarf.getNumCompileUnits())
89     return;
90 
91   // Loop over variable records and insert them to VariableLoc.
92   DWARFCompileUnit *CU = Dwarf.getCompileUnitAtIndex(0);
93   for (const auto &Entry : CU->dies()) {
94     DWARFDie Die(CU, &Entry);
95     // Skip all tags that are not variables.
96     if (Die.getTag() != dwarf::DW_TAG_variable)
97       continue;
98 
99     // Skip if a local variable because we don't need them for generating error
100     // messages. In general, only non-local symbols can fail to be linked.
101     if (!dwarf::toUnsigned(Die.find(dwarf::DW_AT_external), 0))
102       continue;
103 
104     // Get the source filename index for the variable.
105     unsigned File = dwarf::toUnsigned(Die.find(dwarf::DW_AT_decl_file), 0);
106     if (!LT->hasFileAtIndex(File))
107       continue;
108 
109     // Get the line number on which the variable is declared.
110     unsigned Line = dwarf::toUnsigned(Die.find(dwarf::DW_AT_decl_line), 0);
111 
112     // Get the name of the variable and add the collected information to
113     // VariableLoc. Usually Name is non-empty, but it can be empty if the input
114     // object file lacks some debug info.
115     StringRef Name = dwarf::toString(Die.find(dwarf::DW_AT_name), "");
116     if (!Name.empty())
117       VariableLoc.insert({Name, {File, Line}});
118   }
119 }
120 
121 // Returns the pair of file name and line number describing location of data
122 // object (variable, array, etc) definition.
123 template <class ELFT>
124 Optional<std::pair<std::string, unsigned>>
125 ObjFile<ELFT>::getVariableLoc(StringRef Name) {
126   llvm::call_once(InitDwarfLine, [this]() { initializeDwarf(); });
127 
128   // There is always only one CU so it's offset is 0.
129   const DWARFDebugLine::LineTable *LT = DwarfLine->getLineTable(0);
130   if (!LT)
131     return None;
132 
133   // Return if we have no debug information about data object.
134   auto It = VariableLoc.find(Name);
135   if (It == VariableLoc.end())
136     return None;
137 
138   // Take file name string from line table.
139   std::string FileName;
140   if (!LT->getFileNameByIndex(
141           It->second.first /* File */, nullptr,
142           DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, FileName))
143     return None;
144 
145   return std::make_pair(FileName, It->second.second /*Line*/);
146 }
147 
148 // Returns source line information for a given offset
149 // using DWARF debug info.
150 template <class ELFT>
151 Optional<DILineInfo> ObjFile<ELFT>::getDILineInfo(InputSectionBase *S,
152                                                   uint64_t Offset) {
153   llvm::call_once(InitDwarfLine, [this]() { initializeDwarf(); });
154 
155   // The offset to CU is 0.
156   const DWARFDebugLine::LineTable *Tbl = DwarfLine->getLineTable(0);
157   if (!Tbl)
158     return None;
159 
160   // Use fake address calcuated by adding section file offset and offset in
161   // section. See comments for ObjectInfo class.
162   DILineInfo Info;
163   Tbl->getFileLineInfoForAddress(
164       S->getOffsetInFile() + Offset, nullptr,
165       DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, Info);
166   if (Info.Line == 0)
167     return None;
168   return Info;
169 }
170 
171 // Returns source line information for a given offset
172 // using DWARF debug info.
173 template <class ELFT>
174 std::string ObjFile<ELFT>::getLineInfo(InputSectionBase *S, uint64_t Offset) {
175   if (Optional<DILineInfo> Info = getDILineInfo(S, Offset))
176     return Info->FileName + ":" + std::to_string(Info->Line);
177   return "";
178 }
179 
180 // Returns "<internal>", "foo.a(bar.o)" or "baz.o".
181 std::string lld::toString(const InputFile *F) {
182   if (!F)
183     return "<internal>";
184 
185   if (F->ToStringCache.empty()) {
186     if (F->ArchiveName.empty())
187       F->ToStringCache = F->getName();
188     else
189       F->ToStringCache = (F->ArchiveName + "(" + F->getName() + ")").str();
190   }
191   return F->ToStringCache;
192 }
193 
194 template <class ELFT>
195 ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB) : InputFile(K, MB) {
196   if (ELFT::TargetEndianness == support::little)
197     EKind = ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind;
198   else
199     EKind = ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind;
200 
201   EMachine = getObj().getHeader()->e_machine;
202   OSABI = getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI];
203 }
204 
205 template <class ELFT>
206 typename ELFT::SymRange ELFFileBase<ELFT>::getGlobalELFSyms() {
207   return makeArrayRef(ELFSyms.begin() + FirstNonLocal, ELFSyms.end());
208 }
209 
210 template <class ELFT>
211 uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
212   return CHECK(getObj().getSectionIndex(&Sym, ELFSyms, SymtabSHNDX), this);
213 }
214 
215 template <class ELFT>
216 void ELFFileBase<ELFT>::initSymtab(ArrayRef<Elf_Shdr> Sections,
217                                    const Elf_Shdr *Symtab) {
218   FirstNonLocal = Symtab->sh_info;
219   ELFSyms = CHECK(getObj().symbols(Symtab), this);
220   if (FirstNonLocal == 0 || FirstNonLocal > ELFSyms.size())
221     fatal(toString(this) + ": invalid sh_info in symbol table");
222 
223   StringTable =
224       CHECK(getObj().getStringTableForSymtab(*Symtab, Sections), this);
225 }
226 
227 template <class ELFT>
228 ObjFile<ELFT>::ObjFile(MemoryBufferRef M, StringRef ArchiveName)
229     : ELFFileBase<ELFT>(Base::ObjKind, M) {
230   this->ArchiveName = ArchiveName;
231 }
232 
233 template <class ELFT> ArrayRef<Symbol *> ObjFile<ELFT>::getLocalSymbols() {
234   if (this->Symbols.empty())
235     return {};
236   return makeArrayRef(this->Symbols).slice(1, this->FirstNonLocal - 1);
237 }
238 
239 template <class ELFT>
240 void ObjFile<ELFT>::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
241   // Read section and symbol tables.
242   initializeSections(ComdatGroups);
243   initializeSymbols();
244 }
245 
246 // Sections with SHT_GROUP and comdat bits define comdat section groups.
247 // They are identified and deduplicated by group name. This function
248 // returns a group name.
249 template <class ELFT>
250 StringRef ObjFile<ELFT>::getShtGroupSignature(ArrayRef<Elf_Shdr> Sections,
251                                               const Elf_Shdr &Sec) {
252   // Group signatures are stored as symbol names in object files.
253   // sh_info contains a symbol index, so we fetch a symbol and read its name.
254   if (this->ELFSyms.empty())
255     this->initSymtab(
256         Sections, CHECK(object::getSection<ELFT>(Sections, Sec.sh_link), this));
257 
258   const Elf_Sym *Sym =
259       CHECK(object::getSymbol<ELFT>(this->ELFSyms, Sec.sh_info), this);
260   StringRef Signature = CHECK(Sym->getName(this->StringTable), this);
261 
262   // As a special case, if a symbol is a section symbol and has no name,
263   // we use a section name as a signature.
264   //
265   // Such SHT_GROUP sections are invalid from the perspective of the ELF
266   // standard, but GNU gold 1.14 (the neweset version as of July 2017) or
267   // older produce such sections as outputs for the -r option, so we need
268   // a bug-compatibility.
269   if (Signature.empty() && Sym->getType() == STT_SECTION)
270     return getSectionName(Sec);
271   return Signature;
272 }
273 
274 template <class ELFT>
275 ArrayRef<typename ObjFile<ELFT>::Elf_Word>
276 ObjFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
277   const ELFFile<ELFT> &Obj = this->getObj();
278   ArrayRef<Elf_Word> Entries =
279       CHECK(Obj.template getSectionContentsAsArray<Elf_Word>(&Sec), this);
280   if (Entries.empty() || Entries[0] != GRP_COMDAT)
281     fatal(toString(this) + ": unsupported SHT_GROUP format");
282   return Entries.slice(1);
283 }
284 
285 template <class ELFT> bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) {
286   // We don't merge sections if -O0 (default is -O1). This makes sometimes
287   // the linker significantly faster, although the output will be bigger.
288   if (Config->Optimize == 0)
289     return false;
290 
291   // A mergeable section with size 0 is useless because they don't have
292   // any data to merge. A mergeable string section with size 0 can be
293   // argued as invalid because it doesn't end with a null character.
294   // We'll avoid a mess by handling them as if they were non-mergeable.
295   if (Sec.sh_size == 0)
296     return false;
297 
298   // Check for sh_entsize. The ELF spec is not clear about the zero
299   // sh_entsize. It says that "the member [sh_entsize] contains 0 if
300   // the section does not hold a table of fixed-size entries". We know
301   // that Rust 1.13 produces a string mergeable section with a zero
302   // sh_entsize. Here we just accept it rather than being picky about it.
303   uint64_t EntSize = Sec.sh_entsize;
304   if (EntSize == 0)
305     return false;
306   if (Sec.sh_size % EntSize)
307     fatal(toString(this) +
308           ": SHF_MERGE section size must be a multiple of sh_entsize");
309 
310   uint64_t Flags = Sec.sh_flags;
311   if (!(Flags & SHF_MERGE))
312     return false;
313   if (Flags & SHF_WRITE)
314     fatal(toString(this) + ": writable SHF_MERGE section is not supported");
315 
316   return true;
317 }
318 
319 template <class ELFT>
320 void ObjFile<ELFT>::initializeSections(
321     DenseSet<CachedHashStringRef> &ComdatGroups) {
322   const ELFFile<ELFT> &Obj = this->getObj();
323 
324   ArrayRef<Elf_Shdr> ObjSections = CHECK(this->getObj().sections(), this);
325   uint64_t Size = ObjSections.size();
326   this->Sections.resize(Size);
327   this->SectionStringTable =
328       CHECK(Obj.getSectionStringTable(ObjSections), this);
329 
330   for (size_t I = 0, E = ObjSections.size(); I < E; I++) {
331     if (this->Sections[I] == &InputSection::Discarded)
332       continue;
333     const Elf_Shdr &Sec = ObjSections[I];
334 
335     // SHF_EXCLUDE'ed sections are discarded by the linker. However,
336     // if -r is given, we'll let the final link discard such sections.
337     // This is compatible with GNU.
338     if ((Sec.sh_flags & SHF_EXCLUDE) && !Config->Relocatable) {
339       this->Sections[I] = &InputSection::Discarded;
340       continue;
341     }
342 
343     switch (Sec.sh_type) {
344     case SHT_GROUP: {
345       // De-duplicate section groups by their signatures.
346       StringRef Signature = getShtGroupSignature(ObjSections, Sec);
347       bool IsNew = ComdatGroups.insert(CachedHashStringRef(Signature)).second;
348       this->Sections[I] = &InputSection::Discarded;
349 
350       // If it is a new section group, we want to keep group members.
351       // Group leader sections, which contain indices of group members, are
352       // discarded because they are useless beyond this point. The only
353       // exception is the -r option because in order to produce re-linkable
354       // object files, we want to pass through basically everything.
355       if (IsNew) {
356         if (Config->Relocatable)
357           this->Sections[I] = createInputSection(Sec);
358         continue;
359       }
360 
361       // Otherwise, discard group members.
362       for (uint32_t SecIndex : getShtGroupEntries(Sec)) {
363         if (SecIndex >= Size)
364           fatal(toString(this) +
365                 ": invalid section index in group: " + Twine(SecIndex));
366         this->Sections[SecIndex] = &InputSection::Discarded;
367       }
368       break;
369     }
370     case SHT_SYMTAB:
371       this->initSymtab(ObjSections, &Sec);
372       break;
373     case SHT_SYMTAB_SHNDX:
374       this->SymtabSHNDX = CHECK(Obj.getSHNDXTable(Sec, ObjSections), this);
375       break;
376     case SHT_STRTAB:
377     case SHT_NULL:
378       break;
379     default:
380       this->Sections[I] = createInputSection(Sec);
381     }
382 
383     // .ARM.exidx sections have a reverse dependency on the InputSection they
384     // have a SHF_LINK_ORDER dependency, this is identified by the sh_link.
385     if (Sec.sh_flags & SHF_LINK_ORDER) {
386       if (Sec.sh_link >= this->Sections.size())
387         fatal(toString(this) + ": invalid sh_link index: " +
388               Twine(Sec.sh_link));
389       this->Sections[Sec.sh_link]->DependentSections.push_back(
390           cast<InputSection>(this->Sections[I]));
391     }
392   }
393 }
394 
395 // The ARM support in lld makes some use of instructions that are not available
396 // on all ARM architectures. Namely:
397 // - Use of BLX instruction for interworking between ARM and Thumb state.
398 // - Use of the extended Thumb branch encoding in relocation.
399 // - Use of the MOVT/MOVW instructions in Thumb Thunks.
400 // The ARM Attributes section contains information about the architecture chosen
401 // at compile time. We follow the convention that if at least one input object
402 // is compiled with an architecture that supports these features then lld is
403 // permitted to use them.
404 static void updateSupportedARMFeatures(const ARMAttributeParser &Attributes) {
405   if (!Attributes.hasAttribute(ARMBuildAttrs::CPU_arch))
406     return;
407   auto Arch = Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch);
408   switch (Arch) {
409   case ARMBuildAttrs::Pre_v4:
410   case ARMBuildAttrs::v4:
411   case ARMBuildAttrs::v4T:
412     // Architectures prior to v5 do not support BLX instruction
413     break;
414   case ARMBuildAttrs::v5T:
415   case ARMBuildAttrs::v5TE:
416   case ARMBuildAttrs::v5TEJ:
417   case ARMBuildAttrs::v6:
418   case ARMBuildAttrs::v6KZ:
419   case ARMBuildAttrs::v6K:
420     Config->ARMHasBlx = true;
421     // Architectures used in pre-Cortex processors do not support
422     // The J1 = 1 J2 = 1 Thumb branch range extension, with the exception
423     // of Architecture v6T2 (arm1156t2-s and arm1156t2f-s) that do.
424     break;
425   default:
426     // All other Architectures have BLX and extended branch encoding
427     Config->ARMHasBlx = true;
428     Config->ARMJ1J2BranchEncoding = true;
429     if (Arch != ARMBuildAttrs::v6_M && Arch != ARMBuildAttrs::v6S_M)
430       // All Architectures used in Cortex processors with the exception
431       // of v6-M and v6S-M have the MOVT and MOVW instructions.
432       Config->ARMHasMovtMovw = true;
433     break;
434   }
435 }
436 
437 template <class ELFT>
438 InputSectionBase *ObjFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
439   uint32_t Idx = Sec.sh_info;
440   if (Idx >= this->Sections.size())
441     fatal(toString(this) + ": invalid relocated section index: " + Twine(Idx));
442   InputSectionBase *Target = this->Sections[Idx];
443 
444   // Strictly speaking, a relocation section must be included in the
445   // group of the section it relocates. However, LLVM 3.3 and earlier
446   // would fail to do so, so we gracefully handle that case.
447   if (Target == &InputSection::Discarded)
448     return nullptr;
449 
450   if (!Target)
451     fatal(toString(this) + ": unsupported relocation reference");
452   return Target;
453 }
454 
455 // Create a regular InputSection class that has the same contents
456 // as a given section.
457 InputSectionBase *toRegularSection(MergeInputSection *Sec) {
458   auto *Ret = make<InputSection>(Sec->Flags, Sec->Type, Sec->Alignment,
459                                  Sec->Data, Sec->Name);
460   Ret->File = Sec->File;
461   return Ret;
462 }
463 
464 template <class ELFT>
465 InputSectionBase *ObjFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
466   StringRef Name = getSectionName(Sec);
467 
468   switch (Sec.sh_type) {
469   case SHT_ARM_ATTRIBUTES: {
470     if (Config->EMachine != EM_ARM)
471       break;
472     ARMAttributeParser Attributes;
473     ArrayRef<uint8_t> Contents = check(this->getObj().getSectionContents(&Sec));
474     Attributes.Parse(Contents, /*isLittle*/Config->EKind == ELF32LEKind);
475     updateSupportedARMFeatures(Attributes);
476     // FIXME: Retain the first attribute section we see. The eglibc ARM
477     // dynamic loaders require the presence of an attribute section for dlopen
478     // to work. In a full implementation we would merge all attribute sections.
479     if (InX::ARMAttributes == nullptr) {
480       InX::ARMAttributes = make<InputSection>(this, &Sec, Name);
481       return InX::ARMAttributes;
482     }
483     return &InputSection::Discarded;
484   }
485   case SHT_RELA:
486   case SHT_REL: {
487     // Find the relocation target section and associate this
488     // section with it. Target can be discarded, for example
489     // if it is a duplicated member of SHT_GROUP section, we
490     // do not create or proccess relocatable sections then.
491     InputSectionBase *Target = getRelocTarget(Sec);
492     if (!Target)
493       return nullptr;
494 
495     // This section contains relocation information.
496     // If -r is given, we do not interpret or apply relocation
497     // but just copy relocation sections to output.
498     if (Config->Relocatable)
499       return make<InputSection>(this, &Sec, Name);
500 
501     if (Target->FirstRelocation)
502       fatal(toString(this) +
503             ": multiple relocation sections to one section are not supported");
504 
505     // Mergeable sections with relocations are tricky because relocations
506     // need to be taken into account when comparing section contents for
507     // merging. It's not worth supporting such mergeable sections because
508     // they are rare and it'd complicates the internal design (we usually
509     // have to determine if two sections are mergeable early in the link
510     // process much before applying relocations). We simply handle mergeable
511     // sections with relocations as non-mergeable.
512     if (auto *MS = dyn_cast<MergeInputSection>(Target)) {
513       Target = toRegularSection(MS);
514       this->Sections[Sec.sh_info] = Target;
515     }
516 
517     size_t NumRelocations;
518     if (Sec.sh_type == SHT_RELA) {
519       ArrayRef<Elf_Rela> Rels = CHECK(this->getObj().relas(&Sec), this);
520       Target->FirstRelocation = Rels.begin();
521       NumRelocations = Rels.size();
522       Target->AreRelocsRela = true;
523     } else {
524       ArrayRef<Elf_Rel> Rels = CHECK(this->getObj().rels(&Sec), this);
525       Target->FirstRelocation = Rels.begin();
526       NumRelocations = Rels.size();
527       Target->AreRelocsRela = false;
528     }
529     assert(isUInt<31>(NumRelocations));
530     Target->NumRelocations = NumRelocations;
531 
532     // Relocation sections processed by the linker are usually removed
533     // from the output, so returning `nullptr` for the normal case.
534     // However, if -emit-relocs is given, we need to leave them in the output.
535     // (Some post link analysis tools need this information.)
536     if (Config->EmitRelocs) {
537       InputSection *RelocSec = make<InputSection>(this, &Sec, Name);
538       // We will not emit relocation section if target was discarded.
539       Target->DependentSections.push_back(RelocSec);
540       return RelocSec;
541     }
542     return nullptr;
543   }
544   }
545 
546   // The GNU linker uses .note.GNU-stack section as a marker indicating
547   // that the code in the object file does not expect that the stack is
548   // executable (in terms of NX bit). If all input files have the marker,
549   // the GNU linker adds a PT_GNU_STACK segment to tells the loader to
550   // make the stack non-executable. Most object files have this section as
551   // of 2017.
552   //
553   // But making the stack non-executable is a norm today for security
554   // reasons. Failure to do so may result in a serious security issue.
555   // Therefore, we make LLD always add PT_GNU_STACK unless it is
556   // explicitly told to do otherwise (by -z execstack). Because the stack
557   // executable-ness is controlled solely by command line options,
558   // .note.GNU-stack sections are simply ignored.
559   if (Name == ".note.GNU-stack")
560     return &InputSection::Discarded;
561 
562   // Split stacks is a feature to support a discontiguous stack. At least
563   // as of 2017, it seems that the feature is not being used widely.
564   // Only GNU gold supports that. We don't. For the details about that,
565   // see https://gcc.gnu.org/wiki/SplitStacks
566   if (Name == ".note.GNU-split-stack") {
567     error(toString(this) +
568           ": object file compiled with -fsplit-stack is not supported");
569     return &InputSection::Discarded;
570   }
571 
572   // The linkonce feature is a sort of proto-comdat. Some glibc i386 object
573   // files contain definitions of symbol "__x86.get_pc_thunk.bx" in linkonce
574   // sections. Drop those sections to avoid duplicate symbol errors.
575   // FIXME: This is glibc PR20543, we should remove this hack once that has been
576   // fixed for a while.
577   if (Name.startswith(".gnu.linkonce."))
578     return &InputSection::Discarded;
579 
580   // The linker merges EH (exception handling) frames and creates a
581   // .eh_frame_hdr section for runtime. So we handle them with a special
582   // class. For relocatable outputs, they are just passed through.
583   if (Name == ".eh_frame" && !Config->Relocatable)
584     return make<EhInputSection>(this, &Sec, Name);
585 
586   if (shouldMerge(Sec))
587     return make<MergeInputSection>(this, &Sec, Name);
588   return make<InputSection>(this, &Sec, Name);
589 }
590 
591 template <class ELFT>
592 StringRef ObjFile<ELFT>::getSectionName(const Elf_Shdr &Sec) {
593   return CHECK(this->getObj().getSectionName(&Sec, SectionStringTable), this);
594 }
595 
596 template <class ELFT> void ObjFile<ELFT>::initializeSymbols() {
597   this->Symbols.reserve(this->ELFSyms.size());
598   for (const Elf_Sym &Sym : this->ELFSyms)
599     this->Symbols.push_back(createSymbol(&Sym));
600 }
601 
602 template <class ELFT>
603 InputSectionBase *ObjFile<ELFT>::getSection(uint32_t Index) const {
604   if (Index == 0)
605     return nullptr;
606   if (Index >= this->Sections.size())
607     fatal(toString(this) + ": invalid section index: " + Twine(Index));
608 
609   if (InputSectionBase *Sec = this->Sections[Index])
610     return Sec->Repl;
611   return nullptr;
612 }
613 
614 template <class ELFT> Symbol *ObjFile<ELFT>::createSymbol(const Elf_Sym *Sym) {
615   int Binding = Sym->getBinding();
616   InputSectionBase *Sec = getSection(this->getSectionIndex(*Sym));
617 
618   uint8_t StOther = Sym->st_other;
619   uint8_t Type = Sym->getType();
620   uint64_t Value = Sym->st_value;
621   uint64_t Size = Sym->st_size;
622 
623   if (Binding == STB_LOCAL) {
624     if (Sym->getType() == STT_FILE)
625       SourceFile = CHECK(Sym->getName(this->StringTable), this);
626 
627     if (this->StringTable.size() <= Sym->st_name)
628       fatal(toString(this) + ": invalid symbol name offset");
629 
630     StringRefZ Name = this->StringTable.data() + Sym->st_name;
631     if (Sym->st_shndx == SHN_UNDEF)
632       return make<Undefined>(this, Name, Binding, StOther, Type);
633 
634     return make<Defined>(this, Name, Binding, StOther, Type, Value, Size, Sec);
635   }
636 
637   StringRef Name = CHECK(Sym->getName(this->StringTable), this);
638 
639   switch (Sym->st_shndx) {
640   case SHN_UNDEF:
641     return Symtab->addUndefined<ELFT>(Name, Binding, StOther, Type,
642                                       /*CanOmitFromDynSym=*/false, this);
643   case SHN_COMMON:
644     if (Value == 0 || Value >= UINT32_MAX)
645       fatal(toString(this) + ": common symbol '" + Name +
646             "' has invalid alignment: " + Twine(Value));
647     return Symtab->addCommon(Name, Size, Value, Binding, StOther, Type, this);
648   }
649 
650   switch (Binding) {
651   default:
652     fatal(toString(this) + ": unexpected binding: " + Twine(Binding));
653   case STB_GLOBAL:
654   case STB_WEAK:
655   case STB_GNU_UNIQUE:
656     if (Sec == &InputSection::Discarded)
657       return Symtab->addUndefined<ELFT>(Name, Binding, StOther, Type,
658                                         /*CanOmitFromDynSym=*/false, this);
659     return Symtab->addRegular<ELFT>(Name, StOther, Type, Value, Size, Binding,
660                                     Sec, this);
661   }
662 }
663 
664 ArchiveFile::ArchiveFile(std::unique_ptr<Archive> &&File)
665     : InputFile(ArchiveKind, File->getMemoryBufferRef()),
666       File(std::move(File)) {}
667 
668 template <class ELFT> void ArchiveFile::parse() {
669   Symbols.reserve(File->getNumberOfSymbols());
670   for (const Archive::Symbol &Sym : File->symbols())
671     Symbols.push_back(Symtab->addLazyArchive<ELFT>(Sym.getName(), this, Sym));
672 }
673 
674 // Returns a buffer pointing to a member file containing a given symbol.
675 std::pair<MemoryBufferRef, uint64_t>
676 ArchiveFile::getMember(const Archive::Symbol *Sym) {
677   Archive::Child C =
678       CHECK(Sym->getMember(), toString(this) +
679                                   ": could not get the member for symbol " +
680                                   Sym->getName());
681 
682   if (!Seen.insert(C.getChildOffset()).second)
683     return {MemoryBufferRef(), 0};
684 
685   MemoryBufferRef Ret =
686       CHECK(C.getMemoryBufferRef(),
687             toString(this) +
688                 ": could not get the buffer for the member defining symbol " +
689                 Sym->getName());
690 
691   if (C.getParent()->isThin() && Tar)
692     Tar->append(relativeToRoot(CHECK(C.getFullName(), this)), Ret.getBuffer());
693   if (C.getParent()->isThin())
694     return {Ret, 0};
695   return {Ret, C.getChildOffset()};
696 }
697 
698 template <class ELFT>
699 SharedFile<ELFT>::SharedFile(MemoryBufferRef M, StringRef DefaultSoName)
700     : ELFFileBase<ELFT>(Base::SharedKind, M), SoName(DefaultSoName),
701       IsNeeded(!Config->AsNeeded) {}
702 
703 // Partially parse the shared object file so that we can call
704 // getSoName on this object.
705 template <class ELFT> void SharedFile<ELFT>::parseSoName() {
706   const Elf_Shdr *DynamicSec = nullptr;
707   const ELFFile<ELFT> Obj = this->getObj();
708   ArrayRef<Elf_Shdr> Sections = CHECK(Obj.sections(), this);
709 
710   // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d.
711   for (const Elf_Shdr &Sec : Sections) {
712     switch (Sec.sh_type) {
713     default:
714       continue;
715     case SHT_DYNSYM:
716       this->initSymtab(Sections, &Sec);
717       break;
718     case SHT_DYNAMIC:
719       DynamicSec = &Sec;
720       break;
721     case SHT_SYMTAB_SHNDX:
722       this->SymtabSHNDX = CHECK(Obj.getSHNDXTable(Sec, Sections), this);
723       break;
724     case SHT_GNU_versym:
725       this->VersymSec = &Sec;
726       break;
727     case SHT_GNU_verdef:
728       this->VerdefSec = &Sec;
729       break;
730     }
731   }
732 
733   if (this->VersymSec && this->ELFSyms.empty())
734     error("SHT_GNU_versym should be associated with symbol table");
735 
736   // Search for a DT_SONAME tag to initialize this->SoName.
737   if (!DynamicSec)
738     return;
739   ArrayRef<Elf_Dyn> Arr =
740       CHECK(Obj.template getSectionContentsAsArray<Elf_Dyn>(DynamicSec), this);
741   for (const Elf_Dyn &Dyn : Arr) {
742     if (Dyn.d_tag == DT_SONAME) {
743       uint64_t Val = Dyn.getVal();
744       if (Val >= this->StringTable.size())
745         fatal(toString(this) + ": invalid DT_SONAME entry");
746       SoName = this->StringTable.data() + Val;
747       return;
748     }
749   }
750 }
751 
752 // Parse the version definitions in the object file if present. Returns a vector
753 // whose nth element contains a pointer to the Elf_Verdef for version identifier
754 // n. Version identifiers that are not definitions map to nullptr. The array
755 // always has at least length 1.
756 template <class ELFT>
757 std::vector<const typename ELFT::Verdef *>
758 SharedFile<ELFT>::parseVerdefs(const Elf_Versym *&Versym) {
759   std::vector<const Elf_Verdef *> Verdefs(1);
760   // We only need to process symbol versions for this DSO if it has both a
761   // versym and a verdef section, which indicates that the DSO contains symbol
762   // version definitions.
763   if (!VersymSec || !VerdefSec)
764     return Verdefs;
765 
766   // The location of the first global versym entry.
767   const char *Base = this->MB.getBuffer().data();
768   Versym = reinterpret_cast<const Elf_Versym *>(Base + VersymSec->sh_offset) +
769            this->FirstNonLocal;
770 
771   // We cannot determine the largest verdef identifier without inspecting
772   // every Elf_Verdef, but both bfd and gold assign verdef identifiers
773   // sequentially starting from 1, so we predict that the largest identifier
774   // will be VerdefCount.
775   unsigned VerdefCount = VerdefSec->sh_info;
776   Verdefs.resize(VerdefCount + 1);
777 
778   // Build the Verdefs array by following the chain of Elf_Verdef objects
779   // from the start of the .gnu.version_d section.
780   const char *Verdef = Base + VerdefSec->sh_offset;
781   for (unsigned I = 0; I != VerdefCount; ++I) {
782     auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef);
783     Verdef += CurVerdef->vd_next;
784     unsigned VerdefIndex = CurVerdef->vd_ndx;
785     if (Verdefs.size() <= VerdefIndex)
786       Verdefs.resize(VerdefIndex + 1);
787     Verdefs[VerdefIndex] = CurVerdef;
788   }
789 
790   return Verdefs;
791 }
792 
793 // Fully parse the shared object file. This must be called after parseSoName().
794 template <class ELFT> void SharedFile<ELFT>::parseRest() {
795   // Create mapping from version identifiers to Elf_Verdef entries.
796   const Elf_Versym *Versym = nullptr;
797   Verdefs = parseVerdefs(Versym);
798 
799   ArrayRef<Elf_Shdr> Sections = CHECK(this->getObj().sections(), this);
800 
801   // Add symbols to the symbol table.
802   Elf_Sym_Range Syms = this->getGlobalELFSyms();
803   for (const Elf_Sym &Sym : Syms) {
804     unsigned VersymIndex = 0;
805     if (Versym) {
806       VersymIndex = Versym->vs_index;
807       ++Versym;
808     }
809     bool Hidden = VersymIndex & VERSYM_HIDDEN;
810     VersymIndex = VersymIndex & ~VERSYM_HIDDEN;
811 
812     StringRef Name = CHECK(Sym.getName(this->StringTable), this);
813     if (Sym.isUndefined()) {
814       Undefs.push_back(Name);
815       continue;
816     }
817 
818     // Ignore local symbols.
819     if (Versym && VersymIndex == VER_NDX_LOCAL)
820       continue;
821     const Elf_Verdef *Ver = nullptr;
822     if (VersymIndex != VER_NDX_GLOBAL) {
823       if (VersymIndex >= Verdefs.size()) {
824         error("corrupt input file: version definition index " +
825               Twine(VersymIndex) + " for symbol " + Name +
826               " is out of bounds\n>>> defined in " + toString(this));
827         continue;
828       }
829       Ver = Verdefs[VersymIndex];
830     } else {
831       VersymIndex = 0;
832     }
833 
834     // We do not usually care about alignments of data in shared object
835     // files because the loader takes care of it. However, if we promote a
836     // DSO symbol to point to .bss due to copy relocation, we need to keep
837     // the original alignment requirements. We infer it here.
838     uint64_t Alignment = 1;
839     if (Sym.st_value)
840       Alignment = 1ULL << countTrailingZeros((uint64_t)Sym.st_value);
841     if (0 < Sym.st_shndx && Sym.st_shndx < Sections.size()) {
842       uint64_t SecAlign = Sections[Sym.st_shndx].sh_addralign;
843       Alignment = std::min(Alignment, SecAlign);
844     }
845     if (Alignment > UINT32_MAX)
846       error(toString(this) + ": alignment too large: " + Name);
847 
848     if (!Hidden)
849       Symtab->addShared(Name, this, Sym, Alignment, VersymIndex);
850 
851     // Also add the symbol with the versioned name to handle undefined symbols
852     // with explicit versions.
853     if (Ver) {
854       StringRef VerName = this->StringTable.data() + Ver->getAux()->vda_name;
855       Name = Saver.save(Name + "@" + VerName);
856       Symtab->addShared(Name, this, Sym, Alignment, VersymIndex);
857     }
858   }
859 }
860 
861 static ELFKind getBitcodeELFKind(const Triple &T) {
862   if (T.isLittleEndian())
863     return T.isArch64Bit() ? ELF64LEKind : ELF32LEKind;
864   return T.isArch64Bit() ? ELF64BEKind : ELF32BEKind;
865 }
866 
867 static uint8_t getBitcodeMachineKind(StringRef Path, const Triple &T) {
868   switch (T.getArch()) {
869   case Triple::aarch64:
870     return EM_AARCH64;
871   case Triple::arm:
872   case Triple::thumb:
873     return EM_ARM;
874   case Triple::avr:
875     return EM_AVR;
876   case Triple::mips:
877   case Triple::mipsel:
878   case Triple::mips64:
879   case Triple::mips64el:
880     return EM_MIPS;
881   case Triple::ppc:
882     return EM_PPC;
883   case Triple::ppc64:
884     return EM_PPC64;
885   case Triple::x86:
886     return T.isOSIAMCU() ? EM_IAMCU : EM_386;
887   case Triple::x86_64:
888     return EM_X86_64;
889   default:
890     fatal(Path + ": could not infer e_machine from bitcode target triple " +
891           T.str());
892   }
893 }
894 
895 BitcodeFile::BitcodeFile(MemoryBufferRef MB, StringRef ArchiveName,
896                          uint64_t OffsetInArchive)
897     : InputFile(BitcodeKind, MB) {
898   this->ArchiveName = ArchiveName;
899 
900   // Here we pass a new MemoryBufferRef which is identified by ArchiveName
901   // (the fully resolved path of the archive) + member name + offset of the
902   // member in the archive.
903   // ThinLTO uses the MemoryBufferRef identifier to access its internal
904   // data structures and if two archives define two members with the same name,
905   // this causes a collision which result in only one of the objects being
906   // taken into consideration at LTO time (which very likely causes undefined
907   // symbols later in the link stage).
908   MemoryBufferRef MBRef(MB.getBuffer(),
909                         Saver.save(ArchiveName + MB.getBufferIdentifier() +
910                                    utostr(OffsetInArchive)));
911   Obj = CHECK(lto::InputFile::create(MBRef), this);
912 
913   Triple T(Obj->getTargetTriple());
914   EKind = getBitcodeELFKind(T);
915   EMachine = getBitcodeMachineKind(MB.getBufferIdentifier(), T);
916 }
917 
918 static uint8_t mapVisibility(GlobalValue::VisibilityTypes GvVisibility) {
919   switch (GvVisibility) {
920   case GlobalValue::DefaultVisibility:
921     return STV_DEFAULT;
922   case GlobalValue::HiddenVisibility:
923     return STV_HIDDEN;
924   case GlobalValue::ProtectedVisibility:
925     return STV_PROTECTED;
926   }
927   llvm_unreachable("unknown visibility");
928 }
929 
930 template <class ELFT>
931 static Symbol *createBitcodeSymbol(const std::vector<bool> &KeptComdats,
932                                    const lto::InputFile::Symbol &ObjSym,
933                                    BitcodeFile *F) {
934   StringRef NameRef = Saver.save(ObjSym.getName());
935   uint32_t Binding = ObjSym.isWeak() ? STB_WEAK : STB_GLOBAL;
936 
937   uint8_t Type = ObjSym.isTLS() ? STT_TLS : STT_NOTYPE;
938   uint8_t Visibility = mapVisibility(ObjSym.getVisibility());
939   bool CanOmitFromDynSym = ObjSym.canBeOmittedFromSymbolTable();
940 
941   int C = ObjSym.getComdatIndex();
942   if (C != -1 && !KeptComdats[C])
943     return Symtab->addUndefined<ELFT>(NameRef, Binding, Visibility, Type,
944                                       CanOmitFromDynSym, F);
945 
946   if (ObjSym.isUndefined())
947     return Symtab->addUndefined<ELFT>(NameRef, Binding, Visibility, Type,
948                                       CanOmitFromDynSym, F);
949 
950   if (ObjSym.isCommon())
951     return Symtab->addCommon(NameRef, ObjSym.getCommonSize(),
952                              ObjSym.getCommonAlignment(), Binding, Visibility,
953                              STT_OBJECT, F);
954 
955   return Symtab->addBitcode(NameRef, Binding, Visibility, Type,
956                             CanOmitFromDynSym, F);
957 }
958 
959 template <class ELFT>
960 void BitcodeFile::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
961   std::vector<bool> KeptComdats;
962   for (StringRef S : Obj->getComdatTable())
963     KeptComdats.push_back(ComdatGroups.insert(CachedHashStringRef(S)).second);
964 
965   for (const lto::InputFile::Symbol &ObjSym : Obj->symbols())
966     Symbols.push_back(createBitcodeSymbol<ELFT>(KeptComdats, ObjSym, this));
967 }
968 
969 static ELFKind getELFKind(MemoryBufferRef MB) {
970   unsigned char Size;
971   unsigned char Endian;
972   std::tie(Size, Endian) = getElfArchType(MB.getBuffer());
973 
974   if (Endian != ELFDATA2LSB && Endian != ELFDATA2MSB)
975     fatal(MB.getBufferIdentifier() + ": invalid data encoding");
976   if (Size != ELFCLASS32 && Size != ELFCLASS64)
977     fatal(MB.getBufferIdentifier() + ": invalid file class");
978 
979   size_t BufSize = MB.getBuffer().size();
980   if ((Size == ELFCLASS32 && BufSize < sizeof(Elf32_Ehdr)) ||
981       (Size == ELFCLASS64 && BufSize < sizeof(Elf64_Ehdr)))
982     fatal(MB.getBufferIdentifier() + ": file is too short");
983 
984   if (Size == ELFCLASS32)
985     return (Endian == ELFDATA2LSB) ? ELF32LEKind : ELF32BEKind;
986   return (Endian == ELFDATA2LSB) ? ELF64LEKind : ELF64BEKind;
987 }
988 
989 template <class ELFT> void BinaryFile::parse() {
990   ArrayRef<uint8_t> Data = toArrayRef(MB.getBuffer());
991   auto *Section =
992       make<InputSection>(SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, 8, Data, ".data");
993   Sections.push_back(Section);
994 
995   // For each input file foo that is embedded to a result as a binary
996   // blob, we define _binary_foo_{start,end,size} symbols, so that
997   // user programs can access blobs by name. Non-alphanumeric
998   // characters in a filename are replaced with underscore.
999   std::string S = "_binary_" + MB.getBufferIdentifier().str();
1000   for (size_t I = 0; I < S.size(); ++I)
1001     if (!isAlnum(S[I]))
1002       S[I] = '_';
1003 
1004   Symtab->addRegular<ELFT>(Saver.save(S + "_start"), STV_DEFAULT, STT_OBJECT,
1005                            0, 0, STB_GLOBAL, Section, nullptr);
1006   Symtab->addRegular<ELFT>(Saver.save(S + "_end"), STV_DEFAULT, STT_OBJECT,
1007                            Data.size(), 0, STB_GLOBAL, Section, nullptr);
1008   Symtab->addRegular<ELFT>(Saver.save(S + "_size"), STV_DEFAULT, STT_OBJECT,
1009                            Data.size(), 0, STB_GLOBAL, nullptr, nullptr);
1010 }
1011 
1012 static bool isBitcode(MemoryBufferRef MB) {
1013   using namespace sys::fs;
1014   return identify_magic(MB.getBuffer()) == file_magic::bitcode;
1015 }
1016 
1017 InputFile *elf::createObjectFile(MemoryBufferRef MB, StringRef ArchiveName,
1018                                  uint64_t OffsetInArchive) {
1019   if (isBitcode(MB))
1020     return make<BitcodeFile>(MB, ArchiveName, OffsetInArchive);
1021 
1022   switch (getELFKind(MB)) {
1023   case ELF32LEKind:
1024     return make<ObjFile<ELF32LE>>(MB, ArchiveName);
1025   case ELF32BEKind:
1026     return make<ObjFile<ELF32BE>>(MB, ArchiveName);
1027   case ELF64LEKind:
1028     return make<ObjFile<ELF64LE>>(MB, ArchiveName);
1029   case ELF64BEKind:
1030     return make<ObjFile<ELF64BE>>(MB, ArchiveName);
1031   default:
1032     llvm_unreachable("getELFKind");
1033   }
1034 }
1035 
1036 InputFile *elf::createSharedFile(MemoryBufferRef MB, StringRef DefaultSoName) {
1037   switch (getELFKind(MB)) {
1038   case ELF32LEKind:
1039     return make<SharedFile<ELF32LE>>(MB, DefaultSoName);
1040   case ELF32BEKind:
1041     return make<SharedFile<ELF32BE>>(MB, DefaultSoName);
1042   case ELF64LEKind:
1043     return make<SharedFile<ELF64LE>>(MB, DefaultSoName);
1044   case ELF64BEKind:
1045     return make<SharedFile<ELF64BE>>(MB, DefaultSoName);
1046   default:
1047     llvm_unreachable("getELFKind");
1048   }
1049 }
1050 
1051 MemoryBufferRef LazyObjFile::getBuffer() {
1052   if (Seen)
1053     return MemoryBufferRef();
1054   Seen = true;
1055   return MB;
1056 }
1057 
1058 InputFile *LazyObjFile::fetch() {
1059   MemoryBufferRef MBRef = getBuffer();
1060   if (MBRef.getBuffer().empty())
1061     return nullptr;
1062   return createObjectFile(MBRef, ArchiveName, OffsetInArchive);
1063 }
1064 
1065 template <class ELFT> void LazyObjFile::parse() {
1066   for (StringRef Sym : getSymbolNames())
1067     Symtab->addLazyObject<ELFT>(Sym, *this);
1068 }
1069 
1070 template <class ELFT> std::vector<StringRef> LazyObjFile::getElfSymbols() {
1071   typedef typename ELFT::Shdr Elf_Shdr;
1072   typedef typename ELFT::Sym Elf_Sym;
1073   typedef typename ELFT::SymRange Elf_Sym_Range;
1074 
1075   ELFFile<ELFT> Obj = check(ELFFile<ELFT>::create(this->MB.getBuffer()));
1076   ArrayRef<Elf_Shdr> Sections = CHECK(Obj.sections(), this);
1077   for (const Elf_Shdr &Sec : Sections) {
1078     if (Sec.sh_type != SHT_SYMTAB)
1079       continue;
1080 
1081     Elf_Sym_Range Syms = CHECK(Obj.symbols(&Sec), this);
1082     uint32_t FirstNonLocal = Sec.sh_info;
1083     StringRef StringTable =
1084         CHECK(Obj.getStringTableForSymtab(Sec, Sections), this);
1085     std::vector<StringRef> V;
1086 
1087     for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal))
1088       if (Sym.st_shndx != SHN_UNDEF)
1089         V.push_back(CHECK(Sym.getName(StringTable), this));
1090     return V;
1091   }
1092   return {};
1093 }
1094 
1095 std::vector<StringRef> LazyObjFile::getBitcodeSymbols() {
1096   std::unique_ptr<lto::InputFile> Obj =
1097       CHECK(lto::InputFile::create(this->MB), this);
1098   std::vector<StringRef> V;
1099   for (const lto::InputFile::Symbol &Sym : Obj->symbols())
1100     if (!Sym.isUndefined())
1101       V.push_back(Saver.save(Sym.getName()));
1102   return V;
1103 }
1104 
1105 // Returns a vector of globally-visible defined symbol names.
1106 std::vector<StringRef> LazyObjFile::getSymbolNames() {
1107   if (isBitcode(this->MB))
1108     return getBitcodeSymbols();
1109 
1110   switch (getELFKind(this->MB)) {
1111   case ELF32LEKind:
1112     return getElfSymbols<ELF32LE>();
1113   case ELF32BEKind:
1114     return getElfSymbols<ELF32BE>();
1115   case ELF64LEKind:
1116     return getElfSymbols<ELF64LE>();
1117   case ELF64BEKind:
1118     return getElfSymbols<ELF64BE>();
1119   default:
1120     llvm_unreachable("getELFKind");
1121   }
1122 }
1123 
1124 template void ArchiveFile::parse<ELF32LE>();
1125 template void ArchiveFile::parse<ELF32BE>();
1126 template void ArchiveFile::parse<ELF64LE>();
1127 template void ArchiveFile::parse<ELF64BE>();
1128 
1129 template void BitcodeFile::parse<ELF32LE>(DenseSet<CachedHashStringRef> &);
1130 template void BitcodeFile::parse<ELF32BE>(DenseSet<CachedHashStringRef> &);
1131 template void BitcodeFile::parse<ELF64LE>(DenseSet<CachedHashStringRef> &);
1132 template void BitcodeFile::parse<ELF64BE>(DenseSet<CachedHashStringRef> &);
1133 
1134 template void LazyObjFile::parse<ELF32LE>();
1135 template void LazyObjFile::parse<ELF32BE>();
1136 template void LazyObjFile::parse<ELF64LE>();
1137 template void LazyObjFile::parse<ELF64BE>();
1138 
1139 template class elf::ELFFileBase<ELF32LE>;
1140 template class elf::ELFFileBase<ELF32BE>;
1141 template class elf::ELFFileBase<ELF64LE>;
1142 template class elf::ELFFileBase<ELF64BE>;
1143 
1144 template class elf::ObjFile<ELF32LE>;
1145 template class elf::ObjFile<ELF32BE>;
1146 template class elf::ObjFile<ELF64LE>;
1147 template class elf::ObjFile<ELF64BE>;
1148 
1149 template class elf::SharedFile<ELF32LE>;
1150 template class elf::SharedFile<ELF32BE>;
1151 template class elf::SharedFile<ELF64LE>;
1152 template class elf::SharedFile<ELF64BE>;
1153 
1154 template void BinaryFile::parse<ELF32LE>();
1155 template void BinaryFile::parse<ELF32BE>();
1156 template void BinaryFile::parse<ELF64LE>();
1157 template void BinaryFile::parse<ELF64BE>();
1158