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 "Error.h"
12 #include "InputSection.h"
13 #include "Symbols.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/Object/IRObjectFile.h"
18 #include "llvm/Support/raw_ostream.h"
19 
20 using namespace llvm;
21 using namespace llvm::ELF;
22 using namespace llvm::object;
23 using namespace llvm::sys::fs;
24 
25 using namespace lld;
26 using namespace lld::elf;
27 
28 template <class ELFT>
29 static ELFFile<ELFT> createELFObj(MemoryBufferRef MB) {
30   std::error_code EC;
31   ELFFile<ELFT> F(MB.getBuffer(), EC);
32   check(EC);
33   return F;
34 }
35 
36 template <class ELFT>
37 ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB)
38     : InputFile(K, MB), ELFObj(createELFObj<ELFT>(MB)) {}
39 
40 template <class ELFT>
41 ELFKind ELFFileBase<ELFT>::getELFKind() {
42   if (ELFT::TargetEndianness == support::little)
43     return ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind;
44   return ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind;
45 }
46 
47 template <class ELFT>
48 typename ELFFileBase<ELFT>::Elf_Sym_Range
49 ELFFileBase<ELFT>::getElfSymbols(bool OnlyGlobals) {
50   if (!Symtab)
51     return Elf_Sym_Range(nullptr, nullptr);
52   Elf_Sym_Range Syms = ELFObj.symbols(Symtab);
53   uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
54   uint32_t FirstNonLocal = Symtab->sh_info;
55   if (FirstNonLocal > NumSymbols)
56     fatal("invalid sh_info in symbol table");
57 
58   if (OnlyGlobals)
59     return make_range(Syms.begin() + FirstNonLocal, Syms.end());
60   return make_range(Syms.begin(), Syms.end());
61 }
62 
63 template <class ELFT>
64 uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
65   uint32_t I = Sym.st_shndx;
66   if (I == ELF::SHN_XINDEX)
67     return ELFObj.getExtendedSymbolTableIndex(&Sym, Symtab, SymtabSHNDX);
68   if (I >= ELF::SHN_LORESERVE)
69     return 0;
70   return I;
71 }
72 
73 template <class ELFT> void ELFFileBase<ELFT>::initStringTable() {
74   if (!Symtab)
75     return;
76   StringTable = check(ELFObj.getStringTableForSymtab(*Symtab));
77 }
78 
79 template <class ELFT>
80 elf::ObjectFile<ELFT>::ObjectFile(MemoryBufferRef M)
81     : ELFFileBase<ELFT>(Base::ObjectKind, M) {}
82 
83 template <class ELFT>
84 ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getNonLocalSymbols() {
85   if (!this->Symtab)
86     return this->SymbolBodies;
87   uint32_t FirstNonLocal = this->Symtab->sh_info;
88   return makeArrayRef(this->SymbolBodies).slice(FirstNonLocal);
89 }
90 
91 template <class ELFT>
92 ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getLocalSymbols() {
93   if (!this->Symtab)
94     return this->SymbolBodies;
95   uint32_t FirstNonLocal = this->Symtab->sh_info;
96   return makeArrayRef(this->SymbolBodies).slice(1, FirstNonLocal - 1);
97 }
98 
99 template <class ELFT>
100 ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getSymbols() {
101   if (!this->Symtab)
102     return this->SymbolBodies;
103   return makeArrayRef(this->SymbolBodies).slice(1);
104 }
105 
106 template <class ELFT> uint32_t elf::ObjectFile<ELFT>::getMipsGp0() const {
107   if (MipsReginfo)
108     return MipsReginfo->Reginfo->ri_gp_value;
109   return 0;
110 }
111 
112 template <class ELFT>
113 void elf::ObjectFile<ELFT>::parse(DenseSet<StringRef> &ComdatGroups) {
114   // Read section and symbol tables.
115   initializeSections(ComdatGroups);
116   initializeSymbols();
117 }
118 
119 // Sections with SHT_GROUP and comdat bits define comdat section groups.
120 // They are identified and deduplicated by group name. This function
121 // returns a group name.
122 template <class ELFT>
123 StringRef elf::ObjectFile<ELFT>::getShtGroupSignature(const Elf_Shdr &Sec) {
124   const ELFFile<ELFT> &Obj = this->ELFObj;
125   uint32_t SymtabdSectionIndex = Sec.sh_link;
126   const Elf_Shdr *SymtabSec = check(Obj.getSection(SymtabdSectionIndex));
127   uint32_t SymIndex = Sec.sh_info;
128   const Elf_Sym *Sym = Obj.getSymbol(SymtabSec, SymIndex);
129   StringRef StringTable = check(Obj.getStringTableForSymtab(*SymtabSec));
130   return check(Sym->getName(StringTable));
131 }
132 
133 template <class ELFT>
134 ArrayRef<typename elf::ObjectFile<ELFT>::Elf_Word>
135 elf::ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
136   const ELFFile<ELFT> &Obj = this->ELFObj;
137   ArrayRef<Elf_Word> Entries =
138       check(Obj.template getSectionContentsAsArray<Elf_Word>(&Sec));
139   if (Entries.empty() || Entries[0] != GRP_COMDAT)
140     fatal("unsupported SHT_GROUP format");
141   return Entries.slice(1);
142 }
143 
144 template <class ELFT>
145 static bool shouldMerge(const typename ELFFile<ELFT>::Elf_Shdr &Sec) {
146   typedef typename ELFFile<ELFT>::uintX_t uintX_t;
147   uintX_t Flags = Sec.sh_flags;
148   if (!(Flags & SHF_MERGE))
149     return false;
150   if (Flags & SHF_WRITE)
151     fatal("writable SHF_MERGE sections are not supported");
152   uintX_t EntSize = Sec.sh_entsize;
153   if (!EntSize || Sec.sh_size % EntSize)
154     fatal("SHF_MERGE section size must be a multiple of sh_entsize");
155 
156   // Don't try to merge if the aligment is larger than the sh_entsize and this
157   // is not SHF_STRINGS.
158   //
159   // Since this is not a SHF_STRINGS, we would need to pad after every entity.
160   // It would be equivalent for the producer of the .o to just set a larger
161   // sh_entsize.
162   if (Flags & SHF_STRINGS)
163     return true;
164 
165   if (Sec.sh_addralign > EntSize)
166     return false;
167 
168   return true;
169 }
170 
171 template <class ELFT>
172 void elf::ObjectFile<ELFT>::initializeSections(
173     DenseSet<StringRef> &ComdatGroups) {
174   uint64_t Size = this->ELFObj.getNumSections();
175   Sections.resize(Size);
176   unsigned I = -1;
177   const ELFFile<ELFT> &Obj = this->ELFObj;
178   for (const Elf_Shdr &Sec : Obj.sections()) {
179     ++I;
180     if (Sections[I] == InputSection<ELFT>::Discarded)
181       continue;
182 
183     switch (Sec.sh_type) {
184     case SHT_GROUP:
185       Sections[I] = InputSection<ELFT>::Discarded;
186       if (ComdatGroups.insert(getShtGroupSignature(Sec)).second)
187         continue;
188       for (uint32_t SecIndex : getShtGroupEntries(Sec)) {
189         if (SecIndex >= Size)
190           fatal("invalid section index in group");
191         Sections[SecIndex] = InputSection<ELFT>::Discarded;
192       }
193       break;
194     case SHT_SYMTAB:
195       this->Symtab = &Sec;
196       break;
197     case SHT_SYMTAB_SHNDX:
198       this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec));
199       break;
200     case SHT_STRTAB:
201     case SHT_NULL:
202       break;
203     case SHT_RELA:
204     case SHT_REL: {
205       // This section contains relocation information.
206       // If -r is given, we do not interpret or apply relocation
207       // but just copy relocation sections to output.
208       if (Config->Relocatable) {
209         Sections[I] = new (Alloc) InputSection<ELFT>(this, &Sec);
210         break;
211       }
212 
213       // Find the relocation target section and associate this
214       // section with it.
215       InputSectionBase<ELFT> *Target = getRelocTarget(Sec);
216       if (!Target)
217         break;
218       if (auto *S = dyn_cast<InputSection<ELFT>>(Target)) {
219         S->RelocSections.push_back(&Sec);
220         break;
221       }
222       if (auto *S = dyn_cast<EHInputSection<ELFT>>(Target)) {
223         if (S->RelocSection)
224           fatal("multiple relocation sections to .eh_frame are not supported");
225         S->RelocSection = &Sec;
226         break;
227       }
228       fatal("relocations pointing to SHF_MERGE are not supported");
229     }
230     default:
231       Sections[I] = createInputSection(Sec);
232     }
233   }
234 }
235 
236 template <class ELFT>
237 InputSectionBase<ELFT> *
238 elf::ObjectFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
239   uint32_t Idx = Sec.sh_info;
240   if (Idx >= Sections.size())
241     fatal("invalid relocated section index");
242   InputSectionBase<ELFT> *Target = Sections[Idx];
243 
244   // Strictly speaking, a relocation section must be included in the
245   // group of the section it relocates. However, LLVM 3.3 and earlier
246   // would fail to do so, so we gracefully handle that case.
247   if (Target == InputSection<ELFT>::Discarded)
248     return nullptr;
249 
250   if (!Target)
251     fatal("unsupported relocation reference");
252   return Target;
253 }
254 
255 template <class ELFT>
256 InputSectionBase<ELFT> *
257 elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
258   StringRef Name = check(this->ELFObj.getSectionName(&Sec));
259 
260   // .note.GNU-stack is a marker section to control the presence of
261   // PT_GNU_STACK segment in outputs. Since the presence of the segment
262   // is controlled only by the command line option (-z execstack) in LLD,
263   // .note.GNU-stack is ignored.
264   if (Name == ".note.GNU-stack")
265     return InputSection<ELFT>::Discarded;
266 
267   if (Name == ".note.GNU-split-stack")
268     error("objects using splitstacks are not supported");
269 
270   // A MIPS object file has a special section that contains register
271   // usage info, which needs to be handled by the linker specially.
272   if (Config->EMachine == EM_MIPS && Name == ".reginfo") {
273     MipsReginfo = new (Alloc) MipsReginfoInputSection<ELFT>(this, &Sec);
274     return MipsReginfo;
275   }
276 
277   // We dont need special handling of .eh_frame sections if relocatable
278   // output was choosen. Proccess them as usual input sections.
279   if (!Config->Relocatable && Name == ".eh_frame")
280     return new (EHAlloc.Allocate()) EHInputSection<ELFT>(this, &Sec);
281   if (shouldMerge<ELFT>(Sec))
282     return new (MAlloc.Allocate()) MergeInputSection<ELFT>(this, &Sec);
283   return new (Alloc) InputSection<ELFT>(this, &Sec);
284 }
285 
286 template <class ELFT> void elf::ObjectFile<ELFT>::initializeSymbols() {
287   this->initStringTable();
288   Elf_Sym_Range Syms = this->getElfSymbols(false);
289   uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
290   SymbolBodies.reserve(NumSymbols);
291   for (const Elf_Sym &Sym : Syms)
292     SymbolBodies.push_back(createSymbolBody(&Sym));
293 }
294 
295 template <class ELFT>
296 InputSectionBase<ELFT> *
297 elf::ObjectFile<ELFT>::getSection(const Elf_Sym &Sym) const {
298   uint32_t Index = this->getSectionIndex(Sym);
299   if (Index == 0)
300     return nullptr;
301   if (Index >= Sections.size() || !Sections[Index])
302     fatal("invalid section index");
303   InputSectionBase<ELFT> *S = Sections[Index];
304   if (S == InputSectionBase<ELFT>::Discarded)
305     return S;
306   return S->Repl;
307 }
308 
309 template <class ELFT>
310 SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
311   unsigned char Binding = Sym->getBinding();
312   InputSectionBase<ELFT> *Sec = getSection(*Sym);
313   if (Binding == STB_LOCAL) {
314     if (Sec == InputSection<ELFT>::Discarded)
315       Sec = nullptr;
316     return new (Alloc) DefinedRegular<ELFT>("", *Sym, Sec);
317   }
318 
319   StringRef Name = check(Sym->getName(this->StringTable));
320 
321   switch (Sym->st_shndx) {
322   case SHN_UNDEF:
323     return new (Alloc) UndefinedElf<ELFT>(Name, *Sym);
324   case SHN_COMMON:
325     return new (Alloc) DefinedCommon(Name, Sym->st_size, Sym->st_value,
326                                      Sym->getBinding() == llvm::ELF::STB_WEAK,
327                                      Sym->getVisibility());
328   }
329 
330   switch (Binding) {
331   default:
332     fatal("unexpected binding");
333   case STB_GLOBAL:
334   case STB_WEAK:
335   case STB_GNU_UNIQUE:
336     if (Sec == InputSection<ELFT>::Discarded)
337       return new (Alloc) UndefinedElf<ELFT>(Name, *Sym);
338     return new (Alloc) DefinedRegular<ELFT>(Name, *Sym, Sec);
339   }
340 }
341 
342 void ArchiveFile::parse() {
343   File = check(Archive::create(MB), "Failed to parse archive");
344 
345   // Allocate a buffer for Lazy objects.
346   size_t NumSyms = File->getNumberOfSymbols();
347   LazySymbols.reserve(NumSyms);
348 
349   // Read the symbol table to construct Lazy objects.
350   for (const Archive::Symbol &Sym : File->symbols())
351     LazySymbols.emplace_back(this, Sym);
352 }
353 
354 // Returns a buffer pointing to a member file containing a given symbol.
355 MemoryBufferRef ArchiveFile::getMember(const Archive::Symbol *Sym) {
356   Archive::Child C =
357       check(Sym->getMember(),
358             "Could not get the member for symbol " + Sym->getName());
359 
360   if (!Seen.insert(C.getChildOffset()).second)
361     return MemoryBufferRef();
362 
363   return check(C.getMemoryBufferRef(),
364                "Could not get the buffer for the member defining symbol " +
365                    Sym->getName());
366 }
367 
368 template <class ELFT>
369 SharedFile<ELFT>::SharedFile(MemoryBufferRef M)
370     : ELFFileBase<ELFT>(Base::SharedKind, M), AsNeeded(Config->AsNeeded) {}
371 
372 template <class ELFT>
373 const typename ELFFile<ELFT>::Elf_Shdr *
374 SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const {
375   uint32_t Index = this->getSectionIndex(Sym);
376   if (Index == 0)
377     return nullptr;
378   return check(this->ELFObj.getSection(Index));
379 }
380 
381 // Partially parse the shared object file so that we can call
382 // getSoName on this object.
383 template <class ELFT> void SharedFile<ELFT>::parseSoName() {
384   typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
385   typedef typename ELFFile<ELFT>::uintX_t uintX_t;
386   const Elf_Shdr *DynamicSec = nullptr;
387 
388   const ELFFile<ELFT> Obj = this->ELFObj;
389   for (const Elf_Shdr &Sec : Obj.sections()) {
390     switch (Sec.sh_type) {
391     default:
392       continue;
393     case SHT_DYNSYM:
394       this->Symtab = &Sec;
395       break;
396     case SHT_DYNAMIC:
397       DynamicSec = &Sec;
398       break;
399     case SHT_SYMTAB_SHNDX:
400       this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec));
401       break;
402     }
403   }
404 
405   this->initStringTable();
406   SoName = this->getName();
407 
408   if (!DynamicSec)
409     return;
410   auto *Begin =
411       reinterpret_cast<const Elf_Dyn *>(Obj.base() + DynamicSec->sh_offset);
412   const Elf_Dyn *End = Begin + DynamicSec->sh_size / sizeof(Elf_Dyn);
413 
414   for (const Elf_Dyn &Dyn : make_range(Begin, End)) {
415     if (Dyn.d_tag == DT_SONAME) {
416       uintX_t Val = Dyn.getVal();
417       if (Val >= this->StringTable.size())
418         fatal("invalid DT_SONAME entry");
419       SoName = StringRef(this->StringTable.data() + Val);
420       return;
421     }
422   }
423 }
424 
425 // Fully parse the shared object file. This must be called after parseSoName().
426 template <class ELFT> void SharedFile<ELFT>::parseRest() {
427   Elf_Sym_Range Syms = this->getElfSymbols(true);
428   uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
429   SymbolBodies.reserve(NumSymbols);
430   for (const Elf_Sym &Sym : Syms) {
431     StringRef Name = check(Sym.getName(this->StringTable));
432     if (Sym.isUndefined())
433       Undefs.push_back(Name);
434     else
435       SymbolBodies.emplace_back(this, Name, Sym);
436   }
437 }
438 
439 BitcodeFile::BitcodeFile(MemoryBufferRef M) : InputFile(BitcodeKind, M) {}
440 
441 bool BitcodeFile::classof(const InputFile *F) {
442   return F->kind() == BitcodeKind;
443 }
444 
445 static uint8_t getGvVisibility(const GlobalValue *GV) {
446   switch (GV->getVisibility()) {
447   case GlobalValue::DefaultVisibility:
448     return STV_DEFAULT;
449   case GlobalValue::HiddenVisibility:
450     return STV_HIDDEN;
451   case GlobalValue::ProtectedVisibility:
452     return STV_PROTECTED;
453   }
454   llvm_unreachable("unknown visibility");
455 }
456 
457 SymbolBody *
458 BitcodeFile::createSymbolBody(const DenseSet<const Comdat *> &KeptComdats,
459                               const IRObjectFile &Obj,
460                               const BasicSymbolRef &Sym) {
461   const GlobalValue *GV = Obj.getSymbolGV(Sym.getRawDataRefImpl());
462   assert(GV);
463   if (const Comdat *C = GV->getComdat())
464     if (!KeptComdats.count(C))
465       return nullptr;
466 
467   uint8_t Visibility = getGvVisibility(GV);
468 
469   SmallString<64> Name;
470   raw_svector_ostream OS(Name);
471   Sym.printName(OS);
472   StringRef NameRef = Saver.save(StringRef(Name));
473 
474   const Module &M = Obj.getModule();
475   SymbolBody *Body;
476   uint32_t Flags = Sym.getFlags();
477   bool IsWeak = Flags & BasicSymbolRef::SF_Weak;
478   if (Flags & BasicSymbolRef::SF_Undefined) {
479     Body = new (Alloc) Undefined(NameRef, IsWeak, Visibility, false);
480   } else if (Flags & BasicSymbolRef::SF_Common) {
481     const DataLayout &DL = M.getDataLayout();
482     uint64_t Size = DL.getTypeAllocSize(GV->getValueType());
483     Body = new (Alloc)
484         DefinedCommon(NameRef, Size, GV->getAlignment(), IsWeak, Visibility);
485   } else {
486     Body = new (Alloc) DefinedBitcode(NameRef, IsWeak, Visibility);
487   }
488   Body->IsTls = GV->isThreadLocal();
489   return Body;
490 }
491 
492 bool BitcodeFile::shouldSkip(const BasicSymbolRef &Sym) {
493   uint32_t Flags = Sym.getFlags();
494   if (!(Flags & BasicSymbolRef::SF_Global))
495     return true;
496   if (Flags & BasicSymbolRef::SF_FormatSpecific)
497     return true;
498   return false;
499 }
500 
501 void BitcodeFile::parse(DenseSet<StringRef> &ComdatGroups) {
502   LLVMContext Context;
503   std::unique_ptr<IRObjectFile> Obj = check(IRObjectFile::create(MB, Context));
504   const Module &M = Obj->getModule();
505 
506   DenseSet<const Comdat *> KeptComdats;
507   for (const auto &P : M.getComdatSymbolTable()) {
508     StringRef N = Saver.save(P.first());
509     if (ComdatGroups.insert(N).second)
510       KeptComdats.insert(&P.second);
511   }
512 
513   for (const BasicSymbolRef &Sym : Obj->symbols())
514     if (!shouldSkip(Sym))
515       SymbolBodies.push_back(createSymbolBody(KeptComdats, *Obj, Sym));
516 }
517 
518 template <typename T>
519 static std::unique_ptr<InputFile> createELFFileAux(MemoryBufferRef MB) {
520   std::unique_ptr<T> Ret = llvm::make_unique<T>(MB);
521 
522   if (!Config->FirstElf)
523     Config->FirstElf = Ret.get();
524 
525   if (Config->EKind == ELFNoneKind) {
526     Config->EKind = Ret->getELFKind();
527     Config->EMachine = Ret->getEMachine();
528   }
529 
530   return std::move(Ret);
531 }
532 
533 template <template <class> class T>
534 static std::unique_ptr<InputFile> createELFFile(MemoryBufferRef MB) {
535   std::pair<unsigned char, unsigned char> Type = getElfArchType(MB.getBuffer());
536   if (Type.second != ELF::ELFDATA2LSB && Type.second != ELF::ELFDATA2MSB)
537     fatal("invalid data encoding: " + MB.getBufferIdentifier());
538 
539   if (Type.first == ELF::ELFCLASS32) {
540     if (Type.second == ELF::ELFDATA2LSB)
541       return createELFFileAux<T<ELF32LE>>(MB);
542     return createELFFileAux<T<ELF32BE>>(MB);
543   }
544   if (Type.first == ELF::ELFCLASS64) {
545     if (Type.second == ELF::ELFDATA2LSB)
546       return createELFFileAux<T<ELF64LE>>(MB);
547     return createELFFileAux<T<ELF64BE>>(MB);
548   }
549   fatal("invalid file class: " + MB.getBufferIdentifier());
550 }
551 
552 std::unique_ptr<InputFile> elf::createObjectFile(MemoryBufferRef MB,
553                                                  StringRef ArchiveName) {
554   using namespace sys::fs;
555   std::unique_ptr<InputFile> F;
556   if (identify_magic(MB.getBuffer()) == file_magic::bitcode)
557     F.reset(new BitcodeFile(MB));
558   else
559     F = createELFFile<ObjectFile>(MB);
560   F->ArchiveName = ArchiveName;
561   return F;
562 }
563 
564 std::unique_ptr<InputFile> elf::createSharedFile(MemoryBufferRef MB) {
565   return createELFFile<SharedFile>(MB);
566 }
567 
568 template class elf::ELFFileBase<ELF32LE>;
569 template class elf::ELFFileBase<ELF32BE>;
570 template class elf::ELFFileBase<ELF64LE>;
571 template class elf::ELFFileBase<ELF64BE>;
572 
573 template class elf::ObjectFile<ELF32LE>;
574 template class elf::ObjectFile<ELF32BE>;
575 template class elf::ObjectFile<ELF64LE>;
576 template class elf::ObjectFile<ELF64BE>;
577 
578 template class elf::SharedFile<ELF32LE>;
579 template class elf::SharedFile<ELF32BE>;
580 template class elf::SharedFile<ELF64LE>;
581 template class elf::SharedFile<ELF64BE>;
582