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/StringRef.h"
16#include "llvm/BinaryFormat/MachO.h"
17#include "llvm/Support/GlobPattern.h"
18#include "llvm/Support/VersionTuple.h"
19#include "llvm/TextAPI/Architecture.h"
20#include "llvm/TextAPI/Platform.h"
21#include "llvm/TextAPI/Target.h"
22
23#include <vector>
24
25namespace lld {
26namespace macho {
27
28class Symbol;
29struct SymbolPriorityEntry;
30
31using NamePair = std::pair<llvm::StringRef, llvm::StringRef>;
32using SectionRenameMap = llvm::DenseMap<NamePair, NamePair>;
33using SegmentRenameMap = llvm::DenseMap<llvm::StringRef, llvm::StringRef>;
34
35struct PlatformInfo {
36 llvm::MachO::Target target;
37 llvm::VersionTuple minimum;
38 llvm::VersionTuple sdk;
39};
40
41enum class NamespaceKind {
42 twolevel,
43 flat,
44};
45
46enum class UndefinedSymbolTreatment {
47 unknown,
48 error,
49 warning,
50 suppress,
51 dynamic_lookup,
52};
53
54struct SegmentProtection {
55 llvm::StringRef name;
56 uint32_t maxProt;
57 uint32_t initProt;
58};
59
60class SymbolPatterns {
61public:
62 // GlobPattern can also match literals,
63 // but we prefer the O(1) lookup of DenseSet.
64 llvm::DenseSet<llvm::CachedHashStringRef> literals;
65 std::vector<llvm::GlobPattern> globs;
66
67 bool empty() const { return literals.empty() && globs.empty(); }
68 void clear();
69 void insert(llvm::StringRef symbolName);
70 bool matchLiteral(llvm::StringRef symbolName) const;
71 bool matchGlob(llvm::StringRef symbolName) const;
72 bool match(llvm::StringRef symbolName) const;
73};
74
75struct Configuration {
76 Symbol *entry;
77 bool hasReexports = false;
78 bool allLoad = false;
79 bool forceLoadObjC = false;
80 bool staticLink = false;
81 bool implicitDylibs = false;
82 bool isPic = false;
83 bool headerPadMaxInstallNames = false;
84 bool ltoNewPassManager = LLVM_ENABLE_NEW_PASS_MANAGER;
85 bool markDeadStrippableDylib = false;
86 bool printEachFile = false;
87 bool printWhyLoad = false;
88 bool searchDylibsFirst = false;
89 bool saveTemps = false;
90 bool adhocCodesign = false;
91 bool emitFunctionStarts = false;
92 bool emitBitcodeBundle = false;
93 bool emitEncryptionInfo = false;
94 bool timeTraceEnabled = false;
95 bool dataConst = false;
96 uint32_t headerPad;
97 uint32_t dylibCompatibilityVersion = 0;
98 uint32_t dylibCurrentVersion = 0;
99 uint32_t timeTraceGranularity = 500;
100 std::string progName;
101 llvm::StringRef installName;
102 llvm::StringRef mapFile;
103 llvm::StringRef outputFile;
104 llvm::StringRef ltoObjPath;
105 llvm::StringRef thinLTOJobs;
106 bool demangle = false;
107 PlatformInfo platformInfo;
108 NamespaceKind namespaceKind = NamespaceKind::twolevel;
109 UndefinedSymbolTreatment undefinedSymbolTreatment =
110 UndefinedSymbolTreatment::error;
111 llvm::MachO::HeaderFileType outputType;
112 std::vector<llvm::StringRef> systemLibraryRoots;
113 std::vector<llvm::StringRef> librarySearchPaths;
114 std::vector<llvm::StringRef> frameworkSearchPaths;
115 std::vector<llvm::StringRef> runtimePaths;
116 std::vector<std::string> astPaths;
117 std::vector<Symbol *> explicitUndefineds;
118 // There are typically very few custom segmentProtections, so use a vector
119 // instead of a map.
120 std::vector<SegmentProtection> segmentProtections;
121
122 llvm::DenseMap<llvm::StringRef, SymbolPriorityEntry> priorities;
123 SectionRenameMap sectionRenameMap;
124 SegmentRenameMap segmentRenameMap;
125
126 SymbolPatterns exportedSymbols;
127 SymbolPatterns unexportedSymbols;
128
129 llvm::MachO::Architecture arch() const { return platformInfo.target.Arch; }
130
131 llvm::MachO::PlatformKind platform() const {
132 return platformInfo.target.Platform;
133 }
134};
135
136// The symbol with the highest priority should be ordered first in the output
137// section (modulo input section contiguity constraints). Using priority
138// (highest first) instead of order (lowest first) has the convenient property
139// that the default-constructed zero priority -- for symbols/sections without a
140// user-defined order -- naturally ends up putting them at the end of the
141// output.
142struct SymbolPriorityEntry {
143 // The priority given to a matching symbol, regardless of which object file
144 // it originated from.
145 size_t anyObjectFile = 0;
146 // The priority given to a matching symbol from a particular object file.
147 llvm::DenseMap<llvm::StringRef, size_t> objectFiles;
148};
149
150extern Configuration *config;
151
152} // namespace macho
153} // namespace lld
154
155#endif
156