1 //===- lld/Core/LinkingContext.h - Linker Target Info Interface -*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLD_CORE_LINKING_CONTEXT_H
10 #define LLD_CORE_LINKING_CONTEXT_H
12 #include "lld/Core/Node.h"
13 #include "lld/Core/Reader.h"
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/Allocator.h"
17 #include "llvm/Support/Error.h"
30 class SharedLibraryFile;
32 /// The LinkingContext class encapsulates "what and how" to link.
34 /// The base class LinkingContext contains the options needed by core linking.
35 /// Subclasses of LinkingContext have additional options needed by specific
37 class LinkingContext {
39 virtual ~LinkingContext();
41 /// \name Methods needed by core linking
44 /// Name of symbol linker should use as "entry point" to program,
45 /// usually "main" or "start".
46 virtual StringRef entrySymbolName() const { return _entrySymbolName; }
48 /// Whether core linking should remove Atoms not reachable by following
49 /// References from the entry point Atom or from all global scope Atoms
50 /// if globalsAreDeadStripRoots() is true.
51 bool deadStrip() const { return _deadStrip; }
53 /// Only used if deadStrip() returns true. Means all global scope Atoms
54 /// should be marked live (along with all Atoms they reference). Usually
55 /// this method returns false for main executables, but true for dynamic
57 bool globalsAreDeadStripRoots() const { return _globalsAreDeadStripRoots; }
59 /// Only used if deadStrip() returns true. This method returns the names
60 /// of DefinedAtoms that should be marked live (along with all Atoms they
61 /// reference). Only Atoms with scope scopeLinkageUnit or scopeGlobal can
62 /// be kept live using this method.
63 ArrayRef<StringRef> deadStripRoots() const {
64 return _deadStripRoots;
67 /// Add the given symbol name to the dead strip root set. Only used if
68 /// deadStrip() returns true.
69 void addDeadStripRoot(StringRef symbolName) {
70 assert(!symbolName.empty() && "Empty symbol cannot be a dead strip root");
71 _deadStripRoots.push_back(symbolName);
74 /// Normally, every UndefinedAtom must be replaced by a DefinedAtom or a
75 /// SharedLibraryAtom for the link to be successful. This method controls
76 /// whether core linking prints out a list of remaining UndefinedAtoms.
78 /// \todo This should be a method core linking calls with a list of the
79 /// UndefinedAtoms so that different drivers can format the error message
81 bool printRemainingUndefines() const { return _printRemainingUndefines; }
83 /// Normally, every UndefinedAtom must be replaced by a DefinedAtom or a
84 /// SharedLibraryAtom for the link to be successful. This method controls
85 /// whether core linking considers remaining undefines to be an error.
86 bool allowRemainingUndefines() const { return _allowRemainingUndefines; }
88 /// Normally, every UndefinedAtom must be replaced by a DefinedAtom or a
89 /// SharedLibraryAtom for the link to be successful. This method controls
90 /// whether core linking considers remaining undefines from the shared library
92 bool allowShlibUndefines() const { return _allowShlibUndefines; }
94 /// If true, core linking will write the path to each input file to stdout
95 /// (i.e. llvm::outs()) as it is used. This is used to implement the -t
98 /// \todo This should be a method core linking calls so that drivers can
99 /// format the line as needed.
100 bool logInputFiles() const { return _logInputFiles; }
102 /// Parts of LLVM use global variables which are bound to command line
103 /// options (see llvm::cl::Options). This method returns "command line"
104 /// options which are used to configure LLVM's command line settings.
105 /// For instance the -debug-only XXX option can be used to dynamically
106 /// trace different parts of LLVM and lld.
107 ArrayRef<const char *> llvmOptions() const { return _llvmOptions; }
109 /// \name Methods used by Drivers to configure TargetInfo
111 void setOutputPath(StringRef str) { _outputPath = str; }
113 // Set the entry symbol name. You may also need to call addDeadStripRoot() for
114 // the symbol if your platform supports dead-stripping, so that the symbol
115 // will not be removed from the output.
116 void setEntrySymbolName(StringRef name) {
117 _entrySymbolName = name;
120 void setDeadStripping(bool enable) { _deadStrip = enable; }
121 void setGlobalsAreDeadStripRoots(bool v) { _globalsAreDeadStripRoots = v; }
123 void setPrintRemainingUndefines(bool print) {
124 _printRemainingUndefines = print;
127 void setAllowRemainingUndefines(bool allow) {
128 _allowRemainingUndefines = allow;
131 void setAllowShlibUndefines(bool allow) { _allowShlibUndefines = allow; }
132 void setLogInputFiles(bool log) { _logInputFiles = log; }
134 void appendLLVMOption(const char *opt) { _llvmOptions.push_back(opt); }
136 std::vector<std::unique_ptr<Node>> &getNodes() { return _nodes; }
137 const std::vector<std::unique_ptr<Node>> &getNodes() const { return _nodes; }
139 /// This method adds undefined symbols specified by the -u option to the to
140 /// the list of undefined symbols known to the linker. This option essentially
141 /// forces an undefined symbol to be created. You may also need to call
142 /// addDeadStripRoot() for the symbol if your platform supports dead
143 /// stripping, so that the symbol will not be removed from the output.
144 void addInitialUndefinedSymbol(StringRef symbolName) {
145 _initialUndefinedSymbols.push_back(symbolName);
148 /// Iterators for symbols that appear on the command line.
149 typedef std::vector<StringRef> StringRefVector;
150 typedef StringRefVector::iterator StringRefVectorIter;
151 typedef StringRefVector::const_iterator StringRefVectorConstIter;
153 /// Create linker internal files containing atoms for the linker to include
154 /// during link. Flavors can override this function in their LinkingContext
155 /// to add more internal files. These internal files are positioned before
156 /// the actual input files.
157 virtual void createInternalFiles(std::vector<std::unique_ptr<File>> &) const;
159 /// Return the list of undefined symbols that are specified in the
160 /// linker command line, using the -u option.
161 ArrayRef<StringRef> initialUndefinedSymbols() const {
162 return _initialUndefinedSymbols;
165 /// After all set* methods are called, the Driver calls this method
166 /// to validate that there are no missing options or invalid combinations
167 /// of options. If there is a problem, a description of the problem
168 /// is written to the global error handler.
170 /// \returns true if there is an error with the current settings.
173 /// Formats symbol name for use in error messages.
174 virtual std::string demangle(StringRef symbolName) const = 0;
177 /// \name Methods used by Driver::link()
180 /// Returns the file system path to which the linked output should be written.
182 /// \todo To support in-memory linking, we need an abstraction that allows
183 /// the linker to write to an in-memory buffer.
184 StringRef outputPath() const { return _outputPath; }
186 /// Accessor for Register object embedded in LinkingContext.
187 const Registry ®istry() const { return _registry; }
188 Registry ®istry() { return _registry; }
190 /// This method is called by core linking to give the Writer a chance
191 /// to add file format specific "files" to set of files to be linked. This is
192 /// how file format specific atoms can be added to the link.
193 virtual void createImplicitFiles(std::vector<std::unique_ptr<File>> &) = 0;
195 /// This method is called by core linking to build the list of Passes to be
196 /// run on the merged/linked graph of all input files.
197 virtual void addPasses(PassManager &pm) = 0;
199 /// Calls through to the writeFile() method on the specified Writer.
201 /// \param linkedFile This is the merged/linked graph of all input file Atoms.
202 virtual llvm::Error writeFile(const File &linkedFile) const;
204 /// Return the next ordinal and Increment it.
205 virtual uint64_t getNextOrdinalAndIncrement() const { return _nextOrdinal++; }
207 // This function is called just before the Resolver kicks in.
208 // Derived classes may use it to change the list of input files.
209 virtual void finalizeInputFiles() = 0;
211 /// Callback invoked for each file the Resolver decides we are going to load.
212 /// This can be used to update context state based on the file, and emit
213 /// errors for any differences between the context state and a loaded file.
214 /// For example, we can error if we try to load a file which is a different
215 /// arch from that being linked.
216 virtual llvm::Error handleLoadedFile(File &file) = 0;
220 LinkingContext(); // Must be subclassed
222 /// Abstract method to lazily instantiate the Writer.
223 virtual Writer &writer() const = 0;
225 /// Method to create an internal file for the entry symbol
226 virtual std::unique_ptr<File> createEntrySymbolFile() const;
227 std::unique_ptr<File> createEntrySymbolFile(StringRef filename) const;
229 /// Method to create an internal file for an undefined symbol
230 virtual std::unique_ptr<File> createUndefinedSymbolFile() const;
231 std::unique_ptr<File> createUndefinedSymbolFile(StringRef filename) const;
233 StringRef _outputPath;
234 StringRef _entrySymbolName;
235 bool _deadStrip = false;
236 bool _globalsAreDeadStripRoots = false;
237 bool _printRemainingUndefines = true;
238 bool _allowRemainingUndefines = false;
239 bool _logInputFiles = false;
240 bool _allowShlibUndefines = false;
241 std::vector<StringRef> _deadStripRoots;
242 std::vector<const char *> _llvmOptions;
243 StringRefVector _initialUndefinedSymbols;
244 std::vector<std::unique_ptr<Node>> _nodes;
245 mutable llvm::BumpPtrAllocator _allocator;
246 mutable uint64_t _nextOrdinal = 0;
250 /// Validate the subclass bits. Only called by validate.
251 virtual bool validateImpl() = 0;
254 } // end namespace lld
256 #endif // LLD_CORE_LINKING_CONTEXT_H