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