[mlir][py] Enable loading only specified dialects during creation. (#121421)
[llvm-project.git] / lld / COFF / Driver.h
blobb04a00e2d1cd1db3b344ddd73e36f56065495b50
1 //===- Driver.h -------------------------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 #ifndef LLD_COFF_DRIVER_H
10 #define LLD_COFF_DRIVER_H
12 #include "Config.h"
13 #include "SymbolTable.h"
14 #include "lld/Common/LLVM.h"
15 #include "lld/Common/Reproduce.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/ADT/StringSet.h"
18 #include "llvm/Object/Archive.h"
19 #include "llvm/Object/COFF.h"
20 #include "llvm/Option/Arg.h"
21 #include "llvm/Option/ArgList.h"
22 #include "llvm/Support/FileSystem.h"
23 #include "llvm/Support/TarWriter.h"
24 #include "llvm/WindowsDriver/MSVCPaths.h"
25 #include <memory>
26 #include <optional>
27 #include <set>
28 #include <vector>
30 namespace lld::coff {
32 using llvm::COFF::MachineTypes;
33 using llvm::COFF::WindowsSubsystem;
34 using std::optional;
36 class COFFOptTable : public llvm::opt::GenericOptTable {
37 public:
38 COFFOptTable();
41 // The result of parsing the .drective section. The /export: and /include:
42 // options are handled separately because they reference symbols, and the number
43 // of symbols can be quite large. The LLVM Option library will perform at least
44 // one memory allocation per argument, and that is prohibitively slow for
45 // parsing directives.
46 struct ParsedDirectives {
47 std::vector<StringRef> exports;
48 std::vector<StringRef> includes;
49 std::vector<StringRef> excludes;
50 llvm::opt::InputArgList args;
53 class ArgParser {
54 public:
55 ArgParser(COFFLinkerContext &ctx);
57 // Parses command line options.
58 llvm::opt::InputArgList parse(llvm::ArrayRef<const char *> args);
60 // Tokenizes a given string and then parses as command line options.
61 llvm::opt::InputArgList parse(StringRef s) { return parse(tokenize(s)); }
63 // Tokenizes a given string and then parses as command line options in
64 // .drectve section. /EXPORT options are returned in second element
65 // to be processed in fastpath.
66 ParsedDirectives parseDirectives(StringRef s);
68 private:
69 // Concatenate LINK environment variable.
70 void addLINK(SmallVector<const char *, 256> &argv);
72 std::vector<const char *> tokenize(StringRef s);
74 COFFLinkerContext &ctx;
77 class LinkerDriver {
78 public:
79 LinkerDriver(COFFLinkerContext &ctx) : ctx(ctx) {}
81 void linkerMain(llvm::ArrayRef<const char *> args);
83 void addFile(InputFile *file);
85 void addClangLibSearchPaths(const std::string &argv0);
87 // Used by ArchiveFile to enqueue members.
88 void enqueueArchiveMember(const Archive::Child &c, const Archive::Symbol &sym,
89 StringRef parentName);
91 void enqueuePDB(StringRef Path) { enqueuePath(Path, false, false); }
93 MemoryBufferRef takeBuffer(std::unique_ptr<MemoryBuffer> mb);
95 void enqueuePath(StringRef path, bool wholeArchive, bool lazy);
97 std::unique_ptr<llvm::TarWriter> tar; // for /linkrepro
99 void pullArm64ECIcallHelper();
101 private:
102 // Searches a file from search paths.
103 std::optional<StringRef> findFileIfNew(StringRef filename);
104 std::optional<StringRef> findLibIfNew(StringRef filename);
105 StringRef findFile(StringRef filename);
106 StringRef findLib(StringRef filename);
107 StringRef findLibMinGW(StringRef filename);
109 bool findUnderscoreMangle(StringRef sym);
111 // Determines the location of the sysroot based on `args`, environment, etc.
112 void detectWinSysRoot(const llvm::opt::InputArgList &args);
114 // Adds various search paths based on the sysroot. Must only be called once
115 // config->machine has been set.
116 void addWinSysRootLibSearchPaths();
118 // Symbol names are mangled by prepending "_" on x86.
119 StringRef mangle(StringRef sym);
121 void setMachine(llvm::COFF::MachineTypes machine);
122 llvm::Triple::ArchType getArch();
124 uint64_t getDefaultImageBase();
126 bool isDecorated(StringRef sym);
128 std::string getMapFile(const llvm::opt::InputArgList &args,
129 llvm::opt::OptSpecifier os,
130 llvm::opt::OptSpecifier osFile);
132 std::string getImplibPath();
134 // The import name is calculated as follows:
136 // | LIBRARY w/ ext | LIBRARY w/o ext | no LIBRARY
137 // -----+----------------+---------------------+------------------
138 // LINK | {value} | {value}.{.dll/.exe} | {output name}
139 // LIB | {value} | {value}.dll | {output name}.dll
141 std::string getImportName(bool asLib);
143 void createImportLibrary(bool asLib);
145 // Used by the resolver to parse .drectve section contents.
146 void parseDirectives(InputFile *file);
148 void parseModuleDefs(StringRef path);
150 // Parse an /order file. If an option is given, the linker places COMDAT
151 // sections int he same order as their names appear in the given file.
152 void parseOrderFile(StringRef arg);
154 void parseCallGraphFile(StringRef path);
156 void parsePDBAltPath();
158 // Parses LIB environment which contains a list of search paths.
159 void addLibSearchPaths();
161 // Library search path. The first element is always "" (current directory).
162 std::vector<StringRef> searchPaths;
164 // Convert resource files and potentially merge input resource object
165 // trees into one resource tree.
166 void convertResources();
168 void maybeExportMinGWSymbols(const llvm::opt::InputArgList &args);
170 // We don't want to add the same file more than once.
171 // Files are uniquified by their filesystem and file number.
172 std::set<llvm::sys::fs::UniqueID> visitedFiles;
174 std::set<std::string> visitedLibs;
176 Symbol *addUndefined(StringRef sym, bool aliasEC = false);
178 void addUndefinedGlob(StringRef arg);
180 StringRef mangleMaybe(Symbol *s);
182 // Windows specific -- "main" is not the only main function in Windows.
183 // You can choose one from these four -- {w,}{WinMain,main}.
184 // There are four different entry point functions for them,
185 // {w,}{WinMain,main}CRTStartup, respectively. The linker needs to
186 // choose the right one depending on which "main" function is defined.
187 // This function looks up the symbol table and resolve corresponding
188 // entry point name.
189 StringRef findDefaultEntry();
190 WindowsSubsystem inferSubsystem();
192 void addBuffer(std::unique_ptr<MemoryBuffer> mb, bool wholeArchive,
193 bool lazy);
194 void addArchiveBuffer(MemoryBufferRef mbref, StringRef symName,
195 StringRef parentName, uint64_t offsetInArchive);
197 void enqueueTask(std::function<void()> task);
198 bool run();
200 std::list<std::function<void()>> taskQueue;
201 std::vector<MemoryBufferRef> resources;
203 llvm::DenseSet<StringRef> directivesExports;
204 llvm::DenseSet<StringRef> excludedSymbols;
206 COFFLinkerContext &ctx;
208 llvm::ToolsetLayout vsLayout = llvm::ToolsetLayout::OlderVS;
209 std::string vcToolChainPath;
210 llvm::SmallString<128> diaPath;
211 bool useWinSysRootLibPath = false;
212 llvm::SmallString<128> universalCRTLibPath;
213 int sdkMajor = 0;
214 llvm::SmallString<128> windowsSdkLibPath;
216 // Functions below this line are defined in DriverUtils.cpp.
218 void printHelp(const char *argv0);
220 // Parses a string in the form of "<integer>[,<integer>]".
221 void parseNumbers(StringRef arg, uint64_t *addr, uint64_t *size = nullptr);
223 void parseGuard(StringRef arg);
225 // Parses a string in the form of "<integer>[.<integer>]".
226 // Minor's default value is 0.
227 void parseVersion(StringRef arg, uint32_t *major, uint32_t *minor);
229 // Parses a string in the form of "<subsystem>[,<integer>[.<integer>]]".
230 void parseSubsystem(StringRef arg, WindowsSubsystem *sys, uint32_t *major,
231 uint32_t *minor, bool *gotVersion = nullptr);
233 void parseAlternateName(StringRef);
234 void parseMerge(StringRef);
235 void parsePDBPageSize(StringRef);
236 void parseSection(StringRef);
237 void parseAligncomm(StringRef);
239 // Parses a string in the form of "[:<integer>]"
240 void parseFunctionPadMin(llvm::opt::Arg *a);
242 // Parses a string in the form of "[:<integer>]"
243 void parseDependentLoadFlags(llvm::opt::Arg *a);
245 // Parses a string in the form of "EMBED[,=<integer>]|NO".
246 void parseManifest(StringRef arg);
248 // Parses a string in the form of "level=<string>|uiAccess=<string>"
249 void parseManifestUAC(StringRef arg);
251 // Parses a string in the form of "cd|net[,(cd|net)]*"
252 void parseSwaprun(StringRef arg);
254 // Create a resource file containing a manifest XML.
255 std::unique_ptr<MemoryBuffer> createManifestRes();
256 void createSideBySideManifest();
257 std::string createDefaultXml();
258 std::string createManifestXmlWithInternalMt(StringRef defaultXml);
259 std::string createManifestXmlWithExternalMt(StringRef defaultXml);
260 std::string createManifestXml();
262 std::unique_ptr<llvm::WritableMemoryBuffer>
263 createMemoryBufferForManifestRes(size_t manifestRes);
265 // Used for dllexported symbols.
266 Export parseExport(StringRef arg);
267 void fixupExports();
268 void assignExportOrdinals();
270 // Parses a string in the form of "key=value" and check
271 // if value matches previous values for the key.
272 // This feature used in the directive section to reject
273 // incompatible objects.
274 void checkFailIfMismatch(StringRef arg, InputFile *source);
276 // Convert Windows resource files (.res files) to a .obj file.
277 MemoryBufferRef convertResToCOFF(ArrayRef<MemoryBufferRef> mbs,
278 ArrayRef<ObjFile *> objs);
280 // Create export thunks for exported and patchable Arm64EC function symbols.
281 void createECExportThunks();
282 void maybeCreateECExportThunk(StringRef name, Symbol *&sym);
284 bool ltoCompilationDone = false;
287 // Create enum with OPT_xxx values for each option in Options.td
288 enum {
289 OPT_INVALID = 0,
290 #define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
291 #include "Options.inc"
292 #undef OPTION
295 } // namespace lld::coff
297 #endif