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