annotate lld/MachO/MarkLive.cpp @ 266:00f31e85ec16 default tip

Added tag current for changeset 31d058e83c98
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 14 Oct 2023 10:13:55 +0900
parents c4bab56944e8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 //===- MarkLive.cpp -------------------------------------------------------===//
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 //
79ff65ed7e25 LLVM12 Original
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.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 //
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 #include "MarkLive.h"
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 #include "Config.h"
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 #include "OutputSegment.h"
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 #include "SymbolTable.h"
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 #include "Symbols.h"
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 #include "UnwindInfoSection.h"
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
15
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
16 #include "lld/Common/ErrorHandler.h"
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 #include "llvm/Support/TimeProfiler.h"
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
19 #include "mach-o/compact_unwind_encoding.h"
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
20
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
21 namespace lld::macho {
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 using namespace llvm;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 using namespace llvm::MachO;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
26 struct WhyLiveEntry {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
27 InputSection *isec;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
28 // Keep track of the entry that caused us to mark `isec` as live.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
29 const WhyLiveEntry *prev;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
30
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
31 WhyLiveEntry(InputSection *isec, const WhyLiveEntry *prev)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
32 : isec(isec), prev(prev) {}
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
33 };
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
35 // Type-erased interface to MarkLiveImpl. Used for adding roots to the liveness
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
36 // graph.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
37 class MarkLive {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
38 public:
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
39 virtual void enqueue(InputSection *isec, uint64_t off) = 0;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
40 virtual void addSym(Symbol *s) = 0;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
41 virtual void markTransitively() = 0;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
42 virtual ~MarkLive() = default;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
43 };
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
45 template <bool RecordWhyLive> class MarkLiveImpl : public MarkLive {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
46 public:
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
47 // -why_live is a rarely used option, so we don't want support for that flag
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
48 // to slow down the main -dead_strip code path. As such, we employ templates
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
49 // to avoid the usage of WhyLiveEntry in the main code path. This saves us
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
50 // from needless allocations and pointer indirections.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
51 using WorklistEntry =
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
52 std::conditional_t<RecordWhyLive, WhyLiveEntry, InputSection>;
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
54 void enqueue(InputSection *isec, uint64_t off) override {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
55 enqueue(isec, off, nullptr);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
56 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
57 void addSym(Symbol *s) override { addSym(s, nullptr); }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
58 void markTransitively() override;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
59
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
60 private:
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
61 void enqueue(InputSection *isec, uint64_t off, const WorklistEntry *prev);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
62 void addSym(Symbol *s, const WorklistEntry *prev);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
63 const InputSection *getInputSection(const WorklistEntry *) const;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
64 WorklistEntry *makeEntry(InputSection *, const WorklistEntry *prev) const;
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
65
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
66 // We build up a worklist of sections which have been marked as live. We
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
67 // only push into the worklist when we discover an unmarked section, and we
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
68 // mark as we push, so sections never appear twice in the list. Literal
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
69 // sections cannot contain references to other sections, so we only store
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
70 // ConcatInputSections in our worklist.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
71 SmallVector<WorklistEntry *, 256> worklist;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
72 };
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
73
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
74 template <bool RecordWhyLive>
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
75 void MarkLiveImpl<RecordWhyLive>::enqueue(
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
76 InputSection *isec, uint64_t off,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
77 const typename MarkLiveImpl<RecordWhyLive>::WorklistEntry *prev) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
78 if (isec->isLive(off))
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
79 return;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
80 isec->markLive(off);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
81 if (auto s = dyn_cast<ConcatInputSection>(isec)) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
82 assert(!s->isCoalescedWeak());
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
83 worklist.push_back(makeEntry(s, prev));
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
84 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
85 }
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
86
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
87 static void printWhyLive(const Symbol *s, const WhyLiveEntry *prev) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
88 std::string out = toString(*s) + " from " + toString(s->getFile());
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
89 int indent = 2;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
90 for (const WhyLiveEntry *entry = prev; entry;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
91 entry = entry->prev, indent += 2) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
92 const TinyPtrVector<Defined *> &symbols = entry->isec->symbols;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
93 // With .subsections_with_symbols set, most isecs will have exactly one
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
94 // entry in their symbols vector, so we just print the first one.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
95 if (!symbols.empty())
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
96 out += "\n" + std::string(indent, ' ') + toString(*symbols.front()) +
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
97 " from " + toString(symbols.front()->getFile());
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 }
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
99 message(out);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
100 }
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
101
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
102 template <bool RecordWhyLive>
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
103 void MarkLiveImpl<RecordWhyLive>::addSym(
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
104 Symbol *s,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
105 const typename MarkLiveImpl<RecordWhyLive>::WorklistEntry *prev) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
106 if (s->used)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
107 return;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
108 s->used = true;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
109 if constexpr (RecordWhyLive)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
110 if (!config->whyLive.empty() && config->whyLive.match(s->getName()))
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
111 printWhyLive(s, prev);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
112 if (auto *d = dyn_cast<Defined>(s)) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
113 if (d->isec)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
114 enqueue(d->isec, d->value, prev);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
115 if (d->unwindEntry)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
116 enqueue(d->unwindEntry, 0, prev);
223
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
117 }
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
118 }
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
119
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
120 template <bool RecordWhyLive>
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
121 const InputSection *MarkLiveImpl<RecordWhyLive>::getInputSection(
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
122 const MarkLiveImpl<RecordWhyLive>::WorklistEntry *entry) const {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
123 if constexpr (RecordWhyLive)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
124 return entry->isec;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
125 else
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
126 return entry;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
127 }
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
128
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
129 template <bool RecordWhyLive>
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
130 typename MarkLiveImpl<RecordWhyLive>::WorklistEntry *
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
131 MarkLiveImpl<RecordWhyLive>::makeEntry(
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
132 InputSection *isec,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
133 const MarkLiveImpl<RecordWhyLive>::WorklistEntry *prev) const {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
134 if constexpr (RecordWhyLive) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
135 if (!isec) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
136 assert(!prev);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
137 return nullptr;
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
138 }
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
139 return make<WhyLiveEntry>(isec, prev);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
140 } else {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
141 return isec;
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
142 }
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
143 }
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
144
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
145 template <bool RecordWhyLive>
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
146 void MarkLiveImpl<RecordWhyLive>::markTransitively() {
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
147 do {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
148 // Mark things reachable from GC roots as live.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
149 while (!worklist.empty()) {
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
150 WorklistEntry *entry = worklist.pop_back_val();
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
151 // Entries that get placed onto the worklist always contain
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
152 // ConcatInputSections. `WhyLiveEntry::prev` may point to entries that
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
153 // contain other types of InputSections (due to S_ATTR_LIVE_SUPPORT), but
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
154 // those entries should never be pushed onto the worklist.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
155 auto *isec = cast<ConcatInputSection>(getInputSection(entry));
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
156 assert(isec->live && "We mark as live when pushing onto the worklist!");
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
157
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
158 // Mark all symbols listed in the relocation table for this section.
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
159 for (const Reloc &r : isec->relocs) {
223
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
160 if (auto *s = r.referent.dyn_cast<Symbol *>())
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
161 addSym(s, entry);
223
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
162 else
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
163 enqueue(r.referent.get<InputSection *>(), r.addend, entry);
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
164 }
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
165 for (Defined *d : getInputSection(entry)->symbols)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
166 addSym(d, entry);
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
167 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
168
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
169 // S_ATTR_LIVE_SUPPORT sections are live if they point _to_ a live
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
170 // section. Process them in a second pass.
223
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
171 for (ConcatInputSection *isec : inputSections) {
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
172 // FIXME: Check if copying all S_ATTR_LIVE_SUPPORT sections into a
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
173 // separate vector and only walking that here is faster.
223
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
174 if (!(isec->getFlags() & S_ATTR_LIVE_SUPPORT) || isec->live)
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
175 continue;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
176
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
177 for (const Reloc &r : isec->relocs) {
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
178 if (auto *s = r.referent.dyn_cast<Symbol *>()) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
179 if (s->isLive()) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
180 InputSection *referentIsec = nullptr;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
181 if (auto *d = dyn_cast<Defined>(s))
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
182 referentIsec = d->isec;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
183 enqueue(isec, 0, makeEntry(referentIsec, nullptr));
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
184 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
185 } else {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
186 auto *referentIsec = r.referent.get<InputSection *>();
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
187 if (referentIsec->isLive(r.addend))
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
188 enqueue(isec, 0, makeEntry(referentIsec, nullptr));
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
189 }
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
190 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
191 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
192
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
193 // S_ATTR_LIVE_SUPPORT could have marked additional sections live,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
194 // which in turn could mark additional S_ATTR_LIVE_SUPPORT sections live.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
195 // Iterate. In practice, the second iteration won't mark additional
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
196 // S_ATTR_LIVE_SUPPORT sections live.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
197 } while (!worklist.empty());
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
198 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
199
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
200 // Set live bit on for each reachable chunk. Unmarked (unreachable)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
201 // InputSections will be ignored by Writer, so they will be excluded
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
202 // from the final output.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
203 void markLive() {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
204 TimeTraceScope timeScope("markLive");
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
205 MarkLive *marker;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
206 if (config->whyLive.empty())
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
207 marker = make<MarkLiveImpl<false>>();
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
208 else
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
209 marker = make<MarkLiveImpl<true>>();
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
210 // Add GC roots.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
211 if (config->entry)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
212 marker->addSym(config->entry);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
213 for (Symbol *sym : symtab->getSymbols()) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
214 if (auto *defined = dyn_cast<Defined>(sym)) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
215 // -exported_symbol(s_list)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
216 if (!config->exportedSymbols.empty() &&
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
217 config->exportedSymbols.match(defined->getName())) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
218 // NOTE: Even though exporting private externs is an ill-defined
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
219 // operation, we are purposely not checking for privateExtern in
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
220 // order to follow ld64's behavior of treating all exported private
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
221 // extern symbols as live, irrespective of whether they are autohide.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
222 marker->addSym(defined);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
223 continue;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
224 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
225
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
226 // public symbols explicitly marked .no_dead_strip
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
227 if (defined->referencedDynamically || defined->noDeadStrip) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
228 marker->addSym(defined);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
229 continue;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
230 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
231
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
232 // FIXME: When we implement these flags, make symbols from them GC
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
233 // roots:
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
234 // * -reexported_symbol(s_list)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
235 // * -alias_list
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
236 // * -init
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
237
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
238 // In dylibs and bundles and in executables with -export_dynamic,
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
239 // all external functions are GC roots.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
240 bool externsAreRoots =
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
241 config->outputType != MH_EXECUTE || config->exportDynamic;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
242 if (externsAreRoots && !defined->privateExtern) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
243 marker->addSym(defined);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
244 continue;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
245 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
246 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
247 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
248 // -u symbols
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
249 for (Symbol *sym : config->explicitUndefineds)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
250 marker->addSym(sym);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
251 // local symbols explicitly marked .no_dead_strip
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
252 for (const InputFile *file : inputFiles)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
253 if (auto *objFile = dyn_cast<ObjFile>(file))
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
254 for (Symbol *sym : objFile->symbols)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
255 if (auto *defined = dyn_cast_or_null<Defined>(sym))
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
256 if (!defined->isExternal() && defined->noDeadStrip)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
257 marker->addSym(defined);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
258 if (auto *stubBinder =
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
259 dyn_cast_or_null<DylibSymbol>(symtab->find("dyld_stub_binder")))
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
260 marker->addSym(stubBinder);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
261 for (ConcatInputSection *isec : inputSections) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
262 // Sections marked no_dead_strip
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
263 if (isec->getFlags() & S_ATTR_NO_DEAD_STRIP) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
264 marker->enqueue(isec, 0);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
265 continue;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
266 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
267
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
268 // mod_init_funcs, mod_term_funcs sections
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
269 if (sectionType(isec->getFlags()) == S_MOD_INIT_FUNC_POINTERS ||
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
270 sectionType(isec->getFlags()) == S_MOD_TERM_FUNC_POINTERS) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
271 assert(!config->emitInitOffsets ||
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
272 sectionType(isec->getFlags()) != S_MOD_INIT_FUNC_POINTERS);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
273 marker->enqueue(isec, 0);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
274 continue;
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
275 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
276 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
277
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
278 for (ConcatInputSection *isec : in.initOffsets->inputs())
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
279 marker->enqueue(isec, 0);
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
280
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
281 marker->markTransitively();
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
282 }
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
283
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
284 } // namespace lld::macho