1//===- Config.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//===----------------------------------------------------------------------===//
8
9#ifndef LLD_COFF_CONFIG_H
10#define LLD_COFF_CONFIG_H
11
12#include "lld/Common/ErrorHandler.h"
13#include "llvm/ADT/MapVector.h"
14#include "llvm/ADT/SetVector.h"
15#include "llvm/ADT/SmallVector.h"
16#include "llvm/ADT/StringMap.h"
17#include "llvm/ADT/StringRef.h"
18#include "llvm/ADT/StringSet.h"
19#include "llvm/Object/COFF.h"
20#include "llvm/Support/CachePruning.h"
21#include "llvm/Support/VirtualFileSystem.h"
22#include <cstdint>
23#include <map>
24#include <string>
25
26namespace lld::coff {
27
28using llvm::COFF::IMAGE_FILE_MACHINE_UNKNOWN;
29using llvm::COFF::WindowsSubsystem;
30using llvm::StringRef;
31class COFFLinkerContext;
32class DefinedAbsolute;
33class StringChunk;
34class Symbol;
35class InputFile;
36class SectionChunk;
37
38// Short aliases.
39static const auto AMD64 = llvm::COFF::IMAGE_FILE_MACHINE_AMD64;
40static const auto ARM64 = llvm::COFF::IMAGE_FILE_MACHINE_ARM64;
41static const auto ARM64EC = llvm::COFF::IMAGE_FILE_MACHINE_ARM64EC;
42static const auto ARM64X = llvm::COFF::IMAGE_FILE_MACHINE_ARM64X;
43static const auto ARMNT = llvm::COFF::IMAGE_FILE_MACHINE_ARMNT;
44static const auto I386 = llvm::COFF::IMAGE_FILE_MACHINE_I386;
45
46enum class ExportSource {
47 Unset,
48 Directives,
49 Export,
50 ModuleDefinition,
51};
52
53enum class EmitKind { Obj, LLVM, ASM };
54
55// Represents an /export option.
56struct Export {
57 StringRef name; // N in /export:N or /export:E=N
58 StringRef extName; // E in /export:E=N
59 StringRef exportAs; // E in /export:N,EXPORTAS,E
60 StringRef importName; // GNU specific: N in "othername == N"
61 Symbol *sym = nullptr;
62 uint16_t ordinal = 0;
63 bool noname = false;
64 bool data = false;
65 bool isPrivate = false;
66 bool constant = false;
67
68 // If an export is a form of /export:foo=dllname.bar, that means
69 // that foo should be exported as an alias to bar in the DLL.
70 // forwardTo is set to "dllname.bar" part. Usually empty.
71 StringRef forwardTo;
72 StringChunk *forwardChunk = nullptr;
73
74 ExportSource source = ExportSource::Unset;
75 StringRef symbolName;
76 StringRef exportName; // Name in DLL
77
78 bool operator==(const Export &e) const {
79 return (name == e.name && extName == e.extName && exportAs == e.exportAs &&
80 importName == e.importName && ordinal == e.ordinal &&
81 noname == e.noname && data == e.data && isPrivate == e.isPrivate);
82 }
83};
84
85enum class DebugType {
86 None = 0x0,
87 CV = 0x1, /// CodeView
88 PData = 0x2, /// Procedure Data
89 Fixup = 0x4, /// Relocation Table
90};
91
92enum GuardCFLevel {
93 Off = 0x0,
94 CF = 0x1, /// Emit gfids tables
95 LongJmp = 0x2, /// Emit longjmp tables
96 EHCont = 0x4, /// Emit ehcont tables
97 All = 0x7 /// Enable all protections
98};
99
100enum class ICFLevel {
101 None,
102 Safe, // Safe ICF for all sections.
103 All, // Aggressive ICF for code, but safe ICF for data, similar to MSVC's
104 // behavior.
105};
106
107enum class BuildIDHash {
108 None,
109 PDB,
110 Binary,
111};
112
113// Global configuration.
114struct Configuration {
115 enum ManifestKind { Default, SideBySide, Embed, No };
116 bool is64() const { return llvm::COFF::is64Bit(Machine: machine); }
117
118 std::unique_ptr<MemoryBuffer> dosStub;
119 llvm::COFF::MachineTypes machine = IMAGE_FILE_MACHINE_UNKNOWN;
120 bool machineInferred = false;
121 size_t wordsize;
122 bool verbose = false;
123 WindowsSubsystem subsystem = llvm::COFF::IMAGE_SUBSYSTEM_UNKNOWN;
124 bool noEntry = false;
125 std::string outputFile;
126 std::string importName;
127 bool demangle = true;
128 bool doGC = true;
129 ICFLevel doICF = ICFLevel::None;
130 bool tailMerge;
131 bool relocatable = true;
132 bool forceMultiple = false;
133 bool forceMultipleRes = false;
134 bool forceUnresolved = false;
135 bool debug = false;
136 bool includeDwarfChunks = false;
137 bool debugGHashes = false;
138 bool writeSymtab = false;
139 bool driver = false;
140 bool driverUponly = false;
141 bool driverWdm = false;
142 bool showTiming = false;
143 bool showSummary = false;
144 bool printSearchPaths = false;
145 unsigned debugTypes = static_cast<unsigned>(DebugType::None);
146 llvm::SmallVector<llvm::StringRef, 0> mllvmOpts;
147 std::vector<std::string> natvisFiles;
148 llvm::StringMap<std::string> namedStreams;
149 llvm::SmallString<128> pdbAltPath;
150 int pdbPageSize = 4096;
151 llvm::SmallString<128> pdbPath;
152 llvm::SmallString<128> pdbSourcePath;
153 std::vector<llvm::StringRef> argv;
154
155 // Symbols in this set are considered as live by the garbage collector.
156 std::vector<Symbol *> gcroot;
157
158 llvm::StringSet<> noDefaultLibs;
159 bool noDefaultLibAll = false;
160
161 // True if we are creating a DLL.
162 bool dll = false;
163 StringRef implib;
164 bool noimplib = false;
165 llvm::StringSet<> delayLoads;
166 std::map<std::string, int> dllOrder;
167 Symbol *arm64ECIcallHelper = nullptr;
168
169 llvm::DenseSet<llvm::StringRef> saveTempsArgs;
170
171 // /guard:cf
172 int guardCF = GuardCFLevel::Off;
173
174 // Used for SafeSEH.
175 bool safeSEH = false;
176 Symbol *sehTable = nullptr;
177 Symbol *sehCount = nullptr;
178 bool noSEH = false;
179
180 // Used for /opt:lldlto=N
181 unsigned ltoo = 2;
182 // Used for /opt:lldltocgo=N
183 std::optional<unsigned> ltoCgo;
184
185 // Used for /opt:lldltojobs=N
186 std::string thinLTOJobs;
187 // Used for /opt:lldltopartitions=N
188 unsigned ltoPartitions = 1;
189
190 // Used for /lldltocache=path
191 StringRef ltoCache;
192 // Used for /lldltocachepolicy=policy
193 llvm::CachePruningPolicy ltoCachePolicy;
194
195 // Used for /thinlto-distributor:<path>
196 StringRef dtltoDistributor;
197
198 // Used for /thinlto-distributor-arg:<arg>
199 llvm::SmallVector<llvm::StringRef, 0> dtltoDistributorArgs;
200
201 // Used for /thinlto-remote-compiler:<path>
202 StringRef dtltoCompiler;
203
204 // Used for /thinlto-remote-compiler-prepend-arg:<arg>
205 llvm::SmallVector<llvm::StringRef, 0> dtltoCompilerPrependArgs;
206
207 // Used for /thinlto-remote-compiler-arg:<arg>
208 llvm::SmallVector<llvm::StringRef, 0> dtltoCompilerArgs;
209
210 // Used for /fat-lto-objects
211 bool fatLTOObjects = false;
212
213 // Used for /opt:[no]ltodebugpassmanager
214 bool ltoDebugPassManager = false;
215
216 // Used for /merge:from=to (e.g. /merge:.rdata=.text)
217 std::map<StringRef, StringRef> merge;
218
219 // Used for /section=.name,{DEKPRSW} to set section attributes.
220 std::map<StringRef, uint32_t> section;
221 // Used for /sectionlayout: to layout sections in specified order.
222 std::map<std::string, int> sectionOrder;
223
224 // Options for manifest files.
225 ManifestKind manifest = Default;
226 int manifestID = 1;
227 llvm::SetVector<StringRef> manifestDependencies;
228 bool manifestUAC = true;
229 std::vector<std::string> manifestInput;
230 StringRef manifestLevel = "'asInvoker'";
231 StringRef manifestUIAccess = "'false'";
232 StringRef manifestFile;
233
234 // used for /arm64xsameaddress
235 std::vector<std::pair<Symbol *, Symbol *>> sameAddresses;
236
237 // used for /dwodir
238 StringRef dwoDir;
239
240 // Used for /failifmismatch.
241 std::map<StringRef, std::pair<StringRef, InputFile *>> mustMatch;
242
243 // Used for /order.
244 llvm::StringMap<int> order;
245
246 // Used for /lldmap.
247 std::string lldmapFile;
248
249 // Used for /map.
250 std::string mapFile;
251
252 // Used for /mapinfo.
253 bool mapInfo = false;
254
255 // Used for /thinlto-index-only:
256 llvm::StringRef thinLTOIndexOnlyArg;
257
258 // Used for /thinlto-prefix-replace:
259 // Replace the prefix in paths generated for ThinLTO, replacing
260 // thinLTOPrefixReplaceOld with thinLTOPrefixReplaceNew. If
261 // thinLTOPrefixReplaceNativeObject is defined, replace the prefix of object
262 // file paths written to the response file given in the
263 // --thinlto-index-only=${response} option with
264 // thinLTOPrefixReplaceNativeObject, instead of thinLTOPrefixReplaceNew.
265 llvm::StringRef thinLTOPrefixReplaceOld;
266 llvm::StringRef thinLTOPrefixReplaceNew;
267 llvm::StringRef thinLTOPrefixReplaceNativeObject;
268
269 // Used for /thinlto-object-suffix-replace:
270 std::pair<llvm::StringRef, llvm::StringRef> thinLTOObjectSuffixReplace;
271
272 // Used for /lto-obj-path:
273 llvm::StringRef ltoObjPath;
274
275 // Used for /lto-cs-profile-generate:
276 bool ltoCSProfileGenerate = false;
277
278 // Used for /lto-cs-profile-path
279 llvm::StringRef ltoCSProfileFile;
280
281 // Used for /lto-pgo-warn-mismatch:
282 bool ltoPGOWarnMismatch = true;
283
284 // Used for /lto-sample-profile:
285 llvm::StringRef ltoSampleProfileName;
286
287 // Used for /call-graph-ordering-file:
288 llvm::MapVector<std::pair<const SectionChunk *, const SectionChunk *>,
289 uint64_t>
290 callGraphProfile;
291 bool callGraphProfileSort = false;
292
293 // Used for /print-symbol-order:
294 StringRef printSymbolOrder;
295
296 // Used for /vfsoverlay:
297 std::unique_ptr<llvm::vfs::FileSystem> vfs;
298
299 uint64_t align = 4096;
300 uint64_t imageBase = -1;
301 uint64_t fileAlign = 512;
302 uint64_t stackReserve = 1024 * 1024;
303 uint64_t stackCommit = 4096;
304 uint64_t heapReserve = 1024 * 1024;
305 uint64_t heapCommit = 4096;
306 uint32_t majorImageVersion = 0;
307 uint32_t minorImageVersion = 0;
308 // If changing the default os/subsys version here, update the default in
309 // the MinGW driver accordingly.
310 uint32_t majorOSVersion = 6;
311 uint32_t minorOSVersion = 0;
312 uint32_t majorSubsystemVersion = 6;
313 uint32_t minorSubsystemVersion = 0;
314 uint32_t timestamp = 0;
315 uint32_t functionPadMin = 0;
316 uint32_t timeTraceGranularity = 0;
317 uint16_t dependentLoadFlags = 0;
318 bool dynamicBase = true;
319 bool allowBind = true;
320 bool cetCompat = false;
321 bool cetCompatStrict = false;
322 bool cetCompatIpValidationRelaxed = false;
323 bool cetCompatDynamicApisInProcOnly = false;
324 bool hotpatchCompat = false;
325 bool nxCompat = true;
326 bool allowIsolation = true;
327 bool terminalServerAware = true;
328 bool largeAddressAware = false;
329 bool highEntropyVA = false;
330 bool appContainer = false;
331 bool mergeDebugDirectory = true;
332 bool mingw = false;
333 bool warnMissingOrderSymbol = true;
334 bool warnLocallyDefinedImported = true;
335 bool warnDebugInfoUnusable = true;
336 bool warnLongSectionNames = true;
337 bool warnStdcallFixup = true;
338 bool warnImportedDllMain = true;
339 bool incremental = true;
340 bool integrityCheck = false;
341 bool killAt = false;
342 bool repro = false;
343 bool swaprunCD = false;
344 bool swaprunNet = false;
345 bool thinLTOEmitImportsFiles;
346 bool thinLTOIndexOnly;
347 bool timeTraceEnabled = false;
348 bool autoImport = false;
349 bool pseudoRelocs = false;
350 bool stdcallFixup = false;
351 bool writeCheckSum = false;
352 bool prefetchInputs = false;
353 EmitKind emit = EmitKind::Obj;
354 bool allowDuplicateWeak = false;
355 BuildIDHash buildIDHash = BuildIDHash::None;
356};
357
358struct COFFSyncStream : SyncStream {
359 COFFLinkerContext &ctx;
360 COFFSyncStream(COFFLinkerContext &ctx, DiagLevel level);
361};
362
363template <typename T>
364std::enable_if_t<!std::is_pointer_v<std::remove_reference_t<T>>,
365 const COFFSyncStream &>
366operator<<(const COFFSyncStream &s, T &&v) {
367 s.os << std::forward<T>(v);
368 return s;
369}
370
371inline const COFFSyncStream &operator<<(const COFFSyncStream &s,
372 const char *v) {
373 s.os << v;
374 return s;
375}
376
377inline const COFFSyncStream &operator<<(const COFFSyncStream &s, Error v) {
378 s.os << llvm::toString(E: std::move(v));
379 return s;
380}
381
382// Report a log if -verbose is specified.
383COFFSyncStream Log(COFFLinkerContext &ctx);
384
385// Print a message to stdout.
386COFFSyncStream Msg(COFFLinkerContext &ctx);
387
388// Report a warning. Upgraded to an error if /WX is specified.
389COFFSyncStream Warn(COFFLinkerContext &ctx);
390
391// Report an error that will suppress the output file generation.
392COFFSyncStream Err(COFFLinkerContext &ctx);
393
394// Report a fatal error that exits immediately. This should generally be avoided
395// in favor of Err.
396COFFSyncStream Fatal(COFFLinkerContext &ctx);
397
398uint64_t errCount(COFFLinkerContext &ctx);
399
400} // namespace lld::coff
401
402#endif
403