forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllvmorg-16-init-17468-gc512eda38ebe.patch
307 lines (289 loc) · 10.9 KB
/
llvmorg-16-init-17468-gc512eda38ebe.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
From dac3eda77c5bd2f60f2d682a495ce4745925377d Mon Sep 17 00:00:00 2001
From: serge-sans-paille <[email protected]>
Date: Fri, 13 Jan 2023 14:25:54 +0100
Subject: [PATCH] [lld][COFF] Provide unwinding information for Chunk injected
by /delayloaded
For each symbol in a /delayloaded library, lld injects a small piece of
code to handle the symbol lazy loading. This code doesn't have unwind
information, which may be troublesome.
Provide these information for AMD64.
Thanks to Yannis Juglaret <[email protected]> for contributing the
unwinding info and for his support while crafting this patch.
Fix #59639
Differential Revision: https://reviews.llvm.org/D141691
---
lld/COFF/DLL.cpp | 77 +++++++++++++++++++++++++++-
lld/COFF/DLL.h | 6 +++
lld/COFF/Writer.cpp | 4 ++
lld/test/COFF/delayimports.test | 27 +++++++++-
lld/test/COFF/delayimporttables.yaml | 4 +-
lld/test/COFF/giats.s | 6 +--
6 files changed, 117 insertions(+), 7 deletions(-)
diff --git a/lld/COFF/DLL.cpp b/lld/COFF/DLL.cpp
index 42a5a41f87ae..000f8b6dab2e 100644
--- a/lld/COFF/DLL.cpp
+++ b/lld/COFF/DLL.cpp
@@ -221,6 +221,19 @@ static const uint8_t tailMergeX64[] = {
0xFF, 0xE0, // jmp rax
};
+static const uint8_t tailMergeUnwindInfoX64[] = {
+ 0x01, // Version=1, Flags=UNW_FLAG_NHANDLER
+ 0x0a, // Size of prolog
+ 0x05, // Count of unwind codes
+ 0x00, // No frame register
+ 0x0a, 0x82, // Offset 0xa: UWOP_ALLOC_SMALL(0x48)
+ 0x06, 0x02, // Offset 6: UWOP_ALLOC_SMALL(8)
+ 0x04, 0x02, // Offset 4: UWOP_ALLOC_SMALL(8)
+ 0x02, 0x02, // Offset 2: UWOP_ALLOC_SMALL(8)
+ 0x01, 0x02, // Offset 1: UWOP_ALLOC_SMALL(8)
+ 0x00, 0x00 // Padding to align on 32-bits
+};
+
static const uint8_t thunkX86[] = {
0xB8, 0, 0, 0, 0, // mov eax, offset ___imp__<FUNCNAME>
0xE9, 0, 0, 0, 0, // jmp __tailMerge_<lib>
@@ -324,6 +337,41 @@ public:
Defined *helper = nullptr;
};
+class TailMergePDataChunkX64 : public NonSectionChunk {
+public:
+ TailMergePDataChunkX64(Chunk *tm, Chunk *unwind) : tm(tm), unwind(unwind) {
+ // See
+ // https://learn.microsoft.com/en-us/cpp/build/exception-handling-x64#struct-runtime_function
+ setAlignment(4);
+ }
+
+ size_t getSize() const override { return 3 * sizeof(uint32_t); }
+
+ void writeTo(uint8_t *buf) const override {
+ write32le(buf + 0, tm->getRVA()); // TailMergeChunk start RVA
+ write32le(buf + 4, tm->getRVA() + tm->getSize()); // TailMergeChunk stop RVA
+ write32le(buf + 8, unwind->getRVA()); // UnwindInfo RVA
+ }
+
+ Chunk *tm = nullptr;
+ Chunk *unwind = nullptr;
+};
+
+class TailMergeUnwindInfoX64 : public NonSectionChunk {
+public:
+ TailMergeUnwindInfoX64() {
+ // See
+ // https://learn.microsoft.com/en-us/cpp/build/exception-handling-x64#struct-unwind_info
+ setAlignment(4);
+ }
+
+ size_t getSize() const override { return sizeof(tailMergeUnwindInfoX64); }
+
+ void writeTo(uint8_t *buf) const override {
+ memcpy(buf, tailMergeUnwindInfoX64, sizeof(tailMergeUnwindInfoX64));
+ }
+};
+
class ThunkChunkX86 : public NonSectionChunk {
public:
ThunkChunkX86(Defined *i, Chunk *tm) : imp(i), tailMerge(tm) {}
@@ -636,6 +684,8 @@ void DelayLoadContents::create(COFFLinkerContext &ctx, Defined *h) {
helper = h;
std::vector<std::vector<DefinedImportData *>> v = binImports(imports);
+ Chunk *unwind = newTailMergeUnwindInfoChunk();
+
// Create .didat contents for each DLL.
for (std::vector<DefinedImportData *> &syms : v) {
// Create the delay import table header.
@@ -644,6 +694,7 @@ void DelayLoadContents::create(COFFLinkerContext &ctx, Defined *h) {
size_t base = addresses.size();
Chunk *tm = newTailMergeChunk(dir);
+ Chunk *pdataChunk = unwind ? newTailMergePDataChunk(tm, unwind) : nullptr;
for (DefinedImportData *s : syms) {
Chunk *t = newThunkChunk(s, tm);
auto *a = make<DelayAddressChunk>(t);
@@ -656,7 +707,7 @@ void DelayLoadContents::create(COFFLinkerContext &ctx, Defined *h) {
auto *c = make<HintNameChunk>(extName, 0);
names.push_back(make<LookupChunk>(c));
hintNames.push_back(c);
- // Add a syntentic symbol for this load thunk, using the "__imp_load"
+ // Add a synthetic symbol for this load thunk, using the "__imp_load"
// prefix, in case this thunk needs to be added to the list of valid
// call targets for Control Flow Guard.
StringRef symName = saver().save("__imp_load_" + extName);
@@ -665,6 +716,8 @@ void DelayLoadContents::create(COFFLinkerContext &ctx, Defined *h) {
}
}
thunks.push_back(tm);
+ if (pdataChunk)
+ pdata.push_back(pdataChunk);
StringRef tmName =
saver().save("__tailMerge_" + syms[0]->getDLLName().lower());
ctx.symtab.addSynthetic(tmName, tm);
@@ -684,6 +737,9 @@ void DelayLoadContents::create(COFFLinkerContext &ctx, Defined *h) {
dir->nameTab = names[base];
dirs.push_back(dir);
}
+
+ if (unwind)
+ unwindinfo.push_back(unwind);
// Add null terminator.
dirs.push_back(make<NullChunk>(sizeof(delay_import_directory_table_entry)));
}
@@ -703,6 +759,25 @@ Chunk *DelayLoadContents::newTailMergeChunk(Chunk *dir) {
}
}
+Chunk *DelayLoadContents::newTailMergeUnwindInfoChunk() {
+ switch (config->machine) {
+ case AMD64:
+ return make<TailMergeUnwindInfoX64>();
+ // FIXME: Add support for other architectures.
+ default:
+ return nullptr; // Just don't generate unwind info.
+ }
+}
+Chunk *DelayLoadContents::newTailMergePDataChunk(Chunk *tm, Chunk *unwind) {
+ switch (config->machine) {
+ case AMD64:
+ return make<TailMergePDataChunkX64>(tm, unwind);
+ // FIXME: Add support for other architectures.
+ default:
+ return nullptr; // Just don't generate unwind info.
+ }
+}
+
Chunk *DelayLoadContents::newThunkChunk(DefinedImportData *s,
Chunk *tailMerge) {
switch (config->machine) {
diff --git a/lld/COFF/DLL.h b/lld/COFF/DLL.h
index 0d594e675bd2..b4cd654440a9 100644
--- a/lld/COFF/DLL.h
+++ b/lld/COFF/DLL.h
@@ -44,6 +44,8 @@ public:
std::vector<Chunk *> getChunks();
std::vector<Chunk *> getDataChunks();
ArrayRef<Chunk *> getCodeChunks() { return thunks; }
+ ArrayRef<Chunk *> getCodePData() { return pdata; }
+ ArrayRef<Chunk *> getCodeUnwindInfo() { return unwindinfo; }
uint64_t getDirRVA() { return dirs[0]->getRVA(); }
uint64_t getDirSize();
@@ -51,6 +53,8 @@ public:
private:
Chunk *newThunkChunk(DefinedImportData *s, Chunk *tailMerge);
Chunk *newTailMergeChunk(Chunk *dir);
+ Chunk *newTailMergePDataChunk(Chunk *tm, Chunk *unwind);
+ Chunk *newTailMergeUnwindInfoChunk();
Defined *helper;
std::vector<DefinedImportData *> imports;
@@ -60,6 +64,8 @@ private:
std::vector<Chunk *> names;
std::vector<Chunk *> hintNames;
std::vector<Chunk *> thunks;
+ std::vector<Chunk *> pdata;
+ std::vector<Chunk *> unwindinfo;
std::vector<Chunk *> dllNames;
};
diff --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp
index f39697d5d381..3235255ae2be 100644
--- a/lld/COFF/Writer.cpp
+++ b/lld/COFF/Writer.cpp
@@ -1063,6 +1063,10 @@ void Writer::appendImportThunks() {
dataSec->addChunk(c);
for (Chunk *c : delayIdata.getCodeChunks())
textSec->addChunk(c);
+ for (Chunk *c : delayIdata.getCodePData())
+ pdataSec->addChunk(c);
+ for (Chunk *c : delayIdata.getCodeUnwindInfo())
+ rdataSec->addChunk(c);
}
}
diff --git a/lld/test/COFF/delayimports.test b/lld/test/COFF/delayimports.test
index 3a6db67e98db..f410eef35fd1 100644
--- a/lld/test/COFF/delayimports.test
+++ b/lld/test/COFF/delayimports.test
@@ -3,13 +3,14 @@
# RUN: /alternatename:__delayLoadHelper2=main
# RUN: llvm-readobj --coff-imports %t.exe | FileCheck -check-prefix=IMPORT %s
# RUN: llvm-readobj --coff-basereloc %t.exe | FileCheck -check-prefix=BASEREL %s
+# RUN: llvm-readobj --unwind %t.exe | FileCheck -check-prefix=UNWIND %s
IMPORT: DelayImport {
IMPORT-NEXT: Name: std64.dll
IMPORT-NEXT: Attributes: 0x1
IMPORT-NEXT: ModuleHandle: 0x3018
IMPORT-NEXT: ImportAddressTable: 0x3020
-IMPORT-NEXT: ImportNameTable: 0x2040
+IMPORT-NEXT: ImportNameTable: 0x2050
IMPORT-NEXT: BoundDelayImportTable: 0x0
IMPORT-NEXT: UnloadDelayImportTable: 0x0
IMPORT-NEXT: Import {
@@ -39,3 +40,27 @@ BASEREL-NEXT: Entry {
BASEREL-NEXT: Type: DIR64
BASEREL-NEXT: Address: 0x3030
BASEREL-NEXT: }
+
+UNWIND: UnwindInformation [
+UNWIND-NEXT: RuntimeFunction {
+UNWIND-NEXT: StartAddress: (0x14000108A)
+UNWIND-NEXT: EndAddress: (0x1400010DD)
+UNWIND-NEXT: UnwindInfoAddress: (0x140002000)
+UNWIND-NEXT: UnwindInfo {
+UNWIND-NEXT: Version: 1
+UNWIND-NEXT: Flags [ (0x0)
+UNWIND-NEXT: ]
+UNWIND-NEXT: PrologSize: 10
+UNWIND-NEXT: FrameRegister: -
+UNWIND-NEXT: FrameOffset: -
+UNWIND-NEXT: UnwindCodeCount: 5
+UNWIND-NEXT: UnwindCodes [
+UNWIND-NEXT: 0x0A: ALLOC_SMALL size=72
+UNWIND-NEXT: 0x06: ALLOC_SMALL size=8
+UNWIND-NEXT: 0x04: ALLOC_SMALL size=8
+UNWIND-NEXT: 0x02: ALLOC_SMALL size=8
+UNWIND-NEXT: 0x01: ALLOC_SMALL size=8
+UNWIND-NEXT: ]
+UNWIND-NEXT: }
+UNWIND-NEXT: }
+UNWIND-NEXT: ]
diff --git a/lld/test/COFF/delayimporttables.yaml b/lld/test/COFF/delayimporttables.yaml
index c9ceaab48266..f1e7c61f55a5 100644
--- a/lld/test/COFF/delayimporttables.yaml
+++ b/lld/test/COFF/delayimporttables.yaml
@@ -14,7 +14,7 @@
# CHECK-NEXT: Attributes: 0x1
# CHECK-NEXT: ModuleHandle: 0x3000
# CHECK-NEXT: ImportAddressTable: 0x3010
-# CHECK-NEXT: ImportNameTable: 0x2060
+# CHECK-NEXT: ImportNameTable: 0x2070
# CHECK-NEXT: BoundDelayImportTable: 0x0
# CHECK-NEXT: UnloadDelayImportTable: 0x0
# CHECK-NEXT: Import {
@@ -31,7 +31,7 @@
# CHECK-NEXT: Attributes: 0x1
# CHECK-NEXT: ModuleHandle: 0x3008
# CHECK-NEXT: ImportAddressTable: 0x3028
-# CHECK-NEXT: ImportNameTable: 0x2078
+# CHECK-NEXT: ImportNameTable: 0x2088
# CHECK-NEXT: BoundDelayImportTable: 0x0
# CHECK-NEXT: UnloadDelayImportTable: 0x0
# CHECK-NEXT: Import {
diff --git a/lld/test/COFF/giats.s b/lld/test/COFF/giats.s
index 4a6147237737..93369b39bf5d 100644
--- a/lld/test/COFF/giats.s
+++ b/lld/test/COFF/giats.s
@@ -33,10 +33,10 @@
# DELAY-CHECK: ImageBase: 0x140000000
# DELAY-CHECK: LoadConfig [
-# DELAY-CHECK: GuardCFFunctionTable: 0x140002114
+# DELAY-CHECK: GuardCFFunctionTable: 0x140002124
# DELAY-CHECK: GuardCFFunctionCount: 2
# DELAY-CHECK: GuardFlags: 0x10500
-# DELAY-CHECK: GuardAddressTakenIatEntryTable: 0x14000211C
+# DELAY-CHECK: GuardAddressTakenIatEntryTable: 0x14000212C
# DELAY-CHECK: GuardAddressTakenIatEntryCount: 1
# DELAY-CHECK: ]
# DELAY-CHECK: GuardFidTable [
@@ -114,4 +114,4 @@ _load_config_used:
.quad __guard_iat_count
.quad __guard_longjmp_table
.quad __guard_fids_count
- .fill 84, 1, 0
\ No newline at end of file
+ .fill 84, 1, 0
--
2.39.0