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