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