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