xref: /freebsd/contrib/llvm-project/lld/MachO/Config.h (revision 02e9120893770924227138ba49df1edb3896112a)
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::CodeGenOpt {
30 enum Level : int;
31 } // namespace llvm::CodeGenOpt
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(0) << 010) |
51           version.getSubminor().value_or(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 headerPadMaxInstallNames = 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 thinLTOEmitImportsFiles;
139   bool thinLTOEmitIndexFiles;
140   bool thinLTOIndexOnly;
141   bool timeTraceEnabled = false;
142   bool dataConst = false;
143   bool dedupStrings = true;
144   bool deadStripDuplicates = false;
145   bool omitDebugInfo = false;
146   bool warnDylibInstallName = false;
147   bool ignoreOptimizationHints = false;
148   bool forceExactCpuSubtypeMatch = false;
149   uint32_t headerPad;
150   uint32_t dylibCompatibilityVersion = 0;
151   uint32_t dylibCurrentVersion = 0;
152   uint32_t timeTraceGranularity = 500;
153   unsigned optimize;
154   std::string progName;
155 
156   // For `clang -arch arm64 -arch x86_64`, clang will:
157   // 1. invoke the linker twice, to write one temporary output per arch
158   // 2. invoke `lipo` to merge the two outputs into a single file
159   // `outputFile` is the name of the temporary file the linker writes to.
160   // `finalOutput `is the name of the file lipo writes to after the link.
161   llvm::StringRef outputFile;
162   llvm::StringRef finalOutput;
163 
164   llvm::StringRef installName;
165   llvm::StringRef mapFile;
166   llvm::StringRef ltoObjPath;
167   llvm::StringRef thinLTOJobs;
168   llvm::StringRef umbrella;
169   uint32_t ltoo = 2;
170   llvm::CodeGenOpt::Level ltoCgo;
171   llvm::CachePruningPolicy thinLTOCachePolicy;
172   llvm::StringRef thinLTOCacheDir;
173   llvm::StringRef thinLTOIndexOnlyArg;
174   std::pair<llvm::StringRef, llvm::StringRef> thinLTOObjectSuffixReplace;
175   llvm::StringRef thinLTOPrefixReplaceOld;
176   llvm::StringRef thinLTOPrefixReplaceNew;
177   llvm::StringRef thinLTOPrefixReplaceNativeObject;
178   bool deadStripDylibs = false;
179   bool demangle = false;
180   bool deadStrip = false;
181   bool errorForArchMismatch = false;
182   bool ignoreAutoLink = false;
183   // ld64 allows invalid auto link options as long as the link succeeds. LLD
184   // does not, but there are cases in the wild where the invalid linker options
185   // exist. This allows users to ignore the specific invalid options in the case
186   // they can't easily fix them.
187   llvm::StringSet<> ignoreAutoLinkOptions;
188   bool strictAutoLink = false;
189   PlatformInfo platformInfo;
190   std::optional<PlatformInfo> secondaryPlatformInfo;
191   NamespaceKind namespaceKind = NamespaceKind::twolevel;
192   UndefinedSymbolTreatment undefinedSymbolTreatment =
193       UndefinedSymbolTreatment::error;
194   ICFLevel icfLevel = ICFLevel::none;
195   ObjCStubsMode objcStubsMode = ObjCStubsMode::fast;
196   llvm::MachO::HeaderFileType outputType;
197   std::vector<llvm::StringRef> systemLibraryRoots;
198   std::vector<llvm::StringRef> librarySearchPaths;
199   std::vector<llvm::StringRef> frameworkSearchPaths;
200   llvm::SmallVector<llvm::StringRef, 0> runtimePaths;
201   std::vector<std::string> astPaths;
202   std::vector<Symbol *> explicitUndefineds;
203   llvm::StringSet<> explicitDynamicLookups;
204   // There are typically few custom sectionAlignments or segmentProtections,
205   // so use a vector instead of a map.
206   std::vector<SectionAlign> sectionAlignments;
207   std::vector<SegmentProtection> segmentProtections;
208   bool ltoDebugPassManager = false;
209   bool csProfileGenerate = false;
210   llvm::StringRef csProfilePath;
211 
212   bool callGraphProfileSort = false;
213   llvm::StringRef printSymbolOrder;
214 
215   SectionRenameMap sectionRenameMap;
216   SegmentRenameMap segmentRenameMap;
217 
218   bool hasExplicitExports = false;
219   SymbolPatterns exportedSymbols;
220   SymbolPatterns unexportedSymbols;
221   SymbolPatterns whyLive;
222 
223   std::vector<std::pair<llvm::StringRef, llvm::StringRef>> aliasedSymbols;
224 
225   SymtabPresence localSymbolsPresence = SymtabPresence::All;
226   SymbolPatterns localSymbolPatterns;
227   llvm::SmallVector<llvm::StringRef, 0> mllvmOpts;
228 
229   bool zeroModTime = true;
230   bool generateUuid = true;
231 
232   llvm::StringRef osoPrefix;
233 
234   std::vector<llvm::StringRef> dyldEnvs;
235 
236   llvm::MachO::Architecture arch() const { return platformInfo.target.Arch; }
237 
238   llvm::MachO::PlatformType platform() const {
239     return platformInfo.target.Platform;
240   }
241 };
242 
243 extern std::unique_ptr<Configuration> config;
244 
245 } // namespace macho
246 } // namespace lld
247 
248 #endif
249