1 //===- InputFiles.cpp -----------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "InputFiles.h"
10 #include "InputSection.h"
11 #include "LinkerScript.h"
12 #include "SymbolTable.h"
13 #include "Symbols.h"
14 #include "SyntheticSections.h"
15 #include "lld/Common/ErrorHandler.h"
16 #include "lld/Common/Memory.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/CodeGen/Analysis.h"
19 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
20 #include "llvm/IR/LLVMContext.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/LTO/LTO.h"
23 #include "llvm/MC/StringTableBuilder.h"
24 #include "llvm/Object/ELFObjectFile.h"
25 #include "llvm/Support/ARMAttributeParser.h"
26 #include "llvm/Support/ARMBuildAttributes.h"
27 #include "llvm/Support/Path.h"
28 #include "llvm/Support/TarWriter.h"
29 #include "llvm/Support/raw_ostream.h"
30 
31 using namespace llvm;
32 using namespace llvm::ELF;
33 using namespace llvm::object;
34 using namespace llvm::sys;
35 using namespace llvm::sys::fs;
36 
37 using namespace lld;
38 using namespace lld::elf;
39 
40 bool InputFile::IsInGroup;
41 uint32_t InputFile::NextGroupId;
42 std::vector<BinaryFile *> elf::BinaryFiles;
43 std::vector<BitcodeFile *> elf::BitcodeFiles;
44 std::vector<LazyObjFile *> elf::LazyObjFiles;
45 std::vector<InputFile *> elf::ObjectFiles;
46 std::vector<SharedFile *> elf::SharedFiles;
47 
48 std::unique_ptr<TarWriter> elf::Tar;
49 
50 InputFile::InputFile(Kind K, MemoryBufferRef M)
51     : MB(M), GroupId(NextGroupId), FileKind(K) {
52   // All files within the same --{start,end}-group get the same group ID.
53   // Otherwise, a new file will get a new group ID.
54   if (!IsInGroup)
55     ++NextGroupId;
56 }
57 
58 Optional<MemoryBufferRef> elf::readFile(StringRef Path) {
59   // The --chroot option changes our virtual root directory.
60   // This is useful when you are dealing with files created by --reproduce.
61   if (!Config->Chroot.empty() && Path.startswith("/"))
62     Path = Saver.save(Config->Chroot + Path);
63 
64   log(Path);
65 
66   auto MBOrErr = MemoryBuffer::getFile(Path, -1, false);
67   if (auto EC = MBOrErr.getError()) {
68     error("cannot open " + Path + ": " + EC.message());
69     return None;
70   }
71 
72   std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
73   MemoryBufferRef MBRef = MB->getMemBufferRef();
74   make<std::unique_ptr<MemoryBuffer>>(std::move(MB)); // take MB ownership
75 
76   if (Tar)
77     Tar->append(relativeToRoot(Path), MBRef.getBuffer());
78   return MBRef;
79 }
80 
81 // All input object files must be for the same architecture
82 // (e.g. it does not make sense to link x86 object files with
83 // MIPS object files.) This function checks for that error.
84 static bool isCompatible(InputFile *File) {
85   if (!File->isElf() && !isa<BitcodeFile>(File))
86     return true;
87 
88   if (File->EKind == Config->EKind && File->EMachine == Config->EMachine) {
89     if (Config->EMachine != EM_MIPS)
90       return true;
91     if (isMipsN32Abi(File) == Config->MipsN32Abi)
92       return true;
93   }
94 
95   if (!Config->Emulation.empty()) {
96     error(toString(File) + " is incompatible with " + Config->Emulation);
97   } else {
98     InputFile *Existing;
99     if (!ObjectFiles.empty())
100       Existing = ObjectFiles[0];
101     else if (!SharedFiles.empty())
102       Existing = SharedFiles[0];
103     else
104       Existing = BitcodeFiles[0];
105 
106     error(toString(File) + " is incompatible with " + toString(Existing));
107   }
108 
109   return false;
110 }
111 
112 // Add symbols in File to the symbol table.
113 template <class ELFT> void elf::parseFile(InputFile *File) {
114   // Comdat groups define "link once" sections. If two comdat groups have the
115   // same name, only one of them is linked, and the other is ignored. This set
116   // is used to uniquify them.
117   static llvm::DenseSet<llvm::CachedHashStringRef> ComdatGroups;
118 
119   if (!isCompatible(File))
120     return;
121 
122   // Binary file
123   if (auto *F = dyn_cast<BinaryFile>(File)) {
124     BinaryFiles.push_back(F);
125     F->parse();
126     return;
127   }
128 
129   // .a file
130   if (auto *F = dyn_cast<ArchiveFile>(File)) {
131     F->parse<ELFT>();
132     return;
133   }
134 
135   // Lazy object file
136   if (auto *F = dyn_cast<LazyObjFile>(File)) {
137     LazyObjFiles.push_back(F);
138     F->parse<ELFT>();
139     return;
140   }
141 
142   if (Config->Trace)
143     message(toString(File));
144 
145   // .so file
146   if (auto *F = dyn_cast<SharedFile>(File)) {
147     F->parse<ELFT>();
148     return;
149   }
150 
151   // LLVM bitcode file
152   if (auto *F = dyn_cast<BitcodeFile>(File)) {
153     BitcodeFiles.push_back(F);
154     F->parse<ELFT>(ComdatGroups);
155     return;
156   }
157 
158   // Regular object file
159   ObjectFiles.push_back(File);
160   cast<ObjFile<ELFT>>(File)->parse(ComdatGroups);
161 }
162 
163 // Concatenates arguments to construct a string representing an error location.
164 static std::string createFileLineMsg(StringRef Path, unsigned Line) {
165   std::string Filename = path::filename(Path);
166   std::string Lineno = ":" + std::to_string(Line);
167   if (Filename == Path)
168     return Filename + Lineno;
169   return Filename + Lineno + " (" + Path.str() + Lineno + ")";
170 }
171 
172 template <class ELFT>
173 static std::string getSrcMsgAux(ObjFile<ELFT> &File, const Symbol &Sym,
174                                 InputSectionBase &Sec, uint64_t Offset) {
175   // In DWARF, functions and variables are stored to different places.
176   // First, lookup a function for a given offset.
177   if (Optional<DILineInfo> Info = File.getDILineInfo(&Sec, Offset))
178     return createFileLineMsg(Info->FileName, Info->Line);
179 
180   // If it failed, lookup again as a variable.
181   if (Optional<std::pair<std::string, unsigned>> FileLine =
182           File.getVariableLoc(Sym.getName()))
183     return createFileLineMsg(FileLine->first, FileLine->second);
184 
185   // File.SourceFile contains STT_FILE symbol, and that is a last resort.
186   return File.SourceFile;
187 }
188 
189 std::string InputFile::getSrcMsg(const Symbol &Sym, InputSectionBase &Sec,
190                                  uint64_t Offset) {
191   if (kind() != ObjKind)
192     return "";
193   switch (Config->EKind) {
194   default:
195     llvm_unreachable("Invalid kind");
196   case ELF32LEKind:
197     return getSrcMsgAux(cast<ObjFile<ELF32LE>>(*this), Sym, Sec, Offset);
198   case ELF32BEKind:
199     return getSrcMsgAux(cast<ObjFile<ELF32BE>>(*this), Sym, Sec, Offset);
200   case ELF64LEKind:
201     return getSrcMsgAux(cast<ObjFile<ELF64LE>>(*this), Sym, Sec, Offset);
202   case ELF64BEKind:
203     return getSrcMsgAux(cast<ObjFile<ELF64BE>>(*this), Sym, Sec, Offset);
204   }
205 }
206 
207 template <class ELFT> void ObjFile<ELFT>::initializeDwarf() {
208   Dwarf = llvm::make_unique<DWARFContext>(make_unique<LLDDwarfObj<ELFT>>(this));
209   for (std::unique_ptr<DWARFUnit> &CU : Dwarf->compile_units()) {
210     auto Report = [](Error Err) {
211       handleAllErrors(std::move(Err),
212                       [](ErrorInfoBase &Info) { warn(Info.message()); });
213     };
214     Expected<const DWARFDebugLine::LineTable *> ExpectedLT =
215         Dwarf->getLineTableForUnit(CU.get(), Report);
216     const DWARFDebugLine::LineTable *LT = nullptr;
217     if (ExpectedLT)
218       LT = *ExpectedLT;
219     else
220       Report(ExpectedLT.takeError());
221     if (!LT)
222       continue;
223     LineTables.push_back(LT);
224 
225     // Loop over variable records and insert them to VariableLoc.
226     for (const auto &Entry : CU->dies()) {
227       DWARFDie Die(CU.get(), &Entry);
228       // Skip all tags that are not variables.
229       if (Die.getTag() != dwarf::DW_TAG_variable)
230         continue;
231 
232       // Skip if a local variable because we don't need them for generating
233       // error messages. In general, only non-local symbols can fail to be
234       // linked.
235       if (!dwarf::toUnsigned(Die.find(dwarf::DW_AT_external), 0))
236         continue;
237 
238       // Get the source filename index for the variable.
239       unsigned File = dwarf::toUnsigned(Die.find(dwarf::DW_AT_decl_file), 0);
240       if (!LT->hasFileAtIndex(File))
241         continue;
242 
243       // Get the line number on which the variable is declared.
244       unsigned Line = dwarf::toUnsigned(Die.find(dwarf::DW_AT_decl_line), 0);
245 
246       // Here we want to take the variable name to add it into VariableLoc.
247       // Variable can have regular and linkage name associated. At first, we try
248       // to get linkage name as it can be different, for example when we have
249       // two variables in different namespaces of the same object. Use common
250       // name otherwise, but handle the case when it also absent in case if the
251       // input object file lacks some debug info.
252       StringRef Name =
253           dwarf::toString(Die.find(dwarf::DW_AT_linkage_name),
254                           dwarf::toString(Die.find(dwarf::DW_AT_name), ""));
255       if (!Name.empty())
256         VariableLoc.insert({Name, {LT, File, Line}});
257     }
258   }
259 }
260 
261 // Returns the pair of file name and line number describing location of data
262 // object (variable, array, etc) definition.
263 template <class ELFT>
264 Optional<std::pair<std::string, unsigned>>
265 ObjFile<ELFT>::getVariableLoc(StringRef Name) {
266   llvm::call_once(InitDwarfLine, [this]() { initializeDwarf(); });
267 
268   // Return if we have no debug information about data object.
269   auto It = VariableLoc.find(Name);
270   if (It == VariableLoc.end())
271     return None;
272 
273   // Take file name string from line table.
274   std::string FileName;
275   if (!It->second.LT->getFileNameByIndex(
276           It->second.File, nullptr,
277           DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, FileName))
278     return None;
279 
280   return std::make_pair(FileName, It->second.Line);
281 }
282 
283 // Returns source line information for a given offset
284 // using DWARF debug info.
285 template <class ELFT>
286 Optional<DILineInfo> ObjFile<ELFT>::getDILineInfo(InputSectionBase *S,
287                                                   uint64_t Offset) {
288   llvm::call_once(InitDwarfLine, [this]() { initializeDwarf(); });
289 
290   // Detect SectionIndex for specified section.
291   uint64_t SectionIndex = object::SectionedAddress::UndefSection;
292   ArrayRef<InputSectionBase *> Sections = S->File->getSections();
293   for (uint64_t CurIndex = 0; CurIndex < Sections.size(); ++CurIndex) {
294     if (S == Sections[CurIndex]) {
295       SectionIndex = CurIndex;
296       break;
297     }
298   }
299 
300   // Use fake address calcuated by adding section file offset and offset in
301   // section. See comments for ObjectInfo class.
302   DILineInfo Info;
303   for (const llvm::DWARFDebugLine::LineTable *LT : LineTables) {
304     if (LT->getFileLineInfoForAddress(
305             {S->getOffsetInFile() + Offset, SectionIndex}, nullptr,
306             DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, Info))
307       return Info;
308   }
309   return None;
310 }
311 
312 // Returns "<internal>", "foo.a(bar.o)" or "baz.o".
313 std::string lld::toString(const InputFile *F) {
314   if (!F)
315     return "<internal>";
316 
317   if (F->ToStringCache.empty()) {
318     if (F->ArchiveName.empty())
319       F->ToStringCache = F->getName();
320     else
321       F->ToStringCache = (F->ArchiveName + "(" + F->getName() + ")").str();
322   }
323   return F->ToStringCache;
324 }
325 
326 ELFFileBase::ELFFileBase(Kind K, MemoryBufferRef MB) : InputFile(K, MB) {}
327 
328 template <class ELFT> void ELFFileBase::parseHeader() {
329   if (ELFT::TargetEndianness == support::little)
330     EKind = ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind;
331   else
332     EKind = ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind;
333 
334   EMachine = getObj<ELFT>().getHeader()->e_machine;
335   OSABI = getObj<ELFT>().getHeader()->e_ident[llvm::ELF::EI_OSABI];
336   ABIVersion = getObj<ELFT>().getHeader()->e_ident[llvm::ELF::EI_ABIVERSION];
337 }
338 
339 template <class ELFT>
340 void ELFFileBase::initSymtab(ArrayRef<typename ELFT::Shdr> Sections,
341                              const typename ELFT::Shdr *Symtab) {
342   FirstGlobal = Symtab->sh_info;
343   ArrayRef<typename ELFT::Sym> ELFSyms =
344       CHECK(getObj<ELFT>().symbols(Symtab), this);
345   if (FirstGlobal == 0 || FirstGlobal > ELFSyms.size())
346     fatal(toString(this) + ": invalid sh_info in symbol table");
347   this->ELFSyms = reinterpret_cast<const void *>(ELFSyms.data());
348   this->NumELFSyms = ELFSyms.size();
349 
350   StringTable =
351       CHECK(getObj<ELFT>().getStringTableForSymtab(*Symtab, Sections), this);
352 }
353 
354 template <class ELFT>
355 ObjFile<ELFT>::ObjFile(MemoryBufferRef M, StringRef ArchiveName)
356     : ELFFileBase(ObjKind, M) {
357   parseHeader<ELFT>();
358   this->ArchiveName = ArchiveName;
359 }
360 
361 template <class ELFT>
362 uint32_t ObjFile<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
363   return CHECK(
364       this->getObj().getSectionIndex(&Sym, getELFSyms<ELFT>(), ShndxTable),
365       this);
366 }
367 
368 template <class ELFT> ArrayRef<Symbol *> ObjFile<ELFT>::getLocalSymbols() {
369   if (this->Symbols.empty())
370     return {};
371   return makeArrayRef(this->Symbols).slice(1, this->FirstGlobal - 1);
372 }
373 
374 template <class ELFT> ArrayRef<Symbol *> ObjFile<ELFT>::getGlobalSymbols() {
375   return makeArrayRef(this->Symbols).slice(this->FirstGlobal);
376 }
377 
378 template <class ELFT>
379 void ObjFile<ELFT>::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
380   // Read a section table. JustSymbols is usually false.
381   if (this->JustSymbols)
382     initializeJustSymbols();
383   else
384     initializeSections(ComdatGroups);
385 
386   // Read a symbol table.
387   initializeSymbols();
388 }
389 
390 // Sections with SHT_GROUP and comdat bits define comdat section groups.
391 // They are identified and deduplicated by group name. This function
392 // returns a group name.
393 template <class ELFT>
394 StringRef ObjFile<ELFT>::getShtGroupSignature(ArrayRef<Elf_Shdr> Sections,
395                                               const Elf_Shdr &Sec) {
396   // Group signatures are stored as symbol names in object files.
397   // sh_info contains a symbol index, so we fetch a symbol and read its name.
398   if (this->getELFSyms<ELFT>().empty())
399     this->initSymtab<ELFT>(
400         Sections, CHECK(object::getSection<ELFT>(Sections, Sec.sh_link), this));
401 
402   const Elf_Sym *Sym =
403       CHECK(object::getSymbol<ELFT>(this->getELFSyms<ELFT>(), Sec.sh_info), this);
404   StringRef Signature = CHECK(Sym->getName(this->StringTable), this);
405 
406   // As a special case, if a symbol is a section symbol and has no name,
407   // we use a section name as a signature.
408   //
409   // Such SHT_GROUP sections are invalid from the perspective of the ELF
410   // standard, but GNU gold 1.14 (the newest version as of July 2017) or
411   // older produce such sections as outputs for the -r option, so we need
412   // a bug-compatibility.
413   if (Signature.empty() && Sym->getType() == STT_SECTION)
414     return getSectionName(Sec);
415   return Signature;
416 }
417 
418 template <class ELFT> bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) {
419   // On a regular link we don't merge sections if -O0 (default is -O1). This
420   // sometimes makes the linker significantly faster, although the output will
421   // be bigger.
422   //
423   // Doing the same for -r would create a problem as it would combine sections
424   // with different sh_entsize. One option would be to just copy every SHF_MERGE
425   // section as is to the output. While this would produce a valid ELF file with
426   // usable SHF_MERGE sections, tools like (llvm-)?dwarfdump get confused when
427   // they see two .debug_str. We could have separate logic for combining
428   // SHF_MERGE sections based both on their name and sh_entsize, but that seems
429   // to be more trouble than it is worth. Instead, we just use the regular (-O1)
430   // logic for -r.
431   if (Config->Optimize == 0 && !Config->Relocatable)
432     return false;
433 
434   // A mergeable section with size 0 is useless because they don't have
435   // any data to merge. A mergeable string section with size 0 can be
436   // argued as invalid because it doesn't end with a null character.
437   // We'll avoid a mess by handling them as if they were non-mergeable.
438   if (Sec.sh_size == 0)
439     return false;
440 
441   // Check for sh_entsize. The ELF spec is not clear about the zero
442   // sh_entsize. It says that "the member [sh_entsize] contains 0 if
443   // the section does not hold a table of fixed-size entries". We know
444   // that Rust 1.13 produces a string mergeable section with a zero
445   // sh_entsize. Here we just accept it rather than being picky about it.
446   uint64_t EntSize = Sec.sh_entsize;
447   if (EntSize == 0)
448     return false;
449   if (Sec.sh_size % EntSize)
450     fatal(toString(this) +
451           ": SHF_MERGE section size must be a multiple of sh_entsize");
452 
453   uint64_t Flags = Sec.sh_flags;
454   if (!(Flags & SHF_MERGE))
455     return false;
456   if (Flags & SHF_WRITE)
457     fatal(toString(this) + ": writable SHF_MERGE section is not supported");
458 
459   return true;
460 }
461 
462 // This is for --just-symbols.
463 //
464 // --just-symbols is a very minor feature that allows you to link your
465 // output against other existing program, so that if you load both your
466 // program and the other program into memory, your output can refer the
467 // other program's symbols.
468 //
469 // When the option is given, we link "just symbols". The section table is
470 // initialized with null pointers.
471 template <class ELFT> void ObjFile<ELFT>::initializeJustSymbols() {
472   ArrayRef<Elf_Shdr> ObjSections = CHECK(this->getObj().sections(), this);
473   this->Sections.resize(ObjSections.size());
474 
475   for (const Elf_Shdr &Sec : ObjSections) {
476     if (Sec.sh_type != SHT_SYMTAB)
477       continue;
478     this->initSymtab<ELFT>(ObjSections, &Sec);
479     return;
480   }
481 }
482 
483 template <class ELFT>
484 void ObjFile<ELFT>::initializeSections(
485     DenseSet<CachedHashStringRef> &ComdatGroups) {
486   const ELFFile<ELFT> &Obj = this->getObj();
487 
488   ArrayRef<Elf_Shdr> ObjSections = CHECK(Obj.sections(), this);
489   uint64_t Size = ObjSections.size();
490   this->Sections.resize(Size);
491   this->SectionStringTable =
492       CHECK(Obj.getSectionStringTable(ObjSections), this);
493 
494   for (size_t I = 0, E = ObjSections.size(); I < E; I++) {
495     if (this->Sections[I] == &InputSection::Discarded)
496       continue;
497     const Elf_Shdr &Sec = ObjSections[I];
498 
499     if (Sec.sh_type == ELF::SHT_LLVM_CALL_GRAPH_PROFILE)
500       CGProfile =
501           check(Obj.template getSectionContentsAsArray<Elf_CGProfile>(&Sec));
502 
503     // SHF_EXCLUDE'ed sections are discarded by the linker. However,
504     // if -r is given, we'll let the final link discard such sections.
505     // This is compatible with GNU.
506     if ((Sec.sh_flags & SHF_EXCLUDE) && !Config->Relocatable) {
507       if (Sec.sh_type == SHT_LLVM_ADDRSIG) {
508         // We ignore the address-significance table if we know that the object
509         // file was created by objcopy or ld -r. This is because these tools
510         // will reorder the symbols in the symbol table, invalidating the data
511         // in the address-significance table, which refers to symbols by index.
512         if (Sec.sh_link != 0)
513           this->AddrsigSec = &Sec;
514         else if (Config->ICF == ICFLevel::Safe)
515           warn(toString(this) + ": --icf=safe is incompatible with object "
516                                 "files created using objcopy or ld -r");
517       }
518       this->Sections[I] = &InputSection::Discarded;
519       continue;
520     }
521 
522     switch (Sec.sh_type) {
523     case SHT_GROUP: {
524       // De-duplicate section groups by their signatures.
525       StringRef Signature = getShtGroupSignature(ObjSections, Sec);
526       this->Sections[I] = &InputSection::Discarded;
527 
528 
529       ArrayRef<Elf_Word> Entries =
530           CHECK(Obj.template getSectionContentsAsArray<Elf_Word>(&Sec), this);
531       if (Entries.empty())
532         fatal(toString(this) + ": empty SHT_GROUP");
533 
534       // The first word of a SHT_GROUP section contains flags. Currently,
535       // the standard defines only "GRP_COMDAT" flag for the COMDAT group.
536       // An group with the empty flag doesn't define anything; such sections
537       // are just skipped.
538       if (Entries[0] == 0)
539         continue;
540 
541       if (Entries[0] != GRP_COMDAT)
542         fatal(toString(this) + ": unsupported SHT_GROUP format");
543 
544       bool IsNew = ComdatGroups.insert(CachedHashStringRef(Signature)).second;
545       if (IsNew) {
546         if (Config->Relocatable)
547           this->Sections[I] = createInputSection(Sec);
548         continue;
549       }
550 
551       // Otherwise, discard group members.
552       for (uint32_t SecIndex : Entries.slice(1)) {
553         if (SecIndex >= Size)
554           fatal(toString(this) +
555                 ": invalid section index in group: " + Twine(SecIndex));
556         this->Sections[SecIndex] = &InputSection::Discarded;
557       }
558       break;
559     }
560     case SHT_SYMTAB:
561       this->initSymtab<ELFT>(ObjSections, &Sec);
562       break;
563     case SHT_SYMTAB_SHNDX:
564       ShndxTable = CHECK(Obj.getSHNDXTable(Sec, ObjSections), this);
565       break;
566     case SHT_STRTAB:
567     case SHT_NULL:
568       break;
569     default:
570       this->Sections[I] = createInputSection(Sec);
571     }
572 
573     // .ARM.exidx sections have a reverse dependency on the InputSection they
574     // have a SHF_LINK_ORDER dependency, this is identified by the sh_link.
575     if (Sec.sh_flags & SHF_LINK_ORDER) {
576       InputSectionBase *LinkSec = nullptr;
577       if (Sec.sh_link < this->Sections.size())
578         LinkSec = this->Sections[Sec.sh_link];
579       if (!LinkSec)
580         fatal(toString(this) +
581               ": invalid sh_link index: " + Twine(Sec.sh_link));
582 
583       InputSection *IS = cast<InputSection>(this->Sections[I]);
584       LinkSec->DependentSections.push_back(IS);
585       if (!isa<InputSection>(LinkSec))
586         error("a section " + IS->Name +
587               " with SHF_LINK_ORDER should not refer a non-regular "
588               "section: " +
589               toString(LinkSec));
590     }
591   }
592 }
593 
594 // For ARM only, to set the EF_ARM_ABI_FLOAT_SOFT or EF_ARM_ABI_FLOAT_HARD
595 // flag in the ELF Header we need to look at Tag_ABI_VFP_args to find out how
596 // the input objects have been compiled.
597 static void updateARMVFPArgs(const ARMAttributeParser &Attributes,
598                              const InputFile *F) {
599   if (!Attributes.hasAttribute(ARMBuildAttrs::ABI_VFP_args))
600     // If an ABI tag isn't present then it is implicitly given the value of 0
601     // which maps to ARMBuildAttrs::BaseAAPCS. However many assembler files,
602     // including some in glibc that don't use FP args (and should have value 3)
603     // don't have the attribute so we do not consider an implicit value of 0
604     // as a clash.
605     return;
606 
607   unsigned VFPArgs = Attributes.getAttributeValue(ARMBuildAttrs::ABI_VFP_args);
608   ARMVFPArgKind Arg;
609   switch (VFPArgs) {
610   case ARMBuildAttrs::BaseAAPCS:
611     Arg = ARMVFPArgKind::Base;
612     break;
613   case ARMBuildAttrs::HardFPAAPCS:
614     Arg = ARMVFPArgKind::VFP;
615     break;
616   case ARMBuildAttrs::ToolChainFPPCS:
617     // Tool chain specific convention that conforms to neither AAPCS variant.
618     Arg = ARMVFPArgKind::ToolChain;
619     break;
620   case ARMBuildAttrs::CompatibleFPAAPCS:
621     // Object compatible with all conventions.
622     return;
623   default:
624     error(toString(F) + ": unknown Tag_ABI_VFP_args value: " + Twine(VFPArgs));
625     return;
626   }
627   // Follow ld.bfd and error if there is a mix of calling conventions.
628   if (Config->ARMVFPArgs != Arg && Config->ARMVFPArgs != ARMVFPArgKind::Default)
629     error(toString(F) + ": incompatible Tag_ABI_VFP_args");
630   else
631     Config->ARMVFPArgs = Arg;
632 }
633 
634 // The ARM support in lld makes some use of instructions that are not available
635 // on all ARM architectures. Namely:
636 // - Use of BLX instruction for interworking between ARM and Thumb state.
637 // - Use of the extended Thumb branch encoding in relocation.
638 // - Use of the MOVT/MOVW instructions in Thumb Thunks.
639 // The ARM Attributes section contains information about the architecture chosen
640 // at compile time. We follow the convention that if at least one input object
641 // is compiled with an architecture that supports these features then lld is
642 // permitted to use them.
643 static void updateSupportedARMFeatures(const ARMAttributeParser &Attributes) {
644   if (!Attributes.hasAttribute(ARMBuildAttrs::CPU_arch))
645     return;
646   auto Arch = Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch);
647   switch (Arch) {
648   case ARMBuildAttrs::Pre_v4:
649   case ARMBuildAttrs::v4:
650   case ARMBuildAttrs::v4T:
651     // Architectures prior to v5 do not support BLX instruction
652     break;
653   case ARMBuildAttrs::v5T:
654   case ARMBuildAttrs::v5TE:
655   case ARMBuildAttrs::v5TEJ:
656   case ARMBuildAttrs::v6:
657   case ARMBuildAttrs::v6KZ:
658   case ARMBuildAttrs::v6K:
659     Config->ARMHasBlx = true;
660     // Architectures used in pre-Cortex processors do not support
661     // The J1 = 1 J2 = 1 Thumb branch range extension, with the exception
662     // of Architecture v6T2 (arm1156t2-s and arm1156t2f-s) that do.
663     break;
664   default:
665     // All other Architectures have BLX and extended branch encoding
666     Config->ARMHasBlx = true;
667     Config->ARMJ1J2BranchEncoding = true;
668     if (Arch != ARMBuildAttrs::v6_M && Arch != ARMBuildAttrs::v6S_M)
669       // All Architectures used in Cortex processors with the exception
670       // of v6-M and v6S-M have the MOVT and MOVW instructions.
671       Config->ARMHasMovtMovw = true;
672     break;
673   }
674 }
675 
676 template <class ELFT>
677 InputSectionBase *ObjFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
678   uint32_t Idx = Sec.sh_info;
679   if (Idx >= this->Sections.size())
680     fatal(toString(this) + ": invalid relocated section index: " + Twine(Idx));
681   InputSectionBase *Target = this->Sections[Idx];
682 
683   // Strictly speaking, a relocation section must be included in the
684   // group of the section it relocates. However, LLVM 3.3 and earlier
685   // would fail to do so, so we gracefully handle that case.
686   if (Target == &InputSection::Discarded)
687     return nullptr;
688 
689   if (!Target)
690     fatal(toString(this) + ": unsupported relocation reference");
691   return Target;
692 }
693 
694 // Create a regular InputSection class that has the same contents
695 // as a given section.
696 static InputSection *toRegularSection(MergeInputSection *Sec) {
697   return make<InputSection>(Sec->File, Sec->Flags, Sec->Type, Sec->Alignment,
698                             Sec->data(), Sec->Name);
699 }
700 
701 template <class ELFT>
702 InputSectionBase *ObjFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
703   StringRef Name = getSectionName(Sec);
704 
705   switch (Sec.sh_type) {
706   case SHT_ARM_ATTRIBUTES: {
707     if (Config->EMachine != EM_ARM)
708       break;
709     ARMAttributeParser Attributes;
710     ArrayRef<uint8_t> Contents = check(this->getObj().getSectionContents(&Sec));
711     Attributes.Parse(Contents, /*isLittle*/ Config->EKind == ELF32LEKind);
712     updateSupportedARMFeatures(Attributes);
713     updateARMVFPArgs(Attributes, this);
714 
715     // FIXME: Retain the first attribute section we see. The eglibc ARM
716     // dynamic loaders require the presence of an attribute section for dlopen
717     // to work. In a full implementation we would merge all attribute sections.
718     if (In.ARMAttributes == nullptr) {
719       In.ARMAttributes = make<InputSection>(*this, Sec, Name);
720       return In.ARMAttributes;
721     }
722     return &InputSection::Discarded;
723   }
724   case SHT_RELA:
725   case SHT_REL: {
726     // Find a relocation target section and associate this section with that.
727     // Target may have been discarded if it is in a different section group
728     // and the group is discarded, even though it's a violation of the
729     // spec. We handle that situation gracefully by discarding dangling
730     // relocation sections.
731     InputSectionBase *Target = getRelocTarget(Sec);
732     if (!Target)
733       return nullptr;
734 
735     // This section contains relocation information.
736     // If -r is given, we do not interpret or apply relocation
737     // but just copy relocation sections to output.
738     if (Config->Relocatable) {
739       InputSection *RelocSec = make<InputSection>(*this, Sec, Name);
740       // We want to add a dependency to target, similar like we do for
741       // -emit-relocs below. This is useful for the case when linker script
742       // contains the "/DISCARD/". It is perhaps uncommon to use a script with
743       // -r, but we faced it in the Linux kernel and have to handle such case
744       // and not to crash.
745       Target->DependentSections.push_back(RelocSec);
746       return RelocSec;
747     }
748 
749     if (Target->FirstRelocation)
750       fatal(toString(this) +
751             ": multiple relocation sections to one section are not supported");
752 
753     // ELF spec allows mergeable sections with relocations, but they are
754     // rare, and it is in practice hard to merge such sections by contents,
755     // because applying relocations at end of linking changes section
756     // contents. So, we simply handle such sections as non-mergeable ones.
757     // Degrading like this is acceptable because section merging is optional.
758     if (auto *MS = dyn_cast<MergeInputSection>(Target)) {
759       Target = toRegularSection(MS);
760       this->Sections[Sec.sh_info] = Target;
761     }
762 
763     if (Sec.sh_type == SHT_RELA) {
764       ArrayRef<Elf_Rela> Rels = CHECK(getObj().relas(&Sec), this);
765       Target->FirstRelocation = Rels.begin();
766       Target->NumRelocations = Rels.size();
767       Target->AreRelocsRela = true;
768     } else {
769       ArrayRef<Elf_Rel> Rels = CHECK(getObj().rels(&Sec), this);
770       Target->FirstRelocation = Rels.begin();
771       Target->NumRelocations = Rels.size();
772       Target->AreRelocsRela = false;
773     }
774     assert(isUInt<31>(Target->NumRelocations));
775 
776     // Relocation sections processed by the linker are usually removed
777     // from the output, so returning `nullptr` for the normal case.
778     // However, if -emit-relocs is given, we need to leave them in the output.
779     // (Some post link analysis tools need this information.)
780     if (Config->EmitRelocs) {
781       InputSection *RelocSec = make<InputSection>(*this, Sec, Name);
782       // We will not emit relocation section if target was discarded.
783       Target->DependentSections.push_back(RelocSec);
784       return RelocSec;
785     }
786     return nullptr;
787   }
788   }
789 
790   // The GNU linker uses .note.GNU-stack section as a marker indicating
791   // that the code in the object file does not expect that the stack is
792   // executable (in terms of NX bit). If all input files have the marker,
793   // the GNU linker adds a PT_GNU_STACK segment to tells the loader to
794   // make the stack non-executable. Most object files have this section as
795   // of 2017.
796   //
797   // But making the stack non-executable is a norm today for security
798   // reasons. Failure to do so may result in a serious security issue.
799   // Therefore, we make LLD always add PT_GNU_STACK unless it is
800   // explicitly told to do otherwise (by -z execstack). Because the stack
801   // executable-ness is controlled solely by command line options,
802   // .note.GNU-stack sections are simply ignored.
803   if (Name == ".note.GNU-stack")
804     return &InputSection::Discarded;
805 
806   // Split stacks is a feature to support a discontiguous stack,
807   // commonly used in the programming language Go. For the details,
808   // see https://gcc.gnu.org/wiki/SplitStacks. An object file compiled
809   // for split stack will include a .note.GNU-split-stack section.
810   if (Name == ".note.GNU-split-stack") {
811     if (Config->Relocatable) {
812       error("cannot mix split-stack and non-split-stack in a relocatable link");
813       return &InputSection::Discarded;
814     }
815     this->SplitStack = true;
816     return &InputSection::Discarded;
817   }
818 
819   // An object file cmpiled for split stack, but where some of the
820   // functions were compiled with the no_split_stack_attribute will
821   // include a .note.GNU-no-split-stack section.
822   if (Name == ".note.GNU-no-split-stack") {
823     this->SomeNoSplitStack = true;
824     return &InputSection::Discarded;
825   }
826 
827   // The linkonce feature is a sort of proto-comdat. Some glibc i386 object
828   // files contain definitions of symbol "__x86.get_pc_thunk.bx" in linkonce
829   // sections. Drop those sections to avoid duplicate symbol errors.
830   // FIXME: This is glibc PR20543, we should remove this hack once that has been
831   // fixed for a while.
832   if (Name == ".gnu.linkonce.t.__x86.get_pc_thunk.bx" ||
833       Name == ".gnu.linkonce.t.__i686.get_pc_thunk.bx")
834     return &InputSection::Discarded;
835 
836   // If we are creating a new .build-id section, strip existing .build-id
837   // sections so that the output won't have more than one .build-id.
838   // This is not usually a problem because input object files normally don't
839   // have .build-id sections, but you can create such files by
840   // "ld.{bfd,gold,lld} -r --build-id", and we want to guard against it.
841   if (Name == ".note.gnu.build-id" && Config->BuildId != BuildIdKind::None)
842     return &InputSection::Discarded;
843 
844   // The linker merges EH (exception handling) frames and creates a
845   // .eh_frame_hdr section for runtime. So we handle them with a special
846   // class. For relocatable outputs, they are just passed through.
847   if (Name == ".eh_frame" && !Config->Relocatable)
848     return make<EhInputSection>(*this, Sec, Name);
849 
850   if (shouldMerge(Sec))
851     return make<MergeInputSection>(*this, Sec, Name);
852   return make<InputSection>(*this, Sec, Name);
853 }
854 
855 template <class ELFT>
856 StringRef ObjFile<ELFT>::getSectionName(const Elf_Shdr &Sec) {
857   return CHECK(getObj().getSectionName(&Sec, SectionStringTable), this);
858 }
859 
860 template <class ELFT> void ObjFile<ELFT>::initializeSymbols() {
861   this->Symbols.reserve(this->getELFSyms<ELFT>().size());
862   for (const Elf_Sym &Sym : this->getELFSyms<ELFT>())
863     this->Symbols.push_back(createSymbol(&Sym));
864 }
865 
866 template <class ELFT> Symbol *ObjFile<ELFT>::createSymbol(const Elf_Sym *Sym) {
867   int Binding = Sym->getBinding();
868 
869   uint32_t SecIdx = getSectionIndex(*Sym);
870   if (SecIdx >= this->Sections.size())
871     fatal(toString(this) + ": invalid section index: " + Twine(SecIdx));
872 
873   InputSectionBase *Sec = this->Sections[SecIdx];
874   uint8_t StOther = Sym->st_other;
875   uint8_t Type = Sym->getType();
876   uint64_t Value = Sym->st_value;
877   uint64_t Size = Sym->st_size;
878 
879   if (Binding == STB_LOCAL) {
880     if (Sym->getType() == STT_FILE)
881       SourceFile = CHECK(Sym->getName(this->StringTable), this);
882 
883     if (this->StringTable.size() <= Sym->st_name)
884       fatal(toString(this) + ": invalid symbol name offset");
885 
886     StringRefZ Name = this->StringTable.data() + Sym->st_name;
887     if (Sym->st_shndx == SHN_UNDEF)
888       return make<Undefined>(this, Name, Binding, StOther, Type);
889 
890     return make<Defined>(this, Name, Binding, StOther, Type, Value, Size, Sec);
891   }
892 
893   StringRef Name = CHECK(Sym->getName(this->StringTable), this);
894 
895   switch (Sym->st_shndx) {
896   case SHN_UNDEF:
897     return Symtab->addUndefined<ELFT>(Name, Binding, StOther, Type,
898                                       /*CanOmitFromDynSym=*/false, this);
899   case SHN_COMMON:
900     if (Value == 0 || Value >= UINT32_MAX)
901       fatal(toString(this) + ": common symbol '" + Name +
902             "' has invalid alignment: " + Twine(Value));
903     return Symtab->addCommon(Name, Size, Value, Binding, StOther, Type, *this);
904   }
905 
906   switch (Binding) {
907   default:
908     fatal(toString(this) + ": unexpected binding: " + Twine(Binding));
909   case STB_GLOBAL:
910   case STB_WEAK:
911   case STB_GNU_UNIQUE:
912     if (Sec == &InputSection::Discarded)
913       return Symtab->addUndefined<ELFT>(Name, Binding, StOther, Type,
914                                         /*CanOmitFromDynSym=*/false, this);
915     return Symtab->addDefined(Name, StOther, Type, Value, Size, Binding, Sec,
916                               this);
917   }
918 }
919 
920 ArchiveFile::ArchiveFile(std::unique_ptr<Archive> &&File)
921     : InputFile(ArchiveKind, File->getMemoryBufferRef()),
922       File(std::move(File)) {}
923 
924 template <class ELFT> void ArchiveFile::parse() {
925   for (const Archive::Symbol &Sym : File->symbols())
926     Symtab->addLazyArchive<ELFT>(Sym.getName(), *this, Sym);
927 }
928 
929 // Returns a buffer pointing to a member file containing a given symbol.
930 InputFile *ArchiveFile::fetch(const Archive::Symbol &Sym) {
931   Archive::Child C =
932       CHECK(Sym.getMember(), toString(this) +
933                                  ": could not get the member for symbol " +
934                                  Sym.getName());
935 
936   if (!Seen.insert(C.getChildOffset()).second)
937     return nullptr;
938 
939   MemoryBufferRef MB =
940       CHECK(C.getMemoryBufferRef(),
941             toString(this) +
942                 ": could not get the buffer for the member defining symbol " +
943                 Sym.getName());
944 
945   if (Tar && C.getParent()->isThin())
946     Tar->append(relativeToRoot(CHECK(C.getFullName(), this)), MB.getBuffer());
947 
948   InputFile *File = createObjectFile(
949       MB, getName(), C.getParent()->isThin() ? 0 : C.getChildOffset());
950   File->GroupId = GroupId;
951   return File;
952 }
953 
954 unsigned SharedFile::VernauxNum;
955 
956 SharedFile::SharedFile(MemoryBufferRef M, StringRef DefaultSoName)
957     : ELFFileBase(SharedKind, M), SoName(DefaultSoName),
958       IsNeeded(!Config->AsNeeded) {}
959 
960 // Parse the version definitions in the object file if present, and return a
961 // vector whose nth element contains a pointer to the Elf_Verdef for version
962 // identifier n. Version identifiers that are not definitions map to nullptr.
963 template <typename ELFT>
964 static std::vector<const void *> parseVerdefs(const uint8_t *Base,
965                                               const typename ELFT::Shdr *Sec) {
966   if (!Sec)
967     return {};
968 
969   // We cannot determine the largest verdef identifier without inspecting
970   // every Elf_Verdef, but both bfd and gold assign verdef identifiers
971   // sequentially starting from 1, so we predict that the largest identifier
972   // will be VerdefCount.
973   unsigned VerdefCount = Sec->sh_info;
974   std::vector<const void *> Verdefs(VerdefCount + 1);
975 
976   // Build the Verdefs array by following the chain of Elf_Verdef objects
977   // from the start of the .gnu.version_d section.
978   const uint8_t *Verdef = Base + Sec->sh_offset;
979   for (unsigned I = 0; I != VerdefCount; ++I) {
980     auto *CurVerdef = reinterpret_cast<const typename ELFT::Verdef *>(Verdef);
981     Verdef += CurVerdef->vd_next;
982     unsigned VerdefIndex = CurVerdef->vd_ndx;
983     Verdefs.resize(VerdefIndex + 1);
984     Verdefs[VerdefIndex] = CurVerdef;
985   }
986   return Verdefs;
987 }
988 
989 // We do not usually care about alignments of data in shared object
990 // files because the loader takes care of it. However, if we promote a
991 // DSO symbol to point to .bss due to copy relocation, we need to keep
992 // the original alignment requirements. We infer it in this function.
993 template <typename ELFT>
994 static uint64_t getAlignment(ArrayRef<typename ELFT::Shdr> Sections,
995                              const typename ELFT::Sym &Sym) {
996   uint64_t Ret = UINT64_MAX;
997   if (Sym.st_value)
998     Ret = 1ULL << countTrailingZeros((uint64_t)Sym.st_value);
999   if (0 < Sym.st_shndx && Sym.st_shndx < Sections.size())
1000     Ret = std::min<uint64_t>(Ret, Sections[Sym.st_shndx].sh_addralign);
1001   return (Ret > UINT32_MAX) ? 0 : Ret;
1002 }
1003 
1004 // Fully parse the shared object file.
1005 //
1006 // This function parses symbol versions. If a DSO has version information,
1007 // the file has a ".gnu.version_d" section which contains symbol version
1008 // definitions. Each symbol is associated to one version through a table in
1009 // ".gnu.version" section. That table is a parallel array for the symbol
1010 // table, and each table entry contains an index in ".gnu.version_d".
1011 //
1012 // The special index 0 is reserved for VERF_NDX_LOCAL and 1 is for
1013 // VER_NDX_GLOBAL. There's no table entry for these special versions in
1014 // ".gnu.version_d".
1015 //
1016 // The file format for symbol versioning is perhaps a bit more complicated
1017 // than necessary, but you can easily understand the code if you wrap your
1018 // head around the data structure described above.
1019 template <class ELFT> void SharedFile::parse() {
1020   using Elf_Dyn = typename ELFT::Dyn;
1021   using Elf_Shdr = typename ELFT::Shdr;
1022   using Elf_Sym = typename ELFT::Sym;
1023   using Elf_Verdef = typename ELFT::Verdef;
1024   using Elf_Versym = typename ELFT::Versym;
1025 
1026   ArrayRef<Elf_Dyn> DynamicTags;
1027   const ELFFile<ELFT> Obj = this->getObj<ELFT>();
1028   ArrayRef<Elf_Shdr> Sections = CHECK(Obj.sections(), this);
1029 
1030   const Elf_Shdr *VersymSec = nullptr;
1031   const Elf_Shdr *VerdefSec = nullptr;
1032 
1033   // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d.
1034   for (const Elf_Shdr &Sec : Sections) {
1035     switch (Sec.sh_type) {
1036     default:
1037       continue;
1038     case SHT_DYNSYM:
1039       this->initSymtab<ELFT>(Sections, &Sec);
1040       break;
1041     case SHT_DYNAMIC:
1042       DynamicTags =
1043           CHECK(Obj.template getSectionContentsAsArray<Elf_Dyn>(&Sec), this);
1044       break;
1045     case SHT_GNU_versym:
1046       VersymSec = &Sec;
1047       break;
1048     case SHT_GNU_verdef:
1049       VerdefSec = &Sec;
1050       break;
1051     }
1052   }
1053 
1054   if (VersymSec && this->getELFSyms<ELFT>().empty()) {
1055     error("SHT_GNU_versym should be associated with symbol table");
1056     return;
1057   }
1058 
1059   // Search for a DT_SONAME tag to initialize this->SoName.
1060   for (const Elf_Dyn &Dyn : DynamicTags) {
1061     if (Dyn.d_tag == DT_NEEDED) {
1062       uint64_t Val = Dyn.getVal();
1063       if (Val >= this->StringTable.size())
1064         fatal(toString(this) + ": invalid DT_NEEDED entry");
1065       DtNeeded.push_back(this->StringTable.data() + Val);
1066     } else if (Dyn.d_tag == DT_SONAME) {
1067       uint64_t Val = Dyn.getVal();
1068       if (Val >= this->StringTable.size())
1069         fatal(toString(this) + ": invalid DT_SONAME entry");
1070       SoName = this->StringTable.data() + Val;
1071     }
1072   }
1073 
1074   // DSOs are uniquified not by filename but by soname.
1075   DenseMap<StringRef, SharedFile *>::iterator It;
1076   bool WasInserted;
1077   std::tie(It, WasInserted) = Symtab->SoNames.try_emplace(SoName, this);
1078 
1079   // If a DSO appears more than once on the command line with and without
1080   // --as-needed, --no-as-needed takes precedence over --as-needed because a
1081   // user can add an extra DSO with --no-as-needed to force it to be added to
1082   // the dependency list.
1083   It->second->IsNeeded |= IsNeeded;
1084   if (!WasInserted)
1085     return;
1086 
1087   SharedFiles.push_back(this);
1088 
1089   Verdefs = parseVerdefs<ELFT>(Obj.base(), VerdefSec);
1090 
1091   // Parse ".gnu.version" section which is a parallel array for the symbol
1092   // table. If a given file doesn't have a ".gnu.version" section, we use
1093   // VER_NDX_GLOBAL.
1094   size_t Size = this->getELFSyms<ELFT>().size() - this->FirstGlobal;
1095   std::vector<uint32_t> Versyms(Size, VER_NDX_GLOBAL);
1096   if (VersymSec) {
1097     ArrayRef<Elf_Versym> Versym =
1098         CHECK(Obj.template getSectionContentsAsArray<Elf_Versym>(VersymSec),
1099               this)
1100             .slice(FirstGlobal);
1101     for (size_t I = 0; I < Size; ++I)
1102       Versyms[I] = Versym[I].vs_index;
1103   }
1104 
1105   // System libraries can have a lot of symbols with versions. Using a
1106   // fixed buffer for computing the versions name (foo@ver) can save a
1107   // lot of allocations.
1108   SmallString<0> VersionedNameBuffer;
1109 
1110   // Add symbols to the symbol table.
1111   ArrayRef<Elf_Sym> Syms = this->getGlobalELFSyms<ELFT>();
1112   for (size_t I = 0; I < Syms.size(); ++I) {
1113     const Elf_Sym &Sym = Syms[I];
1114 
1115     // ELF spec requires that all local symbols precede weak or global
1116     // symbols in each symbol table, and the index of first non-local symbol
1117     // is stored to sh_info. If a local symbol appears after some non-local
1118     // symbol, that's a violation of the spec.
1119     StringRef Name = CHECK(Sym.getName(this->StringTable), this);
1120     if (Sym.getBinding() == STB_LOCAL) {
1121       warn("found local symbol '" + Name +
1122            "' in global part of symbol table in file " + toString(this));
1123       continue;
1124     }
1125 
1126     if (Sym.isUndefined()) {
1127       Symbol *S = Symtab->addUndefined<ELFT>(Name, Sym.getBinding(),
1128                                              Sym.st_other, Sym.getType(),
1129                                              /*CanOmitFromDynSym=*/false, this);
1130       S->ExportDynamic = true;
1131       continue;
1132     }
1133 
1134     // MIPS BFD linker puts _gp_disp symbol into DSO files and incorrectly
1135     // assigns VER_NDX_LOCAL to this section global symbol. Here is a
1136     // workaround for this bug.
1137     uint32_t Idx = Versyms[I] & ~VERSYM_HIDDEN;
1138     if (Config->EMachine == EM_MIPS && Idx == VER_NDX_LOCAL &&
1139         Name == "_gp_disp")
1140       continue;
1141 
1142     uint64_t Alignment = getAlignment<ELFT>(Sections, Sym);
1143     if (!(Versyms[I] & VERSYM_HIDDEN))
1144       Symtab->addShared(Name, Sym.getBinding(), Sym.st_other, Sym.getType(),
1145                         Sym.st_value, Sym.st_size, Alignment, Idx, this);
1146 
1147     // Also add the symbol with the versioned name to handle undefined symbols
1148     // with explicit versions.
1149     if (Idx == VER_NDX_GLOBAL)
1150       continue;
1151 
1152     if (Idx >= Verdefs.size() || Idx == VER_NDX_LOCAL) {
1153       error("corrupt input file: version definition index " + Twine(Idx) +
1154             " for symbol " + Name + " is out of bounds\n>>> defined in " +
1155             toString(this));
1156       continue;
1157     }
1158 
1159     StringRef VerName =
1160         this->StringTable.data() +
1161         reinterpret_cast<const Elf_Verdef *>(Verdefs[Idx])->getAux()->vda_name;
1162     VersionedNameBuffer.clear();
1163     Name = (Name + "@" + VerName).toStringRef(VersionedNameBuffer);
1164     Symtab->addShared(Saver.save(Name), Sym.getBinding(), Sym.st_other,
1165                       Sym.getType(), Sym.st_value, Sym.st_size, Alignment, Idx,
1166                       this);
1167   }
1168 }
1169 
1170 static ELFKind getBitcodeELFKind(const Triple &T) {
1171   if (T.isLittleEndian())
1172     return T.isArch64Bit() ? ELF64LEKind : ELF32LEKind;
1173   return T.isArch64Bit() ? ELF64BEKind : ELF32BEKind;
1174 }
1175 
1176 static uint8_t getBitcodeMachineKind(StringRef Path, const Triple &T) {
1177   switch (T.getArch()) {
1178   case Triple::aarch64:
1179     return EM_AARCH64;
1180   case Triple::amdgcn:
1181   case Triple::r600:
1182     return EM_AMDGPU;
1183   case Triple::arm:
1184   case Triple::thumb:
1185     return EM_ARM;
1186   case Triple::avr:
1187     return EM_AVR;
1188   case Triple::mips:
1189   case Triple::mipsel:
1190   case Triple::mips64:
1191   case Triple::mips64el:
1192     return EM_MIPS;
1193   case Triple::msp430:
1194     return EM_MSP430;
1195   case Triple::ppc:
1196     return EM_PPC;
1197   case Triple::ppc64:
1198   case Triple::ppc64le:
1199     return EM_PPC64;
1200   case Triple::x86:
1201     return T.isOSIAMCU() ? EM_IAMCU : EM_386;
1202   case Triple::x86_64:
1203     return EM_X86_64;
1204   default:
1205     error(Path + ": could not infer e_machine from bitcode target triple " +
1206           T.str());
1207     return EM_NONE;
1208   }
1209 }
1210 
1211 BitcodeFile::BitcodeFile(MemoryBufferRef MB, StringRef ArchiveName,
1212                          uint64_t OffsetInArchive)
1213     : InputFile(BitcodeKind, MB) {
1214   this->ArchiveName = ArchiveName;
1215 
1216   std::string Path = MB.getBufferIdentifier().str();
1217   if (Config->ThinLTOIndexOnly)
1218     Path = replaceThinLTOSuffix(MB.getBufferIdentifier());
1219 
1220   // ThinLTO assumes that all MemoryBufferRefs given to it have a unique
1221   // name. If two archives define two members with the same name, this
1222   // causes a collision which result in only one of the objects being taken
1223   // into consideration at LTO time (which very likely causes undefined
1224   // symbols later in the link stage). So we append file offset to make
1225   // filename unique.
1226   MemoryBufferRef MBRef(
1227       MB.getBuffer(),
1228       Saver.save(ArchiveName + Path +
1229                  (ArchiveName.empty() ? "" : utostr(OffsetInArchive))));
1230 
1231   Obj = CHECK(lto::InputFile::create(MBRef), this);
1232 
1233   Triple T(Obj->getTargetTriple());
1234   EKind = getBitcodeELFKind(T);
1235   EMachine = getBitcodeMachineKind(MB.getBufferIdentifier(), T);
1236 }
1237 
1238 static uint8_t mapVisibility(GlobalValue::VisibilityTypes GvVisibility) {
1239   switch (GvVisibility) {
1240   case GlobalValue::DefaultVisibility:
1241     return STV_DEFAULT;
1242   case GlobalValue::HiddenVisibility:
1243     return STV_HIDDEN;
1244   case GlobalValue::ProtectedVisibility:
1245     return STV_PROTECTED;
1246   }
1247   llvm_unreachable("unknown visibility");
1248 }
1249 
1250 template <class ELFT>
1251 static Symbol *createBitcodeSymbol(const std::vector<bool> &KeptComdats,
1252                                    const lto::InputFile::Symbol &ObjSym,
1253                                    BitcodeFile &F) {
1254   StringRef Name = Saver.save(ObjSym.getName());
1255   uint32_t Binding = ObjSym.isWeak() ? STB_WEAK : STB_GLOBAL;
1256 
1257   uint8_t Type = ObjSym.isTLS() ? STT_TLS : STT_NOTYPE;
1258   uint8_t Visibility = mapVisibility(ObjSym.getVisibility());
1259   bool CanOmitFromDynSym = ObjSym.canBeOmittedFromSymbolTable();
1260 
1261   int C = ObjSym.getComdatIndex();
1262   if (C != -1 && !KeptComdats[C])
1263     return Symtab->addUndefined<ELFT>(Name, Binding, Visibility, Type,
1264                                       CanOmitFromDynSym, &F);
1265 
1266   if (ObjSym.isUndefined())
1267     return Symtab->addUndefined<ELFT>(Name, Binding, Visibility, Type,
1268                                       CanOmitFromDynSym, &F);
1269 
1270   if (ObjSym.isCommon())
1271     return Symtab->addCommon(Name, ObjSym.getCommonSize(),
1272                              ObjSym.getCommonAlignment(), Binding, Visibility,
1273                              STT_OBJECT, F);
1274 
1275   return Symtab->addBitcode(Name, Binding, Visibility, Type, CanOmitFromDynSym,
1276                             F);
1277 }
1278 
1279 template <class ELFT>
1280 void BitcodeFile::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
1281   std::vector<bool> KeptComdats;
1282   for (StringRef S : Obj->getComdatTable())
1283     KeptComdats.push_back(ComdatGroups.insert(CachedHashStringRef(S)).second);
1284 
1285   for (const lto::InputFile::Symbol &ObjSym : Obj->symbols())
1286     Symbols.push_back(createBitcodeSymbol<ELFT>(KeptComdats, ObjSym, *this));
1287 }
1288 
1289 static ELFKind getELFKind(MemoryBufferRef MB, StringRef ArchiveName) {
1290   unsigned char Size;
1291   unsigned char Endian;
1292   std::tie(Size, Endian) = getElfArchType(MB.getBuffer());
1293 
1294   auto Fatal = [&](StringRef Msg) {
1295     StringRef Filename = MB.getBufferIdentifier();
1296     if (ArchiveName.empty())
1297       fatal(Filename + ": " + Msg);
1298     else
1299       fatal(ArchiveName + "(" + Filename + "): " + Msg);
1300   };
1301 
1302   if (!MB.getBuffer().startswith(ElfMagic))
1303     Fatal("not an ELF file");
1304   if (Endian != ELFDATA2LSB && Endian != ELFDATA2MSB)
1305     Fatal("corrupted ELF file: invalid data encoding");
1306   if (Size != ELFCLASS32 && Size != ELFCLASS64)
1307     Fatal("corrupted ELF file: invalid file class");
1308 
1309   size_t BufSize = MB.getBuffer().size();
1310   if ((Size == ELFCLASS32 && BufSize < sizeof(Elf32_Ehdr)) ||
1311       (Size == ELFCLASS64 && BufSize < sizeof(Elf64_Ehdr)))
1312     Fatal("corrupted ELF file: file is too short");
1313 
1314   if (Size == ELFCLASS32)
1315     return (Endian == ELFDATA2LSB) ? ELF32LEKind : ELF32BEKind;
1316   return (Endian == ELFDATA2LSB) ? ELF64LEKind : ELF64BEKind;
1317 }
1318 
1319 void BinaryFile::parse() {
1320   ArrayRef<uint8_t> Data = arrayRefFromStringRef(MB.getBuffer());
1321   auto *Section = make<InputSection>(this, SHF_ALLOC | SHF_WRITE, SHT_PROGBITS,
1322                                      8, Data, ".data");
1323   Sections.push_back(Section);
1324 
1325   // For each input file foo that is embedded to a result as a binary
1326   // blob, we define _binary_foo_{start,end,size} symbols, so that
1327   // user programs can access blobs by name. Non-alphanumeric
1328   // characters in a filename are replaced with underscore.
1329   std::string S = "_binary_" + MB.getBufferIdentifier().str();
1330   for (size_t I = 0; I < S.size(); ++I)
1331     if (!isAlnum(S[I]))
1332       S[I] = '_';
1333 
1334   Symtab->addDefined(Saver.save(S + "_start"), STV_DEFAULT, STT_OBJECT, 0, 0,
1335                      STB_GLOBAL, Section, nullptr);
1336   Symtab->addDefined(Saver.save(S + "_end"), STV_DEFAULT, STT_OBJECT,
1337                      Data.size(), 0, STB_GLOBAL, Section, nullptr);
1338   Symtab->addDefined(Saver.save(S + "_size"), STV_DEFAULT, STT_OBJECT,
1339                      Data.size(), 0, STB_GLOBAL, nullptr, nullptr);
1340 }
1341 
1342 InputFile *elf::createObjectFile(MemoryBufferRef MB, StringRef ArchiveName,
1343                                  uint64_t OffsetInArchive) {
1344   if (isBitcode(MB))
1345     return make<BitcodeFile>(MB, ArchiveName, OffsetInArchive);
1346 
1347   switch (getELFKind(MB, ArchiveName)) {
1348   case ELF32LEKind:
1349     return make<ObjFile<ELF32LE>>(MB, ArchiveName);
1350   case ELF32BEKind:
1351     return make<ObjFile<ELF32BE>>(MB, ArchiveName);
1352   case ELF64LEKind:
1353     return make<ObjFile<ELF64LE>>(MB, ArchiveName);
1354   case ELF64BEKind:
1355     return make<ObjFile<ELF64BE>>(MB, ArchiveName);
1356   default:
1357     llvm_unreachable("getELFKind");
1358   }
1359 }
1360 
1361 InputFile *elf::createSharedFile(MemoryBufferRef MB, StringRef DefaultSoName) {
1362   auto *F = make<SharedFile>(MB, DefaultSoName);
1363   switch (getELFKind(MB, "")) {
1364   case ELF32LEKind:
1365     F->parseHeader<ELF32LE>();
1366     break;
1367   case ELF32BEKind:
1368     F->parseHeader<ELF32BE>();
1369     break;
1370   case ELF64LEKind:
1371     F->parseHeader<ELF64LE>();
1372     break;
1373   case ELF64BEKind:
1374     F->parseHeader<ELF64BE>();
1375     break;
1376   default:
1377     llvm_unreachable("getELFKind");
1378   }
1379   return F;
1380 }
1381 
1382 MemoryBufferRef LazyObjFile::getBuffer() {
1383   if (AddedToLink)
1384     return MemoryBufferRef();
1385   AddedToLink = true;
1386   return MB;
1387 }
1388 
1389 InputFile *LazyObjFile::fetch() {
1390   MemoryBufferRef MBRef = getBuffer();
1391   if (MBRef.getBuffer().empty())
1392     return nullptr;
1393 
1394   InputFile *File = createObjectFile(MBRef, ArchiveName, OffsetInArchive);
1395   File->GroupId = GroupId;
1396   return File;
1397 }
1398 
1399 template <class ELFT> void LazyObjFile::parse() {
1400   // A lazy object file wraps either a bitcode file or an ELF file.
1401   if (isBitcode(this->MB)) {
1402     std::unique_ptr<lto::InputFile> Obj =
1403         CHECK(lto::InputFile::create(this->MB), this);
1404     for (const lto::InputFile::Symbol &Sym : Obj->symbols())
1405       if (!Sym.isUndefined())
1406         Symtab->addLazyObject<ELFT>(Saver.save(Sym.getName()), *this);
1407     return;
1408   }
1409 
1410   if (getELFKind(this->MB, ArchiveName) != Config->EKind) {
1411     error("incompatible file: " + this->MB.getBufferIdentifier());
1412     return;
1413   }
1414 
1415   ELFFile<ELFT> Obj = check(ELFFile<ELFT>::create(MB.getBuffer()));
1416   ArrayRef<typename ELFT::Shdr> Sections = CHECK(Obj.sections(), this);
1417 
1418   for (const typename ELFT::Shdr &Sec : Sections) {
1419     if (Sec.sh_type != SHT_SYMTAB)
1420       continue;
1421 
1422     typename ELFT::SymRange Syms = CHECK(Obj.symbols(&Sec), this);
1423     uint32_t FirstGlobal = Sec.sh_info;
1424     StringRef StringTable =
1425         CHECK(Obj.getStringTableForSymtab(Sec, Sections), this);
1426 
1427     for (const typename ELFT::Sym &Sym : Syms.slice(FirstGlobal))
1428       if (Sym.st_shndx != SHN_UNDEF)
1429         Symtab->addLazyObject<ELFT>(CHECK(Sym.getName(StringTable), this),
1430                                     *this);
1431     return;
1432   }
1433 }
1434 
1435 std::string elf::replaceThinLTOSuffix(StringRef Path) {
1436   StringRef Suffix = Config->ThinLTOObjectSuffixReplace.first;
1437   StringRef Repl = Config->ThinLTOObjectSuffixReplace.second;
1438 
1439   if (Path.consume_back(Suffix))
1440     return (Path + Repl).str();
1441   return Path;
1442 }
1443 
1444 template void elf::parseFile<ELF32LE>(InputFile *);
1445 template void elf::parseFile<ELF32BE>(InputFile *);
1446 template void elf::parseFile<ELF64LE>(InputFile *);
1447 template void elf::parseFile<ELF64BE>(InputFile *);
1448 
1449 template void ArchiveFile::parse<ELF32LE>();
1450 template void ArchiveFile::parse<ELF32BE>();
1451 template void ArchiveFile::parse<ELF64LE>();
1452 template void ArchiveFile::parse<ELF64BE>();
1453 
1454 template void BitcodeFile::parse<ELF32LE>(DenseSet<CachedHashStringRef> &);
1455 template void BitcodeFile::parse<ELF32BE>(DenseSet<CachedHashStringRef> &);
1456 template void BitcodeFile::parse<ELF64LE>(DenseSet<CachedHashStringRef> &);
1457 template void BitcodeFile::parse<ELF64BE>(DenseSet<CachedHashStringRef> &);
1458 
1459 template void LazyObjFile::parse<ELF32LE>();
1460 template void LazyObjFile::parse<ELF32BE>();
1461 template void LazyObjFile::parse<ELF64LE>();
1462 template void LazyObjFile::parse<ELF64BE>();
1463 
1464 template class elf::ObjFile<ELF32LE>;
1465 template class elf::ObjFile<ELF32BE>;
1466 template class elf::ObjFile<ELF64LE>;
1467 template class elf::ObjFile<ELF64BE>;
1468 
1469 template void SharedFile::parse<ELF32LE>();
1470 template void SharedFile::parse<ELF32BE>();
1471 template void SharedFile::parse<ELF64LE>();
1472 template void SharedFile::parse<ELF64BE>();
1473