1 //===- Symbols.h ------------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLD_MACHO_SYMBOLS_H
10 #define LLD_MACHO_SYMBOLS_H
13 #include "InputFiles.h"
15 #include "lld/Common/ErrorHandler.h"
16 #include "lld/Common/Strings.h"
17 #include "llvm/Object/Archive.h"
18 #include "llvm/Support/MathExtras.h"
23 class MachHeaderSection
;
26 StringRefZ(const char *s
) : data(s
), size(-1) {}
27 StringRefZ(StringRef s
) : data(s
.data()), size(s
.size()) {}
46 Kind
kind() const { return symbolKind
; }
48 StringRef
getName() const {
49 if (nameSize
== (uint32_t)-1)
50 nameSize
= strlen(nameData
);
51 return {nameData
, nameSize
};
54 bool isLive() const { return used
; }
56 return symbolKind
== LazyArchiveKind
|| symbolKind
== LazyObjectKind
;
59 virtual uint64_t getVA() const { return 0; }
61 virtual bool isWeakDef() const { llvm_unreachable("cannot be weak def"); }
63 // Only undefined or dylib symbols can be weak references. A weak reference
64 // need not be satisfied at runtime, e.g. due to the symbol not being
65 // available on a given target platform.
66 virtual bool isWeakRef() const { return false; }
68 virtual bool isTlv() const { llvm_unreachable("cannot be TLV"); }
70 // Whether this symbol is in the GOT or TLVPointer sections.
71 bool isInGot() const { return gotIndex
!= UINT32_MAX
; }
73 // Whether this symbol is in the StubsSection.
74 bool isInStubs() const { return stubsIndex
!= UINT32_MAX
; }
76 uint64_t getStubVA() const;
77 uint64_t getGotVA() const;
78 uint64_t getTlvVA() const;
79 uint64_t resolveBranchVA() const {
80 assert(isa
<Defined
>(this) || isa
<DylibSymbol
>(this));
81 return isInStubs() ? getStubVA() : getVA();
83 uint64_t resolveGotVA() const { return isInGot() ? getGotVA() : getVA(); }
84 uint64_t resolveTlvVA() const { return isInGot() ? getTlvVA() : getVA(); }
86 // The index of this symbol in the GOT or the TLVPointer section, depending
87 // on whether it is a thread-local. A given symbol cannot be referenced by
88 // both these sections at once.
89 uint32_t gotIndex
= UINT32_MAX
;
90 uint32_t lazyBindOffset
= UINT32_MAX
;
91 uint32_t stubsHelperIndex
= UINT32_MAX
;
92 uint32_t stubsIndex
= UINT32_MAX
;
93 uint32_t symtabIndex
= UINT32_MAX
;
95 InputFile
*getFile() const { return file
; }
98 Symbol(Kind k
, StringRefZ name
, InputFile
*file
)
99 : symbolKind(k
), nameData(name
.data
), file(file
), nameSize(name
.size
),
100 isUsedInRegularObj(!file
|| isa
<ObjFile
>(file
)),
101 used(!config
->deadStrip
) {}
104 const char *nameData
;
106 mutable uint32_t nameSize
;
109 // True if this symbol was referenced by a regular (non-bitcode) object.
110 bool isUsedInRegularObj
: 1;
112 // True if this symbol is used from a live section.
116 class Defined
: public Symbol
{
118 Defined(StringRefZ name
, InputFile
*file
, InputSection
*isec
, uint64_t value
,
119 uint64_t size
, bool isWeakDef
, bool isExternal
, bool isPrivateExtern
,
120 bool includeInSymtab
, bool isThumb
, bool isReferencedDynamically
,
121 bool noDeadStrip
, bool canOverrideWeakDef
= false,
122 bool isWeakDefCanBeHidden
= false, bool interposable
= false);
124 bool isWeakDef() const override
{ return weakDef
; }
125 bool isExternalWeakDef() const {
126 return isWeakDef() && isExternal() && !privateExtern
;
128 bool isTlv() const override
;
130 bool isExternal() const { return external
; }
131 bool isAbsolute() const { return isec
== nullptr; }
133 uint64_t getVA() const override
;
135 std::string
getSourceLocation();
137 // Ensure this symbol's pointers to InputSections point to their canonical
141 static bool classof(const Symbol
*s
) { return s
->kind() == DefinedKind
; }
143 // Place the bitfields first so that they can get placed in the tail padding
144 // of the parent class, on platforms which support it.
145 bool overridesWeakDef
: 1;
146 // Whether this symbol should appear in the output binary's export trie.
147 bool privateExtern
: 1;
148 // Whether this symbol should appear in the output symbol table.
149 bool includeInSymtab
: 1;
150 // Whether this symbol was folded into a different symbol during ICF.
151 bool wasIdenticalCodeFolded
: 1;
152 // Only relevant when compiling for Thumb-supporting arm32 archs.
154 // Symbols marked referencedDynamically won't be removed from the output's
155 // symbol table by tools like strip. In theory, this could be set on arbitrary
156 // symbols in input object files. In practice, it's used solely for the
157 // synthetic __mh_execute_header symbol.
158 // This is information for the static linker, and it's also written to the
159 // output file's symbol table for tools running later (such as `strip`).
160 bool referencedDynamically
: 1;
161 // Set on symbols that should not be removed by dead code stripping.
162 // Set for example on `__attribute__((used))` globals, or on some Objective-C
163 // metadata. This is information only for the static linker and not written
165 bool noDeadStrip
: 1;
166 // Whether references to this symbol can be interposed at runtime to point to
167 // a different symbol definition (with the same name). For example, if both
168 // dylib A and B define an interposable symbol _foo, and we load A before B at
169 // runtime, then all references to _foo within dylib B will point to the
170 // definition in dylib A.
172 // Only extern symbols may be interposable.
173 bool interposable
: 1;
175 bool weakDefCanBeHidden
: 1;
178 const bool weakDef
: 1;
179 const bool external
: 1;
183 // Contains the offset from the containing subsection. Note that this is
184 // different from nlist::n_value, which is the absolute address of the symbol.
186 // size is only calculated for regular (non-bitcode) symbols.
188 // This can be a subsection of either __compact_unwind or __eh_frame.
189 ConcatInputSection
*unwindEntry
= nullptr;
192 // This enum does double-duty: as a symbol property, it indicates whether & how
193 // a dylib symbol is referenced. As a DylibFile property, it indicates the kind
194 // of referenced symbols contained within the file. If there are both weak
195 // and strong references to the same file, we will count the file as
196 // strongly-referenced.
197 enum class RefState
: uint8_t { Unreferenced
= 0, Weak
= 1, Strong
= 2 };
199 class Undefined
: public Symbol
{
201 Undefined(StringRefZ name
, InputFile
*file
, RefState refState
)
202 : Symbol(UndefinedKind
, name
, file
), refState(refState
) {
203 assert(refState
!= RefState::Unreferenced
);
206 bool isWeakRef() const override
{ return refState
== RefState::Weak
; }
208 static bool classof(const Symbol
*s
) { return s
->kind() == UndefinedKind
; }
210 RefState refState
: 2;
213 // On Unix, it is traditionally allowed to write variable definitions without
214 // initialization expressions (such as "int foo;") to header files. These are
215 // called tentative definitions.
217 // Using tentative definitions is usually considered a bad practice; you should
218 // write only declarations (such as "extern int foo;") to header files.
219 // Nevertheless, the linker and the compiler have to do something to support
220 // bad code by allowing duplicate definitions for this particular case.
222 // The compiler creates common symbols when it sees tentative definitions.
223 // (You can suppress this behavior and let the compiler create a regular
224 // defined symbol by passing -fno-common. -fno-common is the default in clang
225 // as of LLVM 11.0.) When linking the final binary, if there are remaining
226 // common symbols after name resolution is complete, the linker converts them
227 // to regular defined symbols in a __common section.
228 class CommonSymbol
: public Symbol
{
230 CommonSymbol(StringRefZ name
, InputFile
*file
, uint64_t size
, uint32_t align
,
231 bool isPrivateExtern
)
232 : Symbol(CommonKind
, name
, file
), size(size
),
233 align(align
!= 1 ? align
: llvm::PowerOf2Ceil(size
)),
234 privateExtern(isPrivateExtern
) {
235 // TODO: cap maximum alignment
238 static bool classof(const Symbol
*s
) { return s
->kind() == CommonKind
; }
241 const uint32_t align
;
242 const bool privateExtern
;
245 class DylibSymbol
: public Symbol
{
247 DylibSymbol(DylibFile
*file
, StringRefZ name
, bool isWeakDef
,
248 RefState refState
, bool isTlv
)
249 : Symbol(DylibKind
, name
, file
), refState(refState
), weakDef(isWeakDef
),
251 if (file
&& refState
> RefState::Unreferenced
)
252 file
->numReferencedSymbols
++;
255 uint64_t getVA() const override
;
256 bool isWeakDef() const override
{ return weakDef
; }
258 // Symbols from weak libraries/frameworks are also weakly-referenced.
259 bool isWeakRef() const override
{
260 return refState
== RefState::Weak
||
261 (file
&& getFile()->umbrella
->forceWeakImport
);
263 bool isReferenced() const { return refState
!= RefState::Unreferenced
; }
264 bool isTlv() const override
{ return tlv
; }
265 bool isDynamicLookup() const { return file
== nullptr; }
266 bool hasStubsHelper() const { return stubsHelperIndex
!= UINT32_MAX
; }
268 DylibFile
*getFile() const {
269 assert(!isDynamicLookup());
270 return cast
<DylibFile
>(file
);
273 static bool classof(const Symbol
*s
) { return s
->kind() == DylibKind
; }
275 RefState
getRefState() const { return refState
; }
277 void reference(RefState newState
) {
278 assert(newState
> RefState::Unreferenced
);
279 if (refState
== RefState::Unreferenced
&& file
)
280 getFile()->numReferencedSymbols
++;
281 refState
= std::max(refState
, newState
);
285 // dynamic_lookup symbols have no file.
286 if (refState
> RefState::Unreferenced
&& file
) {
287 assert(getFile()->numReferencedSymbols
> 0);
288 getFile()->numReferencedSymbols
--;
293 RefState refState
: 2;
294 const bool weakDef
: 1;
298 class LazyArchive
: public Symbol
{
300 LazyArchive(ArchiveFile
*file
, const llvm::object::Archive::Symbol
&sym
)
301 : Symbol(LazyArchiveKind
, sym
.getName(), file
), sym(sym
) {}
303 ArchiveFile
*getFile() const { return cast
<ArchiveFile
>(file
); }
304 void fetchArchiveMember();
306 static bool classof(const Symbol
*s
) { return s
->kind() == LazyArchiveKind
; }
309 const llvm::object::Archive::Symbol sym
;
312 // A defined symbol in an ObjFile/BitcodeFile surrounded by --start-lib and
314 class LazyObject
: public Symbol
{
316 LazyObject(InputFile
&file
, StringRef name
)
317 : Symbol(LazyObjectKind
, name
, &file
) {
318 isUsedInRegularObj
= false;
321 static bool classof(const Symbol
*s
) { return s
->kind() == LazyObjectKind
; }
325 alignas(Defined
) char a
[sizeof(Defined
)];
326 alignas(Undefined
) char b
[sizeof(Undefined
)];
327 alignas(CommonSymbol
) char c
[sizeof(CommonSymbol
)];
328 alignas(DylibSymbol
) char d
[sizeof(DylibSymbol
)];
329 alignas(LazyArchive
) char e
[sizeof(LazyArchive
)];
330 alignas(LazyObject
) char f
[sizeof(LazyObject
)];
333 template <typename T
, typename
... ArgT
>
334 T
*replaceSymbol(Symbol
*s
, ArgT
&&...arg
) {
335 static_assert(sizeof(T
) <= sizeof(SymbolUnion
), "SymbolUnion too small");
336 static_assert(alignof(T
) <= alignof(SymbolUnion
),
337 "SymbolUnion not aligned enough");
338 assert(static_cast<Symbol
*>(static_cast<T
*>(nullptr)) == nullptr &&
341 bool isUsedInRegularObj
= s
->isUsedInRegularObj
;
343 T
*sym
= new (s
) T(std::forward
<ArgT
>(arg
)...);
344 sym
->isUsedInRegularObj
|= isUsedInRegularObj
;
351 std::string
toString(const macho::Symbol
&);
352 std::string
toMachOString(const llvm::object::Archive::Symbol
&);