//===- InputFiles.cpp -----------------------------------------------------===// // // The LLVM Linker // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// #include "InputFiles.h" #include "Error.h" #include "InputSection.h" #include "Symbols.h" #include "llvm/ADT/STLExtras.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Module.h" #include "llvm/Object/IRObjectFile.h" #include "llvm/Support/raw_ostream.h" using namespace llvm; using namespace llvm::ELF; using namespace llvm::object; using namespace llvm::sys::fs; using namespace lld; using namespace lld::elf; template static ELFFile createELFObj(MemoryBufferRef MB) { std::error_code EC; ELFFile F(MB.getBuffer(), EC); check(EC); return F; } template ELFFileBase::ELFFileBase(Kind K, MemoryBufferRef MB) : InputFile(K, MB), ELFObj(createELFObj(MB)) {} template ELFKind ELFFileBase::getELFKind() { if (ELFT::TargetEndianness == support::little) return ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind; return ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind; } template typename ELFFileBase::Elf_Sym_Range ELFFileBase::getElfSymbols(bool OnlyGlobals) { if (!Symtab) return Elf_Sym_Range(nullptr, nullptr); Elf_Sym_Range Syms = ELFObj.symbols(Symtab); uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end()); uint32_t FirstNonLocal = Symtab->sh_info; if (FirstNonLocal > NumSymbols) fatal("invalid sh_info in symbol table"); if (OnlyGlobals) return make_range(Syms.begin() + FirstNonLocal, Syms.end()); return make_range(Syms.begin(), Syms.end()); } template uint32_t ELFFileBase::getSectionIndex(const Elf_Sym &Sym) const { uint32_t I = Sym.st_shndx; if (I == ELF::SHN_XINDEX) return ELFObj.getExtendedSymbolTableIndex(&Sym, Symtab, SymtabSHNDX); if (I >= ELF::SHN_LORESERVE) return 0; return I; } template void ELFFileBase::initStringTable() { if (!Symtab) return; StringTable = check(ELFObj.getStringTableForSymtab(*Symtab)); } template elf::ObjectFile::ObjectFile(MemoryBufferRef M) : ELFFileBase(Base::ObjectKind, M) {} template ArrayRef elf::ObjectFile::getNonLocalSymbols() { if (!this->Symtab) return this->SymbolBodies; uint32_t FirstNonLocal = this->Symtab->sh_info; return makeArrayRef(this->SymbolBodies).slice(FirstNonLocal); } template ArrayRef elf::ObjectFile::getLocalSymbols() { if (!this->Symtab) return this->SymbolBodies; uint32_t FirstNonLocal = this->Symtab->sh_info; return makeArrayRef(this->SymbolBodies).slice(1, FirstNonLocal - 1); } template ArrayRef elf::ObjectFile::getSymbols() { if (!this->Symtab) return this->SymbolBodies; return makeArrayRef(this->SymbolBodies).slice(1); } template uint32_t elf::ObjectFile::getMipsGp0() const { if (MipsReginfo) return MipsReginfo->Reginfo->ri_gp_value; return 0; } template void elf::ObjectFile::parse(DenseSet &ComdatGroups) { // Read section and symbol tables. initializeSections(ComdatGroups); initializeSymbols(); } // Sections with SHT_GROUP and comdat bits define comdat section groups. // They are identified and deduplicated by group name. This function // returns a group name. template StringRef elf::ObjectFile::getShtGroupSignature(const Elf_Shdr &Sec) { const ELFFile &Obj = this->ELFObj; uint32_t SymtabdSectionIndex = Sec.sh_link; const Elf_Shdr *SymtabSec = check(Obj.getSection(SymtabdSectionIndex)); uint32_t SymIndex = Sec.sh_info; const Elf_Sym *Sym = Obj.getSymbol(SymtabSec, SymIndex); StringRef StringTable = check(Obj.getStringTableForSymtab(*SymtabSec)); return check(Sym->getName(StringTable)); } template ArrayRef::uint32_X> elf::ObjectFile::getShtGroupEntries(const Elf_Shdr &Sec) { const ELFFile &Obj = this->ELFObj; ArrayRef Entries = check(Obj.template getSectionContentsAsArray(&Sec)); if (Entries.empty() || Entries[0] != GRP_COMDAT) fatal("unsupported SHT_GROUP format"); return Entries.slice(1); } template static bool shouldMerge(const typename ELFFile::Elf_Shdr &Sec) { typedef typename ELFFile::uintX_t uintX_t; uintX_t Flags = Sec.sh_flags; if (!(Flags & SHF_MERGE)) return false; if (Flags & SHF_WRITE) fatal("writable SHF_MERGE sections are not supported"); uintX_t EntSize = Sec.sh_entsize; if (!EntSize || Sec.sh_size % EntSize) fatal("SHF_MERGE section size must be a multiple of sh_entsize"); // Don't try to merge if the aligment is larger than the sh_entsize and this // is not SHF_STRINGS. // // Since this is not a SHF_STRINGS, we would need to pad after every entity. // It would be equivalent for the producer of the .o to just set a larger // sh_entsize. if (Flags & SHF_STRINGS) return true; if (Sec.sh_addralign > EntSize) return false; return true; } template void elf::ObjectFile::initializeSections( DenseSet &ComdatGroups) { uint64_t Size = this->ELFObj.getNumSections(); Sections.resize(Size); unsigned I = -1; const ELFFile &Obj = this->ELFObj; for (const Elf_Shdr &Sec : Obj.sections()) { ++I; if (Sections[I] == InputSection::Discarded) continue; switch (Sec.sh_type) { case SHT_GROUP: Sections[I] = InputSection::Discarded; if (ComdatGroups.insert(getShtGroupSignature(Sec)).second) continue; for (uint32_t SecIndex : getShtGroupEntries(Sec)) { if (SecIndex >= Size) fatal("invalid section index in group"); Sections[SecIndex] = InputSection::Discarded; } break; case SHT_SYMTAB: this->Symtab = &Sec; break; case SHT_SYMTAB_SHNDX: this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec)); break; case SHT_STRTAB: case SHT_NULL: break; case SHT_RELA: case SHT_REL: { // This section contains relocation information. // If -r is given, we do not interpret or apply relocation // but just copy relocation sections to output. if (Config->Relocatable) { Sections[I] = new (Alloc) InputSection(this, &Sec); break; } // Find the relocation target section and associate this // section with it. InputSectionBase *Target = getRelocTarget(Sec); if (!Target) break; if (auto *S = dyn_cast>(Target)) { S->RelocSections.push_back(&Sec); break; } if (auto *S = dyn_cast>(Target)) { if (S->RelocSection) fatal("multiple relocation sections to .eh_frame are not supported"); S->RelocSection = &Sec; break; } fatal("relocations pointing to SHF_MERGE are not supported"); } default: Sections[I] = createInputSection(Sec); } } } template InputSectionBase * elf::ObjectFile::getRelocTarget(const Elf_Shdr &Sec) { uint32_t Idx = Sec.sh_info; if (Idx >= Sections.size()) fatal("invalid relocated section index"); InputSectionBase *Target = Sections[Idx]; // Strictly speaking, a relocation section must be included in the // group of the section it relocates. However, LLVM 3.3 and earlier // would fail to do so, so we gracefully handle that case. if (Target == InputSection::Discarded) return nullptr; if (!Target) fatal("unsupported relocation reference"); return Target; } template InputSectionBase * elf::ObjectFile::createInputSection(const Elf_Shdr &Sec) { StringRef Name = check(this->ELFObj.getSectionName(&Sec)); // .note.GNU-stack is a marker section to control the presence of // PT_GNU_STACK segment in outputs. Since the presence of the segment // is controlled only by the command line option (-z execstack) in LLD, // .note.GNU-stack is ignored. if (Name == ".note.GNU-stack") return InputSection::Discarded; if (Name == ".note.GNU-split-stack") error("objects using splitstacks are not supported"); // A MIPS object file has a special section that contains register // usage info, which needs to be handled by the linker specially. if (Config->EMachine == EM_MIPS && Name == ".reginfo") { MipsReginfo = new (Alloc) MipsReginfoInputSection(this, &Sec); return MipsReginfo; } // We dont need special handling of .eh_frame sections if relocatable // output was choosen. Proccess them as usual input sections. if (!Config->Relocatable && Name == ".eh_frame") return new (EHAlloc.Allocate()) EHInputSection(this, &Sec); if (shouldMerge(Sec)) return new (MAlloc.Allocate()) MergeInputSection(this, &Sec); return new (Alloc) InputSection(this, &Sec); } template void elf::ObjectFile::initializeSymbols() { this->initStringTable(); Elf_Sym_Range Syms = this->getElfSymbols(false); uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end()); SymbolBodies.reserve(NumSymbols); for (const Elf_Sym &Sym : Syms) SymbolBodies.push_back(createSymbolBody(&Sym)); } template InputSectionBase * elf::ObjectFile::getSection(const Elf_Sym &Sym) const { uint32_t Index = this->getSectionIndex(Sym); if (Index == 0) return nullptr; if (Index >= Sections.size() || !Sections[Index]) fatal("invalid section index"); InputSectionBase *S = Sections[Index]; if (S == InputSectionBase::Discarded) return S; return S->Repl; } template SymbolBody *elf::ObjectFile::createSymbolBody(const Elf_Sym *Sym) { unsigned char Binding = Sym->getBinding(); InputSectionBase *Sec = getSection(*Sym); if (Binding == STB_LOCAL) { if (Sec == InputSection::Discarded) Sec = nullptr; return new (Alloc) DefinedRegular("", *Sym, Sec); } StringRef Name = check(Sym->getName(this->StringTable)); switch (Sym->st_shndx) { case SHN_UNDEF: return new (Alloc) UndefinedElf(Name, *Sym); case SHN_COMMON: return new (Alloc) DefinedCommon(Name, Sym->st_size, Sym->st_value, Sym->getBinding() == llvm::ELF::STB_WEAK, Sym->getVisibility()); } switch (Binding) { default: fatal("unexpected binding"); case STB_GLOBAL: case STB_WEAK: case STB_GNU_UNIQUE: if (Sec == InputSection::Discarded) return new (Alloc) UndefinedElf(Name, *Sym); return new (Alloc) DefinedRegular(Name, *Sym, Sec); } } void ArchiveFile::parse() { File = check(Archive::create(MB), "Failed to parse archive"); // Allocate a buffer for Lazy objects. size_t NumSyms = File->getNumberOfSymbols(); LazySymbols.reserve(NumSyms); // Read the symbol table to construct Lazy objects. for (const Archive::Symbol &Sym : File->symbols()) LazySymbols.emplace_back(this, Sym); } // Returns a buffer pointing to a member file containing a given symbol. MemoryBufferRef ArchiveFile::getMember(const Archive::Symbol *Sym) { Archive::Child C = check(Sym->getMember(), "Could not get the member for symbol " + Sym->getName()); if (!Seen.insert(C.getChildOffset()).second) return MemoryBufferRef(); return check(C.getMemoryBufferRef(), "Could not get the buffer for the member defining symbol " + Sym->getName()); } template SharedFile::SharedFile(MemoryBufferRef M) : ELFFileBase(Base::SharedKind, M), AsNeeded(Config->AsNeeded) {} template const typename ELFFile::Elf_Shdr * SharedFile::getSection(const Elf_Sym &Sym) const { uint32_t Index = this->getSectionIndex(Sym); if (Index == 0) return nullptr; return check(this->ELFObj.getSection(Index)); } // Partially parse the shared object file so that we can call // getSoName on this object. template void SharedFile::parseSoName() { typedef typename ELFFile::Elf_Dyn Elf_Dyn; typedef typename ELFFile::uintX_t uintX_t; const Elf_Shdr *DynamicSec = nullptr; const ELFFile Obj = this->ELFObj; for (const Elf_Shdr &Sec : Obj.sections()) { switch (Sec.sh_type) { default: continue; case SHT_DYNSYM: this->Symtab = &Sec; break; case SHT_DYNAMIC: DynamicSec = &Sec; break; case SHT_SYMTAB_SHNDX: this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec)); break; } } this->initStringTable(); SoName = this->getName(); if (!DynamicSec) return; auto *Begin = reinterpret_cast(Obj.base() + DynamicSec->sh_offset); const Elf_Dyn *End = Begin + DynamicSec->sh_size / sizeof(Elf_Dyn); for (const Elf_Dyn &Dyn : make_range(Begin, End)) { if (Dyn.d_tag == DT_SONAME) { uintX_t Val = Dyn.getVal(); if (Val >= this->StringTable.size()) fatal("invalid DT_SONAME entry"); SoName = StringRef(this->StringTable.data() + Val); return; } } } // Fully parse the shared object file. This must be called after parseSoName(). template void SharedFile::parseRest() { Elf_Sym_Range Syms = this->getElfSymbols(true); uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end()); SymbolBodies.reserve(NumSymbols); for (const Elf_Sym &Sym : Syms) { StringRef Name = check(Sym.getName(this->StringTable)); if (Sym.isUndefined()) Undefs.push_back(Name); else SymbolBodies.emplace_back(this, Name, Sym); } } BitcodeFile::BitcodeFile(MemoryBufferRef M) : InputFile(BitcodeKind, M) {} bool BitcodeFile::classof(const InputFile *F) { return F->kind() == BitcodeKind; } static uint8_t getGvVisibility(const GlobalValue *GV) { switch (GV->getVisibility()) { case GlobalValue::DefaultVisibility: return STV_DEFAULT; case GlobalValue::HiddenVisibility: return STV_HIDDEN; case GlobalValue::ProtectedVisibility: return STV_PROTECTED; } llvm_unreachable("unknown visibility"); } SymbolBody * BitcodeFile::createSymbolBody(const DenseSet &KeptComdats, const IRObjectFile &Obj, const BasicSymbolRef &Sym) { const GlobalValue *GV = Obj.getSymbolGV(Sym.getRawDataRefImpl()); assert(GV); if (const Comdat *C = GV->getComdat()) if (!KeptComdats.count(C)) return nullptr; uint8_t Visibility = getGvVisibility(GV); SmallString<64> Name; raw_svector_ostream OS(Name); Sym.printName(OS); StringRef NameRef = Saver.save(StringRef(Name)); const Module &M = Obj.getModule(); SymbolBody *Body; uint32_t Flags = Sym.getFlags(); bool IsWeak = Flags & BasicSymbolRef::SF_Weak; if (Flags & BasicSymbolRef::SF_Undefined) { Body = new (Alloc) Undefined(NameRef, IsWeak, Visibility, false); } else if (Flags & BasicSymbolRef::SF_Common) { const DataLayout &DL = M.getDataLayout(); uint64_t Size = DL.getTypeAllocSize(GV->getValueType()); Body = new (Alloc) DefinedCommon(NameRef, Size, GV->getAlignment(), IsWeak, Visibility); } else { Body = new (Alloc) DefinedBitcode(NameRef, IsWeak, Visibility); } Body->IsTls = GV->isThreadLocal(); return Body; } bool BitcodeFile::shouldSkip(const BasicSymbolRef &Sym) { uint32_t Flags = Sym.getFlags(); if (!(Flags & BasicSymbolRef::SF_Global)) return true; if (Flags & BasicSymbolRef::SF_FormatSpecific) return true; return false; } void BitcodeFile::parse(DenseSet &ComdatGroups) { LLVMContext Context; std::unique_ptr Obj = check(IRObjectFile::create(MB, Context)); const Module &M = Obj->getModule(); DenseSet KeptComdats; for (const auto &P : M.getComdatSymbolTable()) { StringRef N = Saver.save(P.first()); if (ComdatGroups.insert(N).second) KeptComdats.insert(&P.second); } for (const BasicSymbolRef &Sym : Obj->symbols()) if (!shouldSkip(Sym)) SymbolBodies.push_back(createSymbolBody(KeptComdats, *Obj, Sym)); } template static std::unique_ptr createELFFileAux(MemoryBufferRef MB) { std::unique_ptr Ret = llvm::make_unique(MB); if (!Config->FirstElf) Config->FirstElf = Ret.get(); if (Config->EKind == ELFNoneKind) { Config->EKind = Ret->getELFKind(); Config->EMachine = Ret->getEMachine(); } return std::move(Ret); } template