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" 10e8d8bef9SDimitry Andric #include "InputSection.h" 11e8d8bef9SDimitry Andric #include "OutputSection.h" 12e8d8bef9SDimitry Andric #include "OutputSegment.h" 13fe6060f1SDimitry Andric #include "SymbolTable.h" 14e8d8bef9SDimitry Andric #include "Symbols.h" 15e8d8bef9SDimitry Andric #include "SyntheticSections.h" 16e8d8bef9SDimitry Andric #include "Target.h" 17e8d8bef9SDimitry Andric 18e8d8bef9SDimitry Andric #include "lld/Common/ErrorHandler.h" 19fe6060f1SDimitry Andric #include "lld/Common/Memory.h" 20349cc55cSDimitry Andric #include "llvm/ADT/DenseMap.h" 21fe6060f1SDimitry Andric #include "llvm/ADT/STLExtras.h" 22e8d8bef9SDimitry Andric #include "llvm/BinaryFormat/MachO.h" 23349cc55cSDimitry Andric #include "llvm/Support/Parallel.h" 24349cc55cSDimitry Andric 25*bdd1243dSDimitry Andric #include "mach-o/compact_unwind_encoding.h" 26*bdd1243dSDimitry 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 // 85fe6060f1SDimitry Andric // Refer to the definition of unwind_info_section_header in 86fe6060f1SDimitry Andric // compact_unwind_encoding.h for an overview of the format we are encoding 87fe6060f1SDimitry Andric // here. 88e8d8bef9SDimitry Andric 89e8d8bef9SDimitry Andric // TODO(gkm): how do we align the 2nd-level pages? 90e8d8bef9SDimitry Andric 9181ad6265SDimitry Andric // The offsets of various fields in the on-disk representation of each compact 9281ad6265SDimitry Andric // unwind entry. 9381ad6265SDimitry Andric struct CompactUnwindOffsets { 9481ad6265SDimitry Andric uint32_t functionAddress; 9581ad6265SDimitry Andric uint32_t functionLength; 9681ad6265SDimitry Andric uint32_t encoding; 9781ad6265SDimitry Andric uint32_t personality; 9881ad6265SDimitry Andric uint32_t lsda; 9981ad6265SDimitry Andric 10081ad6265SDimitry Andric CompactUnwindOffsets(size_t wordSize) { 10181ad6265SDimitry Andric if (wordSize == 8) 10281ad6265SDimitry Andric init<uint64_t>(); 10381ad6265SDimitry Andric else { 10481ad6265SDimitry Andric assert(wordSize == 4); 10581ad6265SDimitry Andric init<uint32_t>(); 10681ad6265SDimitry Andric } 10781ad6265SDimitry Andric } 10881ad6265SDimitry Andric 10981ad6265SDimitry Andric private: 11081ad6265SDimitry Andric template <class Ptr> void init() { 11181ad6265SDimitry Andric functionAddress = offsetof(Layout<Ptr>, functionAddress); 11281ad6265SDimitry Andric functionLength = offsetof(Layout<Ptr>, functionLength); 11381ad6265SDimitry Andric encoding = offsetof(Layout<Ptr>, encoding); 11481ad6265SDimitry Andric personality = offsetof(Layout<Ptr>, personality); 11581ad6265SDimitry Andric lsda = offsetof(Layout<Ptr>, lsda); 11681ad6265SDimitry Andric } 11781ad6265SDimitry Andric 11881ad6265SDimitry Andric template <class Ptr> struct Layout { 119349cc55cSDimitry Andric Ptr functionAddress; 120349cc55cSDimitry Andric uint32_t functionLength; 121349cc55cSDimitry Andric compact_unwind_encoding_t encoding; 122349cc55cSDimitry Andric Ptr personality; 123349cc55cSDimitry Andric Ptr lsda; 124349cc55cSDimitry Andric }; 12581ad6265SDimitry Andric }; 12681ad6265SDimitry Andric 12781ad6265SDimitry Andric // LLD's internal representation of a compact unwind entry. 12881ad6265SDimitry Andric struct CompactUnwindEntry { 12981ad6265SDimitry Andric uint64_t functionAddress; 13081ad6265SDimitry Andric uint32_t functionLength; 13181ad6265SDimitry Andric compact_unwind_encoding_t encoding; 13281ad6265SDimitry Andric Symbol *personality; 13381ad6265SDimitry Andric InputSection *lsda; 13481ad6265SDimitry Andric }; 135349cc55cSDimitry Andric 136fe6060f1SDimitry Andric using EncodingMap = DenseMap<compact_unwind_encoding_t, size_t>; 137fe6060f1SDimitry Andric 138fe6060f1SDimitry Andric struct SecondLevelPage { 139fe6060f1SDimitry Andric uint32_t kind; 140fe6060f1SDimitry Andric size_t entryIndex; 141fe6060f1SDimitry Andric size_t entryCount; 142fe6060f1SDimitry Andric size_t byteCount; 143fe6060f1SDimitry Andric std::vector<compact_unwind_encoding_t> localEncodings; 144fe6060f1SDimitry Andric EncodingMap localEncodingIndexes; 145fe6060f1SDimitry Andric }; 146fe6060f1SDimitry Andric 14781ad6265SDimitry Andric // UnwindInfoSectionImpl allows us to avoid cluttering our header file with a 14881ad6265SDimitry Andric // lengthy definition of UnwindInfoSection. 149fe6060f1SDimitry Andric class UnwindInfoSectionImpl final : public UnwindInfoSection { 150fe6060f1SDimitry Andric public: 15181ad6265SDimitry Andric UnwindInfoSectionImpl() : cuOffsets(target->wordSize) {} 15281ad6265SDimitry Andric uint64_t getSize() const override { return unwindInfoSize; } 153f3fd488fSDimitry Andric void prepare() override; 154fe6060f1SDimitry Andric void finalize() override; 155fe6060f1SDimitry Andric void writeTo(uint8_t *buf) const override; 156fe6060f1SDimitry Andric 157fe6060f1SDimitry Andric private: 15881ad6265SDimitry Andric void prepareRelocations(ConcatInputSection *); 15981ad6265SDimitry Andric void relocateCompactUnwind(std::vector<CompactUnwindEntry> &); 16081ad6265SDimitry Andric void encodePersonalities(); 161f3fd488fSDimitry Andric Symbol *canonicalizePersonality(Symbol *); 16281ad6265SDimitry Andric 16381ad6265SDimitry Andric uint64_t unwindInfoSize = 0; 16481ad6265SDimitry Andric std::vector<decltype(symbols)::value_type> symbolsVec; 16581ad6265SDimitry Andric CompactUnwindOffsets cuOffsets; 166fe6060f1SDimitry Andric std::vector<std::pair<compact_unwind_encoding_t, size_t>> commonEncodings; 167fe6060f1SDimitry Andric EncodingMap commonEncodingIndexes; 168349cc55cSDimitry Andric // The entries here will be in the same order as their originating symbols 169349cc55cSDimitry Andric // in symbolsVec. 17081ad6265SDimitry Andric std::vector<CompactUnwindEntry> cuEntries; 171349cc55cSDimitry Andric // Indices into the cuEntries vector. 172349cc55cSDimitry Andric std::vector<size_t> cuIndices; 17381ad6265SDimitry Andric std::vector<Symbol *> personalities; 174fe6060f1SDimitry Andric SmallDenseMap<std::pair<InputSection *, uint64_t /* addend */>, Symbol *> 175fe6060f1SDimitry Andric personalityTable; 176349cc55cSDimitry Andric // Indices into cuEntries for CUEs with a non-null LSDA. 177349cc55cSDimitry Andric std::vector<size_t> entriesWithLsda; 178349cc55cSDimitry Andric // Map of cuEntries index to an index within the LSDA array. 179349cc55cSDimitry Andric DenseMap<size_t, uint32_t> lsdaIndex; 180fe6060f1SDimitry Andric std::vector<SecondLevelPage> secondLevelPages; 181fe6060f1SDimitry Andric uint64_t level2PagesOffset = 0; 182*bdd1243dSDimitry Andric // The highest-address function plus its size. The unwinder needs this to 183*bdd1243dSDimitry Andric // determine the address range that is covered by unwind info. 184*bdd1243dSDimitry Andric uint64_t cueEndBoundary = 0; 185fe6060f1SDimitry Andric }; 186fe6060f1SDimitry Andric 187e8d8bef9SDimitry Andric UnwindInfoSection::UnwindInfoSection() 188e8d8bef9SDimitry Andric : SyntheticSection(segment_names::text, section_names::unwindInfo) { 189fe6060f1SDimitry Andric align = 4; 190e8d8bef9SDimitry Andric } 191e8d8bef9SDimitry Andric 192349cc55cSDimitry Andric // Record function symbols that may need entries emitted in __unwind_info, which 193349cc55cSDimitry Andric // stores unwind data for address ranges. 194349cc55cSDimitry Andric // 1956246ae0bSDimitry Andric // Note that if several adjacent functions have the same unwind encoding and 1966246ae0bSDimitry Andric // personality function and no LSDA, they share one unwind entry. For this to 1976246ae0bSDimitry Andric // work, functions without unwind info need explicit "no unwind info" unwind 1986246ae0bSDimitry Andric // entries -- else the unwinder would think they have the unwind info of the 1996246ae0bSDimitry Andric // closest function with unwind info right before in the image. Thus, we add 2006246ae0bSDimitry Andric // function symbols for each unique address regardless of whether they have 2016246ae0bSDimitry Andric // associated unwind info. 202349cc55cSDimitry Andric void UnwindInfoSection::addSymbol(const Defined *d) { 203349cc55cSDimitry Andric if (d->unwindEntry) 204349cc55cSDimitry Andric allEntriesAreOmitted = false; 205349cc55cSDimitry Andric // We don't yet know the final output address of this symbol, but we know that 206349cc55cSDimitry Andric // they are uniquely determined by a combination of the isec and value, so 207349cc55cSDimitry Andric // we use that as the key here. 208349cc55cSDimitry Andric auto p = symbols.insert({{d->isec, d->value}, d}); 209349cc55cSDimitry Andric // If we have multiple symbols at the same address, only one of them can have 210fcaf7f86SDimitry Andric // an associated unwind entry. 211349cc55cSDimitry Andric if (!p.second && d->unwindEntry) { 212*bdd1243dSDimitry Andric assert(p.first->second == d || !p.first->second->unwindEntry); 213349cc55cSDimitry Andric p.first->second = d; 214349cc55cSDimitry Andric } 215fe6060f1SDimitry Andric } 216fe6060f1SDimitry Andric 217f3fd488fSDimitry Andric void UnwindInfoSectionImpl::prepare() { 21881ad6265SDimitry Andric // This iteration needs to be deterministic, since prepareRelocations may add 21981ad6265SDimitry Andric // entries to the GOT. Hence the use of a MapVector for 22081ad6265SDimitry Andric // UnwindInfoSection::symbols. 22181ad6265SDimitry Andric for (const Defined *d : make_second_range(symbols)) 222f3fd488fSDimitry Andric if (d->unwindEntry) { 223f3fd488fSDimitry Andric if (d->unwindEntry->getName() == section_names::compactUnwind) { 22481ad6265SDimitry Andric prepareRelocations(d->unwindEntry); 225f3fd488fSDimitry Andric } else { 226f3fd488fSDimitry Andric // We don't have to add entries to the GOT here because FDEs have 227f3fd488fSDimitry Andric // explicit GOT relocations, so Writer::scanRelocations() will add those 228f3fd488fSDimitry Andric // GOT entries. However, we still need to canonicalize the personality 229f3fd488fSDimitry Andric // pointers (like prepareRelocations() does for CU entries) in order 230f3fd488fSDimitry Andric // to avoid overflowing the 3-personality limit. 231f3fd488fSDimitry Andric FDE &fde = cast<ObjFile>(d->getFile())->fdes[d->unwindEntry]; 232f3fd488fSDimitry Andric fde.personality = canonicalizePersonality(fde.personality); 233f3fd488fSDimitry Andric } 234f3fd488fSDimitry Andric } 23581ad6265SDimitry Andric } 23681ad6265SDimitry Andric 237fe6060f1SDimitry Andric // Compact unwind relocations have different semantics, so we handle them in a 238fe6060f1SDimitry Andric // separate code path from regular relocations. First, we do not wish to add 239fe6060f1SDimitry Andric // rebase opcodes for __LD,__compact_unwind, because that section doesn't 240fe6060f1SDimitry Andric // actually end up in the final binary. Second, personality pointers always 241fe6060f1SDimitry Andric // reside in the GOT and must be treated specially. 24281ad6265SDimitry Andric void UnwindInfoSectionImpl::prepareRelocations(ConcatInputSection *isec) { 243fe6060f1SDimitry Andric assert(!isec->shouldOmitFromOutput() && 244fe6060f1SDimitry Andric "__compact_unwind section should not be omitted"); 245fe6060f1SDimitry Andric 246fe6060f1SDimitry Andric // FIXME: Make this skip relocations for CompactUnwindEntries that 247fe6060f1SDimitry Andric // point to dead-stripped functions. That might save some amount of 248fe6060f1SDimitry Andric // work. But since there are usually just few personality functions 249fe6060f1SDimitry Andric // that are referenced from many places, at least some of them likely 250fe6060f1SDimitry Andric // live, it wouldn't reduce number of got entries. 251fe6060f1SDimitry Andric for (size_t i = 0; i < isec->relocs.size(); ++i) { 252fe6060f1SDimitry Andric Reloc &r = isec->relocs[i]; 253fe6060f1SDimitry Andric assert(target->hasAttr(r.type, RelocAttrBits::UNSIGNED)); 254*bdd1243dSDimitry Andric // Since compact unwind sections aren't part of the inputSections vector, 255*bdd1243dSDimitry Andric // they don't get canonicalized by scanRelocations(), so we have to do the 256*bdd1243dSDimitry Andric // canonicalization here. 257*bdd1243dSDimitry Andric if (auto *referentIsec = r.referent.dyn_cast<InputSection *>()) 258*bdd1243dSDimitry Andric r.referent = referentIsec->canonical(); 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)) { 273*bdd1243dSDimitry Andric // XXX(vyng) This is 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 293f3fd488fSDimitry 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 } 306f3fd488fSDimitry 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 336f3fd488fSDimitry Andric Symbol *UnwindInfoSectionImpl::canonicalizePersonality(Symbol *personality) { 337f3fd488fSDimitry Andric if (auto *defined = dyn_cast_or_null<Defined>(personality)) { 338f3fd488fSDimitry Andric // Check if we have created a synthetic symbol at the same address. 339f3fd488fSDimitry Andric Symbol *&synth = personalityTable[{defined->isec, defined->value}]; 340f3fd488fSDimitry Andric if (synth == nullptr) 341f3fd488fSDimitry Andric synth = defined; 342f3fd488fSDimitry Andric else if (synth != defined) 343f3fd488fSDimitry Andric return synth; 344f3fd488fSDimitry Andric } 345f3fd488fSDimitry Andric return personality; 346f3fd488fSDimitry Andric } 347f3fd488fSDimitry 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_STACK_IND) == 430*bdd1243dSDimitry Andric static_cast<uint32_t>(UNWIND_X86_MODE_STACK_IND)); 431fe6060f1SDimitry Andric if ((target->cpuType == CPU_TYPE_X86_64 || target->cpuType == CPU_TYPE_X86) && 432*bdd1243dSDimitry Andric (encoding & UNWIND_MODE_MASK) == UNWIND_X86_64_MODE_STACK_IND) { 433fe6060f1SDimitry Andric // FIXME: Consider passing in the two function addresses and getting 434fe6060f1SDimitry Andric // their two stack sizes off the `subq` and only returning false if they're 435fe6060f1SDimitry Andric // actually different. 436fe6060f1SDimitry Andric return false; 437fe6060f1SDimitry Andric } 438fe6060f1SDimitry Andric return true; 439e8d8bef9SDimitry Andric } 440e8d8bef9SDimitry Andric 441e8d8bef9SDimitry Andric // Scan the __LD,__compact_unwind entries and compute the space needs of 4421fd87a68SDimitry Andric // __TEXT,__unwind_info and __TEXT,__eh_frame. 44381ad6265SDimitry Andric void UnwindInfoSectionImpl::finalize() { 444349cc55cSDimitry Andric if (symbols.empty()) 445e8d8bef9SDimitry Andric return; 446e8d8bef9SDimitry Andric 447e8d8bef9SDimitry Andric // At this point, the address space for __TEXT,__text has been 448e8d8bef9SDimitry Andric // assigned, so we can relocate the __LD,__compact_unwind entries 449e8d8bef9SDimitry Andric // into a temporary buffer. Relocation is necessary in order to sort 450e8d8bef9SDimitry Andric // the CU entries by function address. Sorting is necessary so that 4516246ae0bSDimitry Andric // we can fold adjacent CU entries with identical encoding+personality 4526246ae0bSDimitry Andric // and without any LSDA. Folding is necessary because it reduces the 4536246ae0bSDimitry Andric // number of CU entries by as much as 3 orders of magnitude! 454349cc55cSDimitry Andric cuEntries.resize(symbols.size()); 455349cc55cSDimitry Andric // The "map" part of the symbols MapVector was only needed for deduplication 456349cc55cSDimitry Andric // in addSymbol(). Now that we are done adding, move the contents to a plain 457349cc55cSDimitry Andric // std::vector for indexed access. 458349cc55cSDimitry Andric symbolsVec = symbols.takeVector(); 459349cc55cSDimitry Andric relocateCompactUnwind(cuEntries); 460e8d8bef9SDimitry Andric 461e8d8bef9SDimitry Andric // Rather than sort & fold the 32-byte entries directly, we create a 462349cc55cSDimitry Andric // vector of indices to entries and sort & fold that instead. 463349cc55cSDimitry Andric cuIndices.resize(cuEntries.size()); 464349cc55cSDimitry Andric std::iota(cuIndices.begin(), cuIndices.end(), 0); 465349cc55cSDimitry Andric llvm::sort(cuIndices, [&](size_t a, size_t b) { 466349cc55cSDimitry Andric return cuEntries[a].functionAddress < cuEntries[b].functionAddress; 467e8d8bef9SDimitry Andric }); 468e8d8bef9SDimitry Andric 469*bdd1243dSDimitry Andric // Record the ending boundary before we fold the entries. 470*bdd1243dSDimitry Andric cueEndBoundary = cuEntries[cuIndices.back()].functionAddress + 471*bdd1243dSDimitry Andric cuEntries[cuIndices.back()].functionLength; 472*bdd1243dSDimitry 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 586*bdd1243dSDimitry Andric // If this is not the final page, see if it's possible to fit more entries 587*bdd1243dSDimitry Andric // by using the regular format. This can happen when there are many unique 588*bdd1243dSDimitry Andric // encodings, and we saturated the local encoding table early. 589349cc55cSDimitry Andric if (i < cuIndices.size() && 590e8d8bef9SDimitry Andric page.entryCount < REGULAR_SECOND_LEVEL_ENTRIES_MAX) { 591e8d8bef9SDimitry Andric page.kind = UNWIND_SECOND_LEVEL_REGULAR; 592e8d8bef9SDimitry Andric page.entryCount = std::min(REGULAR_SECOND_LEVEL_ENTRIES_MAX, 593349cc55cSDimitry Andric cuIndices.size() - page.entryIndex); 594e8d8bef9SDimitry Andric i = page.entryIndex + page.entryCount; 595e8d8bef9SDimitry Andric } else { 596e8d8bef9SDimitry Andric page.kind = UNWIND_SECOND_LEVEL_COMPRESSED; 597e8d8bef9SDimitry Andric } 598e8d8bef9SDimitry Andric } 599e8d8bef9SDimitry Andric 600349cc55cSDimitry Andric for (size_t idx : cuIndices) { 601349cc55cSDimitry Andric lsdaIndex[idx] = entriesWithLsda.size(); 60281ad6265SDimitry Andric if (cuEntries[idx].lsda) 603349cc55cSDimitry Andric entriesWithLsda.push_back(idx); 604fe6060f1SDimitry Andric } 605fe6060f1SDimitry Andric 606e8d8bef9SDimitry Andric // compute size of __TEXT,__unwind_info section 607349cc55cSDimitry Andric level2PagesOffset = sizeof(unwind_info_section_header) + 608e8d8bef9SDimitry Andric commonEncodings.size() * sizeof(uint32_t) + 609e8d8bef9SDimitry Andric personalities.size() * sizeof(uint32_t) + 610e8d8bef9SDimitry Andric // The extra second-level-page entry is for the sentinel 611e8d8bef9SDimitry Andric (secondLevelPages.size() + 1) * 612e8d8bef9SDimitry Andric sizeof(unwind_info_section_header_index_entry) + 613349cc55cSDimitry Andric entriesWithLsda.size() * 614349cc55cSDimitry Andric sizeof(unwind_info_section_header_lsda_index_entry); 615e8d8bef9SDimitry Andric unwindInfoSize = 616e8d8bef9SDimitry Andric level2PagesOffset + secondLevelPages.size() * SECOND_LEVEL_PAGE_BYTES; 617e8d8bef9SDimitry Andric } 618e8d8bef9SDimitry Andric 619e8d8bef9SDimitry Andric // All inputs are relocated and output addresses are known, so write! 620e8d8bef9SDimitry Andric 62181ad6265SDimitry Andric void UnwindInfoSectionImpl::writeTo(uint8_t *buf) const { 622349cc55cSDimitry Andric assert(!cuIndices.empty() && "call only if there is unwind info"); 623fe6060f1SDimitry Andric 624e8d8bef9SDimitry Andric // section header 625e8d8bef9SDimitry Andric auto *uip = reinterpret_cast<unwind_info_section_header *>(buf); 626e8d8bef9SDimitry Andric uip->version = 1; 627e8d8bef9SDimitry Andric uip->commonEncodingsArraySectionOffset = sizeof(unwind_info_section_header); 628e8d8bef9SDimitry Andric uip->commonEncodingsArrayCount = commonEncodings.size(); 629e8d8bef9SDimitry Andric uip->personalityArraySectionOffset = 630e8d8bef9SDimitry Andric uip->commonEncodingsArraySectionOffset + 631e8d8bef9SDimitry Andric (uip->commonEncodingsArrayCount * sizeof(uint32_t)); 632e8d8bef9SDimitry Andric uip->personalityArrayCount = personalities.size(); 633e8d8bef9SDimitry Andric uip->indexSectionOffset = uip->personalityArraySectionOffset + 634e8d8bef9SDimitry Andric (uip->personalityArrayCount * sizeof(uint32_t)); 635e8d8bef9SDimitry Andric uip->indexCount = secondLevelPages.size() + 1; 636e8d8bef9SDimitry Andric 637e8d8bef9SDimitry Andric // Common encodings 638e8d8bef9SDimitry Andric auto *i32p = reinterpret_cast<uint32_t *>(&uip[1]); 639e8d8bef9SDimitry Andric for (const auto &encoding : commonEncodings) 640e8d8bef9SDimitry Andric *i32p++ = encoding.first; 641e8d8bef9SDimitry Andric 642e8d8bef9SDimitry Andric // Personalities 64381ad6265SDimitry Andric for (const Symbol *personality : personalities) 64481ad6265SDimitry Andric *i32p++ = personality->getGotVA() - in.header->addr; 645e8d8bef9SDimitry Andric 646*bdd1243dSDimitry Andric // FIXME: LD64 checks and warns aboutgaps or overlapse in cuEntries address 647*bdd1243dSDimitry Andric // ranges. We should do the same too 648*bdd1243dSDimitry Andric 649e8d8bef9SDimitry Andric // Level-1 index 650e8d8bef9SDimitry Andric uint32_t lsdaOffset = 651e8d8bef9SDimitry Andric uip->indexSectionOffset + 652e8d8bef9SDimitry Andric uip->indexCount * sizeof(unwind_info_section_header_index_entry); 653e8d8bef9SDimitry Andric uint64_t l2PagesOffset = level2PagesOffset; 654e8d8bef9SDimitry Andric auto *iep = reinterpret_cast<unwind_info_section_header_index_entry *>(i32p); 655e8d8bef9SDimitry Andric for (const SecondLevelPage &page : secondLevelPages) { 656349cc55cSDimitry Andric size_t idx = cuIndices[page.entryIndex]; 657349cc55cSDimitry Andric iep->functionOffset = cuEntries[idx].functionAddress - in.header->addr; 658e8d8bef9SDimitry Andric iep->secondLevelPagesSectionOffset = l2PagesOffset; 659fe6060f1SDimitry Andric iep->lsdaIndexArraySectionOffset = 660349cc55cSDimitry Andric lsdaOffset + lsdaIndex.lookup(idx) * 661fe6060f1SDimitry Andric sizeof(unwind_info_section_header_lsda_index_entry); 662e8d8bef9SDimitry Andric iep++; 663e8d8bef9SDimitry Andric l2PagesOffset += SECOND_LEVEL_PAGE_BYTES; 664e8d8bef9SDimitry Andric } 665e8d8bef9SDimitry Andric // Level-1 sentinel 666*bdd1243dSDimitry Andric // XXX(vyng): Note that LD64 adds +1 here. 667*bdd1243dSDimitry Andric // Unsure whether it's a bug or it's their workaround for something else. 668*bdd1243dSDimitry Andric // See comments from https://reviews.llvm.org/D138320. 669*bdd1243dSDimitry Andric iep->functionOffset = cueEndBoundary - in.header->addr; 670e8d8bef9SDimitry Andric iep->secondLevelPagesSectionOffset = 0; 671fe6060f1SDimitry Andric iep->lsdaIndexArraySectionOffset = 672349cc55cSDimitry Andric lsdaOffset + entriesWithLsda.size() * 673349cc55cSDimitry Andric sizeof(unwind_info_section_header_lsda_index_entry); 674e8d8bef9SDimitry Andric iep++; 675e8d8bef9SDimitry Andric 676e8d8bef9SDimitry Andric // LSDAs 677349cc55cSDimitry Andric auto *lep = 678349cc55cSDimitry Andric reinterpret_cast<unwind_info_section_header_lsda_index_entry *>(iep); 679349cc55cSDimitry Andric for (size_t idx : entriesWithLsda) { 68081ad6265SDimitry Andric const CompactUnwindEntry &cu = cuEntries[idx]; 68181ad6265SDimitry Andric lep->lsdaOffset = cu.lsda->getVA(/*off=*/0) - in.header->addr; 682349cc55cSDimitry Andric lep->functionOffset = cu.functionAddress - in.header->addr; 683349cc55cSDimitry Andric lep++; 684349cc55cSDimitry Andric } 685e8d8bef9SDimitry Andric 686e8d8bef9SDimitry Andric // Level-2 pages 687349cc55cSDimitry Andric auto *pp = reinterpret_cast<uint32_t *>(lep); 688e8d8bef9SDimitry Andric for (const SecondLevelPage &page : secondLevelPages) { 689e8d8bef9SDimitry Andric if (page.kind == UNWIND_SECOND_LEVEL_COMPRESSED) { 690e8d8bef9SDimitry Andric uintptr_t functionAddressBase = 691349cc55cSDimitry Andric cuEntries[cuIndices[page.entryIndex]].functionAddress; 692e8d8bef9SDimitry Andric auto *p2p = 693e8d8bef9SDimitry Andric reinterpret_cast<unwind_info_compressed_second_level_page_header *>( 694e8d8bef9SDimitry Andric pp); 695e8d8bef9SDimitry Andric p2p->kind = page.kind; 696e8d8bef9SDimitry Andric p2p->entryPageOffset = 697e8d8bef9SDimitry Andric sizeof(unwind_info_compressed_second_level_page_header); 698e8d8bef9SDimitry Andric p2p->entryCount = page.entryCount; 699e8d8bef9SDimitry Andric p2p->encodingsPageOffset = 700e8d8bef9SDimitry Andric p2p->entryPageOffset + p2p->entryCount * sizeof(uint32_t); 701e8d8bef9SDimitry Andric p2p->encodingsCount = page.localEncodings.size(); 702e8d8bef9SDimitry Andric auto *ep = reinterpret_cast<uint32_t *>(&p2p[1]); 703e8d8bef9SDimitry Andric for (size_t i = 0; i < page.entryCount; i++) { 70481ad6265SDimitry Andric const CompactUnwindEntry &cue = 705349cc55cSDimitry Andric cuEntries[cuIndices[page.entryIndex + i]]; 706349cc55cSDimitry Andric auto it = commonEncodingIndexes.find(cue.encoding); 707e8d8bef9SDimitry Andric if (it == commonEncodingIndexes.end()) 708349cc55cSDimitry Andric it = page.localEncodingIndexes.find(cue.encoding); 709e8d8bef9SDimitry Andric *ep++ = (it->second << COMPRESSED_ENTRY_FUNC_OFFSET_BITS) | 710349cc55cSDimitry Andric (cue.functionAddress - functionAddressBase); 711e8d8bef9SDimitry Andric } 712349cc55cSDimitry Andric if (!page.localEncodings.empty()) 713e8d8bef9SDimitry Andric memcpy(ep, page.localEncodings.data(), 714e8d8bef9SDimitry Andric page.localEncodings.size() * sizeof(uint32_t)); 715e8d8bef9SDimitry Andric } else { 716e8d8bef9SDimitry Andric auto *p2p = 717e8d8bef9SDimitry Andric reinterpret_cast<unwind_info_regular_second_level_page_header *>(pp); 718e8d8bef9SDimitry Andric p2p->kind = page.kind; 719e8d8bef9SDimitry Andric p2p->entryPageOffset = 720e8d8bef9SDimitry Andric sizeof(unwind_info_regular_second_level_page_header); 721e8d8bef9SDimitry Andric p2p->entryCount = page.entryCount; 722e8d8bef9SDimitry Andric auto *ep = reinterpret_cast<uint32_t *>(&p2p[1]); 723e8d8bef9SDimitry Andric for (size_t i = 0; i < page.entryCount; i++) { 72481ad6265SDimitry Andric const CompactUnwindEntry &cue = 725349cc55cSDimitry Andric cuEntries[cuIndices[page.entryIndex + i]]; 726349cc55cSDimitry Andric *ep++ = cue.functionAddress; 727349cc55cSDimitry Andric *ep++ = cue.encoding; 728e8d8bef9SDimitry Andric } 729e8d8bef9SDimitry Andric } 730e8d8bef9SDimitry Andric pp += SECOND_LEVEL_PAGE_WORDS; 731e8d8bef9SDimitry Andric } 732e8d8bef9SDimitry Andric } 733fe6060f1SDimitry Andric 734fe6060f1SDimitry Andric UnwindInfoSection *macho::makeUnwindInfoSection() { 73581ad6265SDimitry Andric return make<UnwindInfoSectionImpl>(); 736fe6060f1SDimitry Andric } 737