1e8d8bef9SDimitry Andric //===- UnwindInfoSection.cpp ----------------------------------------------===// 2e8d8bef9SDimitry Andric // 3e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6e8d8bef9SDimitry Andric // 7e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 8e8d8bef9SDimitry Andric 9e8d8bef9SDimitry Andric #include "UnwindInfoSection.h" 10fe6060f1SDimitry Andric #include "ConcatOutputSection.h" 11e8d8bef9SDimitry Andric #include "Config.h" 12e8d8bef9SDimitry Andric #include "InputSection.h" 13e8d8bef9SDimitry Andric #include "OutputSection.h" 14e8d8bef9SDimitry Andric #include "OutputSegment.h" 15fe6060f1SDimitry Andric #include "SymbolTable.h" 16e8d8bef9SDimitry Andric #include "Symbols.h" 17e8d8bef9SDimitry Andric #include "SyntheticSections.h" 18e8d8bef9SDimitry Andric #include "Target.h" 19e8d8bef9SDimitry Andric 20e8d8bef9SDimitry Andric #include "lld/Common/ErrorHandler.h" 21fe6060f1SDimitry Andric #include "lld/Common/Memory.h" 22349cc55cSDimitry Andric #include "llvm/ADT/DenseMap.h" 23fe6060f1SDimitry Andric #include "llvm/ADT/STLExtras.h" 24e8d8bef9SDimitry Andric #include "llvm/BinaryFormat/MachO.h" 25349cc55cSDimitry Andric #include "llvm/Support/Parallel.h" 26349cc55cSDimitry Andric 27349cc55cSDimitry Andric #include <numeric> 28e8d8bef9SDimitry Andric 29e8d8bef9SDimitry Andric using namespace llvm; 30e8d8bef9SDimitry Andric using namespace llvm::MachO; 3181ad6265SDimitry Andric using namespace llvm::support::endian; 32e8d8bef9SDimitry Andric using namespace lld; 33e8d8bef9SDimitry Andric using namespace lld::macho; 34e8d8bef9SDimitry Andric 35e8d8bef9SDimitry Andric #define COMMON_ENCODINGS_MAX 127 36e8d8bef9SDimitry Andric #define COMPACT_ENCODINGS_MAX 256 37e8d8bef9SDimitry Andric 38e8d8bef9SDimitry Andric #define SECOND_LEVEL_PAGE_BYTES 4096 39e8d8bef9SDimitry Andric #define SECOND_LEVEL_PAGE_WORDS (SECOND_LEVEL_PAGE_BYTES / sizeof(uint32_t)) 40e8d8bef9SDimitry Andric #define REGULAR_SECOND_LEVEL_ENTRIES_MAX \ 41e8d8bef9SDimitry Andric ((SECOND_LEVEL_PAGE_BYTES - \ 42e8d8bef9SDimitry Andric sizeof(unwind_info_regular_second_level_page_header)) / \ 43e8d8bef9SDimitry Andric sizeof(unwind_info_regular_second_level_entry)) 44e8d8bef9SDimitry Andric #define COMPRESSED_SECOND_LEVEL_ENTRIES_MAX \ 45e8d8bef9SDimitry Andric ((SECOND_LEVEL_PAGE_BYTES - \ 46e8d8bef9SDimitry Andric sizeof(unwind_info_compressed_second_level_page_header)) / \ 47e8d8bef9SDimitry Andric sizeof(uint32_t)) 48e8d8bef9SDimitry Andric 49e8d8bef9SDimitry Andric #define COMPRESSED_ENTRY_FUNC_OFFSET_BITS 24 50e8d8bef9SDimitry Andric #define COMPRESSED_ENTRY_FUNC_OFFSET_MASK \ 51e8d8bef9SDimitry Andric UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET(~0) 52e8d8bef9SDimitry Andric 53e8d8bef9SDimitry Andric // Compact Unwind format is a Mach-O evolution of DWARF Unwind that 54e8d8bef9SDimitry Andric // optimizes space and exception-time lookup. Most DWARF unwind 55e8d8bef9SDimitry Andric // entries can be replaced with Compact Unwind entries, but the ones 56e8d8bef9SDimitry Andric // that cannot are retained in DWARF form. 57e8d8bef9SDimitry Andric // 58e8d8bef9SDimitry Andric // This comment will address macro-level organization of the pre-link 59e8d8bef9SDimitry Andric // and post-link compact unwind tables. For micro-level organization 60e8d8bef9SDimitry Andric // pertaining to the bitfield layout of the 32-bit compact unwind 61e8d8bef9SDimitry Andric // entries, see libunwind/include/mach-o/compact_unwind_encoding.h 62e8d8bef9SDimitry Andric // 63e8d8bef9SDimitry Andric // Important clarifying factoids: 64e8d8bef9SDimitry Andric // 65e8d8bef9SDimitry Andric // * __LD,__compact_unwind is the compact unwind format for compiler 66e8d8bef9SDimitry Andric // output and linker input. It is never a final output. It could be 67e8d8bef9SDimitry Andric // an intermediate output with the `-r` option which retains relocs. 68e8d8bef9SDimitry Andric // 69e8d8bef9SDimitry Andric // * __TEXT,__unwind_info is the compact unwind format for final 70e8d8bef9SDimitry Andric // linker output. It is never an input. 71e8d8bef9SDimitry Andric // 72e8d8bef9SDimitry Andric // * __TEXT,__eh_frame is the DWARF format for both linker input and output. 73e8d8bef9SDimitry Andric // 74e8d8bef9SDimitry Andric // * __TEXT,__unwind_info entries are divided into 4 KiB pages (2nd 75e8d8bef9SDimitry Andric // level) by ascending address, and the pages are referenced by an 76e8d8bef9SDimitry Andric // index (1st level) in the section header. 77e8d8bef9SDimitry Andric // 78e8d8bef9SDimitry Andric // * Following the headers in __TEXT,__unwind_info, the bulk of the 79e8d8bef9SDimitry Andric // section contains a vector of compact unwind entries 80e8d8bef9SDimitry Andric // `{functionOffset, encoding}` sorted by ascending `functionOffset`. 81e8d8bef9SDimitry Andric // Adjacent entries with the same encoding can be folded to great 82e8d8bef9SDimitry Andric // advantage, achieving a 3-order-of-magnitude reduction in the 83e8d8bef9SDimitry Andric // number of entries. 84e8d8bef9SDimitry Andric // 85e8d8bef9SDimitry Andric // * The __TEXT,__unwind_info format can accommodate up to 127 unique 86e8d8bef9SDimitry Andric // encodings for the space-efficient compressed format. In practice, 87e8d8bef9SDimitry Andric // fewer than a dozen unique encodings are used by C++ programs of 88e8d8bef9SDimitry Andric // all sizes. Therefore, we don't even bother implementing the regular 89e8d8bef9SDimitry Andric // non-compressed format. Time will tell if anyone in the field ever 90e8d8bef9SDimitry Andric // overflows the 127-encodings limit. 91fe6060f1SDimitry Andric // 92fe6060f1SDimitry Andric // Refer to the definition of unwind_info_section_header in 93fe6060f1SDimitry Andric // compact_unwind_encoding.h for an overview of the format we are encoding 94fe6060f1SDimitry Andric // here. 95e8d8bef9SDimitry Andric 96fe6060f1SDimitry Andric // TODO(gkm): prune __eh_frame entries superseded by __unwind_info, PR50410 97e8d8bef9SDimitry Andric // TODO(gkm): how do we align the 2nd-level pages? 98e8d8bef9SDimitry Andric 9981ad6265SDimitry Andric // The offsets of various fields in the on-disk representation of each compact 10081ad6265SDimitry Andric // unwind entry. 10181ad6265SDimitry Andric struct CompactUnwindOffsets { 10281ad6265SDimitry Andric uint32_t functionAddress; 10381ad6265SDimitry Andric uint32_t functionLength; 10481ad6265SDimitry Andric uint32_t encoding; 10581ad6265SDimitry Andric uint32_t personality; 10681ad6265SDimitry Andric uint32_t lsda; 10781ad6265SDimitry Andric 10881ad6265SDimitry Andric CompactUnwindOffsets(size_t wordSize) { 10981ad6265SDimitry Andric if (wordSize == 8) 11081ad6265SDimitry Andric init<uint64_t>(); 11181ad6265SDimitry Andric else { 11281ad6265SDimitry Andric assert(wordSize == 4); 11381ad6265SDimitry Andric init<uint32_t>(); 11481ad6265SDimitry Andric } 11581ad6265SDimitry Andric } 11681ad6265SDimitry Andric 11781ad6265SDimitry Andric private: 11881ad6265SDimitry Andric template <class Ptr> void init() { 11981ad6265SDimitry Andric functionAddress = offsetof(Layout<Ptr>, functionAddress); 12081ad6265SDimitry Andric functionLength = offsetof(Layout<Ptr>, functionLength); 12181ad6265SDimitry Andric encoding = offsetof(Layout<Ptr>, encoding); 12281ad6265SDimitry Andric personality = offsetof(Layout<Ptr>, personality); 12381ad6265SDimitry Andric lsda = offsetof(Layout<Ptr>, lsda); 12481ad6265SDimitry Andric } 12581ad6265SDimitry Andric 12681ad6265SDimitry Andric template <class Ptr> struct Layout { 127349cc55cSDimitry Andric Ptr functionAddress; 128349cc55cSDimitry Andric uint32_t functionLength; 129349cc55cSDimitry Andric compact_unwind_encoding_t encoding; 130349cc55cSDimitry Andric Ptr personality; 131349cc55cSDimitry Andric Ptr lsda; 132349cc55cSDimitry Andric }; 13381ad6265SDimitry Andric }; 13481ad6265SDimitry Andric 13581ad6265SDimitry Andric // LLD's internal representation of a compact unwind entry. 13681ad6265SDimitry Andric struct CompactUnwindEntry { 13781ad6265SDimitry Andric uint64_t functionAddress; 13881ad6265SDimitry Andric uint32_t functionLength; 13981ad6265SDimitry Andric compact_unwind_encoding_t encoding; 14081ad6265SDimitry Andric Symbol *personality; 14181ad6265SDimitry Andric InputSection *lsda; 14281ad6265SDimitry Andric }; 143349cc55cSDimitry Andric 144fe6060f1SDimitry Andric using EncodingMap = DenseMap<compact_unwind_encoding_t, size_t>; 145fe6060f1SDimitry Andric 146fe6060f1SDimitry Andric struct SecondLevelPage { 147fe6060f1SDimitry Andric uint32_t kind; 148fe6060f1SDimitry Andric size_t entryIndex; 149fe6060f1SDimitry Andric size_t entryCount; 150fe6060f1SDimitry Andric size_t byteCount; 151fe6060f1SDimitry Andric std::vector<compact_unwind_encoding_t> localEncodings; 152fe6060f1SDimitry Andric EncodingMap localEncodingIndexes; 153fe6060f1SDimitry Andric }; 154fe6060f1SDimitry Andric 15581ad6265SDimitry Andric // UnwindInfoSectionImpl allows us to avoid cluttering our header file with a 15681ad6265SDimitry Andric // lengthy definition of UnwindInfoSection. 157fe6060f1SDimitry Andric class UnwindInfoSectionImpl final : public UnwindInfoSection { 158fe6060f1SDimitry Andric public: 15981ad6265SDimitry Andric UnwindInfoSectionImpl() : cuOffsets(target->wordSize) {} 16081ad6265SDimitry Andric uint64_t getSize() const override { return unwindInfoSize; } 16181ad6265SDimitry Andric void prepareRelocations() override; 162fe6060f1SDimitry Andric void finalize() override; 163fe6060f1SDimitry Andric void writeTo(uint8_t *buf) const override; 164fe6060f1SDimitry Andric 165fe6060f1SDimitry Andric private: 16681ad6265SDimitry Andric void prepareRelocations(ConcatInputSection *); 16781ad6265SDimitry Andric void relocateCompactUnwind(std::vector<CompactUnwindEntry> &); 16881ad6265SDimitry Andric void encodePersonalities(); 16981ad6265SDimitry Andric 17081ad6265SDimitry Andric uint64_t unwindInfoSize = 0; 17181ad6265SDimitry Andric std::vector<decltype(symbols)::value_type> symbolsVec; 17281ad6265SDimitry Andric CompactUnwindOffsets cuOffsets; 173fe6060f1SDimitry Andric std::vector<std::pair<compact_unwind_encoding_t, size_t>> commonEncodings; 174fe6060f1SDimitry Andric EncodingMap commonEncodingIndexes; 175349cc55cSDimitry Andric // The entries here will be in the same order as their originating symbols 176349cc55cSDimitry Andric // in symbolsVec. 17781ad6265SDimitry Andric std::vector<CompactUnwindEntry> cuEntries; 178349cc55cSDimitry Andric // Indices into the cuEntries vector. 179349cc55cSDimitry Andric std::vector<size_t> cuIndices; 18081ad6265SDimitry Andric std::vector<Symbol *> personalities; 181fe6060f1SDimitry Andric SmallDenseMap<std::pair<InputSection *, uint64_t /* addend */>, Symbol *> 182fe6060f1SDimitry Andric personalityTable; 183349cc55cSDimitry Andric // Indices into cuEntries for CUEs with a non-null LSDA. 184349cc55cSDimitry Andric std::vector<size_t> entriesWithLsda; 185349cc55cSDimitry Andric // Map of cuEntries index to an index within the LSDA array. 186349cc55cSDimitry Andric DenseMap<size_t, uint32_t> lsdaIndex; 187fe6060f1SDimitry Andric std::vector<SecondLevelPage> secondLevelPages; 188fe6060f1SDimitry Andric uint64_t level2PagesOffset = 0; 189fe6060f1SDimitry Andric }; 190fe6060f1SDimitry Andric 191e8d8bef9SDimitry Andric UnwindInfoSection::UnwindInfoSection() 192e8d8bef9SDimitry Andric : SyntheticSection(segment_names::text, section_names::unwindInfo) { 193fe6060f1SDimitry Andric align = 4; 194e8d8bef9SDimitry Andric } 195e8d8bef9SDimitry Andric 196349cc55cSDimitry Andric // Record function symbols that may need entries emitted in __unwind_info, which 197349cc55cSDimitry Andric // stores unwind data for address ranges. 198349cc55cSDimitry Andric // 199*6246ae0bSDimitry Andric // Note that if several adjacent functions have the same unwind encoding and 200*6246ae0bSDimitry Andric // personality function and no LSDA, they share one unwind entry. For this to 201*6246ae0bSDimitry Andric // work, functions without unwind info need explicit "no unwind info" unwind 202*6246ae0bSDimitry Andric // entries -- else the unwinder would think they have the unwind info of the 203*6246ae0bSDimitry Andric // closest function with unwind info right before in the image. Thus, we add 204*6246ae0bSDimitry Andric // function symbols for each unique address regardless of whether they have 205*6246ae0bSDimitry Andric // associated unwind info. 206349cc55cSDimitry Andric void UnwindInfoSection::addSymbol(const Defined *d) { 207349cc55cSDimitry Andric if (d->unwindEntry) 208349cc55cSDimitry Andric allEntriesAreOmitted = false; 209349cc55cSDimitry Andric // We don't yet know the final output address of this symbol, but we know that 210349cc55cSDimitry Andric // they are uniquely determined by a combination of the isec and value, so 211349cc55cSDimitry Andric // we use that as the key here. 212349cc55cSDimitry Andric auto p = symbols.insert({{d->isec, d->value}, d}); 213349cc55cSDimitry Andric // If we have multiple symbols at the same address, only one of them can have 214fcaf7f86SDimitry Andric // an associated unwind entry. 215349cc55cSDimitry Andric if (!p.second && d->unwindEntry) { 216349cc55cSDimitry Andric assert(!p.first->second->unwindEntry); 217349cc55cSDimitry Andric p.first->second = d; 218349cc55cSDimitry Andric } 219fe6060f1SDimitry Andric } 220fe6060f1SDimitry Andric 22181ad6265SDimitry Andric void UnwindInfoSectionImpl::prepareRelocations() { 22281ad6265SDimitry Andric // This iteration needs to be deterministic, since prepareRelocations may add 22381ad6265SDimitry Andric // entries to the GOT. Hence the use of a MapVector for 22481ad6265SDimitry Andric // UnwindInfoSection::symbols. 22581ad6265SDimitry Andric for (const Defined *d : make_second_range(symbols)) 22681ad6265SDimitry Andric if (d->unwindEntry && 22781ad6265SDimitry Andric d->unwindEntry->getName() == section_names::compactUnwind) 22881ad6265SDimitry Andric prepareRelocations(d->unwindEntry); 22981ad6265SDimitry Andric } 23081ad6265SDimitry Andric 231fe6060f1SDimitry Andric // Compact unwind relocations have different semantics, so we handle them in a 232fe6060f1SDimitry Andric // separate code path from regular relocations. First, we do not wish to add 233fe6060f1SDimitry Andric // rebase opcodes for __LD,__compact_unwind, because that section doesn't 234fe6060f1SDimitry Andric // actually end up in the final binary. Second, personality pointers always 235fe6060f1SDimitry Andric // reside in the GOT and must be treated specially. 23681ad6265SDimitry Andric void UnwindInfoSectionImpl::prepareRelocations(ConcatInputSection *isec) { 237fe6060f1SDimitry Andric assert(!isec->shouldOmitFromOutput() && 238fe6060f1SDimitry Andric "__compact_unwind section should not be omitted"); 239fe6060f1SDimitry Andric 240fe6060f1SDimitry Andric // FIXME: Make this skip relocations for CompactUnwindEntries that 241fe6060f1SDimitry Andric // point to dead-stripped functions. That might save some amount of 242fe6060f1SDimitry Andric // work. But since there are usually just few personality functions 243fe6060f1SDimitry Andric // that are referenced from many places, at least some of them likely 244fe6060f1SDimitry Andric // live, it wouldn't reduce number of got entries. 245fe6060f1SDimitry Andric for (size_t i = 0; i < isec->relocs.size(); ++i) { 246fe6060f1SDimitry Andric Reloc &r = isec->relocs[i]; 247fe6060f1SDimitry Andric assert(target->hasAttr(r.type, RelocAttrBits::UNSIGNED)); 248fe6060f1SDimitry Andric 249349cc55cSDimitry Andric // Functions and LSDA entries always reside in the same object file as the 250349cc55cSDimitry Andric // compact unwind entries that references them, and thus appear as section 251349cc55cSDimitry Andric // relocs. There is no need to prepare them. We only prepare relocs for 252349cc55cSDimitry Andric // personality functions. 25381ad6265SDimitry Andric if (r.offset != cuOffsets.personality) 254fe6060f1SDimitry Andric continue; 255fe6060f1SDimitry Andric 256fe6060f1SDimitry Andric if (auto *s = r.referent.dyn_cast<Symbol *>()) { 257349cc55cSDimitry Andric // Personality functions are nearly always system-defined (e.g., 258349cc55cSDimitry Andric // ___gxx_personality_v0 for C++) and relocated as dylib symbols. When an 259349cc55cSDimitry Andric // application provides its own personality function, it might be 260349cc55cSDimitry Andric // referenced by an extern Defined symbol reloc, or a local section reloc. 261349cc55cSDimitry Andric if (auto *defined = dyn_cast<Defined>(s)) { 262349cc55cSDimitry Andric // XXX(vyng) This is a a special case for handling duplicate personality 263349cc55cSDimitry Andric // symbols. Note that LD64's behavior is a bit different and it is 264349cc55cSDimitry Andric // inconsistent with how symbol resolution usually work 265349cc55cSDimitry Andric // 266349cc55cSDimitry Andric // So we've decided not to follow it. Instead, simply pick the symbol 267349cc55cSDimitry Andric // with the same name from the symbol table to replace the local one. 268349cc55cSDimitry Andric // 269349cc55cSDimitry Andric // (See discussions/alternatives already considered on D107533) 270349cc55cSDimitry Andric if (!defined->isExternal()) 2714824e7fdSDimitry Andric if (Symbol *sym = symtab->find(defined->getName())) 27204eeddc0SDimitry Andric if (!sym->isLazy()) 2734824e7fdSDimitry Andric r.referent = s = sym; 274349cc55cSDimitry Andric } 275fe6060f1SDimitry Andric if (auto *undefined = dyn_cast<Undefined>(s)) { 27681ad6265SDimitry Andric treatUndefinedSymbol(*undefined, isec, r.offset); 277fe6060f1SDimitry Andric // treatUndefinedSymbol() can replace s with a DylibSymbol; re-check. 278fe6060f1SDimitry Andric if (isa<Undefined>(s)) 279fe6060f1SDimitry Andric continue; 280fe6060f1SDimitry Andric } 281349cc55cSDimitry Andric 282fe6060f1SDimitry Andric if (auto *defined = dyn_cast<Defined>(s)) { 283fe6060f1SDimitry Andric // Check if we have created a synthetic symbol at the same address. 284fe6060f1SDimitry Andric Symbol *&personality = 285fe6060f1SDimitry Andric personalityTable[{defined->isec, defined->value}]; 286fe6060f1SDimitry Andric if (personality == nullptr) { 287fe6060f1SDimitry Andric personality = defined; 288fe6060f1SDimitry Andric in.got->addEntry(defined); 289fe6060f1SDimitry Andric } else if (personality != defined) { 290fe6060f1SDimitry Andric r.referent = personality; 291fe6060f1SDimitry Andric } 292fe6060f1SDimitry Andric continue; 293fe6060f1SDimitry Andric } 294fe6060f1SDimitry Andric assert(isa<DylibSymbol>(s)); 295fe6060f1SDimitry Andric in.got->addEntry(s); 296fe6060f1SDimitry Andric continue; 297fe6060f1SDimitry Andric } 298fe6060f1SDimitry Andric 299fe6060f1SDimitry Andric if (auto *referentIsec = r.referent.dyn_cast<InputSection *>()) { 300fe6060f1SDimitry Andric assert(!isCoalescedWeak(referentIsec)); 301fe6060f1SDimitry Andric // Personality functions can be referenced via section relocations 302fe6060f1SDimitry Andric // if they live in the same object file. Create placeholder synthetic 303fe6060f1SDimitry Andric // symbols for them in the GOT. 304fe6060f1SDimitry Andric Symbol *&s = personalityTable[{referentIsec, r.addend}]; 305fe6060f1SDimitry Andric if (s == nullptr) { 306fe6060f1SDimitry Andric // This runs after dead stripping, so the noDeadStrip argument does not 307fe6060f1SDimitry Andric // matter. 308fe6060f1SDimitry Andric s = make<Defined>("<internal>", /*file=*/nullptr, referentIsec, 309fe6060f1SDimitry Andric r.addend, /*size=*/0, /*isWeakDef=*/false, 310fe6060f1SDimitry Andric /*isExternal=*/false, /*isPrivateExtern=*/false, 31181ad6265SDimitry Andric /*includeInSymtab=*/true, 312fe6060f1SDimitry Andric /*isThumb=*/false, /*isReferencedDynamically=*/false, 313fe6060f1SDimitry Andric /*noDeadStrip=*/false); 31481ad6265SDimitry Andric s->used = true; 315fe6060f1SDimitry Andric in.got->addEntry(s); 316fe6060f1SDimitry Andric } 317fe6060f1SDimitry Andric r.referent = s; 318fe6060f1SDimitry Andric r.addend = 0; 319fe6060f1SDimitry Andric } 320fe6060f1SDimitry Andric } 321fe6060f1SDimitry Andric } 322fe6060f1SDimitry Andric 323fe6060f1SDimitry Andric // We need to apply the relocations to the pre-link compact unwind section 324fe6060f1SDimitry Andric // before converting it to post-link form. There should only be absolute 325fe6060f1SDimitry Andric // relocations here: since we are not emitting the pre-link CU section, there 326fe6060f1SDimitry Andric // is no source address to make a relative location meaningful. 32781ad6265SDimitry Andric void UnwindInfoSectionImpl::relocateCompactUnwind( 32881ad6265SDimitry Andric std::vector<CompactUnwindEntry> &cuEntries) { 32981ad6265SDimitry Andric parallelFor(0, symbolsVec.size(), [&](size_t i) { 33081ad6265SDimitry Andric CompactUnwindEntry &cu = cuEntries[i]; 331349cc55cSDimitry Andric const Defined *d = symbolsVec[i].second; 33281ad6265SDimitry Andric cu.functionAddress = d->getVA(); 333349cc55cSDimitry Andric if (!d->unwindEntry) 334349cc55cSDimitry Andric return; 335fe6060f1SDimitry Andric 33681ad6265SDimitry Andric // If we have DWARF unwind info, create a CU entry that points to it. 33781ad6265SDimitry Andric if (d->unwindEntry->getName() == section_names::ehFrame) { 33881ad6265SDimitry Andric cu.encoding = target->modeDwarfEncoding | d->unwindEntry->outSecOff; 33981ad6265SDimitry Andric const FDE &fde = cast<ObjFile>(d->getFile())->fdes[d->unwindEntry]; 34081ad6265SDimitry Andric cu.functionLength = fde.funcLength; 34181ad6265SDimitry Andric cu.personality = fde.personality; 34281ad6265SDimitry Andric cu.lsda = fde.lsda; 34381ad6265SDimitry Andric return; 34481ad6265SDimitry Andric } 34581ad6265SDimitry Andric 34681ad6265SDimitry Andric assert(d->unwindEntry->getName() == section_names::compactUnwind); 34781ad6265SDimitry Andric 34881ad6265SDimitry Andric auto buf = reinterpret_cast<const uint8_t *>(d->unwindEntry->data.data()) - 34981ad6265SDimitry Andric target->wordSize; 35081ad6265SDimitry Andric cu.functionLength = 35181ad6265SDimitry Andric support::endian::read32le(buf + cuOffsets.functionLength); 35281ad6265SDimitry Andric cu.encoding = support::endian::read32le(buf + cuOffsets.encoding); 353349cc55cSDimitry Andric for (const Reloc &r : d->unwindEntry->relocs) { 35481ad6265SDimitry Andric if (r.offset == cuOffsets.personality) { 35581ad6265SDimitry Andric cu.personality = r.referent.get<Symbol *>(); 35681ad6265SDimitry Andric } else if (r.offset == cuOffsets.lsda) { 35781ad6265SDimitry Andric if (auto *referentSym = r.referent.dyn_cast<Symbol *>()) 35881ad6265SDimitry Andric cu.lsda = cast<Defined>(referentSym)->isec; 35981ad6265SDimitry Andric else 36081ad6265SDimitry Andric cu.lsda = r.referent.get<InputSection *>(); 361fe6060f1SDimitry Andric } 362fe6060f1SDimitry Andric } 363349cc55cSDimitry Andric }); 364fe6060f1SDimitry Andric } 365fe6060f1SDimitry Andric 366fe6060f1SDimitry Andric // There should only be a handful of unique personality pointers, so we can 367fe6060f1SDimitry Andric // encode them as 2-bit indices into a small array. 36881ad6265SDimitry Andric void UnwindInfoSectionImpl::encodePersonalities() { 369349cc55cSDimitry Andric for (size_t idx : cuIndices) { 37081ad6265SDimitry Andric CompactUnwindEntry &cu = cuEntries[idx]; 37181ad6265SDimitry Andric if (cu.personality == nullptr) 372fe6060f1SDimitry Andric continue; 373fe6060f1SDimitry Andric // Linear search is fast enough for a small array. 374349cc55cSDimitry Andric auto it = find(personalities, cu.personality); 375fe6060f1SDimitry Andric uint32_t personalityIndex; // 1-based index 376fe6060f1SDimitry Andric if (it != personalities.end()) { 377fe6060f1SDimitry Andric personalityIndex = std::distance(personalities.begin(), it) + 1; 378fe6060f1SDimitry Andric } else { 379349cc55cSDimitry Andric personalities.push_back(cu.personality); 380fe6060f1SDimitry Andric personalityIndex = personalities.size(); 381fe6060f1SDimitry Andric } 382349cc55cSDimitry Andric cu.encoding |= 383fe6060f1SDimitry Andric personalityIndex << countTrailingZeros( 384fe6060f1SDimitry Andric static_cast<compact_unwind_encoding_t>(UNWIND_PERSONALITY_MASK)); 385fe6060f1SDimitry Andric } 386fe6060f1SDimitry Andric if (personalities.size() > 3) 38781ad6265SDimitry Andric error("too many personalities (" + Twine(personalities.size()) + 388fe6060f1SDimitry Andric ") for compact unwind to encode"); 389fe6060f1SDimitry Andric } 390fe6060f1SDimitry Andric 391fe6060f1SDimitry Andric static bool canFoldEncoding(compact_unwind_encoding_t encoding) { 392fe6060f1SDimitry Andric // From compact_unwind_encoding.h: 393fe6060f1SDimitry Andric // UNWIND_X86_64_MODE_STACK_IND: 394fe6060f1SDimitry Andric // A "frameless" (RBP not used as frame pointer) function large constant 395fe6060f1SDimitry Andric // stack size. This case is like the previous, except the stack size is too 396fe6060f1SDimitry Andric // large to encode in the compact unwind encoding. Instead it requires that 397fe6060f1SDimitry Andric // the function contains "subq $nnnnnnnn,RSP" in its prolog. The compact 398fe6060f1SDimitry Andric // encoding contains the offset to the nnnnnnnn value in the function in 399fe6060f1SDimitry Andric // UNWIND_X86_64_FRAMELESS_STACK_SIZE. 400fe6060f1SDimitry Andric // Since this means the unwinder has to look at the `subq` in the function 401fe6060f1SDimitry Andric // of the unwind info's unwind address, two functions that have identical 402fe6060f1SDimitry Andric // unwind info can't be folded if it's using this encoding since both 403fe6060f1SDimitry Andric // entries need unique addresses. 40461cfbce3SDimitry Andric static_assert(static_cast<uint32_t>(UNWIND_X86_64_MODE_MASK) == 40561cfbce3SDimitry Andric static_cast<uint32_t>(UNWIND_X86_MODE_MASK), 40661cfbce3SDimitry Andric ""); 40761cfbce3SDimitry Andric static_assert(static_cast<uint32_t>(UNWIND_X86_64_MODE_STACK_IND) == 40861cfbce3SDimitry Andric static_cast<uint32_t>(UNWIND_X86_MODE_STACK_IND), 40961cfbce3SDimitry Andric ""); 410fe6060f1SDimitry Andric if ((target->cpuType == CPU_TYPE_X86_64 || target->cpuType == CPU_TYPE_X86) && 411fe6060f1SDimitry Andric (encoding & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_STACK_IND) { 412fe6060f1SDimitry Andric // FIXME: Consider passing in the two function addresses and getting 413fe6060f1SDimitry Andric // their two stack sizes off the `subq` and only returning false if they're 414fe6060f1SDimitry Andric // actually different. 415fe6060f1SDimitry Andric return false; 416fe6060f1SDimitry Andric } 417fe6060f1SDimitry Andric return true; 418e8d8bef9SDimitry Andric } 419e8d8bef9SDimitry Andric 420e8d8bef9SDimitry Andric // Scan the __LD,__compact_unwind entries and compute the space needs of 4211fd87a68SDimitry Andric // __TEXT,__unwind_info and __TEXT,__eh_frame. 42281ad6265SDimitry Andric void UnwindInfoSectionImpl::finalize() { 423349cc55cSDimitry Andric if (symbols.empty()) 424e8d8bef9SDimitry Andric return; 425e8d8bef9SDimitry Andric 426e8d8bef9SDimitry Andric // At this point, the address space for __TEXT,__text has been 427e8d8bef9SDimitry Andric // assigned, so we can relocate the __LD,__compact_unwind entries 428e8d8bef9SDimitry Andric // into a temporary buffer. Relocation is necessary in order to sort 429e8d8bef9SDimitry Andric // the CU entries by function address. Sorting is necessary so that 430*6246ae0bSDimitry Andric // we can fold adjacent CU entries with identical encoding+personality 431*6246ae0bSDimitry Andric // and without any LSDA. Folding is necessary because it reduces the 432*6246ae0bSDimitry Andric // number of CU entries by as much as 3 orders of magnitude! 433349cc55cSDimitry Andric cuEntries.resize(symbols.size()); 434349cc55cSDimitry Andric // The "map" part of the symbols MapVector was only needed for deduplication 435349cc55cSDimitry Andric // in addSymbol(). Now that we are done adding, move the contents to a plain 436349cc55cSDimitry Andric // std::vector for indexed access. 437349cc55cSDimitry Andric symbolsVec = symbols.takeVector(); 438349cc55cSDimitry Andric relocateCompactUnwind(cuEntries); 439e8d8bef9SDimitry Andric 440e8d8bef9SDimitry Andric // Rather than sort & fold the 32-byte entries directly, we create a 441349cc55cSDimitry Andric // vector of indices to entries and sort & fold that instead. 442349cc55cSDimitry Andric cuIndices.resize(cuEntries.size()); 443349cc55cSDimitry Andric std::iota(cuIndices.begin(), cuIndices.end(), 0); 444349cc55cSDimitry Andric llvm::sort(cuIndices, [&](size_t a, size_t b) { 445349cc55cSDimitry Andric return cuEntries[a].functionAddress < cuEntries[b].functionAddress; 446e8d8bef9SDimitry Andric }); 447e8d8bef9SDimitry Andric 448*6246ae0bSDimitry Andric // Fold adjacent entries with matching encoding+personality and without LSDA 449349cc55cSDimitry Andric // We use three iterators on the same cuIndices to fold in-situ: 450e8d8bef9SDimitry Andric // (1) `foldBegin` is the first of a potential sequence of matching entries 451e8d8bef9SDimitry Andric // (2) `foldEnd` is the first non-matching entry after `foldBegin`. 452e8d8bef9SDimitry Andric // The semi-open interval [ foldBegin .. foldEnd ) contains a range 453e8d8bef9SDimitry Andric // entries that can be folded into a single entry and written to ... 454e8d8bef9SDimitry Andric // (3) `foldWrite` 455349cc55cSDimitry Andric auto foldWrite = cuIndices.begin(); 456349cc55cSDimitry Andric for (auto foldBegin = cuIndices.begin(); foldBegin < cuIndices.end();) { 457e8d8bef9SDimitry Andric auto foldEnd = foldBegin; 458*6246ae0bSDimitry Andric // Common LSDA encodings (e.g. for C++ and Objective-C) contain offsets from 459*6246ae0bSDimitry Andric // a base address. The base address is normally not contained directly in 460*6246ae0bSDimitry Andric // the LSDA, and in that case, the personality function treats the starting 461*6246ae0bSDimitry Andric // address of the function (which is computed by the unwinder) as the base 462*6246ae0bSDimitry Andric // address and interprets the LSDA accordingly. The unwinder computes the 463*6246ae0bSDimitry Andric // starting address of a function as the address associated with its CU 464*6246ae0bSDimitry Andric // entry. For this reason, we cannot fold adjacent entries if they have an 465*6246ae0bSDimitry Andric // LSDA, because folding would make the unwinder compute the wrong starting 466*6246ae0bSDimitry Andric // address for the functions with the folded entries, which in turn would 467*6246ae0bSDimitry Andric // cause the personality function to misinterpret the LSDA for those 468*6246ae0bSDimitry Andric // functions. In the very rare case where the base address is encoded 469*6246ae0bSDimitry Andric // directly in the LSDA, two functions at different addresses would 470*6246ae0bSDimitry Andric // necessarily have different LSDAs, so their CU entries would not have been 471*6246ae0bSDimitry Andric // folded anyway. 472349cc55cSDimitry Andric while (++foldEnd < cuIndices.end() && 473349cc55cSDimitry Andric cuEntries[*foldBegin].encoding == cuEntries[*foldEnd].encoding && 474*6246ae0bSDimitry Andric !cuEntries[*foldBegin].lsda && !cuEntries[*foldEnd].lsda && 475*6246ae0bSDimitry Andric // If we've gotten to this point, we don't have an LSDA, which should 476*6246ae0bSDimitry Andric // also imply that we don't have a personality function, since in all 477*6246ae0bSDimitry Andric // likelihood a personality function needs the LSDA to do anything 478*6246ae0bSDimitry Andric // useful. It can be technically valid to have a personality function 479*6246ae0bSDimitry Andric // and no LSDA though (e.g. the C++ personality __gxx_personality_v0 480*6246ae0bSDimitry Andric // is just a no-op without LSDA), so we still check for personality 481*6246ae0bSDimitry Andric // function equivalence to handle that case. 482349cc55cSDimitry Andric cuEntries[*foldBegin].personality == 483349cc55cSDimitry Andric cuEntries[*foldEnd].personality && 48481ad6265SDimitry Andric canFoldEncoding(cuEntries[*foldEnd].encoding)) 48581ad6265SDimitry Andric ; 486e8d8bef9SDimitry Andric *foldWrite++ = *foldBegin; 487e8d8bef9SDimitry Andric foldBegin = foldEnd; 488e8d8bef9SDimitry Andric } 489349cc55cSDimitry Andric cuIndices.erase(foldWrite, cuIndices.end()); 490e8d8bef9SDimitry Andric 491349cc55cSDimitry Andric encodePersonalities(); 492fe6060f1SDimitry Andric 493e8d8bef9SDimitry Andric // Count frequencies of the folded encodings 494e8d8bef9SDimitry Andric EncodingMap encodingFrequencies; 495349cc55cSDimitry Andric for (size_t idx : cuIndices) 496349cc55cSDimitry Andric encodingFrequencies[cuEntries[idx].encoding]++; 497e8d8bef9SDimitry Andric 498e8d8bef9SDimitry Andric // Make a vector of encodings, sorted by descending frequency 499e8d8bef9SDimitry Andric for (const auto &frequency : encodingFrequencies) 500e8d8bef9SDimitry Andric commonEncodings.emplace_back(frequency); 501fe6060f1SDimitry Andric llvm::sort(commonEncodings, 502e8d8bef9SDimitry Andric [](const std::pair<compact_unwind_encoding_t, size_t> &a, 503e8d8bef9SDimitry Andric const std::pair<compact_unwind_encoding_t, size_t> &b) { 504e8d8bef9SDimitry Andric if (a.second == b.second) 505e8d8bef9SDimitry Andric // When frequencies match, secondarily sort on encoding 506e8d8bef9SDimitry Andric // to maintain parity with validate-unwind-info.py 507e8d8bef9SDimitry Andric return a.first > b.first; 508e8d8bef9SDimitry Andric return a.second > b.second; 509e8d8bef9SDimitry Andric }); 510e8d8bef9SDimitry Andric 511e8d8bef9SDimitry Andric // Truncate the vector to 127 elements. 512e8d8bef9SDimitry Andric // Common encoding indexes are limited to 0..126, while encoding 513e8d8bef9SDimitry Andric // indexes 127..255 are local to each second-level page 514e8d8bef9SDimitry Andric if (commonEncodings.size() > COMMON_ENCODINGS_MAX) 515e8d8bef9SDimitry Andric commonEncodings.resize(COMMON_ENCODINGS_MAX); 516e8d8bef9SDimitry Andric 517e8d8bef9SDimitry Andric // Create a map from encoding to common-encoding-table index 518e8d8bef9SDimitry Andric for (size_t i = 0; i < commonEncodings.size(); i++) 519e8d8bef9SDimitry Andric commonEncodingIndexes[commonEncodings[i].first] = i; 520e8d8bef9SDimitry Andric 521e8d8bef9SDimitry Andric // Split folded encodings into pages, where each page is limited by ... 522e8d8bef9SDimitry Andric // (a) 4 KiB capacity 523e8d8bef9SDimitry Andric // (b) 24-bit difference between first & final function address 524e8d8bef9SDimitry Andric // (c) 8-bit compact-encoding-table index, 525e8d8bef9SDimitry Andric // for which 0..126 references the global common-encodings table, 526e8d8bef9SDimitry Andric // and 127..255 references a local per-second-level-page table. 527e8d8bef9SDimitry Andric // First we try the compact format and determine how many entries fit. 528e8d8bef9SDimitry Andric // If more entries fit in the regular format, we use that. 529349cc55cSDimitry Andric for (size_t i = 0; i < cuIndices.size();) { 530349cc55cSDimitry Andric size_t idx = cuIndices[i]; 531e8d8bef9SDimitry Andric secondLevelPages.emplace_back(); 532fe6060f1SDimitry Andric SecondLevelPage &page = secondLevelPages.back(); 533e8d8bef9SDimitry Andric page.entryIndex = i; 534753f127fSDimitry Andric uint64_t functionAddressMax = 535349cc55cSDimitry Andric cuEntries[idx].functionAddress + COMPRESSED_ENTRY_FUNC_OFFSET_MASK; 536e8d8bef9SDimitry Andric size_t n = commonEncodings.size(); 537e8d8bef9SDimitry Andric size_t wordsRemaining = 538e8d8bef9SDimitry Andric SECOND_LEVEL_PAGE_WORDS - 539e8d8bef9SDimitry Andric sizeof(unwind_info_compressed_second_level_page_header) / 540e8d8bef9SDimitry Andric sizeof(uint32_t); 541349cc55cSDimitry Andric while (wordsRemaining >= 1 && i < cuIndices.size()) { 542349cc55cSDimitry Andric idx = cuIndices[i]; 54381ad6265SDimitry Andric const CompactUnwindEntry *cuPtr = &cuEntries[idx]; 544e8d8bef9SDimitry Andric if (cuPtr->functionAddress >= functionAddressMax) { 545e8d8bef9SDimitry Andric break; 546e8d8bef9SDimitry Andric } else if (commonEncodingIndexes.count(cuPtr->encoding) || 547e8d8bef9SDimitry Andric page.localEncodingIndexes.count(cuPtr->encoding)) { 548e8d8bef9SDimitry Andric i++; 549e8d8bef9SDimitry Andric wordsRemaining--; 550e8d8bef9SDimitry Andric } else if (wordsRemaining >= 2 && n < COMPACT_ENCODINGS_MAX) { 551e8d8bef9SDimitry Andric page.localEncodings.emplace_back(cuPtr->encoding); 552e8d8bef9SDimitry Andric page.localEncodingIndexes[cuPtr->encoding] = n++; 553e8d8bef9SDimitry Andric i++; 554e8d8bef9SDimitry Andric wordsRemaining -= 2; 555e8d8bef9SDimitry Andric } else { 556e8d8bef9SDimitry Andric break; 557e8d8bef9SDimitry Andric } 558e8d8bef9SDimitry Andric } 559e8d8bef9SDimitry Andric page.entryCount = i - page.entryIndex; 560e8d8bef9SDimitry Andric 561e8d8bef9SDimitry Andric // If this is not the final page, see if it's possible to fit more 562e8d8bef9SDimitry Andric // entries by using the regular format. This can happen when there 563e8d8bef9SDimitry Andric // are many unique encodings, and we we saturated the local 564e8d8bef9SDimitry Andric // encoding table early. 565349cc55cSDimitry Andric if (i < cuIndices.size() && 566e8d8bef9SDimitry Andric page.entryCount < REGULAR_SECOND_LEVEL_ENTRIES_MAX) { 567e8d8bef9SDimitry Andric page.kind = UNWIND_SECOND_LEVEL_REGULAR; 568e8d8bef9SDimitry Andric page.entryCount = std::min(REGULAR_SECOND_LEVEL_ENTRIES_MAX, 569349cc55cSDimitry Andric cuIndices.size() - page.entryIndex); 570e8d8bef9SDimitry Andric i = page.entryIndex + page.entryCount; 571e8d8bef9SDimitry Andric } else { 572e8d8bef9SDimitry Andric page.kind = UNWIND_SECOND_LEVEL_COMPRESSED; 573e8d8bef9SDimitry Andric } 574e8d8bef9SDimitry Andric } 575e8d8bef9SDimitry Andric 576349cc55cSDimitry Andric for (size_t idx : cuIndices) { 577349cc55cSDimitry Andric lsdaIndex[idx] = entriesWithLsda.size(); 57881ad6265SDimitry Andric if (cuEntries[idx].lsda) 579349cc55cSDimitry Andric entriesWithLsda.push_back(idx); 580fe6060f1SDimitry Andric } 581fe6060f1SDimitry Andric 582e8d8bef9SDimitry Andric // compute size of __TEXT,__unwind_info section 583349cc55cSDimitry Andric level2PagesOffset = sizeof(unwind_info_section_header) + 584e8d8bef9SDimitry Andric commonEncodings.size() * sizeof(uint32_t) + 585e8d8bef9SDimitry Andric personalities.size() * sizeof(uint32_t) + 586e8d8bef9SDimitry Andric // The extra second-level-page entry is for the sentinel 587e8d8bef9SDimitry Andric (secondLevelPages.size() + 1) * 588e8d8bef9SDimitry Andric sizeof(unwind_info_section_header_index_entry) + 589349cc55cSDimitry Andric entriesWithLsda.size() * 590349cc55cSDimitry Andric sizeof(unwind_info_section_header_lsda_index_entry); 591e8d8bef9SDimitry Andric unwindInfoSize = 592e8d8bef9SDimitry Andric level2PagesOffset + secondLevelPages.size() * SECOND_LEVEL_PAGE_BYTES; 593e8d8bef9SDimitry Andric } 594e8d8bef9SDimitry Andric 595e8d8bef9SDimitry Andric // All inputs are relocated and output addresses are known, so write! 596e8d8bef9SDimitry Andric 59781ad6265SDimitry Andric void UnwindInfoSectionImpl::writeTo(uint8_t *buf) const { 598349cc55cSDimitry Andric assert(!cuIndices.empty() && "call only if there is unwind info"); 599fe6060f1SDimitry Andric 600e8d8bef9SDimitry Andric // section header 601e8d8bef9SDimitry Andric auto *uip = reinterpret_cast<unwind_info_section_header *>(buf); 602e8d8bef9SDimitry Andric uip->version = 1; 603e8d8bef9SDimitry Andric uip->commonEncodingsArraySectionOffset = sizeof(unwind_info_section_header); 604e8d8bef9SDimitry Andric uip->commonEncodingsArrayCount = commonEncodings.size(); 605e8d8bef9SDimitry Andric uip->personalityArraySectionOffset = 606e8d8bef9SDimitry Andric uip->commonEncodingsArraySectionOffset + 607e8d8bef9SDimitry Andric (uip->commonEncodingsArrayCount * sizeof(uint32_t)); 608e8d8bef9SDimitry Andric uip->personalityArrayCount = personalities.size(); 609e8d8bef9SDimitry Andric uip->indexSectionOffset = uip->personalityArraySectionOffset + 610e8d8bef9SDimitry Andric (uip->personalityArrayCount * sizeof(uint32_t)); 611e8d8bef9SDimitry Andric uip->indexCount = secondLevelPages.size() + 1; 612e8d8bef9SDimitry Andric 613e8d8bef9SDimitry Andric // Common encodings 614e8d8bef9SDimitry Andric auto *i32p = reinterpret_cast<uint32_t *>(&uip[1]); 615e8d8bef9SDimitry Andric for (const auto &encoding : commonEncodings) 616e8d8bef9SDimitry Andric *i32p++ = encoding.first; 617e8d8bef9SDimitry Andric 618e8d8bef9SDimitry Andric // Personalities 61981ad6265SDimitry Andric for (const Symbol *personality : personalities) 62081ad6265SDimitry Andric *i32p++ = personality->getGotVA() - in.header->addr; 621e8d8bef9SDimitry Andric 622e8d8bef9SDimitry Andric // Level-1 index 623e8d8bef9SDimitry Andric uint32_t lsdaOffset = 624e8d8bef9SDimitry Andric uip->indexSectionOffset + 625e8d8bef9SDimitry Andric uip->indexCount * sizeof(unwind_info_section_header_index_entry); 626e8d8bef9SDimitry Andric uint64_t l2PagesOffset = level2PagesOffset; 627e8d8bef9SDimitry Andric auto *iep = reinterpret_cast<unwind_info_section_header_index_entry *>(i32p); 628e8d8bef9SDimitry Andric for (const SecondLevelPage &page : secondLevelPages) { 629349cc55cSDimitry Andric size_t idx = cuIndices[page.entryIndex]; 630349cc55cSDimitry Andric iep->functionOffset = cuEntries[idx].functionAddress - in.header->addr; 631e8d8bef9SDimitry Andric iep->secondLevelPagesSectionOffset = l2PagesOffset; 632fe6060f1SDimitry Andric iep->lsdaIndexArraySectionOffset = 633349cc55cSDimitry Andric lsdaOffset + lsdaIndex.lookup(idx) * 634fe6060f1SDimitry Andric sizeof(unwind_info_section_header_lsda_index_entry); 635e8d8bef9SDimitry Andric iep++; 636e8d8bef9SDimitry Andric l2PagesOffset += SECOND_LEVEL_PAGE_BYTES; 637e8d8bef9SDimitry Andric } 638e8d8bef9SDimitry Andric // Level-1 sentinel 63981ad6265SDimitry Andric const CompactUnwindEntry &cuEnd = cuEntries[cuIndices.back()]; 640fe6060f1SDimitry Andric iep->functionOffset = 641fe6060f1SDimitry Andric cuEnd.functionAddress - in.header->addr + cuEnd.functionLength; 642e8d8bef9SDimitry Andric iep->secondLevelPagesSectionOffset = 0; 643fe6060f1SDimitry Andric iep->lsdaIndexArraySectionOffset = 644349cc55cSDimitry Andric lsdaOffset + entriesWithLsda.size() * 645349cc55cSDimitry Andric sizeof(unwind_info_section_header_lsda_index_entry); 646e8d8bef9SDimitry Andric iep++; 647e8d8bef9SDimitry Andric 648e8d8bef9SDimitry Andric // LSDAs 649349cc55cSDimitry Andric auto *lep = 650349cc55cSDimitry Andric reinterpret_cast<unwind_info_section_header_lsda_index_entry *>(iep); 651349cc55cSDimitry Andric for (size_t idx : entriesWithLsda) { 65281ad6265SDimitry Andric const CompactUnwindEntry &cu = cuEntries[idx]; 65381ad6265SDimitry Andric lep->lsdaOffset = cu.lsda->getVA(/*off=*/0) - in.header->addr; 654349cc55cSDimitry Andric lep->functionOffset = cu.functionAddress - in.header->addr; 655349cc55cSDimitry Andric lep++; 656349cc55cSDimitry Andric } 657e8d8bef9SDimitry Andric 658e8d8bef9SDimitry Andric // Level-2 pages 659349cc55cSDimitry Andric auto *pp = reinterpret_cast<uint32_t *>(lep); 660e8d8bef9SDimitry Andric for (const SecondLevelPage &page : secondLevelPages) { 661e8d8bef9SDimitry Andric if (page.kind == UNWIND_SECOND_LEVEL_COMPRESSED) { 662e8d8bef9SDimitry Andric uintptr_t functionAddressBase = 663349cc55cSDimitry Andric cuEntries[cuIndices[page.entryIndex]].functionAddress; 664e8d8bef9SDimitry Andric auto *p2p = 665e8d8bef9SDimitry Andric reinterpret_cast<unwind_info_compressed_second_level_page_header *>( 666e8d8bef9SDimitry Andric pp); 667e8d8bef9SDimitry Andric p2p->kind = page.kind; 668e8d8bef9SDimitry Andric p2p->entryPageOffset = 669e8d8bef9SDimitry Andric sizeof(unwind_info_compressed_second_level_page_header); 670e8d8bef9SDimitry Andric p2p->entryCount = page.entryCount; 671e8d8bef9SDimitry Andric p2p->encodingsPageOffset = 672e8d8bef9SDimitry Andric p2p->entryPageOffset + p2p->entryCount * sizeof(uint32_t); 673e8d8bef9SDimitry Andric p2p->encodingsCount = page.localEncodings.size(); 674e8d8bef9SDimitry Andric auto *ep = reinterpret_cast<uint32_t *>(&p2p[1]); 675e8d8bef9SDimitry Andric for (size_t i = 0; i < page.entryCount; i++) { 67681ad6265SDimitry Andric const CompactUnwindEntry &cue = 677349cc55cSDimitry Andric cuEntries[cuIndices[page.entryIndex + i]]; 678349cc55cSDimitry Andric auto it = commonEncodingIndexes.find(cue.encoding); 679e8d8bef9SDimitry Andric if (it == commonEncodingIndexes.end()) 680349cc55cSDimitry Andric it = page.localEncodingIndexes.find(cue.encoding); 681e8d8bef9SDimitry Andric *ep++ = (it->second << COMPRESSED_ENTRY_FUNC_OFFSET_BITS) | 682349cc55cSDimitry Andric (cue.functionAddress - functionAddressBase); 683e8d8bef9SDimitry Andric } 684349cc55cSDimitry Andric if (!page.localEncodings.empty()) 685e8d8bef9SDimitry Andric memcpy(ep, page.localEncodings.data(), 686e8d8bef9SDimitry Andric page.localEncodings.size() * sizeof(uint32_t)); 687e8d8bef9SDimitry Andric } else { 688e8d8bef9SDimitry Andric auto *p2p = 689e8d8bef9SDimitry Andric reinterpret_cast<unwind_info_regular_second_level_page_header *>(pp); 690e8d8bef9SDimitry Andric p2p->kind = page.kind; 691e8d8bef9SDimitry Andric p2p->entryPageOffset = 692e8d8bef9SDimitry Andric sizeof(unwind_info_regular_second_level_page_header); 693e8d8bef9SDimitry Andric p2p->entryCount = page.entryCount; 694e8d8bef9SDimitry Andric auto *ep = reinterpret_cast<uint32_t *>(&p2p[1]); 695e8d8bef9SDimitry Andric for (size_t i = 0; i < page.entryCount; i++) { 69681ad6265SDimitry Andric const CompactUnwindEntry &cue = 697349cc55cSDimitry Andric cuEntries[cuIndices[page.entryIndex + i]]; 698349cc55cSDimitry Andric *ep++ = cue.functionAddress; 699349cc55cSDimitry Andric *ep++ = cue.encoding; 700e8d8bef9SDimitry Andric } 701e8d8bef9SDimitry Andric } 702e8d8bef9SDimitry Andric pp += SECOND_LEVEL_PAGE_WORDS; 703e8d8bef9SDimitry Andric } 704e8d8bef9SDimitry Andric } 705fe6060f1SDimitry Andric 706fe6060f1SDimitry Andric UnwindInfoSection *macho::makeUnwindInfoSection() { 70781ad6265SDimitry Andric return make<UnwindInfoSectionImpl>(); 708fe6060f1SDimitry Andric } 709