//===- Core/Resolver.h - Resolves Atom References -------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #ifndef LLD_CORE_RESOLVER_H #define LLD_CORE_RESOLVER_H #include "lld/Core/ArchiveLibraryFile.h" #include "lld/Core/File.h" #include "lld/Core/SharedLibraryFile.h" #include "lld/Core/Simple.h" #include "lld/Core/SymbolTable.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseSet.h" #include "llvm/Support/ErrorOr.h" #include #include #include #include namespace lld { class Atom; class LinkingContext; /// The Resolver is responsible for merging all input object files /// and producing a merged graph. class Resolver { public: Resolver(LinkingContext &ctx) : _ctx(ctx), _result(new MergedFile()) {} // InputFiles::Handler methods void doDefinedAtom(OwningAtomPtr atom); bool doUndefinedAtom(OwningAtomPtr atom); void doSharedLibraryAtom(OwningAtomPtr atom); void doAbsoluteAtom(OwningAtomPtr atom); // Handle files, this adds atoms from the current file thats // being processed by the resolver llvm::Expected handleFile(File &); // Handle an archive library file. llvm::Expected handleArchiveFile(File &); // Handle a shared library file. llvm::Error handleSharedLibrary(File &); /// do work of merging and resolving and return list bool resolve(); std::unique_ptr resultFile() { return std::move(_result); } private: typedef std::function(StringRef)> UndefCallback; bool undefinesAdded(int begin, int end); File *getFile(int &index); /// The main function that iterates over the files to resolve bool resolveUndefines(); void updateReferences(); void deadStripOptimize(); bool checkUndefines(); void removeCoalescedAwayAtoms(); llvm::Expected forEachUndefines(File &file, UndefCallback callback); void markLive(const Atom *atom); class MergedFile : public SimpleFile { public: MergedFile() : SimpleFile("", kindResolverMergedObject) {} void addAtoms(llvm::MutableArrayRef> atoms); }; LinkingContext &_ctx; SymbolTable _symbolTable; std::vector> _atoms; std::set _deadStripRoots; llvm::DenseSet _liveAtoms; llvm::DenseSet _deadAtoms; std::unique_ptr _result; std::unordered_multimap _reverseRef; // --start-group and --end-group std::vector _files; std::map _newUndefinesAdded; // List of undefined symbols. std::vector _undefines; // Start position in _undefines for each archive/shared library file. // Symbols from index 0 to the start position are already searched before. // Searching them again would never succeed. When we look for undefined // symbols from an archive/shared library file, start from its start // position to save time. std::map _undefineIndex; }; } // namespace lld #endif // LLD_CORE_RESOLVER_H