xref: /freebsd/contrib/llvm-project/lld/MachO/Config.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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 
encodeVersion(const llvm::VersionTuple & version)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 
empty()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 interposable = false;
187   bool errorForArchMismatch = false;
188   bool ignoreAutoLink = false;
189   // ld64 allows invalid auto link options as long as the link succeeds. LLD
190   // does not, but there are cases in the wild where the invalid linker options
191   // exist. This allows users to ignore the specific invalid options in the case
192   // they can't easily fix them.
193   llvm::StringSet<> ignoreAutoLinkOptions;
194   bool strictAutoLink = false;
195   PlatformInfo platformInfo;
196   std::optional<PlatformInfo> secondaryPlatformInfo;
197   NamespaceKind namespaceKind = NamespaceKind::twolevel;
198   UndefinedSymbolTreatment undefinedSymbolTreatment =
199       UndefinedSymbolTreatment::error;
200   ICFLevel icfLevel = ICFLevel::none;
201   bool keepICFStabs = false;
202   ObjCStubsMode objcStubsMode = ObjCStubsMode::fast;
203   llvm::MachO::HeaderFileType outputType;
204   std::vector<llvm::StringRef> systemLibraryRoots;
205   std::vector<llvm::StringRef> librarySearchPaths;
206   std::vector<llvm::StringRef> frameworkSearchPaths;
207   bool warnDuplicateRpath = true;
208   llvm::SmallVector<llvm::StringRef, 0> runtimePaths;
209   llvm::SmallVector<llvm::StringRef, 0> allowableClients;
210   std::vector<std::string> astPaths;
211   std::vector<Symbol *> explicitUndefineds;
212   llvm::StringSet<> explicitDynamicLookups;
213   // There are typically few custom sectionAlignments or segmentProtections,
214   // so use a vector instead of a map.
215   std::vector<SectionAlign> sectionAlignments;
216   std::vector<SegmentProtection> segmentProtections;
217   bool ltoDebugPassManager = false;
218   llvm::StringRef codegenDataGeneratePath;
219   bool csProfileGenerate = false;
220   llvm::StringRef csProfilePath;
221   bool pgoWarnMismatch;
222   bool warnThinArchiveMissingMembers;
223   bool disableVerify;
224 
225   bool callGraphProfileSort = false;
226   llvm::StringRef printSymbolOrder;
227 
228   llvm::StringRef irpgoProfilePath;
229   bool bpStartupFunctionSort = false;
230   bool bpCompressionSortStartupFunctions = false;
231   bool bpFunctionOrderForCompression = false;
232   bool bpDataOrderForCompression = false;
233   bool bpVerboseSectionOrderer = false;
234 
235   SectionRenameMap sectionRenameMap;
236   SegmentRenameMap segmentRenameMap;
237 
238   bool hasExplicitExports = false;
239   SymbolPatterns exportedSymbols;
240   SymbolPatterns unexportedSymbols;
241   SymbolPatterns whyLive;
242 
243   std::vector<std::pair<llvm::StringRef, llvm::StringRef>> aliasedSymbols;
244 
245   SymtabPresence localSymbolsPresence = SymtabPresence::All;
246   SymbolPatterns localSymbolPatterns;
247   llvm::SmallVector<llvm::StringRef, 0> mllvmOpts;
248   llvm::SmallVector<llvm::StringRef, 0> passPlugins;
249 
250   bool zeroModTime = true;
251   bool generateUuid = true;
252 
253   llvm::StringRef osoPrefix;
254 
255   std::vector<llvm::StringRef> dyldEnvs;
256 
archConfiguration257   llvm::MachO::Architecture arch() const { return platformInfo.target.Arch; }
258 
platformConfiguration259   llvm::MachO::PlatformType platform() const {
260     return platformInfo.target.Platform;
261   }
262 };
263 
264 extern std::unique_ptr<Configuration> config;
265 
266 } // namespace macho
267 } // namespace lld
268 
269 #endif
270