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