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