blob: 14c97a98875bf132d50137348be9b918f178c220 [file] [log] [blame]
Rafael Espindolabeee25e2015-08-14 14:12:541//===- Driver.h -------------------------------------------------*- C++ -*-===//
Rui Ueyama411c63602015-05-28 19:09:302//
Chandler Carruth2946cd72019-01-19 08:50:563// 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
Rui Ueyama411c63602015-05-28 19:09:306//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLD_COFF_DRIVER_H
10#define LLD_COFF_DRIVER_H
11
Rui Ueyama97dff9e2015-06-17 00:16:3312#include "Config.h"
Rui Ueyama0d2e9992015-06-23 23:56:3913#include "SymbolTable.h"
Rui Ueyama3f851702017-10-02 21:00:4114#include "lld/Common/LLVM.h"
15#include "lld/Common/Reproduce.h"
Rui Ueyama411c63602015-05-28 19:09:3016#include "llvm/ADT/StringRef.h"
Rui Ueyamaa63eed02017-12-28 07:41:1917#include "llvm/ADT/StringSet.h"
Peter Collingbourne6ee0b4e2016-12-15 04:02:2318#include "llvm/Object/Archive.h"
Rui Ueyama3500f662015-05-28 20:30:0619#include "llvm/Object/COFF.h"
20#include "llvm/Option/Arg.h"
21#include "llvm/Option/ArgList.h"
Rui Ueyama4eed6cc2018-06-12 21:47:3122#include "llvm/Support/FileSystem.h"
Rui Ueyama7f1f9122017-01-06 02:33:5323#include "llvm/Support/TarWriter.h"
Peter Kastingc5fb05f2022-02-16 14:20:0324#include "llvm/WindowsDriver/MSVCPaths.h"
Rui Ueyama411c63602015-05-28 19:09:3025#include <memory>
Fangrui Song9da7aee2022-11-28 00:39:4026#include <optional>
Rui Ueyamaa9cbbf82015-05-31 19:17:0927#include <set>
Rui Ueyama411c63602015-05-28 19:09:3028#include <vector>
29
Nico Weber7c266412022-08-08 15:32:2630namespace lld::coff {
Rui Ueyama411c63602015-05-28 19:09:3031
Rui Ueyama3d3e6fb2015-05-29 16:06:0032using llvm::COFF::MachineTypes;
Rui Ueyama15cc47e2015-05-29 16:34:3133using llvm::COFF::WindowsSubsystem;
Fangrui Song9da7aee2022-11-28 00:39:4034using std::optional;
Rui Ueyama411c63602015-05-28 19:09:3035
serge-sans-paille07bb29d2022-12-30 07:32:5936class COFFOptTable : public llvm::opt::GenericOptTable {
Rui Ueyamaaffb40e2017-08-28 20:46:3037public:
38 COFFOptTable();
39};
40
Reid Kleckner01b5f522020-04-25 00:26:1741// 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.
46struct ParsedDirectives {
47 std::vector<StringRef> exports;
48 std::vector<StringRef> includes;
Martin Storsjö5d513ef2022-07-17 21:11:3749 std::vector<StringRef> excludes;
Reid Kleckner01b5f522020-04-25 00:26:1750 llvm::opt::InputArgList args;
51};
52
Rui Ueyama115d7c12015-06-07 02:55:1953class ArgParser {
54public:
Amy Huang5a58b192023-01-10 04:37:2855 ArgParser(COFFLinkerContext &ctx);
56
Nico Weberd48ea5d2019-09-13 13:13:5257 // Parses command line options.
58 llvm::opt::InputArgList parse(llvm::ArrayRef<const char *> args);
Rui Ueyama06cf3df2015-06-28 02:35:3159
Rui Ueyama115d7c12015-06-07 02:55:1960 // Tokenizes a given string and then parses as command line options.
Rui Ueyama136d27a2019-07-11 05:40:3061 llvm::opt::InputArgList parse(StringRef s) { return parse(tokenize(s)); }
Rui Ueyama115d7c12015-06-07 02:55:1962
Rui Ueyama130eb042017-12-27 06:08:1063 // Tokenizes a given string and then parses as command line options in
Rui Ueyama5fa0d6e2018-01-09 20:36:4264 // .drectve section. /EXPORT options are returned in second element
65 // to be processed in fastpath.
Reid Kleckner01b5f522020-04-25 00:26:1766 ParsedDirectives parseDirectives(StringRef s);
Rui Ueyama130eb042017-12-27 06:08:1067
Rui Ueyama115d7c12015-06-07 02:55:1968private:
Nico Weberd48ea5d2019-09-13 13:13:5269 // Concatenate LINK environment variable.
70 void addLINK(SmallVector<const char *, 256> &argv);
Nico Webera05cbb82017-09-05 23:46:4571
Rui Ueyama136d27a2019-07-11 05:40:3072 std::vector<const char *> tokenize(StringRef s);
Amy Huang5a58b192023-01-10 04:37:2873
74 COFFLinkerContext &ctx;
Rui Ueyama115d7c12015-06-07 02:55:1975};
76
Rui Ueyamaa9cbbf82015-05-31 19:17:0977class LinkerDriver {
78public:
Amy Huang5a58b192023-01-10 04:37:2879 LinkerDriver(COFFLinkerContext &ctx) : ctx(ctx) {}
Amy Huang6f7483b2021-09-16 23:48:2680
Reshabh Sharmafdd6ed82020-12-18 06:39:0181 void linkerMain(llvm::ArrayRef<const char *> args);
Rui Ueyamaa9cbbf82015-05-31 19:17:0982
Jacek Caban84352252025-01-01 18:42:4983 void addFile(InputFile *file);
Peter Kastingc5fb05f2022-02-16 14:20:0384
Tobias Hietaaf744f02023-07-14 07:46:5485 void addClangLibSearchPaths(const std::string &argv0);
86
Peter Collingbourne6ee0b4e2016-12-15 04:02:2387 // Used by ArchiveFile to enqueue members.
Nico Webercb2c5002019-07-19 13:29:1088 void enqueueArchiveMember(const Archive::Child &c, const Archive::Symbol &sym,
Rui Ueyama136d27a2019-07-11 05:40:3089 StringRef parentName);
Peter Collingbournefeee2102016-07-26 02:00:4290
Reid Kleckner54a335a2020-05-09 13:58:1591 void enqueuePDB(StringRef Path) { enqueuePath(Path, false, false); }
92
Rui Ueyama136d27a2019-07-11 05:40:3093 MemoryBufferRef takeBuffer(std::unique_ptr<MemoryBuffer> mb);
Peter Collingbourne75257bc2017-10-20 19:48:2694
Bob Haarman7dc5e7a02019-09-03 20:32:1695 void enqueuePath(StringRef path, bool wholeArchive, bool lazy);
Alexandre Ganea9c78db62019-06-03 12:39:4796
Jacek Caban1bd5f342025-01-16 11:55:1297 // Returns a list of chunks of selected symbols.
98 std::vector<Chunk *> getChunks() const;
99
Rui Ueyama136d27a2019-07-11 05:40:30100 std::unique_ptr<llvm::TarWriter> tar; // for /linkrepro
Peter Collingbourne6ee0b4e2016-12-15 04:02:23101
Jacek Caban99a23542024-09-11 12:46:40102 void pullArm64ECIcallHelper();
103
Nico Webere6d1f262021-02-22 19:29:55104private:
Rui Ueyama54b71da2015-05-31 19:17:12105 // Searches a file from search paths.
Arthur Eubanks5f8f3102023-06-06 18:11:52106 std::optional<StringRef> findFileIfNew(StringRef filename);
107 std::optional<StringRef> findLibIfNew(StringRef filename);
108 StringRef findFile(StringRef filename);
109 StringRef findLib(StringRef filename);
110 StringRef findLibMinGW(StringRef filename);
Rui Ueyama54b71da2015-05-31 19:17:12111
Peter Kastingc5fb05f2022-02-16 14:20:03112 // Determines the location of the sysroot based on `args`, environment, etc.
113 void detectWinSysRoot(const llvm::opt::InputArgList &args);
114
Jacek Caban0a9810d2024-12-15 17:41:26115 // Adds various search paths based on the sysroot. Must only be called once
Fangrui Song5aef8ab2025-01-07 07:06:33116 // config.machine has been set.
Jacek Caban0a9810d2024-12-15 17:41:26117 void addWinSysRootLibSearchPaths();
118
Jacek Caban84352252025-01-01 18:42:49119 void setMachine(llvm::COFF::MachineTypes machine);
Amy Huang5a58b192023-01-10 04:37:28120 llvm::Triple::ArchType getArch();
121
122 uint64_t getDefaultImageBase();
123
124 bool isDecorated(StringRef sym);
125
126 std::string getMapFile(const llvm::opt::InputArgList &args,
127 llvm::opt::OptSpecifier os,
128 llvm::opt::OptSpecifier osFile);
129
130 std::string getImplibPath();
131
132 // The import name is calculated as follows:
133 //
134 // | LIBRARY w/ ext | LIBRARY w/o ext | no LIBRARY
135 // -----+----------------+---------------------+------------------
136 // LINK | {value} | {value}.{.dll/.exe} | {output name}
137 // LIB | {value} | {value}.dll | {output name}.dll
138 //
139 std::string getImportName(bool asLib);
140
141 void createImportLibrary(bool asLib);
142
Jacek Caban84352252025-01-01 18:42:49143 // Used by the resolver to parse .drectve section contents.
144 void parseDirectives(InputFile *file);
145
Amy Huang5a58b192023-01-10 04:37:28146 // Parse an /order file. If an option is given, the linker places COMDAT
147 // sections int he same order as their names appear in the given file.
148 void parseOrderFile(StringRef arg);
149
150 void parseCallGraphFile(StringRef path);
151
152 void parsePDBAltPath();
153
Rui Ueyama54b71da2015-05-31 19:17:12154 // Parses LIB environment which contains a list of search paths.
Rui Ueyamaf00df0a2015-06-19 22:39:48155 void addLibSearchPaths();
Rui Ueyama54b71da2015-05-31 19:17:12156
Rui Ueyamaf00df0a2015-06-19 22:39:48157 // Library search path. The first element is always "" (current directory).
Rui Ueyama136d27a2019-07-11 05:40:30158 std::vector<StringRef> searchPaths;
Rui Ueyama4eed6cc2018-06-12 21:47:31159
Martin Storsjo3d3a9b32019-08-30 06:56:33160 // Convert resource files and potentially merge input resource object
161 // trees into one resource tree.
162 void convertResources();
163
Rui Ueyama136d27a2019-07-11 05:40:30164 void maybeExportMinGWSymbols(const llvm::opt::InputArgList &args);
Rui Ueyama659f2752019-02-19 22:06:44165
Rui Ueyama4eed6cc2018-06-12 21:47:31166 // We don't want to add the same file more than once.
167 // Files are uniquified by their filesystem and file number.
Rui Ueyama136d27a2019-07-11 05:40:30168 std::set<llvm::sys::fs::UniqueID> visitedFiles;
Rui Ueyama4eed6cc2018-06-12 21:47:31169
Rui Ueyama136d27a2019-07-11 05:40:30170 std::set<std::string> visitedLibs;
Rui Ueyamad7c2f582015-05-31 21:04:56171
Bob Haarman7dc5e7a02019-09-03 20:32:16172 void addBuffer(std::unique_ptr<MemoryBuffer> mb, bool wholeArchive,
173 bool lazy);
Rui Ueyama136d27a2019-07-11 05:40:30174 void addArchiveBuffer(MemoryBufferRef mbref, StringRef symName,
175 StringRef parentName, uint64_t offsetInArchive);
Peter Collingbourne6ee0b4e2016-12-15 04:02:23176
Rui Ueyama136d27a2019-07-11 05:40:30177 void enqueueTask(std::function<void()> task);
Peter Collingbourne6ee0b4e2016-12-15 04:02:23178 bool run();
179
Rui Ueyama136d27a2019-07-11 05:40:30180 std::list<std::function<void()>> taskQueue;
Rui Ueyama136d27a2019-07-11 05:40:30181 std::vector<MemoryBufferRef> resources;
Rui Ueyamaa63eed02017-12-28 07:41:19182
Martin Storsjö5d513ef2022-07-17 21:11:37183 llvm::DenseSet<StringRef> excludedSymbols;
Amy Huang6f7483b2021-09-16 23:48:26184
185 COFFLinkerContext &ctx;
Peter Kastingc5fb05f2022-02-16 14:20:03186
187 llvm::ToolsetLayout vsLayout = llvm::ToolsetLayout::OlderVS;
188 std::string vcToolChainPath;
189 llvm::SmallString<128> diaPath;
190 bool useWinSysRootLibPath = false;
191 llvm::SmallString<128> universalCRTLibPath;
192 int sdkMajor = 0;
193 llvm::SmallString<128> windowsSdkLibPath;
Amy Huang5a58b192023-01-10 04:37:28194
195 // Functions below this line are defined in DriverUtils.cpp.
196
197 void printHelp(const char *argv0);
198
199 // Parses a string in the form of "<integer>[,<integer>]".
200 void parseNumbers(StringRef arg, uint64_t *addr, uint64_t *size = nullptr);
201
202 void parseGuard(StringRef arg);
203
204 // Parses a string in the form of "<integer>[.<integer>]".
205 // Minor's default value is 0.
206 void parseVersion(StringRef arg, uint32_t *major, uint32_t *minor);
207
208 // Parses a string in the form of "<subsystem>[,<integer>[.<integer>]]".
209 void parseSubsystem(StringRef arg, WindowsSubsystem *sys, uint32_t *major,
210 uint32_t *minor, bool *gotVersion = nullptr);
211
Amy Huang5a58b192023-01-10 04:37:28212 void parseMerge(StringRef);
213 void parsePDBPageSize(StringRef);
214 void parseSection(StringRef);
Amy Huang5a58b192023-01-10 04:37:28215
kkent030315fb974e82025-01-20 21:38:59216 // Parses a MS-DOS stub file
217 void parseDosStub(StringRef path);
218
Amy Huang5a58b192023-01-10 04:37:28219 // Parses a string in the form of "[:<integer>]"
220 void parseFunctionPadMin(llvm::opt::Arg *a);
221
Aleksei Nurmukhametov76947e02023-11-08 20:21:05222 // Parses a string in the form of "[:<integer>]"
223 void parseDependentLoadFlags(llvm::opt::Arg *a);
224
Amy Huang5a58b192023-01-10 04:37:28225 // Parses a string in the form of "EMBED[,=<integer>]|NO".
226 void parseManifest(StringRef arg);
227
228 // Parses a string in the form of "level=<string>|uiAccess=<string>"
229 void parseManifestUAC(StringRef arg);
230
231 // Parses a string in the form of "cd|net[,(cd|net)]*"
232 void parseSwaprun(StringRef arg);
233
234 // Create a resource file containing a manifest XML.
235 std::unique_ptr<MemoryBuffer> createManifestRes();
236 void createSideBySideManifest();
237 std::string createDefaultXml();
238 std::string createManifestXmlWithInternalMt(StringRef defaultXml);
239 std::string createManifestXmlWithExternalMt(StringRef defaultXml);
240 std::string createManifestXml();
241
242 std::unique_ptr<llvm::WritableMemoryBuffer>
243 createMemoryBufferForManifestRes(size_t manifestRes);
244
245 // Used for dllexported symbols.
246 Export parseExport(StringRef arg);
Amy Huang5a58b192023-01-10 04:37:28247
248 // Parses a string in the form of "key=value" and check
249 // if value matches previous values for the key.
250 // This feature used in the directive section to reject
251 // incompatible objects.
252 void checkFailIfMismatch(StringRef arg, InputFile *source);
253
254 // Convert Windows resource files (.res files) to a .obj file.
255 MemoryBufferRef convertResToCOFF(ArrayRef<MemoryBufferRef> mbs,
256 ArrayRef<ObjFile *> objs);
Jacek Cabana2d87432024-08-22 20:03:05257
258 // Create export thunks for exported and patchable Arm64EC function symbols.
259 void createECExportThunks();
260 void maybeCreateECExportThunk(StringRef name, Symbol *&sym);
Jacek Caban84352252025-01-01 18:42:49261
262 bool ltoCompilationDone = false;
Rui Ueyamaa9cbbf82015-05-31 19:17:09263};
264
Rui Ueyama3500f662015-05-28 20:30:06265// Create enum with OPT_xxx values for each option in Options.td
266enum {
267 OPT_INVALID = 0,
Jan Svoboda3f092f32023-08-04 18:19:09268#define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
Rui Ueyama3500f662015-05-28 20:30:06269#include "Options.inc"
270#undef OPTION
271};
272
Nico Weber7c266412022-08-08 15:32:26273} // namespace lld::coff
Rui Ueyama411c63602015-05-28 19:09:30274
275#endif