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 13 using namespace llvm; 14 using namespace lld; 15 using namespace lld::macho; 16 17 // Returns a symbol for an error message. 18 static std::string demangle(StringRef symName) { 19 if (config->demangle) 20 return demangleItanium(symName); 21 return std::string(symName); 22 } 23 24 std::string lld::toString(const Symbol &sym) { return demangle(sym.getName()); } 25 26 std::string lld::toMachOString(const object::Archive::Symbol &b) { 27 return demangle(b.getName()); 28 } 29 30 uint64_t Symbol::getStubVA() const { return in.stubs->getVA(stubsIndex); } 31 uint64_t Symbol::getGotVA() const { return in.got->getVA(gotIndex); } 32 uint64_t Symbol::getTlvVA() const { return in.tlvPointers->getVA(gotIndex); } 33 34 Defined::Defined(StringRefZ name, InputFile *file, InputSection *isec, 35 uint64_t value, uint64_t size, bool isWeakDef, bool isExternal, 36 bool isPrivateExtern, bool isThumb, 37 bool isReferencedDynamically, bool noDeadStrip, 38 bool canOverrideWeakDef, bool isWeakDefCanBeHidden) 39 : Symbol(DefinedKind, name, file), isec(isec), value(value), size(size), 40 overridesWeakDef(canOverrideWeakDef), privateExtern(isPrivateExtern), 41 includeInSymtab(true), thumb(isThumb), 42 referencedDynamically(isReferencedDynamically), noDeadStrip(noDeadStrip), 43 weakDefCanBeHidden(isWeakDefCanBeHidden), weakDef(isWeakDef), 44 external(isExternal) { 45 if (isec) { 46 isec->symbols.push_back(this); 47 // Maintain sorted order. 48 for (auto it = isec->symbols.rbegin(), rend = isec->symbols.rend(); 49 it != rend; ++it) { 50 auto next = std::next(it); 51 if (next == rend) 52 break; 53 if ((*it)->value < (*next)->value) 54 std::swap(*next, *it); 55 else 56 break; 57 } 58 } 59 } 60 61 bool Defined::isTlv() const { 62 return !isAbsolute() && isThreadLocalVariables(isec->getFlags()); 63 } 64 65 uint64_t Defined::getVA() const { 66 assert(isLive() && "this should only be called for live symbols"); 67 68 if (isAbsolute()) 69 return value; 70 71 if (!isec->isFinal) { 72 // A target arch that does not use thunks ought never ask for 73 // the address of a function that has not yet been finalized. 74 assert(target->usesThunks()); 75 76 // ConcatOutputSection::finalize() can seek the address of a 77 // function before its address is assigned. The thunking algorithm 78 // knows that unfinalized functions will be out of range, so it is 79 // expedient to return a contrived out-of-range address. 80 return TargetInfo::outOfRangeVA; 81 } 82 return isec->getVA(value); 83 } 84 85 void Defined::canonicalize() { 86 if (compactUnwind) 87 compactUnwind = compactUnwind->canonical(); 88 if (isec) 89 isec = isec->canonical(); 90 } 91 92 uint64_t DylibSymbol::getVA() const { 93 return isInStubs() ? getStubVA() : Symbol::getVA(); 94 } 95 96 void LazySymbol::fetchArchiveMember() { getFile()->fetch(sym); } 97