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; } 161*f3fd488fSDimitry Andric void prepare() 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(); 169*f3fd488fSDimitry Andric Symbol *canonicalizePersonality(Symbol *); 17081ad6265SDimitry Andric 17181ad6265SDimitry Andric uint64_t unwindInfoSize = 0; 17281ad6265SDimitry Andric std::vector<decltype(symbols)::value_type> symbolsVec; 17381ad6265SDimitry Andric CompactUnwindOffsets cuOffsets; 174fe6060f1SDimitry Andric std::vector<std::pair<compact_unwind_encoding_t, size_t>> commonEncodings; 175fe6060f1SDimitry Andric EncodingMap commonEncodingIndexes; 176349cc55cSDimitry Andric // The entries here will be in the same order as their originating symbols 177349cc55cSDimitry Andric // in symbolsVec. 17881ad6265SDimitry Andric std::vector<CompactUnwindEntry> cuEntries; 179349cc55cSDimitry Andric // Indices into the cuEntries vector. 180349cc55cSDimitry Andric std::vector<size_t> cuIndices; 18181ad6265SDimitry Andric std::vector<Symbol *> personalities; 182fe6060f1SDimitry Andric SmallDenseMap<std::pair<InputSection *, uint64_t /* addend */>, Symbol *> 183fe6060f1SDimitry Andric personalityTable; 184349cc55cSDimitry Andric // Indices into cuEntries for CUEs with a non-null LSDA. 185349cc55cSDimitry Andric std::vector<size_t> entriesWithLsda; 186349cc55cSDimitry Andric // Map of cuEntries index to an index within the LSDA array. 187349cc55cSDimitry Andric DenseMap<size_t, uint32_t> lsdaIndex; 188fe6060f1SDimitry Andric std::vector<SecondLevelPage> secondLevelPages; 189fe6060f1SDimitry Andric uint64_t level2PagesOffset = 0; 190fe6060f1SDimitry Andric }; 191fe6060f1SDimitry Andric 192e8d8bef9SDimitry Andric UnwindInfoSection::UnwindInfoSection() 193e8d8bef9SDimitry Andric : SyntheticSection(segment_names::text, section_names::unwindInfo) { 194fe6060f1SDimitry Andric align = 4; 195e8d8bef9SDimitry Andric } 196e8d8bef9SDimitry Andric 197349cc55cSDimitry Andric // Record function symbols that may need entries emitted in __unwind_info, which 198349cc55cSDimitry Andric // stores unwind data for address ranges. 199349cc55cSDimitry Andric // 2006246ae0bSDimitry Andric // Note that if several adjacent functions have the same unwind encoding and 2016246ae0bSDimitry Andric // personality function and no LSDA, they share one unwind entry. For this to 2026246ae0bSDimitry Andric // work, functions without unwind info need explicit "no unwind info" unwind 2036246ae0bSDimitry Andric // entries -- else the unwinder would think they have the unwind info of the 2046246ae0bSDimitry Andric // closest function with unwind info right before in the image. Thus, we add 2056246ae0bSDimitry Andric // function symbols for each unique address regardless of whether they have 2066246ae0bSDimitry Andric // associated unwind info. 207349cc55cSDimitry Andric void UnwindInfoSection::addSymbol(const Defined *d) { 208349cc55cSDimitry Andric if (d->unwindEntry) 209349cc55cSDimitry Andric allEntriesAreOmitted = false; 210349cc55cSDimitry Andric // We don't yet know the final output address of this symbol, but we know that 211349cc55cSDimitry Andric // they are uniquely determined by a combination of the isec and value, so 212349cc55cSDimitry Andric // we use that as the key here. 213349cc55cSDimitry Andric auto p = symbols.insert({{d->isec, d->value}, d}); 214349cc55cSDimitry Andric // If we have multiple symbols at the same address, only one of them can have 215fcaf7f86SDimitry Andric // an associated unwind entry. 216349cc55cSDimitry Andric if (!p.second && d->unwindEntry) { 217349cc55cSDimitry Andric assert(!p.first->second->unwindEntry); 218349cc55cSDimitry Andric p.first->second = d; 219349cc55cSDimitry Andric } 220fe6060f1SDimitry Andric } 221fe6060f1SDimitry Andric 222*f3fd488fSDimitry Andric void UnwindInfoSectionImpl::prepare() { 22381ad6265SDimitry Andric // This iteration needs to be deterministic, since prepareRelocations may add 22481ad6265SDimitry Andric // entries to the GOT. Hence the use of a MapVector for 22581ad6265SDimitry Andric // UnwindInfoSection::symbols. 22681ad6265SDimitry Andric for (const Defined *d : make_second_range(symbols)) 227*f3fd488fSDimitry Andric if (d->unwindEntry) { 228*f3fd488fSDimitry Andric if (d->unwindEntry->getName() == section_names::compactUnwind) { 22981ad6265SDimitry Andric prepareRelocations(d->unwindEntry); 230*f3fd488fSDimitry Andric } else { 231*f3fd488fSDimitry Andric // We don't have to add entries to the GOT here because FDEs have 232*f3fd488fSDimitry Andric // explicit GOT relocations, so Writer::scanRelocations() will add those 233*f3fd488fSDimitry Andric // GOT entries. However, we still need to canonicalize the personality 234*f3fd488fSDimitry Andric // pointers (like prepareRelocations() does for CU entries) in order 235*f3fd488fSDimitry Andric // to avoid overflowing the 3-personality limit. 236*f3fd488fSDimitry Andric FDE &fde = cast<ObjFile>(d->getFile())->fdes[d->unwindEntry]; 237*f3fd488fSDimitry Andric fde.personality = canonicalizePersonality(fde.personality); 238*f3fd488fSDimitry Andric } 239*f3fd488fSDimitry Andric } 24081ad6265SDimitry Andric } 24181ad6265SDimitry Andric 242fe6060f1SDimitry Andric // Compact unwind relocations have different semantics, so we handle them in a 243fe6060f1SDimitry Andric // separate code path from regular relocations. First, we do not wish to add 244fe6060f1SDimitry Andric // rebase opcodes for __LD,__compact_unwind, because that section doesn't 245fe6060f1SDimitry Andric // actually end up in the final binary. Second, personality pointers always 246fe6060f1SDimitry Andric // reside in the GOT and must be treated specially. 24781ad6265SDimitry Andric void UnwindInfoSectionImpl::prepareRelocations(ConcatInputSection *isec) { 248fe6060f1SDimitry Andric assert(!isec->shouldOmitFromOutput() && 249fe6060f1SDimitry Andric "__compact_unwind section should not be omitted"); 250fe6060f1SDimitry Andric 251fe6060f1SDimitry Andric // FIXME: Make this skip relocations for CompactUnwindEntries that 252fe6060f1SDimitry Andric // point to dead-stripped functions. That might save some amount of 253fe6060f1SDimitry Andric // work. But since there are usually just few personality functions 254fe6060f1SDimitry Andric // that are referenced from many places, at least some of them likely 255fe6060f1SDimitry Andric // live, it wouldn't reduce number of got entries. 256fe6060f1SDimitry Andric for (size_t i = 0; i < isec->relocs.size(); ++i) { 257fe6060f1SDimitry Andric Reloc &r = isec->relocs[i]; 258fe6060f1SDimitry Andric assert(target->hasAttr(r.type, RelocAttrBits::UNSIGNED)); 259fe6060f1SDimitry Andric 260349cc55cSDimitry Andric // Functions and LSDA entries always reside in the same object file as the 261349cc55cSDimitry Andric // compact unwind entries that references them, and thus appear as section 262349cc55cSDimitry Andric // relocs. There is no need to prepare them. We only prepare relocs for 263349cc55cSDimitry Andric // personality functions. 26481ad6265SDimitry Andric if (r.offset != cuOffsets.personality) 265fe6060f1SDimitry Andric continue; 266fe6060f1SDimitry Andric 267fe6060f1SDimitry Andric if (auto *s = r.referent.dyn_cast<Symbol *>()) { 268349cc55cSDimitry Andric // Personality functions are nearly always system-defined (e.g., 269349cc55cSDimitry Andric // ___gxx_personality_v0 for C++) and relocated as dylib symbols. When an 270349cc55cSDimitry Andric // application provides its own personality function, it might be 271349cc55cSDimitry Andric // referenced by an extern Defined symbol reloc, or a local section reloc. 272349cc55cSDimitry Andric if (auto *defined = dyn_cast<Defined>(s)) { 273349cc55cSDimitry Andric // XXX(vyng) This is a a special case for handling duplicate personality 274349cc55cSDimitry Andric // symbols. Note that LD64's behavior is a bit different and it is 275349cc55cSDimitry Andric // inconsistent with how symbol resolution usually work 276349cc55cSDimitry Andric // 277349cc55cSDimitry Andric // So we've decided not to follow it. Instead, simply pick the symbol 278349cc55cSDimitry Andric // with the same name from the symbol table to replace the local one. 279349cc55cSDimitry Andric // 280349cc55cSDimitry Andric // (See discussions/alternatives already considered on D107533) 281349cc55cSDimitry Andric if (!defined->isExternal()) 2824824e7fdSDimitry Andric if (Symbol *sym = symtab->find(defined->getName())) 28304eeddc0SDimitry Andric if (!sym->isLazy()) 2844824e7fdSDimitry Andric r.referent = s = sym; 285349cc55cSDimitry Andric } 286fe6060f1SDimitry Andric if (auto *undefined = dyn_cast<Undefined>(s)) { 28781ad6265SDimitry Andric treatUndefinedSymbol(*undefined, isec, r.offset); 288fe6060f1SDimitry Andric // treatUndefinedSymbol() can replace s with a DylibSymbol; re-check. 289fe6060f1SDimitry Andric if (isa<Undefined>(s)) 290fe6060f1SDimitry Andric continue; 291fe6060f1SDimitry Andric } 292349cc55cSDimitry Andric 293*f3fd488fSDimitry Andric // Similar to canonicalizePersonality(), but we also register a GOT entry. 294fe6060f1SDimitry Andric if (auto *defined = dyn_cast<Defined>(s)) { 295fe6060f1SDimitry Andric // Check if we have created a synthetic symbol at the same address. 296fe6060f1SDimitry Andric Symbol *&personality = 297fe6060f1SDimitry Andric personalityTable[{defined->isec, defined->value}]; 298fe6060f1SDimitry Andric if (personality == nullptr) { 299fe6060f1SDimitry Andric personality = defined; 300fe6060f1SDimitry Andric in.got->addEntry(defined); 301fe6060f1SDimitry Andric } else if (personality != defined) { 302fe6060f1SDimitry Andric r.referent = personality; 303fe6060f1SDimitry Andric } 304fe6060f1SDimitry Andric continue; 305fe6060f1SDimitry Andric } 306*f3fd488fSDimitry Andric 307fe6060f1SDimitry Andric assert(isa<DylibSymbol>(s)); 308fe6060f1SDimitry Andric in.got->addEntry(s); 309fe6060f1SDimitry Andric continue; 310fe6060f1SDimitry Andric } 311fe6060f1SDimitry Andric 312fe6060f1SDimitry Andric if (auto *referentIsec = r.referent.dyn_cast<InputSection *>()) { 313fe6060f1SDimitry Andric assert(!isCoalescedWeak(referentIsec)); 314fe6060f1SDimitry Andric // Personality functions can be referenced via section relocations 315fe6060f1SDimitry Andric // if they live in the same object file. Create placeholder synthetic 316fe6060f1SDimitry Andric // symbols for them in the GOT. 317fe6060f1SDimitry Andric Symbol *&s = personalityTable[{referentIsec, r.addend}]; 318fe6060f1SDimitry Andric if (s == nullptr) { 319fe6060f1SDimitry Andric // This runs after dead stripping, so the noDeadStrip argument does not 320fe6060f1SDimitry Andric // matter. 321fe6060f1SDimitry Andric s = make<Defined>("<internal>", /*file=*/nullptr, referentIsec, 322fe6060f1SDimitry Andric r.addend, /*size=*/0, /*isWeakDef=*/false, 323fe6060f1SDimitry Andric /*isExternal=*/false, /*isPrivateExtern=*/false, 32481ad6265SDimitry Andric /*includeInSymtab=*/true, 325fe6060f1SDimitry Andric /*isThumb=*/false, /*isReferencedDynamically=*/false, 326fe6060f1SDimitry Andric /*noDeadStrip=*/false); 32781ad6265SDimitry Andric s->used = true; 328fe6060f1SDimitry Andric in.got->addEntry(s); 329fe6060f1SDimitry Andric } 330fe6060f1SDimitry Andric r.referent = s; 331fe6060f1SDimitry Andric r.addend = 0; 332fe6060f1SDimitry Andric } 333fe6060f1SDimitry Andric } 334fe6060f1SDimitry Andric } 335fe6060f1SDimitry Andric 336*f3fd488fSDimitry Andric Symbol *UnwindInfoSectionImpl::canonicalizePersonality(Symbol *personality) { 337*f3fd488fSDimitry Andric if (auto *defined = dyn_cast_or_null<Defined>(personality)) { 338*f3fd488fSDimitry Andric // Check if we have created a synthetic symbol at the same address. 339*f3fd488fSDimitry Andric Symbol *&synth = personalityTable[{defined->isec, defined->value}]; 340*f3fd488fSDimitry Andric if (synth == nullptr) 341*f3fd488fSDimitry Andric synth = defined; 342*f3fd488fSDimitry Andric else if (synth != defined) 343*f3fd488fSDimitry Andric return synth; 344*f3fd488fSDimitry Andric } 345*f3fd488fSDimitry Andric return personality; 346*f3fd488fSDimitry Andric } 347*f3fd488fSDimitry Andric 348fe6060f1SDimitry Andric // We need to apply the relocations to the pre-link compact unwind section 349fe6060f1SDimitry Andric // before converting it to post-link form. There should only be absolute 350fe6060f1SDimitry Andric // relocations here: since we are not emitting the pre-link CU section, there 351fe6060f1SDimitry Andric // is no source address to make a relative location meaningful. 35281ad6265SDimitry Andric void UnwindInfoSectionImpl::relocateCompactUnwind( 35381ad6265SDimitry Andric std::vector<CompactUnwindEntry> &cuEntries) { 35481ad6265SDimitry Andric parallelFor(0, symbolsVec.size(), [&](size_t i) { 35581ad6265SDimitry Andric CompactUnwindEntry &cu = cuEntries[i]; 356349cc55cSDimitry Andric const Defined *d = symbolsVec[i].second; 35781ad6265SDimitry Andric cu.functionAddress = d->getVA(); 358349cc55cSDimitry Andric if (!d->unwindEntry) 359349cc55cSDimitry Andric return; 360fe6060f1SDimitry Andric 36181ad6265SDimitry Andric // If we have DWARF unwind info, create a CU entry that points to it. 36281ad6265SDimitry Andric if (d->unwindEntry->getName() == section_names::ehFrame) { 36381ad6265SDimitry Andric cu.encoding = target->modeDwarfEncoding | d->unwindEntry->outSecOff; 36481ad6265SDimitry Andric const FDE &fde = cast<ObjFile>(d->getFile())->fdes[d->unwindEntry]; 36581ad6265SDimitry Andric cu.functionLength = fde.funcLength; 36681ad6265SDimitry Andric cu.personality = fde.personality; 36781ad6265SDimitry Andric cu.lsda = fde.lsda; 36881ad6265SDimitry Andric return; 36981ad6265SDimitry Andric } 37081ad6265SDimitry Andric 37181ad6265SDimitry Andric assert(d->unwindEntry->getName() == section_names::compactUnwind); 37281ad6265SDimitry Andric 37381ad6265SDimitry Andric auto buf = reinterpret_cast<const uint8_t *>(d->unwindEntry->data.data()) - 37481ad6265SDimitry Andric target->wordSize; 37581ad6265SDimitry Andric cu.functionLength = 37681ad6265SDimitry Andric support::endian::read32le(buf + cuOffsets.functionLength); 37781ad6265SDimitry Andric cu.encoding = support::endian::read32le(buf + cuOffsets.encoding); 378349cc55cSDimitry Andric for (const Reloc &r : d->unwindEntry->relocs) { 37981ad6265SDimitry Andric if (r.offset == cuOffsets.personality) { 38081ad6265SDimitry Andric cu.personality = r.referent.get<Symbol *>(); 38181ad6265SDimitry Andric } else if (r.offset == cuOffsets.lsda) { 38281ad6265SDimitry Andric if (auto *referentSym = r.referent.dyn_cast<Symbol *>()) 38381ad6265SDimitry Andric cu.lsda = cast<Defined>(referentSym)->isec; 38481ad6265SDimitry Andric else 38581ad6265SDimitry Andric cu.lsda = r.referent.get<InputSection *>(); 386fe6060f1SDimitry Andric } 387fe6060f1SDimitry Andric } 388349cc55cSDimitry Andric }); 389fe6060f1SDimitry Andric } 390fe6060f1SDimitry Andric 391fe6060f1SDimitry Andric // There should only be a handful of unique personality pointers, so we can 392fe6060f1SDimitry Andric // encode them as 2-bit indices into a small array. 39381ad6265SDimitry Andric void UnwindInfoSectionImpl::encodePersonalities() { 394349cc55cSDimitry Andric for (size_t idx : cuIndices) { 39581ad6265SDimitry Andric CompactUnwindEntry &cu = cuEntries[idx]; 39681ad6265SDimitry Andric if (cu.personality == nullptr) 397fe6060f1SDimitry Andric continue; 398fe6060f1SDimitry Andric // Linear search is fast enough for a small array. 399349cc55cSDimitry Andric auto it = find(personalities, cu.personality); 400fe6060f1SDimitry Andric uint32_t personalityIndex; // 1-based index 401fe6060f1SDimitry Andric if (it != personalities.end()) { 402fe6060f1SDimitry Andric personalityIndex = std::distance(personalities.begin(), it) + 1; 403fe6060f1SDimitry Andric } else { 404349cc55cSDimitry Andric personalities.push_back(cu.personality); 405fe6060f1SDimitry Andric personalityIndex = personalities.size(); 406fe6060f1SDimitry Andric } 407349cc55cSDimitry Andric cu.encoding |= 408fe6060f1SDimitry Andric personalityIndex << countTrailingZeros( 409fe6060f1SDimitry Andric static_cast<compact_unwind_encoding_t>(UNWIND_PERSONALITY_MASK)); 410fe6060f1SDimitry Andric } 411fe6060f1SDimitry Andric if (personalities.size() > 3) 41281ad6265SDimitry Andric error("too many personalities (" + Twine(personalities.size()) + 413fe6060f1SDimitry Andric ") for compact unwind to encode"); 414fe6060f1SDimitry Andric } 415fe6060f1SDimitry Andric 416fe6060f1SDimitry Andric static bool canFoldEncoding(compact_unwind_encoding_t encoding) { 417fe6060f1SDimitry Andric // From compact_unwind_encoding.h: 418fe6060f1SDimitry Andric // UNWIND_X86_64_MODE_STACK_IND: 419fe6060f1SDimitry Andric // A "frameless" (RBP not used as frame pointer) function large constant 420fe6060f1SDimitry Andric // stack size. This case is like the previous, except the stack size is too 421fe6060f1SDimitry Andric // large to encode in the compact unwind encoding. Instead it requires that 422fe6060f1SDimitry Andric // the function contains "subq $nnnnnnnn,RSP" in its prolog. The compact 423fe6060f1SDimitry Andric // encoding contains the offset to the nnnnnnnn value in the function in 424fe6060f1SDimitry Andric // UNWIND_X86_64_FRAMELESS_STACK_SIZE. 425fe6060f1SDimitry Andric // Since this means the unwinder has to look at the `subq` in the function 426fe6060f1SDimitry Andric // of the unwind info's unwind address, two functions that have identical 427fe6060f1SDimitry Andric // unwind info can't be folded if it's using this encoding since both 428fe6060f1SDimitry Andric // entries need unique addresses. 42961cfbce3SDimitry Andric static_assert(static_cast<uint32_t>(UNWIND_X86_64_MODE_MASK) == 43061cfbce3SDimitry Andric static_cast<uint32_t>(UNWIND_X86_MODE_MASK), 43161cfbce3SDimitry Andric ""); 43261cfbce3SDimitry Andric static_assert(static_cast<uint32_t>(UNWIND_X86_64_MODE_STACK_IND) == 43361cfbce3SDimitry Andric static_cast<uint32_t>(UNWIND_X86_MODE_STACK_IND), 43461cfbce3SDimitry Andric ""); 435fe6060f1SDimitry Andric if ((target->cpuType == CPU_TYPE_X86_64 || target->cpuType == CPU_TYPE_X86) && 436fe6060f1SDimitry Andric (encoding & UNWIND_X86_64_MODE_MASK) == UNWIND_X86_64_MODE_STACK_IND) { 437fe6060f1SDimitry Andric // FIXME: Consider passing in the two function addresses and getting 438fe6060f1SDimitry Andric // their two stack sizes off the `subq` and only returning false if they're 439fe6060f1SDimitry Andric // actually different. 440fe6060f1SDimitry Andric return false; 441fe6060f1SDimitry Andric } 442fe6060f1SDimitry Andric return true; 443e8d8bef9SDimitry Andric } 444e8d8bef9SDimitry Andric 445e8d8bef9SDimitry Andric // Scan the __LD,__compact_unwind entries and compute the space needs of 4461fd87a68SDimitry Andric // __TEXT,__unwind_info and __TEXT,__eh_frame. 44781ad6265SDimitry Andric void UnwindInfoSectionImpl::finalize() { 448349cc55cSDimitry Andric if (symbols.empty()) 449e8d8bef9SDimitry Andric return; 450e8d8bef9SDimitry Andric 451e8d8bef9SDimitry Andric // At this point, the address space for __TEXT,__text has been 452e8d8bef9SDimitry Andric // assigned, so we can relocate the __LD,__compact_unwind entries 453e8d8bef9SDimitry Andric // into a temporary buffer. Relocation is necessary in order to sort 454e8d8bef9SDimitry Andric // the CU entries by function address. Sorting is necessary so that 4556246ae0bSDimitry Andric // we can fold adjacent CU entries with identical encoding+personality 4566246ae0bSDimitry Andric // and without any LSDA. Folding is necessary because it reduces the 4576246ae0bSDimitry Andric // number of CU entries by as much as 3 orders of magnitude! 458349cc55cSDimitry Andric cuEntries.resize(symbols.size()); 459349cc55cSDimitry Andric // The "map" part of the symbols MapVector was only needed for deduplication 460349cc55cSDimitry Andric // in addSymbol(). Now that we are done adding, move the contents to a plain 461349cc55cSDimitry Andric // std::vector for indexed access. 462349cc55cSDimitry Andric symbolsVec = symbols.takeVector(); 463349cc55cSDimitry Andric relocateCompactUnwind(cuEntries); 464e8d8bef9SDimitry Andric 465e8d8bef9SDimitry Andric // Rather than sort & fold the 32-byte entries directly, we create a 466349cc55cSDimitry Andric // vector of indices to entries and sort & fold that instead. 467349cc55cSDimitry Andric cuIndices.resize(cuEntries.size()); 468349cc55cSDimitry Andric std::iota(cuIndices.begin(), cuIndices.end(), 0); 469349cc55cSDimitry Andric llvm::sort(cuIndices, [&](size_t a, size_t b) { 470349cc55cSDimitry Andric return cuEntries[a].functionAddress < cuEntries[b].functionAddress; 471e8d8bef9SDimitry Andric }); 472e8d8bef9SDimitry Andric 4736246ae0bSDimitry Andric // Fold adjacent entries with matching encoding+personality and without LSDA 474349cc55cSDimitry Andric // We use three iterators on the same cuIndices to fold in-situ: 475e8d8bef9SDimitry Andric // (1) `foldBegin` is the first of a potential sequence of matching entries 476e8d8bef9SDimitry Andric // (2) `foldEnd` is the first non-matching entry after `foldBegin`. 477e8d8bef9SDimitry Andric // The semi-open interval [ foldBegin .. foldEnd ) contains a range 478e8d8bef9SDimitry Andric // entries that can be folded into a single entry and written to ... 479e8d8bef9SDimitry Andric // (3) `foldWrite` 480349cc55cSDimitry Andric auto foldWrite = cuIndices.begin(); 481349cc55cSDimitry Andric for (auto foldBegin = cuIndices.begin(); foldBegin < cuIndices.end();) { 482e8d8bef9SDimitry Andric auto foldEnd = foldBegin; 4836246ae0bSDimitry Andric // Common LSDA encodings (e.g. for C++ and Objective-C) contain offsets from 4846246ae0bSDimitry Andric // a base address. The base address is normally not contained directly in 4856246ae0bSDimitry Andric // the LSDA, and in that case, the personality function treats the starting 4866246ae0bSDimitry Andric // address of the function (which is computed by the unwinder) as the base 4876246ae0bSDimitry Andric // address and interprets the LSDA accordingly. The unwinder computes the 4886246ae0bSDimitry Andric // starting address of a function as the address associated with its CU 4896246ae0bSDimitry Andric // entry. For this reason, we cannot fold adjacent entries if they have an 4906246ae0bSDimitry Andric // LSDA, because folding would make the unwinder compute the wrong starting 4916246ae0bSDimitry Andric // address for the functions with the folded entries, which in turn would 4926246ae0bSDimitry Andric // cause the personality function to misinterpret the LSDA for those 4936246ae0bSDimitry Andric // functions. In the very rare case where the base address is encoded 4946246ae0bSDimitry Andric // directly in the LSDA, two functions at different addresses would 4956246ae0bSDimitry Andric // necessarily have different LSDAs, so their CU entries would not have been 4966246ae0bSDimitry Andric // folded anyway. 497349cc55cSDimitry Andric while (++foldEnd < cuIndices.end() && 498349cc55cSDimitry Andric cuEntries[*foldBegin].encoding == cuEntries[*foldEnd].encoding && 4996246ae0bSDimitry Andric !cuEntries[*foldBegin].lsda && !cuEntries[*foldEnd].lsda && 5006246ae0bSDimitry Andric // If we've gotten to this point, we don't have an LSDA, which should 5016246ae0bSDimitry Andric // also imply that we don't have a personality function, since in all 5026246ae0bSDimitry Andric // likelihood a personality function needs the LSDA to do anything 5036246ae0bSDimitry Andric // useful. It can be technically valid to have a personality function 5046246ae0bSDimitry Andric // and no LSDA though (e.g. the C++ personality __gxx_personality_v0 5056246ae0bSDimitry Andric // is just a no-op without LSDA), so we still check for personality 5066246ae0bSDimitry Andric // function equivalence to handle that case. 507349cc55cSDimitry Andric cuEntries[*foldBegin].personality == 508349cc55cSDimitry Andric cuEntries[*foldEnd].personality && 50981ad6265SDimitry Andric canFoldEncoding(cuEntries[*foldEnd].encoding)) 51081ad6265SDimitry Andric ; 511e8d8bef9SDimitry Andric *foldWrite++ = *foldBegin; 512e8d8bef9SDimitry Andric foldBegin = foldEnd; 513e8d8bef9SDimitry Andric } 514349cc55cSDimitry Andric cuIndices.erase(foldWrite, cuIndices.end()); 515e8d8bef9SDimitry Andric 516349cc55cSDimitry Andric encodePersonalities(); 517fe6060f1SDimitry Andric 518e8d8bef9SDimitry Andric // Count frequencies of the folded encodings 519e8d8bef9SDimitry Andric EncodingMap encodingFrequencies; 520349cc55cSDimitry Andric for (size_t idx : cuIndices) 521349cc55cSDimitry Andric encodingFrequencies[cuEntries[idx].encoding]++; 522e8d8bef9SDimitry Andric 523e8d8bef9SDimitry Andric // Make a vector of encodings, sorted by descending frequency 524e8d8bef9SDimitry Andric for (const auto &frequency : encodingFrequencies) 525e8d8bef9SDimitry Andric commonEncodings.emplace_back(frequency); 526fe6060f1SDimitry Andric llvm::sort(commonEncodings, 527e8d8bef9SDimitry Andric [](const std::pair<compact_unwind_encoding_t, size_t> &a, 528e8d8bef9SDimitry Andric const std::pair<compact_unwind_encoding_t, size_t> &b) { 529e8d8bef9SDimitry Andric if (a.second == b.second) 530e8d8bef9SDimitry Andric // When frequencies match, secondarily sort on encoding 531e8d8bef9SDimitry Andric // to maintain parity with validate-unwind-info.py 532e8d8bef9SDimitry Andric return a.first > b.first; 533e8d8bef9SDimitry Andric return a.second > b.second; 534e8d8bef9SDimitry Andric }); 535e8d8bef9SDimitry Andric 536e8d8bef9SDimitry Andric // Truncate the vector to 127 elements. 537e8d8bef9SDimitry Andric // Common encoding indexes are limited to 0..126, while encoding 538e8d8bef9SDimitry Andric // indexes 127..255 are local to each second-level page 539e8d8bef9SDimitry Andric if (commonEncodings.size() > COMMON_ENCODINGS_MAX) 540e8d8bef9SDimitry Andric commonEncodings.resize(COMMON_ENCODINGS_MAX); 541e8d8bef9SDimitry Andric 542e8d8bef9SDimitry Andric // Create a map from encoding to common-encoding-table index 543e8d8bef9SDimitry Andric for (size_t i = 0; i < commonEncodings.size(); i++) 544e8d8bef9SDimitry Andric commonEncodingIndexes[commonEncodings[i].first] = i; 545e8d8bef9SDimitry Andric 546e8d8bef9SDimitry Andric // Split folded encodings into pages, where each page is limited by ... 547e8d8bef9SDimitry Andric // (a) 4 KiB capacity 548e8d8bef9SDimitry Andric // (b) 24-bit difference between first & final function address 549e8d8bef9SDimitry Andric // (c) 8-bit compact-encoding-table index, 550e8d8bef9SDimitry Andric // for which 0..126 references the global common-encodings table, 551e8d8bef9SDimitry Andric // and 127..255 references a local per-second-level-page table. 552e8d8bef9SDimitry Andric // First we try the compact format and determine how many entries fit. 553e8d8bef9SDimitry Andric // If more entries fit in the regular format, we use that. 554349cc55cSDimitry Andric for (size_t i = 0; i < cuIndices.size();) { 555349cc55cSDimitry Andric size_t idx = cuIndices[i]; 556e8d8bef9SDimitry Andric secondLevelPages.emplace_back(); 557fe6060f1SDimitry Andric SecondLevelPage &page = secondLevelPages.back(); 558e8d8bef9SDimitry Andric page.entryIndex = i; 559753f127fSDimitry Andric uint64_t functionAddressMax = 560349cc55cSDimitry Andric cuEntries[idx].functionAddress + COMPRESSED_ENTRY_FUNC_OFFSET_MASK; 561e8d8bef9SDimitry Andric size_t n = commonEncodings.size(); 562e8d8bef9SDimitry Andric size_t wordsRemaining = 563e8d8bef9SDimitry Andric SECOND_LEVEL_PAGE_WORDS - 564e8d8bef9SDimitry Andric sizeof(unwind_info_compressed_second_level_page_header) / 565e8d8bef9SDimitry Andric sizeof(uint32_t); 566349cc55cSDimitry Andric while (wordsRemaining >= 1 && i < cuIndices.size()) { 567349cc55cSDimitry Andric idx = cuIndices[i]; 56881ad6265SDimitry Andric const CompactUnwindEntry *cuPtr = &cuEntries[idx]; 569e8d8bef9SDimitry Andric if (cuPtr->functionAddress >= functionAddressMax) { 570e8d8bef9SDimitry Andric break; 571e8d8bef9SDimitry Andric } else if (commonEncodingIndexes.count(cuPtr->encoding) || 572e8d8bef9SDimitry Andric page.localEncodingIndexes.count(cuPtr->encoding)) { 573e8d8bef9SDimitry Andric i++; 574e8d8bef9SDimitry Andric wordsRemaining--; 575e8d8bef9SDimitry Andric } else if (wordsRemaining >= 2 && n < COMPACT_ENCODINGS_MAX) { 576e8d8bef9SDimitry Andric page.localEncodings.emplace_back(cuPtr->encoding); 577e8d8bef9SDimitry Andric page.localEncodingIndexes[cuPtr->encoding] = n++; 578e8d8bef9SDimitry Andric i++; 579e8d8bef9SDimitry Andric wordsRemaining -= 2; 580e8d8bef9SDimitry Andric } else { 581e8d8bef9SDimitry Andric break; 582e8d8bef9SDimitry Andric } 583e8d8bef9SDimitry Andric } 584e8d8bef9SDimitry Andric page.entryCount = i - page.entryIndex; 585e8d8bef9SDimitry Andric 586e8d8bef9SDimitry Andric // If this is not the final page, see if it's possible to fit more 587e8d8bef9SDimitry Andric // entries by using the regular format. This can happen when there 588e8d8bef9SDimitry Andric // are many unique encodings, and we we saturated the local 589e8d8bef9SDimitry Andric // encoding table early. 590349cc55cSDimitry Andric if (i < cuIndices.size() && 591e8d8bef9SDimitry Andric page.entryCount < REGULAR_SECOND_LEVEL_ENTRIES_MAX) { 592e8d8bef9SDimitry Andric page.kind = UNWIND_SECOND_LEVEL_REGULAR; 593e8d8bef9SDimitry Andric page.entryCount = std::min(REGULAR_SECOND_LEVEL_ENTRIES_MAX, 594349cc55cSDimitry Andric cuIndices.size() - page.entryIndex); 595e8d8bef9SDimitry Andric i = page.entryIndex + page.entryCount; 596e8d8bef9SDimitry Andric } else { 597e8d8bef9SDimitry Andric page.kind = UNWIND_SECOND_LEVEL_COMPRESSED; 598e8d8bef9SDimitry Andric } 599e8d8bef9SDimitry Andric } 600e8d8bef9SDimitry Andric 601349cc55cSDimitry Andric for (size_t idx : cuIndices) { 602349cc55cSDimitry Andric lsdaIndex[idx] = entriesWithLsda.size(); 60381ad6265SDimitry Andric if (cuEntries[idx].lsda) 604349cc55cSDimitry Andric entriesWithLsda.push_back(idx); 605fe6060f1SDimitry Andric } 606fe6060f1SDimitry Andric 607e8d8bef9SDimitry Andric // compute size of __TEXT,__unwind_info section 608349cc55cSDimitry Andric level2PagesOffset = sizeof(unwind_info_section_header) + 609e8d8bef9SDimitry Andric commonEncodings.size() * sizeof(uint32_t) + 610e8d8bef9SDimitry Andric personalities.size() * sizeof(uint32_t) + 611e8d8bef9SDimitry Andric // The extra second-level-page entry is for the sentinel 612e8d8bef9SDimitry Andric (secondLevelPages.size() + 1) * 613e8d8bef9SDimitry Andric sizeof(unwind_info_section_header_index_entry) + 614349cc55cSDimitry Andric entriesWithLsda.size() * 615349cc55cSDimitry Andric sizeof(unwind_info_section_header_lsda_index_entry); 616e8d8bef9SDimitry Andric unwindInfoSize = 617e8d8bef9SDimitry Andric level2PagesOffset + secondLevelPages.size() * SECOND_LEVEL_PAGE_BYTES; 618e8d8bef9SDimitry Andric } 619e8d8bef9SDimitry Andric 620e8d8bef9SDimitry Andric // All inputs are relocated and output addresses are known, so write! 621e8d8bef9SDimitry Andric 62281ad6265SDimitry Andric void UnwindInfoSectionImpl::writeTo(uint8_t *buf) const { 623349cc55cSDimitry Andric assert(!cuIndices.empty() && "call only if there is unwind info"); 624fe6060f1SDimitry Andric 625e8d8bef9SDimitry Andric // section header 626e8d8bef9SDimitry Andric auto *uip = reinterpret_cast<unwind_info_section_header *>(buf); 627e8d8bef9SDimitry Andric uip->version = 1; 628e8d8bef9SDimitry Andric uip->commonEncodingsArraySectionOffset = sizeof(unwind_info_section_header); 629e8d8bef9SDimitry Andric uip->commonEncodingsArrayCount = commonEncodings.size(); 630e8d8bef9SDimitry Andric uip->personalityArraySectionOffset = 631e8d8bef9SDimitry Andric uip->commonEncodingsArraySectionOffset + 632e8d8bef9SDimitry Andric (uip->commonEncodingsArrayCount * sizeof(uint32_t)); 633e8d8bef9SDimitry Andric uip->personalityArrayCount = personalities.size(); 634e8d8bef9SDimitry Andric uip->indexSectionOffset = uip->personalityArraySectionOffset + 635e8d8bef9SDimitry Andric (uip->personalityArrayCount * sizeof(uint32_t)); 636e8d8bef9SDimitry Andric uip->indexCount = secondLevelPages.size() + 1; 637e8d8bef9SDimitry Andric 638e8d8bef9SDimitry Andric // Common encodings 639e8d8bef9SDimitry Andric auto *i32p = reinterpret_cast<uint32_t *>(&uip[1]); 640e8d8bef9SDimitry Andric for (const auto &encoding : commonEncodings) 641e8d8bef9SDimitry Andric *i32p++ = encoding.first; 642e8d8bef9SDimitry Andric 643e8d8bef9SDimitry Andric // Personalities 64481ad6265SDimitry Andric for (const Symbol *personality : personalities) 64581ad6265SDimitry Andric *i32p++ = personality->getGotVA() - in.header->addr; 646e8d8bef9SDimitry Andric 647e8d8bef9SDimitry Andric // Level-1 index 648e8d8bef9SDimitry Andric uint32_t lsdaOffset = 649e8d8bef9SDimitry Andric uip->indexSectionOffset + 650e8d8bef9SDimitry Andric uip->indexCount * sizeof(unwind_info_section_header_index_entry); 651e8d8bef9SDimitry Andric uint64_t l2PagesOffset = level2PagesOffset; 652e8d8bef9SDimitry Andric auto *iep = reinterpret_cast<unwind_info_section_header_index_entry *>(i32p); 653e8d8bef9SDimitry Andric for (const SecondLevelPage &page : secondLevelPages) { 654349cc55cSDimitry Andric size_t idx = cuIndices[page.entryIndex]; 655349cc55cSDimitry Andric iep->functionOffset = cuEntries[idx].functionAddress - in.header->addr; 656e8d8bef9SDimitry Andric iep->secondLevelPagesSectionOffset = l2PagesOffset; 657fe6060f1SDimitry Andric iep->lsdaIndexArraySectionOffset = 658349cc55cSDimitry Andric lsdaOffset + lsdaIndex.lookup(idx) * 659fe6060f1SDimitry Andric sizeof(unwind_info_section_header_lsda_index_entry); 660e8d8bef9SDimitry Andric iep++; 661e8d8bef9SDimitry Andric l2PagesOffset += SECOND_LEVEL_PAGE_BYTES; 662e8d8bef9SDimitry Andric } 663e8d8bef9SDimitry Andric // Level-1 sentinel 66481ad6265SDimitry Andric const CompactUnwindEntry &cuEnd = cuEntries[cuIndices.back()]; 665fe6060f1SDimitry Andric iep->functionOffset = 666fe6060f1SDimitry Andric cuEnd.functionAddress - in.header->addr + cuEnd.functionLength; 667e8d8bef9SDimitry Andric iep->secondLevelPagesSectionOffset = 0; 668fe6060f1SDimitry Andric iep->lsdaIndexArraySectionOffset = 669349cc55cSDimitry Andric lsdaOffset + entriesWithLsda.size() * 670349cc55cSDimitry Andric sizeof(unwind_info_section_header_lsda_index_entry); 671e8d8bef9SDimitry Andric iep++; 672e8d8bef9SDimitry Andric 673e8d8bef9SDimitry Andric // LSDAs 674349cc55cSDimitry Andric auto *lep = 675349cc55cSDimitry Andric reinterpret_cast<unwind_info_section_header_lsda_index_entry *>(iep); 676349cc55cSDimitry Andric for (size_t idx : entriesWithLsda) { 67781ad6265SDimitry Andric const CompactUnwindEntry &cu = cuEntries[idx]; 67881ad6265SDimitry Andric lep->lsdaOffset = cu.lsda->getVA(/*off=*/0) - in.header->addr; 679349cc55cSDimitry Andric lep->functionOffset = cu.functionAddress - in.header->addr; 680349cc55cSDimitry Andric lep++; 681349cc55cSDimitry Andric } 682e8d8bef9SDimitry Andric 683e8d8bef9SDimitry Andric // Level-2 pages 684349cc55cSDimitry Andric auto *pp = reinterpret_cast<uint32_t *>(lep); 685e8d8bef9SDimitry Andric for (const SecondLevelPage &page : secondLevelPages) { 686e8d8bef9SDimitry Andric if (page.kind == UNWIND_SECOND_LEVEL_COMPRESSED) { 687e8d8bef9SDimitry Andric uintptr_t functionAddressBase = 688349cc55cSDimitry Andric cuEntries[cuIndices[page.entryIndex]].functionAddress; 689e8d8bef9SDimitry Andric auto *p2p = 690e8d8bef9SDimitry Andric reinterpret_cast<unwind_info_compressed_second_level_page_header *>( 691e8d8bef9SDimitry Andric pp); 692e8d8bef9SDimitry Andric p2p->kind = page.kind; 693e8d8bef9SDimitry Andric p2p->entryPageOffset = 694e8d8bef9SDimitry Andric sizeof(unwind_info_compressed_second_level_page_header); 695e8d8bef9SDimitry Andric p2p->entryCount = page.entryCount; 696e8d8bef9SDimitry Andric p2p->encodingsPageOffset = 697e8d8bef9SDimitry Andric p2p->entryPageOffset + p2p->entryCount * sizeof(uint32_t); 698e8d8bef9SDimitry Andric p2p->encodingsCount = page.localEncodings.size(); 699e8d8bef9SDimitry Andric auto *ep = reinterpret_cast<uint32_t *>(&p2p[1]); 700e8d8bef9SDimitry Andric for (size_t i = 0; i < page.entryCount; i++) { 70181ad6265SDimitry Andric const CompactUnwindEntry &cue = 702349cc55cSDimitry Andric cuEntries[cuIndices[page.entryIndex + i]]; 703349cc55cSDimitry Andric auto it = commonEncodingIndexes.find(cue.encoding); 704e8d8bef9SDimitry Andric if (it == commonEncodingIndexes.end()) 705349cc55cSDimitry Andric it = page.localEncodingIndexes.find(cue.encoding); 706e8d8bef9SDimitry Andric *ep++ = (it->second << COMPRESSED_ENTRY_FUNC_OFFSET_BITS) | 707349cc55cSDimitry Andric (cue.functionAddress - functionAddressBase); 708e8d8bef9SDimitry Andric } 709349cc55cSDimitry Andric if (!page.localEncodings.empty()) 710e8d8bef9SDimitry Andric memcpy(ep, page.localEncodings.data(), 711e8d8bef9SDimitry Andric page.localEncodings.size() * sizeof(uint32_t)); 712e8d8bef9SDimitry Andric } else { 713e8d8bef9SDimitry Andric auto *p2p = 714e8d8bef9SDimitry Andric reinterpret_cast<unwind_info_regular_second_level_page_header *>(pp); 715e8d8bef9SDimitry Andric p2p->kind = page.kind; 716e8d8bef9SDimitry Andric p2p->entryPageOffset = 717e8d8bef9SDimitry Andric sizeof(unwind_info_regular_second_level_page_header); 718e8d8bef9SDimitry Andric p2p->entryCount = page.entryCount; 719e8d8bef9SDimitry Andric auto *ep = reinterpret_cast<uint32_t *>(&p2p[1]); 720e8d8bef9SDimitry Andric for (size_t i = 0; i < page.entryCount; i++) { 72181ad6265SDimitry Andric const CompactUnwindEntry &cue = 722349cc55cSDimitry Andric cuEntries[cuIndices[page.entryIndex + i]]; 723349cc55cSDimitry Andric *ep++ = cue.functionAddress; 724349cc55cSDimitry Andric *ep++ = cue.encoding; 725e8d8bef9SDimitry Andric } 726e8d8bef9SDimitry Andric } 727e8d8bef9SDimitry Andric pp += SECOND_LEVEL_PAGE_WORDS; 728e8d8bef9SDimitry Andric } 729e8d8bef9SDimitry Andric } 730fe6060f1SDimitry Andric 731fe6060f1SDimitry Andric UnwindInfoSection *macho::makeUnwindInfoSection() { 73281ad6265SDimitry Andric return make<UnwindInfoSectionImpl>(); 733fe6060f1SDimitry Andric } 734