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