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 (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 211 error("Relocations pointing to SHF_MERGE are not supported"); 212 break; 213 } 214 default: 215 if (shouldMerge<ELFT>(Sec)) 216 Sections[I] = new (this->Alloc) MergeInputSection<ELFT>(this, &Sec); 217 else 218 Sections[I] = new (this->Alloc) InputSection<ELFT>(this, &Sec); 219 break; 220 } 221 } 222 } 223 224 template <class ELFT> void elf2::ObjectFile<ELFT>::initializeSymbols() { 225 this->initStringTable(); 226 Elf_Sym_Range Syms = this->getNonLocalSymbols(); 227 uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end()); 228 this->SymbolBodies.reserve(NumSymbols); 229 for (const Elf_Sym &Sym : Syms) 230 this->SymbolBodies.push_back(createSymbolBody(this->StringTable, &Sym)); 231 } 232 233 template <class ELFT> 234 InputSectionBase<ELFT> * 235 elf2::ObjectFile<ELFT>::getSection(const Elf_Sym &Sym) const { 236 uint32_t Index = this->getSectionIndex(Sym); 237 if (Index == 0) 238 return nullptr; 239 if (Index >= Sections.size() || !Sections[Index]) 240 error("Invalid section index"); 241 return Sections[Index]; 242 } 243 244 template <class ELFT> 245 SymbolBody *elf2::ObjectFile<ELFT>::createSymbolBody(StringRef StringTable, 246 const Elf_Sym *Sym) { 247 ErrorOr<StringRef> NameOrErr = Sym->getName(StringTable); 248 error(NameOrErr.getError()); 249 StringRef Name = *NameOrErr; 250 251 switch (Sym->st_shndx) { 252 case SHN_ABS: 253 return new (this->Alloc) DefinedAbsolute<ELFT>(Name, *Sym); 254 case SHN_UNDEF: 255 return new (this->Alloc) Undefined<ELFT>(Name, *Sym); 256 case SHN_COMMON: 257 return new (this->Alloc) DefinedCommon<ELFT>(Name, *Sym); 258 } 259 260 switch (Sym->getBinding()) { 261 default: 262 error("unexpected binding"); 263 case STB_GLOBAL: 264 case STB_WEAK: 265 case STB_GNU_UNIQUE: { 266 InputSectionBase<ELFT> *Sec = getSection(*Sym); 267 if (Sec == &InputSection<ELFT>::Discarded) 268 return new (this->Alloc) Undefined<ELFT>(Name, *Sym); 269 return new (this->Alloc) DefinedRegular<ELFT>(Name, *Sym, *Sec); 270 } 271 } 272 } 273 274 static std::unique_ptr<Archive> openArchive(MemoryBufferRef MB) { 275 ErrorOr<std::unique_ptr<Archive>> ArchiveOrErr = Archive::create(MB); 276 error(ArchiveOrErr, "Failed to parse archive"); 277 return std::move(*ArchiveOrErr); 278 } 279 280 void ArchiveFile::parse() { 281 File = openArchive(MB); 282 283 // Allocate a buffer for Lazy objects. 284 size_t NumSyms = File->getNumberOfSymbols(); 285 LazySymbols.reserve(NumSyms); 286 287 // Read the symbol table to construct Lazy objects. 288 for (const Archive::Symbol &Sym : File->symbols()) 289 LazySymbols.emplace_back(this, Sym); 290 } 291 292 // Returns a buffer pointing to a member file containing a given symbol. 293 MemoryBufferRef ArchiveFile::getMember(const Archive::Symbol *Sym) { 294 ErrorOr<Archive::child_iterator> ItOrErr = Sym->getMember(); 295 error(ItOrErr, "Could not get the member for symbol " + Sym->getName()); 296 Archive::child_iterator It = *ItOrErr; 297 298 if (!Seen.insert(It->getChildOffset()).second) 299 return MemoryBufferRef(); 300 301 ErrorOr<MemoryBufferRef> Ret = It->getMemoryBufferRef(); 302 error(Ret, "Could not get the buffer for the member defining symbol " + 303 Sym->getName()); 304 return *Ret; 305 } 306 307 std::vector<MemoryBufferRef> ArchiveFile::getMembers() { 308 File = openArchive(MB); 309 310 std::vector<MemoryBufferRef> Result; 311 for (const Archive::Child &Child : File->children()) { 312 ErrorOr<MemoryBufferRef> MbOrErr = Child.getMemoryBufferRef(); 313 error(MbOrErr, "Could not get the buffer for a child of the archive " + 314 File->getFileName()); 315 Result.push_back(MbOrErr.get()); 316 } 317 return Result; 318 } 319 320 template <class ELFT> 321 SharedFile<ELFT>::SharedFile(MemoryBufferRef M) 322 : ELFFileBase<ELFT>(Base::SharedKind, M) { 323 AsNeeded = Config->AsNeeded; 324 } 325 326 template <class ELFT> 327 const typename ELFFile<ELFT>::Elf_Shdr * 328 SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const { 329 uint32_t Index = this->getSectionIndex(Sym); 330 if (Index == 0) 331 return nullptr; 332 ErrorOr<const Elf_Shdr *> Ret = this->ELFObj.getSection(Index); 333 error(Ret); 334 return *Ret; 335 } 336 337 template <class ELFT> void SharedFile<ELFT>::parseSoName() { 338 typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn; 339 typedef typename ELFFile<ELFT>::uintX_t uintX_t; 340 const Elf_Shdr *DynamicSec = nullptr; 341 342 const ELFFile<ELFT> Obj = this->ELFObj; 343 for (const Elf_Shdr &Sec : Obj.sections()) { 344 switch (Sec.sh_type) { 345 default: 346 continue; 347 case SHT_DYNSYM: 348 this->Symtab = &Sec; 349 break; 350 case SHT_DYNAMIC: 351 DynamicSec = &Sec; 352 break; 353 case SHT_SYMTAB_SHNDX: { 354 ErrorOr<ArrayRef<Elf_Word>> ErrorOrTable = Obj.getSHNDXTable(Sec); 355 error(ErrorOrTable); 356 this->SymtabSHNDX = *ErrorOrTable; 357 break; 358 } 359 } 360 } 361 362 this->initStringTable(); 363 this->SoName = this->getName(); 364 365 if (!DynamicSec) 366 return; 367 auto *Begin = 368 reinterpret_cast<const Elf_Dyn *>(Obj.base() + DynamicSec->sh_offset); 369 const Elf_Dyn *End = Begin + DynamicSec->sh_size / sizeof(Elf_Dyn); 370 371 for (const Elf_Dyn &Dyn : make_range(Begin, End)) { 372 if (Dyn.d_tag == DT_SONAME) { 373 uintX_t Val = Dyn.getVal(); 374 if (Val >= this->StringTable.size()) 375 error("Invalid DT_SONAME entry"); 376 this->SoName = StringRef(this->StringTable.data() + Val); 377 return; 378 } 379 } 380 } 381 382 template <class ELFT> void SharedFile<ELFT>::parse() { 383 Elf_Sym_Range Syms = this->getNonLocalSymbols(); 384 uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end()); 385 SymbolBodies.reserve(NumSymbols); 386 for (const Elf_Sym &Sym : Syms) { 387 ErrorOr<StringRef> NameOrErr = Sym.getName(this->StringTable); 388 error(NameOrErr.getError()); 389 StringRef Name = *NameOrErr; 390 391 if (Sym.isUndefined()) 392 Undefs.push_back(Name); 393 else 394 SymbolBodies.emplace_back(this, Name, Sym); 395 } 396 } 397 398 template <typename T> 399 static std::unique_ptr<InputFile> createELFFileAux(MemoryBufferRef MB) { 400 std::unique_ptr<T> Ret = llvm::make_unique<T>(MB); 401 402 if (!Config->FirstElf) 403 Config->FirstElf = Ret.get(); 404 405 if (Config->EKind == ELFNoneKind) { 406 Config->EKind = Ret->getELFKind(); 407 Config->EMachine = Ret->getEMachine(); 408 } 409 410 return std::move(Ret); 411 } 412 413 template <template <class> class T> 414 std::unique_ptr<InputFile> lld::elf2::createELFFile(MemoryBufferRef MB) { 415 using namespace llvm; 416 417 std::pair<unsigned char, unsigned char> Type = 418 object::getElfArchType(MB.getBuffer()); 419 if (Type.second != ELF::ELFDATA2LSB && Type.second != ELF::ELFDATA2MSB) 420 error("Invalid data encoding: " + MB.getBufferIdentifier()); 421 422 if (Type.first == ELF::ELFCLASS32) { 423 if (Type.second == ELF::ELFDATA2LSB) 424 return createELFFileAux<T<object::ELF32LE>>(MB); 425 return createELFFileAux<T<object::ELF32BE>>(MB); 426 } 427 if (Type.first == ELF::ELFCLASS64) { 428 if (Type.second == ELF::ELFDATA2LSB) 429 return createELFFileAux<T<object::ELF64LE>>(MB); 430 return createELFFileAux<T<object::ELF64BE>>(MB); 431 } 432 error("Invalid file class: " + MB.getBufferIdentifier()); 433 } 434 435 namespace lld { 436 namespace elf2 { 437 template class ELFFileBase<llvm::object::ELF32LE>; 438 template class ELFFileBase<llvm::object::ELF32BE>; 439 template class ELFFileBase<llvm::object::ELF64LE>; 440 template class ELFFileBase<llvm::object::ELF64BE>; 441 442 template class ObjectFile<llvm::object::ELF32LE>; 443 template class ObjectFile<llvm::object::ELF32BE>; 444 template class ObjectFile<llvm::object::ELF64LE>; 445 template class ObjectFile<llvm::object::ELF64BE>; 446 447 template class SharedFile<llvm::object::ELF32LE>; 448 template class SharedFile<llvm::object::ELF32BE>; 449 template class SharedFile<llvm::object::ELF64LE>; 450 template class SharedFile<llvm::object::ELF64BE>; 451 452 template std::unique_ptr<InputFile> createELFFile<ObjectFile>(MemoryBufferRef); 453 template std::unique_ptr<InputFile> createELFFile<SharedFile>(MemoryBufferRef); 454 } 455 } 456