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 : Symbol(DefinedKind, name, file), isec(isec), value(value), size(size), 39 overridesWeakDef(false), privateExtern(isPrivateExtern), 40 includeInSymtab(true), thumb(isThumb), 41 referencedDynamically(isReferencedDynamically), noDeadStrip(noDeadStrip), 42 weakDef(isWeakDef), external(isExternal) { 43 if (isec) { 44 isec->symbols.push_back(this); 45 // Maintain sorted order. 46 for (auto it = isec->symbols.rbegin(), rend = isec->symbols.rend(); 47 it != rend; ++it) { 48 auto next = std::next(it); 49 if (next == rend) 50 break; 51 if ((*it)->value < (*next)->value) 52 std::swap(*next, *it); 53 else 54 break; 55 } 56 } 57 } 58 59 bool Defined::isTlv() const { 60 return !isAbsolute() && isThreadLocalVariables(isec->getFlags()); 61 } 62 63 uint64_t Defined::getVA() const { 64 assert(isLive() && "this should only be called for live symbols"); 65 66 if (isAbsolute()) 67 return value; 68 69 if (!isec->isFinal) { 70 // A target arch that does not use thunks ought never ask for 71 // the address of a function that has not yet been finalized. 72 assert(target->usesThunks()); 73 74 // ConcatOutputSection::finalize() can seek the address of a 75 // function before its address is assigned. The thunking algorithm 76 // knows that unfinalized functions will be out of range, so it is 77 // expedient to return a contrived out-of-range address. 78 return TargetInfo::outOfRangeVA; 79 } 80 return isec->getVA(value); 81 } 82 83 void Defined::canonicalize() { 84 if (compactUnwind) 85 compactUnwind = compactUnwind->canonical(); 86 if (isec) 87 isec = isec->canonical(); 88 } 89 90 uint64_t DylibSymbol::getVA() const { 91 return isInStubs() ? getStubVA() : Symbol::getVA(); 92 } 93 94 void LazySymbol::fetchArchiveMember() { getFile()->fetch(sym); } 95