//===- lib/ReaderWriter/ELF/OutputELFWriter.h ----------------------------===// // // The LLVM Linker // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// #ifndef LLD_READER_WRITER_ELF_OUTPUT_WRITER_H #define LLD_READER_WRITER_ELF_OUTPUT_WRITER_H #include "DefaultLayout.h" #include "ELFFile.h" #include "TargetLayout.h" #include "lld/Core/Instrumentation.h" #include "lld/Core/Parallel.h" #include "lld/Core/SharedLibraryFile.h" #include "lld/ReaderWriter/ELFLinkingContext.h" #include "lld/Core/Simple.h" #include "lld/Core/Writer.h" #include "llvm/ADT/Optional.h" #include "llvm/ADT/StringSet.h" #include "llvm/Support/Path.h" namespace lld { namespace elf { using namespace llvm; using namespace llvm::object; template class OutputELFWriter; template class TargetLayout; namespace { template class SymbolFile : public RuntimeFile { public: SymbolFile(ELFLinkingContext &context) : RuntimeFile(context, "Dynamic absolute symbols"), _atomsAdded(false) {} Atom *addAbsoluteAtom(StringRef symbolName) override { auto *a = RuntimeFile::addAbsoluteAtom(symbolName); if (a) _atomsAdded = true; return a; } Atom *addUndefinedAtom(StringRef) override { llvm_unreachable("Cannot add undefined atoms to resolve undefined symbols"); } bool hasAtoms() const { return _atomsAdded; } private: bool _atomsAdded; }; template class DynamicSymbolFile : public SimpleArchiveLibraryFile { typedef std::function &)> Resolver; public: DynamicSymbolFile(ELFLinkingContext &context, Resolver resolver) : SimpleArchiveLibraryFile("Dynamically added runtime symbols"), _context(context), _resolver(resolver) {} File *find(StringRef sym, bool dataSymbolOnly) override { if (!_file) _file.reset(new (_alloc) SymbolFile(_context)); assert(!_file->hasAtoms() && "The file shouldn't have atoms yet"); _resolver(sym, *_file); // If atoms were added - release the file to the caller. return _file->hasAtoms() ? _file.release() : nullptr; } private: ELFLinkingContext &_context; Resolver _resolver; // The allocator should go before bump pointers because of // reversed destruction order. llvm::BumpPtrAllocator _alloc; unique_bump_ptr> _file; }; } // end anon namespace //===----------------------------------------------------------------------===// // OutputELFWriter Class //===----------------------------------------------------------------------===// /// \brief This acts as the base class for all the ELF writers that are output /// for emitting an ELF output file. This class also acts as a common class for /// creating static and dynamic executables. All the function in this class /// can be overridden and an appropriate writer be created template class OutputELFWriter : public ELFWriter { public: typedef Elf_Shdr_Impl Elf_Shdr; typedef Elf_Sym_Impl Elf_Sym; typedef Elf_Dyn_Impl Elf_Dyn; OutputELFWriter(ELFLinkingContext &context, TargetLayout &layout); protected: // build the sections that need to be created virtual void createDefaultSections(); // Build all the output sections void buildChunks(const File &file) override; // Build the output file virtual std::error_code buildOutput(const File &file); // Setup the ELF header. virtual std::error_code setELFHeader(); // Write the file to the path specified std::error_code writeFile(const File &File, StringRef path) override; // Write to the output file. virtual std::error_code writeOutput(const File &file, StringRef path); // Get the size of the output file that the linker would emit. virtual uint64_t outputFileSize() const; // Build the atom to address map, this has to be called // before applying relocations virtual void buildAtomToAddressMap(const File &file); // Build the symbol table for static linking virtual void buildStaticSymbolTable(const File &file); // Build the dynamic symbol table for dynamic linking virtual void buildDynamicSymbolTable(const File &file); // Build the section header table virtual void buildSectionHeaderTable(); // Assign sections that have no segments such as the symbol table, // section header table, string table etc virtual void assignSectionsWithNoSegments(); // Add default atoms that need to be present in the output file virtual void addDefaultAtoms(); // Add any runtime files and their atoms to the output bool createImplicitFiles(std::vector> &) override; // Finalize the default atom values virtual void finalizeDefaultAtomValues(); // This is called by the write section to apply relocations uint64_t addressOfAtom(const Atom *atom) override { auto addr = _atomToAddressMap.find(atom); return addr == _atomToAddressMap.end() ? 0 : addr->second; } // This is a hook for creating default dynamic entries virtual void createDefaultDynamicEntries() {} /// \brief Create symbol table. virtual unique_bump_ptr> createSymbolTable(); /// \brief create dynamic table. virtual unique_bump_ptr> createDynamicTable(); /// \brief create dynamic symbol table. virtual unique_bump_ptr> createDynamicSymbolTable(); /// \brief Create entry in the dynamic symbols table for this atom. virtual bool isDynSymEntryRequired(const SharedLibraryAtom *sla) const { return _layout.isReferencedByDefinedAtom(sla); } /// \brief Create DT_NEEDED dynamic tage for the shared library. virtual bool isNeededTagRequired(const SharedLibraryAtom *sla) const { return false; } /// \brief Process undefined symbols that left after resolution step. virtual void processUndefinedSymbol(StringRef symName, RuntimeFile &file) const {} llvm::BumpPtrAllocator _alloc; ELFLinkingContext &_context; TargetHandler &_targetHandler; typedef llvm::DenseMap AtomToAddress; AtomToAddress _atomToAddressMap; TargetLayout &_layout; unique_bump_ptr> _elfHeader; unique_bump_ptr> _programHeader; unique_bump_ptr> _symtab; unique_bump_ptr> _strtab; unique_bump_ptr> _shstrtab; unique_bump_ptr> _shdrtab; unique_bump_ptr> _ehFrameHeader; /// \name Dynamic sections. /// @{ unique_bump_ptr> _dynamicTable; unique_bump_ptr> _dynamicSymbolTable; unique_bump_ptr> _dynamicStringTable; unique_bump_ptr> _hashTable; llvm::StringSet<> _soNeeded; /// @} std::unique_ptr> _scriptFile; private: static StringRef maybeGetSOName(Node *node); }; //===----------------------------------------------------------------------===// // OutputELFWriter //===----------------------------------------------------------------------===// template OutputELFWriter::OutputELFWriter(ELFLinkingContext &context, TargetLayout &layout) : _context(context), _targetHandler(context.getTargetHandler()), _layout(layout), _scriptFile(new RuntimeFile(context, "Linker script runtime")) {} template void OutputELFWriter::buildChunks(const File &file) { ScopedTask task(getDefaultDomain(), "buildChunks"); for (const DefinedAtom *definedAtom : file.defined()) { DefinedAtom::ContentType contentType = definedAtom->contentType(); // Dont add COMDAT group atoms and GNU linkonce atoms, as they are used for // symbol resolution. // TODO: handle partial linking. if (contentType == DefinedAtom::typeGroupComdat || contentType == DefinedAtom::typeGnuLinkOnce) continue; _layout.addAtom(definedAtom); } for (const AbsoluteAtom *absoluteAtom : file.absolute()) _layout.addAtom(absoluteAtom); } template void OutputELFWriter::buildStaticSymbolTable(const File &file) { ScopedTask task(getDefaultDomain(), "buildStaticSymbolTable"); for (auto sec : _layout.sections()) if (auto section = dyn_cast>(sec)) for (const auto &atom : section->atoms()) _symtab->addSymbol(atom->_atom, section->ordinal(), atom->_virtualAddr); for (auto &atom : _layout.absoluteAtoms()) _symtab->addSymbol(atom->_atom, ELF::SHN_ABS, atom->_virtualAddr); for (const UndefinedAtom *a : file.undefined()) _symtab->addSymbol(a, ELF::SHN_UNDEF); } // Returns the DSO name for a given input file if it's a shared library // file and not marked as --as-needed. template StringRef OutputELFWriter::maybeGetSOName(Node *node) { if (auto *fnode = dyn_cast(node)) if (!fnode->asNeeded()) if (auto *file = dyn_cast(fnode->getFile())) return file->getDSOName(); return ""; } template void OutputELFWriter::buildDynamicSymbolTable(const File &file) { ScopedTask task(getDefaultDomain(), "buildDynamicSymbolTable"); for (const auto &sla : file.sharedLibrary()) { if (isDynSymEntryRequired(sla)) { _dynamicSymbolTable->addSymbol(sla, ELF::SHN_UNDEF); _soNeeded.insert(sla->loadName()); continue; } if (isNeededTagRequired(sla)) _soNeeded.insert(sla->loadName()); } for (const std::unique_ptr &node : _context.getNodes()) { StringRef soname = maybeGetSOName(node.get()); if (!soname.empty()) _soNeeded.insert(soname); } // Never mark the dynamic linker as DT_NEEDED _soNeeded.erase(sys::path::filename(_context.getInterpreter())); for (const auto &loadName : _soNeeded) { Elf_Dyn dyn; dyn.d_tag = DT_NEEDED; dyn.d_un.d_val = _dynamicStringTable->addString(loadName.getKey()); _dynamicTable->addEntry(dyn); } const auto &rpathList = _context.getRpathList(); if (!rpathList.empty()) { auto rpath = new (_alloc) std::string(join(rpathList.begin(), rpathList.end(), ":")); Elf_Dyn dyn; dyn.d_tag = DT_RPATH; dyn.d_un.d_val = _dynamicStringTable->addString(*rpath); _dynamicTable->addEntry(dyn); } StringRef soname = _context.sharedObjectName(); if (!soname.empty() && _context.getOutputELFType() == llvm::ELF::ET_DYN) { Elf_Dyn dyn; dyn.d_tag = DT_SONAME; dyn.d_un.d_val = _dynamicStringTable->addString(soname); _dynamicTable->addEntry(dyn); } // The dynamic symbol table need to be sorted earlier because the hash // table needs to be built using the dynamic symbol table. It would be // late to sort the symbols due to that in finalize. In the dynamic symbol // table finalize, we call the symbol table finalize and we don't want to // sort again _dynamicSymbolTable->sortSymbols(); // Add the dynamic symbols into the hash table _dynamicSymbolTable->addSymbolsToHashTable(); } template void OutputELFWriter::buildAtomToAddressMap(const File &file) { ScopedTask task(getDefaultDomain(), "buildAtomToAddressMap"); int64_t totalAbsAtoms = _layout.absoluteAtoms().size(); int64_t totalUndefinedAtoms = file.undefined().size(); int64_t totalDefinedAtoms = 0; for (auto sec : _layout.sections()) if (auto section = dyn_cast >(sec)) { totalDefinedAtoms += section->atoms().size(); for (const auto &atom : section->atoms()) _atomToAddressMap[atom->_atom] = atom->_virtualAddr; } // build the atomToAddressMap that contains absolute symbols too for (auto &atom : _layout.absoluteAtoms()) _atomToAddressMap[atom->_atom] = atom->_virtualAddr; // Set the total number of atoms in the symbol table, so that appropriate // resizing of the string table can be done _symtab->setNumEntries(totalDefinedAtoms + totalAbsAtoms + totalUndefinedAtoms); } template void OutputELFWriter::buildSectionHeaderTable() { ScopedTask task(getDefaultDomain(), "buildSectionHeaderTable"); for (auto outputSection : _layout.outputSections()) { if (outputSection->kind() != Chunk::Kind::ELFSection && outputSection->kind() != Chunk::Kind::AtomSection) continue; if (outputSection->hasSegment()) _shdrtab->appendSection(outputSection); } } template void OutputELFWriter::assignSectionsWithNoSegments() { ScopedTask task(getDefaultDomain(), "assignSectionsWithNoSegments"); for (auto outputSection : _layout.outputSections()) { if (outputSection->kind() != Chunk::Kind::ELFSection && outputSection->kind() != Chunk::Kind::AtomSection) continue; if (!outputSection->hasSegment()) _shdrtab->appendSection(outputSection); } _layout.assignFileOffsetsForMiscSections(); for (auto sec : _layout.sections()) if (auto section = dyn_cast>(sec)) if (!DefaultLayout::hasOutputSegment(section)) _shdrtab->updateSection(section); } template void OutputELFWriter::addDefaultAtoms() { const llvm::StringSet<> &symbols = _context.linkerScriptSema().getScriptDefinedSymbols(); for (auto &sym : symbols) _scriptFile->addAbsoluteAtom(sym.getKey()); } template bool OutputELFWriter::createImplicitFiles( std::vector> &result) { // Add the virtual archive to resolve undefined symbols. // The file will be added later in the linking context. auto callback = [this](StringRef sym, RuntimeFile &file) { processUndefinedSymbol(sym, file); }; auto &ctx = const_cast(_context); ctx.setUndefinesResolver( llvm::make_unique>(ctx, std::move(callback))); // Add script defined symbols result.push_back(std::move(_scriptFile)); return true; } template void OutputELFWriter::finalizeDefaultAtomValues() { const llvm::StringSet<> &symbols = _context.linkerScriptSema().getScriptDefinedSymbols(); for (auto &sym : symbols) { uint64_t res = _context.linkerScriptSema().getLinkerScriptExprValue(sym.getKey()); auto a = _layout.findAbsoluteAtom(sym.getKey()); (*a)->_virtualAddr = res; } } template void OutputELFWriter::createDefaultSections() { _elfHeader.reset(new (_alloc) ELFHeader(_context)); _programHeader.reset(new (_alloc) ProgramHeader(_context)); _layout.setHeader(_elfHeader.get()); _layout.setProgramHeader(_programHeader.get()); _symtab = std::move(this->createSymbolTable()); _strtab.reset(new (_alloc) StringTable( _context, ".strtab", DefaultLayout::ORDER_STRING_TABLE)); _shstrtab.reset(new (_alloc) StringTable( _context, ".shstrtab", DefaultLayout::ORDER_SECTION_STRINGS)); _shdrtab.reset(new (_alloc) SectionHeader( _context, DefaultLayout::ORDER_SECTION_HEADERS)); _layout.addSection(_symtab.get()); _layout.addSection(_strtab.get()); _layout.addSection(_shstrtab.get()); _shdrtab->setStringSection(_shstrtab.get()); _symtab->setStringSection(_strtab.get()); _layout.addSection(_shdrtab.get()); for (auto sec : _layout.sections()) { // TODO: use findOutputSection auto section = dyn_cast>(sec); if (!section || section->outputSectionName() != ".eh_frame") continue; _ehFrameHeader.reset(new (_alloc) EHFrameHeader( _context, ".eh_frame_hdr", _layout, DefaultLayout::ORDER_EH_FRAMEHDR)); _layout.addSection(_ehFrameHeader.get()); break; } if (_context.isDynamic()) { _dynamicTable = std::move(createDynamicTable()); _dynamicStringTable.reset(new (_alloc) StringTable( _context, ".dynstr", DefaultLayout::ORDER_DYNAMIC_STRINGS, true)); _dynamicSymbolTable = std::move(createDynamicSymbolTable()); _hashTable.reset(new (_alloc) HashSection( _context, ".hash", DefaultLayout::ORDER_HASH)); // Set the hash table in the dynamic symbol table so that the entries in the // hash table can be created _dynamicSymbolTable->setHashTable(_hashTable.get()); _hashTable->setSymbolTable(_dynamicSymbolTable.get()); _layout.addSection(_dynamicTable.get()); _layout.addSection(_dynamicStringTable.get()); _layout.addSection(_dynamicSymbolTable.get()); _layout.addSection(_hashTable.get()); _dynamicSymbolTable->setStringSection(_dynamicStringTable.get()); _dynamicTable->setSymbolTable(_dynamicSymbolTable.get()); _dynamicTable->setHashTable(_hashTable.get()); if (_layout.hasDynamicRelocationTable()) _layout.getDynamicRelocationTable()->setSymbolTable( _dynamicSymbolTable.get()); if (_layout.hasPLTRelocationTable()) _layout.getPLTRelocationTable()->setSymbolTable( _dynamicSymbolTable.get()); } } template unique_bump_ptr> OutputELFWriter::createSymbolTable() { return unique_bump_ptr>(new (_alloc) SymbolTable( this->_context, ".symtab", DefaultLayout::ORDER_SYMBOL_TABLE)); } /// \brief create dynamic table template unique_bump_ptr> OutputELFWriter::createDynamicTable() { return unique_bump_ptr>( new (_alloc) DynamicTable( this->_context, _layout, ".dynamic", DefaultLayout::ORDER_DYNAMIC)); } /// \brief create dynamic symbol table template unique_bump_ptr> OutputELFWriter::createDynamicSymbolTable() { return unique_bump_ptr>( new (_alloc) DynamicSymbolTable( this->_context, _layout, ".dynsym", DefaultLayout::ORDER_DYNAMIC_SYMBOLS)); } template std::error_code OutputELFWriter::buildOutput(const File &file) { ScopedTask buildTask(getDefaultDomain(), "ELF Writer buildOutput"); buildChunks(file); // Create the default sections like the symbol table, string table, and the // section string table createDefaultSections(); // Set the Layout _layout.assignSectionsToSegments(); // Create the dynamic table entries if (_context.isDynamic()) { _dynamicTable->createDefaultEntries(); buildDynamicSymbolTable(file); } // Call the preFlight callbacks to modify the sections and the atoms // contained in them, in anyway the targets may want _layout.doPreFlight(); _layout.assignVirtualAddress(); // Finalize the default value of symbols that the linker adds finalizeDefaultAtomValues(); // Build the Atom To Address map for applying relocations buildAtomToAddressMap(file); // Create symbol table and section string table // Do it only if -s is not specified. if (!_context.stripSymbols()) buildStaticSymbolTable(file); // Finalize the layout by calling the finalize() functions _layout.finalize(); // build Section Header table buildSectionHeaderTable(); // assign Offsets and virtual addresses // for sections with no segments assignSectionsWithNoSegments(); if (_context.isDynamic()) _dynamicTable->updateDynamicTable(); return std::error_code(); } template std::error_code OutputELFWriter::setELFHeader() { _elfHeader->e_type(_context.getOutputELFType()); _elfHeader->e_machine(_context.getOutputMachine()); _elfHeader->e_ident(ELF::EI_VERSION, 1); _elfHeader->e_ident(ELF::EI_OSABI, 0); _elfHeader->e_version(1); _elfHeader->e_phoff(_programHeader->fileOffset()); _elfHeader->e_shoff(_shdrtab->fileOffset()); _elfHeader->e_phentsize(_programHeader->entsize()); _elfHeader->e_phnum(_programHeader->numHeaders()); _elfHeader->e_shentsize(_shdrtab->entsize()); _elfHeader->e_shnum(_shdrtab->numHeaders()); _elfHeader->e_shstrndx(_shstrtab->ordinal()); if (const auto *al = _layout.findAtomLayoutByName(_context.entrySymbolName())) _elfHeader->e_entry(al->_virtualAddr); else _elfHeader->e_entry(0); return std::error_code(); } template uint64_t OutputELFWriter::outputFileSize() const { return _shdrtab->fileOffset() + _shdrtab->fileSize(); } template std::error_code OutputELFWriter::writeOutput(const File &file, StringRef path) { std::unique_ptr buffer; ScopedTask createOutputTask(getDefaultDomain(), "ELF Writer Create Output"); std::error_code ec = FileOutputBuffer::create(path, outputFileSize(), buffer, FileOutputBuffer::F_executable); createOutputTask.end(); if (ec) return ec; ScopedTask writeTask(getDefaultDomain(), "ELF Writer write to memory"); // HACK: We have to write out the header and program header here even though // they are a member of a segment because only sections are written in the // following loop. // Finalize ELF Header / Program Headers. _elfHeader->finalize(); _programHeader->finalize(); _elfHeader->write(this, _layout, *buffer); _programHeader->write(this, _layout, *buffer); auto sections = _layout.sections(); parallel_for_each( sections.begin(), sections.end(), [&](Chunk *section) { section->write(this, _layout, *buffer); }); writeTask.end(); ScopedTask commitTask(getDefaultDomain(), "ELF Writer commit to disk"); return buffer->commit(); } template std::error_code OutputELFWriter::writeFile(const File &file, StringRef path) { std::error_code ec = buildOutput(file); if (ec) return ec; ec = setELFHeader(); if (ec) return ec; return writeOutput(file, path); } } // namespace elf } // namespace lld #endif // LLD_READER_WRITER_ELF_OUTPUT_WRITER_H