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