399 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLD_COFF_CONFIG_H10#define LLD_COFF_CONFIG_H11 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/Object/COFF.h"19#include "llvm/Support/CachePruning.h"20#include "llvm/Support/VirtualFileSystem.h"21#include <cstdint>22#include <map>23#include <set>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=N58 StringRef extName; // E in /export:E=N59 StringRef exportAs; // E in /export:N,EXPORTAS,E60 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 means69 // 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 DLL77 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, /// CodeView88 PData = 0x2, /// Procedure Data89 Fixup = 0x4, /// Relocation Table90};91 92enum GuardCFLevel {93 Off = 0x0,94 CF = 0x1, /// Emit gfids tables95 LongJmp = 0x2, /// Emit longjmp tables96 EHCont = 0x4, /// Emit ehcont tables97 All = 0x7 /// Enable all protections98};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's104 // 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); }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 std::set<std::string> 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 std::set<std::string> delayLoads;166 std::map<std::string, int> dllOrder;167 Symbol *arm64ECIcallHelper = nullptr;168 169 llvm::DenseSet<llvm::StringRef> saveTempsArgs;170 171 // /guard:cf172 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=N181 unsigned ltoo = 2;182 // Used for /opt:lldltocgo=N183 std::optional<unsigned> ltoCgo;184 185 // Used for /opt:lldltojobs=N186 std::string thinLTOJobs;187 // Used for /opt:lldltopartitions=N188 unsigned ltoPartitions = 1;189 190 // Used for /lldltocache=path191 StringRef ltoCache;192 // Used for /lldltocachepolicy=policy193 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 /opt:[no]ltodebugpassmanager211 bool ltoDebugPassManager = false;212 213 // Used for /merge:from=to (e.g. /merge:.rdata=.text)214 std::map<StringRef, StringRef> merge;215 216 // Used for /section=.name,{DEKPRSW} to set section attributes.217 std::map<StringRef, uint32_t> section;218 // Used for /sectionlayout: to layout sections in specified order.219 std::map<std::string, int> sectionOrder;220 221 // Options for manifest files.222 ManifestKind manifest = Default;223 int manifestID = 1;224 llvm::SetVector<StringRef> manifestDependencies;225 bool manifestUAC = true;226 std::vector<std::string> manifestInput;227 StringRef manifestLevel = "'asInvoker'";228 StringRef manifestUIAccess = "'false'";229 StringRef manifestFile;230 231 // used for /arm64xsameaddress232 std::vector<std::pair<Symbol *, Symbol *>> sameAddresses;233 234 // used for /dwodir235 StringRef dwoDir;236 237 // Used for /failifmismatch.238 std::map<StringRef, std::pair<StringRef, InputFile *>> mustMatch;239 240 // Used for /order.241 llvm::StringMap<int> order;242 243 // Used for /lldmap.244 std::string lldmapFile;245 246 // Used for /map.247 std::string mapFile;248 249 // Used for /mapinfo.250 bool mapInfo = false;251 252 // Used for /thinlto-index-only:253 llvm::StringRef thinLTOIndexOnlyArg;254 255 // Used for /thinlto-prefix-replace:256 // Replace the prefix in paths generated for ThinLTO, replacing257 // thinLTOPrefixReplaceOld with thinLTOPrefixReplaceNew. If258 // thinLTOPrefixReplaceNativeObject is defined, replace the prefix of object259 // file paths written to the response file given in the260 // --thinlto-index-only=${response} option with261 // thinLTOPrefixReplaceNativeObject, instead of thinLTOPrefixReplaceNew.262 llvm::StringRef thinLTOPrefixReplaceOld;263 llvm::StringRef thinLTOPrefixReplaceNew;264 llvm::StringRef thinLTOPrefixReplaceNativeObject;265 266 // Used for /thinlto-object-suffix-replace:267 std::pair<llvm::StringRef, llvm::StringRef> thinLTOObjectSuffixReplace;268 269 // Used for /lto-obj-path:270 llvm::StringRef ltoObjPath;271 272 // Used for /lto-cs-profile-generate:273 bool ltoCSProfileGenerate = false;274 275 // Used for /lto-cs-profile-path276 llvm::StringRef ltoCSProfileFile;277 278 // Used for /lto-pgo-warn-mismatch:279 bool ltoPGOWarnMismatch = true;280 281 // Used for /lto-sample-profile:282 llvm::StringRef ltoSampleProfileName;283 284 // Used for /call-graph-ordering-file:285 llvm::MapVector<std::pair<const SectionChunk *, const SectionChunk *>,286 uint64_t>287 callGraphProfile;288 bool callGraphProfileSort = false;289 290 // Used for /print-symbol-order:291 StringRef printSymbolOrder;292 293 // Used for /vfsoverlay:294 std::unique_ptr<llvm::vfs::FileSystem> vfs;295 296 uint64_t align = 4096;297 uint64_t imageBase = -1;298 uint64_t fileAlign = 512;299 uint64_t stackReserve = 1024 * 1024;300 uint64_t stackCommit = 4096;301 uint64_t heapReserve = 1024 * 1024;302 uint64_t heapCommit = 4096;303 uint32_t majorImageVersion = 0;304 uint32_t minorImageVersion = 0;305 // If changing the default os/subsys version here, update the default in306 // the MinGW driver accordingly.307 uint32_t majorOSVersion = 6;308 uint32_t minorOSVersion = 0;309 uint32_t majorSubsystemVersion = 6;310 uint32_t minorSubsystemVersion = 0;311 uint32_t timestamp = 0;312 uint32_t functionPadMin = 0;313 uint32_t timeTraceGranularity = 0;314 uint16_t dependentLoadFlags = 0;315 bool dynamicBase = true;316 bool allowBind = true;317 bool cetCompat = false;318 bool cetCompatStrict = false;319 bool cetCompatIpValidationRelaxed = false;320 bool cetCompatDynamicApisInProcOnly = false;321 bool hotpatchCompat = false;322 bool nxCompat = true;323 bool allowIsolation = true;324 bool terminalServerAware = true;325 bool largeAddressAware = false;326 bool highEntropyVA = false;327 bool appContainer = false;328 bool mergeDebugDirectory = true;329 bool mingw = false;330 bool warnMissingOrderSymbol = true;331 bool warnLocallyDefinedImported = true;332 bool warnDebugInfoUnusable = true;333 bool warnLongSectionNames = true;334 bool warnStdcallFixup = true;335 bool warnImportedDllMain = true;336 bool incremental = true;337 bool integrityCheck = false;338 bool killAt = false;339 bool repro = false;340 bool swaprunCD = false;341 bool swaprunNet = false;342 bool thinLTOEmitImportsFiles;343 bool thinLTOIndexOnly;344 bool timeTraceEnabled = false;345 bool autoImport = false;346 bool pseudoRelocs = false;347 bool stdcallFixup = false;348 bool writeCheckSum = false;349 EmitKind emit = EmitKind::Obj;350 bool allowDuplicateWeak = false;351 BuildIDHash buildIDHash = BuildIDHash::None;352};353 354struct COFFSyncStream : SyncStream {355 COFFLinkerContext &ctx;356 COFFSyncStream(COFFLinkerContext &ctx, DiagLevel level);357};358 359template <typename T>360std::enable_if_t<!std::is_pointer_v<std::remove_reference_t<T>>,361 const COFFSyncStream &>362operator<<(const COFFSyncStream &s, T &&v) {363 s.os << std::forward<T>(v);364 return s;365}366 367inline const COFFSyncStream &operator<<(const COFFSyncStream &s,368 const char *v) {369 s.os << v;370 return s;371}372 373inline const COFFSyncStream &operator<<(const COFFSyncStream &s, Error v) {374 s.os << llvm::toString(std::move(v));375 return s;376}377 378// Report a log if -verbose is specified.379COFFSyncStream Log(COFFLinkerContext &ctx);380 381// Print a message to stdout.382COFFSyncStream Msg(COFFLinkerContext &ctx);383 384// Report a warning. Upgraded to an error if /WX is specified.385COFFSyncStream Warn(COFFLinkerContext &ctx);386 387// Report an error that will suppress the output file generation.388COFFSyncStream Err(COFFLinkerContext &ctx);389 390// Report a fatal error that exits immediately. This should generally be avoided391// in favor of Err.392COFFSyncStream Fatal(COFFLinkerContext &ctx);393 394uint64_t errCount(COFFLinkerContext &ctx);395 396} // namespace lld::coff397 398#endif399