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 bool Symbol::isLive() const { 35 if (isa<DylibSymbol>(this) || isa<Undefined>(this)) 36 return used; 37 38 if (auto *d = dyn_cast<Defined>(this)) { 39 // Non-absolute symbols might be alive because their section is 40 // no_dead_strip or live_support. In that case, the section will know 41 // that it's live but `used` might be false. Non-absolute symbols always 42 // have to use the section's `live` bit as source of truth. 43 return d->isAbsolute() ? used : d->isec->isLive(d->value); 44 } 45 46 assert(!isa<CommonSymbol>(this) && 47 "replaceCommonSymbols() runs before dead code stripping, and isLive() " 48 "should only be called after dead code stripping"); 49 50 // Assume any other kind of symbol is live. 51 return true; 52 } 53 54 uint64_t Defined::getVA() const { 55 assert(isLive() && "this should only be called for live symbols"); 56 57 if (isAbsolute()) 58 return value; 59 60 if (!isec->isFinal) { 61 // A target arch that does not use thunks ought never ask for 62 // the address of a function that has not yet been finalized. 63 assert(target->usesThunks()); 64 65 // ConcatOutputSection::finalize() can seek the address of a 66 // function before its address is assigned. The thunking algorithm 67 // knows that unfinalized functions will be out of range, so it is 68 // expedient to return a contrived out-of-range address. 69 return TargetInfo::outOfRangeVA; 70 } 71 return isec->getVA(value); 72 } 73 74 uint64_t DylibSymbol::getVA() const { 75 return isInStubs() ? getStubVA() : Symbol::getVA(); 76 } 77 78 void LazySymbol::fetchArchiveMember() { getFile()->fetch(sym); } 79