blob: 1466e7b4fb394f5081fe42825617e32ef50575db [file] [log] [blame]
Greg McGary2124ca12020-08-20 20:05:131//===- 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"
Greg McGary2124ca12020-08-20 20:05:1310#include "InputSection.h"
Jez Ng453102a2023-02-16 21:18:4611#include "Layout.h"
Greg McGary2124ca12020-08-20 20:05:1312#include "OutputSection.h"
13#include "OutputSegment.h"
Jez Ng525bfa12021-02-08 18:47:3314#include "SymbolTable.h"
Greg McGary2124ca12020-08-20 20:05:1315#include "Symbols.h"
16#include "SyntheticSections.h"
17#include "Target.h"
18
19#include "lld/Common/ErrorHandler.h"
Jez Ng525bfa12021-02-08 18:47:3320#include "lld/Common/Memory.h"
Jez Nga2404f12021-11-11 00:31:5421#include "llvm/ADT/DenseMap.h"
Jez Ng7ca133c2021-04-26 05:23:3222#include "llvm/ADT/STLExtras.h"
Greg McGary2124ca12020-08-20 20:05:1323#include "llvm/BinaryFormat/MachO.h"
Jez Nga9353db2021-10-26 20:04:0624#include "llvm/Support/Parallel.h"
Greg McGary2124ca12020-08-20 20:05:1325
Vy Nguyenfc7a7182022-10-19 16:45:4926#include "mach-o/compact_unwind_encoding.h"
27
Jez Nga2404f12021-11-11 00:31:5428#include <numeric>
29
Greg McGary2124ca12020-08-20 20:05:1330using namespace llvm;
31using namespace llvm::MachO;
Jez Nge183bf82022-06-13 01:56:4532using namespace llvm::support::endian;
Greg McGary2124ca12020-08-20 20:05:1333using namespace lld;
34using namespace lld::macho;
35
Greg McGary99930712020-12-07 06:33:3836#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
Jez Ngf7bc79c2023-04-04 13:26:0354static_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
59constexpr uint64_t DWARF_SECTION_OFFSET = UNWIND_X86_64_DWARF_SECTION_OFFSET;
60
Greg McGary2124ca12020-08-20 20:05:1361// 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//
Jez Ng525bfa12021-02-08 18:47:3393// 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.
Greg McGary2124ca12020-08-20 20:05:1396
Greg McGary2124ca12020-08-20 20:05:1397// TODO(gkm): how do we align the 2nd-level pages?
98
Jez Ng453102a2023-02-16 21:18:4699// 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)
Jez Ng2a666902022-04-13 20:17:29107
Jez Ng453102a2023-02-16 21:18:46108CREATE_LAYOUT_CLASS(CompactUnwind, FOR_EACH_CU_FIELD);
Jez Ng2a666902022-04-13 20:17:29109
Jez Ng453102a2023-02-16 21:18:46110#undef FOR_EACH_CU_FIELD
Jez Nga9353db2021-10-26 20:04:06111
Jez Ng2a666902022-04-13 20:17:29112// LLD's internal representation of a compact unwind entry.
Jez Ng82dcf302022-04-09 02:33:00113struct CompactUnwindEntry {
114 uint64_t functionAddress;
115 uint32_t functionLength;
116 compact_unwind_encoding_t encoding;
117 Symbol *personality;
118 InputSection *lsda;
119};
120
Jez Ng28a21022021-07-11 22:35:45121using EncodingMap = DenseMap<compact_unwind_encoding_t, size_t>;
Greg McGary2124ca12020-08-20 20:05:13122
Jez Ng14609422021-04-16 01:14:33123struct 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
Jez Ng2a666902022-04-13 20:17:29132// UnwindInfoSectionImpl allows us to avoid cluttering our header file with a
133// lengthy definition of UnwindInfoSection.
Jez Ng3a115282021-07-02 00:33:42134class UnwindInfoSectionImpl final : public UnwindInfoSection {
Jez Ng14609422021-04-16 01:14:33135public:
Jez Ng453102a2023-02-16 21:18:46136 UnwindInfoSectionImpl() : cuLayout(target->wordSize) {}
Jez Ng2a666902022-04-13 20:17:29137 uint64_t getSize() const override { return unwindInfoSize; }
Jez Ng7b45dfc2022-10-12 03:50:46138 void prepare() override;
Jez Ng14609422021-04-16 01:14:33139 void finalize() override;
140 void writeTo(uint8_t *buf) const override;
141
142private:
Jez Ng2a666902022-04-13 20:17:29143 void prepareRelocations(ConcatInputSection *);
144 void relocateCompactUnwind(std::vector<CompactUnwindEntry> &);
145 void encodePersonalities();
Jez Ng7b45dfc2022-10-12 03:50:46146 Symbol *canonicalizePersonality(Symbol *);
Jez Ng2a666902022-04-13 20:17:29147
148 uint64_t unwindInfoSize = 0;
Fangrui Songfb2a9712023-07-25 05:04:03149 SmallVector<decltype(symbols)::value_type, 0> symbolsVec;
Jez Ng453102a2023-02-16 21:18:46150 CompactUnwindLayout cuLayout;
Jez Ng14609422021-04-16 01:14:33151 std::vector<std::pair<compact_unwind_encoding_t, size_t>> commonEncodings;
152 EncodingMap commonEncodingIndexes;
Jez Nga2404f12021-11-11 00:31:54153 // The entries here will be in the same order as their originating symbols
154 // in symbolsVec.
Jez Ng82dcf302022-04-09 02:33:00155 std::vector<CompactUnwindEntry> cuEntries;
Jez Nga2404f12021-11-11 00:31:54156 // Indices into the cuEntries vector.
157 std::vector<size_t> cuIndices;
Jez Ng82dcf302022-04-09 02:33:00158 std::vector<Symbol *> personalities;
Jez Ng14609422021-04-16 01:14:33159 SmallDenseMap<std::pair<InputSection *, uint64_t /* addend */>, Symbol *>
160 personalityTable;
Jez Nga2404f12021-11-11 00:31:54161 // 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;
Jez Ng14609422021-04-16 01:14:33165 std::vector<SecondLevelPage> secondLevelPages;
166 uint64_t level2PagesOffset = 0;
Vy Nguyen65226d32022-11-18 20:21:23167 // 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;
Jez Ng14609422021-04-16 01:14:33170};
Jez Ng4a5e1112021-02-24 02:42:02171
Jez Ng3a115282021-07-02 00:33:42172UnwindInfoSection::UnwindInfoSection()
173 : SyntheticSection(segment_names::text, section_names::unwindInfo) {
174 align = 4;
Jez Ng3a115282021-07-02 00:33:42175}
176
Jez Nga9353db2021-10-26 20:04:06177// Record function symbols that may need entries emitted in __unwind_info, which
178// stores unwind data for address ranges.
179//
Shoaib Meenai56bd3182022-08-28 20:09:56180// 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.
Jez Nga9353db2021-10-26 20:04:06187void UnwindInfoSection::addSymbol(const Defined *d) {
Greg McGary9cc489a2021-11-15 18:46:59188 if (d->unwindEntry)
Jez Nga9353db2021-10-26 20:04:06189 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({{d->isec, d->value}, d});
194 // If we have multiple symbols at the same address, only one of them can have
Jez Ng241f62d2022-07-21 13:44:01195 // an associated unwind entry.
Greg McGary9cc489a2021-11-15 18:46:59196 if (!p.second && d->unwindEntry) {
Jez Ng7f60ed12022-12-21 22:44:45197 assert(p.first->second == d || !p.first->second->unwindEntry);
Jez Nga9353db2021-10-26 20:04:06198 p.first->second = d;
Jez Ng002eda72021-10-26 20:04:04199 }
Jez Ng3a115282021-07-02 00:33:42200}
201
Jez Ng7b45dfc2022-10-12 03:50:46202void UnwindInfoSectionImpl::prepare() {
Jez Ng2a666902022-04-13 20:17:29203 // 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(symbols))
Jez Ng7b45dfc2022-10-12 03:50:46207 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>(d->getFile())->fdes[d->unwindEntry];
217 fde.personality = canonicalizePersonality(fde.personality);
218 }
219 }
Jez Ng2a666902022-04-13 20:17:29220}
221
Jez Ng525bfa12021-02-08 18:47:33222// 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.
Jez Ng2a666902022-04-13 20:17:29227void UnwindInfoSectionImpl::prepareRelocations(ConcatInputSection *isec) {
Nico Weberd5a70db2021-05-06 18:47:57228 assert(!isec->shouldOmitFromOutput() &&
229 "__compact_unwind section should not be omitted");
Jez Ng525bfa12021-02-08 18:47:33230
Nico Weber7d4c8a22021-06-13 17:30:05231 // FIXME: Make this skip relocations for CompactUnwindEntries that
Nico Webera5645512021-05-07 21:10:05232 // 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.
Greg McGaryf27e4542021-05-19 16:58:17236 for (size_t i = 0; i < isec->relocs.size(); ++i) {
237 Reloc &r = isec->relocs[i];
Jez Ng4a5e1112021-02-24 02:42:02238 assert(target->hasAttr(r.type, RelocAttrBits::UNSIGNED));
Jez Ng04b1dad2022-12-05 21:18:15239 // 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();
Nico Weber8a7b5eb2021-07-07 15:28:27244
Greg McGary9cc489a2021-11-15 18:46:59245 // 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.
Jez Ng453102a2023-02-16 21:18:46249 if (r.offset != cuLayout.personalityOffset)
Jez Ng525bfa12021-02-08 18:47:33250 continue;
251
Greg McGary427d3592021-03-30 00:19:29252 if (auto *s = r.referent.dyn_cast<Symbol *>()) {
Greg McGary9cc489a2021-11-15 18:46:59253 // 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.
Vy Nguyenb428c3e2021-09-15 19:49:56257 if (auto *defined = dyn_cast<Defined>(s)) {
Fangrui Song640d9b32022-11-09 01:28:04258 // XXX(vyng) This is a special case for handling duplicate personality
Vy Nguyenb428c3e2021-09-15 19:49:56259 // 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())
Vy Nguyen944071e2021-11-19 15:56:58267 if (Symbol *sym = symtab->find(defined->getName()))
Fangrui Song0aae2bf2022-01-19 18:14:49268 if (!sym->isLazy())
Vy Nguyen944071e2021-11-19 15:56:58269 r.referent = s = sym;
Vy Nguyenb428c3e2021-09-15 19:49:56270 }
Jez Ng4a5e1112021-02-24 02:42:02271 if (auto *undefined = dyn_cast<Undefined>(s)) {
Daniel Bertalanf2e92cf2022-06-14 13:41:28272 treatUndefinedSymbol(*undefined, isec, r.offset);
Nico Weber0658fc62021-02-28 18:42:14273 // treatUndefinedSymbol() can replace s with a DylibSymbol; re-check.
274 if (isa<Undefined>(s))
275 continue;
Jez Ng4a5e1112021-02-24 02:42:02276 }
Vy Nguyenb428c3e2021-09-15 19:49:56277
Jez Ng7b45dfc2022-10-12 03:50:46278 // Similar to canonicalizePersonality(), but we also register a GOT entry.
Jez Ng4a5e1112021-02-24 02:42:02279 if (auto *defined = dyn_cast<Defined>(s)) {
280 // Check if we have created a synthetic symbol at the same address.
Greg McGary427d3592021-03-30 00:19:29281 Symbol *&personality =
Jez Ng4a5e1112021-02-24 02:42:02282 personalityTable[{defined->isec, defined->value}];
283 if (personality == nullptr) {
284 personality = defined;
285 in.got->addEntry(defined);
286 } else if (personality != defined) {
287 r.referent = personality;
288 }
289 continue;
290 }
Jez Ng7b45dfc2022-10-12 03:50:46291
Jez Ng4a5e1112021-02-24 02:42:02292 assert(isa<DylibSymbol>(s));
293 in.got->addEntry(s);
294 continue;
295 }
296
297 if (auto *referentIsec = r.referent.dyn_cast<InputSection *>()) {
Jez Ngb8bbb972021-06-16 19:23:04298 assert(!isCoalescedWeak(referentIsec));
Jez Ng525bfa12021-02-08 18:47:33299 // Personality functions can be referenced via section relocations
Jez Ng4a5e1112021-02-24 02:42:02300 // if they live in the same object file. Create placeholder synthetic
301 // symbols for them in the GOT.
Greg McGary427d3592021-03-30 00:19:29302 Symbol *&s = personalityTable[{referentIsec, r.addend}];
Jez Ng525bfa12021-02-08 18:47:33303 if (s == nullptr) {
Nico Webera5645512021-05-07 21:10:05304 // This runs after dead stripping, so the noDeadStrip argument does not
305 // matter.
Nico Weberc1b2a7b2021-04-22 14:44:56306 s = make<Defined>("<internal>", /*file=*/nullptr, referentIsec,
307 r.addend, /*size=*/0, /*isWeakDef=*/false,
Jez Ng05c53632021-04-30 20:17:26308 /*isExternal=*/false, /*isPrivateExtern=*/false,
Jez Ng1cff7232022-04-11 19:45:25309 /*includeInSymtab=*/true,
Vincent Leeed59b8a2023-05-15 09:00:29310 /*isReferencedDynamically=*/false,
Nico Webera5645512021-05-07 21:10:05311 /*noDeadStrip=*/false);
Alex Brachet190b0f42022-05-20 21:39:16312 s->used = true;
Jez Ng525bfa12021-02-08 18:47:33313 in.got->addEntry(s);
314 }
315 r.referent = s;
316 r.addend = 0;
317 }
318 }
319}
320
Jez Ng7b45dfc2022-10-12 03:50:46321Symbol *UnwindInfoSectionImpl::canonicalizePersonality(Symbol *personality) {
322 if (auto *defined = dyn_cast_or_null<Defined>(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
Jez Ng525bfa12021-02-08 18:47:33333// 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.
Jez Ng2a666902022-04-13 20:17:29337void UnwindInfoSectionImpl::relocateCompactUnwind(
Jez Ng82dcf302022-04-09 02:33:00338 std::vector<CompactUnwindEntry> &cuEntries) {
Nico Weber7effcbd2022-06-19 16:30:06339 parallelFor(0, symbolsVec.size(), [&](size_t i) {
Jez Ng82dcf302022-04-09 02:33:00340 CompactUnwindEntry &cu = cuEntries[i];
Jez Nga9353db2021-10-26 20:04:06341 const Defined *d = symbolsVec[i].second;
Jez Ng82dcf302022-04-09 02:33:00342 cu.functionAddress = d->getVA();
Greg McGary9cc489a2021-11-15 18:46:59343 if (!d->unwindEntry)
Jez Nga9353db2021-10-26 20:04:06344 return;
Nico Weberd5a70db2021-05-06 18:47:57345
Vy Nguyene60b30d2023-06-06 18:00:47346 // If we have DWARF unwind info, create a slimmed-down CU entry that points
347 // to it.
Jez Nge183bf82022-06-13 01:56:45348 if (d->unwindEntry->getName() == section_names::ehFrame) {
Jez Ngf7bc79c2023-04-04 13:26:03349 // 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;
Jez Nge183bf82022-06-13 01:56:45362 const FDE &fde = cast<ObjFile>(d->getFile())->fdes[d->unwindEntry];
363 cu.functionLength = fde.funcLength;
Vy Nguyene60b30d2023-06-06 18:00:47364 // Omit the DWARF personality from compact-unwind entry so that we
365 // don't need to encode it.
366 cu.personality = nullptr;
Jez Nge183bf82022-06-13 01:56:45367 cu.lsda = fde.lsda;
368 return;
369 }
370
371 assert(d->unwindEntry->getName() == section_names::compactUnwind);
372
Jez Ng82dcf302022-04-09 02:33:00373 auto buf = reinterpret_cast<const uint8_t *>(d->unwindEntry->data.data()) -
Jez Ng2a666902022-04-13 20:17:29374 target->wordSize;
375 cu.functionLength =
Jez Ng453102a2023-02-16 21:18:46376 support::endian::read32le(buf + cuLayout.functionLengthOffset);
377 cu.encoding = support::endian::read32le(buf + cuLayout.encodingOffset);
Greg McGary9cc489a2021-11-15 18:46:59378 for (const Reloc &r : d->unwindEntry->relocs) {
Jez Ng453102a2023-02-16 21:18:46379 if (r.offset == cuLayout.personalityOffset)
Jez Ng82dcf302022-04-09 02:33:00380 cu.personality = r.referent.get<Symbol *>();
Jez Ng453102a2023-02-16 21:18:46381 else if (r.offset == cuLayout.lsdaOffset)
382 cu.lsda = r.getReferentInputSection();
Jez Ng525bfa12021-02-08 18:47:33383 }
Jez Nga9353db2021-10-26 20:04:06384 });
Jez Ng525bfa12021-02-08 18:47:33385}
386
387// There should only be a handful of unique personality pointers, so we can
388// encode them as 2-bit indices into a small array.
Jez Ng2a666902022-04-13 20:17:29389void UnwindInfoSectionImpl::encodePersonalities() {
Jez Nga2404f12021-11-11 00:31:54390 for (size_t idx : cuIndices) {
Jez Ng82dcf302022-04-09 02:33:00391 CompactUnwindEntry &cu = cuEntries[idx];
392 if (cu.personality == nullptr)
Jez Ng525bfa12021-02-08 18:47:33393 continue;
Jez Ng525bfa12021-02-08 18:47:33394 // Linear search is fast enough for a small array.
Jez Nga2404f12021-11-11 00:31:54395 auto it = find(personalities, cu.personality);
Jez Ng525bfa12021-02-08 18:47:33396 uint32_t personalityIndex; // 1-based index
397 if (it != personalities.end()) {
398 personalityIndex = std::distance(personalities.begin(), it) + 1;
399 } else {
Jez Nga2404f12021-11-11 00:31:54400 personalities.push_back(cu.personality);
Jez Ng525bfa12021-02-08 18:47:33401 personalityIndex = personalities.size();
402 }
Jez Nga2404f12021-11-11 00:31:54403 cu.encoding |=
Kazu Hirata55e2cd12023-01-28 20:41:19404 personalityIndex << llvm::countr_zero(
Jez Ng525bfa12021-02-08 18:47:33405 static_cast<compact_unwind_encoding_t>(UNWIND_PERSONALITY_MASK));
406 }
407 if (personalities.size() > 3)
Jez Ng82dcf302022-04-09 02:33:00408 error("too many personalities (" + Twine(personalities.size()) +
Jez Ng525bfa12021-02-08 18:47:33409 ") for compact unwind to encode");
410}
411
Nico Weber0f24ffc2021-06-26 02:50:46412static bool canFoldEncoding(compact_unwind_encoding_t encoding) {
413 // From compact_unwind_encoding.h:
414 // UNWIND_X86_64_MODE_STACK_IND:
415 // A "frameless" (RBP not used as frame pointer) function large constant
416 // stack size. This case is like the previous, except the stack size is too
417 // large to encode in the compact unwind encoding. Instead it requires that
418 // the function contains "subq $nnnnnnnn,RSP" in its prolog. The compact
419 // encoding contains the offset to the nnnnnnnn value in the function in
420 // UNWIND_X86_64_FRAMELESS_STACK_SIZE.
421 // Since this means the unwinder has to look at the `subq` in the function
422 // of the unwind info's unwind address, two functions that have identical
423 // unwind info can't be folded if it's using this encoding since both
424 // entries need unique addresses.
Martin Storsjö59c6f412022-08-02 07:29:01425 static_assert(static_cast<uint32_t>(UNWIND_X86_64_MODE_STACK_IND) ==
Kazu Hirata32aa35b2022-09-03 18:17:47426 static_cast<uint32_t>(UNWIND_X86_MODE_STACK_IND));
Nico Weber0f24ffc2021-06-26 02:50:46427 if ((target->cpuType == CPU_TYPE_X86_64 || target->cpuType == CPU_TYPE_X86) &&
Vy Nguyena6d67342022-10-06 13:08:00428 (encoding & UNWIND_MODE_MASK) == UNWIND_X86_64_MODE_STACK_IND) {
Nico Weber0f24ffc2021-06-26 02:50:46429 // FIXME: Consider passing in the two function addresses and getting
430 // their two stack sizes off the `subq` and only returning false if they're
431 // actually different.
432 return false;
433 }
434 return true;
435}
436
Greg McGary2124ca12020-08-20 20:05:13437// Scan the __LD,__compact_unwind entries and compute the space needs of
Jez Ng3e951802022-02-01 18:45:38438// __TEXT,__unwind_info and __TEXT,__eh_frame.
Jez Ng2a666902022-04-13 20:17:29439void UnwindInfoSectionImpl::finalize() {
Jez Nga9353db2021-10-26 20:04:06440 if (symbols.empty())
Greg McGary2124ca12020-08-20 20:05:13441 return;
442
443 // At this point, the address space for __TEXT,__text has been
444 // assigned, so we can relocate the __LD,__compact_unwind entries
445 // into a temporary buffer. Relocation is necessary in order to sort
446 // the CU entries by function address. Sorting is necessary so that
Shoaib Meenai56bd3182022-08-28 20:09:56447 // we can fold adjacent CU entries with identical encoding+personality
448 // and without any LSDA. Folding is necessary because it reduces the
449 // number of CU entries by as much as 3 orders of magnitude!
Jez Nga2404f12021-11-11 00:31:54450 cuEntries.resize(symbols.size());
451 // The "map" part of the symbols MapVector was only needed for deduplication
452 // in addSymbol(). Now that we are done adding, move the contents to a plain
453 // std::vector for indexed access.
454 symbolsVec = symbols.takeVector();
455 relocateCompactUnwind(cuEntries);
Nico Weberd6565a22021-06-22 02:29:11456
Greg McGary2124ca12020-08-20 20:05:13457 // Rather than sort & fold the 32-byte entries directly, we create a
Jez Nga2404f12021-11-11 00:31:54458 // vector of indices to entries and sort & fold that instead.
459 cuIndices.resize(cuEntries.size());
460 std::iota(cuIndices.begin(), cuIndices.end(), 0);
461 llvm::sort(cuIndices, [&](size_t a, size_t b) {
462 return cuEntries[a].functionAddress < cuEntries[b].functionAddress;
Jez Ng7ca133c2021-04-26 05:23:32463 });
Greg McGary2124ca12020-08-20 20:05:13464
Vy Nguyen65226d32022-11-18 20:21:23465 // Record the ending boundary before we fold the entries.
466 cueEndBoundary = cuEntries[cuIndices.back()].functionAddress +
467 cuEntries[cuIndices.back()].functionLength;
468
Shoaib Meenai56bd3182022-08-28 20:09:56469 // Fold adjacent entries with matching encoding+personality and without LSDA
Jez Nga2404f12021-11-11 00:31:54470 // We use three iterators on the same cuIndices to fold in-situ:
Greg McGary2124ca12020-08-20 20:05:13471 // (1) `foldBegin` is the first of a potential sequence of matching entries
472 // (2) `foldEnd` is the first non-matching entry after `foldBegin`.
473 // The semi-open interval [ foldBegin .. foldEnd ) contains a range
474 // entries that can be folded into a single entry and written to ...
475 // (3) `foldWrite`
Jez Nga2404f12021-11-11 00:31:54476 auto foldWrite = cuIndices.begin();
477 for (auto foldBegin = cuIndices.begin(); foldBegin < cuIndices.end();) {
Greg McGary2124ca12020-08-20 20:05:13478 auto foldEnd = foldBegin;
Shoaib Meenai56bd3182022-08-28 20:09:56479 // Common LSDA encodings (e.g. for C++ and Objective-C) contain offsets from
480 // a base address. The base address is normally not contained directly in
481 // the LSDA, and in that case, the personality function treats the starting
482 // address of the function (which is computed by the unwinder) as the base
483 // address and interprets the LSDA accordingly. The unwinder computes the
484 // starting address of a function as the address associated with its CU
485 // entry. For this reason, we cannot fold adjacent entries if they have an
486 // LSDA, because folding would make the unwinder compute the wrong starting
487 // address for the functions with the folded entries, which in turn would
488 // cause the personality function to misinterpret the LSDA for those
489 // functions. In the very rare case where the base address is encoded
490 // directly in the LSDA, two functions at different addresses would
491 // necessarily have different LSDAs, so their CU entries would not have been
492 // folded anyway.
Jez Nga2404f12021-11-11 00:31:54493 while (++foldEnd < cuIndices.end() &&
494 cuEntries[*foldBegin].encoding == cuEntries[*foldEnd].encoding &&
Shoaib Meenai56bd3182022-08-28 20:09:56495 !cuEntries[*foldBegin].lsda && !cuEntries[*foldEnd].lsda &&
496 // If we've gotten to this point, we don't have an LSDA, which should
497 // also imply that we don't have a personality function, since in all
498 // likelihood a personality function needs the LSDA to do anything
499 // useful. It can be technically valid to have a personality function
500 // and no LSDA though (e.g. the C++ personality __gxx_personality_v0
501 // is just a no-op without LSDA), so we still check for personality
502 // function equivalence to handle that case.
Jez Nga2404f12021-11-11 00:31:54503 cuEntries[*foldBegin].personality ==
504 cuEntries[*foldEnd].personality &&
Jez Ng82dcf302022-04-09 02:33:00505 canFoldEncoding(cuEntries[*foldEnd].encoding))
506 ;
Greg McGary2124ca12020-08-20 20:05:13507 *foldWrite++ = *foldBegin;
508 foldBegin = foldEnd;
509 }
Jez Nga2404f12021-11-11 00:31:54510 cuIndices.erase(foldWrite, cuIndices.end());
Greg McGary2124ca12020-08-20 20:05:13511
Jez Nga2404f12021-11-11 00:31:54512 encodePersonalities();
Jez Ng525bfa12021-02-08 18:47:33513
Greg McGary2124ca12020-08-20 20:05:13514 // Count frequencies of the folded encodings
Greg McGary99930712020-12-07 06:33:38515 EncodingMap encodingFrequencies;
Jez Nga2404f12021-11-11 00:31:54516 for (size_t idx : cuIndices)
517 encodingFrequencies[cuEntries[idx].encoding]++;
Greg McGary2124ca12020-08-20 20:05:13518
Greg McGary99930712020-12-07 06:33:38519 // Make a vector of encodings, sorted by descending frequency
Greg McGary2124ca12020-08-20 20:05:13520 for (const auto &frequency : encodingFrequencies)
521 commonEncodings.emplace_back(frequency);
Jez Ng7ca133c2021-04-26 05:23:32522 llvm::sort(commonEncodings,
523 [](const std::pair<compact_unwind_encoding_t, size_t> &a,
524 const std::pair<compact_unwind_encoding_t, size_t> &b) {
525 if (a.second == b.second)
526 // When frequencies match, secondarily sort on encoding
527 // to maintain parity with validate-unwind-info.py
528 return a.first > b.first;
529 return a.second > b.second;
530 });
Greg McGary2124ca12020-08-20 20:05:13531
Greg McGary99930712020-12-07 06:33:38532 // Truncate the vector to 127 elements.
Nico Weber56882472021-01-02 03:28:11533 // Common encoding indexes are limited to 0..126, while encoding
Greg McGary99930712020-12-07 06:33:38534 // indexes 127..255 are local to each second-level page
535 if (commonEncodings.size() > COMMON_ENCODINGS_MAX)
536 commonEncodings.resize(COMMON_ENCODINGS_MAX);
537
538 // Create a map from encoding to common-encoding-table index
539 for (size_t i = 0; i < commonEncodings.size(); i++)
540 commonEncodingIndexes[commonEncodings[i].first] = i;
541
542 // Split folded encodings into pages, where each page is limited by ...
543 // (a) 4 KiB capacity
544 // (b) 24-bit difference between first & final function address
545 // (c) 8-bit compact-encoding-table index,
546 // for which 0..126 references the global common-encodings table,
547 // and 127..255 references a local per-second-level-page table.
548 // First we try the compact format and determine how many entries fit.
549 // If more entries fit in the regular format, we use that.
Jez Nga2404f12021-11-11 00:31:54550 for (size_t i = 0; i < cuIndices.size();) {
551 size_t idx = cuIndices[i];
Greg McGary99930712020-12-07 06:33:38552 secondLevelPages.emplace_back();
Jez Ng14609422021-04-16 01:14:33553 SecondLevelPage &page = secondLevelPages.back();
Greg McGary99930712020-12-07 06:33:38554 page.entryIndex = i;
David Spickett79942d32022-07-08 11:32:44555 uint64_t functionAddressMax =
Jez Nga2404f12021-11-11 00:31:54556 cuEntries[idx].functionAddress + COMPRESSED_ENTRY_FUNC_OFFSET_MASK;
Greg McGary99930712020-12-07 06:33:38557 size_t n = commonEncodings.size();
558 size_t wordsRemaining =
559 SECOND_LEVEL_PAGE_WORDS -
560 sizeof(unwind_info_compressed_second_level_page_header) /
561 sizeof(uint32_t);
Jez Nga2404f12021-11-11 00:31:54562 while (wordsRemaining >= 1 && i < cuIndices.size()) {
563 idx = cuIndices[i];
Jez Ng82dcf302022-04-09 02:33:00564 const CompactUnwindEntry *cuPtr = &cuEntries[idx];
Jez Ngc4d9df92023-04-05 05:48:34565 if (cuPtr->functionAddress >= functionAddressMax)
Greg McGary99930712020-12-07 06:33:38566 break;
Jez Ngc4d9df92023-04-05 05:48:34567 if (commonEncodingIndexes.count(cuPtr->encoding) ||
568 page.localEncodingIndexes.count(cuPtr->encoding)) {
Greg McGary99930712020-12-07 06:33:38569 i++;
570 wordsRemaining--;
571 } else if (wordsRemaining >= 2 && n < COMPACT_ENCODINGS_MAX) {
572 page.localEncodings.emplace_back(cuPtr->encoding);
573 page.localEncodingIndexes[cuPtr->encoding] = n++;
574 i++;
575 wordsRemaining -= 2;
576 } else {
577 break;
578 }
579 }
580 page.entryCount = i - page.entryIndex;
581
Fangrui Song640d9b32022-11-09 01:28:04582 // If this is not the final page, see if it's possible to fit more entries
583 // by using the regular format. This can happen when there are many unique
584 // encodings, and we saturated the local encoding table early.
Jez Nga2404f12021-11-11 00:31:54585 if (i < cuIndices.size() &&
Greg McGary99930712020-12-07 06:33:38586 page.entryCount < REGULAR_SECOND_LEVEL_ENTRIES_MAX) {
587 page.kind = UNWIND_SECOND_LEVEL_REGULAR;
588 page.entryCount = std::min(REGULAR_SECOND_LEVEL_ENTRIES_MAX,
Jez Nga2404f12021-11-11 00:31:54589 cuIndices.size() - page.entryIndex);
Greg McGary99930712020-12-07 06:33:38590 i = page.entryIndex + page.entryCount;
591 } else {
592 page.kind = UNWIND_SECOND_LEVEL_COMPRESSED;
593 }
Greg McGary2124ca12020-08-20 20:05:13594 }
595
Jez Nga2404f12021-11-11 00:31:54596 for (size_t idx : cuIndices) {
Jez Nga2404f12021-11-11 00:31:54597 lsdaIndex[idx] = entriesWithLsda.size();
Jez Ng82dcf302022-04-09 02:33:00598 if (cuEntries[idx].lsda)
Jez Nga2404f12021-11-11 00:31:54599 entriesWithLsda.push_back(idx);
Jez Ng51120352021-02-08 18:47:34600 }
601
Greg McGary2124ca12020-08-20 20:05:13602 // compute size of __TEXT,__unwind_info section
Jez Nga2404f12021-11-11 00:31:54603 level2PagesOffset = sizeof(unwind_info_section_header) +
604 commonEncodings.size() * sizeof(uint32_t) +
605 personalities.size() * sizeof(uint32_t) +
606 // The extra second-level-page entry is for the sentinel
607 (secondLevelPages.size() + 1) *
608 sizeof(unwind_info_section_header_index_entry) +
609 entriesWithLsda.size() *
610 sizeof(unwind_info_section_header_lsda_index_entry);
Greg McGary99930712020-12-07 06:33:38611 unwindInfoSize =
612 level2PagesOffset + secondLevelPages.size() * SECOND_LEVEL_PAGE_BYTES;
Greg McGary2124ca12020-08-20 20:05:13613}
614
Nico Weber126f58e2020-12-02 01:27:33615// All inputs are relocated and output addresses are known, so write!
Greg McGary2124ca12020-08-20 20:05:13616
Jez Ng2a666902022-04-13 20:17:29617void UnwindInfoSectionImpl::writeTo(uint8_t *buf) const {
Jez Nga2404f12021-11-11 00:31:54618 assert(!cuIndices.empty() && "call only if there is unwind info");
Nico Weber8a7b5eb2021-07-07 15:28:27619
Greg McGary2124ca12020-08-20 20:05:13620 // section header
621 auto *uip = reinterpret_cast<unwind_info_section_header *>(buf);
622 uip->version = 1;
623 uip->commonEncodingsArraySectionOffset = sizeof(unwind_info_section_header);
624 uip->commonEncodingsArrayCount = commonEncodings.size();
625 uip->personalityArraySectionOffset =
626 uip->commonEncodingsArraySectionOffset +
627 (uip->commonEncodingsArrayCount * sizeof(uint32_t));
628 uip->personalityArrayCount = personalities.size();
629 uip->indexSectionOffset = uip->personalityArraySectionOffset +
630 (uip->personalityArrayCount * sizeof(uint32_t));
Greg McGary99930712020-12-07 06:33:38631 uip->indexCount = secondLevelPages.size() + 1;
Greg McGary2124ca12020-08-20 20:05:13632
633 // Common encodings
634 auto *i32p = reinterpret_cast<uint32_t *>(&uip[1]);
635 for (const auto &encoding : commonEncodings)
636 *i32p++ = encoding.first;
637
638 // Personalities
Jez Ng82dcf302022-04-09 02:33:00639 for (const Symbol *personality : personalities)
640 *i32p++ = personality->getGotVA() - in.header->addr;
Greg McGary2124ca12020-08-20 20:05:13641
Vy Nguyen65226d32022-11-18 20:21:23642 // FIXME: LD64 checks and warns aboutgaps or overlapse in cuEntries address
643 // ranges. We should do the same too
644
Greg McGary2124ca12020-08-20 20:05:13645 // Level-1 index
646 uint32_t lsdaOffset =
647 uip->indexSectionOffset +
648 uip->indexCount * sizeof(unwind_info_section_header_index_entry);
649 uint64_t l2PagesOffset = level2PagesOffset;
650 auto *iep = reinterpret_cast<unwind_info_section_header_index_entry *>(i32p);
Greg McGary99930712020-12-07 06:33:38651 for (const SecondLevelPage &page : secondLevelPages) {
Jez Nga2404f12021-11-11 00:31:54652 size_t idx = cuIndices[page.entryIndex];
653 iep->functionOffset = cuEntries[idx].functionAddress - in.header->addr;
Greg McGary2124ca12020-08-20 20:05:13654 iep->secondLevelPagesSectionOffset = l2PagesOffset;
Jez Ng51120352021-02-08 18:47:34655 iep->lsdaIndexArraySectionOffset =
Jez Nga2404f12021-11-11 00:31:54656 lsdaOffset + lsdaIndex.lookup(idx) *
Jez Ng51120352021-02-08 18:47:34657 sizeof(unwind_info_section_header_lsda_index_entry);
Greg McGary2124ca12020-08-20 20:05:13658 iep++;
Greg McGary99930712020-12-07 06:33:38659 l2PagesOffset += SECOND_LEVEL_PAGE_BYTES;
Greg McGary2124ca12020-08-20 20:05:13660 }
661 // Level-1 sentinel
Vy Nguyen65226d32022-11-18 20:21:23662 // XXX(vyng): Note that LD64 adds +1 here.
663 // Unsure whether it's a bug or it's their workaround for something else.
664 // See comments from https://reviews.llvm.org/D138320.
665 iep->functionOffset = cueEndBoundary - in.header->addr;
Greg McGary2124ca12020-08-20 20:05:13666 iep->secondLevelPagesSectionOffset = 0;
Jez Ng51120352021-02-08 18:47:34667 iep->lsdaIndexArraySectionOffset =
Jez Nga2404f12021-11-11 00:31:54668 lsdaOffset + entriesWithLsda.size() *
669 sizeof(unwind_info_section_header_lsda_index_entry);
Greg McGary2124ca12020-08-20 20:05:13670 iep++;
671
672 // LSDAs
Jez Nga2404f12021-11-11 00:31:54673 auto *lep =
674 reinterpret_cast<unwind_info_section_header_lsda_index_entry *>(iep);
675 for (size_t idx : entriesWithLsda) {
Jez Ng82dcf302022-04-09 02:33:00676 const CompactUnwindEntry &cu = cuEntries[idx];
677 lep->lsdaOffset = cu.lsda->getVA(/*off=*/0) - in.header->addr;
Jez Nga2404f12021-11-11 00:31:54678 lep->functionOffset = cu.functionAddress - in.header->addr;
679 lep++;
680 }
Greg McGary2124ca12020-08-20 20:05:13681
Greg McGary2124ca12020-08-20 20:05:13682 // Level-2 pages
Jez Nga2404f12021-11-11 00:31:54683 auto *pp = reinterpret_cast<uint32_t *>(lep);
Greg McGary99930712020-12-07 06:33:38684 for (const SecondLevelPage &page : secondLevelPages) {
685 if (page.kind == UNWIND_SECOND_LEVEL_COMPRESSED) {
686 uintptr_t functionAddressBase =
Jez Nga2404f12021-11-11 00:31:54687 cuEntries[cuIndices[page.entryIndex]].functionAddress;
Greg McGary99930712020-12-07 06:33:38688 auto *p2p =
689 reinterpret_cast<unwind_info_compressed_second_level_page_header *>(
690 pp);
691 p2p->kind = page.kind;
692 p2p->entryPageOffset =
693 sizeof(unwind_info_compressed_second_level_page_header);
694 p2p->entryCount = page.entryCount;
695 p2p->encodingsPageOffset =
696 p2p->entryPageOffset + p2p->entryCount * sizeof(uint32_t);
697 p2p->encodingsCount = page.localEncodings.size();
698 auto *ep = reinterpret_cast<uint32_t *>(&p2p[1]);
699 for (size_t i = 0; i < page.entryCount; i++) {
Jez Ng82dcf302022-04-09 02:33:00700 const CompactUnwindEntry &cue =
Jez Nga2404f12021-11-11 00:31:54701 cuEntries[cuIndices[page.entryIndex + i]];
702 auto it = commonEncodingIndexes.find(cue.encoding);
Greg McGary99930712020-12-07 06:33:38703 if (it == commonEncodingIndexes.end())
Jez Nga2404f12021-11-11 00:31:54704 it = page.localEncodingIndexes.find(cue.encoding);
Greg McGary99930712020-12-07 06:33:38705 *ep++ = (it->second << COMPRESSED_ENTRY_FUNC_OFFSET_BITS) |
Jez Nga2404f12021-11-11 00:31:54706 (cue.functionAddress - functionAddressBase);
Greg McGary99930712020-12-07 06:33:38707 }
Vy Nguyen3f35dd02021-10-26 19:14:25708 if (!page.localEncodings.empty())
Fangrui Song791fe7a2020-12-21 04:01:20709 memcpy(ep, page.localEncodings.data(),
710 page.localEncodings.size() * sizeof(uint32_t));
Greg McGary99930712020-12-07 06:33:38711 } else {
712 auto *p2p =
713 reinterpret_cast<unwind_info_regular_second_level_page_header *>(pp);
714 p2p->kind = page.kind;
715 p2p->entryPageOffset =
716 sizeof(unwind_info_regular_second_level_page_header);
717 p2p->entryCount = page.entryCount;
718 auto *ep = reinterpret_cast<uint32_t *>(&p2p[1]);
719 for (size_t i = 0; i < page.entryCount; i++) {
Jez Ng82dcf302022-04-09 02:33:00720 const CompactUnwindEntry &cue =
Jez Nga2404f12021-11-11 00:31:54721 cuEntries[cuIndices[page.entryIndex + i]];
722 *ep++ = cue.functionAddress;
723 *ep++ = cue.encoding;
Greg McGary99930712020-12-07 06:33:38724 }
Greg McGary2124ca12020-08-20 20:05:13725 }
Greg McGary99930712020-12-07 06:33:38726 pp += SECOND_LEVEL_PAGE_WORDS;
Greg McGary2124ca12020-08-20 20:05:13727 }
Greg McGary2124ca12020-08-20 20:05:13728}
Jez Ng14609422021-04-16 01:14:33729
730UnwindInfoSection *macho::makeUnwindInfoSection() {
Jez Ng2a666902022-04-13 20:17:29731 return make<UnwindInfoSectionImpl>();
Jez Ng14609422021-04-16 01:14:33732}