annotate lld/MachO/UnwindInfoSection.cpp @ 207:2e18cbf3894f

LLVM12
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 08 Jun 2021 06:07:14 +0900
parents
children 5f17cb93ff66
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 //===- UnwindInfoSection.cpp ----------------------------------------------===//
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 #include "UnwindInfoSection.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 #include "ConcatOutputSection.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 #include "Config.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 #include "InputSection.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 #include "OutputSection.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 #include "OutputSegment.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 #include "SymbolTable.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 #include "Symbols.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 #include "SyntheticSections.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 #include "Target.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 #include "lld/Common/ErrorHandler.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 #include "lld/Common/Memory.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 #include "llvm/ADT/STLExtras.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 #include "llvm/ADT/SmallVector.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 #include "llvm/BinaryFormat/MachO.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 using namespace llvm;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 using namespace llvm::MachO;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 using namespace lld;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 using namespace lld::macho;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 #define COMMON_ENCODINGS_MAX 127
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 #define COMPACT_ENCODINGS_MAX 256
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 #define SECOND_LEVEL_PAGE_BYTES 4096
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 #define SECOND_LEVEL_PAGE_WORDS (SECOND_LEVEL_PAGE_BYTES / sizeof(uint32_t))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 #define REGULAR_SECOND_LEVEL_ENTRIES_MAX \
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 ((SECOND_LEVEL_PAGE_BYTES - \
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 sizeof(unwind_info_regular_second_level_page_header)) / \
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 sizeof(unwind_info_regular_second_level_entry))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 #define COMPRESSED_SECOND_LEVEL_ENTRIES_MAX \
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 ((SECOND_LEVEL_PAGE_BYTES - \
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 sizeof(unwind_info_compressed_second_level_page_header)) / \
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 sizeof(uint32_t))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 #define COMPRESSED_ENTRY_FUNC_OFFSET_BITS 24
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 #define COMPRESSED_ENTRY_FUNC_OFFSET_MASK \
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET(~0)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 // Compact Unwind format is a Mach-O evolution of DWARF Unwind that
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 // optimizes space and exception-time lookup. Most DWARF unwind
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 // entries can be replaced with Compact Unwind entries, but the ones
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 // that cannot are retained in DWARF form.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 // This comment will address macro-level organization of the pre-link
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 // and post-link compact unwind tables. For micro-level organization
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 // pertaining to the bitfield layout of the 32-bit compact unwind
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 // entries, see libunwind/include/mach-o/compact_unwind_encoding.h
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 // Important clarifying factoids:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 // * __LD,__compact_unwind is the compact unwind format for compiler
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 // output and linker input. It is never a final output. It could be
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 // an intermediate output with the `-r` option which retains relocs.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 // * __TEXT,__unwind_info is the compact unwind format for final
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 // linker output. It is never an input.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 // * __TEXT,__eh_frame is the DWARF format for both linker input and output.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 // * __TEXT,__unwind_info entries are divided into 4 KiB pages (2nd
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 // level) by ascending address, and the pages are referenced by an
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 // index (1st level) in the section header.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 // * Following the headers in __TEXT,__unwind_info, the bulk of the
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 // section contains a vector of compact unwind entries
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 // `{functionOffset, encoding}` sorted by ascending `functionOffset`.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 // Adjacent entries with the same encoding can be folded to great
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 // advantage, achieving a 3-order-of-magnitude reduction in the
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 // number of entries.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 // * The __TEXT,__unwind_info format can accommodate up to 127 unique
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 // encodings for the space-efficient compressed format. In practice,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
83 // fewer than a dozen unique encodings are used by C++ programs of
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 // all sizes. Therefore, we don't even bother implementing the regular
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 // non-compressed format. Time will tell if anyone in the field ever
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 // overflows the 127-encodings limit.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 // Refer to the definition of unwind_info_section_header in
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 // compact_unwind_encoding.h for an overview of the format we are encoding
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
90 // here.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
91
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 // TODO(gkm): prune __eh_frame entries superseded by __unwind_info, PR50410
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
93 // TODO(gkm): how do we align the 2nd-level pages?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
94
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
95 using EncodingMap = llvm::DenseMap<compact_unwind_encoding_t, size_t>;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
96
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 struct SecondLevelPage {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 uint32_t kind;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 size_t entryIndex;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 size_t entryCount;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 size_t byteCount;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 std::vector<compact_unwind_encoding_t> localEncodings;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 EncodingMap localEncodingIndexes;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
104 };
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
105
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
106 template <class Ptr> class UnwindInfoSectionImpl : public UnwindInfoSection {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
107 public:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
108 void prepareRelocations(InputSection *) override;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 void finalize() override;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
110 void writeTo(uint8_t *buf) const override;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
111
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
112 private:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
113 std::vector<std::pair<compact_unwind_encoding_t, size_t>> commonEncodings;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
114 EncodingMap commonEncodingIndexes;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
115 // Indices of personality functions within the GOT.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
116 std::vector<uint32_t> personalities;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
117 SmallDenseMap<std::pair<InputSection *, uint64_t /* addend */>, Symbol *>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
118 personalityTable;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
119 std::vector<unwind_info_section_header_lsda_index_entry> lsdaEntries;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
120 // Map of function offset (from the image base) to an index within the LSDA
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
121 // array.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
122 llvm::DenseMap<uint32_t, uint32_t> functionToLsdaIndex;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
123 std::vector<CompactUnwindEntry<Ptr>> cuVector;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
124 std::vector<CompactUnwindEntry<Ptr> *> cuPtrVector;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
125 std::vector<SecondLevelPage> secondLevelPages;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
126 uint64_t level2PagesOffset = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
127 };
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
128
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
129 // Compact unwind relocations have different semantics, so we handle them in a
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
130 // separate code path from regular relocations. First, we do not wish to add
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
131 // rebase opcodes for __LD,__compact_unwind, because that section doesn't
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
132 // actually end up in the final binary. Second, personality pointers always
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
133 // reside in the GOT and must be treated specially.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
134 template <class Ptr>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
135 void UnwindInfoSectionImpl<Ptr>::prepareRelocations(InputSection *isec) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
136 assert(isec->segname == segment_names::ld &&
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
137 isec->name == section_names::compactUnwind);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
138 assert(!isec->shouldOmitFromOutput() &&
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
139 "__compact_unwind section should not be omitted");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
140
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
141 // FIXME: This could skip relocations for CompactUnwindEntries that
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
142 // point to dead-stripped functions. That might save some amount of
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
143 // work. But since there are usually just few personality functions
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
144 // that are referenced from many places, at least some of them likely
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
145 // live, it wouldn't reduce number of got entries.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
146 for (Reloc &r : isec->relocs) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
147 assert(target->hasAttr(r.type, RelocAttrBits::UNSIGNED));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
148 if (r.offset % sizeof(CompactUnwindEntry<Ptr>) !=
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
149 offsetof(CompactUnwindEntry<Ptr>, personality))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
150 continue;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
151
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
152 if (auto *s = r.referent.dyn_cast<Symbol *>()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
153 if (auto *undefined = dyn_cast<Undefined>(s)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
154 treatUndefinedSymbol(*undefined);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
155 // treatUndefinedSymbol() can replace s with a DylibSymbol; re-check.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
156 if (isa<Undefined>(s))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
157 continue;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
158 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
159 if (auto *defined = dyn_cast<Defined>(s)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
160 // Check if we have created a synthetic symbol at the same address.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
161 Symbol *&personality =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
162 personalityTable[{defined->isec, defined->value}];
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
163 if (personality == nullptr) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
164 personality = defined;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
165 in.got->addEntry(defined);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
166 } else if (personality != defined) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
167 r.referent = personality;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
168 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
169 continue;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
170 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
171 assert(isa<DylibSymbol>(s));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
172 in.got->addEntry(s);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
173 continue;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
174 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
175
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
176 if (auto *referentIsec = r.referent.dyn_cast<InputSection *>()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
177 assert(!referentIsec->isCoalescedWeak());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
178
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
179 // Personality functions can be referenced via section relocations
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
180 // if they live in the same object file. Create placeholder synthetic
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
181 // symbols for them in the GOT.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
182 Symbol *&s = personalityTable[{referentIsec, r.addend}];
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
183 if (s == nullptr) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
184 // This runs after dead stripping, so the noDeadStrip argument does not
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
185 // matter.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
186 s = make<Defined>("<internal>", /*file=*/nullptr, referentIsec,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
187 r.addend, /*size=*/0, /*isWeakDef=*/false,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
188 /*isExternal=*/false, /*isPrivateExtern=*/false,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
189 /*isThumb=*/false, /*isReferencedDynamically=*/false,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
190 /*noDeadStrip=*/false);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
191 in.got->addEntry(s);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
192 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
193 r.referent = s;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
194 r.addend = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
195 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
196 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
197 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
198
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
199 // Unwind info lives in __DATA, and finalization of __TEXT will occur before
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
200 // finalization of __DATA. Moreover, the finalization of unwind info depends on
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
201 // the exact addresses that it references. So it is safe for compact unwind to
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
202 // reference addresses in __TEXT, but not addresses in any other segment.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
203 static void checkTextSegment(InputSection *isec) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
204 if (isec->segname != segment_names::text)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
205 error("compact unwind references address in " + toString(isec) +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
206 " which is not in segment __TEXT");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
207 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
208
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
209 // We need to apply the relocations to the pre-link compact unwind section
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
210 // before converting it to post-link form. There should only be absolute
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
211 // relocations here: since we are not emitting the pre-link CU section, there
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
212 // is no source address to make a relative location meaningful.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
213 template <class Ptr>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
214 static void
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
215 relocateCompactUnwind(ConcatOutputSection *compactUnwindSection,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
216 std::vector<CompactUnwindEntry<Ptr>> &cuVector) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
217 for (const InputSection *isec : compactUnwindSection->inputs) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
218 assert(isec->parent == compactUnwindSection);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
219
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
220 uint8_t *buf =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
221 reinterpret_cast<uint8_t *>(cuVector.data()) + isec->outSecFileOff;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
222 memcpy(buf, isec->data.data(), isec->data.size());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
223
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
224 for (const Reloc &r : isec->relocs) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
225 uint64_t referentVA = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
226 if (auto *referentSym = r.referent.dyn_cast<Symbol *>()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
227 if (!isa<Undefined>(referentSym)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
228 assert(referentSym->isInGot());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
229 if (auto *defined = dyn_cast<Defined>(referentSym))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
230 checkTextSegment(defined->isec);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
231 // At this point in the link, we may not yet know the final address of
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
232 // the GOT, so we just encode the index. We make it a 1-based index so
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
233 // that we can distinguish the null pointer case.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
234 referentVA = referentSym->gotIndex + 1;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
235 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
236 } else if (auto *referentIsec = r.referent.dyn_cast<InputSection *>()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
237 checkTextSegment(referentIsec);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
238 if (referentIsec->shouldOmitFromOutput())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
239 referentVA = UINT64_MAX; // Tombstone value
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
240 else
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
241 referentVA = referentIsec->getVA() + r.addend;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
242 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
243
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
244 writeAddress(buf + r.offset, referentVA, r.length);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
245 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
246 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
247 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
248
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
249 // There should only be a handful of unique personality pointers, so we can
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
250 // encode them as 2-bit indices into a small array.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
251 template <class Ptr>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
252 void encodePersonalities(
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
253 const std::vector<CompactUnwindEntry<Ptr> *> &cuPtrVector,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
254 std::vector<uint32_t> &personalities) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
255 for (CompactUnwindEntry<Ptr> *cu : cuPtrVector) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
256 if (cu->personality == 0)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
257 continue;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
258 // Linear search is fast enough for a small array.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
259 auto it = find(personalities, cu->personality);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
260 uint32_t personalityIndex; // 1-based index
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
261 if (it != personalities.end()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
262 personalityIndex = std::distance(personalities.begin(), it) + 1;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
263 } else {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
264 personalities.push_back(cu->personality);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
265 personalityIndex = personalities.size();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
266 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
267 cu->encoding |=
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
268 personalityIndex << countTrailingZeros(
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
269 static_cast<compact_unwind_encoding_t>(UNWIND_PERSONALITY_MASK));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
270 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
271 if (personalities.size() > 3)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
272 error("too many personalities (" + std::to_string(personalities.size()) +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
273 ") for compact unwind to encode");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
274 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
275
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
276 // Scan the __LD,__compact_unwind entries and compute the space needs of
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
277 // __TEXT,__unwind_info and __TEXT,__eh_frame
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
278 template <class Ptr> void UnwindInfoSectionImpl<Ptr>::finalize() {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
279 if (compactUnwindSection == nullptr)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
280 return;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
281
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
282 // At this point, the address space for __TEXT,__text has been
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
283 // assigned, so we can relocate the __LD,__compact_unwind entries
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
284 // into a temporary buffer. Relocation is necessary in order to sort
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
285 // the CU entries by function address. Sorting is necessary so that
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
286 // we can fold adjacent CU entries with identical
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
287 // encoding+personality+lsda. Folding is necessary because it reduces
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
288 // the number of CU entries by as much as 3 orders of magnitude!
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
289 compactUnwindSection->finalize();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
290 assert(compactUnwindSection->getSize() % sizeof(CompactUnwindEntry<Ptr>) ==
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
291 0);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
292 size_t cuCount =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
293 compactUnwindSection->getSize() / sizeof(CompactUnwindEntry<Ptr>);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
294 cuVector.resize(cuCount);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
295 relocateCompactUnwind(compactUnwindSection, cuVector);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
296
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
297 // Rather than sort & fold the 32-byte entries directly, we create a
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
298 // vector of pointers to entries and sort & fold that instead.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
299 cuPtrVector.reserve(cuCount);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
300 for (CompactUnwindEntry<Ptr> &cuEntry : cuVector)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
301 cuPtrVector.emplace_back(&cuEntry);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
302 llvm::sort(cuPtrVector, [](const CompactUnwindEntry<Ptr> *a,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
303 const CompactUnwindEntry<Ptr> *b) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
304 return a->functionAddress < b->functionAddress;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
305 });
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
306
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
307 // Dead-stripped functions get a functionAddress of UINT64_MAX in
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
308 // relocateCompactUnwind(). Filter them out here.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
309 // FIXME: This doesn't yet collect associated data like LSDAs kept
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
310 // alive only by a now-removed CompactUnwindEntry or other comdat-like
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
311 // data (`kindNoneGroupSubordinate*` in ld64).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
312 CompactUnwindEntry<Ptr> tombstone;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
313 tombstone.functionAddress = static_cast<Ptr>(UINT64_MAX);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
314 cuPtrVector.erase(
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
315 std::lower_bound(cuPtrVector.begin(), cuPtrVector.end(), &tombstone,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
316 [](const CompactUnwindEntry<Ptr> *a,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
317 const CompactUnwindEntry<Ptr> *b) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
318 return a->functionAddress < b->functionAddress;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
319 }),
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
320 cuPtrVector.end());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
321
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
322 // Fold adjacent entries with matching encoding+personality+lsda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
323 // We use three iterators on the same cuPtrVector to fold in-situ:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
324 // (1) `foldBegin` is the first of a potential sequence of matching entries
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
325 // (2) `foldEnd` is the first non-matching entry after `foldBegin`.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
326 // The semi-open interval [ foldBegin .. foldEnd ) contains a range
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
327 // entries that can be folded into a single entry and written to ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
328 // (3) `foldWrite`
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
329 auto foldWrite = cuPtrVector.begin();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
330 for (auto foldBegin = cuPtrVector.begin(); foldBegin < cuPtrVector.end();) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
331 auto foldEnd = foldBegin;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
332 while (++foldEnd < cuPtrVector.end() &&
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
333 (*foldBegin)->encoding == (*foldEnd)->encoding &&
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
334 (*foldBegin)->personality == (*foldEnd)->personality &&
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
335 (*foldBegin)->lsda == (*foldEnd)->lsda)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
336 ;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
337 *foldWrite++ = *foldBegin;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
338 foldBegin = foldEnd;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
339 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
340 cuPtrVector.erase(foldWrite, cuPtrVector.end());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
341
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
342 encodePersonalities(cuPtrVector, personalities);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
343
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
344 // Count frequencies of the folded encodings
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
345 EncodingMap encodingFrequencies;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
346 for (const CompactUnwindEntry<Ptr> *cuPtrEntry : cuPtrVector)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
347 encodingFrequencies[cuPtrEntry->encoding]++;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
348
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
349 // Make a vector of encodings, sorted by descending frequency
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
350 for (const auto &frequency : encodingFrequencies)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
351 commonEncodings.emplace_back(frequency);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
352 llvm::sort(commonEncodings,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
353 [](const std::pair<compact_unwind_encoding_t, size_t> &a,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
354 const std::pair<compact_unwind_encoding_t, size_t> &b) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
355 if (a.second == b.second)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
356 // When frequencies match, secondarily sort on encoding
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
357 // to maintain parity with validate-unwind-info.py
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
358 return a.first > b.first;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
359 return a.second > b.second;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
360 });
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
361
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
362 // Truncate the vector to 127 elements.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
363 // Common encoding indexes are limited to 0..126, while encoding
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
364 // indexes 127..255 are local to each second-level page
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
365 if (commonEncodings.size() > COMMON_ENCODINGS_MAX)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
366 commonEncodings.resize(COMMON_ENCODINGS_MAX);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
367
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
368 // Create a map from encoding to common-encoding-table index
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
369 for (size_t i = 0; i < commonEncodings.size(); i++)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
370 commonEncodingIndexes[commonEncodings[i].first] = i;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
371
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
372 // Split folded encodings into pages, where each page is limited by ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
373 // (a) 4 KiB capacity
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
374 // (b) 24-bit difference between first & final function address
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
375 // (c) 8-bit compact-encoding-table index,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
376 // for which 0..126 references the global common-encodings table,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
377 // and 127..255 references a local per-second-level-page table.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
378 // First we try the compact format and determine how many entries fit.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
379 // If more entries fit in the regular format, we use that.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
380 for (size_t i = 0; i < cuPtrVector.size();) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
381 secondLevelPages.emplace_back();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
382 SecondLevelPage &page = secondLevelPages.back();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
383 page.entryIndex = i;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
384 uintptr_t functionAddressMax =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
385 cuPtrVector[i]->functionAddress + COMPRESSED_ENTRY_FUNC_OFFSET_MASK;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
386 size_t n = commonEncodings.size();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
387 size_t wordsRemaining =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
388 SECOND_LEVEL_PAGE_WORDS -
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
389 sizeof(unwind_info_compressed_second_level_page_header) /
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
390 sizeof(uint32_t);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
391 while (wordsRemaining >= 1 && i < cuPtrVector.size()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
392 const CompactUnwindEntry<Ptr> *cuPtr = cuPtrVector[i];
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
393 if (cuPtr->functionAddress >= functionAddressMax) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
394 break;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
395 } else if (commonEncodingIndexes.count(cuPtr->encoding) ||
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
396 page.localEncodingIndexes.count(cuPtr->encoding)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
397 i++;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
398 wordsRemaining--;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
399 } else if (wordsRemaining >= 2 && n < COMPACT_ENCODINGS_MAX) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
400 page.localEncodings.emplace_back(cuPtr->encoding);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
401 page.localEncodingIndexes[cuPtr->encoding] = n++;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
402 i++;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
403 wordsRemaining -= 2;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
404 } else {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
405 break;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
406 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
407 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
408 page.entryCount = i - page.entryIndex;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
409
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
410 // If this is not the final page, see if it's possible to fit more
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
411 // entries by using the regular format. This can happen when there
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
412 // are many unique encodings, and we we saturated the local
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
413 // encoding table early.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
414 if (i < cuPtrVector.size() &&
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
415 page.entryCount < REGULAR_SECOND_LEVEL_ENTRIES_MAX) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
416 page.kind = UNWIND_SECOND_LEVEL_REGULAR;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
417 page.entryCount = std::min(REGULAR_SECOND_LEVEL_ENTRIES_MAX,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
418 cuPtrVector.size() - page.entryIndex);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
419 i = page.entryIndex + page.entryCount;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
420 } else {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
421 page.kind = UNWIND_SECOND_LEVEL_COMPRESSED;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
422 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
423 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
424
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
425 for (const CompactUnwindEntry<Ptr> *cu : cuPtrVector) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
426 uint32_t functionOffset = cu->functionAddress - in.header->addr;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
427 functionToLsdaIndex[functionOffset] = lsdaEntries.size();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
428 if (cu->lsda != 0)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
429 lsdaEntries.push_back(
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
430 {functionOffset, static_cast<uint32_t>(cu->lsda - in.header->addr)});
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
431 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
432
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
433 // compute size of __TEXT,__unwind_info section
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
434 level2PagesOffset =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
435 sizeof(unwind_info_section_header) +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
436 commonEncodings.size() * sizeof(uint32_t) +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
437 personalities.size() * sizeof(uint32_t) +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
438 // The extra second-level-page entry is for the sentinel
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
439 (secondLevelPages.size() + 1) *
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
440 sizeof(unwind_info_section_header_index_entry) +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
441 lsdaEntries.size() * sizeof(unwind_info_section_header_lsda_index_entry);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
442 unwindInfoSize =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
443 level2PagesOffset + secondLevelPages.size() * SECOND_LEVEL_PAGE_BYTES;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
444 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
445
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
446 // All inputs are relocated and output addresses are known, so write!
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
447
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
448 template <class Ptr>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
449 void UnwindInfoSectionImpl<Ptr>::writeTo(uint8_t *buf) const {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
450 // section header
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
451 auto *uip = reinterpret_cast<unwind_info_section_header *>(buf);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
452 uip->version = 1;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
453 uip->commonEncodingsArraySectionOffset = sizeof(unwind_info_section_header);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
454 uip->commonEncodingsArrayCount = commonEncodings.size();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
455 uip->personalityArraySectionOffset =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
456 uip->commonEncodingsArraySectionOffset +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
457 (uip->commonEncodingsArrayCount * sizeof(uint32_t));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
458 uip->personalityArrayCount = personalities.size();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
459 uip->indexSectionOffset = uip->personalityArraySectionOffset +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
460 (uip->personalityArrayCount * sizeof(uint32_t));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
461 uip->indexCount = secondLevelPages.size() + 1;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
462
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
463 // Common encodings
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
464 auto *i32p = reinterpret_cast<uint32_t *>(&uip[1]);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
465 for (const auto &encoding : commonEncodings)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
466 *i32p++ = encoding.first;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
467
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
468 // Personalities
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
469 for (const uint32_t &personality : personalities)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
470 *i32p++ =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
471 in.got->addr + (personality - 1) * target->wordSize - in.header->addr;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
472
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
473 // Level-1 index
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
474 uint32_t lsdaOffset =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
475 uip->indexSectionOffset +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
476 uip->indexCount * sizeof(unwind_info_section_header_index_entry);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
477 uint64_t l2PagesOffset = level2PagesOffset;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
478 auto *iep = reinterpret_cast<unwind_info_section_header_index_entry *>(i32p);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
479 for (const SecondLevelPage &page : secondLevelPages) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
480 iep->functionOffset =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
481 cuPtrVector[page.entryIndex]->functionAddress - in.header->addr;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
482 iep->secondLevelPagesSectionOffset = l2PagesOffset;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
483 iep->lsdaIndexArraySectionOffset =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
484 lsdaOffset + functionToLsdaIndex.lookup(iep->functionOffset) *
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
485 sizeof(unwind_info_section_header_lsda_index_entry);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
486 iep++;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
487 l2PagesOffset += SECOND_LEVEL_PAGE_BYTES;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
488 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
489 // Level-1 sentinel
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
490 const CompactUnwindEntry<Ptr> &cuEnd = cuVector.back();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
491 iep->functionOffset = cuEnd.functionAddress + cuEnd.functionLength;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
492 iep->secondLevelPagesSectionOffset = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
493 iep->lsdaIndexArraySectionOffset =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
494 lsdaOffset +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
495 lsdaEntries.size() * sizeof(unwind_info_section_header_lsda_index_entry);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
496 iep++;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
497
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
498 // LSDAs
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
499 size_t lsdaBytes =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
500 lsdaEntries.size() * sizeof(unwind_info_section_header_lsda_index_entry);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
501 if (lsdaBytes > 0)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
502 memcpy(iep, lsdaEntries.data(), lsdaBytes);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
503
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
504 // Level-2 pages
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
505 auto *pp = reinterpret_cast<uint32_t *>(reinterpret_cast<uint8_t *>(iep) +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
506 lsdaBytes);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
507 for (const SecondLevelPage &page : secondLevelPages) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
508 if (page.kind == UNWIND_SECOND_LEVEL_COMPRESSED) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
509 uintptr_t functionAddressBase =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
510 cuPtrVector[page.entryIndex]->functionAddress;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
511 auto *p2p =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
512 reinterpret_cast<unwind_info_compressed_second_level_page_header *>(
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
513 pp);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
514 p2p->kind = page.kind;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
515 p2p->entryPageOffset =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
516 sizeof(unwind_info_compressed_second_level_page_header);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
517 p2p->entryCount = page.entryCount;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
518 p2p->encodingsPageOffset =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
519 p2p->entryPageOffset + p2p->entryCount * sizeof(uint32_t);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
520 p2p->encodingsCount = page.localEncodings.size();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
521 auto *ep = reinterpret_cast<uint32_t *>(&p2p[1]);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
522 for (size_t i = 0; i < page.entryCount; i++) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
523 const CompactUnwindEntry<Ptr> *cuep = cuPtrVector[page.entryIndex + i];
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
524 auto it = commonEncodingIndexes.find(cuep->encoding);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
525 if (it == commonEncodingIndexes.end())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
526 it = page.localEncodingIndexes.find(cuep->encoding);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
527 *ep++ = (it->second << COMPRESSED_ENTRY_FUNC_OFFSET_BITS) |
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
528 (cuep->functionAddress - functionAddressBase);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
529 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
530 if (page.localEncodings.size() != 0)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
531 memcpy(ep, page.localEncodings.data(),
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
532 page.localEncodings.size() * sizeof(uint32_t));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
533 } else {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
534 auto *p2p =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
535 reinterpret_cast<unwind_info_regular_second_level_page_header *>(pp);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
536 p2p->kind = page.kind;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
537 p2p->entryPageOffset =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
538 sizeof(unwind_info_regular_second_level_page_header);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
539 p2p->entryCount = page.entryCount;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
540 auto *ep = reinterpret_cast<uint32_t *>(&p2p[1]);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
541 for (size_t i = 0; i < page.entryCount; i++) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
542 const CompactUnwindEntry<Ptr> *cuep = cuPtrVector[page.entryIndex + i];
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
543 *ep++ = cuep->functionAddress;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
544 *ep++ = cuep->encoding;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
545 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
546 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
547 pp += SECOND_LEVEL_PAGE_WORDS;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
548 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
549 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
550
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
551 UnwindInfoSection *macho::makeUnwindInfoSection() {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
552 if (target->wordSize == 8)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
553 return make<UnwindInfoSectionImpl<uint64_t>>();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
554 else
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
555 return make<UnwindInfoSectionImpl<uint32_t>>();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
556 }