Path: blob/main/contrib/llvm-project/lld/MachO/Symbols.cpp
34878 views
//===- Symbols.cpp --------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "Symbols.h"9#include "InputFiles.h"10#include "SyntheticSections.h"11#include "llvm/Demangle/Demangle.h"1213using namespace llvm;14using namespace lld;15using namespace lld::macho;1617static_assert(sizeof(void *) != 8 || sizeof(Symbol) == 56,18"Try to minimize Symbol's size; we create many instances");1920// The Microsoft ABI doesn't support using parent class tail padding for child21// members, hence the _MSC_VER check.22#if !defined(_MSC_VER)23static_assert(sizeof(void *) != 8 || sizeof(Defined) == 88,24"Try to minimize Defined's size; we create many instances");25#endif2627static_assert(sizeof(SymbolUnion) == sizeof(Defined),28"Defined should be the largest Symbol kind");2930// Returns a symbol name for an error message.31static std::string maybeDemangleSymbol(StringRef symName) {32if (config->demangle) {33symName.consume_front("_");34return demangle(symName);35}36return symName.str();37}3839std::string lld::toString(const Symbol &sym) {40return maybeDemangleSymbol(sym.getName());41}4243std::string lld::toMachOString(const object::Archive::Symbol &b) {44return maybeDemangleSymbol(b.getName());45}4647uint64_t Symbol::getStubVA() const { return in.stubs->getVA(stubsIndex); }48uint64_t Symbol::getLazyPtrVA() const {49return in.lazyPointers->getVA(stubsIndex);50}51uint64_t Symbol::getGotVA() const { return in.got->getVA(gotIndex); }52uint64_t Symbol::getTlvVA() const { return in.tlvPointers->getVA(gotIndex); }5354Defined::Defined(StringRefZ name, InputFile *file, InputSection *isec,55uint64_t value, uint64_t size, bool isWeakDef, bool isExternal,56bool isPrivateExtern, bool includeInSymtab,57bool isReferencedDynamically, bool noDeadStrip,58bool canOverrideWeakDef, bool isWeakDefCanBeHidden,59bool interposable)60: Symbol(DefinedKind, name, file), overridesWeakDef(canOverrideWeakDef),61privateExtern(isPrivateExtern), includeInSymtab(includeInSymtab),62wasIdenticalCodeFolded(false),63referencedDynamically(isReferencedDynamically), noDeadStrip(noDeadStrip),64interposable(interposable), weakDefCanBeHidden(isWeakDefCanBeHidden),65weakDef(isWeakDef), external(isExternal), originalIsec(isec),66value(value), size(size) {67if (isec) {68isec->symbols.push_back(this);69// Maintain sorted order.70for (auto it = isec->symbols.rbegin(), rend = isec->symbols.rend();71it != rend; ++it) {72auto next = std::next(it);73if (next == rend)74break;75if ((*it)->value < (*next)->value)76std::swap(*next, *it);77else78break;79}80}81}8283bool Defined::isTlv() const {84return !isAbsolute() && isThreadLocalVariables(originalIsec->getFlags());85}8687uint64_t Defined::getVA() const {88assert(isLive() && "this should only be called for live symbols");8990if (isAbsolute())91return value;9293if (!isec()->isFinal) {94// A target arch that does not use thunks ought never ask for95// the address of a function that has not yet been finalized.96assert(target->usesThunks());9798// ConcatOutputSection::finalize() can seek the address of a99// function before its address is assigned. The thunking algorithm100// knows that unfinalized functions will be out of range, so it is101// expedient to return a contrived out-of-range address.102return TargetInfo::outOfRangeVA;103}104return isec()->getVA(value);105}106107ObjFile *Defined::getObjectFile() const {108return originalIsec ? dyn_cast_or_null<ObjFile>(originalIsec->getFile())109: nullptr;110}111112std::string Defined::getSourceLocation() {113if (!originalIsec)114return {};115return originalIsec->getSourceLocation(value);116}117118// Get the canonical InputSection of the symbol.119InputSection *Defined::isec() const {120return originalIsec ? originalIsec->canonical() : nullptr;121}122123// Get the canonical unwind entry of the symbol.124ConcatInputSection *Defined::unwindEntry() const {125return originalUnwindEntry ? originalUnwindEntry->canonical() : nullptr;126}127128uint64_t DylibSymbol::getVA() const {129return isInStubs() ? getStubVA() : Symbol::getVA();130}131132void LazyArchive::fetchArchiveMember() { getFile()->fetch(sym); }133134135