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 emitRelativeMethodLists = false;
142 bool thinLTOEmitImportsFiles;
143 bool thinLTOEmitIndexFiles;
144 bool thinLTOIndexOnly;
145 bool timeTraceEnabled = false;
146 bool dataConst = false;
147 bool dedupStrings = true;
148 bool dedupSymbolStrings = true;
149 bool deadStripDuplicates = false;
150 bool omitDebugInfo = false;
151 bool stripSwiftForceLoad = false;
152 bool warnDylibInstallName = false;
153 bool ignoreOptimizationHints = false;
154 bool forceExactCpuSubtypeMatch = false;
155 uint32_t headerPad;
156 uint32_t dylibCompatibilityVersion = 0;
157 uint32_t dylibCurrentVersion = 0;
158 uint32_t timeTraceGranularity = 500;
159 unsigned optimize;
160 std::string progName;
161
162 // For `clang -arch arm64 -arch x86_64`, clang will:
163 // 1. invoke the linker twice, to write one temporary output per arch
164 // 2. invoke `lipo` to merge the two outputs into a single file
165 // `outputFile` is the name of the temporary file the linker writes to.
166 // `finalOutput `is the name of the file lipo writes to after the link.
167 llvm::StringRef outputFile;
168 llvm::StringRef finalOutput;
169
170 llvm::StringRef installName;
171 llvm::StringRef clientName;
172 llvm::StringRef mapFile;
173 llvm::StringRef ltoNewPmPasses;
174 llvm::StringRef ltoObjPath;
175 llvm::StringRef thinLTOJobs;
176 llvm::StringRef umbrella;
177 uint32_t ltoo = 2;
178 llvm::CodeGenOptLevel ltoCgo;
179 llvm::CachePruningPolicy thinLTOCachePolicy;
180 llvm::StringRef thinLTOCacheDir;
181 llvm::StringRef thinLTOIndexOnlyArg;
182 std::pair<llvm::StringRef, llvm::StringRef> thinLTOObjectSuffixReplace;
183 llvm::StringRef thinLTOPrefixReplaceOld;
184 llvm::StringRef thinLTOPrefixReplaceNew;
185 llvm::StringRef thinLTOPrefixReplaceNativeObject;
186 bool deadStripDylibs = false;
187 bool demangle = false;
188 bool deadStrip = false;
189 bool interposable = false;
190 bool errorForArchMismatch = false;
191 bool ignoreAutoLink = false;
192 int readWorkers = 0;
193 // ld64 allows invalid auto link options as long as the link succeeds. LLD
194 // does not, but there are cases in the wild where the invalid linker options
195 // exist. This allows users to ignore the specific invalid options in the case
196 // they can't easily fix them.
197 llvm::StringSet<> ignoreAutoLinkOptions;
198 bool strictAutoLink = false;
199 PlatformInfo platformInfo;
200 std::optional<PlatformInfo> secondaryPlatformInfo;
201 NamespaceKind namespaceKind = NamespaceKind::twolevel;
202 UndefinedSymbolTreatment undefinedSymbolTreatment =
203 UndefinedSymbolTreatment::error;
204 ICFLevel icfLevel = ICFLevel::none;
205 bool keepICFStabs = false;
206 ObjCStubsMode objcStubsMode = ObjCStubsMode::fast;
207 llvm::MachO::HeaderFileType outputType;
208 std::vector<llvm::StringRef> systemLibraryRoots;
209 std::vector<llvm::StringRef> librarySearchPaths;
210 std::vector<llvm::StringRef> frameworkSearchPaths;
211 bool warnDuplicateRpath = true;
212 llvm::SmallVector<llvm::StringRef, 0> runtimePaths;
213 llvm::SmallVector<llvm::StringRef, 0> allowableClients;
214 std::vector<std::string> astPaths;
215 std::vector<Symbol *> explicitUndefineds;
216 llvm::StringSet<> explicitDynamicLookups;
217 // There are typically few custom sectionAlignments or segmentProtections,
218 // so use a vector instead of a map.
219 std::vector<SectionAlign> sectionAlignments;
220 std::vector<SegmentProtection> segmentProtections;
221 bool ltoDebugPassManager = false;
222 bool emitLLVM = false;
223 llvm::StringRef codegenDataGeneratePath;
224 bool csProfileGenerate = false;
225 llvm::StringRef csProfilePath;
226 bool pgoWarnMismatch;
227 bool warnThinArchiveMissingMembers;
228 bool warnMissingSubsectionsViaSymbols = false;
229 bool disableVerify;
230 bool separateCstringLiteralSections;
231 bool tailMergeStrings;
232 unsigned slopScale = 256;
233
234 bool callGraphProfileSort = false;
235 llvm::StringRef printSymbolOrder;
236
237 llvm::StringRef irpgoProfilePath;
238 bool bpStartupFunctionSort = false;
239 bool bpCompressionSortStartupFunctions = false;
240 bool bpFunctionOrderForCompression = false;
241 bool bpDataOrderForCompression = false;
242 llvm::SmallVector<BPCompressionSortSpec> bpCompressionSortSpecs;
243 bool bpVerboseSectionOrderer = false;
244
245 SectionRenameMap sectionRenameMap;
246 SegmentRenameMap segmentRenameMap;
247
248 bool hasExplicitExports = false;
249 SymbolPatterns exportedSymbols;
250 SymbolPatterns unexportedSymbols;
251 SymbolPatterns whyLive;
252
253 std::vector<std::pair<llvm::StringRef, llvm::StringRef>> aliasedSymbols;
254
255 SymtabPresence localSymbolsPresence = SymtabPresence::All;
256 SymbolPatterns localSymbolPatterns;
257 llvm::SmallVector<llvm::StringRef, 0> mllvmOpts;
258 llvm::cl::LibraryOptionsContext libraryOptions;
259 llvm::SmallVector<llvm::StringRef, 0> passPlugins;
260
261 bool zeroModTime = true;
262 bool generateUuid = true;
263
264 llvm::StringRef osoPrefix;
265
266 std::vector<llvm::StringRef> dyldEnvs;
267
268 llvm::MachO::Architecture arch() const { return platformInfo.target.Arch; }
269
270 llvm::MachO::PlatformType platform() const {
271 return platformInfo.target.Platform;
272 }
273};
274
275extern std::unique_ptr<Configuration> config;
276
277} // namespace macho
278} // namespace lld
279
280#endif
281