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/MapVector.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::DenseSet<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 | ObjCStubsMode objcStubsMode = ObjCStubsMode::fast; |
197 | llvm::MachO::HeaderFileType outputType; |
198 | std::vector<llvm::StringRef> systemLibraryRoots; |
199 | std::vector<llvm::StringRef> librarySearchPaths; |
200 | std::vector<llvm::StringRef> frameworkSearchPaths; |
201 | llvm::SmallVector<llvm::StringRef, 0> runtimePaths; |
202 | std::vector<std::string> astPaths; |
203 | std::vector<Symbol *> explicitUndefineds; |
204 | llvm::StringSet<> explicitDynamicLookups; |
205 | // There are typically few custom sectionAlignments or segmentProtections, |
206 | // so use a vector instead of a map. |
207 | std::vector<SectionAlign> sectionAlignments; |
208 | std::vector<SegmentProtection> segmentProtections; |
209 | bool ltoDebugPassManager = false; |
210 | bool csProfileGenerate = false; |
211 | llvm::StringRef csProfilePath; |
212 | bool pgoWarnMismatch; |
213 | |
214 | bool callGraphProfileSort = false; |
215 | llvm::StringRef printSymbolOrder; |
216 | |
217 | SectionRenameMap sectionRenameMap; |
218 | SegmentRenameMap segmentRenameMap; |
219 | |
220 | bool hasExplicitExports = false; |
221 | SymbolPatterns exportedSymbols; |
222 | SymbolPatterns unexportedSymbols; |
223 | SymbolPatterns whyLive; |
224 | |
225 | std::vector<std::pair<llvm::StringRef, llvm::StringRef>> aliasedSymbols; |
226 | |
227 | SymtabPresence localSymbolsPresence = SymtabPresence::All; |
228 | SymbolPatterns localSymbolPatterns; |
229 | llvm::SmallVector<llvm::StringRef, 0> mllvmOpts; |
230 | |
231 | bool zeroModTime = true; |
232 | bool generateUuid = true; |
233 | |
234 | llvm::StringRef osoPrefix; |
235 | |
236 | std::vector<llvm::StringRef> dyldEnvs; |
237 | |
238 | llvm::MachO::Architecture arch() const { return platformInfo.target.Arch; } |
239 | |
240 | llvm::MachO::PlatformType platform() const { |
241 | return platformInfo.target.Platform; |
242 | } |
243 | }; |
244 | |
245 | extern std::unique_ptr<Configuration> config; |
246 | |
247 | } // namespace macho |
248 | } // namespace lld |
249 | |
250 | #endif |
251 | |