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/StringRef.h"
17 #include "llvm/ADT/StringSet.h"
18 #include "llvm/BinaryFormat/MachO.h"
19 #include "llvm/Support/CachePruning.h"
20 #include "llvm/Support/GlobPattern.h"
21 #include "llvm/Support/VersionTuple.h"
22 #include "llvm/TextAPI/Architecture.h"
23 #include "llvm/TextAPI/Platform.h"
24 #include "llvm/TextAPI/Target.h"
25 
26 #include <vector>
27 
28 namespace lld {
29 namespace macho {
30 
31 class InputSection;
32 class Symbol;
33 
34 using NamePair = std::pair<llvm::StringRef, llvm::StringRef>;
35 using SectionRenameMap = llvm::DenseMap<NamePair, NamePair>;
36 using SegmentRenameMap = llvm::DenseMap<llvm::StringRef, llvm::StringRef>;
37 
38 struct PlatformInfo {
39   llvm::MachO::Target target;
40   llvm::VersionTuple minimum;
41   llvm::VersionTuple sdk;
42 };
43 
44 inline uint32_t encodeVersion(const llvm::VersionTuple &version) {
45   return ((version.getMajor() << 020) |
46           (version.getMinor().value_or(0) << 010) |
47           version.getSubminor().value_or(0));
48 }
49 
50 enum class NamespaceKind {
51   twolevel,
52   flat,
53 };
54 
55 enum class UndefinedSymbolTreatment {
56   unknown,
57   error,
58   warning,
59   suppress,
60   dynamic_lookup,
61 };
62 
63 enum class ICFLevel {
64   unknown,
65   none,
66   safe,
67   all,
68 };
69 
70 struct SectionAlign {
71   llvm::StringRef segName;
72   llvm::StringRef sectName;
73   uint32_t align;
74 };
75 
76 struct SegmentProtection {
77   llvm::StringRef name;
78   uint32_t maxProt;
79   uint32_t initProt;
80 };
81 
82 class SymbolPatterns {
83 public:
84   // GlobPattern can also match literals,
85   // but we prefer the O(1) lookup of DenseSet.
86   llvm::DenseSet<llvm::CachedHashStringRef> literals;
87   std::vector<llvm::GlobPattern> globs;
88 
89   bool empty() const { return literals.empty() && globs.empty(); }
90   void clear();
91   void insert(llvm::StringRef symbolName);
92   bool matchLiteral(llvm::StringRef symbolName) const;
93   bool matchGlob(llvm::StringRef symbolName) const;
94   bool match(llvm::StringRef symbolName) const;
95 };
96 
97 enum class SymtabPresence {
98   All,
99   None,
100   SelectivelyIncluded,
101   SelectivelyExcluded,
102 };
103 
104 struct Configuration {
105   Symbol *entry = nullptr;
106   bool hasReexports = false;
107   bool allLoad = false;
108   bool applicationExtension = false;
109   bool archMultiple = false;
110   bool exportDynamic = false;
111   bool forceLoadObjC = false;
112   bool forceLoadSwift = false;
113   bool staticLink = false;
114   bool implicitDylibs = false;
115   bool isPic = false;
116   bool headerPadMaxInstallNames = false;
117   bool markDeadStrippableDylib = false;
118   bool printDylibSearch = false;
119   bool printEachFile = false;
120   bool printWhyLoad = false;
121   bool searchDylibsFirst = false;
122   bool saveTemps = false;
123   bool adhocCodesign = false;
124   bool emitFunctionStarts = false;
125   bool emitBitcodeBundle = false;
126   bool emitDataInCodeInfo = false;
127   bool emitEncryptionInfo = false;
128   bool timeTraceEnabled = false;
129   bool dataConst = false;
130   bool dedupLiterals = true;
131   bool omitDebugInfo = false;
132   bool warnDylibInstallName = false;
133   bool ignoreOptimizationHints = false;
134   // Temporary config flag that will be removed once we have fully implemented
135   // support for __eh_frame.
136   bool parseEhFrames = false;
137   uint32_t headerPad;
138   uint32_t dylibCompatibilityVersion = 0;
139   uint32_t dylibCurrentVersion = 0;
140   uint32_t timeTraceGranularity = 500;
141   unsigned optimize;
142   std::string progName;
143 
144   // For `clang -arch arm64 -arch x86_64`, clang will:
145   // 1. invoke the linker twice, to write one temporary output per arch
146   // 2. invoke `lipo` to merge the two outputs into a single file
147   // `outputFile` is the name of the temporary file the linker writes to.
148   // `finalOutput `is the name of the file lipo writes to after the link.
149   llvm::StringRef outputFile;
150   llvm::StringRef finalOutput;
151 
152   llvm::StringRef installName;
153   llvm::StringRef mapFile;
154   llvm::StringRef ltoObjPath;
155   llvm::StringRef thinLTOJobs;
156   llvm::StringRef umbrella;
157   uint32_t ltoo = 2;
158   llvm::CachePruningPolicy thinLTOCachePolicy;
159   llvm::StringRef thinLTOCacheDir;
160   bool deadStripDylibs = false;
161   bool demangle = false;
162   bool deadStrip = false;
163   bool errorForArchMismatch = false;
164   PlatformInfo platformInfo;
165   llvm::Optional<PlatformInfo> secondaryPlatformInfo;
166   NamespaceKind namespaceKind = NamespaceKind::twolevel;
167   UndefinedSymbolTreatment undefinedSymbolTreatment =
168       UndefinedSymbolTreatment::error;
169   ICFLevel icfLevel = ICFLevel::none;
170   llvm::MachO::HeaderFileType outputType;
171   std::vector<llvm::StringRef> systemLibraryRoots;
172   std::vector<llvm::StringRef> librarySearchPaths;
173   std::vector<llvm::StringRef> frameworkSearchPaths;
174   std::vector<llvm::StringRef> runtimePaths;
175   std::vector<std::string> astPaths;
176   std::vector<Symbol *> explicitUndefineds;
177   llvm::StringSet<> explicitDynamicLookups;
178   // There are typically few custom sectionAlignments or segmentProtections,
179   // so use a vector instead of a map.
180   std::vector<SectionAlign> sectionAlignments;
181   std::vector<SegmentProtection> segmentProtections;
182 
183   bool callGraphProfileSort = false;
184   llvm::StringRef printSymbolOrder;
185 
186   SectionRenameMap sectionRenameMap;
187   SegmentRenameMap segmentRenameMap;
188 
189   bool hasExplicitExports = false;
190   SymbolPatterns exportedSymbols;
191   SymbolPatterns unexportedSymbols;
192   SymbolPatterns whyLive;
193 
194   SymtabPresence localSymbolsPresence = SymtabPresence::All;
195   SymbolPatterns localSymbolPatterns;
196 
197   bool zeroModTime = false;
198 
199   llvm::StringRef osoPrefix;
200 
201   llvm::MachO::Architecture arch() const { return platformInfo.target.Arch; }
202 
203   llvm::MachO::PlatformType platform() const {
204     return platformInfo.target.Platform;
205   }
206 };
207 
208 // Whether to force-load an archive.
209 enum class ForceLoad {
210   Default, // Apply -all_load or -ObjC behaviors if those flags are enabled
211   Yes,     // Always load the archive, regardless of other flags
212   No,      // Never load the archive, regardless of other flags
213 };
214 
215 extern std::unique_ptr<Configuration> config;
216 
217 } // namespace macho
218 } // namespace lld
219 
220 #endif
221