11cf9926bSpatrick //===- UnwindInfoSection.cpp ----------------------------------------------===//
21cf9926bSpatrick //
31cf9926bSpatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
41cf9926bSpatrick // See https://llvm.org/LICENSE.txt for license information.
51cf9926bSpatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
61cf9926bSpatrick //
71cf9926bSpatrick //===----------------------------------------------------------------------===//
81cf9926bSpatrick
91cf9926bSpatrick #include "UnwindInfoSection.h"
101cf9926bSpatrick #include "InputSection.h"
111cf9926bSpatrick #include "OutputSection.h"
121cf9926bSpatrick #include "OutputSegment.h"
131cf9926bSpatrick #include "SymbolTable.h"
141cf9926bSpatrick #include "Symbols.h"
151cf9926bSpatrick #include "SyntheticSections.h"
161cf9926bSpatrick #include "Target.h"
171cf9926bSpatrick
181cf9926bSpatrick #include "lld/Common/ErrorHandler.h"
191cf9926bSpatrick #include "lld/Common/Memory.h"
20*dfe94b16Srobert #include "llvm/ADT/DenseMap.h"
211cf9926bSpatrick #include "llvm/ADT/STLExtras.h"
221cf9926bSpatrick #include "llvm/BinaryFormat/MachO.h"
23*dfe94b16Srobert #include "llvm/Support/Parallel.h"
24*dfe94b16Srobert
25*dfe94b16Srobert #include "mach-o/compact_unwind_encoding.h"
26*dfe94b16Srobert
27*dfe94b16Srobert #include <numeric>
281cf9926bSpatrick
291cf9926bSpatrick using namespace llvm;
301cf9926bSpatrick using namespace llvm::MachO;
31*dfe94b16Srobert using namespace llvm::support::endian;
321cf9926bSpatrick using namespace lld;
331cf9926bSpatrick using namespace lld::macho;
341cf9926bSpatrick
351cf9926bSpatrick #define COMMON_ENCODINGS_MAX 127
361cf9926bSpatrick #define COMPACT_ENCODINGS_MAX 256
371cf9926bSpatrick
381cf9926bSpatrick #define SECOND_LEVEL_PAGE_BYTES 4096
391cf9926bSpatrick #define SECOND_LEVEL_PAGE_WORDS (SECOND_LEVEL_PAGE_BYTES / sizeof(uint32_t))
401cf9926bSpatrick #define REGULAR_SECOND_LEVEL_ENTRIES_MAX \
411cf9926bSpatrick ((SECOND_LEVEL_PAGE_BYTES - \
421cf9926bSpatrick sizeof(unwind_info_regular_second_level_page_header)) / \
431cf9926bSpatrick sizeof(unwind_info_regular_second_level_entry))
441cf9926bSpatrick #define COMPRESSED_SECOND_LEVEL_ENTRIES_MAX \
451cf9926bSpatrick ((SECOND_LEVEL_PAGE_BYTES - \
461cf9926bSpatrick sizeof(unwind_info_compressed_second_level_page_header)) / \
471cf9926bSpatrick sizeof(uint32_t))
481cf9926bSpatrick
491cf9926bSpatrick #define COMPRESSED_ENTRY_FUNC_OFFSET_BITS 24
501cf9926bSpatrick #define COMPRESSED_ENTRY_FUNC_OFFSET_MASK \
511cf9926bSpatrick UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET(~0)
521cf9926bSpatrick
531cf9926bSpatrick // Compact Unwind format is a Mach-O evolution of DWARF Unwind that
541cf9926bSpatrick // optimizes space and exception-time lookup. Most DWARF unwind
551cf9926bSpatrick // entries can be replaced with Compact Unwind entries, but the ones
561cf9926bSpatrick // that cannot are retained in DWARF form.
571cf9926bSpatrick //
581cf9926bSpatrick // This comment will address macro-level organization of the pre-link
591cf9926bSpatrick // and post-link compact unwind tables. For micro-level organization
601cf9926bSpatrick // pertaining to the bitfield layout of the 32-bit compact unwind
611cf9926bSpatrick // entries, see libunwind/include/mach-o/compact_unwind_encoding.h
621cf9926bSpatrick //
631cf9926bSpatrick // Important clarifying factoids:
641cf9926bSpatrick //
651cf9926bSpatrick // * __LD,__compact_unwind is the compact unwind format for compiler
661cf9926bSpatrick // output and linker input. It is never a final output. It could be
671cf9926bSpatrick // an intermediate output with the `-r` option which retains relocs.
681cf9926bSpatrick //
691cf9926bSpatrick // * __TEXT,__unwind_info is the compact unwind format for final
701cf9926bSpatrick // linker output. It is never an input.
711cf9926bSpatrick //
721cf9926bSpatrick // * __TEXT,__eh_frame is the DWARF format for both linker input and output.
731cf9926bSpatrick //
741cf9926bSpatrick // * __TEXT,__unwind_info entries are divided into 4 KiB pages (2nd
751cf9926bSpatrick // level) by ascending address, and the pages are referenced by an
761cf9926bSpatrick // index (1st level) in the section header.
771cf9926bSpatrick //
781cf9926bSpatrick // * Following the headers in __TEXT,__unwind_info, the bulk of the
791cf9926bSpatrick // section contains a vector of compact unwind entries
801cf9926bSpatrick // `{functionOffset, encoding}` sorted by ascending `functionOffset`.
811cf9926bSpatrick // Adjacent entries with the same encoding can be folded to great
821cf9926bSpatrick // advantage, achieving a 3-order-of-magnitude reduction in the
831cf9926bSpatrick // number of entries.
841cf9926bSpatrick //
851cf9926bSpatrick // Refer to the definition of unwind_info_section_header in
861cf9926bSpatrick // compact_unwind_encoding.h for an overview of the format we are encoding
871cf9926bSpatrick // here.
881cf9926bSpatrick
891cf9926bSpatrick // TODO(gkm): how do we align the 2nd-level pages?
901cf9926bSpatrick
91*dfe94b16Srobert // The offsets of various fields in the on-disk representation of each compact
92*dfe94b16Srobert // unwind entry.
93*dfe94b16Srobert struct CompactUnwindOffsets {
94*dfe94b16Srobert uint32_t functionAddress;
95*dfe94b16Srobert uint32_t functionLength;
96*dfe94b16Srobert uint32_t encoding;
97*dfe94b16Srobert uint32_t personality;
98*dfe94b16Srobert uint32_t lsda;
99*dfe94b16Srobert
CompactUnwindOffsetsCompactUnwindOffsets100*dfe94b16Srobert CompactUnwindOffsets(size_t wordSize) {
101*dfe94b16Srobert if (wordSize == 8)
102*dfe94b16Srobert init<uint64_t>();
103*dfe94b16Srobert else {
104*dfe94b16Srobert assert(wordSize == 4);
105*dfe94b16Srobert init<uint32_t>();
106*dfe94b16Srobert }
107*dfe94b16Srobert }
108*dfe94b16Srobert
109*dfe94b16Srobert private:
initCompactUnwindOffsets110*dfe94b16Srobert template <class Ptr> void init() {
111*dfe94b16Srobert functionAddress = offsetof(Layout<Ptr>, functionAddress);
112*dfe94b16Srobert functionLength = offsetof(Layout<Ptr>, functionLength);
113*dfe94b16Srobert encoding = offsetof(Layout<Ptr>, encoding);
114*dfe94b16Srobert personality = offsetof(Layout<Ptr>, personality);
115*dfe94b16Srobert lsda = offsetof(Layout<Ptr>, lsda);
116*dfe94b16Srobert }
117*dfe94b16Srobert
118*dfe94b16Srobert template <class Ptr> struct Layout {
119*dfe94b16Srobert Ptr functionAddress;
120*dfe94b16Srobert uint32_t functionLength;
121*dfe94b16Srobert compact_unwind_encoding_t encoding;
122*dfe94b16Srobert Ptr personality;
123*dfe94b16Srobert Ptr lsda;
124*dfe94b16Srobert };
125*dfe94b16Srobert };
126*dfe94b16Srobert
127*dfe94b16Srobert // LLD's internal representation of a compact unwind entry.
128*dfe94b16Srobert struct CompactUnwindEntry {
129*dfe94b16Srobert uint64_t functionAddress;
130*dfe94b16Srobert uint32_t functionLength;
131*dfe94b16Srobert compact_unwind_encoding_t encoding;
132*dfe94b16Srobert Symbol *personality;
133*dfe94b16Srobert InputSection *lsda;
134*dfe94b16Srobert };
135*dfe94b16Srobert
1361cf9926bSpatrick using EncodingMap = DenseMap<compact_unwind_encoding_t, size_t>;
1371cf9926bSpatrick
1381cf9926bSpatrick struct SecondLevelPage {
1391cf9926bSpatrick uint32_t kind;
1401cf9926bSpatrick size_t entryIndex;
1411cf9926bSpatrick size_t entryCount;
1421cf9926bSpatrick size_t byteCount;
1431cf9926bSpatrick std::vector<compact_unwind_encoding_t> localEncodings;
1441cf9926bSpatrick EncodingMap localEncodingIndexes;
1451cf9926bSpatrick };
1461cf9926bSpatrick
147*dfe94b16Srobert // UnwindInfoSectionImpl allows us to avoid cluttering our header file with a
148*dfe94b16Srobert // lengthy definition of UnwindInfoSection.
1491cf9926bSpatrick class UnwindInfoSectionImpl final : public UnwindInfoSection {
1501cf9926bSpatrick public:
UnwindInfoSectionImpl()151*dfe94b16Srobert UnwindInfoSectionImpl() : cuOffsets(target->wordSize) {}
getSize() const152*dfe94b16Srobert uint64_t getSize() const override { return unwindInfoSize; }
153*dfe94b16Srobert void prepare() override;
1541cf9926bSpatrick void finalize() override;
1551cf9926bSpatrick void writeTo(uint8_t *buf) const override;
1561cf9926bSpatrick
1571cf9926bSpatrick private:
158*dfe94b16Srobert void prepareRelocations(ConcatInputSection *);
159*dfe94b16Srobert void relocateCompactUnwind(std::vector<CompactUnwindEntry> &);
160*dfe94b16Srobert void encodePersonalities();
161*dfe94b16Srobert Symbol *canonicalizePersonality(Symbol *);
162*dfe94b16Srobert
163*dfe94b16Srobert uint64_t unwindInfoSize = 0;
164*dfe94b16Srobert std::vector<decltype(symbols)::value_type> symbolsVec;
165*dfe94b16Srobert CompactUnwindOffsets cuOffsets;
1661cf9926bSpatrick std::vector<std::pair<compact_unwind_encoding_t, size_t>> commonEncodings;
1671cf9926bSpatrick EncodingMap commonEncodingIndexes;
168*dfe94b16Srobert // The entries here will be in the same order as their originating symbols
169*dfe94b16Srobert // in symbolsVec.
170*dfe94b16Srobert std::vector<CompactUnwindEntry> cuEntries;
171*dfe94b16Srobert // Indices into the cuEntries vector.
172*dfe94b16Srobert std::vector<size_t> cuIndices;
173*dfe94b16Srobert std::vector<Symbol *> personalities;
1741cf9926bSpatrick SmallDenseMap<std::pair<InputSection *, uint64_t /* addend */>, Symbol *>
1751cf9926bSpatrick personalityTable;
176*dfe94b16Srobert // Indices into cuEntries for CUEs with a non-null LSDA.
177*dfe94b16Srobert std::vector<size_t> entriesWithLsda;
178*dfe94b16Srobert // Map of cuEntries index to an index within the LSDA array.
179*dfe94b16Srobert DenseMap<size_t, uint32_t> lsdaIndex;
1801cf9926bSpatrick std::vector<SecondLevelPage> secondLevelPages;
1811cf9926bSpatrick uint64_t level2PagesOffset = 0;
182*dfe94b16Srobert // The highest-address function plus its size. The unwinder needs this to
183*dfe94b16Srobert // determine the address range that is covered by unwind info.
184*dfe94b16Srobert uint64_t cueEndBoundary = 0;
1851cf9926bSpatrick };
1861cf9926bSpatrick
UnwindInfoSection()1871cf9926bSpatrick UnwindInfoSection::UnwindInfoSection()
1881cf9926bSpatrick : SyntheticSection(segment_names::text, section_names::unwindInfo) {
1891cf9926bSpatrick align = 4;
1901cf9926bSpatrick }
1911cf9926bSpatrick
192*dfe94b16Srobert // Record function symbols that may need entries emitted in __unwind_info, which
193*dfe94b16Srobert // stores unwind data for address ranges.
194*dfe94b16Srobert //
195*dfe94b16Srobert // Note that if several adjacent functions have the same unwind encoding and
196*dfe94b16Srobert // personality function and no LSDA, they share one unwind entry. For this to
197*dfe94b16Srobert // work, functions without unwind info need explicit "no unwind info" unwind
198*dfe94b16Srobert // entries -- else the unwinder would think they have the unwind info of the
199*dfe94b16Srobert // closest function with unwind info right before in the image. Thus, we add
200*dfe94b16Srobert // function symbols for each unique address regardless of whether they have
201*dfe94b16Srobert // associated unwind info.
addSymbol(const Defined * d)202*dfe94b16Srobert void UnwindInfoSection::addSymbol(const Defined *d) {
203*dfe94b16Srobert if (d->unwindEntry)
204*dfe94b16Srobert allEntriesAreOmitted = false;
205*dfe94b16Srobert // We don't yet know the final output address of this symbol, but we know that
206*dfe94b16Srobert // they are uniquely determined by a combination of the isec and value, so
207*dfe94b16Srobert // we use that as the key here.
208*dfe94b16Srobert auto p = symbols.insert({{d->isec, d->value}, d});
209*dfe94b16Srobert // If we have multiple symbols at the same address, only one of them can have
210*dfe94b16Srobert // an associated unwind entry.
211*dfe94b16Srobert if (!p.second && d->unwindEntry) {
212*dfe94b16Srobert assert(p.first->second == d || !p.first->second->unwindEntry);
213*dfe94b16Srobert p.first->second = d;
214*dfe94b16Srobert }
2151cf9926bSpatrick }
2161cf9926bSpatrick
prepare()217*dfe94b16Srobert void UnwindInfoSectionImpl::prepare() {
218*dfe94b16Srobert // This iteration needs to be deterministic, since prepareRelocations may add
219*dfe94b16Srobert // entries to the GOT. Hence the use of a MapVector for
220*dfe94b16Srobert // UnwindInfoSection::symbols.
221*dfe94b16Srobert for (const Defined *d : make_second_range(symbols))
222*dfe94b16Srobert if (d->unwindEntry) {
223*dfe94b16Srobert if (d->unwindEntry->getName() == section_names::compactUnwind) {
224*dfe94b16Srobert prepareRelocations(d->unwindEntry);
225*dfe94b16Srobert } else {
226*dfe94b16Srobert // We don't have to add entries to the GOT here because FDEs have
227*dfe94b16Srobert // explicit GOT relocations, so Writer::scanRelocations() will add those
228*dfe94b16Srobert // GOT entries. However, we still need to canonicalize the personality
229*dfe94b16Srobert // pointers (like prepareRelocations() does for CU entries) in order
230*dfe94b16Srobert // to avoid overflowing the 3-personality limit.
231*dfe94b16Srobert FDE &fde = cast<ObjFile>(d->getFile())->fdes[d->unwindEntry];
232*dfe94b16Srobert fde.personality = canonicalizePersonality(fde.personality);
233*dfe94b16Srobert }
234*dfe94b16Srobert }
2351cf9926bSpatrick }
2361cf9926bSpatrick
2371cf9926bSpatrick // Compact unwind relocations have different semantics, so we handle them in a
2381cf9926bSpatrick // separate code path from regular relocations. First, we do not wish to add
2391cf9926bSpatrick // rebase opcodes for __LD,__compact_unwind, because that section doesn't
2401cf9926bSpatrick // actually end up in the final binary. Second, personality pointers always
2411cf9926bSpatrick // reside in the GOT and must be treated specially.
prepareRelocations(ConcatInputSection * isec)242*dfe94b16Srobert void UnwindInfoSectionImpl::prepareRelocations(ConcatInputSection *isec) {
2431cf9926bSpatrick assert(!isec->shouldOmitFromOutput() &&
2441cf9926bSpatrick "__compact_unwind section should not be omitted");
2451cf9926bSpatrick
2461cf9926bSpatrick // FIXME: Make this skip relocations for CompactUnwindEntries that
2471cf9926bSpatrick // point to dead-stripped functions. That might save some amount of
2481cf9926bSpatrick // work. But since there are usually just few personality functions
2491cf9926bSpatrick // that are referenced from many places, at least some of them likely
2501cf9926bSpatrick // live, it wouldn't reduce number of got entries.
2511cf9926bSpatrick for (size_t i = 0; i < isec->relocs.size(); ++i) {
2521cf9926bSpatrick Reloc &r = isec->relocs[i];
2531cf9926bSpatrick assert(target->hasAttr(r.type, RelocAttrBits::UNSIGNED));
254*dfe94b16Srobert // Since compact unwind sections aren't part of the inputSections vector,
255*dfe94b16Srobert // they don't get canonicalized by scanRelocations(), so we have to do the
256*dfe94b16Srobert // canonicalization here.
257*dfe94b16Srobert if (auto *referentIsec = r.referent.dyn_cast<InputSection *>())
258*dfe94b16Srobert r.referent = referentIsec->canonical();
2591cf9926bSpatrick
260*dfe94b16Srobert // Functions and LSDA entries always reside in the same object file as the
261*dfe94b16Srobert // compact unwind entries that references them, and thus appear as section
262*dfe94b16Srobert // relocs. There is no need to prepare them. We only prepare relocs for
263*dfe94b16Srobert // personality functions.
264*dfe94b16Srobert if (r.offset != cuOffsets.personality)
2651cf9926bSpatrick continue;
2661cf9926bSpatrick
2671cf9926bSpatrick if (auto *s = r.referent.dyn_cast<Symbol *>()) {
268*dfe94b16Srobert // Personality functions are nearly always system-defined (e.g.,
269*dfe94b16Srobert // ___gxx_personality_v0 for C++) and relocated as dylib symbols. When an
270*dfe94b16Srobert // application provides its own personality function, it might be
271*dfe94b16Srobert // referenced by an extern Defined symbol reloc, or a local section reloc.
272*dfe94b16Srobert if (auto *defined = dyn_cast<Defined>(s)) {
273*dfe94b16Srobert // XXX(vyng) This is a special case for handling duplicate personality
274*dfe94b16Srobert // symbols. Note that LD64's behavior is a bit different and it is
275*dfe94b16Srobert // inconsistent with how symbol resolution usually work
276*dfe94b16Srobert //
277*dfe94b16Srobert // So we've decided not to follow it. Instead, simply pick the symbol
278*dfe94b16Srobert // with the same name from the symbol table to replace the local one.
279*dfe94b16Srobert //
280*dfe94b16Srobert // (See discussions/alternatives already considered on D107533)
281*dfe94b16Srobert if (!defined->isExternal())
282*dfe94b16Srobert if (Symbol *sym = symtab->find(defined->getName()))
283*dfe94b16Srobert if (!sym->isLazy())
284*dfe94b16Srobert r.referent = s = sym;
285*dfe94b16Srobert }
2861cf9926bSpatrick if (auto *undefined = dyn_cast<Undefined>(s)) {
287*dfe94b16Srobert treatUndefinedSymbol(*undefined, isec, r.offset);
2881cf9926bSpatrick // treatUndefinedSymbol() can replace s with a DylibSymbol; re-check.
2891cf9926bSpatrick if (isa<Undefined>(s))
2901cf9926bSpatrick continue;
2911cf9926bSpatrick }
292*dfe94b16Srobert
293*dfe94b16Srobert // Similar to canonicalizePersonality(), but we also register a GOT entry.
2941cf9926bSpatrick if (auto *defined = dyn_cast<Defined>(s)) {
2951cf9926bSpatrick // Check if we have created a synthetic symbol at the same address.
2961cf9926bSpatrick Symbol *&personality =
2971cf9926bSpatrick personalityTable[{defined->isec, defined->value}];
2981cf9926bSpatrick if (personality == nullptr) {
2991cf9926bSpatrick personality = defined;
3001cf9926bSpatrick in.got->addEntry(defined);
3011cf9926bSpatrick } else if (personality != defined) {
3021cf9926bSpatrick r.referent = personality;
3031cf9926bSpatrick }
3041cf9926bSpatrick continue;
3051cf9926bSpatrick }
306*dfe94b16Srobert
3071cf9926bSpatrick assert(isa<DylibSymbol>(s));
3081cf9926bSpatrick in.got->addEntry(s);
3091cf9926bSpatrick continue;
3101cf9926bSpatrick }
3111cf9926bSpatrick
3121cf9926bSpatrick if (auto *referentIsec = r.referent.dyn_cast<InputSection *>()) {
3131cf9926bSpatrick assert(!isCoalescedWeak(referentIsec));
3141cf9926bSpatrick // Personality functions can be referenced via section relocations
3151cf9926bSpatrick // if they live in the same object file. Create placeholder synthetic
3161cf9926bSpatrick // symbols for them in the GOT.
3171cf9926bSpatrick Symbol *&s = personalityTable[{referentIsec, r.addend}];
3181cf9926bSpatrick if (s == nullptr) {
3191cf9926bSpatrick // This runs after dead stripping, so the noDeadStrip argument does not
3201cf9926bSpatrick // matter.
3211cf9926bSpatrick s = make<Defined>("<internal>", /*file=*/nullptr, referentIsec,
3221cf9926bSpatrick r.addend, /*size=*/0, /*isWeakDef=*/false,
3231cf9926bSpatrick /*isExternal=*/false, /*isPrivateExtern=*/false,
324*dfe94b16Srobert /*includeInSymtab=*/true,
3251cf9926bSpatrick /*isThumb=*/false, /*isReferencedDynamically=*/false,
3261cf9926bSpatrick /*noDeadStrip=*/false);
327*dfe94b16Srobert s->used = true;
3281cf9926bSpatrick in.got->addEntry(s);
3291cf9926bSpatrick }
3301cf9926bSpatrick r.referent = s;
3311cf9926bSpatrick r.addend = 0;
3321cf9926bSpatrick }
3331cf9926bSpatrick }
3341cf9926bSpatrick }
3351cf9926bSpatrick
canonicalizePersonality(Symbol * personality)336*dfe94b16Srobert Symbol *UnwindInfoSectionImpl::canonicalizePersonality(Symbol *personality) {
337*dfe94b16Srobert if (auto *defined = dyn_cast_or_null<Defined>(personality)) {
338*dfe94b16Srobert // Check if we have created a synthetic symbol at the same address.
339*dfe94b16Srobert Symbol *&synth = personalityTable[{defined->isec, defined->value}];
340*dfe94b16Srobert if (synth == nullptr)
341*dfe94b16Srobert synth = defined;
342*dfe94b16Srobert else if (synth != defined)
343*dfe94b16Srobert return synth;
3441cf9926bSpatrick }
345*dfe94b16Srobert return personality;
346*dfe94b16Srobert }
3471cf9926bSpatrick
3481cf9926bSpatrick // We need to apply the relocations to the pre-link compact unwind section
3491cf9926bSpatrick // before converting it to post-link form. There should only be absolute
3501cf9926bSpatrick // relocations here: since we are not emitting the pre-link CU section, there
3511cf9926bSpatrick // is no source address to make a relative location meaningful.
relocateCompactUnwind(std::vector<CompactUnwindEntry> & cuEntries)352*dfe94b16Srobert void UnwindInfoSectionImpl::relocateCompactUnwind(
353*dfe94b16Srobert std::vector<CompactUnwindEntry> &cuEntries) {
354*dfe94b16Srobert parallelFor(0, symbolsVec.size(), [&](size_t i) {
355*dfe94b16Srobert CompactUnwindEntry &cu = cuEntries[i];
356*dfe94b16Srobert const Defined *d = symbolsVec[i].second;
357*dfe94b16Srobert cu.functionAddress = d->getVA();
358*dfe94b16Srobert if (!d->unwindEntry)
359*dfe94b16Srobert return;
3601cf9926bSpatrick
361*dfe94b16Srobert // If we have DWARF unwind info, create a CU entry that points to it.
362*dfe94b16Srobert if (d->unwindEntry->getName() == section_names::ehFrame) {
363*dfe94b16Srobert cu.encoding = target->modeDwarfEncoding | d->unwindEntry->outSecOff;
364*dfe94b16Srobert const FDE &fde = cast<ObjFile>(d->getFile())->fdes[d->unwindEntry];
365*dfe94b16Srobert cu.functionLength = fde.funcLength;
366*dfe94b16Srobert cu.personality = fde.personality;
367*dfe94b16Srobert cu.lsda = fde.lsda;
368*dfe94b16Srobert return;
369*dfe94b16Srobert }
3701cf9926bSpatrick
371*dfe94b16Srobert assert(d->unwindEntry->getName() == section_names::compactUnwind);
372*dfe94b16Srobert
373*dfe94b16Srobert auto buf = reinterpret_cast<const uint8_t *>(d->unwindEntry->data.data()) -
374*dfe94b16Srobert target->wordSize;
375*dfe94b16Srobert cu.functionLength =
376*dfe94b16Srobert support::endian::read32le(buf + cuOffsets.functionLength);
377*dfe94b16Srobert cu.encoding = support::endian::read32le(buf + cuOffsets.encoding);
378*dfe94b16Srobert for (const Reloc &r : d->unwindEntry->relocs) {
379*dfe94b16Srobert if (r.offset == cuOffsets.personality) {
380*dfe94b16Srobert cu.personality = r.referent.get<Symbol *>();
381*dfe94b16Srobert } else if (r.offset == cuOffsets.lsda) {
382*dfe94b16Srobert if (auto *referentSym = r.referent.dyn_cast<Symbol *>())
383*dfe94b16Srobert cu.lsda = cast<Defined>(referentSym)->isec;
384*dfe94b16Srobert else
385*dfe94b16Srobert cu.lsda = r.referent.get<InputSection *>();
3861cf9926bSpatrick }
3871cf9926bSpatrick }
388*dfe94b16Srobert });
3891cf9926bSpatrick }
3901cf9926bSpatrick
3911cf9926bSpatrick // There should only be a handful of unique personality pointers, so we can
3921cf9926bSpatrick // encode them as 2-bit indices into a small array.
encodePersonalities()393*dfe94b16Srobert void UnwindInfoSectionImpl::encodePersonalities() {
394*dfe94b16Srobert for (size_t idx : cuIndices) {
395*dfe94b16Srobert CompactUnwindEntry &cu = cuEntries[idx];
396*dfe94b16Srobert if (cu.personality == nullptr)
3971cf9926bSpatrick continue;
3981cf9926bSpatrick // Linear search is fast enough for a small array.
399*dfe94b16Srobert auto it = find(personalities, cu.personality);
4001cf9926bSpatrick uint32_t personalityIndex; // 1-based index
4011cf9926bSpatrick if (it != personalities.end()) {
4021cf9926bSpatrick personalityIndex = std::distance(personalities.begin(), it) + 1;
4031cf9926bSpatrick } else {
404*dfe94b16Srobert personalities.push_back(cu.personality);
4051cf9926bSpatrick personalityIndex = personalities.size();
4061cf9926bSpatrick }
407*dfe94b16Srobert cu.encoding |=
4081cf9926bSpatrick personalityIndex << countTrailingZeros(
4091cf9926bSpatrick static_cast<compact_unwind_encoding_t>(UNWIND_PERSONALITY_MASK));
4101cf9926bSpatrick }
4111cf9926bSpatrick if (personalities.size() > 3)
412*dfe94b16Srobert error("too many personalities (" + Twine(personalities.size()) +
4131cf9926bSpatrick ") for compact unwind to encode");
4141cf9926bSpatrick }
4151cf9926bSpatrick
canFoldEncoding(compact_unwind_encoding_t encoding)4161cf9926bSpatrick static bool canFoldEncoding(compact_unwind_encoding_t encoding) {
4171cf9926bSpatrick // From compact_unwind_encoding.h:
4181cf9926bSpatrick // UNWIND_X86_64_MODE_STACK_IND:
4191cf9926bSpatrick // A "frameless" (RBP not used as frame pointer) function large constant
4201cf9926bSpatrick // stack size. This case is like the previous, except the stack size is too
4211cf9926bSpatrick // large to encode in the compact unwind encoding. Instead it requires that
4221cf9926bSpatrick // the function contains "subq $nnnnnnnn,RSP" in its prolog. The compact
4231cf9926bSpatrick // encoding contains the offset to the nnnnnnnn value in the function in
4241cf9926bSpatrick // UNWIND_X86_64_FRAMELESS_STACK_SIZE.
4251cf9926bSpatrick // Since this means the unwinder has to look at the `subq` in the function
4261cf9926bSpatrick // of the unwind info's unwind address, two functions that have identical
4271cf9926bSpatrick // unwind info can't be folded if it's using this encoding since both
4281cf9926bSpatrick // entries need unique addresses.
429*dfe94b16Srobert static_assert(static_cast<uint32_t>(UNWIND_X86_64_MODE_STACK_IND) ==
430*dfe94b16Srobert static_cast<uint32_t>(UNWIND_X86_MODE_STACK_IND));
4311cf9926bSpatrick if ((target->cpuType == CPU_TYPE_X86_64 || target->cpuType == CPU_TYPE_X86) &&
432*dfe94b16Srobert (encoding & UNWIND_MODE_MASK) == UNWIND_X86_64_MODE_STACK_IND) {
4331cf9926bSpatrick // FIXME: Consider passing in the two function addresses and getting
4341cf9926bSpatrick // their two stack sizes off the `subq` and only returning false if they're
4351cf9926bSpatrick // actually different.
4361cf9926bSpatrick return false;
4371cf9926bSpatrick }
4381cf9926bSpatrick return true;
4391cf9926bSpatrick }
4401cf9926bSpatrick
4411cf9926bSpatrick // Scan the __LD,__compact_unwind entries and compute the space needs of
442*dfe94b16Srobert // __TEXT,__unwind_info and __TEXT,__eh_frame.
finalize()443*dfe94b16Srobert void UnwindInfoSectionImpl::finalize() {
444*dfe94b16Srobert if (symbols.empty())
4451cf9926bSpatrick return;
4461cf9926bSpatrick
4471cf9926bSpatrick // At this point, the address space for __TEXT,__text has been
4481cf9926bSpatrick // assigned, so we can relocate the __LD,__compact_unwind entries
4491cf9926bSpatrick // into a temporary buffer. Relocation is necessary in order to sort
4501cf9926bSpatrick // the CU entries by function address. Sorting is necessary so that
451*dfe94b16Srobert // we can fold adjacent CU entries with identical encoding+personality
452*dfe94b16Srobert // and without any LSDA. Folding is necessary because it reduces the
453*dfe94b16Srobert // number of CU entries by as much as 3 orders of magnitude!
454*dfe94b16Srobert cuEntries.resize(symbols.size());
455*dfe94b16Srobert // The "map" part of the symbols MapVector was only needed for deduplication
456*dfe94b16Srobert // in addSymbol(). Now that we are done adding, move the contents to a plain
457*dfe94b16Srobert // std::vector for indexed access.
458*dfe94b16Srobert symbolsVec = symbols.takeVector();
459*dfe94b16Srobert relocateCompactUnwind(cuEntries);
4601cf9926bSpatrick
4611cf9926bSpatrick // Rather than sort & fold the 32-byte entries directly, we create a
462*dfe94b16Srobert // vector of indices to entries and sort & fold that instead.
463*dfe94b16Srobert cuIndices.resize(cuEntries.size());
464*dfe94b16Srobert std::iota(cuIndices.begin(), cuIndices.end(), 0);
465*dfe94b16Srobert llvm::sort(cuIndices, [&](size_t a, size_t b) {
466*dfe94b16Srobert return cuEntries[a].functionAddress < cuEntries[b].functionAddress;
4671cf9926bSpatrick });
4681cf9926bSpatrick
469*dfe94b16Srobert // Record the ending boundary before we fold the entries.
470*dfe94b16Srobert cueEndBoundary = cuEntries[cuIndices.back()].functionAddress +
471*dfe94b16Srobert cuEntries[cuIndices.back()].functionLength;
4721cf9926bSpatrick
473*dfe94b16Srobert // Fold adjacent entries with matching encoding+personality and without LSDA
474*dfe94b16Srobert // We use three iterators on the same cuIndices to fold in-situ:
4751cf9926bSpatrick // (1) `foldBegin` is the first of a potential sequence of matching entries
4761cf9926bSpatrick // (2) `foldEnd` is the first non-matching entry after `foldBegin`.
4771cf9926bSpatrick // The semi-open interval [ foldBegin .. foldEnd ) contains a range
4781cf9926bSpatrick // entries that can be folded into a single entry and written to ...
4791cf9926bSpatrick // (3) `foldWrite`
480*dfe94b16Srobert auto foldWrite = cuIndices.begin();
481*dfe94b16Srobert for (auto foldBegin = cuIndices.begin(); foldBegin < cuIndices.end();) {
4821cf9926bSpatrick auto foldEnd = foldBegin;
483*dfe94b16Srobert // Common LSDA encodings (e.g. for C++ and Objective-C) contain offsets from
484*dfe94b16Srobert // a base address. The base address is normally not contained directly in
485*dfe94b16Srobert // the LSDA, and in that case, the personality function treats the starting
486*dfe94b16Srobert // address of the function (which is computed by the unwinder) as the base
487*dfe94b16Srobert // address and interprets the LSDA accordingly. The unwinder computes the
488*dfe94b16Srobert // starting address of a function as the address associated with its CU
489*dfe94b16Srobert // entry. For this reason, we cannot fold adjacent entries if they have an
490*dfe94b16Srobert // LSDA, because folding would make the unwinder compute the wrong starting
491*dfe94b16Srobert // address for the functions with the folded entries, which in turn would
492*dfe94b16Srobert // cause the personality function to misinterpret the LSDA for those
493*dfe94b16Srobert // functions. In the very rare case where the base address is encoded
494*dfe94b16Srobert // directly in the LSDA, two functions at different addresses would
495*dfe94b16Srobert // necessarily have different LSDAs, so their CU entries would not have been
496*dfe94b16Srobert // folded anyway.
497*dfe94b16Srobert while (++foldEnd < cuIndices.end() &&
498*dfe94b16Srobert cuEntries[*foldBegin].encoding == cuEntries[*foldEnd].encoding &&
499*dfe94b16Srobert !cuEntries[*foldBegin].lsda && !cuEntries[*foldEnd].lsda &&
500*dfe94b16Srobert // If we've gotten to this point, we don't have an LSDA, which should
501*dfe94b16Srobert // also imply that we don't have a personality function, since in all
502*dfe94b16Srobert // likelihood a personality function needs the LSDA to do anything
503*dfe94b16Srobert // useful. It can be technically valid to have a personality function
504*dfe94b16Srobert // and no LSDA though (e.g. the C++ personality __gxx_personality_v0
505*dfe94b16Srobert // is just a no-op without LSDA), so we still check for personality
506*dfe94b16Srobert // function equivalence to handle that case.
507*dfe94b16Srobert cuEntries[*foldBegin].personality ==
508*dfe94b16Srobert cuEntries[*foldEnd].personality &&
509*dfe94b16Srobert canFoldEncoding(cuEntries[*foldEnd].encoding))
5101cf9926bSpatrick ;
5111cf9926bSpatrick *foldWrite++ = *foldBegin;
5121cf9926bSpatrick foldBegin = foldEnd;
5131cf9926bSpatrick }
514*dfe94b16Srobert cuIndices.erase(foldWrite, cuIndices.end());
5151cf9926bSpatrick
516*dfe94b16Srobert encodePersonalities();
5171cf9926bSpatrick
5181cf9926bSpatrick // Count frequencies of the folded encodings
5191cf9926bSpatrick EncodingMap encodingFrequencies;
520*dfe94b16Srobert for (size_t idx : cuIndices)
521*dfe94b16Srobert encodingFrequencies[cuEntries[idx].encoding]++;
5221cf9926bSpatrick
5231cf9926bSpatrick // Make a vector of encodings, sorted by descending frequency
5241cf9926bSpatrick for (const auto &frequency : encodingFrequencies)
5251cf9926bSpatrick commonEncodings.emplace_back(frequency);
5261cf9926bSpatrick llvm::sort(commonEncodings,
5271cf9926bSpatrick [](const std::pair<compact_unwind_encoding_t, size_t> &a,
5281cf9926bSpatrick const std::pair<compact_unwind_encoding_t, size_t> &b) {
5291cf9926bSpatrick if (a.second == b.second)
5301cf9926bSpatrick // When frequencies match, secondarily sort on encoding
5311cf9926bSpatrick // to maintain parity with validate-unwind-info.py
5321cf9926bSpatrick return a.first > b.first;
5331cf9926bSpatrick return a.second > b.second;
5341cf9926bSpatrick });
5351cf9926bSpatrick
5361cf9926bSpatrick // Truncate the vector to 127 elements.
5371cf9926bSpatrick // Common encoding indexes are limited to 0..126, while encoding
5381cf9926bSpatrick // indexes 127..255 are local to each second-level page
5391cf9926bSpatrick if (commonEncodings.size() > COMMON_ENCODINGS_MAX)
5401cf9926bSpatrick commonEncodings.resize(COMMON_ENCODINGS_MAX);
5411cf9926bSpatrick
5421cf9926bSpatrick // Create a map from encoding to common-encoding-table index
5431cf9926bSpatrick for (size_t i = 0; i < commonEncodings.size(); i++)
5441cf9926bSpatrick commonEncodingIndexes[commonEncodings[i].first] = i;
5451cf9926bSpatrick
5461cf9926bSpatrick // Split folded encodings into pages, where each page is limited by ...
5471cf9926bSpatrick // (a) 4 KiB capacity
5481cf9926bSpatrick // (b) 24-bit difference between first & final function address
5491cf9926bSpatrick // (c) 8-bit compact-encoding-table index,
5501cf9926bSpatrick // for which 0..126 references the global common-encodings table,
5511cf9926bSpatrick // and 127..255 references a local per-second-level-page table.
5521cf9926bSpatrick // First we try the compact format and determine how many entries fit.
5531cf9926bSpatrick // If more entries fit in the regular format, we use that.
554*dfe94b16Srobert for (size_t i = 0; i < cuIndices.size();) {
555*dfe94b16Srobert size_t idx = cuIndices[i];
5561cf9926bSpatrick secondLevelPages.emplace_back();
5571cf9926bSpatrick SecondLevelPage &page = secondLevelPages.back();
5581cf9926bSpatrick page.entryIndex = i;
559*dfe94b16Srobert uint64_t functionAddressMax =
560*dfe94b16Srobert cuEntries[idx].functionAddress + COMPRESSED_ENTRY_FUNC_OFFSET_MASK;
5611cf9926bSpatrick size_t n = commonEncodings.size();
5621cf9926bSpatrick size_t wordsRemaining =
5631cf9926bSpatrick SECOND_LEVEL_PAGE_WORDS -
5641cf9926bSpatrick sizeof(unwind_info_compressed_second_level_page_header) /
5651cf9926bSpatrick sizeof(uint32_t);
566*dfe94b16Srobert while (wordsRemaining >= 1 && i < cuIndices.size()) {
567*dfe94b16Srobert idx = cuIndices[i];
568*dfe94b16Srobert const CompactUnwindEntry *cuPtr = &cuEntries[idx];
5691cf9926bSpatrick if (cuPtr->functionAddress >= functionAddressMax) {
5701cf9926bSpatrick break;
5711cf9926bSpatrick } else if (commonEncodingIndexes.count(cuPtr->encoding) ||
5721cf9926bSpatrick page.localEncodingIndexes.count(cuPtr->encoding)) {
5731cf9926bSpatrick i++;
5741cf9926bSpatrick wordsRemaining--;
5751cf9926bSpatrick } else if (wordsRemaining >= 2 && n < COMPACT_ENCODINGS_MAX) {
5761cf9926bSpatrick page.localEncodings.emplace_back(cuPtr->encoding);
5771cf9926bSpatrick page.localEncodingIndexes[cuPtr->encoding] = n++;
5781cf9926bSpatrick i++;
5791cf9926bSpatrick wordsRemaining -= 2;
5801cf9926bSpatrick } else {
5811cf9926bSpatrick break;
5821cf9926bSpatrick }
5831cf9926bSpatrick }
5841cf9926bSpatrick page.entryCount = i - page.entryIndex;
5851cf9926bSpatrick
586*dfe94b16Srobert // If this is not the final page, see if it's possible to fit more entries
587*dfe94b16Srobert // by using the regular format. This can happen when there are many unique
588*dfe94b16Srobert // encodings, and we saturated the local encoding table early.
589*dfe94b16Srobert if (i < cuIndices.size() &&
5901cf9926bSpatrick page.entryCount < REGULAR_SECOND_LEVEL_ENTRIES_MAX) {
5911cf9926bSpatrick page.kind = UNWIND_SECOND_LEVEL_REGULAR;
5921cf9926bSpatrick page.entryCount = std::min(REGULAR_SECOND_LEVEL_ENTRIES_MAX,
593*dfe94b16Srobert cuIndices.size() - page.entryIndex);
5941cf9926bSpatrick i = page.entryIndex + page.entryCount;
5951cf9926bSpatrick } else {
5961cf9926bSpatrick page.kind = UNWIND_SECOND_LEVEL_COMPRESSED;
5971cf9926bSpatrick }
5981cf9926bSpatrick }
5991cf9926bSpatrick
600*dfe94b16Srobert for (size_t idx : cuIndices) {
601*dfe94b16Srobert lsdaIndex[idx] = entriesWithLsda.size();
602*dfe94b16Srobert if (cuEntries[idx].lsda)
603*dfe94b16Srobert entriesWithLsda.push_back(idx);
6041cf9926bSpatrick }
6051cf9926bSpatrick
6061cf9926bSpatrick // compute size of __TEXT,__unwind_info section
607*dfe94b16Srobert level2PagesOffset = sizeof(unwind_info_section_header) +
6081cf9926bSpatrick commonEncodings.size() * sizeof(uint32_t) +
6091cf9926bSpatrick personalities.size() * sizeof(uint32_t) +
6101cf9926bSpatrick // The extra second-level-page entry is for the sentinel
6111cf9926bSpatrick (secondLevelPages.size() + 1) *
6121cf9926bSpatrick sizeof(unwind_info_section_header_index_entry) +
613*dfe94b16Srobert entriesWithLsda.size() *
614*dfe94b16Srobert sizeof(unwind_info_section_header_lsda_index_entry);
6151cf9926bSpatrick unwindInfoSize =
6161cf9926bSpatrick level2PagesOffset + secondLevelPages.size() * SECOND_LEVEL_PAGE_BYTES;
6171cf9926bSpatrick }
6181cf9926bSpatrick
6191cf9926bSpatrick // All inputs are relocated and output addresses are known, so write!
6201cf9926bSpatrick
writeTo(uint8_t * buf) const621*dfe94b16Srobert void UnwindInfoSectionImpl::writeTo(uint8_t *buf) const {
622*dfe94b16Srobert assert(!cuIndices.empty() && "call only if there is unwind info");
6231cf9926bSpatrick
6241cf9926bSpatrick // section header
6251cf9926bSpatrick auto *uip = reinterpret_cast<unwind_info_section_header *>(buf);
6261cf9926bSpatrick uip->version = 1;
6271cf9926bSpatrick uip->commonEncodingsArraySectionOffset = sizeof(unwind_info_section_header);
6281cf9926bSpatrick uip->commonEncodingsArrayCount = commonEncodings.size();
6291cf9926bSpatrick uip->personalityArraySectionOffset =
6301cf9926bSpatrick uip->commonEncodingsArraySectionOffset +
6311cf9926bSpatrick (uip->commonEncodingsArrayCount * sizeof(uint32_t));
6321cf9926bSpatrick uip->personalityArrayCount = personalities.size();
6331cf9926bSpatrick uip->indexSectionOffset = uip->personalityArraySectionOffset +
6341cf9926bSpatrick (uip->personalityArrayCount * sizeof(uint32_t));
6351cf9926bSpatrick uip->indexCount = secondLevelPages.size() + 1;
6361cf9926bSpatrick
6371cf9926bSpatrick // Common encodings
6381cf9926bSpatrick auto *i32p = reinterpret_cast<uint32_t *>(&uip[1]);
6391cf9926bSpatrick for (const auto &encoding : commonEncodings)
6401cf9926bSpatrick *i32p++ = encoding.first;
6411cf9926bSpatrick
6421cf9926bSpatrick // Personalities
643*dfe94b16Srobert for (const Symbol *personality : personalities)
644*dfe94b16Srobert *i32p++ = personality->getGotVA() - in.header->addr;
645*dfe94b16Srobert
646*dfe94b16Srobert // FIXME: LD64 checks and warns aboutgaps or overlapse in cuEntries address
647*dfe94b16Srobert // ranges. We should do the same too
6481cf9926bSpatrick
6491cf9926bSpatrick // Level-1 index
6501cf9926bSpatrick uint32_t lsdaOffset =
6511cf9926bSpatrick uip->indexSectionOffset +
6521cf9926bSpatrick uip->indexCount * sizeof(unwind_info_section_header_index_entry);
6531cf9926bSpatrick uint64_t l2PagesOffset = level2PagesOffset;
6541cf9926bSpatrick auto *iep = reinterpret_cast<unwind_info_section_header_index_entry *>(i32p);
6551cf9926bSpatrick for (const SecondLevelPage &page : secondLevelPages) {
656*dfe94b16Srobert size_t idx = cuIndices[page.entryIndex];
657*dfe94b16Srobert iep->functionOffset = cuEntries[idx].functionAddress - in.header->addr;
6581cf9926bSpatrick iep->secondLevelPagesSectionOffset = l2PagesOffset;
6591cf9926bSpatrick iep->lsdaIndexArraySectionOffset =
660*dfe94b16Srobert lsdaOffset + lsdaIndex.lookup(idx) *
6611cf9926bSpatrick sizeof(unwind_info_section_header_lsda_index_entry);
6621cf9926bSpatrick iep++;
6631cf9926bSpatrick l2PagesOffset += SECOND_LEVEL_PAGE_BYTES;
6641cf9926bSpatrick }
6651cf9926bSpatrick // Level-1 sentinel
666*dfe94b16Srobert // XXX(vyng): Note that LD64 adds +1 here.
667*dfe94b16Srobert // Unsure whether it's a bug or it's their workaround for something else.
668*dfe94b16Srobert // See comments from https://reviews.llvm.org/D138320.
669*dfe94b16Srobert iep->functionOffset = cueEndBoundary - in.header->addr;
6701cf9926bSpatrick iep->secondLevelPagesSectionOffset = 0;
6711cf9926bSpatrick iep->lsdaIndexArraySectionOffset =
672*dfe94b16Srobert lsdaOffset + entriesWithLsda.size() *
673*dfe94b16Srobert sizeof(unwind_info_section_header_lsda_index_entry);
6741cf9926bSpatrick iep++;
6751cf9926bSpatrick
6761cf9926bSpatrick // LSDAs
677*dfe94b16Srobert auto *lep =
678*dfe94b16Srobert reinterpret_cast<unwind_info_section_header_lsda_index_entry *>(iep);
679*dfe94b16Srobert for (size_t idx : entriesWithLsda) {
680*dfe94b16Srobert const CompactUnwindEntry &cu = cuEntries[idx];
681*dfe94b16Srobert lep->lsdaOffset = cu.lsda->getVA(/*off=*/0) - in.header->addr;
682*dfe94b16Srobert lep->functionOffset = cu.functionAddress - in.header->addr;
683*dfe94b16Srobert lep++;
684*dfe94b16Srobert }
6851cf9926bSpatrick
6861cf9926bSpatrick // Level-2 pages
687*dfe94b16Srobert auto *pp = reinterpret_cast<uint32_t *>(lep);
6881cf9926bSpatrick for (const SecondLevelPage &page : secondLevelPages) {
6891cf9926bSpatrick if (page.kind == UNWIND_SECOND_LEVEL_COMPRESSED) {
6901cf9926bSpatrick uintptr_t functionAddressBase =
691*dfe94b16Srobert cuEntries[cuIndices[page.entryIndex]].functionAddress;
6921cf9926bSpatrick auto *p2p =
6931cf9926bSpatrick reinterpret_cast<unwind_info_compressed_second_level_page_header *>(
6941cf9926bSpatrick pp);
6951cf9926bSpatrick p2p->kind = page.kind;
6961cf9926bSpatrick p2p->entryPageOffset =
6971cf9926bSpatrick sizeof(unwind_info_compressed_second_level_page_header);
6981cf9926bSpatrick p2p->entryCount = page.entryCount;
6991cf9926bSpatrick p2p->encodingsPageOffset =
7001cf9926bSpatrick p2p->entryPageOffset + p2p->entryCount * sizeof(uint32_t);
7011cf9926bSpatrick p2p->encodingsCount = page.localEncodings.size();
7021cf9926bSpatrick auto *ep = reinterpret_cast<uint32_t *>(&p2p[1]);
7031cf9926bSpatrick for (size_t i = 0; i < page.entryCount; i++) {
704*dfe94b16Srobert const CompactUnwindEntry &cue =
705*dfe94b16Srobert cuEntries[cuIndices[page.entryIndex + i]];
706*dfe94b16Srobert auto it = commonEncodingIndexes.find(cue.encoding);
7071cf9926bSpatrick if (it == commonEncodingIndexes.end())
708*dfe94b16Srobert it = page.localEncodingIndexes.find(cue.encoding);
7091cf9926bSpatrick *ep++ = (it->second << COMPRESSED_ENTRY_FUNC_OFFSET_BITS) |
710*dfe94b16Srobert (cue.functionAddress - functionAddressBase);
7111cf9926bSpatrick }
712*dfe94b16Srobert if (!page.localEncodings.empty())
7131cf9926bSpatrick memcpy(ep, page.localEncodings.data(),
7141cf9926bSpatrick page.localEncodings.size() * sizeof(uint32_t));
7151cf9926bSpatrick } else {
7161cf9926bSpatrick auto *p2p =
7171cf9926bSpatrick reinterpret_cast<unwind_info_regular_second_level_page_header *>(pp);
7181cf9926bSpatrick p2p->kind = page.kind;
7191cf9926bSpatrick p2p->entryPageOffset =
7201cf9926bSpatrick sizeof(unwind_info_regular_second_level_page_header);
7211cf9926bSpatrick p2p->entryCount = page.entryCount;
7221cf9926bSpatrick auto *ep = reinterpret_cast<uint32_t *>(&p2p[1]);
7231cf9926bSpatrick for (size_t i = 0; i < page.entryCount; i++) {
724*dfe94b16Srobert const CompactUnwindEntry &cue =
725*dfe94b16Srobert cuEntries[cuIndices[page.entryIndex + i]];
726*dfe94b16Srobert *ep++ = cue.functionAddress;
727*dfe94b16Srobert *ep++ = cue.encoding;
7281cf9926bSpatrick }
7291cf9926bSpatrick }
7301cf9926bSpatrick pp += SECOND_LEVEL_PAGE_WORDS;
7311cf9926bSpatrick }
7321cf9926bSpatrick }
7331cf9926bSpatrick
makeUnwindInfoSection()7341cf9926bSpatrick UnwindInfoSection *macho::makeUnwindInfoSection() {
735*dfe94b16Srobert return make<UnwindInfoSectionImpl>();
7361cf9926bSpatrick }
737