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 "InputSection.h"
12 #include "Error.h"
13 #include "Symbols.h"
14 #include "llvm/ADT/STLExtras.h"
15 
16 using namespace llvm;
17 using namespace llvm::ELF;
18 using namespace llvm::object;
19 using namespace llvm::sys::fs;
20 
21 using namespace lld;
22 using namespace lld::elf2;
23 
24 namespace {
25 class ECRAII {
26   std::error_code EC;
27 
28 public:
29   std::error_code &getEC() { return EC; }
30   ~ECRAII() { error(EC); }
31 };
32 }
33 
34 template <class ELFT>
35 ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef M)
36     : InputFile(K, M), ELFObj(MB.getBuffer(), ECRAII().getEC()) {}
37 
38 template <class ELFT>
39 typename ELFFileBase<ELFT>::Elf_Sym_Range
40 ELFFileBase<ELFT>::getSymbolsHelper(bool Local) {
41   if (!Symtab)
42     return Elf_Sym_Range(nullptr, nullptr);
43   Elf_Sym_Range Syms = ELFObj.symbols(Symtab);
44   uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
45   uint32_t FirstNonLocal = Symtab->sh_info;
46   if (FirstNonLocal > NumSymbols)
47     error("Invalid sh_info in symbol table");
48   if (!Local)
49     return make_range(Syms.begin() + FirstNonLocal, Syms.end());
50   // +1 to skip over dummy symbol.
51   return make_range(Syms.begin() + 1, Syms.begin() + FirstNonLocal);
52 }
53 
54 template <class ELFT>
55 uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
56   uint32_t Index = Sym.st_shndx;
57   if (Index == ELF::SHN_XINDEX)
58     Index = this->ELFObj.getExtendedSymbolTableIndex(&Sym, this->Symtab,
59                                                      SymtabSHNDX);
60   else if (Index == ELF::SHN_UNDEF || Index >= ELF::SHN_LORESERVE)
61     return 0;
62 
63   if (!Index)
64     error("Invalid section index");
65   return Index;
66 }
67 
68 template <class ELFT> void ELFFileBase<ELFT>::initStringTable() {
69   if (!Symtab)
70     return;
71   ErrorOr<StringRef> StringTableOrErr = ELFObj.getStringTableForSymtab(*Symtab);
72   error(StringTableOrErr.getError());
73   StringTable = *StringTableOrErr;
74 }
75 
76 template <class ELFT>
77 typename ELFFileBase<ELFT>::Elf_Sym_Range
78 ELFFileBase<ELFT>::getNonLocalSymbols() {
79   return getSymbolsHelper(false);
80 }
81 
82 template <class ELFT>
83 ObjectFile<ELFT>::ObjectFile(MemoryBufferRef M)
84     : ELFFileBase<ELFT>(Base::ObjectKind, M) {}
85 
86 template <class ELFT>
87 typename ObjectFile<ELFT>::Elf_Sym_Range ObjectFile<ELFT>::getLocalSymbols() {
88   return this->getSymbolsHelper(true);
89 }
90 
91 template <class ELFT>
92 const typename ObjectFile<ELFT>::Elf_Sym *
93 ObjectFile<ELFT>::getLocalSymbol(uintX_t SymIndex) {
94   uint32_t FirstNonLocal = this->Symtab->sh_info;
95   if (SymIndex >= FirstNonLocal)
96     return nullptr;
97   Elf_Sym_Range Syms = this->ELFObj.symbols(this->Symtab);
98   return Syms.begin() + SymIndex;
99 }
100 
101 template <class ELFT>
102 void elf2::ObjectFile<ELFT>::parse(DenseSet<StringRef> &Comdats) {
103   // Read section and symbol tables.
104   initializeSections(Comdats);
105   initializeSymbols();
106 }
107 
108 template <class ELFT>
109 StringRef ObjectFile<ELFT>::getShtGroupSignature(const Elf_Shdr &Sec) {
110   const ELFFile<ELFT> &Obj = this->ELFObj;
111   uint32_t SymtabdSectionIndex = Sec.sh_link;
112   ErrorOr<const Elf_Shdr *> SecOrErr = Obj.getSection(SymtabdSectionIndex);
113   error(SecOrErr);
114   const Elf_Shdr *SymtabSec = *SecOrErr;
115   uint32_t SymIndex = Sec.sh_info;
116   const Elf_Sym *Sym = Obj.getSymbol(SymtabSec, SymIndex);
117   ErrorOr<StringRef> StringTableOrErr = Obj.getStringTableForSymtab(*SymtabSec);
118   error(StringTableOrErr);
119   ErrorOr<StringRef> SignatureOrErr = Sym->getName(*StringTableOrErr);
120   error(SignatureOrErr);
121   return *SignatureOrErr;
122 }
123 
124 template <class ELFT>
125 ArrayRef<typename ObjectFile<ELFT>::GroupEntryType>
126 ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
127   const ELFFile<ELFT> &Obj = this->ELFObj;
128   ErrorOr<ArrayRef<GroupEntryType>> EntriesOrErr =
129       Obj.template getSectionContentsAsArray<GroupEntryType>(&Sec);
130   error(EntriesOrErr.getError());
131   ArrayRef<GroupEntryType> Entries = *EntriesOrErr;
132   if (Entries.empty() || Entries[0] != GRP_COMDAT)
133     error("Unsupported SHT_GROUP format");
134   return Entries.slice(1);
135 }
136 
137 template <class ELFT>
138 static bool shouldMerge(const typename ELFFile<ELFT>::Elf_Shdr &Sec) {
139   typedef typename ELFFile<ELFT>::uintX_t uintX_t;
140   uintX_t Flags = Sec.sh_flags;
141   if (!(Flags & SHF_MERGE))
142     return false;
143   if (Flags & SHF_WRITE)
144     error("Writable SHF_MERGE sections are not supported");
145   uintX_t EntSize = Sec.sh_entsize;
146   if (!EntSize || Sec.sh_size % EntSize)
147     error("SHF_MERGE section size must be a multiple of sh_entsize");
148 
149   // Don't try to merge if the aligment is larger than the sh_entsize.
150   //
151   // If this is not a SHF_STRINGS, we would need to pad after every entity. It
152   // would be equivalent for the producer of the .o to just set a larger
153   // sh_entsize.
154   //
155   // If this is a SHF_STRINGS, the larger alignment makes sense. Unfortunately
156   // it would complicate tail merging. This doesn't seem that common to
157   // justify the effort.
158   if (Sec.sh_addralign > EntSize)
159     return false;
160 
161   return true;
162 }
163 
164 template <class ELFT>
165 void elf2::ObjectFile<ELFT>::initializeSections(DenseSet<StringRef> &Comdats) {
166   uint64_t Size = this->ELFObj.getNumSections();
167   Sections.resize(Size);
168   unsigned I = -1;
169   const ELFFile<ELFT> &Obj = this->ELFObj;
170   for (const Elf_Shdr &Sec : Obj.sections()) {
171     ++I;
172     if (Sections[I] == &InputSection<ELFT>::Discarded)
173       continue;
174 
175     switch (Sec.sh_type) {
176     case SHT_GROUP:
177       Sections[I] = &InputSection<ELFT>::Discarded;
178       if (Comdats.insert(getShtGroupSignature(Sec)).second)
179         continue;
180       for (GroupEntryType E : getShtGroupEntries(Sec)) {
181         uint32_t SecIndex = E;
182         if (SecIndex >= Size)
183           error("Invalid section index in group");
184         Sections[SecIndex] = &InputSection<ELFT>::Discarded;
185       }
186       break;
187     case SHT_SYMTAB:
188       this->Symtab = &Sec;
189       break;
190     case SHT_SYMTAB_SHNDX: {
191       ErrorOr<ArrayRef<Elf_Word>> ErrorOrTable = Obj.getSHNDXTable(Sec);
192       error(ErrorOrTable);
193       this->SymtabSHNDX = *ErrorOrTable;
194       break;
195     }
196     case SHT_STRTAB:
197     case SHT_NULL:
198       break;
199     case SHT_RELA:
200     case SHT_REL: {
201       uint32_t RelocatedSectionIndex = Sec.sh_info;
202       if (RelocatedSectionIndex >= Size)
203         error("Invalid relocated section index");
204       InputSectionBase<ELFT> *RelocatedSection =
205           Sections[RelocatedSectionIndex];
206       if (!RelocatedSection)
207         error("Unsupported relocation reference");
208       if (auto *S = dyn_cast<InputSection<ELFT>>(RelocatedSection)) {
209         S->RelocSections.push_back(&Sec);
210       } else if (auto *S = dyn_cast<EHInputSection<ELFT>>(RelocatedSection)) {
211         if (S->RelocSection)
212           error("Multiple relocation sections to .eh_frame are not supported");
213         S->RelocSection = &Sec;
214       } else {
215         error("Relocations pointing to SHF_MERGE are not supported");
216       }
217       break;
218     }
219     default: {
220       ErrorOr<StringRef> NameOrErr = this->ELFObj.getSectionName(&Sec);
221       error(NameOrErr);
222       if (*NameOrErr == ".note.GNU-stack")
223         Sections[I] = &InputSection<ELFT>::Discarded;
224       else if (*NameOrErr == ".eh_frame")
225         Sections[I] = new (this->Alloc) EHInputSection<ELFT>(this, &Sec);
226       else if (shouldMerge<ELFT>(Sec))
227         Sections[I] = new (this->Alloc) MergeInputSection<ELFT>(this, &Sec);
228       else
229         Sections[I] = new (this->Alloc) InputSection<ELFT>(this, &Sec);
230       break;
231     }
232     }
233   }
234 }
235 
236 template <class ELFT> void elf2::ObjectFile<ELFT>::initializeSymbols() {
237   this->initStringTable();
238   Elf_Sym_Range Syms = this->getNonLocalSymbols();
239   uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
240   this->SymbolBodies.reserve(NumSymbols);
241   for (const Elf_Sym &Sym : Syms)
242     this->SymbolBodies.push_back(createSymbolBody(this->StringTable, &Sym));
243 }
244 
245 template <class ELFT>
246 InputSectionBase<ELFT> *
247 elf2::ObjectFile<ELFT>::getSection(const Elf_Sym &Sym) const {
248   uint32_t Index = this->getSectionIndex(Sym);
249   if (Index == 0)
250     return nullptr;
251   if (Index >= Sections.size() || !Sections[Index])
252     error("Invalid section index");
253   return Sections[Index];
254 }
255 
256 template <class ELFT>
257 SymbolBody *elf2::ObjectFile<ELFT>::createSymbolBody(StringRef StringTable,
258                                                      const Elf_Sym *Sym) {
259   ErrorOr<StringRef> NameOrErr = Sym->getName(StringTable);
260   error(NameOrErr.getError());
261   StringRef Name = *NameOrErr;
262 
263   switch (Sym->st_shndx) {
264   case SHN_ABS:
265     return new (this->Alloc) DefinedAbsolute<ELFT>(Name, *Sym);
266   case SHN_UNDEF:
267     return new (this->Alloc) Undefined<ELFT>(Name, *Sym);
268   case SHN_COMMON:
269     return new (this->Alloc) DefinedCommon<ELFT>(Name, *Sym);
270   }
271 
272   switch (Sym->getBinding()) {
273   default:
274     error("unexpected binding");
275   case STB_GLOBAL:
276   case STB_WEAK:
277   case STB_GNU_UNIQUE: {
278     InputSectionBase<ELFT> *Sec = getSection(*Sym);
279     if (Sec == &InputSection<ELFT>::Discarded)
280       return new (this->Alloc) Undefined<ELFT>(Name, *Sym);
281     return new (this->Alloc) DefinedRegular<ELFT>(Name, *Sym, *Sec);
282   }
283   }
284 }
285 
286 static std::unique_ptr<Archive> openArchive(MemoryBufferRef MB) {
287   ErrorOr<std::unique_ptr<Archive>> ArchiveOrErr = Archive::create(MB);
288   error(ArchiveOrErr, "Failed to parse archive");
289   return std::move(*ArchiveOrErr);
290 }
291 
292 void ArchiveFile::parse() {
293   File = openArchive(MB);
294 
295   // Allocate a buffer for Lazy objects.
296   size_t NumSyms = File->getNumberOfSymbols();
297   LazySymbols.reserve(NumSyms);
298 
299   // Read the symbol table to construct Lazy objects.
300   for (const Archive::Symbol &Sym : File->symbols())
301     LazySymbols.emplace_back(this, Sym);
302 }
303 
304 // Returns a buffer pointing to a member file containing a given symbol.
305 MemoryBufferRef ArchiveFile::getMember(const Archive::Symbol *Sym) {
306   ErrorOr<Archive::Child> COrErr = Sym->getMember();
307   error(COrErr, "Could not get the member for symbol " + Sym->getName());
308   const Archive::Child &C = *COrErr;
309 
310   if (!Seen.insert(C.getChildOffset()).second)
311     return MemoryBufferRef();
312 
313   ErrorOr<MemoryBufferRef> Ret = C.getMemoryBufferRef();
314   error(Ret, "Could not get the buffer for the member defining symbol " +
315                  Sym->getName());
316   return *Ret;
317 }
318 
319 std::vector<MemoryBufferRef> ArchiveFile::getMembers() {
320   File = openArchive(MB);
321 
322   std::vector<MemoryBufferRef> Result;
323   for (auto &ChildOrErr : File->children()) {
324     error(ChildOrErr,
325           "Could not get the child of the archive " + File->getFileName());
326     const Archive::Child Child(*ChildOrErr);
327     ErrorOr<MemoryBufferRef> MbOrErr = Child.getMemoryBufferRef();
328     error(MbOrErr, "Could not get the buffer for a child of the archive " +
329                        File->getFileName());
330     Result.push_back(MbOrErr.get());
331   }
332   return Result;
333 }
334 
335 template <class ELFT>
336 SharedFile<ELFT>::SharedFile(MemoryBufferRef M)
337     : ELFFileBase<ELFT>(Base::SharedKind, M) {
338   AsNeeded = Config->AsNeeded;
339 }
340 
341 template <class ELFT>
342 const typename ELFFile<ELFT>::Elf_Shdr *
343 SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const {
344   uint32_t Index = this->getSectionIndex(Sym);
345   if (Index == 0)
346     return nullptr;
347   ErrorOr<const Elf_Shdr *> Ret = this->ELFObj.getSection(Index);
348   error(Ret);
349   return *Ret;
350 }
351 
352 template <class ELFT> void SharedFile<ELFT>::parseSoName() {
353   typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
354   typedef typename ELFFile<ELFT>::uintX_t uintX_t;
355   const Elf_Shdr *DynamicSec = nullptr;
356 
357   const ELFFile<ELFT> Obj = this->ELFObj;
358   for (const Elf_Shdr &Sec : Obj.sections()) {
359     switch (Sec.sh_type) {
360     default:
361       continue;
362     case SHT_DYNSYM:
363       this->Symtab = &Sec;
364       break;
365     case SHT_DYNAMIC:
366       DynamicSec = &Sec;
367       break;
368     case SHT_SYMTAB_SHNDX: {
369       ErrorOr<ArrayRef<Elf_Word>> ErrorOrTable = Obj.getSHNDXTable(Sec);
370       error(ErrorOrTable);
371       this->SymtabSHNDX = *ErrorOrTable;
372       break;
373     }
374     }
375   }
376 
377   this->initStringTable();
378   this->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         error("Invalid DT_SONAME entry");
391       this->SoName = StringRef(this->StringTable.data() + Val);
392       return;
393     }
394   }
395 }
396 
397 template <class ELFT> void SharedFile<ELFT>::parse() {
398   Elf_Sym_Range Syms = this->getNonLocalSymbols();
399   uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
400   SymbolBodies.reserve(NumSymbols);
401   for (const Elf_Sym &Sym : Syms) {
402     ErrorOr<StringRef> NameOrErr = Sym.getName(this->StringTable);
403     error(NameOrErr.getError());
404     StringRef Name = *NameOrErr;
405 
406     if (Sym.isUndefined())
407       Undefs.push_back(Name);
408     else
409       SymbolBodies.emplace_back(this, Name, Sym);
410   }
411 }
412 
413 template <typename T>
414 static std::unique_ptr<InputFile> createELFFileAux(MemoryBufferRef MB) {
415   std::unique_ptr<T> Ret = llvm::make_unique<T>(MB);
416 
417   if (!Config->FirstElf)
418     Config->FirstElf = Ret.get();
419 
420   if (Config->EKind == ELFNoneKind) {
421     Config->EKind = Ret->getELFKind();
422     Config->EMachine = Ret->getEMachine();
423   }
424 
425   return std::move(Ret);
426 }
427 
428 template <template <class> class T>
429 std::unique_ptr<InputFile> lld::elf2::createELFFile(MemoryBufferRef MB) {
430   using namespace llvm;
431 
432   std::pair<unsigned char, unsigned char> Type =
433     object::getElfArchType(MB.getBuffer());
434   if (Type.second != ELF::ELFDATA2LSB && Type.second != ELF::ELFDATA2MSB)
435     error("Invalid data encoding: " + MB.getBufferIdentifier());
436 
437   if (Type.first == ELF::ELFCLASS32) {
438     if (Type.second == ELF::ELFDATA2LSB)
439       return createELFFileAux<T<object::ELF32LE>>(MB);
440     return createELFFileAux<T<object::ELF32BE>>(MB);
441   }
442   if (Type.first == ELF::ELFCLASS64) {
443     if (Type.second == ELF::ELFDATA2LSB)
444       return createELFFileAux<T<object::ELF64LE>>(MB);
445     return createELFFileAux<T<object::ELF64BE>>(MB);
446   }
447   error("Invalid file class: " + MB.getBufferIdentifier());
448 }
449 
450 namespace lld {
451 namespace elf2 {
452 template class ELFFileBase<llvm::object::ELF32LE>;
453 template class ELFFileBase<llvm::object::ELF32BE>;
454 template class ELFFileBase<llvm::object::ELF64LE>;
455 template class ELFFileBase<llvm::object::ELF64BE>;
456 
457 template class ObjectFile<llvm::object::ELF32LE>;
458 template class ObjectFile<llvm::object::ELF32BE>;
459 template class ObjectFile<llvm::object::ELF64LE>;
460 template class ObjectFile<llvm::object::ELF64BE>;
461 
462 template class SharedFile<llvm::object::ELF32LE>;
463 template class SharedFile<llvm::object::ELF32BE>;
464 template class SharedFile<llvm::object::ELF64LE>;
465 template class SharedFile<llvm::object::ELF64BE>;
466 
467 template std::unique_ptr<InputFile> createELFFile<ObjectFile>(MemoryBufferRef);
468 template std::unique_ptr<InputFile> createELFFile<SharedFile>(MemoryBufferRef);
469 }
470 }
471