xref: /freebsd/contrib/llvm-project/lld/COFF/Config.h (revision 350b7c3570aa6c87c537e54f706f1866f93a4142)
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 "llvm/ADT/MapVector.h"
13 #include "llvm/ADT/SetVector.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringMap.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/Object/COFF.h"
18 #include "llvm/Support/CachePruning.h"
19 #include "llvm/Support/VirtualFileSystem.h"
20 #include <cstdint>
21 #include <map>
22 #include <set>
23 #include <string>
24 
25 namespace lld::coff {
26 
27 using llvm::COFF::IMAGE_FILE_MACHINE_UNKNOWN;
28 using llvm::COFF::WindowsSubsystem;
29 using llvm::StringRef;
30 class DefinedAbsolute;
31 class DefinedRelative;
32 class StringChunk;
33 class Symbol;
34 class InputFile;
35 class SectionChunk;
36 
37 // Short aliases.
38 static const auto AMD64 = llvm::COFF::IMAGE_FILE_MACHINE_AMD64;
39 static const auto ARM64 = llvm::COFF::IMAGE_FILE_MACHINE_ARM64;
40 static const auto ARMNT = llvm::COFF::IMAGE_FILE_MACHINE_ARMNT;
41 static const auto I386 = llvm::COFF::IMAGE_FILE_MACHINE_I386;
42 
43 // Represents an /export option.
44 struct Export {
45   StringRef name;       // N in /export:N or /export:E=N
46   StringRef extName;    // E in /export:E=N
47   StringRef aliasTarget; // GNU specific: N in "alias == N"
48   Symbol *sym = nullptr;
49   uint16_t ordinal = 0;
50   bool noname = false;
51   bool data = false;
52   bool isPrivate = false;
53   bool constant = false;
54 
55   // If an export is a form of /export:foo=dllname.bar, that means
56   // that foo should be exported as an alias to bar in the DLL.
57   // forwardTo is set to "dllname.bar" part. Usually empty.
58   StringRef forwardTo;
59   StringChunk *forwardChunk = nullptr;
60 
61   // True if this /export option was in .drectves section.
62   bool directives = false;
63   StringRef symbolName;
64   StringRef exportName; // Name in DLL
65 
66   bool operator==(const Export &e) {
67     return (name == e.name && extName == e.extName &&
68             aliasTarget == e.aliasTarget &&
69             ordinal == e.ordinal && noname == e.noname &&
70             data == e.data && isPrivate == e.isPrivate);
71   }
72 };
73 
74 enum class DebugType {
75   None  = 0x0,
76   CV    = 0x1,  /// CodeView
77   PData = 0x2,  /// Procedure Data
78   Fixup = 0x4,  /// Relocation Table
79 };
80 
81 enum GuardCFLevel {
82   Off     = 0x0,
83   CF      = 0x1, /// Emit gfids tables
84   LongJmp = 0x2, /// Emit longjmp tables
85   EHCont  = 0x4, /// Emit ehcont tables
86   All     = 0x7  /// Enable all protections
87 };
88 
89 enum class ICFLevel {
90   None,
91   Safe, // Safe ICF for all sections.
92   All,  // Aggressive ICF for code, but safe ICF for data, similar to MSVC's
93         // behavior.
94 };
95 
96 // Global configuration.
97 struct Configuration {
98   enum ManifestKind { Default, SideBySide, Embed, No };
99   bool is64() const { return machine == AMD64 || machine == ARM64; }
100 
101   llvm::COFF::MachineTypes machine = IMAGE_FILE_MACHINE_UNKNOWN;
102   size_t wordsize;
103   bool verbose = false;
104   WindowsSubsystem subsystem = llvm::COFF::IMAGE_SUBSYSTEM_UNKNOWN;
105   Symbol *entry = nullptr;
106   bool noEntry = false;
107   std::string outputFile;
108   std::string importName;
109   bool demangle = true;
110   bool doGC = true;
111   ICFLevel doICF = ICFLevel::None;
112   bool tailMerge;
113   bool relocatable = true;
114   bool forceMultiple = false;
115   bool forceMultipleRes = false;
116   bool forceUnresolved = false;
117   bool debug = false;
118   bool debugDwarf = false;
119   bool debugGHashes = false;
120   bool debugSymtab = false;
121   bool driver = false;
122   bool driverUponly = false;
123   bool driverWdm = false;
124   bool showTiming = false;
125   bool showSummary = false;
126   unsigned debugTypes = static_cast<unsigned>(DebugType::None);
127   llvm::SmallVector<llvm::StringRef, 0> mllvmOpts;
128   std::vector<std::string> natvisFiles;
129   llvm::StringMap<std::string> namedStreams;
130   llvm::SmallString<128> pdbAltPath;
131   int pdbPageSize = 4096;
132   llvm::SmallString<128> pdbPath;
133   llvm::SmallString<128> pdbSourcePath;
134   std::vector<llvm::StringRef> argv;
135 
136   // Symbols in this set are considered as live by the garbage collector.
137   std::vector<Symbol *> gcroot;
138 
139   std::set<std::string> noDefaultLibs;
140   bool noDefaultLibAll = false;
141 
142   // True if we are creating a DLL.
143   bool dll = false;
144   StringRef implib;
145   bool noimplib = false;
146   std::vector<Export> exports;
147   bool hadExplicitExports;
148   std::set<std::string> delayLoads;
149   std::map<std::string, int> dllOrder;
150   Symbol *delayLoadHelper = nullptr;
151 
152   bool saveTemps = false;
153 
154   // /guard:cf
155   int guardCF = GuardCFLevel::Off;
156 
157   // Used for SafeSEH.
158   bool safeSEH = false;
159   Symbol *sehTable = nullptr;
160   Symbol *sehCount = nullptr;
161   bool noSEH = false;
162 
163   // Used for /opt:lldlto=N
164   unsigned ltoo = 2;
165 
166   // Used for /opt:lldltojobs=N
167   std::string thinLTOJobs;
168   // Used for /opt:lldltopartitions=N
169   unsigned ltoPartitions = 1;
170 
171   // Used for /opt:lldltocache=path
172   StringRef ltoCache;
173   // Used for /opt:lldltocachepolicy=policy
174   llvm::CachePruningPolicy ltoCachePolicy;
175 
176   // Used for /opt:[no]ltodebugpassmanager
177   bool ltoDebugPassManager = false;
178 
179   // Used for /merge:from=to (e.g. /merge:.rdata=.text)
180   std::map<StringRef, StringRef> merge;
181 
182   // Used for /section=.name,{DEKPRSW} to set section attributes.
183   std::map<StringRef, uint32_t> section;
184 
185   // Options for manifest files.
186   ManifestKind manifest = Default;
187   int manifestID = 1;
188   llvm::SetVector<StringRef> manifestDependencies;
189   bool manifestUAC = true;
190   std::vector<std::string> manifestInput;
191   StringRef manifestLevel = "'asInvoker'";
192   StringRef manifestUIAccess = "'false'";
193   StringRef manifestFile;
194 
195   // Used for /aligncomm.
196   std::map<std::string, int> alignComm;
197 
198   // Used for /failifmismatch.
199   std::map<StringRef, std::pair<StringRef, InputFile *>> mustMatch;
200 
201   // Used for /alternatename.
202   std::map<StringRef, StringRef> alternateNames;
203 
204   // Used for /order.
205   llvm::StringMap<int> order;
206 
207   // Used for /lldmap.
208   std::string lldmapFile;
209 
210   // Used for /map.
211   std::string mapFile;
212 
213   // Used for /mapinfo.
214   bool mapInfo = false;
215 
216   // Used for /thinlto-index-only:
217   llvm::StringRef thinLTOIndexOnlyArg;
218 
219   // Used for /thinlto-object-prefix-replace:
220   std::pair<llvm::StringRef, llvm::StringRef> thinLTOPrefixReplace;
221 
222   // Used for /thinlto-object-suffix-replace:
223   std::pair<llvm::StringRef, llvm::StringRef> thinLTOObjectSuffixReplace;
224 
225   // Used for /lto-obj-path:
226   llvm::StringRef ltoObjPath;
227 
228   // Used for /lto-cs-profile-generate:
229   bool ltoCSProfileGenerate = false;
230 
231   // Used for /lto-cs-profile-path
232   llvm::StringRef ltoCSProfileFile;
233 
234   // Used for /lto-pgo-warn-mismatch:
235   bool ltoPGOWarnMismatch = true;
236 
237   // Used for /call-graph-ordering-file:
238   llvm::MapVector<std::pair<const SectionChunk *, const SectionChunk *>,
239                   uint64_t>
240       callGraphProfile;
241   bool callGraphProfileSort = false;
242 
243   // Used for /print-symbol-order:
244   StringRef printSymbolOrder;
245 
246   // Used for /vfsoverlay:
247   std::unique_ptr<llvm::vfs::FileSystem> vfs;
248 
249   uint64_t align = 4096;
250   uint64_t imageBase = -1;
251   uint64_t fileAlign = 512;
252   uint64_t stackReserve = 1024 * 1024;
253   uint64_t stackCommit = 4096;
254   uint64_t heapReserve = 1024 * 1024;
255   uint64_t heapCommit = 4096;
256   uint32_t majorImageVersion = 0;
257   uint32_t minorImageVersion = 0;
258   // If changing the default os/subsys version here, update the default in
259   // the MinGW driver accordingly.
260   uint32_t majorOSVersion = 6;
261   uint32_t minorOSVersion = 0;
262   uint32_t majorSubsystemVersion = 6;
263   uint32_t minorSubsystemVersion = 0;
264   uint32_t timestamp = 0;
265   uint32_t functionPadMin = 0;
266   bool dynamicBase = true;
267   bool allowBind = true;
268   bool cetCompat = false;
269   bool nxCompat = true;
270   bool allowIsolation = true;
271   bool terminalServerAware = true;
272   bool largeAddressAware = false;
273   bool highEntropyVA = false;
274   bool appContainer = false;
275   bool mingw = false;
276   bool warnMissingOrderSymbol = true;
277   bool warnLocallyDefinedImported = true;
278   bool warnDebugInfoUnusable = true;
279   bool warnLongSectionNames = true;
280   bool warnStdcallFixup = true;
281   bool incremental = true;
282   bool integrityCheck = false;
283   bool killAt = false;
284   bool repro = false;
285   bool swaprunCD = false;
286   bool swaprunNet = false;
287   bool thinLTOEmitImportsFiles;
288   bool thinLTOIndexOnly;
289   bool autoImport = false;
290   bool pseudoRelocs = false;
291   bool stdcallFixup = false;
292   bool writeCheckSum = false;
293 };
294 
295 } // namespace lld::coff
296 
297 #endif
298