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::elf;
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 elf::ObjectFile<ELFT>::ObjectFile(MemoryBufferRef M)
90     : ELFFileBase<ELFT>(Base::ObjectKind, M) {}
91 
92 template <class ELFT>
93 typename elf::ObjectFile<ELFT>::Elf_Sym_Range
94 elf::ObjectFile<ELFT>::getLocalSymbols() {
95   return this->getSymbolsHelper(true);
96 }
97 
98 template <class ELFT> uint32_t elf::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 elf::ObjectFile<ELFT>::Elf_Sym *
106 elf::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 elf::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 elf::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 elf::ObjectFile<ELFT>::uint32_X>
142 elf::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 elf::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 (Config->Relocatable) {
230         // For -r, relocation sections are handled as regular input sections.
231         Sections[I] = new (Alloc) InputSection<ELFT>(this, &Sec);
232       } else if (auto *S = dyn_cast<InputSection<ELFT>>(RelocatedSection)) {
233         S->RelocSections.push_back(&Sec);
234       } else if (auto *S = dyn_cast<EHInputSection<ELFT>>(RelocatedSection)) {
235         if (S->RelocSection)
236           fatal("Multiple relocation sections to .eh_frame are not supported");
237         S->RelocSection = &Sec;
238       } else {
239         fatal("Relocations pointing to SHF_MERGE are not supported");
240       }
241       break;
242     }
243     default:
244       Sections[I] = createInputSection(Sec);
245     }
246   }
247 }
248 
249 template <class ELFT>
250 InputSectionBase<ELFT> *
251 elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
252   ErrorOr<StringRef> NameOrErr = this->ELFObj.getSectionName(&Sec);
253   fatal(NameOrErr);
254   StringRef Name = *NameOrErr;
255 
256   // .note.GNU-stack is a marker section to control the presence of
257   // PT_GNU_STACK segment in outputs. Since the presence of the segment
258   // is controlled only by the command line option (-z execstack) in LLD,
259   // .note.GNU-stack is ignored.
260   if (Name == ".note.GNU-stack")
261     return InputSection<ELFT>::Discarded;
262 
263   // A MIPS object file has a special section that contains register
264   // usage info, which needs to be handled by the linker specially.
265   if (Config->EMachine == EM_MIPS && Name == ".reginfo") {
266     MipsReginfo = new (Alloc) MipsReginfoInputSection<ELFT>(this, &Sec);
267     return MipsReginfo;
268   }
269 
270   if (Name == ".eh_frame")
271     return new (EHAlloc.Allocate()) EHInputSection<ELFT>(this, &Sec);
272   if (shouldMerge<ELFT>(Sec))
273     return new (MAlloc.Allocate()) MergeInputSection<ELFT>(this, &Sec);
274   return new (Alloc) InputSection<ELFT>(this, &Sec);
275 }
276 
277 template <class ELFT> void elf::ObjectFile<ELFT>::initializeSymbols() {
278   this->initStringTable();
279   Elf_Sym_Range Syms = this->getNonLocalSymbols();
280   uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
281   SymbolBodies.reserve(NumSymbols);
282   for (const Elf_Sym &Sym : Syms)
283     SymbolBodies.push_back(createSymbolBody(&Sym));
284 }
285 
286 template <class ELFT>
287 InputSectionBase<ELFT> *
288 elf::ObjectFile<ELFT>::getSection(const Elf_Sym &Sym) const {
289   uint32_t Index = this->getSectionIndex(Sym);
290   if (Index == 0)
291     return nullptr;
292   if (Index >= Sections.size() || !Sections[Index])
293     fatal("Invalid section index");
294   InputSectionBase<ELFT> *S = Sections[Index];
295   if (S == InputSectionBase<ELFT>::Discarded)
296     return S;
297   return S->Repl;
298 }
299 
300 template <class ELFT>
301 SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
302   ErrorOr<StringRef> NameOrErr = Sym->getName(this->StringTable);
303   fatal(NameOrErr);
304   StringRef Name = *NameOrErr;
305 
306   switch (Sym->st_shndx) {
307   case SHN_UNDEF:
308     return new (Alloc) UndefinedElf<ELFT>(Name, *Sym);
309   case SHN_COMMON:
310     return new (Alloc) DefinedCommon(Name, Sym->st_size, Sym->st_value,
311                                      Sym->getBinding() == llvm::ELF::STB_WEAK,
312                                      Sym->getVisibility());
313   }
314 
315   switch (Sym->getBinding()) {
316   default:
317     fatal("unexpected binding");
318   case STB_GLOBAL:
319   case STB_WEAK:
320   case STB_GNU_UNIQUE: {
321     InputSectionBase<ELFT> *Sec = getSection(*Sym);
322     if (Sec == InputSection<ELFT>::Discarded)
323       return new (Alloc) UndefinedElf<ELFT>(Name, *Sym);
324     return new (Alloc) DefinedRegular<ELFT>(Name, *Sym, Sec);
325   }
326   }
327 }
328 
329 void ArchiveFile::parse() {
330   ErrorOr<std::unique_ptr<Archive>> FileOrErr = Archive::create(MB);
331   fatal(FileOrErr, "Failed to parse archive");
332   File = std::move(*FileOrErr);
333 
334   // Allocate a buffer for Lazy objects.
335   size_t NumSyms = File->getNumberOfSymbols();
336   LazySymbols.reserve(NumSyms);
337 
338   // Read the symbol table to construct Lazy objects.
339   for (const Archive::Symbol &Sym : File->symbols())
340     LazySymbols.emplace_back(this, Sym);
341 }
342 
343 // Returns a buffer pointing to a member file containing a given symbol.
344 MemoryBufferRef ArchiveFile::getMember(const Archive::Symbol *Sym) {
345   ErrorOr<Archive::Child> COrErr = Sym->getMember();
346   fatal(COrErr, "Could not get the member for symbol " + Sym->getName());
347   const Archive::Child &C = *COrErr;
348 
349   if (!Seen.insert(C.getChildOffset()).second)
350     return MemoryBufferRef();
351 
352   ErrorOr<MemoryBufferRef> RefOrErr = C.getMemoryBufferRef();
353   if (!RefOrErr)
354     fatal(RefOrErr, "Could not get the buffer for the member defining symbol " +
355                         Sym->getName());
356   return *RefOrErr;
357 }
358 
359 template <class ELFT>
360 SharedFile<ELFT>::SharedFile(MemoryBufferRef M)
361     : ELFFileBase<ELFT>(Base::SharedKind, M), AsNeeded(Config->AsNeeded) {}
362 
363 template <class ELFT>
364 const typename ELFFile<ELFT>::Elf_Shdr *
365 SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const {
366   uint32_t Index = this->getSectionIndex(Sym);
367   if (Index == 0)
368     return nullptr;
369   ErrorOr<const Elf_Shdr *> Ret = this->ELFObj.getSection(Index);
370   fatal(Ret);
371   return *Ret;
372 }
373 
374 // Partially parse the shared object file so that we can call
375 // getSoName on this object.
376 template <class ELFT> void SharedFile<ELFT>::parseSoName() {
377   typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
378   typedef typename ELFFile<ELFT>::uintX_t uintX_t;
379   const Elf_Shdr *DynamicSec = nullptr;
380 
381   const ELFFile<ELFT> Obj = this->ELFObj;
382   for (const Elf_Shdr &Sec : Obj.sections()) {
383     switch (Sec.sh_type) {
384     default:
385       continue;
386     case SHT_DYNSYM:
387       this->Symtab = &Sec;
388       break;
389     case SHT_DYNAMIC:
390       DynamicSec = &Sec;
391       break;
392     case SHT_SYMTAB_SHNDX: {
393       ErrorOr<ArrayRef<Elf_Word>> ErrorOrTable = Obj.getSHNDXTable(Sec);
394       fatal(ErrorOrTable);
395       this->SymtabSHNDX = *ErrorOrTable;
396       break;
397     }
398     }
399   }
400 
401   this->initStringTable();
402   SoName = this->getName();
403 
404   if (!DynamicSec)
405     return;
406   auto *Begin =
407       reinterpret_cast<const Elf_Dyn *>(Obj.base() + DynamicSec->sh_offset);
408   const Elf_Dyn *End = Begin + DynamicSec->sh_size / sizeof(Elf_Dyn);
409 
410   for (const Elf_Dyn &Dyn : make_range(Begin, End)) {
411     if (Dyn.d_tag == DT_SONAME) {
412       uintX_t Val = Dyn.getVal();
413       if (Val >= this->StringTable.size())
414         fatal("Invalid DT_SONAME entry");
415       SoName = StringRef(this->StringTable.data() + Val);
416       return;
417     }
418   }
419 }
420 
421 // Fully parse the shared object file. This must be called after parseSoName().
422 template <class ELFT> void SharedFile<ELFT>::parseRest() {
423   Elf_Sym_Range Syms = this->getNonLocalSymbols();
424   uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
425   SymbolBodies.reserve(NumSymbols);
426   for (const Elf_Sym &Sym : Syms) {
427     ErrorOr<StringRef> NameOrErr = Sym.getName(this->StringTable);
428     fatal(NameOrErr.getError());
429     StringRef Name = *NameOrErr;
430 
431     if (Sym.isUndefined())
432       Undefs.push_back(Name);
433     else
434       SymbolBodies.emplace_back(this, Name, Sym);
435   }
436 }
437 
438 BitcodeFile::BitcodeFile(MemoryBufferRef M) : InputFile(BitcodeKind, M) {}
439 
440 bool BitcodeFile::classof(const InputFile *F) {
441   return F->kind() == BitcodeKind;
442 }
443 
444 void BitcodeFile::parse() {
445   LLVMContext Context;
446   ErrorOr<std::unique_ptr<IRObjectFile>> ObjOrErr =
447       IRObjectFile::create(MB, Context);
448   fatal(ObjOrErr);
449   IRObjectFile &Obj = **ObjOrErr;
450   for (const BasicSymbolRef &Sym : Obj.symbols()) {
451     SmallString<64> Name;
452     raw_svector_ostream OS(Name);
453     Sym.printName(OS);
454     StringRef NameRef = Saver.save(StringRef(Name));
455     SymbolBody *Body;
456     uint32_t Flags = Sym.getFlags();
457     if (Flags & BasicSymbolRef::SF_Undefined)
458       Body = new (Alloc) Undefined(NameRef, false, STV_DEFAULT, false);
459     else
460       Body =
461           new (Alloc) DefinedBitcode(NameRef, Flags & BasicSymbolRef::SF_Weak);
462     SymbolBodies.push_back(Body);
463   }
464 }
465 
466 template <typename T>
467 static std::unique_ptr<InputFile> createELFFileAux(MemoryBufferRef MB) {
468   std::unique_ptr<T> Ret = llvm::make_unique<T>(MB);
469 
470   if (!Config->FirstElf)
471     Config->FirstElf = Ret.get();
472 
473   if (Config->EKind == ELFNoneKind) {
474     Config->EKind = Ret->getELFKind();
475     Config->EMachine = Ret->getEMachine();
476   }
477 
478   return std::move(Ret);
479 }
480 
481 template <template <class> class T>
482 static std::unique_ptr<InputFile> createELFFile(MemoryBufferRef MB) {
483   std::pair<unsigned char, unsigned char> Type = getElfArchType(MB.getBuffer());
484   if (Type.second != ELF::ELFDATA2LSB && Type.second != ELF::ELFDATA2MSB)
485     fatal("Invalid data encoding: " + MB.getBufferIdentifier());
486 
487   if (Type.first == ELF::ELFCLASS32) {
488     if (Type.second == ELF::ELFDATA2LSB)
489       return createELFFileAux<T<ELF32LE>>(MB);
490     return createELFFileAux<T<ELF32BE>>(MB);
491   }
492   if (Type.first == ELF::ELFCLASS64) {
493     if (Type.second == ELF::ELFDATA2LSB)
494       return createELFFileAux<T<ELF64LE>>(MB);
495     return createELFFileAux<T<ELF64BE>>(MB);
496   }
497   fatal("Invalid file class: " + MB.getBufferIdentifier());
498 }
499 
500 std::unique_ptr<InputFile> elf::createObjectFile(MemoryBufferRef MB,
501                                                  StringRef ArchiveName) {
502   using namespace sys::fs;
503   std::unique_ptr<InputFile> F;
504   if (identify_magic(MB.getBuffer()) == file_magic::bitcode)
505     F.reset(new BitcodeFile(MB));
506   else
507     F = createELFFile<ObjectFile>(MB);
508   F->ArchiveName = ArchiveName;
509   return F;
510 }
511 
512 std::unique_ptr<InputFile> elf::createSharedFile(MemoryBufferRef MB) {
513   return createELFFile<SharedFile>(MB);
514 }
515 
516 template class elf::ELFFileBase<ELF32LE>;
517 template class elf::ELFFileBase<ELF32BE>;
518 template class elf::ELFFileBase<ELF64LE>;
519 template class elf::ELFFileBase<ELF64BE>;
520 
521 template class elf::ObjectFile<ELF32LE>;
522 template class elf::ObjectFile<ELF32BE>;
523 template class elf::ObjectFile<ELF64LE>;
524 template class elf::ObjectFile<ELF64BE>;
525 
526 template class elf::SharedFile<ELF32LE>;
527 template class elf::SharedFile<ELF32BE>;
528 template class elf::SharedFile<ELF64LE>;
529 template class elf::SharedFile<ELF64BE>;
530