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