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