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