15ffd83dbSDimitry Andric //===- Symbols.h ------------------------------------------------*- C++ -*-===// 25ffd83dbSDimitry Andric // 35ffd83dbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 45ffd83dbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 55ffd83dbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 65ffd83dbSDimitry Andric // 75ffd83dbSDimitry Andric //===----------------------------------------------------------------------===// 85ffd83dbSDimitry Andric 95ffd83dbSDimitry Andric #ifndef LLD_MACHO_SYMBOLS_H 105ffd83dbSDimitry Andric #define LLD_MACHO_SYMBOLS_H 115ffd83dbSDimitry Andric 12349cc55cSDimitry Andric #include "Config.h" 13fe6060f1SDimitry Andric #include "InputFiles.h" 145ffd83dbSDimitry Andric #include "Target.h" 15bdd1243dSDimitry Andric 165ffd83dbSDimitry Andric #include "llvm/Object/Archive.h" 17e8d8bef9SDimitry Andric #include "llvm/Support/MathExtras.h" 185ffd83dbSDimitry Andric 195ffd83dbSDimitry Andric namespace lld { 205ffd83dbSDimitry Andric namespace macho { 215ffd83dbSDimitry Andric 22e8d8bef9SDimitry Andric class MachHeaderSection; 235ffd83dbSDimitry Andric 245ffd83dbSDimitry Andric struct StringRefZ { 255ffd83dbSDimitry Andric StringRefZ(const char *s) : data(s), size(-1) {} 265ffd83dbSDimitry Andric StringRefZ(StringRef s) : data(s.data()), size(s.size()) {} 275ffd83dbSDimitry Andric 285ffd83dbSDimitry Andric const char *data; 295ffd83dbSDimitry Andric const uint32_t size; 305ffd83dbSDimitry Andric }; 315ffd83dbSDimitry Andric 325ffd83dbSDimitry Andric class Symbol { 335ffd83dbSDimitry Andric public: 345ffd83dbSDimitry Andric enum Kind { 355ffd83dbSDimitry Andric DefinedKind, 365ffd83dbSDimitry Andric UndefinedKind, 37e8d8bef9SDimitry Andric CommonKind, 385ffd83dbSDimitry Andric DylibKind, 3904eeddc0SDimitry Andric LazyArchiveKind, 4004eeddc0SDimitry Andric LazyObjectKind, 41bdd1243dSDimitry Andric AliasKind, 425ffd83dbSDimitry Andric }; 435ffd83dbSDimitry Andric 44e8d8bef9SDimitry Andric virtual ~Symbol() {} 45e8d8bef9SDimitry Andric 46fe6060f1SDimitry Andric Kind kind() const { return symbolKind; } 475ffd83dbSDimitry Andric 48e8d8bef9SDimitry Andric StringRef getName() const { 49e8d8bef9SDimitry Andric if (nameSize == (uint32_t)-1) 50e8d8bef9SDimitry Andric nameSize = strlen(nameData); 51e8d8bef9SDimitry Andric return {nameData, nameSize}; 52e8d8bef9SDimitry Andric } 535ffd83dbSDimitry Andric 54349cc55cSDimitry Andric bool isLive() const { return used; } 5504eeddc0SDimitry Andric bool isLazy() const { 5604eeddc0SDimitry Andric return symbolKind == LazyArchiveKind || symbolKind == LazyObjectKind; 5704eeddc0SDimitry Andric } 585ffd83dbSDimitry Andric 59fe6060f1SDimitry Andric virtual uint64_t getVA() const { return 0; } 605ffd83dbSDimitry Andric 61e8d8bef9SDimitry Andric virtual bool isWeakDef() const { llvm_unreachable("cannot be weak def"); } 62e8d8bef9SDimitry Andric 63e8d8bef9SDimitry Andric // Only undefined or dylib symbols can be weak references. A weak reference 64e8d8bef9SDimitry Andric // need not be satisfied at runtime, e.g. due to the symbol not being 65e8d8bef9SDimitry Andric // available on a given target platform. 6681ad6265SDimitry Andric virtual bool isWeakRef() const { return false; } 67e8d8bef9SDimitry Andric 68e8d8bef9SDimitry Andric virtual bool isTlv() const { llvm_unreachable("cannot be TLV"); } 69e8d8bef9SDimitry Andric 70e8d8bef9SDimitry Andric // Whether this symbol is in the GOT or TLVPointer sections. 71e8d8bef9SDimitry Andric bool isInGot() const { return gotIndex != UINT32_MAX; } 72e8d8bef9SDimitry Andric 73e8d8bef9SDimitry Andric // Whether this symbol is in the StubsSection. 74e8d8bef9SDimitry Andric bool isInStubs() const { return stubsIndex != UINT32_MAX; } 75e8d8bef9SDimitry Andric 76fe6060f1SDimitry Andric uint64_t getStubVA() const; 77bdd1243dSDimitry Andric uint64_t getLazyPtrVA() const; 78fe6060f1SDimitry Andric uint64_t getGotVA() const; 79fe6060f1SDimitry Andric uint64_t getTlvVA() const; 80fe6060f1SDimitry Andric uint64_t resolveBranchVA() const { 81fe6060f1SDimitry Andric assert(isa<Defined>(this) || isa<DylibSymbol>(this)); 82fe6060f1SDimitry Andric return isInStubs() ? getStubVA() : getVA(); 83fe6060f1SDimitry Andric } 84fe6060f1SDimitry Andric uint64_t resolveGotVA() const { return isInGot() ? getGotVA() : getVA(); } 85fe6060f1SDimitry Andric uint64_t resolveTlvVA() const { return isInGot() ? getTlvVA() : getVA(); } 86fe6060f1SDimitry Andric 87e8d8bef9SDimitry Andric // The index of this symbol in the GOT or the TLVPointer section, depending 88e8d8bef9SDimitry Andric // on whether it is a thread-local. A given symbol cannot be referenced by 89e8d8bef9SDimitry Andric // both these sections at once. 905ffd83dbSDimitry Andric uint32_t gotIndex = UINT32_MAX; 9181ad6265SDimitry Andric uint32_t lazyBindOffset = UINT32_MAX; 9281ad6265SDimitry Andric uint32_t stubsHelperIndex = UINT32_MAX; 93e8d8bef9SDimitry Andric uint32_t stubsIndex = UINT32_MAX; 94e8d8bef9SDimitry Andric uint32_t symtabIndex = UINT32_MAX; 95e8d8bef9SDimitry Andric 96fe6060f1SDimitry Andric InputFile *getFile() const { return file; } 97fe6060f1SDimitry Andric 985ffd83dbSDimitry Andric protected: 99fe6060f1SDimitry Andric Symbol(Kind k, StringRefZ name, InputFile *file) 100349cc55cSDimitry Andric : symbolKind(k), nameData(name.data), file(file), nameSize(name.size), 101fe6060f1SDimitry Andric isUsedInRegularObj(!file || isa<ObjFile>(file)), 102fe6060f1SDimitry Andric used(!config->deadStrip) {} 1035ffd83dbSDimitry Andric 1045ffd83dbSDimitry Andric Kind symbolKind; 105e8d8bef9SDimitry Andric const char *nameData; 106fe6060f1SDimitry Andric InputFile *file; 107349cc55cSDimitry Andric mutable uint32_t nameSize; 108fe6060f1SDimitry Andric 109fe6060f1SDimitry Andric public: 110fe6060f1SDimitry Andric // True if this symbol was referenced by a regular (non-bitcode) object. 111fe6060f1SDimitry Andric bool isUsedInRegularObj : 1; 112fe6060f1SDimitry Andric 11381ad6265SDimitry Andric // True if this symbol is used from a live section. 114fe6060f1SDimitry Andric bool used : 1; 1155ffd83dbSDimitry Andric }; 1165ffd83dbSDimitry Andric 1175ffd83dbSDimitry Andric class Defined : public Symbol { 1185ffd83dbSDimitry Andric public: 119fe6060f1SDimitry Andric Defined(StringRefZ name, InputFile *file, InputSection *isec, uint64_t value, 120fe6060f1SDimitry Andric uint64_t size, bool isWeakDef, bool isExternal, bool isPrivateExtern, 121*06c3fb27SDimitry Andric bool includeInSymtab, bool isReferencedDynamically, bool noDeadStrip, 122*06c3fb27SDimitry Andric bool canOverrideWeakDef = false, bool isWeakDefCanBeHidden = false, 123*06c3fb27SDimitry Andric bool interposable = false); 124e8d8bef9SDimitry Andric 125e8d8bef9SDimitry Andric bool isWeakDef() const override { return weakDef; } 126e8d8bef9SDimitry Andric bool isExternalWeakDef() const { 127e8d8bef9SDimitry Andric return isWeakDef() && isExternal() && !privateExtern; 128e8d8bef9SDimitry Andric } 129349cc55cSDimitry Andric bool isTlv() const override; 130e8d8bef9SDimitry Andric 131e8d8bef9SDimitry Andric bool isExternal() const { return external; } 132e8d8bef9SDimitry Andric bool isAbsolute() const { return isec == nullptr; } 133e8d8bef9SDimitry Andric 134e8d8bef9SDimitry Andric uint64_t getVA() const override; 135e8d8bef9SDimitry Andric 136bdd1243dSDimitry Andric // Returns the object file that this symbol was defined in. This value differs 137bdd1243dSDimitry Andric // from `getFile()` if the symbol originated from a bitcode file. 138bdd1243dSDimitry Andric ObjFile *getObjectFile() const; 139bdd1243dSDimitry Andric 14081ad6265SDimitry Andric std::string getSourceLocation(); 14181ad6265SDimitry Andric 142349cc55cSDimitry Andric // Ensure this symbol's pointers to InputSections point to their canonical 143349cc55cSDimitry Andric // copies. 144349cc55cSDimitry Andric void canonicalize(); 145349cc55cSDimitry Andric 146e8d8bef9SDimitry Andric static bool classof(const Symbol *s) { return s->kind() == DefinedKind; } 1475ffd83dbSDimitry Andric 148349cc55cSDimitry Andric // Place the bitfields first so that they can get placed in the tail padding 149349cc55cSDimitry Andric // of the parent class, on platforms which support it. 150e8d8bef9SDimitry Andric bool overridesWeakDef : 1; 151fe6060f1SDimitry Andric // Whether this symbol should appear in the output binary's export trie. 152e8d8bef9SDimitry Andric bool privateExtern : 1; 153fe6060f1SDimitry Andric // Whether this symbol should appear in the output symbol table. 154fe6060f1SDimitry Andric bool includeInSymtab : 1; 15581ad6265SDimitry Andric // Whether this symbol was folded into a different symbol during ICF. 15681ad6265SDimitry Andric bool wasIdenticalCodeFolded : 1; 157fe6060f1SDimitry Andric // Symbols marked referencedDynamically won't be removed from the output's 158fe6060f1SDimitry Andric // symbol table by tools like strip. In theory, this could be set on arbitrary 159fe6060f1SDimitry Andric // symbols in input object files. In practice, it's used solely for the 160fe6060f1SDimitry Andric // synthetic __mh_execute_header symbol. 161fe6060f1SDimitry Andric // This is information for the static linker, and it's also written to the 162fe6060f1SDimitry Andric // output file's symbol table for tools running later (such as `strip`). 163fe6060f1SDimitry Andric bool referencedDynamically : 1; 164fe6060f1SDimitry Andric // Set on symbols that should not be removed by dead code stripping. 165fe6060f1SDimitry Andric // Set for example on `__attribute__((used))` globals, or on some Objective-C 166fe6060f1SDimitry Andric // metadata. This is information only for the static linker and not written 167fe6060f1SDimitry Andric // to the output. 168fe6060f1SDimitry Andric bool noDeadStrip : 1; 16981ad6265SDimitry Andric // Whether references to this symbol can be interposed at runtime to point to 17081ad6265SDimitry Andric // a different symbol definition (with the same name). For example, if both 17181ad6265SDimitry Andric // dylib A and B define an interposable symbol _foo, and we load A before B at 17281ad6265SDimitry Andric // runtime, then all references to _foo within dylib B will point to the 17381ad6265SDimitry Andric // definition in dylib A. 17481ad6265SDimitry Andric // 17581ad6265SDimitry Andric // Only extern symbols may be interposable. 17681ad6265SDimitry Andric bool interposable : 1; 177e8d8bef9SDimitry Andric 178349cc55cSDimitry Andric bool weakDefCanBeHidden : 1; 179349cc55cSDimitry Andric 180e8d8bef9SDimitry Andric private: 181e8d8bef9SDimitry Andric const bool weakDef : 1; 182e8d8bef9SDimitry Andric const bool external : 1; 183349cc55cSDimitry Andric 184349cc55cSDimitry Andric public: 185349cc55cSDimitry Andric InputSection *isec; 186349cc55cSDimitry Andric // Contains the offset from the containing subsection. Note that this is 187349cc55cSDimitry Andric // different from nlist::n_value, which is the absolute address of the symbol. 188349cc55cSDimitry Andric uint64_t value; 189349cc55cSDimitry Andric // size is only calculated for regular (non-bitcode) symbols. 190349cc55cSDimitry Andric uint64_t size; 19181ad6265SDimitry Andric // This can be a subsection of either __compact_unwind or __eh_frame. 192349cc55cSDimitry Andric ConcatInputSection *unwindEntry = nullptr; 1935ffd83dbSDimitry Andric }; 1945ffd83dbSDimitry Andric 195e8d8bef9SDimitry Andric // This enum does double-duty: as a symbol property, it indicates whether & how 196e8d8bef9SDimitry Andric // a dylib symbol is referenced. As a DylibFile property, it indicates the kind 197e8d8bef9SDimitry Andric // of referenced symbols contained within the file. If there are both weak 198e8d8bef9SDimitry Andric // and strong references to the same file, we will count the file as 199e8d8bef9SDimitry Andric // strongly-referenced. 200e8d8bef9SDimitry Andric enum class RefState : uint8_t { Unreferenced = 0, Weak = 1, Strong = 2 }; 201e8d8bef9SDimitry Andric 2025ffd83dbSDimitry Andric class Undefined : public Symbol { 2035ffd83dbSDimitry Andric public: 204bdd1243dSDimitry Andric Undefined(StringRefZ name, InputFile *file, RefState refState, 205bdd1243dSDimitry Andric bool wasBitcodeSymbol) 206bdd1243dSDimitry Andric : Symbol(UndefinedKind, name, file), refState(refState), 207bdd1243dSDimitry Andric wasBitcodeSymbol(wasBitcodeSymbol) { 208e8d8bef9SDimitry Andric assert(refState != RefState::Unreferenced); 209e8d8bef9SDimitry Andric } 210e8d8bef9SDimitry Andric 211e8d8bef9SDimitry Andric bool isWeakRef() const override { return refState == RefState::Weak; } 2125ffd83dbSDimitry Andric 2135ffd83dbSDimitry Andric static bool classof(const Symbol *s) { return s->kind() == UndefinedKind; } 214e8d8bef9SDimitry Andric 215e8d8bef9SDimitry Andric RefState refState : 2; 216bdd1243dSDimitry Andric bool wasBitcodeSymbol; 217e8d8bef9SDimitry Andric }; 218e8d8bef9SDimitry Andric 219e8d8bef9SDimitry Andric // On Unix, it is traditionally allowed to write variable definitions without 220e8d8bef9SDimitry Andric // initialization expressions (such as "int foo;") to header files. These are 221e8d8bef9SDimitry Andric // called tentative definitions. 222e8d8bef9SDimitry Andric // 223e8d8bef9SDimitry Andric // Using tentative definitions is usually considered a bad practice; you should 224e8d8bef9SDimitry Andric // write only declarations (such as "extern int foo;") to header files. 225e8d8bef9SDimitry Andric // Nevertheless, the linker and the compiler have to do something to support 226e8d8bef9SDimitry Andric // bad code by allowing duplicate definitions for this particular case. 227e8d8bef9SDimitry Andric // 228e8d8bef9SDimitry Andric // The compiler creates common symbols when it sees tentative definitions. 229e8d8bef9SDimitry Andric // (You can suppress this behavior and let the compiler create a regular 230e8d8bef9SDimitry Andric // defined symbol by passing -fno-common. -fno-common is the default in clang 231e8d8bef9SDimitry Andric // as of LLVM 11.0.) When linking the final binary, if there are remaining 232e8d8bef9SDimitry Andric // common symbols after name resolution is complete, the linker converts them 233e8d8bef9SDimitry Andric // to regular defined symbols in a __common section. 234e8d8bef9SDimitry Andric class CommonSymbol : public Symbol { 235e8d8bef9SDimitry Andric public: 236e8d8bef9SDimitry Andric CommonSymbol(StringRefZ name, InputFile *file, uint64_t size, uint32_t align, 237e8d8bef9SDimitry Andric bool isPrivateExtern) 238fe6060f1SDimitry Andric : Symbol(CommonKind, name, file), size(size), 239e8d8bef9SDimitry Andric align(align != 1 ? align : llvm::PowerOf2Ceil(size)), 240e8d8bef9SDimitry Andric privateExtern(isPrivateExtern) { 241e8d8bef9SDimitry Andric // TODO: cap maximum alignment 242e8d8bef9SDimitry Andric } 243e8d8bef9SDimitry Andric 244e8d8bef9SDimitry Andric static bool classof(const Symbol *s) { return s->kind() == CommonKind; } 245e8d8bef9SDimitry Andric 246e8d8bef9SDimitry Andric const uint64_t size; 247e8d8bef9SDimitry Andric const uint32_t align; 248e8d8bef9SDimitry Andric const bool privateExtern; 2495ffd83dbSDimitry Andric }; 2505ffd83dbSDimitry Andric 2515ffd83dbSDimitry Andric class DylibSymbol : public Symbol { 2525ffd83dbSDimitry Andric public: 253e8d8bef9SDimitry Andric DylibSymbol(DylibFile *file, StringRefZ name, bool isWeakDef, 254e8d8bef9SDimitry Andric RefState refState, bool isTlv) 255*06c3fb27SDimitry Andric : Symbol(DylibKind, name, file), shouldReexport(false), 256*06c3fb27SDimitry Andric refState(refState), weakDef(isWeakDef), tlv(isTlv) { 257fe6060f1SDimitry Andric if (file && refState > RefState::Unreferenced) 258fe6060f1SDimitry Andric file->numReferencedSymbols++; 259fe6060f1SDimitry Andric } 260e8d8bef9SDimitry Andric 261fe6060f1SDimitry Andric uint64_t getVA() const override; 262e8d8bef9SDimitry Andric bool isWeakDef() const override { return weakDef; } 2634824e7fdSDimitry Andric 2644824e7fdSDimitry Andric // Symbols from weak libraries/frameworks are also weakly-referenced. 2654824e7fdSDimitry Andric bool isWeakRef() const override { 2664824e7fdSDimitry Andric return refState == RefState::Weak || 2674824e7fdSDimitry Andric (file && getFile()->umbrella->forceWeakImport); 2684824e7fdSDimitry Andric } 269e8d8bef9SDimitry Andric bool isReferenced() const { return refState != RefState::Unreferenced; } 270e8d8bef9SDimitry Andric bool isTlv() const override { return tlv; } 271fe6060f1SDimitry Andric bool isDynamicLookup() const { return file == nullptr; } 272e8d8bef9SDimitry Andric bool hasStubsHelper() const { return stubsHelperIndex != UINT32_MAX; } 2735ffd83dbSDimitry Andric 274fe6060f1SDimitry Andric DylibFile *getFile() const { 275fe6060f1SDimitry Andric assert(!isDynamicLookup()); 276fe6060f1SDimitry Andric return cast<DylibFile>(file); 277fe6060f1SDimitry Andric } 278fe6060f1SDimitry Andric 2795ffd83dbSDimitry Andric static bool classof(const Symbol *s) { return s->kind() == DylibKind; } 2805ffd83dbSDimitry Andric 281fe6060f1SDimitry Andric RefState getRefState() const { return refState; } 282fe6060f1SDimitry Andric 283fe6060f1SDimitry Andric void reference(RefState newState) { 284fe6060f1SDimitry Andric assert(newState > RefState::Unreferenced); 285fe6060f1SDimitry Andric if (refState == RefState::Unreferenced && file) 286fe6060f1SDimitry Andric getFile()->numReferencedSymbols++; 287fe6060f1SDimitry Andric refState = std::max(refState, newState); 288fe6060f1SDimitry Andric } 289fe6060f1SDimitry Andric 290fe6060f1SDimitry Andric void unreference() { 291fe6060f1SDimitry Andric // dynamic_lookup symbols have no file. 292fe6060f1SDimitry Andric if (refState > RefState::Unreferenced && file) { 293fe6060f1SDimitry Andric assert(getFile()->numReferencedSymbols > 0); 294fe6060f1SDimitry Andric getFile()->numReferencedSymbols--; 295fe6060f1SDimitry Andric } 296fe6060f1SDimitry Andric } 297e8d8bef9SDimitry Andric 298*06c3fb27SDimitry Andric bool shouldReexport : 1; 299e8d8bef9SDimitry Andric private: 300fe6060f1SDimitry Andric RefState refState : 2; 301e8d8bef9SDimitry Andric const bool weakDef : 1; 302e8d8bef9SDimitry Andric const bool tlv : 1; 3035ffd83dbSDimitry Andric }; 3045ffd83dbSDimitry Andric 30504eeddc0SDimitry Andric class LazyArchive : public Symbol { 3065ffd83dbSDimitry Andric public: 30704eeddc0SDimitry Andric LazyArchive(ArchiveFile *file, const llvm::object::Archive::Symbol &sym) 30804eeddc0SDimitry Andric : Symbol(LazyArchiveKind, sym.getName(), file), sym(sym) {} 309fe6060f1SDimitry Andric 310fe6060f1SDimitry Andric ArchiveFile *getFile() const { return cast<ArchiveFile>(file); } 311fe6060f1SDimitry Andric void fetchArchiveMember(); 3125ffd83dbSDimitry Andric 31304eeddc0SDimitry Andric static bool classof(const Symbol *s) { return s->kind() == LazyArchiveKind; } 3145ffd83dbSDimitry Andric 3155ffd83dbSDimitry Andric private: 3165ffd83dbSDimitry Andric const llvm::object::Archive::Symbol sym; 3175ffd83dbSDimitry Andric }; 3185ffd83dbSDimitry Andric 31904eeddc0SDimitry Andric // A defined symbol in an ObjFile/BitcodeFile surrounded by --start-lib and 32004eeddc0SDimitry Andric // --end-lib. 32104eeddc0SDimitry Andric class LazyObject : public Symbol { 32204eeddc0SDimitry Andric public: 32304eeddc0SDimitry Andric LazyObject(InputFile &file, StringRef name) 32404eeddc0SDimitry Andric : Symbol(LazyObjectKind, name, &file) { 32504eeddc0SDimitry Andric isUsedInRegularObj = false; 32604eeddc0SDimitry Andric } 32704eeddc0SDimitry Andric 32804eeddc0SDimitry Andric static bool classof(const Symbol *s) { return s->kind() == LazyObjectKind; } 32904eeddc0SDimitry Andric }; 33004eeddc0SDimitry Andric 331bdd1243dSDimitry Andric // Represents N_INDR symbols. Note that if we are given valid, linkable inputs, 332bdd1243dSDimitry Andric // then all AliasSymbol instances will be converted into one of the other Symbol 333bdd1243dSDimitry Andric // types after `createAliases()` runs. 334bdd1243dSDimitry Andric class AliasSymbol final : public Symbol { 335bdd1243dSDimitry Andric public: 336bdd1243dSDimitry Andric AliasSymbol(InputFile *file, StringRef name, StringRef aliasedName, 337bdd1243dSDimitry Andric bool isPrivateExtern) 338bdd1243dSDimitry Andric : Symbol(AliasKind, name, file), privateExtern(isPrivateExtern), 339bdd1243dSDimitry Andric aliasedName(aliasedName) {} 340bdd1243dSDimitry Andric 341bdd1243dSDimitry Andric StringRef getAliasedName() const { return aliasedName; } 342bdd1243dSDimitry Andric 343bdd1243dSDimitry Andric static bool classof(const Symbol *s) { return s->kind() == AliasKind; } 344bdd1243dSDimitry Andric 345bdd1243dSDimitry Andric const bool privateExtern; 346bdd1243dSDimitry Andric 347bdd1243dSDimitry Andric private: 348bdd1243dSDimitry Andric StringRef aliasedName; 349bdd1243dSDimitry Andric }; 350bdd1243dSDimitry Andric 3515ffd83dbSDimitry Andric union SymbolUnion { 3525ffd83dbSDimitry Andric alignas(Defined) char a[sizeof(Defined)]; 3535ffd83dbSDimitry Andric alignas(Undefined) char b[sizeof(Undefined)]; 354e8d8bef9SDimitry Andric alignas(CommonSymbol) char c[sizeof(CommonSymbol)]; 355e8d8bef9SDimitry Andric alignas(DylibSymbol) char d[sizeof(DylibSymbol)]; 35604eeddc0SDimitry Andric alignas(LazyArchive) char e[sizeof(LazyArchive)]; 35704eeddc0SDimitry Andric alignas(LazyObject) char f[sizeof(LazyObject)]; 358bdd1243dSDimitry Andric alignas(AliasSymbol) char g[sizeof(AliasSymbol)]; 3595ffd83dbSDimitry Andric }; 3605ffd83dbSDimitry Andric 3615ffd83dbSDimitry Andric template <typename T, typename... ArgT> 362e8d8bef9SDimitry Andric T *replaceSymbol(Symbol *s, ArgT &&...arg) { 3635ffd83dbSDimitry Andric static_assert(sizeof(T) <= sizeof(SymbolUnion), "SymbolUnion too small"); 3645ffd83dbSDimitry Andric static_assert(alignof(T) <= alignof(SymbolUnion), 3655ffd83dbSDimitry Andric "SymbolUnion not aligned enough"); 3665ffd83dbSDimitry Andric assert(static_cast<Symbol *>(static_cast<T *>(nullptr)) == nullptr && 3675ffd83dbSDimitry Andric "Not a Symbol"); 3685ffd83dbSDimitry Andric 369fe6060f1SDimitry Andric bool isUsedInRegularObj = s->isUsedInRegularObj; 370fe6060f1SDimitry Andric bool used = s->used; 371fe6060f1SDimitry Andric T *sym = new (s) T(std::forward<ArgT>(arg)...); 372fe6060f1SDimitry Andric sym->isUsedInRegularObj |= isUsedInRegularObj; 373fe6060f1SDimitry Andric sym->used |= used; 374fe6060f1SDimitry Andric return sym; 3755ffd83dbSDimitry Andric } 3765ffd83dbSDimitry Andric 377bdd1243dSDimitry Andric // Can a symbol's address only be resolved at runtime? 378bdd1243dSDimitry Andric inline bool needsBinding(const Symbol *sym) { 379bdd1243dSDimitry Andric if (isa<DylibSymbol>(sym)) 380bdd1243dSDimitry Andric return true; 381bdd1243dSDimitry Andric if (const auto *defined = dyn_cast<Defined>(sym)) 382bdd1243dSDimitry Andric return defined->isExternalWeakDef() || defined->interposable; 383bdd1243dSDimitry Andric return false; 384bdd1243dSDimitry Andric } 385*06c3fb27SDimitry Andric 386*06c3fb27SDimitry Andric // Symbols with `l` or `L` as a prefix are linker-private and never appear in 387*06c3fb27SDimitry Andric // the output. 388*06c3fb27SDimitry Andric inline bool isPrivateLabel(StringRef name) { 389*06c3fb27SDimitry Andric return name.starts_with("l") || name.starts_with("L"); 390*06c3fb27SDimitry Andric } 3915ffd83dbSDimitry Andric } // namespace macho 3925ffd83dbSDimitry Andric 3935ffd83dbSDimitry Andric std::string toString(const macho::Symbol &); 394e8d8bef9SDimitry Andric std::string toMachOString(const llvm::object::Archive::Symbol &); 395e8d8bef9SDimitry Andric 3965ffd83dbSDimitry Andric } // namespace lld 3975ffd83dbSDimitry Andric 3985ffd83dbSDimitry Andric #endif 399