annotate lld/MachO/Symbols.cpp @ 207:2e18cbf3894f

LLVM12
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 08 Jun 2021 06:07:14 +0900
parents 0572611fdcc8
children 5f17cb93ff66
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 //===- Symbols.cpp --------------------------------------------------------===//
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 //
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 //
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 #include "Symbols.h"
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 #include "InputFiles.h"
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
11 #include "SyntheticSections.h"
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 using namespace llvm;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 using namespace lld;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 using namespace lld::macho;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
17 // Returns a symbol for an error message.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
18 static std::string demangle(StringRef symName) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
19 if (config->demangle)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
20 return demangleItanium(symName);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
21 return std::string(symName);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
22 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
23
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
24 std::string lld::toString(const Symbol &sym) { return demangle(sym.getName()); }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
25
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
26 std::string lld::toMachOString(const object::Archive::Symbol &b) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
27 return demangle(b.getName());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
28 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
29
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
30 uint64_t Symbol::getStubVA() const { return in.stubs->getVA(stubsIndex); }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
31 uint64_t Symbol::getGotVA() const { return in.got->getVA(gotIndex); }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
32 uint64_t Symbol::getTlvVA() const { return in.tlvPointers->getVA(gotIndex); }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
33
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
34 bool Symbol::isLive() const {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
35 if (isa<DylibSymbol>(this) || isa<Undefined>(this))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
36 return used;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
37
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
38 if (auto *d = dyn_cast<Defined>(this)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
39 // Non-absolute symbols might be alive because their section is
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
40 // no_dead_strip or live_support. In that case, the section will know
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
41 // that it's live but `used` might be false. Non-absolute symbols always
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
42 // have to use the section's `live` bit as source of truth.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
43 return d->isAbsolute() ? used : d->isec->live;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
44 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
45
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
46 assert(!isa<CommonSymbol>(this) &&
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
47 "replaceCommonSymbols() runs before dead code stripping, and isLive() "
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
48 "should only be called after dead code stripping");
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
50 // Assume any other kind of symbol is live.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
51 return true;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 }
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
53
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
54 uint64_t Defined::getVA() const {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
55 assert(isLive() && "this should only be called for live symbols");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
56
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
57 if (isAbsolute())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
58 return value;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
59
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
60 if (!isec->isFinal) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
61 // A target arch that does not use thunks ought never ask for
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
62 // the address of a function that has not yet been finalized.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
63 assert(target->usesThunks());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
64
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
65 // ConcatOutputSection::finalize() can seek the address of a
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
66 // function before its address is assigned. The thunking algorithm
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
67 // knows that unfinalized functions will be out of range, so it is
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
68 // expedient to return a contrived out-of-range address.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
69 return TargetInfo::outOfRangeVA;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
70 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
71 return isec->getVA() + value;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
72 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
73
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
74 uint64_t Defined::getFileOffset() const {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
75 if (isAbsolute()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
76 error("absolute symbol " + toString(*this) +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
77 " does not have a file offset");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
78 return 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
79 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
80 return isec->getFileOffset() + value;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
81 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
82
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
83 uint64_t DylibSymbol::getVA() const {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
84 return isInStubs() ? getStubVA() : Symbol::getVA();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
85 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
86
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
87 void LazySymbol::fetchArchiveMember() { getFile()->fetch(sym); }