xref: /llvm-project/lld/MachO/Config.h (revision c676104875f34a87051b446469cc395932bc1f13)
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_MACHO_CONFIG_H
10 #define LLD_MACHO_CONFIG_H
11 
12 #include "llvm/ADT/CachedHashString.h"
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/DenseSet.h"
15 #include "llvm/ADT/SetVector.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/StringSet.h"
19 #include "llvm/BinaryFormat/MachO.h"
20 #include "llvm/Support/CachePruning.h"
21 #include "llvm/Support/GlobPattern.h"
22 #include "llvm/Support/VersionTuple.h"
23 #include "llvm/TextAPI/Architecture.h"
24 #include "llvm/TextAPI/Platform.h"
25 #include "llvm/TextAPI/Target.h"
26 
27 #include <vector>
28 
29 namespace llvm {
30 enum class CodeGenOptLevel;
31 } // namespace llvm
32 
33 namespace lld {
34 namespace macho {
35 
36 class InputSection;
37 class Symbol;
38 
39 using NamePair = std::pair<llvm::StringRef, llvm::StringRef>;
40 using SectionRenameMap = llvm::DenseMap<NamePair, NamePair>;
41 using SegmentRenameMap = llvm::DenseMap<llvm::StringRef, llvm::StringRef>;
42 
43 struct PlatformInfo {
44   llvm::MachO::Target target;
45   llvm::VersionTuple sdk;
46 };
47 
48 inline uint32_t encodeVersion(const llvm::VersionTuple &version) {
49   return ((version.getMajor() << 020) |
50           (version.getMinor().value_or(0) << 010) |
51           version.getSubminor().value_or(0));
52 }
53 
54 enum class NamespaceKind {
55   twolevel,
56   flat,
57 };
58 
59 enum class UndefinedSymbolTreatment {
60   unknown,
61   error,
62   warning,
63   suppress,
64   dynamic_lookup,
65 };
66 
67 enum class ICFLevel {
68   unknown,
69   none,
70   safe,
71   safe_thunks,
72   all,
73 };
74 
75 enum class ObjCStubsMode {
76   fast,
77   small,
78 };
79 
80 struct SectionAlign {
81   llvm::StringRef segName;
82   llvm::StringRef sectName;
83   uint32_t align;
84 };
85 
86 struct SegmentProtection {
87   llvm::StringRef name;
88   uint32_t maxProt;
89   uint32_t initProt;
90 };
91 
92 class SymbolPatterns {
93 public:
94   // GlobPattern can also match literals,
95   // but we prefer the O(1) lookup of DenseSet.
96   llvm::SetVector<llvm::CachedHashStringRef> literals;
97   std::vector<llvm::GlobPattern> globs;
98 
99   bool empty() const { return literals.empty() && globs.empty(); }
100   void clear();
101   void insert(llvm::StringRef symbolName);
102   bool matchLiteral(llvm::StringRef symbolName) const;
103   bool matchGlob(llvm::StringRef symbolName) const;
104   bool match(llvm::StringRef symbolName) const;
105 };
106 
107 enum class SymtabPresence {
108   All,
109   None,
110   SelectivelyIncluded,
111   SelectivelyExcluded,
112 };
113 
114 struct Configuration {
115   Symbol *entry = nullptr;
116   bool hasReexports = false;
117   bool allLoad = false;
118   bool applicationExtension = false;
119   bool archMultiple = false;
120   bool exportDynamic = false;
121   bool forceLoadObjC = false;
122   bool forceLoadSwift = false; // Only applies to LC_LINKER_OPTIONs.
123   bool staticLink = false;
124   bool implicitDylibs = false;
125   bool isPic = false;
126   bool headerPadMaxInstallNames = false;
127   bool markDeadStrippableDylib = false;
128   bool printDylibSearch = false;
129   bool printEachFile = false;
130   bool printWhyLoad = false;
131   bool searchDylibsFirst = false;
132   bool saveTemps = false;
133   bool adhocCodesign = false;
134   bool emitFunctionStarts = false;
135   bool emitDataInCodeInfo = false;
136   bool emitEncryptionInfo = false;
137   bool emitInitOffsets = false;
138   bool emitChainedFixups = false;
139   bool emitRelativeMethodLists = false;
140   bool thinLTOEmitImportsFiles;
141   bool thinLTOEmitIndexFiles;
142   bool thinLTOIndexOnly;
143   bool timeTraceEnabled = false;
144   bool dataConst = false;
145   bool dedupStrings = true;
146   bool dedupSymbolStrings = true;
147   bool deadStripDuplicates = false;
148   bool omitDebugInfo = false;
149   bool warnDylibInstallName = false;
150   bool ignoreOptimizationHints = false;
151   bool forceExactCpuSubtypeMatch = false;
152   uint32_t headerPad;
153   uint32_t dylibCompatibilityVersion = 0;
154   uint32_t dylibCurrentVersion = 0;
155   uint32_t timeTraceGranularity = 500;
156   unsigned optimize;
157   std::string progName;
158 
159   // For `clang -arch arm64 -arch x86_64`, clang will:
160   // 1. invoke the linker twice, to write one temporary output per arch
161   // 2. invoke `lipo` to merge the two outputs into a single file
162   // `outputFile` is the name of the temporary file the linker writes to.
163   // `finalOutput `is the name of the file lipo writes to after the link.
164   llvm::StringRef outputFile;
165   llvm::StringRef finalOutput;
166 
167   llvm::StringRef installName;
168   llvm::StringRef clientName;
169   llvm::StringRef mapFile;
170   llvm::StringRef ltoNewPmPasses;
171   llvm::StringRef ltoObjPath;
172   llvm::StringRef thinLTOJobs;
173   llvm::StringRef umbrella;
174   uint32_t ltoo = 2;
175   llvm::CodeGenOptLevel ltoCgo;
176   llvm::CachePruningPolicy thinLTOCachePolicy;
177   llvm::StringRef thinLTOCacheDir;
178   llvm::StringRef thinLTOIndexOnlyArg;
179   std::pair<llvm::StringRef, llvm::StringRef> thinLTOObjectSuffixReplace;
180   llvm::StringRef thinLTOPrefixReplaceOld;
181   llvm::StringRef thinLTOPrefixReplaceNew;
182   llvm::StringRef thinLTOPrefixReplaceNativeObject;
183   bool deadStripDylibs = false;
184   bool demangle = false;
185   bool deadStrip = false;
186   bool errorForArchMismatch = false;
187   bool ignoreAutoLink = false;
188   // ld64 allows invalid auto link options as long as the link succeeds. LLD
189   // does not, but there are cases in the wild where the invalid linker options
190   // exist. This allows users to ignore the specific invalid options in the case
191   // they can't easily fix them.
192   llvm::StringSet<> ignoreAutoLinkOptions;
193   bool strictAutoLink = false;
194   PlatformInfo platformInfo;
195   std::optional<PlatformInfo> secondaryPlatformInfo;
196   NamespaceKind namespaceKind = NamespaceKind::twolevel;
197   UndefinedSymbolTreatment undefinedSymbolTreatment =
198       UndefinedSymbolTreatment::error;
199   ICFLevel icfLevel = ICFLevel::none;
200   bool keepICFStabs = false;
201   ObjCStubsMode objcStubsMode = ObjCStubsMode::fast;
202   llvm::MachO::HeaderFileType outputType;
203   std::vector<llvm::StringRef> systemLibraryRoots;
204   std::vector<llvm::StringRef> librarySearchPaths;
205   std::vector<llvm::StringRef> frameworkSearchPaths;
206   bool warnDuplicateRpath = true;
207   llvm::SmallVector<llvm::StringRef, 0> runtimePaths;
208   llvm::SmallVector<llvm::StringRef, 0> allowableClients;
209   std::vector<std::string> astPaths;
210   std::vector<Symbol *> explicitUndefineds;
211   llvm::StringSet<> explicitDynamicLookups;
212   // There are typically few custom sectionAlignments or segmentProtections,
213   // so use a vector instead of a map.
214   std::vector<SectionAlign> sectionAlignments;
215   std::vector<SegmentProtection> segmentProtections;
216   bool ltoDebugPassManager = false;
217   llvm::StringRef codegenDataGeneratePath;
218   bool csProfileGenerate = false;
219   llvm::StringRef csProfilePath;
220   bool pgoWarnMismatch;
221   bool warnThinArchiveMissingMembers;
222 
223   bool callGraphProfileSort = false;
224   llvm::StringRef printSymbolOrder;
225 
226   llvm::StringRef irpgoProfilePath;
227   bool bpStartupFunctionSort = false;
228   bool bpCompressionSortStartupFunctions = false;
229   bool bpFunctionOrderForCompression = false;
230   bool bpDataOrderForCompression = false;
231   bool bpVerboseSectionOrderer = false;
232 
233   SectionRenameMap sectionRenameMap;
234   SegmentRenameMap segmentRenameMap;
235 
236   bool hasExplicitExports = false;
237   SymbolPatterns exportedSymbols;
238   SymbolPatterns unexportedSymbols;
239   SymbolPatterns whyLive;
240 
241   std::vector<std::pair<llvm::StringRef, llvm::StringRef>> aliasedSymbols;
242 
243   SymtabPresence localSymbolsPresence = SymtabPresence::All;
244   SymbolPatterns localSymbolPatterns;
245   llvm::SmallVector<llvm::StringRef, 0> mllvmOpts;
246   llvm::SmallVector<llvm::StringRef, 0> passPlugins;
247 
248   bool zeroModTime = true;
249   bool generateUuid = true;
250 
251   llvm::StringRef osoPrefix;
252 
253   std::vector<llvm::StringRef> dyldEnvs;
254 
255   llvm::MachO::Architecture arch() const { return platformInfo.target.Arch; }
256 
257   llvm::MachO::PlatformType platform() const {
258     return platformInfo.target.Platform;
259   }
260 };
261 
262 extern std::unique_ptr<Configuration> config;
263 
264 } // namespace macho
265 } // namespace lld
266 
267 #endif
268