[WebAssembly] Fix asan issue from https://reviews.llvm.org/D121349
[llvm-project.git] / lld / COFF / Driver.h
blob162517340e80bf55b7aac18e01f0deaeb11cd8c9
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 "COFFLinkerContext.h"
13 #include "Config.h"
14 #include "SymbolTable.h"
15 #include "lld/Common/LLVM.h"
16 #include "lld/Common/Reproduce.h"
17 #include "llvm/ADT/Optional.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/StringSet.h"
20 #include "llvm/Object/Archive.h"
21 #include "llvm/Object/COFF.h"
22 #include "llvm/Option/Arg.h"
23 #include "llvm/Option/ArgList.h"
24 #include "llvm/Support/FileSystem.h"
25 #include "llvm/Support/TarWriter.h"
26 #include "llvm/WindowsDriver/MSVCPaths.h"
27 #include <memory>
28 #include <set>
29 #include <vector>
31 namespace lld {
32 namespace coff {
34 extern std::unique_ptr<class LinkerDriver> driver;
36 using llvm::COFF::MachineTypes;
37 using llvm::COFF::WindowsSubsystem;
38 using llvm::Optional;
40 class COFFOptTable : public llvm::opt::OptTable {
41 public:
42 COFFOptTable();
45 // Constructing the option table is expensive. Use a global table to avoid doing
46 // it more than once.
47 extern COFFOptTable optTable;
49 // The result of parsing the .drective section. The /export: and /include:
50 // options are handled separately because they reference symbols, and the number
51 // of symbols can be quite large. The LLVM Option library will perform at least
52 // one memory allocation per argument, and that is prohibitively slow for
53 // parsing directives.
54 struct ParsedDirectives {
55 std::vector<StringRef> exports;
56 std::vector<StringRef> includes;
57 llvm::opt::InputArgList args;
60 class ArgParser {
61 public:
62 // Parses command line options.
63 llvm::opt::InputArgList parse(llvm::ArrayRef<const char *> args);
65 // Tokenizes a given string and then parses as command line options.
66 llvm::opt::InputArgList parse(StringRef s) { return parse(tokenize(s)); }
68 // Tokenizes a given string and then parses as command line options in
69 // .drectve section. /EXPORT options are returned in second element
70 // to be processed in fastpath.
71 ParsedDirectives parseDirectives(StringRef s);
73 private:
74 // Concatenate LINK environment variable.
75 void addLINK(SmallVector<const char *, 256> &argv);
77 std::vector<const char *> tokenize(StringRef s);
80 class LinkerDriver {
81 public:
82 LinkerDriver(COFFLinkerContext &c) : ctx(c) {}
84 void linkerMain(llvm::ArrayRef<const char *> args);
86 // Adds various search paths based on the sysroot. Must only be called once
87 // config->machine has been set.
88 void addWinSysRootLibSearchPaths();
90 // Used by the resolver to parse .drectve section contents.
91 void parseDirectives(InputFile *file);
93 // Used by ArchiveFile to enqueue members.
94 void enqueueArchiveMember(const Archive::Child &c, const Archive::Symbol &sym,
95 StringRef parentName);
97 void enqueuePDB(StringRef Path) { enqueuePath(Path, false, false); }
99 MemoryBufferRef takeBuffer(std::unique_ptr<MemoryBuffer> mb);
101 void enqueuePath(StringRef path, bool wholeArchive, bool lazy);
103 std::unique_ptr<llvm::TarWriter> tar; // for /linkrepro
105 private:
106 // Searches a file from search paths.
107 Optional<StringRef> findFile(StringRef filename);
108 Optional<StringRef> findLib(StringRef filename);
109 StringRef doFindFile(StringRef filename);
110 StringRef doFindLib(StringRef filename);
111 StringRef doFindLibMinGW(StringRef filename);
113 bool findUnderscoreMangle(StringRef sym);
115 // Determines the location of the sysroot based on `args`, environment, etc.
116 void detectWinSysRoot(const llvm::opt::InputArgList &args);
118 // Parses LIB environment which contains a list of search paths.
119 void addLibSearchPaths();
121 // Library search path. The first element is always "" (current directory).
122 std::vector<StringRef> searchPaths;
124 // Convert resource files and potentially merge input resource object
125 // trees into one resource tree.
126 void convertResources();
128 void maybeExportMinGWSymbols(const llvm::opt::InputArgList &args);
130 // We don't want to add the same file more than once.
131 // Files are uniquified by their filesystem and file number.
132 std::set<llvm::sys::fs::UniqueID> visitedFiles;
134 std::set<std::string> visitedLibs;
136 Symbol *addUndefined(StringRef sym);
138 StringRef mangleMaybe(Symbol *s);
140 // Windows specific -- "main" is not the only main function in Windows.
141 // You can choose one from these four -- {w,}{WinMain,main}.
142 // There are four different entry point functions for them,
143 // {w,}{WinMain,main}CRTStartup, respectively. The linker needs to
144 // choose the right one depending on which "main" function is defined.
145 // This function looks up the symbol table and resolve corresponding
146 // entry point name.
147 StringRef findDefaultEntry();
148 WindowsSubsystem inferSubsystem();
150 void addBuffer(std::unique_ptr<MemoryBuffer> mb, bool wholeArchive,
151 bool lazy);
152 void addArchiveBuffer(MemoryBufferRef mbref, StringRef symName,
153 StringRef parentName, uint64_t offsetInArchive);
155 void enqueueTask(std::function<void()> task);
156 bool run();
158 std::list<std::function<void()>> taskQueue;
159 std::vector<StringRef> filePaths;
160 std::vector<MemoryBufferRef> resources;
162 llvm::StringSet<> directivesExports;
164 COFFLinkerContext &ctx;
166 llvm::ToolsetLayout vsLayout = llvm::ToolsetLayout::OlderVS;
167 std::string vcToolChainPath;
168 llvm::SmallString<128> diaPath;
169 bool useWinSysRootLibPath = false;
170 llvm::SmallString<128> universalCRTLibPath;
171 int sdkMajor = 0;
172 llvm::SmallString<128> windowsSdkLibPath;
175 // Functions below this line are defined in DriverUtils.cpp.
177 void printHelp(const char *argv0);
179 // Parses a string in the form of "<integer>[,<integer>]".
180 void parseNumbers(StringRef arg, uint64_t *addr, uint64_t *size = nullptr);
182 void parseGuard(StringRef arg);
184 // Parses a string in the form of "<integer>[.<integer>]".
185 // Minor's default value is 0.
186 void parseVersion(StringRef arg, uint32_t *major, uint32_t *minor);
188 // Parses a string in the form of "<subsystem>[,<integer>[.<integer>]]".
189 void parseSubsystem(StringRef arg, WindowsSubsystem *sys, uint32_t *major,
190 uint32_t *minor, bool *gotVersion = nullptr);
192 void parseAlternateName(StringRef);
193 void parseMerge(StringRef);
194 void parsePDBPageSize(StringRef);
195 void parseSection(StringRef);
196 void parseAligncomm(StringRef);
198 // Parses a string in the form of "[:<integer>]"
199 void parseFunctionPadMin(llvm::opt::Arg *a, llvm::COFF::MachineTypes machine);
201 // Parses a string in the form of "EMBED[,=<integer>]|NO".
202 void parseManifest(StringRef arg);
204 // Parses a string in the form of "level=<string>|uiAccess=<string>"
205 void parseManifestUAC(StringRef arg);
207 // Parses a string in the form of "cd|net[,(cd|net)]*"
208 void parseSwaprun(StringRef arg);
210 // Create a resource file containing a manifest XML.
211 std::unique_ptr<MemoryBuffer> createManifestRes();
212 void createSideBySideManifest();
214 // Used for dllexported symbols.
215 Export parseExport(StringRef arg);
216 void fixupExports();
217 void assignExportOrdinals();
219 // Parses a string in the form of "key=value" and check
220 // if value matches previous values for the key.
221 // This feature used in the directive section to reject
222 // incompatible objects.
223 void checkFailIfMismatch(StringRef arg, InputFile *source);
225 // Convert Windows resource files (.res files) to a .obj file.
226 MemoryBufferRef convertResToCOFF(ArrayRef<MemoryBufferRef> mbs,
227 ArrayRef<ObjFile *> objs);
229 // Create enum with OPT_xxx values for each option in Options.td
230 enum {
231 OPT_INVALID = 0,
232 #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID,
233 #include "Options.inc"
234 #undef OPTION
237 } // namespace coff
238 } // namespace lld
240 #endif