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(&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(const Elf_Sym *Sym) {
285   ErrorOr<StringRef> NameOrErr = Sym->getName(this->StringTable);
286   error(NameOrErr);
287   StringRef Name = *NameOrErr;
288 
289   switch (Sym->st_shndx) {
290   case SHN_UNDEF:
291     return new (Alloc) UndefinedElf<ELFT>(Name, *Sym);
292   case SHN_COMMON:
293     return new (Alloc) DefinedCommon(Name, Sym->st_size, Sym->st_value,
294                                      Sym->getBinding() == llvm::ELF::STB_WEAK,
295                                      Sym->getVisibility());
296   }
297 
298   switch (Sym->getBinding()) {
299   default:
300     error("unexpected binding");
301   case STB_GLOBAL:
302   case STB_WEAK:
303   case STB_GNU_UNIQUE: {
304     InputSectionBase<ELFT> *Sec = getSection(*Sym);
305     if (Sec == &InputSection<ELFT>::Discarded)
306       return new (Alloc) UndefinedElf<ELFT>(Name, *Sym);
307     return new (Alloc) DefinedRegular<ELFT>(Name, *Sym, Sec);
308   }
309   }
310 }
311 
312 void ArchiveFile::parse() {
313   ErrorOr<std::unique_ptr<Archive>> FileOrErr = Archive::create(MB);
314   error(FileOrErr, "Failed to parse archive");
315   File = std::move(*FileOrErr);
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   ErrorOr<Archive::Child> COrErr = Sym->getMember();
329   error(COrErr, "Could not get the member for symbol " + Sym->getName());
330   const Archive::Child &C = *COrErr;
331 
332   if (!Seen.insert(C.getChildOffset()).second)
333     return MemoryBufferRef();
334 
335   ErrorOr<MemoryBufferRef> RefOrErr = C.getMemoryBufferRef();
336   if (!RefOrErr)
337     error(RefOrErr, "Could not get the buffer for the member defining symbol " +
338           Sym->getName());
339   return *RefOrErr;
340 }
341 
342 template <class ELFT>
343 SharedFile<ELFT>::SharedFile(MemoryBufferRef M)
344     : ELFFileBase<ELFT>(Base::SharedKind, M), AsNeeded(Config->AsNeeded) {}
345 
346 template <class ELFT>
347 const typename ELFFile<ELFT>::Elf_Shdr *
348 SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const {
349   uint32_t Index = this->getSectionIndex(Sym);
350   if (Index == 0)
351     return nullptr;
352   ErrorOr<const Elf_Shdr *> Ret = this->ELFObj.getSection(Index);
353   error(Ret);
354   return *Ret;
355 }
356 
357 // Partially parse the shared object file so that we can call
358 // getSoName on this object.
359 template <class ELFT> void SharedFile<ELFT>::parseSoName() {
360   typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
361   typedef typename ELFFile<ELFT>::uintX_t uintX_t;
362   const Elf_Shdr *DynamicSec = nullptr;
363 
364   const ELFFile<ELFT> Obj = this->ELFObj;
365   for (const Elf_Shdr &Sec : Obj.sections()) {
366     switch (Sec.sh_type) {
367     default:
368       continue;
369     case SHT_DYNSYM:
370       this->Symtab = &Sec;
371       break;
372     case SHT_DYNAMIC:
373       DynamicSec = &Sec;
374       break;
375     case SHT_SYMTAB_SHNDX: {
376       ErrorOr<ArrayRef<Elf_Word>> ErrorOrTable = Obj.getSHNDXTable(Sec);
377       error(ErrorOrTable);
378       this->SymtabSHNDX = *ErrorOrTable;
379       break;
380     }
381     }
382   }
383 
384   this->initStringTable();
385   SoName = this->getName();
386 
387   if (!DynamicSec)
388     return;
389   auto *Begin =
390       reinterpret_cast<const Elf_Dyn *>(Obj.base() + DynamicSec->sh_offset);
391   const Elf_Dyn *End = Begin + DynamicSec->sh_size / sizeof(Elf_Dyn);
392 
393   for (const Elf_Dyn &Dyn : make_range(Begin, End)) {
394     if (Dyn.d_tag == DT_SONAME) {
395       uintX_t Val = Dyn.getVal();
396       if (Val >= this->StringTable.size())
397         error("Invalid DT_SONAME entry");
398       SoName = StringRef(this->StringTable.data() + Val);
399       return;
400     }
401   }
402 }
403 
404 // Fully parse the shared object file. This must be called after parseSoName().
405 template <class ELFT> void SharedFile<ELFT>::parseRest() {
406   Elf_Sym_Range Syms = this->getNonLocalSymbols();
407   uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
408   SymbolBodies.reserve(NumSymbols);
409   for (const Elf_Sym &Sym : Syms) {
410     ErrorOr<StringRef> NameOrErr = Sym.getName(this->StringTable);
411     error(NameOrErr.getError());
412     StringRef Name = *NameOrErr;
413 
414     if (Sym.isUndefined())
415       Undefs.push_back(Name);
416     else
417       SymbolBodies.emplace_back(this, Name, Sym);
418   }
419 }
420 
421 template <typename T>
422 static std::unique_ptr<InputFile> createELFFileAux(MemoryBufferRef MB) {
423   std::unique_ptr<T> Ret = llvm::make_unique<T>(MB);
424 
425   if (!Config->FirstElf)
426     Config->FirstElf = Ret.get();
427 
428   if (Config->EKind == ELFNoneKind) {
429     Config->EKind = Ret->getELFKind();
430     Config->EMachine = Ret->getEMachine();
431   }
432 
433   return std::move(Ret);
434 }
435 
436 template <template <class> class T>
437 static std::unique_ptr<InputFile> createELFFile(MemoryBufferRef MB) {
438   std::pair<unsigned char, unsigned char> Type = getElfArchType(MB.getBuffer());
439   if (Type.second != ELF::ELFDATA2LSB && Type.second != ELF::ELFDATA2MSB)
440     error("Invalid data encoding: " + MB.getBufferIdentifier());
441 
442   if (Type.first == ELF::ELFCLASS32) {
443     if (Type.second == ELF::ELFDATA2LSB)
444       return createELFFileAux<T<ELF32LE>>(MB);
445     return createELFFileAux<T<ELF32BE>>(MB);
446   }
447   if (Type.first == ELF::ELFCLASS64) {
448     if (Type.second == ELF::ELFDATA2LSB)
449       return createELFFileAux<T<ELF64LE>>(MB);
450     return createELFFileAux<T<ELF64BE>>(MB);
451   }
452   error("Invalid file class: " + MB.getBufferIdentifier());
453 }
454 
455 std::unique_ptr<InputFile> elf2::createObjectFile(MemoryBufferRef MB) {
456   return createELFFile<ObjectFile>(MB);
457 }
458 
459 std::unique_ptr<InputFile> elf2::createSharedFile(MemoryBufferRef MB) {
460   return createELFFile<SharedFile>(MB);
461 }
462 
463 template class elf2::ELFFileBase<ELF32LE>;
464 template class elf2::ELFFileBase<ELF32BE>;
465 template class elf2::ELFFileBase<ELF64LE>;
466 template class elf2::ELFFileBase<ELF64BE>;
467 
468 template class elf2::ObjectFile<ELF32LE>;
469 template class elf2::ObjectFile<ELF32BE>;
470 template class elf2::ObjectFile<ELF64LE>;
471 template class elf2::ObjectFile<ELF64BE>;
472 
473 template class elf2::SharedFile<ELF32LE>;
474 template class elf2::SharedFile<ELF32BE>;
475 template class elf2::SharedFile<ELF64LE>;
476 template class elf2::SharedFile<ELF64BE>;
477