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