1 | //===- UnwindInfoSection.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 "UnwindInfoSection.h" |
10 | #include "InputSection.h" |
11 | #include "Layout.h" |
12 | #include "OutputSection.h" |
13 | #include "OutputSegment.h" |
14 | #include "SymbolTable.h" |
15 | #include "Symbols.h" |
16 | #include "SyntheticSections.h" |
17 | #include "Target.h" |
18 | |
19 | #include "lld/Common/ErrorHandler.h" |
20 | #include "lld/Common/Memory.h" |
21 | #include "llvm/ADT/DenseMap.h" |
22 | #include "llvm/ADT/STLExtras.h" |
23 | #include "llvm/BinaryFormat/MachO.h" |
24 | #include "llvm/Support/Parallel.h" |
25 | |
26 | #include "mach-o/compact_unwind_encoding.h" |
27 | |
28 | #include <numeric> |
29 | |
30 | using namespace llvm; |
31 | using namespace llvm::MachO; |
32 | using namespace llvm::support::endian; |
33 | using namespace lld; |
34 | using namespace lld::macho; |
35 | |
36 | #define COMMON_ENCODINGS_MAX 127 |
37 | #define COMPACT_ENCODINGS_MAX 256 |
38 | |
39 | #define SECOND_LEVEL_PAGE_BYTES 4096 |
40 | #define SECOND_LEVEL_PAGE_WORDS (SECOND_LEVEL_PAGE_BYTES / sizeof(uint32_t)) |
41 | #define REGULAR_SECOND_LEVEL_ENTRIES_MAX \ |
42 | ((SECOND_LEVEL_PAGE_BYTES - \ |
43 | sizeof(unwind_info_regular_second_level_page_header)) / \ |
44 | sizeof(unwind_info_regular_second_level_entry)) |
45 | #define COMPRESSED_SECOND_LEVEL_ENTRIES_MAX \ |
46 | ((SECOND_LEVEL_PAGE_BYTES - \ |
47 | sizeof(unwind_info_compressed_second_level_page_header)) / \ |
48 | sizeof(uint32_t)) |
49 | |
50 | #define COMPRESSED_ENTRY_FUNC_OFFSET_BITS 24 |
51 | #define COMPRESSED_ENTRY_FUNC_OFFSET_MASK \ |
52 | UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET(~0) |
53 | |
54 | static_assert(static_cast<uint32_t>(UNWIND_X86_64_DWARF_SECTION_OFFSET) == |
55 | static_cast<uint32_t>(UNWIND_ARM64_DWARF_SECTION_OFFSET) && |
56 | static_cast<uint32_t>(UNWIND_X86_64_DWARF_SECTION_OFFSET) == |
57 | static_cast<uint32_t>(UNWIND_X86_DWARF_SECTION_OFFSET)); |
58 | |
59 | constexpr uint64_t DWARF_SECTION_OFFSET = UNWIND_X86_64_DWARF_SECTION_OFFSET; |
60 | |
61 | // Compact Unwind format is a Mach-O evolution of DWARF Unwind that |
62 | // optimizes space and exception-time lookup. Most DWARF unwind |
63 | // entries can be replaced with Compact Unwind entries, but the ones |
64 | // that cannot are retained in DWARF form. |
65 | // |
66 | // This comment will address macro-level organization of the pre-link |
67 | // and post-link compact unwind tables. For micro-level organization |
68 | // pertaining to the bitfield layout of the 32-bit compact unwind |
69 | // entries, see libunwind/include/mach-o/compact_unwind_encoding.h |
70 | // |
71 | // Important clarifying factoids: |
72 | // |
73 | // * __LD,__compact_unwind is the compact unwind format for compiler |
74 | // output and linker input. It is never a final output. It could be |
75 | // an intermediate output with the `-r` option which retains relocs. |
76 | // |
77 | // * __TEXT,__unwind_info is the compact unwind format for final |
78 | // linker output. It is never an input. |
79 | // |
80 | // * __TEXT,__eh_frame is the DWARF format for both linker input and output. |
81 | // |
82 | // * __TEXT,__unwind_info entries are divided into 4 KiB pages (2nd |
83 | // level) by ascending address, and the pages are referenced by an |
84 | // index (1st level) in the section header. |
85 | // |
86 | // * Following the headers in __TEXT,__unwind_info, the bulk of the |
87 | // section contains a vector of compact unwind entries |
88 | // `{functionOffset, encoding}` sorted by ascending `functionOffset`. |
89 | // Adjacent entries with the same encoding can be folded to great |
90 | // advantage, achieving a 3-order-of-magnitude reduction in the |
91 | // number of entries. |
92 | // |
93 | // Refer to the definition of unwind_info_section_header in |
94 | // compact_unwind_encoding.h for an overview of the format we are encoding |
95 | // here. |
96 | |
97 | // TODO(gkm): how do we align the 2nd-level pages? |
98 | |
99 | // The various fields in the on-disk representation of each compact unwind |
100 | // entry. |
101 | #define FOR_EACH_CU_FIELD(DO) \ |
102 | DO(Ptr, functionAddress) \ |
103 | DO(uint32_t, functionLength) \ |
104 | DO(compact_unwind_encoding_t, encoding) \ |
105 | DO(Ptr, personality) \ |
106 | DO(Ptr, lsda) |
107 | |
108 | CREATE_LAYOUT_CLASS(CompactUnwind, FOR_EACH_CU_FIELD); |
109 | |
110 | #undef FOR_EACH_CU_FIELD |
111 | |
112 | // LLD's internal representation of a compact unwind entry. |
113 | struct CompactUnwindEntry { |
114 | uint64_t functionAddress; |
115 | uint32_t functionLength; |
116 | compact_unwind_encoding_t encoding; |
117 | Symbol *personality; |
118 | InputSection *lsda; |
119 | }; |
120 | |
121 | using EncodingMap = DenseMap<compact_unwind_encoding_t, size_t>; |
122 | |
123 | struct SecondLevelPage { |
124 | uint32_t kind; |
125 | size_t entryIndex; |
126 | size_t entryCount; |
127 | size_t byteCount; |
128 | std::vector<compact_unwind_encoding_t> localEncodings; |
129 | EncodingMap localEncodingIndexes; |
130 | }; |
131 | |
132 | // UnwindInfoSectionImpl allows us to avoid cluttering our header file with a |
133 | // lengthy definition of UnwindInfoSection. |
134 | class UnwindInfoSectionImpl final : public UnwindInfoSection { |
135 | public: |
136 | UnwindInfoSectionImpl() : cuLayout(target->wordSize) {} |
137 | uint64_t getSize() const override { return unwindInfoSize; } |
138 | void prepare() override; |
139 | void finalize() override; |
140 | void writeTo(uint8_t *buf) const override; |
141 | |
142 | private: |
143 | void prepareRelocations(ConcatInputSection *); |
144 | void relocateCompactUnwind(std::vector<CompactUnwindEntry> &); |
145 | void encodePersonalities(); |
146 | Symbol *canonicalizePersonality(Symbol *); |
147 | |
148 | uint64_t unwindInfoSize = 0; |
149 | SmallVector<decltype(symbols)::value_type, 0> symbolsVec; |
150 | CompactUnwindLayout cuLayout; |
151 | std::vector<std::pair<compact_unwind_encoding_t, size_t>> commonEncodings; |
152 | EncodingMap commonEncodingIndexes; |
153 | // The entries here will be in the same order as their originating symbols |
154 | // in symbolsVec. |
155 | std::vector<CompactUnwindEntry> cuEntries; |
156 | // Indices into the cuEntries vector. |
157 | std::vector<size_t> cuIndices; |
158 | std::vector<Symbol *> personalities; |
159 | SmallDenseMap<std::pair<InputSection *, uint64_t /* addend */>, Symbol *> |
160 | personalityTable; |
161 | // Indices into cuEntries for CUEs with a non-null LSDA. |
162 | std::vector<size_t> entriesWithLsda; |
163 | // Map of cuEntries index to an index within the LSDA array. |
164 | DenseMap<size_t, uint32_t> lsdaIndex; |
165 | std::vector<SecondLevelPage> secondLevelPages; |
166 | uint64_t level2PagesOffset = 0; |
167 | // The highest-address function plus its size. The unwinder needs this to |
168 | // determine the address range that is covered by unwind info. |
169 | uint64_t cueEndBoundary = 0; |
170 | }; |
171 | |
172 | UnwindInfoSection::UnwindInfoSection() |
173 | : SyntheticSection(segment_names::text, section_names::unwindInfo) { |
174 | align = 4; |
175 | } |
176 | |
177 | // Record function symbols that may need entries emitted in __unwind_info, which |
178 | // stores unwind data for address ranges. |
179 | // |
180 | // Note that if several adjacent functions have the same unwind encoding and |
181 | // personality function and no LSDA, they share one unwind entry. For this to |
182 | // work, functions without unwind info need explicit "no unwind info" unwind |
183 | // entries -- else the unwinder would think they have the unwind info of the |
184 | // closest function with unwind info right before in the image. Thus, we add |
185 | // function symbols for each unique address regardless of whether they have |
186 | // associated unwind info. |
187 | void UnwindInfoSection::addSymbol(const Defined *d) { |
188 | if (d->unwindEntry()) |
189 | allEntriesAreOmitted = false; |
190 | // We don't yet know the final output address of this symbol, but we know that |
191 | // they are uniquely determined by a combination of the isec and value, so |
192 | // we use that as the key here. |
193 | auto p = symbols.insert(KV: {{d->isec(), d->value}, d}); |
194 | // If we have multiple symbols at the same address, only one of them can have |
195 | // an associated unwind entry. |
196 | if (!p.second && d->unwindEntry()) { |
197 | assert(p.first->second == d || !p.first->second->unwindEntry()); |
198 | p.first->second = d; |
199 | } |
200 | } |
201 | |
202 | void UnwindInfoSectionImpl::prepare() { |
203 | // This iteration needs to be deterministic, since prepareRelocations may add |
204 | // entries to the GOT. Hence the use of a MapVector for |
205 | // UnwindInfoSection::symbols. |
206 | for (const Defined *d : make_second_range(c&: symbols)) |
207 | if (d->unwindEntry()) { |
208 | if (d->unwindEntry()->getName() == section_names::compactUnwind) { |
209 | prepareRelocations(d->unwindEntry()); |
210 | } else { |
211 | // We don't have to add entries to the GOT here because FDEs have |
212 | // explicit GOT relocations, so Writer::scanRelocations() will add those |
213 | // GOT entries. However, we still need to canonicalize the personality |
214 | // pointers (like prepareRelocations() does for CU entries) in order |
215 | // to avoid overflowing the 3-personality limit. |
216 | FDE &fde = cast<ObjFile>(Val: d->getFile())->fdes[d->unwindEntry()]; |
217 | fde.personality = canonicalizePersonality(fde.personality); |
218 | } |
219 | } |
220 | } |
221 | |
222 | // Compact unwind relocations have different semantics, so we handle them in a |
223 | // separate code path from regular relocations. First, we do not wish to add |
224 | // rebase opcodes for __LD,__compact_unwind, because that section doesn't |
225 | // actually end up in the final binary. Second, personality pointers always |
226 | // reside in the GOT and must be treated specially. |
227 | void UnwindInfoSectionImpl::prepareRelocations(ConcatInputSection *isec) { |
228 | assert(!isec->shouldOmitFromOutput() && |
229 | "__compact_unwind section should not be omitted" ); |
230 | |
231 | // FIXME: Make this skip relocations for CompactUnwindEntries that |
232 | // point to dead-stripped functions. That might save some amount of |
233 | // work. But since there are usually just few personality functions |
234 | // that are referenced from many places, at least some of them likely |
235 | // live, it wouldn't reduce number of got entries. |
236 | for (size_t i = 0; i < isec->relocs.size(); ++i) { |
237 | Reloc &r = isec->relocs[i]; |
238 | assert(target->hasAttr(r.type, RelocAttrBits::UNSIGNED)); |
239 | // Since compact unwind sections aren't part of the inputSections vector, |
240 | // they don't get canonicalized by scanRelocations(), so we have to do the |
241 | // canonicalization here. |
242 | if (auto *referentIsec = r.referent.dyn_cast<InputSection *>()) |
243 | r.referent = referentIsec->canonical(); |
244 | |
245 | // Functions and LSDA entries always reside in the same object file as the |
246 | // compact unwind entries that references them, and thus appear as section |
247 | // relocs. There is no need to prepare them. We only prepare relocs for |
248 | // personality functions. |
249 | if (r.offset != cuLayout.personalityOffset) |
250 | continue; |
251 | |
252 | if (auto *s = r.referent.dyn_cast<Symbol *>()) { |
253 | // Personality functions are nearly always system-defined (e.g., |
254 | // ___gxx_personality_v0 for C++) and relocated as dylib symbols. When an |
255 | // application provides its own personality function, it might be |
256 | // referenced by an extern Defined symbol reloc, or a local section reloc. |
257 | if (auto *defined = dyn_cast<Defined>(Val: s)) { |
258 | // XXX(vyng) This is a special case for handling duplicate personality |
259 | // symbols. Note that LD64's behavior is a bit different and it is |
260 | // inconsistent with how symbol resolution usually work |
261 | // |
262 | // So we've decided not to follow it. Instead, simply pick the symbol |
263 | // with the same name from the symbol table to replace the local one. |
264 | // |
265 | // (See discussions/alternatives already considered on D107533) |
266 | if (!defined->isExternal()) |
267 | if (Symbol *sym = symtab->find(name: defined->getName())) |
268 | if (!sym->isLazy()) |
269 | r.referent = s = sym; |
270 | } |
271 | if (auto *undefined = dyn_cast<Undefined>(Val: s)) { |
272 | treatUndefinedSymbol(*undefined, isec, offset: r.offset); |
273 | // treatUndefinedSymbol() can replace s with a DylibSymbol; re-check. |
274 | if (isa<Undefined>(Val: s)) |
275 | continue; |
276 | } |
277 | |
278 | // Similar to canonicalizePersonality(), but we also register a GOT entry. |
279 | if (auto *defined = dyn_cast<Defined>(Val: s)) { |
280 | // Check if we have created a synthetic symbol at the same address. |
281 | Symbol *&personality = |
282 | personalityTable[{defined->isec(), defined->value}]; |
283 | if (personality == nullptr) { |
284 | personality = defined; |
285 | in.got->addEntry(sym: defined); |
286 | } else if (personality != defined) { |
287 | r.referent = personality; |
288 | } |
289 | continue; |
290 | } |
291 | |
292 | assert(isa<DylibSymbol>(s)); |
293 | in.got->addEntry(sym: s); |
294 | continue; |
295 | } |
296 | |
297 | if (auto *referentIsec = r.referent.dyn_cast<InputSection *>()) { |
298 | assert(!isCoalescedWeak(referentIsec)); |
299 | // Personality functions can be referenced via section relocations |
300 | // if they live in the same object file. Create placeholder synthetic |
301 | // symbols for them in the GOT. |
302 | Symbol *&s = personalityTable[{referentIsec, r.addend}]; |
303 | if (s == nullptr) { |
304 | // This runs after dead stripping, so the noDeadStrip argument does not |
305 | // matter. |
306 | s = make<Defined>(args: "<internal>" , /*file=*/args: nullptr, args&: referentIsec, |
307 | args&: r.addend, /*size=*/args: 0, /*isWeakDef=*/args: false, |
308 | /*isExternal=*/args: false, /*isPrivateExtern=*/args: false, |
309 | /*includeInSymtab=*/args: true, |
310 | /*isReferencedDynamically=*/args: false, |
311 | /*noDeadStrip=*/args: false); |
312 | s->used = true; |
313 | in.got->addEntry(sym: s); |
314 | } |
315 | r.referent = s; |
316 | r.addend = 0; |
317 | } |
318 | } |
319 | } |
320 | |
321 | Symbol *UnwindInfoSectionImpl::canonicalizePersonality(Symbol *personality) { |
322 | if (auto *defined = dyn_cast_or_null<Defined>(Val: personality)) { |
323 | // Check if we have created a synthetic symbol at the same address. |
324 | Symbol *&synth = personalityTable[{defined->isec(), defined->value}]; |
325 | if (synth == nullptr) |
326 | synth = defined; |
327 | else if (synth != defined) |
328 | return synth; |
329 | } |
330 | return personality; |
331 | } |
332 | |
333 | // We need to apply the relocations to the pre-link compact unwind section |
334 | // before converting it to post-link form. There should only be absolute |
335 | // relocations here: since we are not emitting the pre-link CU section, there |
336 | // is no source address to make a relative location meaningful. |
337 | void UnwindInfoSectionImpl::relocateCompactUnwind( |
338 | std::vector<CompactUnwindEntry> &cuEntries) { |
339 | parallelFor(Begin: 0, End: symbolsVec.size(), Fn: [&](size_t i) { |
340 | CompactUnwindEntry &cu = cuEntries[i]; |
341 | const Defined *d = symbolsVec[i].second; |
342 | cu.functionAddress = d->getVA(); |
343 | if (!d->unwindEntry()) |
344 | return; |
345 | |
346 | // If we have DWARF unwind info, create a slimmed-down CU entry that points |
347 | // to it. |
348 | if (d->unwindEntry()->getName() == section_names::ehFrame) { |
349 | // The unwinder will look for the DWARF entry starting at the hint, |
350 | // assuming the hint points to a valid CFI record start. If it |
351 | // fails to find the record, it proceeds in a linear search through the |
352 | // contiguous CFI records from the hint until the end of the section. |
353 | // Ideally, in the case where the offset is too large to be encoded, we |
354 | // would instead encode the largest possible offset to a valid CFI record, |
355 | // but since we don't keep track of that, just encode zero -- the start of |
356 | // the section is always the start of a CFI record. |
357 | uint64_t dwarfOffsetHint = |
358 | d->unwindEntry()->outSecOff <= DWARF_SECTION_OFFSET |
359 | ? d->unwindEntry()->outSecOff |
360 | : 0; |
361 | cu.encoding = target->modeDwarfEncoding | dwarfOffsetHint; |
362 | const FDE &fde = cast<ObjFile>(Val: d->getFile())->fdes[d->unwindEntry()]; |
363 | cu.functionLength = fde.funcLength; |
364 | // Omit the DWARF personality from compact-unwind entry so that we |
365 | // don't need to encode it. |
366 | cu.personality = nullptr; |
367 | cu.lsda = fde.lsda; |
368 | return; |
369 | } |
370 | |
371 | assert(d->unwindEntry()->getName() == section_names::compactUnwind); |
372 | |
373 | auto buf = |
374 | reinterpret_cast<const uint8_t *>(d->unwindEntry()->data.data()) - |
375 | target->wordSize; |
376 | cu.functionLength = |
377 | support::endian::read32le(P: buf + cuLayout.functionLengthOffset); |
378 | cu.encoding = support::endian::read32le(P: buf + cuLayout.encodingOffset); |
379 | for (const Reloc &r : d->unwindEntry()->relocs) { |
380 | if (r.offset == cuLayout.personalityOffset) |
381 | cu.personality = r.referent.get<Symbol *>(); |
382 | else if (r.offset == cuLayout.lsdaOffset) |
383 | cu.lsda = r.getReferentInputSection(); |
384 | } |
385 | }); |
386 | } |
387 | |
388 | // There should only be a handful of unique personality pointers, so we can |
389 | // encode them as 2-bit indices into a small array. |
390 | void UnwindInfoSectionImpl::encodePersonalities() { |
391 | for (size_t idx : cuIndices) { |
392 | CompactUnwindEntry &cu = cuEntries[idx]; |
393 | if (cu.personality == nullptr) |
394 | continue; |
395 | // Linear search is fast enough for a small array. |
396 | auto it = find(Range&: personalities, Val: cu.personality); |
397 | uint32_t personalityIndex; // 1-based index |
398 | if (it != personalities.end()) { |
399 | personalityIndex = std::distance(first: personalities.begin(), last: it) + 1; |
400 | } else { |
401 | personalities.push_back(x: cu.personality); |
402 | personalityIndex = personalities.size(); |
403 | } |
404 | cu.encoding |= |
405 | personalityIndex << llvm::countr_zero( |
406 | Val: static_cast<compact_unwind_encoding_t>(UNWIND_PERSONALITY_MASK)); |
407 | } |
408 | if (personalities.size() > 3) |
409 | error(msg: "too many personalities (" + Twine(personalities.size()) + |
410 | ") for compact unwind to encode" ); |
411 | } |
412 | |
413 | static bool canFoldEncoding(compact_unwind_encoding_t encoding) { |
414 | // From compact_unwind_encoding.h: |
415 | // UNWIND_X86_64_MODE_STACK_IND: |
416 | // A "frameless" (RBP not used as frame pointer) function large constant |
417 | // stack size. This case is like the previous, except the stack size is too |
418 | // large to encode in the compact unwind encoding. Instead it requires that |
419 | // the function contains "subq $nnnnnnnn,RSP" in its prolog. The compact |
420 | // encoding contains the offset to the nnnnnnnn value in the function in |
421 | // UNWIND_X86_64_FRAMELESS_STACK_SIZE. |
422 | // Since this means the unwinder has to look at the `subq` in the function |
423 | // of the unwind info's unwind address, two functions that have identical |
424 | // unwind info can't be folded if it's using this encoding since both |
425 | // entries need unique addresses. |
426 | static_assert(static_cast<uint32_t>(UNWIND_X86_64_MODE_STACK_IND) == |
427 | static_cast<uint32_t>(UNWIND_X86_MODE_STACK_IND)); |
428 | if ((target->cpuType == CPU_TYPE_X86_64 || target->cpuType == CPU_TYPE_X86) && |
429 | (encoding & UNWIND_MODE_MASK) == UNWIND_X86_64_MODE_STACK_IND) { |
430 | // FIXME: Consider passing in the two function addresses and getting |
431 | // their two stack sizes off the `subq` and only returning false if they're |
432 | // actually different. |
433 | return false; |
434 | } |
435 | return true; |
436 | } |
437 | |
438 | // Scan the __LD,__compact_unwind entries and compute the space needs of |
439 | // __TEXT,__unwind_info and __TEXT,__eh_frame. |
440 | void UnwindInfoSectionImpl::finalize() { |
441 | if (symbols.empty()) |
442 | return; |
443 | |
444 | // At this point, the address space for __TEXT,__text has been |
445 | // assigned, so we can relocate the __LD,__compact_unwind entries |
446 | // into a temporary buffer. Relocation is necessary in order to sort |
447 | // the CU entries by function address. Sorting is necessary so that |
448 | // we can fold adjacent CU entries with identical encoding+personality |
449 | // and without any LSDA. Folding is necessary because it reduces the |
450 | // number of CU entries by as much as 3 orders of magnitude! |
451 | cuEntries.resize(new_size: symbols.size()); |
452 | // The "map" part of the symbols MapVector was only needed for deduplication |
453 | // in addSymbol(). Now that we are done adding, move the contents to a plain |
454 | // std::vector for indexed access. |
455 | symbolsVec = symbols.takeVector(); |
456 | relocateCompactUnwind(cuEntries); |
457 | |
458 | // Rather than sort & fold the 32-byte entries directly, we create a |
459 | // vector of indices to entries and sort & fold that instead. |
460 | cuIndices.resize(new_size: cuEntries.size()); |
461 | std::iota(first: cuIndices.begin(), last: cuIndices.end(), value: 0); |
462 | llvm::sort(C&: cuIndices, Comp: [&](size_t a, size_t b) { |
463 | return cuEntries[a].functionAddress < cuEntries[b].functionAddress; |
464 | }); |
465 | |
466 | // Record the ending boundary before we fold the entries. |
467 | cueEndBoundary = cuEntries[cuIndices.back()].functionAddress + |
468 | cuEntries[cuIndices.back()].functionLength; |
469 | |
470 | // Fold adjacent entries with matching encoding+personality and without LSDA |
471 | // We use three iterators on the same cuIndices to fold in-situ: |
472 | // (1) `foldBegin` is the first of a potential sequence of matching entries |
473 | // (2) `foldEnd` is the first non-matching entry after `foldBegin`. |
474 | // The semi-open interval [ foldBegin .. foldEnd ) contains a range |
475 | // entries that can be folded into a single entry and written to ... |
476 | // (3) `foldWrite` |
477 | auto foldWrite = cuIndices.begin(); |
478 | for (auto foldBegin = cuIndices.begin(); foldBegin < cuIndices.end();) { |
479 | auto foldEnd = foldBegin; |
480 | // Common LSDA encodings (e.g. for C++ and Objective-C) contain offsets from |
481 | // a base address. The base address is normally not contained directly in |
482 | // the LSDA, and in that case, the personality function treats the starting |
483 | // address of the function (which is computed by the unwinder) as the base |
484 | // address and interprets the LSDA accordingly. The unwinder computes the |
485 | // starting address of a function as the address associated with its CU |
486 | // entry. For this reason, we cannot fold adjacent entries if they have an |
487 | // LSDA, because folding would make the unwinder compute the wrong starting |
488 | // address for the functions with the folded entries, which in turn would |
489 | // cause the personality function to misinterpret the LSDA for those |
490 | // functions. In the very rare case where the base address is encoded |
491 | // directly in the LSDA, two functions at different addresses would |
492 | // necessarily have different LSDAs, so their CU entries would not have been |
493 | // folded anyway. |
494 | while (++foldEnd < cuIndices.end() && |
495 | cuEntries[*foldBegin].encoding == cuEntries[*foldEnd].encoding && |
496 | !cuEntries[*foldBegin].lsda && !cuEntries[*foldEnd].lsda && |
497 | // If we've gotten to this point, we don't have an LSDA, which should |
498 | // also imply that we don't have a personality function, since in all |
499 | // likelihood a personality function needs the LSDA to do anything |
500 | // useful. It can be technically valid to have a personality function |
501 | // and no LSDA though (e.g. the C++ personality __gxx_personality_v0 |
502 | // is just a no-op without LSDA), so we still check for personality |
503 | // function equivalence to handle that case. |
504 | cuEntries[*foldBegin].personality == |
505 | cuEntries[*foldEnd].personality && |
506 | canFoldEncoding(encoding: cuEntries[*foldEnd].encoding)) |
507 | ; |
508 | *foldWrite++ = *foldBegin; |
509 | foldBegin = foldEnd; |
510 | } |
511 | cuIndices.erase(first: foldWrite, last: cuIndices.end()); |
512 | |
513 | encodePersonalities(); |
514 | |
515 | // Count frequencies of the folded encodings |
516 | EncodingMap encodingFrequencies; |
517 | for (size_t idx : cuIndices) |
518 | encodingFrequencies[cuEntries[idx].encoding]++; |
519 | |
520 | // Make a vector of encodings, sorted by descending frequency |
521 | for (const auto &frequency : encodingFrequencies) |
522 | commonEncodings.emplace_back(args: frequency); |
523 | llvm::sort(C&: commonEncodings, |
524 | Comp: [](const std::pair<compact_unwind_encoding_t, size_t> &a, |
525 | const std::pair<compact_unwind_encoding_t, size_t> &b) { |
526 | if (a.second == b.second) |
527 | // When frequencies match, secondarily sort on encoding |
528 | // to maintain parity with validate-unwind-info.py |
529 | return a.first > b.first; |
530 | return a.second > b.second; |
531 | }); |
532 | |
533 | // Truncate the vector to 127 elements. |
534 | // Common encoding indexes are limited to 0..126, while encoding |
535 | // indexes 127..255 are local to each second-level page |
536 | if (commonEncodings.size() > COMMON_ENCODINGS_MAX) |
537 | commonEncodings.resize(COMMON_ENCODINGS_MAX); |
538 | |
539 | // Create a map from encoding to common-encoding-table index |
540 | for (size_t i = 0; i < commonEncodings.size(); i++) |
541 | commonEncodingIndexes[commonEncodings[i].first] = i; |
542 | |
543 | // Split folded encodings into pages, where each page is limited by ... |
544 | // (a) 4 KiB capacity |
545 | // (b) 24-bit difference between first & final function address |
546 | // (c) 8-bit compact-encoding-table index, |
547 | // for which 0..126 references the global common-encodings table, |
548 | // and 127..255 references a local per-second-level-page table. |
549 | // First we try the compact format and determine how many entries fit. |
550 | // If more entries fit in the regular format, we use that. |
551 | for (size_t i = 0; i < cuIndices.size();) { |
552 | size_t idx = cuIndices[i]; |
553 | secondLevelPages.emplace_back(); |
554 | SecondLevelPage &page = secondLevelPages.back(); |
555 | page.entryIndex = i; |
556 | uint64_t functionAddressMax = |
557 | cuEntries[idx].functionAddress + COMPRESSED_ENTRY_FUNC_OFFSET_MASK; |
558 | size_t n = commonEncodings.size(); |
559 | size_t wordsRemaining = |
560 | SECOND_LEVEL_PAGE_WORDS - |
561 | sizeof(unwind_info_compressed_second_level_page_header) / |
562 | sizeof(uint32_t); |
563 | while (wordsRemaining >= 1 && i < cuIndices.size()) { |
564 | idx = cuIndices[i]; |
565 | const CompactUnwindEntry *cuPtr = &cuEntries[idx]; |
566 | if (cuPtr->functionAddress >= functionAddressMax) |
567 | break; |
568 | if (commonEncodingIndexes.count(Val: cuPtr->encoding) || |
569 | page.localEncodingIndexes.count(Val: cuPtr->encoding)) { |
570 | i++; |
571 | wordsRemaining--; |
572 | } else if (wordsRemaining >= 2 && n < COMPACT_ENCODINGS_MAX) { |
573 | page.localEncodings.emplace_back(args: cuPtr->encoding); |
574 | page.localEncodingIndexes[cuPtr->encoding] = n++; |
575 | i++; |
576 | wordsRemaining -= 2; |
577 | } else { |
578 | break; |
579 | } |
580 | } |
581 | page.entryCount = i - page.entryIndex; |
582 | |
583 | // If this is not the final page, see if it's possible to fit more entries |
584 | // by using the regular format. This can happen when there are many unique |
585 | // encodings, and we saturated the local encoding table early. |
586 | if (i < cuIndices.size() && |
587 | page.entryCount < REGULAR_SECOND_LEVEL_ENTRIES_MAX) { |
588 | page.kind = UNWIND_SECOND_LEVEL_REGULAR; |
589 | page.entryCount = std::min(REGULAR_SECOND_LEVEL_ENTRIES_MAX, |
590 | b: cuIndices.size() - page.entryIndex); |
591 | i = page.entryIndex + page.entryCount; |
592 | } else { |
593 | page.kind = UNWIND_SECOND_LEVEL_COMPRESSED; |
594 | } |
595 | } |
596 | |
597 | for (size_t idx : cuIndices) { |
598 | lsdaIndex[idx] = entriesWithLsda.size(); |
599 | if (cuEntries[idx].lsda) |
600 | entriesWithLsda.push_back(x: idx); |
601 | } |
602 | |
603 | // compute size of __TEXT,__unwind_info section |
604 | level2PagesOffset = sizeof(unwind_info_section_header) + |
605 | commonEncodings.size() * sizeof(uint32_t) + |
606 | personalities.size() * sizeof(uint32_t) + |
607 | // The extra second-level-page entry is for the sentinel |
608 | (secondLevelPages.size() + 1) * |
609 | sizeof(unwind_info_section_header_index_entry) + |
610 | entriesWithLsda.size() * |
611 | sizeof(unwind_info_section_header_lsda_index_entry); |
612 | unwindInfoSize = |
613 | level2PagesOffset + secondLevelPages.size() * SECOND_LEVEL_PAGE_BYTES; |
614 | } |
615 | |
616 | // All inputs are relocated and output addresses are known, so write! |
617 | |
618 | void UnwindInfoSectionImpl::writeTo(uint8_t *buf) const { |
619 | assert(!cuIndices.empty() && "call only if there is unwind info" ); |
620 | |
621 | // section header |
622 | auto *uip = reinterpret_cast<unwind_info_section_header *>(buf); |
623 | uip->version = 1; |
624 | uip->commonEncodingsArraySectionOffset = sizeof(unwind_info_section_header); |
625 | uip->commonEncodingsArrayCount = commonEncodings.size(); |
626 | uip->personalityArraySectionOffset = |
627 | uip->commonEncodingsArraySectionOffset + |
628 | (uip->commonEncodingsArrayCount * sizeof(uint32_t)); |
629 | uip->personalityArrayCount = personalities.size(); |
630 | uip->indexSectionOffset = uip->personalityArraySectionOffset + |
631 | (uip->personalityArrayCount * sizeof(uint32_t)); |
632 | uip->indexCount = secondLevelPages.size() + 1; |
633 | |
634 | // Common encodings |
635 | auto *i32p = reinterpret_cast<uint32_t *>(&uip[1]); |
636 | for (const auto &encoding : commonEncodings) |
637 | *i32p++ = encoding.first; |
638 | |
639 | // Personalities |
640 | for (const Symbol *personality : personalities) |
641 | *i32p++ = personality->getGotVA() - in.header->addr; |
642 | |
643 | // FIXME: LD64 checks and warns aboutgaps or overlapse in cuEntries address |
644 | // ranges. We should do the same too |
645 | |
646 | // Level-1 index |
647 | uint32_t lsdaOffset = |
648 | uip->indexSectionOffset + |
649 | uip->indexCount * sizeof(unwind_info_section_header_index_entry); |
650 | uint64_t l2PagesOffset = level2PagesOffset; |
651 | auto *iep = reinterpret_cast<unwind_info_section_header_index_entry *>(i32p); |
652 | for (const SecondLevelPage &page : secondLevelPages) { |
653 | size_t idx = cuIndices[page.entryIndex]; |
654 | iep->functionOffset = cuEntries[idx].functionAddress - in.header->addr; |
655 | iep->secondLevelPagesSectionOffset = l2PagesOffset; |
656 | iep->lsdaIndexArraySectionOffset = |
657 | lsdaOffset + lsdaIndex.lookup(Val: idx) * |
658 | sizeof(unwind_info_section_header_lsda_index_entry); |
659 | iep++; |
660 | l2PagesOffset += SECOND_LEVEL_PAGE_BYTES; |
661 | } |
662 | // Level-1 sentinel |
663 | // XXX(vyng): Note that LD64 adds +1 here. |
664 | // Unsure whether it's a bug or it's their workaround for something else. |
665 | // See comments from https://reviews.llvm.org/D138320. |
666 | iep->functionOffset = cueEndBoundary - in.header->addr; |
667 | iep->secondLevelPagesSectionOffset = 0; |
668 | iep->lsdaIndexArraySectionOffset = |
669 | lsdaOffset + entriesWithLsda.size() * |
670 | sizeof(unwind_info_section_header_lsda_index_entry); |
671 | iep++; |
672 | |
673 | // LSDAs |
674 | auto *lep = |
675 | reinterpret_cast<unwind_info_section_header_lsda_index_entry *>(iep); |
676 | for (size_t idx : entriesWithLsda) { |
677 | const CompactUnwindEntry &cu = cuEntries[idx]; |
678 | lep->lsdaOffset = cu.lsda->getVA(/*off=*/0) - in.header->addr; |
679 | lep->functionOffset = cu.functionAddress - in.header->addr; |
680 | lep++; |
681 | } |
682 | |
683 | // Level-2 pages |
684 | auto *pp = reinterpret_cast<uint32_t *>(lep); |
685 | for (const SecondLevelPage &page : secondLevelPages) { |
686 | if (page.kind == UNWIND_SECOND_LEVEL_COMPRESSED) { |
687 | uintptr_t functionAddressBase = |
688 | cuEntries[cuIndices[page.entryIndex]].functionAddress; |
689 | auto *p2p = |
690 | reinterpret_cast<unwind_info_compressed_second_level_page_header *>( |
691 | pp); |
692 | p2p->kind = page.kind; |
693 | p2p->entryPageOffset = |
694 | sizeof(unwind_info_compressed_second_level_page_header); |
695 | p2p->entryCount = page.entryCount; |
696 | p2p->encodingsPageOffset = |
697 | p2p->entryPageOffset + p2p->entryCount * sizeof(uint32_t); |
698 | p2p->encodingsCount = page.localEncodings.size(); |
699 | auto *ep = reinterpret_cast<uint32_t *>(&p2p[1]); |
700 | for (size_t i = 0; i < page.entryCount; i++) { |
701 | const CompactUnwindEntry &cue = |
702 | cuEntries[cuIndices[page.entryIndex + i]]; |
703 | auto it = commonEncodingIndexes.find(Val: cue.encoding); |
704 | if (it == commonEncodingIndexes.end()) |
705 | it = page.localEncodingIndexes.find(Val: cue.encoding); |
706 | *ep++ = (it->second << COMPRESSED_ENTRY_FUNC_OFFSET_BITS) | |
707 | (cue.functionAddress - functionAddressBase); |
708 | } |
709 | if (!page.localEncodings.empty()) |
710 | memcpy(dest: ep, src: page.localEncodings.data(), |
711 | n: page.localEncodings.size() * sizeof(uint32_t)); |
712 | } else { |
713 | auto *p2p = |
714 | reinterpret_cast<unwind_info_regular_second_level_page_header *>(pp); |
715 | p2p->kind = page.kind; |
716 | p2p->entryPageOffset = |
717 | sizeof(unwind_info_regular_second_level_page_header); |
718 | p2p->entryCount = page.entryCount; |
719 | auto *ep = reinterpret_cast<uint32_t *>(&p2p[1]); |
720 | for (size_t i = 0; i < page.entryCount; i++) { |
721 | const CompactUnwindEntry &cue = |
722 | cuEntries[cuIndices[page.entryIndex + i]]; |
723 | *ep++ = cue.functionAddress; |
724 | *ep++ = cue.encoding; |
725 | } |
726 | } |
727 | pp += SECOND_LEVEL_PAGE_WORDS; |
728 | } |
729 | } |
730 | |
731 | UnwindInfoSection *macho::makeUnwindInfoSection() { |
732 | return make<UnwindInfoSectionImpl>(); |
733 | } |
734 | |