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