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