1 //===- Symbols.cpp --------------------------------------------------------===// 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 #include "Symbols.h" 10 #include "InputFiles.h" 11 #include "SyntheticSections.h" 12 #include "lld/Common/Strings.h" 13 14 using namespace llvm; 15 using namespace lld; 16 using namespace lld::macho; 17 18 static_assert(sizeof(void *) != 8 || sizeof(Symbol) == 56, 19 "Try to minimize Symbol's size; we create many instances"); 20 21 // The Microsoft ABI doesn't support using parent class tail padding for child 22 // members, hence the _MSC_VER check. 23 #if !defined(_MSC_VER) 24 static_assert(sizeof(void *) != 8 || sizeof(Defined) == 88, 25 "Try to minimize Defined's size; we create many instances"); 26 #endif 27 28 static_assert(sizeof(SymbolUnion) == sizeof(Defined), 29 "Defined should be the largest Symbol kind"); 30 31 std::string lld::toString(const Symbol &sym) { 32 return demangle(sym.getName(), config->demangle); 33 } 34 35 std::string lld::toMachOString(const object::Archive::Symbol &b) { 36 return demangle(b.getName(), config->demangle); 37 } 38 39 uint64_t Symbol::getStubVA() const { return in.stubs->getVA(stubsIndex); } 40 uint64_t Symbol::getGotVA() const { return in.got->getVA(gotIndex); } 41 uint64_t Symbol::getTlvVA() const { return in.tlvPointers->getVA(gotIndex); } 42 43 Defined::Defined(StringRefZ name, InputFile *file, InputSection *isec, 44 uint64_t value, uint64_t size, bool isWeakDef, bool isExternal, 45 bool isPrivateExtern, bool isThumb, 46 bool isReferencedDynamically, bool noDeadStrip, 47 bool canOverrideWeakDef, bool isWeakDefCanBeHidden, 48 bool interposable) 49 : Symbol(DefinedKind, name, file), overridesWeakDef(canOverrideWeakDef), 50 privateExtern(isPrivateExtern), includeInSymtab(true), thumb(isThumb), 51 referencedDynamically(isReferencedDynamically), noDeadStrip(noDeadStrip), 52 interposable(interposable), weakDefCanBeHidden(isWeakDefCanBeHidden), 53 weakDef(isWeakDef), external(isExternal), isec(isec), value(value), 54 size(size) { 55 if (isec) { 56 isec->symbols.push_back(this); 57 // Maintain sorted order. 58 for (auto it = isec->symbols.rbegin(), rend = isec->symbols.rend(); 59 it != rend; ++it) { 60 auto next = std::next(it); 61 if (next == rend) 62 break; 63 if ((*it)->value < (*next)->value) 64 std::swap(*next, *it); 65 else 66 break; 67 } 68 } 69 } 70 71 bool Defined::isTlv() const { 72 return !isAbsolute() && isThreadLocalVariables(isec->getFlags()); 73 } 74 75 uint64_t Defined::getVA() const { 76 assert(isLive() && "this should only be called for live symbols"); 77 78 if (isAbsolute()) 79 return value; 80 81 if (!isec->isFinal) { 82 // A target arch that does not use thunks ought never ask for 83 // the address of a function that has not yet been finalized. 84 assert(target->usesThunks()); 85 86 // ConcatOutputSection::finalize() can seek the address of a 87 // function before its address is assigned. The thunking algorithm 88 // knows that unfinalized functions will be out of range, so it is 89 // expedient to return a contrived out-of-range address. 90 return TargetInfo::outOfRangeVA; 91 } 92 return isec->getVA(value); 93 } 94 95 void Defined::canonicalize() { 96 if (unwindEntry) 97 unwindEntry = unwindEntry->canonical(); 98 if (isec) 99 isec = isec->canonical(); 100 } 101 102 uint64_t DylibSymbol::getVA() const { 103 return isInStubs() ? getStubVA() : Symbol::getVA(); 104 } 105 106 void LazyArchive::fetchArchiveMember() { getFile()->fetch(sym); } 107