1 //===- InputFiles.cpp -----------------------------------------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "InputFiles.h"
11 #include "Driver.h"
12 #include "Error.h"
13 #include "InputSection.h"
14 #include "LinkerScript.h"
15 #include "Memory.h"
16 #include "SymbolTable.h"
17 #include "Symbols.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/Bitcode/ReaderWriter.h"
20 #include "llvm/CodeGen/Analysis.h"
21 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
22 #include "llvm/IR/LLVMContext.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/LTO/LTO.h"
25 #include "llvm/MC/StringTableBuilder.h"
26 #include "llvm/Object/ELFObjectFile.h"
27 #include "llvm/Support/Path.h"
28 #include "llvm/Support/raw_ostream.h"
29 
30 using namespace llvm;
31 using namespace llvm::ELF;
32 using namespace llvm::object;
33 using namespace llvm::sys::fs;
34 
35 using namespace lld;
36 using namespace lld::elf;
37 
38 namespace {
39 // In ELF object file all section addresses are zero. If we have multiple
40 // .text sections (when using -ffunction-section or comdat group) then
41 // LLVM DWARF parser will not be able to parse .debug_line correctly, unless
42 // we assign each section some unique address. This callback method assigns
43 // each section an address equal to its offset in ELF object file.
44 class ObjectInfo : public LoadedObjectInfo {
45 public:
46   uint64_t getSectionLoadAddress(const object::SectionRef &Sec) const override {
47     return static_cast<const ELFSectionRef &>(Sec).getOffset();
48   }
49   std::unique_ptr<LoadedObjectInfo> clone() const override {
50     return std::unique_ptr<LoadedObjectInfo>();
51   }
52 };
53 }
54 
55 template <class ELFT> void elf::ObjectFile<ELFT>::initializeDwarfLine() {
56   std::unique_ptr<object::ObjectFile> Obj =
57       check(object::ObjectFile::createObjectFile(this->MB),
58             "createObjectFile failed");
59 
60   ObjectInfo ObjInfo;
61   DWARFContextInMemory Dwarf(*Obj.get(), &ObjInfo);
62   DwarfLine.reset(new DWARFDebugLine(&Dwarf.getLineSection().Relocs));
63   DataExtractor LineData(Dwarf.getLineSection().Data,
64                          ELFT::TargetEndianness == support::little,
65                          ELFT::Is64Bits ? 8 : 4);
66 
67   // The second parameter is offset in .debug_line section
68   // for compilation unit (CU) of interest. We have only one
69   // CU (object file), so offset is always 0.
70   DwarfLine->getOrParseLineTable(LineData, 0);
71 }
72 
73 // Returns source line information for a given offset
74 // using DWARF debug info.
75 template <class ELFT>
76 std::string elf::ObjectFile<ELFT>::getLineInfo(InputSectionBase<ELFT> *S,
77                                                uintX_t Offset) {
78   if (!DwarfLine)
79     initializeDwarfLine();
80 
81   // The offset to CU is 0.
82   const DWARFDebugLine::LineTable *Tbl = DwarfLine->getLineTable(0);
83   if (!Tbl)
84     return "";
85 
86   // Use fake address calcuated by adding section file offset and offset in
87   // section. See comments for ObjectInfo class.
88   DILineInfo Info;
89   DILineInfoSpecifier Spec;
90   Tbl->getFileLineInfoForAddress(S->Offset + Offset, nullptr, Spec.FLIKind,
91                                  Info);
92   if (Info.Line == 0)
93     return "";
94   return Info.FileName + " (" + std::to_string(Info.Line) + ")";
95 }
96 
97 // Returns "(internal)", "foo.a(bar.o)" or "baz.o".
98 std::string elf::getFilename(const InputFile *F) {
99   if (!F)
100     return "(internal)";
101   if (!F->ArchiveName.empty())
102     return (F->ArchiveName + "(" + F->getName() + ")").str();
103   return F->getName();
104 }
105 
106 template <class ELFT> static ELFKind getELFKind() {
107   if (ELFT::TargetEndianness == support::little)
108     return ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind;
109   return ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind;
110 }
111 
112 template <class ELFT>
113 ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB) : InputFile(K, MB) {
114   EKind = getELFKind<ELFT>();
115   EMachine = getObj().getHeader()->e_machine;
116   OSABI = getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI];
117 }
118 
119 template <class ELFT>
120 typename ELFT::SymRange ELFFileBase<ELFT>::getGlobalSymbols() {
121     return makeArrayRef(Symbols.begin() + FirstNonLocal, Symbols.end());
122 }
123 
124 template <class ELFT>
125 uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
126   return check(getObj().getSectionIndex(&Sym, Symbols, SymtabSHNDX));
127 }
128 
129 template <class ELFT>
130 void ELFFileBase<ELFT>::initSymtab(ArrayRef<Elf_Shdr> Sections,
131                                    const Elf_Shdr *Symtab) {
132   FirstNonLocal = Symtab->sh_info;
133   Symbols = check(getObj().symbols(Symtab));
134   if (FirstNonLocal == 0 || FirstNonLocal > Symbols.size())
135     fatal(getFilename(this) + ": invalid sh_info in symbol table");
136 
137   StringTable = check(getObj().getStringTableForSymtab(*Symtab, Sections));
138 }
139 
140 template <class ELFT>
141 elf::ObjectFile<ELFT>::ObjectFile(MemoryBufferRef M)
142     : ELFFileBase<ELFT>(Base::ObjectKind, M) {}
143 
144 template <class ELFT>
145 ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getNonLocalSymbols() {
146   return makeArrayRef(this->SymbolBodies).slice(this->FirstNonLocal);
147 }
148 
149 template <class ELFT>
150 ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getLocalSymbols() {
151   if (this->SymbolBodies.empty())
152     return this->SymbolBodies;
153   return makeArrayRef(this->SymbolBodies).slice(1, this->FirstNonLocal - 1);
154 }
155 
156 template <class ELFT>
157 ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getSymbols() {
158   if (this->SymbolBodies.empty())
159     return this->SymbolBodies;
160   return makeArrayRef(this->SymbolBodies).slice(1);
161 }
162 
163 template <class ELFT> uint32_t elf::ObjectFile<ELFT>::getMipsGp0() const {
164   if (ELFT::Is64Bits && MipsOptions && MipsOptions->Reginfo)
165     return MipsOptions->Reginfo->ri_gp_value;
166   if (!ELFT::Is64Bits && MipsReginfo && MipsReginfo->Reginfo)
167     return MipsReginfo->Reginfo->ri_gp_value;
168   return 0;
169 }
170 
171 template <class ELFT>
172 void elf::ObjectFile<ELFT>::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
173   // Read section and symbol tables.
174   ArrayRef<Elf_Shdr> ObjSections = check(this->getObj().sections());
175   initializeSections(ComdatGroups, ObjSections);
176   initializeSymbols(ObjSections);
177 }
178 
179 // Sections with SHT_GROUP and comdat bits define comdat section groups.
180 // They are identified and deduplicated by group name. This function
181 // returns a group name.
182 template <class ELFT>
183 StringRef
184 elf::ObjectFile<ELFT>::getShtGroupSignature(ArrayRef<Elf_Shdr> Sections,
185                                             const Elf_Shdr &Sec) {
186   if (this->Symbols.empty())
187     this->initSymtab(Sections,
188                      check(object::getSection<ELFT>(Sections, Sec.sh_link)));
189   const Elf_Sym *Sym =
190       check(object::getSymbol<ELFT>(this->Symbols, Sec.sh_info));
191   return check(Sym->getName(this->StringTable));
192 }
193 
194 template <class ELFT>
195 ArrayRef<typename elf::ObjectFile<ELFT>::Elf_Word>
196 elf::ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
197   const ELFFile<ELFT> &Obj = this->getObj();
198   ArrayRef<Elf_Word> Entries =
199       check(Obj.template getSectionContentsAsArray<Elf_Word>(&Sec));
200   if (Entries.empty() || Entries[0] != GRP_COMDAT)
201     fatal(getFilename(this) + ": unsupported SHT_GROUP format");
202   return Entries.slice(1);
203 }
204 
205 template <class ELFT>
206 bool elf::ObjectFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) {
207   // We don't merge sections if -O0 (default is -O1). This makes sometimes
208   // the linker significantly faster, although the output will be bigger.
209   if (Config->Optimize == 0)
210     return false;
211 
212   // Do not merge sections if generating a relocatable object. It makes
213   // the code simpler because we do not need to update relocation addends
214   // to reflect changes introduced by merging. Instead of that we write
215   // such "merge" sections into separate OutputSections and keep SHF_MERGE
216   // / SHF_STRINGS flags and sh_entsize value to be able to perform merging
217   // later during a final linking.
218   if (Config->Relocatable)
219     return false;
220 
221   // A mergeable section with size 0 is useless because they don't have
222   // any data to merge. A mergeable string section with size 0 can be
223   // argued as invalid because it doesn't end with a null character.
224   // We'll avoid a mess by handling them as if they were non-mergeable.
225   if (Sec.sh_size == 0)
226     return false;
227 
228   // Check for sh_entsize. The ELF spec is not clear about the zero
229   // sh_entsize. It says that "the member [sh_entsize] contains 0 if
230   // the section does not hold a table of fixed-size entries". We know
231   // that Rust 1.13 produces a string mergeable section with a zero
232   // sh_entsize. Here we just accept it rather than being picky about it.
233   uintX_t EntSize = Sec.sh_entsize;
234   if (EntSize == 0)
235     return false;
236   if (Sec.sh_size % EntSize)
237     fatal(getFilename(this) +
238           ": SHF_MERGE section size must be a multiple of sh_entsize");
239 
240   uintX_t Flags = Sec.sh_flags;
241   if (!(Flags & SHF_MERGE))
242     return false;
243   if (Flags & SHF_WRITE)
244     fatal(getFilename(this) + ": writable SHF_MERGE section is not supported");
245 
246   // Don't try to merge if the alignment is larger than the sh_entsize and this
247   // is not SHF_STRINGS.
248   //
249   // Since this is not a SHF_STRINGS, we would need to pad after every entity.
250   // It would be equivalent for the producer of the .o to just set a larger
251   // sh_entsize.
252   if (Flags & SHF_STRINGS)
253     return true;
254 
255   return Sec.sh_addralign <= EntSize;
256 }
257 
258 template <class ELFT>
259 void elf::ObjectFile<ELFT>::initializeSections(
260     DenseSet<CachedHashStringRef> &ComdatGroups,
261     ArrayRef<Elf_Shdr> ObjSections) {
262   const ELFFile<ELFT> &Obj = this->getObj();
263   uint64_t Size = ObjSections.size();
264   Sections.resize(Size);
265   unsigned I = -1;
266   StringRef SectionStringTable = check(Obj.getSectionStringTable(ObjSections));
267   for (const Elf_Shdr &Sec : ObjSections) {
268     ++I;
269     if (Sections[I] == &InputSection<ELFT>::Discarded)
270       continue;
271 
272     // SHF_EXCLUDE'ed sections are discarded by the linker. However,
273     // if -r is given, we'll let the final link discard such sections.
274     // This is compatible with GNU.
275     if ((Sec.sh_flags & SHF_EXCLUDE) && !Config->Relocatable) {
276       Sections[I] = &InputSection<ELFT>::Discarded;
277       continue;
278     }
279 
280     switch (Sec.sh_type) {
281     case SHT_GROUP:
282       Sections[I] = &InputSection<ELFT>::Discarded;
283       if (ComdatGroups
284               .insert(
285                   CachedHashStringRef(getShtGroupSignature(ObjSections, Sec)))
286               .second)
287         continue;
288       for (uint32_t SecIndex : getShtGroupEntries(Sec)) {
289         if (SecIndex >= Size)
290           fatal(getFilename(this) + ": invalid section index in group: " +
291                 Twine(SecIndex));
292         Sections[SecIndex] = &InputSection<ELFT>::Discarded;
293       }
294       break;
295     case SHT_SYMTAB:
296       this->initSymtab(ObjSections, &Sec);
297       break;
298     case SHT_SYMTAB_SHNDX:
299       this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec, ObjSections));
300       break;
301     case SHT_STRTAB:
302     case SHT_NULL:
303       break;
304     default:
305       Sections[I] = createInputSection(Sec, SectionStringTable);
306     }
307 
308     // .ARM.exidx sections have a reverse dependency on the InputSection they
309     // have a SHF_LINK_ORDER dependency, this is identified by the sh_link.
310     if (Sec.sh_flags & SHF_LINK_ORDER) {
311       if (Sec.sh_link >= Sections.size())
312         fatal(getFilename(this) + ": invalid sh_link index: " +
313               Twine(Sec.sh_link));
314       auto *IS = cast<InputSection<ELFT>>(Sections[Sec.sh_link]);
315       IS->DependentSection = Sections[I];
316     }
317   }
318 }
319 
320 template <class ELFT>
321 InputSectionBase<ELFT> *
322 elf::ObjectFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
323   uint32_t Idx = Sec.sh_info;
324   if (Idx >= Sections.size())
325     fatal(getFilename(this) + ": invalid relocated section index: " +
326           Twine(Idx));
327   InputSectionBase<ELFT> *Target = Sections[Idx];
328 
329   // Strictly speaking, a relocation section must be included in the
330   // group of the section it relocates. However, LLVM 3.3 and earlier
331   // would fail to do so, so we gracefully handle that case.
332   if (Target == &InputSection<ELFT>::Discarded)
333     return nullptr;
334 
335   if (!Target)
336     fatal(getFilename(this) + ": unsupported relocation reference");
337   return Target;
338 }
339 
340 template <class ELFT>
341 InputSectionBase<ELFT> *
342 elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec,
343                                           StringRef SectionStringTable) {
344   StringRef Name =
345       check(this->getObj().getSectionName(&Sec, SectionStringTable));
346 
347   switch (Sec.sh_type) {
348   case SHT_ARM_ATTRIBUTES:
349     // FIXME: ARM meta-data section. At present attributes are ignored,
350     // they can be used to reason about object compatibility.
351     return &InputSection<ELFT>::Discarded;
352   case SHT_MIPS_REGINFO:
353     if (MipsReginfo)
354       fatal(getFilename(this) +
355             ": multiple SHT_MIPS_REGINFO sections are not allowed");
356     MipsReginfo.reset(new MipsReginfoInputSection<ELFT>(this, &Sec, Name));
357     return MipsReginfo.get();
358   case SHT_MIPS_OPTIONS:
359     if (MipsOptions)
360       fatal(getFilename(this) +
361             ": multiple SHT_MIPS_OPTIONS sections are not allowed");
362     MipsOptions.reset(new MipsOptionsInputSection<ELFT>(this, &Sec, Name));
363     return MipsOptions.get();
364   case SHT_MIPS_ABIFLAGS:
365     if (MipsAbiFlags)
366       fatal(getFilename(this) +
367             ": multiple SHT_MIPS_ABIFLAGS sections are not allowed");
368     MipsAbiFlags.reset(new MipsAbiFlagsInputSection<ELFT>(this, &Sec, Name));
369     return MipsAbiFlags.get();
370   case SHT_RELA:
371   case SHT_REL: {
372     // This section contains relocation information.
373     // If -r is given, we do not interpret or apply relocation
374     // but just copy relocation sections to output.
375     if (Config->Relocatable)
376       return make<InputSection<ELFT>>(this, &Sec, Name);
377 
378     // Find the relocation target section and associate this
379     // section with it.
380     InputSectionBase<ELFT> *Target = getRelocTarget(Sec);
381     if (!Target)
382       return nullptr;
383     if (auto *S = dyn_cast<InputSection<ELFT>>(Target)) {
384       S->RelocSections.push_back(&Sec);
385       return nullptr;
386     }
387     if (auto *S = dyn_cast<EhInputSection<ELFT>>(Target)) {
388       if (S->RelocSection)
389         fatal(getFilename(this) +
390               ": multiple relocation sections to .eh_frame are not supported");
391       S->RelocSection = &Sec;
392       return nullptr;
393     }
394     fatal(getFilename(this) +
395           ": relocations pointing to SHF_MERGE are not supported");
396   }
397   }
398 
399   // .note.GNU-stack is a marker section to control the presence of
400   // PT_GNU_STACK segment in outputs. Since the presence of the segment
401   // is controlled only by the command line option (-z execstack) in LLD,
402   // .note.GNU-stack is ignored.
403   if (Name == ".note.GNU-stack")
404     return &InputSection<ELFT>::Discarded;
405 
406   if (Name == ".note.GNU-split-stack") {
407     error("objects using splitstacks are not supported");
408     return &InputSection<ELFT>::Discarded;
409   }
410 
411   if (Config->Strip != StripPolicy::None && Name.startswith(".debug"))
412     return &InputSection<ELFT>::Discarded;
413 
414   // The linker merges EH (exception handling) frames and creates a
415   // .eh_frame_hdr section for runtime. So we handle them with a special
416   // class. For relocatable outputs, they are just passed through.
417   if (Name == ".eh_frame" && !Config->Relocatable)
418     return make<EhInputSection<ELFT>>(this, &Sec, Name);
419 
420   if (shouldMerge(Sec))
421     return make<MergeInputSection<ELFT>>(this, &Sec, Name);
422   return make<InputSection<ELFT>>(this, &Sec, Name);
423 }
424 
425 template <class ELFT>
426 void elf::ObjectFile<ELFT>::initializeSymbols(ArrayRef<Elf_Shdr> Sections) {
427   SymbolBodies.reserve(this->Symbols.size());
428   for (const Elf_Sym &Sym : this->Symbols)
429     SymbolBodies.push_back(createSymbolBody(&Sym));
430 }
431 
432 template <class ELFT>
433 InputSectionBase<ELFT> *
434 elf::ObjectFile<ELFT>::getSection(const Elf_Sym &Sym) const {
435   uint32_t Index = this->getSectionIndex(Sym);
436   if (Index >= Sections.size())
437     fatal(getFilename(this) + ": invalid section index: " + Twine(Index));
438   InputSectionBase<ELFT> *S = Sections[Index];
439 
440   // We found that GNU assembler 2.17.50 [FreeBSD] 2007-07-03
441   // could generate broken objects. STT_SECTION symbols can be
442   // associated with SHT_REL[A]/SHT_SYMTAB/SHT_STRTAB sections.
443   // In this case it is fine for section to be null here as we
444   // do not allocate sections of these types.
445   if (!S) {
446     if (Index == 0 || Sym.getType() == STT_SECTION)
447       return nullptr;
448     fatal(getFilename(this) + ": invalid section index: " + Twine(Index));
449   }
450 
451   if (S == &InputSectionBase<ELFT>::Discarded)
452     return S;
453   return S->Repl;
454 }
455 
456 template <class ELFT>
457 SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
458   int Binding = Sym->getBinding();
459   InputSectionBase<ELFT> *Sec = getSection(*Sym);
460   if (Binding == STB_LOCAL) {
461     if (Sym->getType() == STT_FILE)
462       SourceFile = check(Sym->getName(this->StringTable));
463     if (Sym->st_shndx == SHN_UNDEF)
464       return new (BAlloc)
465           Undefined(Sym->st_name, Sym->st_other, Sym->getType(), this);
466     return new (BAlloc) DefinedRegular<ELFT>(*Sym, Sec);
467   }
468 
469   StringRef Name = check(Sym->getName(this->StringTable));
470 
471   switch (Sym->st_shndx) {
472   case SHN_UNDEF:
473     return elf::Symtab<ELFT>::X->addUndefined(Name, Binding, Sym->st_other,
474                                               Sym->getType(),
475                                               /*CanOmitFromDynSym*/ false, this)
476         ->body();
477   case SHN_COMMON:
478     if (Sym->st_value == 0 || Sym->st_value >= UINT32_MAX)
479       fatal(getFilename(this) + ": common symbol '" + Name +
480             "' has invalid alignment: " + Twine(Sym->st_value));
481     return elf::Symtab<ELFT>::X->addCommon(Name, Sym->st_size, Sym->st_value,
482                                            Binding, Sym->st_other,
483                                            Sym->getType(), this)
484         ->body();
485   }
486 
487   switch (Binding) {
488   default:
489     fatal(getFilename(this) + ": unexpected binding: " + Twine(Binding));
490   case STB_GLOBAL:
491   case STB_WEAK:
492   case STB_GNU_UNIQUE:
493     if (Sec == &InputSection<ELFT>::Discarded)
494       return elf::Symtab<ELFT>::X->addUndefined(Name, Binding, Sym->st_other,
495                                                 Sym->getType(),
496                                                 /*CanOmitFromDynSym*/ false,
497                                                 this)
498           ->body();
499     return elf::Symtab<ELFT>::X->addRegular(Name, *Sym, Sec)->body();
500   }
501 }
502 
503 template <class ELFT> void ArchiveFile::parse() {
504   File = check(Archive::create(MB), "failed to parse archive");
505 
506   // Read the symbol table to construct Lazy objects.
507   for (const Archive::Symbol &Sym : File->symbols())
508     Symtab<ELFT>::X->addLazyArchive(this, Sym);
509 }
510 
511 // Returns a buffer pointing to a member file containing a given symbol.
512 std::pair<MemoryBufferRef, uint64_t>
513 ArchiveFile::getMember(const Archive::Symbol *Sym) {
514   Archive::Child C =
515       check(Sym->getMember(),
516             "could not get the member for symbol " + Sym->getName());
517 
518   if (!Seen.insert(C.getChildOffset()).second)
519     return {MemoryBufferRef(), 0};
520 
521   MemoryBufferRef Ret =
522       check(C.getMemoryBufferRef(),
523             "could not get the buffer for the member defining symbol " +
524                 Sym->getName());
525 
526   if (C.getParent()->isThin() && Driver->Cpio)
527     Driver->Cpio->append(relativeToRoot(check(C.getFullName())),
528                          Ret.getBuffer());
529   if (C.getParent()->isThin())
530     return {Ret, 0};
531   return {Ret, C.getChildOffset()};
532 }
533 
534 template <class ELFT>
535 SharedFile<ELFT>::SharedFile(MemoryBufferRef M)
536     : ELFFileBase<ELFT>(Base::SharedKind, M), AsNeeded(Config->AsNeeded) {}
537 
538 template <class ELFT>
539 const typename ELFT::Shdr *
540 SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const {
541   return check(
542       this->getObj().getSection(&Sym, this->Symbols, this->SymtabSHNDX));
543 }
544 
545 // Partially parse the shared object file so that we can call
546 // getSoName on this object.
547 template <class ELFT> void SharedFile<ELFT>::parseSoName() {
548   typedef typename ELFT::Dyn Elf_Dyn;
549   typedef typename ELFT::uint uintX_t;
550   const Elf_Shdr *DynamicSec = nullptr;
551 
552   const ELFFile<ELFT> Obj = this->getObj();
553   ArrayRef<Elf_Shdr> Sections = check(Obj.sections());
554   for (const Elf_Shdr &Sec : Sections) {
555     switch (Sec.sh_type) {
556     default:
557       continue;
558     case SHT_DYNSYM:
559       this->initSymtab(Sections, &Sec);
560       break;
561     case SHT_DYNAMIC:
562       DynamicSec = &Sec;
563       break;
564     case SHT_SYMTAB_SHNDX:
565       this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec, Sections));
566       break;
567     case SHT_GNU_versym:
568       this->VersymSec = &Sec;
569       break;
570     case SHT_GNU_verdef:
571       this->VerdefSec = &Sec;
572       break;
573     }
574   }
575 
576   if (this->VersymSec && this->Symbols.empty())
577     error("SHT_GNU_versym should be associated with symbol table");
578 
579   // DSOs are identified by soname, and they usually contain
580   // DT_SONAME tag in their header. But if they are missing,
581   // filenames are used as default sonames.
582   SoName = sys::path::filename(this->getName());
583 
584   if (!DynamicSec)
585     return;
586 
587   ArrayRef<Elf_Dyn> Arr =
588       check(Obj.template getSectionContentsAsArray<Elf_Dyn>(DynamicSec),
589             getFilename(this) + ": getSectionContentsAsArray failed");
590   for (const Elf_Dyn &Dyn : Arr) {
591     if (Dyn.d_tag == DT_SONAME) {
592       uintX_t Val = Dyn.getVal();
593       if (Val >= this->StringTable.size())
594         fatal(getFilename(this) + ": invalid DT_SONAME entry");
595       SoName = StringRef(this->StringTable.data() + Val);
596       return;
597     }
598   }
599 }
600 
601 // Parse the version definitions in the object file if present. Returns a vector
602 // whose nth element contains a pointer to the Elf_Verdef for version identifier
603 // n. Version identifiers that are not definitions map to nullptr. The array
604 // always has at least length 1.
605 template <class ELFT>
606 std::vector<const typename ELFT::Verdef *>
607 SharedFile<ELFT>::parseVerdefs(const Elf_Versym *&Versym) {
608   std::vector<const Elf_Verdef *> Verdefs(1);
609   // We only need to process symbol versions for this DSO if it has both a
610   // versym and a verdef section, which indicates that the DSO contains symbol
611   // version definitions.
612   if (!VersymSec || !VerdefSec)
613     return Verdefs;
614 
615   // The location of the first global versym entry.
616   const char *Base = this->MB.getBuffer().data();
617   Versym = reinterpret_cast<const Elf_Versym *>(Base + VersymSec->sh_offset) +
618            this->FirstNonLocal;
619 
620   // We cannot determine the largest verdef identifier without inspecting
621   // every Elf_Verdef, but both bfd and gold assign verdef identifiers
622   // sequentially starting from 1, so we predict that the largest identifier
623   // will be VerdefCount.
624   unsigned VerdefCount = VerdefSec->sh_info;
625   Verdefs.resize(VerdefCount + 1);
626 
627   // Build the Verdefs array by following the chain of Elf_Verdef objects
628   // from the start of the .gnu.version_d section.
629   const char *Verdef = Base + VerdefSec->sh_offset;
630   for (unsigned I = 0; I != VerdefCount; ++I) {
631     auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef);
632     Verdef += CurVerdef->vd_next;
633     unsigned VerdefIndex = CurVerdef->vd_ndx;
634     if (Verdefs.size() <= VerdefIndex)
635       Verdefs.resize(VerdefIndex + 1);
636     Verdefs[VerdefIndex] = CurVerdef;
637   }
638 
639   return Verdefs;
640 }
641 
642 // Fully parse the shared object file. This must be called after parseSoName().
643 template <class ELFT> void SharedFile<ELFT>::parseRest() {
644   // Create mapping from version identifiers to Elf_Verdef entries.
645   const Elf_Versym *Versym = nullptr;
646   std::vector<const Elf_Verdef *> Verdefs = parseVerdefs(Versym);
647 
648   Elf_Sym_Range Syms = this->getGlobalSymbols();
649   for (const Elf_Sym &Sym : Syms) {
650     unsigned VersymIndex = 0;
651     if (Versym) {
652       VersymIndex = Versym->vs_index;
653       ++Versym;
654     }
655 
656     StringRef Name = check(Sym.getName(this->StringTable));
657     if (Sym.isUndefined()) {
658       Undefs.push_back(Name);
659       continue;
660     }
661 
662     if (Versym) {
663       // Ignore local symbols and non-default versions.
664       if (VersymIndex == VER_NDX_LOCAL || (VersymIndex & VERSYM_HIDDEN))
665         continue;
666     }
667 
668     const Elf_Verdef *V =
669         VersymIndex == VER_NDX_GLOBAL ? nullptr : Verdefs[VersymIndex];
670     elf::Symtab<ELFT>::X->addShared(this, Name, Sym, V);
671   }
672 }
673 
674 static ELFKind getBitcodeELFKind(MemoryBufferRef MB) {
675   Triple T(getBitcodeTargetTriple(MB, Driver->Context));
676   if (T.isLittleEndian())
677     return T.isArch64Bit() ? ELF64LEKind : ELF32LEKind;
678   return T.isArch64Bit() ? ELF64BEKind : ELF32BEKind;
679 }
680 
681 static uint8_t getBitcodeMachineKind(MemoryBufferRef MB) {
682   Triple T(getBitcodeTargetTriple(MB, Driver->Context));
683   switch (T.getArch()) {
684   case Triple::aarch64:
685     return EM_AARCH64;
686   case Triple::arm:
687     return EM_ARM;
688   case Triple::mips:
689   case Triple::mipsel:
690   case Triple::mips64:
691   case Triple::mips64el:
692     return EM_MIPS;
693   case Triple::ppc:
694     return EM_PPC;
695   case Triple::ppc64:
696     return EM_PPC64;
697   case Triple::x86:
698     return T.isOSIAMCU() ? EM_IAMCU : EM_386;
699   case Triple::x86_64:
700     return EM_X86_64;
701   default:
702     fatal(MB.getBufferIdentifier() +
703           ": could not infer e_machine from bitcode target triple " + T.str());
704   }
705 }
706 
707 BitcodeFile::BitcodeFile(MemoryBufferRef MB) : InputFile(BitcodeKind, MB) {
708   EKind = getBitcodeELFKind(MB);
709   EMachine = getBitcodeMachineKind(MB);
710 }
711 
712 static uint8_t mapVisibility(GlobalValue::VisibilityTypes GvVisibility) {
713   switch (GvVisibility) {
714   case GlobalValue::DefaultVisibility:
715     return STV_DEFAULT;
716   case GlobalValue::HiddenVisibility:
717     return STV_HIDDEN;
718   case GlobalValue::ProtectedVisibility:
719     return STV_PROTECTED;
720   }
721   llvm_unreachable("unknown visibility");
722 }
723 
724 template <class ELFT>
725 static Symbol *createBitcodeSymbol(const std::vector<bool> &KeptComdats,
726                                    const lto::InputFile::Symbol &ObjSym,
727                                    BitcodeFile *F) {
728   StringRef NameRef = Saver.save(ObjSym.getName());
729   uint32_t Flags = ObjSym.getFlags();
730   uint32_t Binding = (Flags & BasicSymbolRef::SF_Weak) ? STB_WEAK : STB_GLOBAL;
731 
732   uint8_t Type = ObjSym.isTLS() ? STT_TLS : STT_NOTYPE;
733   uint8_t Visibility = mapVisibility(ObjSym.getVisibility());
734   bool CanOmitFromDynSym = ObjSym.canBeOmittedFromSymbolTable();
735 
736   int C = check(ObjSym.getComdatIndex());
737   if (C != -1 && !KeptComdats[C])
738     return Symtab<ELFT>::X->addUndefined(NameRef, Binding, Visibility, Type,
739                                          CanOmitFromDynSym, F);
740 
741   if (Flags & BasicSymbolRef::SF_Undefined)
742     return Symtab<ELFT>::X->addUndefined(NameRef, Binding, Visibility, Type,
743                                          CanOmitFromDynSym, F);
744 
745   if (Flags & BasicSymbolRef::SF_Common)
746     return Symtab<ELFT>::X->addCommon(NameRef, ObjSym.getCommonSize(),
747                                       ObjSym.getCommonAlignment(), Binding,
748                                       Visibility, STT_OBJECT, F);
749 
750   return Symtab<ELFT>::X->addBitcode(NameRef, Binding, Visibility, Type,
751                                      CanOmitFromDynSym, F);
752 }
753 
754 template <class ELFT>
755 void BitcodeFile::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
756 
757   // Here we pass a new MemoryBufferRef which is identified by ArchiveName
758   // (the fully resolved path of the archive) + member name + offset of the
759   // member in the archive.
760   // ThinLTO uses the MemoryBufferRef identifier to access its internal
761   // data structures and if two archives define two members with the same name,
762   // this causes a collision which result in only one of the objects being
763   // taken into consideration at LTO time (which very likely causes undefined
764   // symbols later in the link stage).
765   Obj = check(lto::InputFile::create(MemoryBufferRef(
766       MB.getBuffer(), Saver.save(ArchiveName + MB.getBufferIdentifier() +
767                                  utostr(OffsetInArchive)))));
768 
769   std::vector<bool> KeptComdats;
770   for (StringRef S : Obj->getComdatTable()) {
771     StringRef N = Saver.save(S);
772     KeptComdats.push_back(ComdatGroups.insert(CachedHashStringRef(N)).second);
773   }
774 
775   for (const lto::InputFile::Symbol &ObjSym : Obj->symbols())
776     Symbols.push_back(createBitcodeSymbol<ELFT>(KeptComdats, ObjSym, this));
777 }
778 
779 template <template <class> class T>
780 static InputFile *createELFFile(MemoryBufferRef MB) {
781   unsigned char Size;
782   unsigned char Endian;
783   std::tie(Size, Endian) = getElfArchType(MB.getBuffer());
784   if (Endian != ELFDATA2LSB && Endian != ELFDATA2MSB)
785     fatal("invalid data encoding: " + MB.getBufferIdentifier());
786 
787   size_t BufSize = MB.getBuffer().size();
788   if ((Size == ELFCLASS32 && BufSize < sizeof(Elf32_Ehdr)) ||
789       (Size == ELFCLASS64 && BufSize < sizeof(Elf64_Ehdr)))
790     fatal("file is too short");
791 
792   InputFile *Obj;
793   if (Size == ELFCLASS32 && Endian == ELFDATA2LSB)
794     Obj = make<T<ELF32LE>>(MB);
795   else if (Size == ELFCLASS32 && Endian == ELFDATA2MSB)
796     Obj = make<T<ELF32BE>>(MB);
797   else if (Size == ELFCLASS64 && Endian == ELFDATA2LSB)
798     Obj = make<T<ELF64LE>>(MB);
799   else if (Size == ELFCLASS64 && Endian == ELFDATA2MSB)
800     Obj = make<T<ELF64BE>>(MB);
801   else
802     fatal("invalid file class: " + MB.getBufferIdentifier());
803 
804   if (!Config->FirstElf)
805     Config->FirstElf = Obj;
806   return Obj;
807 }
808 
809 template <class ELFT> void BinaryFile::parse() {
810   StringRef Buf = MB.getBuffer();
811   ArrayRef<uint8_t> Data =
812       makeArrayRef<uint8_t>((const uint8_t *)Buf.data(), Buf.size());
813 
814   std::string Filename = MB.getBufferIdentifier();
815   std::transform(Filename.begin(), Filename.end(), Filename.begin(),
816                  [](char C) { return isalnum(C) ? C : '_'; });
817   Filename = "_binary_" + Filename;
818   StringRef StartName = Saver.save(Twine(Filename) + "_start");
819   StringRef EndName = Saver.save(Twine(Filename) + "_end");
820   StringRef SizeName = Saver.save(Twine(Filename) + "_size");
821 
822   auto *Section =
823       new InputSection<ELFT>(SHF_ALLOC, SHT_PROGBITS, 8, Data, ".data");
824   Sections.push_back(Section);
825 
826   elf::Symtab<ELFT>::X->addRegular(StartName, STV_DEFAULT, Section, STB_GLOBAL,
827                                    STT_OBJECT, 0);
828   elf::Symtab<ELFT>::X->addRegular(EndName, STV_DEFAULT, Section, STB_GLOBAL,
829                                    STT_OBJECT, Data.size());
830   elf::Symtab<ELFT>::X->addRegular(SizeName, STV_DEFAULT, nullptr, STB_GLOBAL,
831                                    STT_OBJECT, Data.size());
832 }
833 
834 static bool isBitcode(MemoryBufferRef MB) {
835   using namespace sys::fs;
836   return identify_magic(MB.getBuffer()) == file_magic::bitcode;
837 }
838 
839 InputFile *elf::createObjectFile(MemoryBufferRef MB, StringRef ArchiveName,
840                                  uint64_t OffsetInArchive) {
841   InputFile *F =
842       isBitcode(MB) ? new BitcodeFile(MB) : createELFFile<ObjectFile>(MB);
843   F->ArchiveName = ArchiveName;
844   F->OffsetInArchive = OffsetInArchive;
845   return F;
846 }
847 
848 InputFile *elf::createSharedFile(MemoryBufferRef MB) {
849   return createELFFile<SharedFile>(MB);
850 }
851 
852 MemoryBufferRef LazyObjectFile::getBuffer() {
853   if (Seen)
854     return MemoryBufferRef();
855   Seen = true;
856   return MB;
857 }
858 
859 template <class ELFT> void LazyObjectFile::parse() {
860   for (StringRef Sym : getSymbols())
861     Symtab<ELFT>::X->addLazyObject(Sym, *this);
862 }
863 
864 template <class ELFT> std::vector<StringRef> LazyObjectFile::getElfSymbols() {
865   typedef typename ELFT::Shdr Elf_Shdr;
866   typedef typename ELFT::Sym Elf_Sym;
867   typedef typename ELFT::SymRange Elf_Sym_Range;
868 
869   const ELFFile<ELFT> Obj(this->MB.getBuffer());
870   ArrayRef<Elf_Shdr> Sections = check(Obj.sections());
871   for (const Elf_Shdr &Sec : Sections) {
872     if (Sec.sh_type != SHT_SYMTAB)
873       continue;
874     Elf_Sym_Range Syms = check(Obj.symbols(&Sec));
875     uint32_t FirstNonLocal = Sec.sh_info;
876     StringRef StringTable = check(Obj.getStringTableForSymtab(Sec, Sections));
877     std::vector<StringRef> V;
878     for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal))
879       if (Sym.st_shndx != SHN_UNDEF)
880         V.push_back(check(Sym.getName(StringTable)));
881     return V;
882   }
883   return {};
884 }
885 
886 std::vector<StringRef> LazyObjectFile::getBitcodeSymbols() {
887   std::unique_ptr<lto::InputFile> Obj = check(lto::InputFile::create(this->MB));
888   std::vector<StringRef> V;
889   for (const lto::InputFile::Symbol &Sym : Obj->symbols())
890     if (!(Sym.getFlags() & BasicSymbolRef::SF_Undefined))
891       V.push_back(Saver.save(Sym.getName()));
892   return V;
893 }
894 
895 // Returns a vector of globally-visible defined symbol names.
896 std::vector<StringRef> LazyObjectFile::getSymbols() {
897   if (isBitcode(this->MB))
898     return getBitcodeSymbols();
899 
900   unsigned char Size;
901   unsigned char Endian;
902   std::tie(Size, Endian) = getElfArchType(this->MB.getBuffer());
903   if (Size == ELFCLASS32) {
904     if (Endian == ELFDATA2LSB)
905       return getElfSymbols<ELF32LE>();
906     return getElfSymbols<ELF32BE>();
907   }
908   if (Endian == ELFDATA2LSB)
909     return getElfSymbols<ELF64LE>();
910   return getElfSymbols<ELF64BE>();
911 }
912 
913 template void ArchiveFile::parse<ELF32LE>();
914 template void ArchiveFile::parse<ELF32BE>();
915 template void ArchiveFile::parse<ELF64LE>();
916 template void ArchiveFile::parse<ELF64BE>();
917 
918 template void BitcodeFile::parse<ELF32LE>(DenseSet<CachedHashStringRef> &);
919 template void BitcodeFile::parse<ELF32BE>(DenseSet<CachedHashStringRef> &);
920 template void BitcodeFile::parse<ELF64LE>(DenseSet<CachedHashStringRef> &);
921 template void BitcodeFile::parse<ELF64BE>(DenseSet<CachedHashStringRef> &);
922 
923 template void LazyObjectFile::parse<ELF32LE>();
924 template void LazyObjectFile::parse<ELF32BE>();
925 template void LazyObjectFile::parse<ELF64LE>();
926 template void LazyObjectFile::parse<ELF64BE>();
927 
928 template class elf::ELFFileBase<ELF32LE>;
929 template class elf::ELFFileBase<ELF32BE>;
930 template class elf::ELFFileBase<ELF64LE>;
931 template class elf::ELFFileBase<ELF64BE>;
932 
933 template class elf::ObjectFile<ELF32LE>;
934 template class elf::ObjectFile<ELF32BE>;
935 template class elf::ObjectFile<ELF64LE>;
936 template class elf::ObjectFile<ELF64BE>;
937 
938 template class elf::SharedFile<ELF32LE>;
939 template class elf::SharedFile<ELF32BE>;
940 template class elf::SharedFile<ELF64LE>;
941 template class elf::SharedFile<ELF64BE>;
942 
943 template void BinaryFile::parse<ELF32LE>();
944 template void BinaryFile::parse<ELF32BE>();
945 template void BinaryFile::parse<ELF64LE>();
946 template void BinaryFile::parse<ELF64BE>();
947