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