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