Revert "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)
[llvm-project.git] / lld / COFF / Driver.h
blob3889feb7511c0a2f4b71ea5267a0f9ede445384a
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 // Adds various search paths based on the sysroot. Must only be called once
84 // config->machine has been set.
85 void addWinSysRootLibSearchPaths();
87 void addClangLibSearchPaths(const std::string &argv0);
89 // Used by the resolver to parse .drectve section contents.
90 void parseDirectives(InputFile *file);
92 // Used by ArchiveFile to enqueue members.
93 void enqueueArchiveMember(const Archive::Child &c, const Archive::Symbol &sym,
94 StringRef parentName);
96 void enqueuePDB(StringRef Path) { enqueuePath(Path, false, false); }
98 MemoryBufferRef takeBuffer(std::unique_ptr<MemoryBuffer> mb);
100 void enqueuePath(StringRef path, bool wholeArchive, bool lazy);
102 std::unique_ptr<llvm::TarWriter> tar; // for /linkrepro
104 void pullArm64ECIcallHelper();
106 private:
107 // Searches a file from search paths.
108 std::optional<StringRef> findFileIfNew(StringRef filename);
109 std::optional<StringRef> findLibIfNew(StringRef filename);
110 StringRef findFile(StringRef filename);
111 StringRef findLib(StringRef filename);
112 StringRef findLibMinGW(StringRef filename);
114 bool findUnderscoreMangle(StringRef sym);
116 // Determines the location of the sysroot based on `args`, environment, etc.
117 void detectWinSysRoot(const llvm::opt::InputArgList &args);
119 // Symbol names are mangled by prepending "_" on x86.
120 StringRef mangle(StringRef sym);
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 void parseModuleDefs(StringRef path);
147 // Parse an /order file. If an option is given, the linker places COMDAT
148 // sections int he same order as their names appear in the given file.
149 void parseOrderFile(StringRef arg);
151 void parseCallGraphFile(StringRef path);
153 void parsePDBAltPath();
155 // Parses LIB environment which contains a list of search paths.
156 void addLibSearchPaths();
158 // Library search path. The first element is always "" (current directory).
159 std::vector<StringRef> searchPaths;
161 // Convert resource files and potentially merge input resource object
162 // trees into one resource tree.
163 void convertResources();
165 void maybeExportMinGWSymbols(const llvm::opt::InputArgList &args);
167 // We don't want to add the same file more than once.
168 // Files are uniquified by their filesystem and file number.
169 std::set<llvm::sys::fs::UniqueID> visitedFiles;
171 std::set<std::string> visitedLibs;
173 Symbol *addUndefined(StringRef sym, bool aliasEC = false);
175 void addUndefinedGlob(StringRef arg);
177 StringRef mangleMaybe(Symbol *s);
179 // Windows specific -- "main" is not the only main function in Windows.
180 // You can choose one from these four -- {w,}{WinMain,main}.
181 // There are four different entry point functions for them,
182 // {w,}{WinMain,main}CRTStartup, respectively. The linker needs to
183 // choose the right one depending on which "main" function is defined.
184 // This function looks up the symbol table and resolve corresponding
185 // entry point name.
186 StringRef findDefaultEntry();
187 WindowsSubsystem inferSubsystem();
189 void addBuffer(std::unique_ptr<MemoryBuffer> mb, bool wholeArchive,
190 bool lazy);
191 void addArchiveBuffer(MemoryBufferRef mbref, StringRef symName,
192 StringRef parentName, uint64_t offsetInArchive);
194 void enqueueTask(std::function<void()> task);
195 bool run();
197 std::list<std::function<void()>> taskQueue;
198 std::vector<StringRef> filePaths;
199 std::vector<MemoryBufferRef> resources;
201 llvm::DenseSet<StringRef> directivesExports;
202 llvm::DenseSet<StringRef> excludedSymbols;
204 COFFLinkerContext &ctx;
206 llvm::ToolsetLayout vsLayout = llvm::ToolsetLayout::OlderVS;
207 std::string vcToolChainPath;
208 llvm::SmallString<128> diaPath;
209 bool useWinSysRootLibPath = false;
210 llvm::SmallString<128> universalCRTLibPath;
211 int sdkMajor = 0;
212 llvm::SmallString<128> windowsSdkLibPath;
214 // Functions below this line are defined in DriverUtils.cpp.
216 void printHelp(const char *argv0);
218 // Parses a string in the form of "<integer>[,<integer>]".
219 void parseNumbers(StringRef arg, uint64_t *addr, uint64_t *size = nullptr);
221 void parseGuard(StringRef arg);
223 // Parses a string in the form of "<integer>[.<integer>]".
224 // Minor's default value is 0.
225 void parseVersion(StringRef arg, uint32_t *major, uint32_t *minor);
227 // Parses a string in the form of "<subsystem>[,<integer>[.<integer>]]".
228 void parseSubsystem(StringRef arg, WindowsSubsystem *sys, uint32_t *major,
229 uint32_t *minor, bool *gotVersion = nullptr);
231 void parseAlternateName(StringRef);
232 void parseMerge(StringRef);
233 void parsePDBPageSize(StringRef);
234 void parseSection(StringRef);
235 void parseAligncomm(StringRef);
237 // Parses a string in the form of "[:<integer>]"
238 void parseFunctionPadMin(llvm::opt::Arg *a);
240 // Parses a string in the form of "[:<integer>]"
241 void parseDependentLoadFlags(llvm::opt::Arg *a);
243 // Parses a string in the form of "EMBED[,=<integer>]|NO".
244 void parseManifest(StringRef arg);
246 // Parses a string in the form of "level=<string>|uiAccess=<string>"
247 void parseManifestUAC(StringRef arg);
249 // Parses a string in the form of "cd|net[,(cd|net)]*"
250 void parseSwaprun(StringRef arg);
252 // Create a resource file containing a manifest XML.
253 std::unique_ptr<MemoryBuffer> createManifestRes();
254 void createSideBySideManifest();
255 std::string createDefaultXml();
256 std::string createManifestXmlWithInternalMt(StringRef defaultXml);
257 std::string createManifestXmlWithExternalMt(StringRef defaultXml);
258 std::string createManifestXml();
260 std::unique_ptr<llvm::WritableMemoryBuffer>
261 createMemoryBufferForManifestRes(size_t manifestRes);
263 // Used for dllexported symbols.
264 Export parseExport(StringRef arg);
265 void fixupExports();
266 void assignExportOrdinals();
268 // Parses a string in the form of "key=value" and check
269 // if value matches previous values for the key.
270 // This feature used in the directive section to reject
271 // incompatible objects.
272 void checkFailIfMismatch(StringRef arg, InputFile *source);
274 // Convert Windows resource files (.res files) to a .obj file.
275 MemoryBufferRef convertResToCOFF(ArrayRef<MemoryBufferRef> mbs,
276 ArrayRef<ObjFile *> objs);
278 // Create export thunks for exported and patchable Arm64EC function symbols.
279 void createECExportThunks();
280 void maybeCreateECExportThunk(StringRef name, Symbol *&sym);
283 // Create enum with OPT_xxx values for each option in Options.td
284 enum {
285 OPT_INVALID = 0,
286 #define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
287 #include "Options.inc"
288 #undef OPTION
291 } // namespace lld::coff
293 #endif