xref: /llvm-project-15.0.7/lld/MachO/Symbols.cpp (revision d56729b4)
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 uint64_t Defined::getVA() const {
35   if (isAbsolute())
36     return value;
37 
38   if (!isec->isFinal) {
39     // A target arch that does not use thunks ought never ask for
40     // the address of a function that has not yet been finalized.
41     assert(target->usesThunks());
42 
43     // MergedOutputSection::finalize() can seek the address of a
44     // function before its address is assigned. The thunking algorithm
45     // knows that unfinalized functions will be out of range, so it is
46     // expedient to return a contrived out-of-range address.
47     return TargetInfo::outOfRangeVA;
48   }
49   return isec->getVA() + value;
50 }
51 
52 uint64_t Defined::getFileOffset() const {
53   if (isAbsolute()) {
54     error("absolute symbol " + toString(*this) +
55           " does not have a file offset");
56     return 0;
57   }
58   return isec->getFileOffset() + value;
59 }
60 
61 uint64_t DylibSymbol::getVA() const {
62   return isInStubs() ? getStubVA() : Symbol::getVA();
63 }
64 
65 void LazySymbol::fetchArchiveMember() { getFile()->fetch(sym); }
66