//===- lib/ReaderWriter/ELF/ELFFile.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_FILE_H #define LLD_READER_WRITER_ELF_FILE_H #include "Atoms.h" #include "lld/Core/File.h" #include "lld/Core/Reference.h" #include "lld/ReaderWriter/ELFLinkingContext.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringRef.h" #include "llvm/Object/ELF.h" #include "llvm/Object/ObjectFile.h" #include "llvm/Support/Allocator.h" #include "llvm/Support/Casting.h" #include "llvm/Support/ELF.h" #include "llvm/Support/Endian.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ErrorOr.h" #include "llvm/Support/MathExtras.h" #include "llvm/Support/Memory.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" #include "llvm/Support/raw_ostream.h" #include #include #include namespace lld { namespace elf { /// \brief Read a binary, find out based on the symbol table contents what kind /// of symbol it is and create corresponding atoms for it template class ELFFile : public File { typedef llvm::object::Elf_Sym_Impl Elf_Sym; typedef llvm::object::Elf_Shdr_Impl Elf_Shdr; typedef llvm::object::Elf_Rel_Impl Elf_Rel; typedef llvm::object::Elf_Rel_Impl Elf_Rela; typedef typename llvm::object::ELFFile::Elf_Sym_Iter Elf_Sym_Iter; typedef typename llvm::object::ELFFile::Elf_Rela_Iter Elf_Rela_Iter; typedef typename llvm::object::ELFFile::Elf_Rel_Iter Elf_Rel_Iter; // A Map is used to hold the atoms that have been divided up // after reading the section that contains Merge String attributes struct MergeSectionKey { MergeSectionKey(const Elf_Shdr *shdr, int64_t offset) : _shdr(shdr), _offset(offset) {} // Data members const Elf_Shdr *_shdr; int64_t _offset; }; struct MergeSectionEq { int64_t operator()(const MergeSectionKey &k) const { return llvm::hash_combine((int64_t)(k._shdr->sh_name), (int64_t)k._offset); } bool operator()(const MergeSectionKey &lhs, const MergeSectionKey &rhs) const { return ((lhs._shdr->sh_name == rhs._shdr->sh_name) && (lhs._offset == rhs._offset)); } }; struct MergeString { MergeString(int64_t offset, StringRef str, const Elf_Shdr *shdr, StringRef sectionName) : _offset(offset), _string(str), _shdr(shdr), _sectionName(sectionName) {} // the offset of this atom int64_t _offset; // The content StringRef _string; // Section header const Elf_Shdr *_shdr; // Section name StringRef _sectionName; }; // This is used to find the MergeAtom given a relocation // offset typedef std::vector *> MergeAtomsT; /// \brief find a mergeAtom given a start offset struct FindByOffset { const Elf_Shdr *_shdr; int64_t _offset; FindByOffset(const Elf_Shdr *shdr, int64_t offset) : _shdr(shdr), _offset(offset) {} bool operator()(const ELFMergeAtom *a) { int64_t off = a->offset(); return (_shdr->sh_name == a->section()) && ((_offset >= off) && (_offset <= off + (int64_t)a->size())); } }; /// \brief find a merge atom given a offset ELFMergeAtom *findMergeAtom(const Elf_Shdr *shdr, uint64_t offset) { auto it = std::find_if(_mergeAtoms.begin(), _mergeAtoms.end(), FindByOffset(shdr, offset)); assert(it != _mergeAtoms.end()); return *it; } typedef std::unordered_map MergedSectionMapT; typedef typename MergedSectionMapT::iterator MergedSectionMapIterT; public: ELFFile(StringRef name) : File(name, kindObject), _ordinal(0), _doStringsMerge(false) {} ELFFile(std::unique_ptr mb, bool atomizeStrings = false) : File(mb->getBufferIdentifier(), kindObject), _mb(std::move(mb)), _ordinal(0), _doStringsMerge(atomizeStrings) {} static ErrorOr> create(std::unique_ptr mb, bool atomizeStrings); virtual Reference::KindArch kindArch(); /// \brief Read input sections and populate necessary data structures /// to read them later and create atoms virtual std::error_code createAtomizableSections(); /// \brief Create mergeable atoms from sections that have the merge attribute /// set virtual std::error_code createMergeableAtoms(); /// \brief Add the symbols that the sections contain. The symbols will be /// converted to atoms for /// Undefined symbols, absolute symbols virtual std::error_code createSymbolsFromAtomizableSections(); /// \brief Create individual atoms virtual std::error_code createAtoms(); const atom_collection &defined() const override { return _definedAtoms; } const atom_collection &undefined() const override { return _undefinedAtoms; } const atom_collection &sharedLibrary() const override { return _sharedLibraryAtoms; } const atom_collection &absolute() const override { return _absoluteAtoms; } Atom *findAtom(const Elf_Sym *symbol) { return _symbolToAtomMapping.lookup(symbol); } protected: ELFDefinedAtom *createDefinedAtomAndAssignRelocations( StringRef symbolName, StringRef sectionName, const Elf_Sym *symbol, const Elf_Shdr *section, ArrayRef symContent, ArrayRef secContent); std::error_code doParse() override; /// \brief Iterate over Elf_Rela relocations list and create references. virtual void createRelocationReferences(const Elf_Sym &symbol, ArrayRef content, range rels); /// \brief Iterate over Elf_Rel relocations list and create references. virtual void createRelocationReferences(const Elf_Sym &symbol, ArrayRef symContent, ArrayRef secContent, range rels); /// \brief After all the Atoms and References are created, update each /// Reference's target with the Atom pointer it refers to. virtual void updateReferences(); /// \brief Update the reference if the access corresponds to a merge string /// section. virtual void updateReferenceForMergeStringAccess(ELFReference *ref, const Elf_Sym *symbol, const Elf_Shdr *shdr); /// \brief Return true if the symbol is corresponding to an architecture /// specific section. We will let the TargetHandler handle such atoms. virtual bool isTargetSpecificAtom(const Elf_Shdr *shdr, const Elf_Sym *sym); /// \brief Do we want to ignore the section. Ignored sections are /// not processed to create atoms virtual bool isIgnoredSection(const Elf_Shdr *section); /// \brief Is the current section be treated as a mergeable string section. /// The contents of a mergeable string section are null-terminated strings. /// If the section have mergeable strings, the linker would need to split /// the section into multiple atoms and mark them mergeByContent. virtual bool isMergeableStringSection(const Elf_Shdr *section); /// \brief Returns a new anonymous atom whose size is equal to the /// section size. That atom will be used to represent the entire /// section that have no symbols. virtual ELFDefinedAtom *createSectionAtom(const Elf_Shdr *section, StringRef sectionName, ArrayRef contents); /// Returns the symbol's content size. The nextSymbol should be null if the /// symbol is the last one in the section. virtual uint64_t symbolContentSize(const Elf_Shdr *section, const Elf_Sym *symbol, const Elf_Sym *nextSymbol); virtual void createEdge(ELFDefinedAtom *from, ELFDefinedAtom *to, uint32_t edgeKind); /// Determines if the reader needs to create atoms for the section. virtual bool ignoreCreateAtomsForSection(const Elf_Shdr *shdr) { return false; } /// Get the section name for a section. virtual ErrorOr getSectionName(const Elf_Shdr *shdr) const { if (!shdr) return StringRef(); return _objFile->getSectionName(shdr); } /// Determines if the section occupy memory space. virtual bool sectionOccupiesMemorySpace(const Elf_Shdr *shdr) const { return (shdr->sh_type != llvm::ELF::SHT_NOBITS); } /// Return the section contents. virtual ErrorOr> getSectionContents(const Elf_Shdr *shdr) const { if (!shdr || !sectionOccupiesMemorySpace(shdr)) return ArrayRef(); return _objFile->getSectionContents(shdr); } /// Returns true if the symbol is a undefined symbol. virtual bool isUndefinedSymbol(const Elf_Sym *sym) const { return (sym->st_shndx == llvm::ELF::SHN_UNDEF); } /// Determines if the target wants to create an atom for a section that has no /// symbol references. virtual bool handleSectionWithNoSymbols(const Elf_Shdr *shdr, std::vector &symbols) const { if (shdr && shdr->sh_type == llvm::ELF::SHT_PROGBITS && symbols.empty()) return true; return false; } /// Process the Undefined symbol and create an atom for it. virtual ErrorOr *> handleUndefinedSymbol(StringRef symName, const Elf_Sym *sym) { return new (_readerStorage) ELFUndefinedAtom(*this, symName, sym); } /// Returns true if the symbol is a absolute symbol. virtual bool isAbsoluteSymbol(const Elf_Sym *sym) const { return (sym->st_shndx == llvm::ELF::SHN_ABS); } /// Process the Absolute symbol and create an atom for it. virtual ErrorOr *> handleAbsoluteSymbol(StringRef symName, const Elf_Sym *sym, int64_t value) { return new (_readerStorage) ELFAbsoluteAtom(*this, symName, sym, value); } /// Returns true if the symbol is common symbol. A common symbol represents a /// tentive definition in C. It has name, size and alignment constraint, but /// actual storage has not yet been allocated. (The linker will allocate /// storage for them in the later pass after coalescing tentative symbols by /// name.) virtual bool isCommonSymbol(const Elf_Sym *symbol) const { return symbol->getType() == llvm::ELF::STT_COMMON || symbol->st_shndx == llvm::ELF::SHN_COMMON; } /// Process the common symbol and create an atom for it. virtual ErrorOr *> handleCommonSymbol(StringRef symName, const Elf_Sym *sym) { return new (_readerStorage) ELFCommonAtom(*this, symName, sym); } /// Returns true if the symbol is a defined symbol. virtual bool isDefinedSymbol(const Elf_Sym *sym) const { return (sym->getType() == llvm::ELF::STT_NOTYPE || sym->getType() == llvm::ELF::STT_OBJECT || sym->getType() == llvm::ELF::STT_FUNC || sym->getType() == llvm::ELF::STT_GNU_IFUNC || sym->getType() == llvm::ELF::STT_SECTION || sym->getType() == llvm::ELF::STT_FILE || sym->getType() == llvm::ELF::STT_TLS); } /// Process the Defined symbol and create an atom for it. virtual ErrorOr *> handleDefinedSymbol(StringRef symName, StringRef sectionName, const Elf_Sym *sym, const Elf_Shdr *sectionHdr, ArrayRef contentData, unsigned int referenceStart, unsigned int referenceEnd, std::vector *> &referenceList) { return new (_readerStorage) ELFDefinedAtom( *this, symName, sectionName, sym, sectionHdr, contentData, referenceStart, referenceEnd, referenceList); } /// Process the Merge string and create an atom for it. virtual ErrorOr *> handleMergeString(StringRef sectionName, const Elf_Shdr *sectionHdr, ArrayRef contentData, unsigned int offset) { ELFMergeAtom *mergeAtom = new (_readerStorage) ELFMergeAtom(*this, sectionName, sectionHdr, contentData, offset); const MergeSectionKey mergedSectionKey(sectionHdr, offset); if (_mergedSectionMap.find(mergedSectionKey) == _mergedSectionMap.end()) _mergedSectionMap.insert(std::make_pair(mergedSectionKey, mergeAtom)); return mergeAtom; } llvm::BumpPtrAllocator _readerStorage; std::unique_ptr > _objFile; atom_collection_vector _definedAtoms; atom_collection_vector _undefinedAtoms; atom_collection_vector _sharedLibraryAtoms; atom_collection_vector _absoluteAtoms; /// \brief _relocationAddendReferences and _relocationReferences contain the /// list of relocations references. In ELF, if a section named, ".text" has /// relocations will also have a section named ".rel.text" or ".rela.text" /// which will hold the entries. std::unordered_map> _relocationAddendReferences; MergedSectionMapT _mergedSectionMap; std::unordered_map> _relocationReferences; std::vector *> _references; llvm::DenseMap _symbolToAtomMapping; /// \brief Atoms that are created for a section that has the merge property /// set MergeAtomsT _mergeAtoms; /// \brief the section and the symbols that are contained within it to create /// used to create atoms std::map> _sectionSymbols; /// \brief Sections that have merge string property std::vector _mergeStringSections; std::unique_ptr _mb; int64_t _ordinal; /// \brief the cached options relevant while reading the ELF File bool _doStringsMerge; }; /// \brief All atoms are owned by a File. To add linker specific atoms /// the atoms need to be inserted to a file called (CRuntimeFile) which /// are basically additional symbols required by libc and other runtime /// libraries part of executing a program. This class provides support /// for adding absolute symbols and undefined symbols template class CRuntimeFile : public ELFFile { public: typedef llvm::object::Elf_Sym_Impl Elf_Sym; CRuntimeFile(const ELFLinkingContext &context, StringRef name = "C runtime") : ELFFile(name) {} /// \brief add a global absolute atom virtual Atom *addAbsoluteAtom(StringRef symbolName) { assert(!symbolName.empty() && "AbsoluteAtoms must have a name"); Elf_Sym *symbol = new (this->_readerStorage) Elf_Sym; symbol->st_name = 0; symbol->st_value = 0; symbol->st_shndx = llvm::ELF::SHN_ABS; symbol->setBindingAndType(llvm::ELF::STB_GLOBAL, llvm::ELF::STT_OBJECT); symbol->st_other = llvm::ELF::STV_DEFAULT; symbol->st_size = 0; auto newAtom = this->handleAbsoluteSymbol(symbolName, symbol, -1); this->_absoluteAtoms._atoms.push_back(*newAtom); return *newAtom; } /// \brief add an undefined atom virtual Atom *addUndefinedAtom(StringRef symbolName) { assert(!symbolName.empty() && "UndefinedAtoms must have a name"); Elf_Sym *symbol = new (this->_readerStorage) Elf_Sym; symbol->st_name = 0; symbol->st_value = 0; symbol->st_shndx = llvm::ELF::SHN_UNDEF; symbol->setBindingAndType(llvm::ELF::STB_GLOBAL, llvm::ELF::STT_NOTYPE); symbol->st_other = llvm::ELF::STV_DEFAULT; symbol->st_size = 0; auto newAtom = this->handleUndefinedSymbol(symbolName, symbol); this->_undefinedAtoms._atoms.push_back(*newAtom); return *newAtom; } // cannot add atoms to C Runtime file virtual void addAtom(const Atom &) { llvm_unreachable("cannot add atoms to Runtime files"); } }; template ErrorOr>> ELFFile::create(std::unique_ptr mb, bool atomizeStrings) { std::unique_ptr> file( new ELFFile(std::move(mb), atomizeStrings)); return std::move(file); } template std::error_code ELFFile::doParse() { std::error_code ec; _objFile.reset(new llvm::object::ELFFile(_mb.release()->getBuffer(), ec)); if (ec) return ec; // Read input sections from the input file that need to be converted to // atoms if ((ec = createAtomizableSections())) return ec; // For mergeable strings, we would need to split the section into various // atoms if ((ec = createMergeableAtoms())) return ec; // Create the necessary symbols that are part of the section that we // created in createAtomizableSections function if ((ec = createSymbolsFromAtomizableSections())) return ec; // Create the appropriate atoms from the file if ((ec = createAtoms())) return ec; return std::error_code(); } template Reference::KindArch ELFFile::kindArch() { switch (_objFile->getHeader()->e_machine) { case llvm::ELF::EM_X86_64: return Reference::KindArch::x86_64; case llvm::ELF::EM_386: return Reference::KindArch::x86; case llvm::ELF::EM_ARM: return Reference::KindArch::ARM; case llvm::ELF::EM_PPC: return Reference::KindArch::PowerPC; case llvm::ELF::EM_HEXAGON: return Reference::KindArch::Hexagon; case llvm::ELF::EM_MIPS: return Reference::KindArch::Mips; case llvm::ELF::EM_AARCH64: return Reference::KindArch::AArch64; } llvm_unreachable("unsupported e_machine value"); } template std::error_code ELFFile::createAtomizableSections() { // Handle: SHT_REL and SHT_RELA sections: // Increment over the sections, when REL/RELA section types are found add // the contents to the RelocationReferences map. // Record the number of relocs to guess at preallocating the buffer. uint64_t totalRelocs = 0; for (const Elf_Shdr §ion : _objFile->sections()) { if (isIgnoredSection(§ion)) continue; if (isMergeableStringSection(§ion)) { _mergeStringSections.push_back(§ion); continue; } // Create a sectionSymbols entry for every progbits section. if ((section.sh_type == llvm::ELF::SHT_PROGBITS) || (section.sh_type == llvm::ELF::SHT_INIT_ARRAY) || (section.sh_type == llvm::ELF::SHT_FINI_ARRAY)) _sectionSymbols[§ion]; if (section.sh_type == llvm::ELF::SHT_RELA) { auto sHdr = _objFile->getSection(section.sh_info); auto sectionName = _objFile->getSectionName(sHdr); if (std::error_code ec = sectionName.getError()) return ec; auto rai(_objFile->begin_rela(§ion)); auto rae(_objFile->end_rela(§ion)); _relocationAddendReferences[*sectionName] = make_range(rai, rae); totalRelocs += std::distance(rai, rae); } if (section.sh_type == llvm::ELF::SHT_REL) { auto sHdr = _objFile->getSection(section.sh_info); auto sectionName = _objFile->getSectionName(sHdr); if (std::error_code ec = sectionName.getError()) return ec; auto ri(_objFile->begin_rel(§ion)); auto re(_objFile->end_rel(§ion)); _relocationReferences[*sectionName] = make_range(ri, re); totalRelocs += std::distance(ri, re); } } _references.reserve(totalRelocs); return std::error_code(); } template std::error_code ELFFile::createMergeableAtoms() { // Divide the section that contains mergeable strings into tokens // TODO // a) add resolver support to recognize multibyte chars // b) Create a separate section chunk to write mergeable atoms std::vector tokens; for (const Elf_Shdr *msi : _mergeStringSections) { auto sectionName = getSectionName(msi); if (std::error_code ec = sectionName.getError()) return ec; auto sectionContents = getSectionContents(msi); if (std::error_code ec = sectionContents.getError()) return ec; StringRef secCont(reinterpret_cast(sectionContents->begin()), sectionContents->size()); unsigned int prev = 0; for (std::size_t i = 0, e = sectionContents->size(); i != e; ++i) { if ((*sectionContents)[i] == '\0') { tokens.push_back(new (_readerStorage) MergeString( prev, secCont.slice(prev, i + 1), msi, *sectionName)); prev = i + 1; } } } // Create Mergeable atoms for (const MergeString *tai : tokens) { ArrayRef content((const uint8_t *)tai->_string.data(), tai->_string.size()); ErrorOr *> mergeAtom = handleMergeString(tai->_sectionName, tai->_shdr, content, tai->_offset); (*mergeAtom)->setOrdinal(++_ordinal); _definedAtoms._atoms.push_back(*mergeAtom); _mergeAtoms.push_back(*mergeAtom); } return std::error_code(); } template std::error_code ELFFile::createSymbolsFromAtomizableSections() { // Increment over all the symbols collecting atoms and symbol names for // later use. auto SymI = _objFile->begin_symbols(), SymE = _objFile->end_symbols(); // Skip over dummy sym. if (SymI != SymE) ++SymI; for (; SymI != SymE; ++SymI) { const Elf_Shdr *section = _objFile->getSection(&*SymI); auto symbolName = _objFile->getSymbolName(SymI); if (std::error_code ec = symbolName.getError()) return ec; if (isAbsoluteSymbol(&*SymI)) { ErrorOr *> absAtom = handleAbsoluteSymbol(*symbolName, &*SymI, SymI->st_value); _absoluteAtoms._atoms.push_back(*absAtom); _symbolToAtomMapping.insert(std::make_pair(&*SymI, *absAtom)); } else if (isUndefinedSymbol(&*SymI)) { ErrorOr *> undefAtom = handleUndefinedSymbol(*symbolName, &*SymI); _undefinedAtoms._atoms.push_back(*undefAtom); _symbolToAtomMapping.insert(std::make_pair(&*SymI, *undefAtom)); } else if (isCommonSymbol(&*SymI)) { ErrorOr *> commonAtom = handleCommonSymbol(*symbolName, &*SymI); (*commonAtom)->setOrdinal(++_ordinal); _definedAtoms._atoms.push_back(*commonAtom); _symbolToAtomMapping.insert(std::make_pair(&*SymI, *commonAtom)); } else if (isDefinedSymbol(&*SymI)) { _sectionSymbols[section].push_back(SymI); } else { llvm::errs() << "Unable to create atom for: " << *symbolName << "\n"; return llvm::object::object_error::parse_failed; } } return std::error_code(); } template std::error_code ELFFile::createAtoms() { for (auto &i : _sectionSymbols) { const Elf_Shdr *section = i.first; // Check if need to create atoms for this section? if ((ignoreCreateAtomsForSection(section))) continue; std::vector &symbols = i.second; // Sort symbols by position. std::stable_sort(symbols.begin(), symbols.end(), [](Elf_Sym_Iter A, Elf_Sym_Iter B) { return A->st_value < B->st_value; }); ErrorOr sectionName = this->getSectionName(section); if (std::error_code ec = sectionName.getError()) return ec; auto sectionContents = getSectionContents(section); if (std::error_code ec = sectionContents.getError()) return ec; if (handleSectionWithNoSymbols(section, symbols)) { ELFDefinedAtom *newAtom = createSectionAtom(section, *sectionName, *sectionContents); _definedAtoms._atoms.push_back(newAtom); newAtom->setOrdinal(++_ordinal); continue; } ELFDefinedAtom *previousAtom = nullptr; ELFDefinedAtom *inGroupAtom = nullptr; ELFReference *anonFollowedBy = nullptr; for (auto si = symbols.begin(), se = symbols.end(); si != se; ++si) { auto symbol = *si; StringRef symbolName = ""; if (symbol->getType() != llvm::ELF::STT_SECTION) { auto symName = _objFile->getSymbolName(symbol); if (std::error_code ec = symName.getError()) return ec; symbolName = *symName; } uint64_t contentSize = symbolContentSize( section, &*symbol, (si + 1 == se) ? nullptr : &**(si + 1)); // Check to see if we need to add the FollowOn Reference ELFReference *followOn = nullptr; if (previousAtom) { // Replace the followon atom with the anonymous atom that we created, // so that the next symbol that we create is a followon from the // anonymous atom. if (anonFollowedBy) { followOn = anonFollowedBy; } else { followOn = new (_readerStorage) ELFReference(lld::Reference::kindLayoutAfter); previousAtom->addReference(followOn); } } ArrayRef symbolData((const uint8_t *)sectionContents->data() + symbol->st_value, contentSize); // If the linker finds that a section has global atoms that are in a // mergeable section, treat them as defined atoms as they shouldn't be // merged away as well as these symbols have to be part of symbol // resolution if (isMergeableStringSection(section)) { if (symbol->getBinding() == llvm::ELF::STB_GLOBAL) { auto definedMergeAtom = handleDefinedSymbol( symbolName, *sectionName, &**si, section, symbolData, _references.size(), _references.size(), _references); _definedAtoms._atoms.push_back(*definedMergeAtom); (*definedMergeAtom)->setOrdinal(++_ordinal); } continue; } // Don't allocate content to a weak symbol, as they may be merged away. // Create an anonymous atom to hold the data. ELFDefinedAtom *anonAtom = nullptr; anonFollowedBy = nullptr; if (symbol->getBinding() == llvm::ELF::STB_WEAK) { // Create anonymous new non-weak ELF symbol that holds the symbol // data. auto sym = new (_readerStorage) Elf_Sym(*symbol); sym->setBinding(llvm::ELF::STB_GLOBAL); anonAtom = createDefinedAtomAndAssignRelocations( "", *sectionName, sym, section, symbolData, *sectionContents); anonAtom->setOrdinal(++_ordinal); symbolData = ArrayRef(); // If this is the last atom, let's not create a followon reference. if (anonAtom && (si + 1) != se) { anonFollowedBy = new (_readerStorage) ELFReference(lld::Reference::kindLayoutAfter); anonAtom->addReference(anonFollowedBy); } } ELFDefinedAtom *newAtom = createDefinedAtomAndAssignRelocations( symbolName, *sectionName, &*symbol, section, symbolData, *sectionContents); newAtom->setOrdinal(++_ordinal); // If the atom was a weak symbol, let's create a followon reference to // the anonymous atom that we created. if (anonAtom) createEdge(newAtom, anonAtom, Reference::kindLayoutAfter); if (previousAtom) { // Set the followon atom to the weak atom that we have created, so // that they would alias when the file gets written. followOn->setTarget(anonAtom ? anonAtom : newAtom); // Add a preceded-by reference only if the current atom is not a weak // atom. if (symbol->getBinding() != llvm::ELF::STB_WEAK) createEdge(newAtom, inGroupAtom, lld::Reference::kindInGroup); } // The previous atom is always the atom created before unless the atom // is a weak atom. previousAtom = anonAtom ? anonAtom : newAtom; if (!inGroupAtom) inGroupAtom = previousAtom; _definedAtoms._atoms.push_back(newAtom); _symbolToAtomMapping.insert(std::make_pair(&*symbol, newAtom)); if (anonAtom) _definedAtoms._atoms.push_back(anonAtom); } } updateReferences(); return std::error_code(); } template ELFDefinedAtom *ELFFile::createDefinedAtomAndAssignRelocations( StringRef symbolName, StringRef sectionName, const Elf_Sym *symbol, const Elf_Shdr *section, ArrayRef symContent, ArrayRef secContent) { unsigned int referenceStart = _references.size(); // Add Rela (those with r_addend) references: auto rari = _relocationAddendReferences.find(sectionName); if (rari != _relocationAddendReferences.end()) createRelocationReferences(*symbol, symContent, rari->second); // Add Rel references. auto rri = _relocationReferences.find(sectionName); if (rri != _relocationReferences.end()) createRelocationReferences(*symbol, symContent, secContent, rri->second); // Create the DefinedAtom and add it to the list of DefinedAtoms. return *handleDefinedSymbol(symbolName, sectionName, symbol, section, symContent, referenceStart, _references.size(), _references); } template void ELFFile::createRelocationReferences(const Elf_Sym &symbol, ArrayRef content, range rels) { bool isMips64EL = _objFile->isMips64EL(); for (const auto &rel : rels) { if (rel.r_offset < symbol.st_value || symbol.st_value + content.size() <= rel.r_offset) continue; _references.push_back(new (_readerStorage) ELFReference( &rel, rel.r_offset - symbol.st_value, kindArch(), rel.getType(isMips64EL), rel.getSymbol(isMips64EL))); } } template void ELFFile::createRelocationReferences(const Elf_Sym &symbol, ArrayRef symContent, ArrayRef secContent, range rels) { bool isMips64EL = _objFile->isMips64EL(); for (const auto &rel : rels) { if (rel.r_offset < symbol.st_value || symbol.st_value + symContent.size() <= rel.r_offset) continue; _references.push_back(new (_readerStorage) ELFReference( &rel, rel.r_offset - symbol.st_value, kindArch(), rel.getType(isMips64EL), rel.getSymbol(isMips64EL))); int32_t addend = *(symContent.data() + rel.r_offset - symbol.st_value); _references.back()->setAddend(addend); } } template void ELFFile::updateReferenceForMergeStringAccess(ELFReference *ref, const Elf_Sym *symbol, const Elf_Shdr *shdr) { // If the target atom is mergeable strefng atom, the atom might have been // merged with other atom having the same contents. Try to find the // merged one if that's the case. int64_t addend = ref->addend(); if (addend < 0) addend = 0; const MergeSectionKey ms(shdr, addend); auto msec = _mergedSectionMap.find(ms); if (msec != _mergedSectionMap.end()) { ref->setTarget(msec->second); return; } // The target atom was not merged. Mergeable atoms are not in // _symbolToAtomMapping, so we cannot find it by calling findAtom(). We // instead call findMergeAtom(). if (symbol->getType() != llvm::ELF::STT_SECTION) addend = symbol->st_value + addend; ELFMergeAtom *mergedAtom = findMergeAtom(shdr, addend); ref->setOffset(addend - mergedAtom->offset()); ref->setAddend(0); ref->setTarget(mergedAtom); } template void ELFFile::updateReferences() { for (auto &ri : _references) { if (ri->kindNamespace() == lld::Reference::KindNamespace::ELF) { const Elf_Sym *symbol = _objFile->getSymbol(ri->targetSymbolIndex()); const Elf_Shdr *shdr = _objFile->getSection(symbol); // If the atom is not in mergeable string section, the target atom is // simply that atom. if (!isMergeableStringSection(shdr)) { ri->setTarget(findAtom(symbol)); continue; } updateReferenceForMergeStringAccess(ri, symbol, shdr); } } } template bool ELFFile::isTargetSpecificAtom(const Elf_Shdr *shdr, const Elf_Sym *sym) { return ((shdr && (shdr->sh_flags & llvm::ELF::SHF_MASKPROC)) || (sym->st_shndx >= llvm::ELF::SHN_LOPROC && sym->st_shndx <= llvm::ELF::SHN_HIPROC)); } template bool ELFFile::isIgnoredSection(const Elf_Shdr *section) { switch (section->sh_type) { case llvm::ELF::SHT_NOTE: case llvm::ELF::SHT_STRTAB: case llvm::ELF::SHT_SYMTAB: case llvm::ELF::SHT_SYMTAB_SHNDX: return true; default: break; } return false; } template bool ELFFile::isMergeableStringSection(const Elf_Shdr *section) { if (_doStringsMerge && section) { int64_t sectionFlags = section->sh_flags; sectionFlags &= ~llvm::ELF::SHF_ALLOC; // Mergeable string sections have both SHF_MERGE and SHF_STRINGS flags // set. sh_entsize is the size of each character which is normally 1. if ((section->sh_entsize < 2) && (sectionFlags == (llvm::ELF::SHF_MERGE | llvm::ELF::SHF_STRINGS))) { return true; } } return false; } template ELFDefinedAtom * ELFFile::createSectionAtom(const Elf_Shdr *section, StringRef sectionName, ArrayRef content) { Elf_Sym *sym = new (_readerStorage) Elf_Sym; sym->st_name = 0; sym->setBindingAndType(llvm::ELF::STB_LOCAL, llvm::ELF::STT_SECTION); sym->st_other = 0; sym->st_shndx = 0; sym->st_value = 0; sym->st_size = 0; auto *newAtom = new (_readerStorage) ELFDefinedAtom( *this, "", sectionName, sym, section, content, 0, 0, _references); newAtom->setOrdinal(++_ordinal); return newAtom; } template uint64_t ELFFile::symbolContentSize(const Elf_Shdr *section, const Elf_Sym *symbol, const Elf_Sym *nextSymbol) { // if this is the last symbol, take up the remaining data. return nextSymbol ? nextSymbol->st_value - symbol->st_value : section->sh_size - symbol->st_value; } template void ELFFile::createEdge(ELFDefinedAtom *from, ELFDefinedAtom *to, uint32_t edgeKind) { auto reference = new (_readerStorage) ELFReference(edgeKind); reference->setTarget(to); from->addReference(reference); } } // end namespace elf } // end namespace lld #endif // LLD_READER_WRITER_ELF_FILE_H