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