llvm.org GIT mirror llvm / e10866a
[ARM64][Windows] Add unwind support to llvm-readobj This patch adds support for dumping the unwind info from ARM64 COFF object files. Differential Revision: https://reviews.llvm.org/D53264 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@345108 91177308-0d34-0410-b5e6-96231b3b80d8 Sanjin Sijaric 1 year, 7 months ago
10 changed file(s) with 713 addition(s) and 56 deletion(s). Raw diff Collapse all Expand all
206206
207207 /// ExceptionDataRecord - An entry in the table of exception data (.xdata)
208208 ///
209 /// The format on ARM is:
210 ///
209211 /// 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
210212 /// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
211213 /// +-------+---------+-+-+-+---+-----------------------------------+
212214 /// | C Wrd | Epi Cnt |F|E|X|Ver| Function Length |
213215 /// +-------+--------+'-'-'-'---'---+-------------------------------+
216 /// | Reserved |Ex. Code Words| (Extended Epilogue Count) |
217 /// +-------+--------+--------------+-------------------------------+
218 ///
219 /// The format on ARM64 is:
220 ///
221 /// 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
222 /// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
223 /// +---------+---------+-+-+---+-----------------------------------+
224 /// | C Wrd | Epi Cnt |E|X|Ver| Function Length |
225 /// +---------+------+--'-'-'---'---+-------------------------------+
214226 /// | Reserved |Ex. Code Words| (Extended Epilogue Count) |
215227 /// +-------+--------+--------------+-------------------------------+
216228 ///
224236 /// header
225237 /// F : 1-bit field indicating that the record describes a function fragment
226238 /// (implies that no prologue is present, and prologue processing should be
227 /// skipped)
239 /// skipped) (ARM only)
228240 /// Epilogue Count : 5-bit field that differs in meaning based on the E field.
229241 ///
230242 /// If E is set, then this field specifies the index of the
234246 /// scopes. If more than 31 scopes exist, then this field and
235247 /// the Code Words field must both be set to 0 to indicate that
236248 /// an extension word is required.
237 /// Code Words : 4-bit field that species the number of 32-bit words needed to
238 /// contain all the unwind codes. If more than 15 words (63 code
239 /// bytes) are required, then this field and the Epilogue Count
240 /// field must both be set to 0 to indicate that an extension word
241 /// is required.
249 /// Code Words : 4-bit (5-bit on ARM64) field that specifies the number of
250 /// 32-bit words needed to contain all the unwind codes. If more
251 /// than 15 words (31 words on ARM64) are required, then this field
252 /// and the Epilogue Count field must both be set to 0 to indicate
253 /// that an extension word is required.
242254 /// Extended Epilogue Count, Extended Code Words :
243255 /// Valid only if Epilog Count and Code Words are both
244256 /// set to 0. Provides an 8-bit extended code word
245257 /// count and 16-bits for epilogue count
246258 ///
259 /// The epilogue scope format on ARM is:
260 ///
247261 /// 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
248262 /// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
249263 /// +----------------+------+---+---+-------------------------------+
250264 /// | Ep Start Idx | Cond |Res| Epilogue Start Offset |
251265 /// +----------------+------+---+-----------------------------------+
252266 ///
267 /// The epilogue scope format on ARM64 is:
268 ///
269 /// 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
270 /// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
271 /// +-------------------+-------+---+-------------------------------+
272 /// | Ep Start Idx | Res | Epilogue Start Offset |
273 /// +-------------------+-------+-----------------------------------+
274 ///
253275 /// If the E bit is unset in the header, the header is followed by a series of
254276 /// epilogue scopes, which are sorted by their offset.
255277 ///
256278 /// Epilogue Start Offset: 18-bit field encoding the offset of epilogue relative
257279 /// to the start of the function in bytes divided by two
258280 /// Res : 2-bit field reserved for future expansion (must be set to 0)
259 /// Condition : 4-bit field providing the condition under which the epilogue is
260 /// executed. Unconditional epilogues should set this field to 0xe.
261 /// Epilogues must be entirely conditional or unconditional, and in
262 /// Thumb-2 mode. The epilogue beings with the first instruction
263 /// after the IT opcode.
281 /// Condition : (ARM only) 4-bit field providing the condition under which the
282 /// epilogue is executed. Unconditional epilogues should set this
283 /// field to 0xe. Epilogues must be entirely conditional or
284 /// unconditional, and in Thumb-2 mode. The epilogue begins with
285 /// the first instruction after the IT opcode.
264286 /// Epilogue Start Index : 8-bit field indicating the byte index of the first
265287 /// unwind code describing the epilogue
266288 ///
292314 const support::ulittle32_t ES;
293315
294316 EpilogueScope(const support::ulittle32_t Data) : ES(Data) {}
317 // Same for both ARM and AArch64.
295318 uint32_t EpilogueStartOffset() const {
296319 return (ES & 0x0003ffff);
297320 }
298 uint8_t Res() const {
321
322 // Different implementations for ARM and AArch64.
323 uint8_t ResARM() const {
299324 return ((ES & 0x000c0000) >> 18);
300325 }
326
327 uint8_t ResAArch64() const {
328 return ((ES & 0x000f0000) >> 18);
329 }
330
331 // Condition is only applicable to ARM.
301332 uint8_t Condition() const {
302333 return ((ES & 0x00f00000) >> 20);
303334 }
304 uint8_t EpilogueStartIndex() const {
335
336 // Different implementations for ARM and AArch64.
337 uint8_t EpilogueStartIndexARM() const {
305338 return ((ES & 0xff000000) >> 24);
339 }
340
341 uint8_t EpilogueStartIndexAArch64() const {
342 return ((ES & 0xffc00000) >> 22);
306343 }
307344 };
308345
311348
312349 struct ExceptionDataRecord {
313350 const support::ulittle32_t *Data;
314
315 ExceptionDataRecord(const support::ulittle32_t *Data) : Data(Data) {}
351 bool isAArch64;
352
353 ExceptionDataRecord(const support::ulittle32_t *Data, bool isAArch64) :
354 Data(Data), isAArch64(isAArch64) {}
316355
317356 uint32_t FunctionLength() const {
318357 return (Data[0] & 0x0003ffff);
319358 }
320359
360 uint32_t FunctionLengthInBytesARM() const {
361 return FunctionLength() << 1;
362 }
363
364 uint32_t FunctionLengthInBytesAArch64() const {
365 return FunctionLength() << 2;
366 }
367
321368 uint8_t Vers() const {
322369 return (Data[0] & 0x000C0000) >> 18;
323370 }
331378 }
332379
333380 bool F() const {
381 assert(!isAArch64 && "Fragments are only supported on ARMv7 WinEH");
334382 return ((Data[0] & 0x00400000) >> 22);
335383 }
336384
337385 uint8_t EpilogueCount() const {
338 if (HeaderWords(*this) == 1)
386 if (HeaderWords(*this) == 1) {
387 if (isAArch64)
388 return (Data[0] & 0x07C00000) >> 22;
339389 return (Data[0] & 0x0f800000) >> 23;
390 }
340391 return Data[1] & 0x0000ffff;
341392 }
342393
343394 uint8_t CodeWords() const {
344 if (HeaderWords(*this) == 1)
395 if (HeaderWords(*this) == 1) {
396 if (isAArch64)
397 return (Data[0] & 0xf8000000) >> 27;
345398 return (Data[0] & 0xf0000000) >> 28;
399 }
346400 return (Data[1] & 0x00ff0000) >> 16;
347401 }
348402
372426 };
373427
374428 inline size_t HeaderWords(const ExceptionDataRecord &XR) {
429 if (XR.isAArch64)
430 return (XR.Data[0] & 0xffc0000) ? 1 : 2;
375431 return (XR.Data[0] & 0xff800000) ? 1 : 2;
376432 }
377433 }
0 ## Check that error handling for bad opcodes works.
1 ## .xdata below contains the bad opcode 0xdf in the 4th word of .xdata.
2
3 // REQUIRES: aarch64-registered-target
4 // RUN: llvm-mc -filetype=obj -triple aarch64-windows %s -o - \
5 // RUN: | llvm-readobj -unwind - | FileCheck %s
6
7 // CHECK: Prologue [
8 // CHECK: 0xdf ; Bad opcode!
9 // CHECK: 0xd600 ; stp x19, lr, [sp, #0]
10 // CHECK: 0x01 ; sub sp, #16
11 // CHECK: 0xe4 ; end
12 // CHECK: ]
13
14 .text
15 .globl "?func@@YAHXZ"
16 .p2align 3
17 "?func@@YAHXZ":
18 sub sp,sp,#0x10
19 stp x19,lr,[sp]
20 sub sp,sp,#0x1F0
21 mov w19,w0
22 bl "?func2@@YAXXZ"
23 cmp w19,#2
24 ble .LBB0_1
25 bl "?func2@@YAHXZ"
26 add sp,sp,#0x1F0
27 ldp x19,lr,[sp]
28 add sp,sp,#0x10
29 ret
30 .LBB0_1:
31 mov x0,sp
32 bl "?func3@@YAHPEAH@Z"
33 add sp,sp,#0x1F0
34 ldp x19,lr,[sp]
35 add sp,sp,#0x10
36 ret
37
38
39 .section .pdata,"dr"
40 .long "?func@@YAHXZ"@IMGREL
41 .long "$unwind$func@@YAHXZ"@IMGREL
42
43
44 .section .xdata,"dr"
45 "$unwind$func@@YAHXZ":
46 .p2align 3
47 .long 0x10800012
48 .long 0x8
49 .long 0xe
50 .long 0x100d6df
51 .long 0xe3e3e3e4
52
0 ## Check that the sanity check for an inconsistent header works.
1 ## The first word contains the bad value for CodeWords, 0xf, which indicates
2 ## that we need 0x11110 << 2 = 120 bytes of space for the unwind codes.
3 ## It follows that the .xdata section is badly formed as only 8 bytes are
4 ## allocated for the unwind codes.
5
6 // REQUIRES: aarch64-registered-target
7 // RUN: llvm-mc -filetype=obj -triple aarch64-windows %s -o - \
8 // RUN: | not llvm-readobj -unwind - 2>&1 | FileCheck %s
9
10 // CHECK: LLVM ERROR: Malformed unwind data
11
12 .text
13 .globl "?func@@YAHXZ"
14 .p2align 3
15 "?func@@YAHXZ":
16 sub sp,sp,#0x10
17 stp x19,lr,[sp]
18 sub sp,sp,#0x1F0
19 mov w19,w0
20 bl "?func2@@YAXXZ"
21 cmp w19,#2
22 ble .LBB0_1
23 bl "?func2@@YAHXZ"
24 add sp,sp,#0x1F0
25 ldp x19,lr,[sp]
26 add sp,sp,#0x10
27 ret
28 .LBB0_1:
29 mov x0,sp
30 bl "?func3@@YAHPEAH@Z"
31 add sp,sp,#0x1F0
32 ldp x19,lr,[sp]
33 add sp,sp,#0x10
34 ret
35
36 .section .pdata,"dr"
37 .long "?func@@YAHXZ"@IMGREL
38 .long "$unwind$func@@YAHXZ"@IMGREL
39
40
41 .section .xdata,"dr"
42 "$unwind$func@@YAHXZ":
43 .p2align 3
44 .long 0xf0800012
45 .long 0x8
46 .long 0xe
47 .long 0x100d61f
48 .long 0xe3e3e3e4
49
0 ## Check that error handling for going past the unwind data works.
1 ## .xdata below contains bad opcodes in the last word. The last byte, 0xe0,
2 ## indicates that we have come across alloc_l, which requires 4 bytes. In this
3 ## case, unwind code processing will go past the allocated unwind data.
4
5 // REQUIRES: aarch64-registered-target
6 // RUN: llvm-mc -filetype=obj -triple aarch64-windows %s -o - \
7 // RUN: | llvm-readobj -unwind - | FileCheck %s
8
9 // CHECK: Prologue [
10 // CHECK: Opcode 0xe0 goes past the unwind data
11
12 .text
13 .globl "?func@@YAHXZ"
14 .p2align 3
15 "?func@@YAHXZ":
16 sub sp,sp,#0x10
17 stp x19,lr,[sp]
18 sub sp,sp,#0x1F0
19 mov w19,w0
20 bl "?func2@@YAXXZ"
21 cmp w19,#2
22 ble .LBB0_1
23 bl "?func2@@YAHXZ"
24 add sp,sp,#0x1F0
25 ldp x19,lr,[sp]
26 add sp,sp,#0x10
27 ret
28 .LBB0_1:
29 mov x0,sp
30 bl "?func3@@YAHPEAH@Z"
31 add sp,sp,#0x1F0
32 ldp x19,lr,[sp]
33 add sp,sp,#0x10
34 ret
35
36
37 .section .pdata,"dr"
38 .long "?func@@YAHXZ"@IMGREL
39 .long "$unwind$func@@YAHXZ"@IMGREL
40
41
42 .section .xdata,"dr"
43 "$unwind$func@@YAHXZ":
44 .p2align 3
45 .long 0x10800012
46 .long 0x8
47 .long 0xe
48 .long 0x100d61f
49 .long 0xe0000000
50
0 RUN: llvm-readobj -unwind %p/Inputs/arm64-win1.obj | FileCheck %s -check-prefix=UNWIND1
1 RUN: llvm-readobj -unwind %p/Inputs/arm64-win2.obj | FileCheck %s -check-prefix=UNWIND2
2
3 UNWIND1: ExceptionData {
4 UNWIND1-NEXT: FunctionLength: 340
5 UNWIND1-NEXT: Version: 0
6 UNWIND1-NEXT: ExceptionData: No
7 UNWIND1-NEXT: EpiloguePacked: Yes
8 UNWIND1-NEXT: EpilogueOffset: 15
9 UNWIND1-NEXT: ByteCodeLength: 28
10 UNWIND1-NEXT: Prologue [
11 UNWIND1-NEXT: 0xe002dac8 ; sub sp, #2993280
12 UNWIND1-NEXT: 0xe3 ; nop
13 UNWIND1-NEXT: 0xe3 ; nop
14 UNWIND1-NEXT: 0xe3 ; nop
15 UNWIND1-NEXT: 0xd885 ; stp d10, d11, [sp, #40]
16 UNWIND1-NEXT: 0xd803 ; stp d8, d9, [sp, #24]
17 UNWIND1-NEXT: 0xd2c2 ; str x30, [sp, #16]
18 UNWIND1-NEXT: 0x28 ; stp x19, x20, [sp, #-64]!
19 UNWIND1-NEXT: 0xe4 ; end
20 UNWIND1-NEXT: ]
21 UNWIND1-NEXT: Epilogue [
22 UNWIND1-NEXT: 0xe002dac8 ; add sp, #2993280
23 UNWIND1-NEXT: 0xd885 ; ldp d10, d11, [sp, #40]
24 UNWIND1-NEXT: 0xd803 ; ldp d8, d9, [sp, #24]
25 UNWIND1-NEXT: 0xd2c2 ; ldr x30, [sp, #16]
26 UNWIND1-NEXT: 0x28 ; ldp x19, x20, [sp], #64
27 UNWIND1-NEXT: 0xe4 ; end
28 UNWIND1-NEXT: ]
29 UNWIND1_NEXT: }
30
31
32 UNWIND2: ExceptionData {
33 UNWIND2-NEXT: FunctionLength: 72
34 UNWIND2-NEXT: Version: 0
35 UNWIND2-NEXT: ExceptionData: No
36 UNWIND2-NEXT: EpiloguePacked: No
37 UNWIND2-NEXT: EpilogueScopes: 2
38 UNWIND2-NEXT: ByteCodeLength: 8
39 UNWIND2-NEXT: Prologue [
40 UNWIND2-NEXT: 0x1f ; sub sp, #496
41 UNWIND2-NEXT: 0xd600 ; stp x19, lr, [sp, #0]
42 UNWIND2-NEXT: 0x01 ; sub sp, #16
43 UNWIND2-NEXT: 0xe4 ; end
44 UNWIND2-NEXT: ]
45 UNWIND2-NEXT: EpilogueScopes [
46 UNWIND2-NEXT: EpilogueScope {
47 UNWIND2-NEXT: StartOffset: 8
48 UNWIND2-NEXT: EpilogueStartIndex: 0
49 UNWIND2-NEXT: Opcodes [
50 UNWIND2-NEXT: 0x1f ; add sp, #496
51 UNWIND2-NEXT: 0xd600 ; ldp x19, lr, [sp, #0]
52 UNWIND2-NEXT: 0x01 ; add sp, #16
53 UNWIND2-NEXT: 0xe4 ; end
54 UNWIND2-NEXT: ]
55 UNWIND2-NEXT: }
56 UNWIND2-NEXT: EpilogueScope {
57 UNWIND2-NEXT: StartOffset: 14
58 UNWIND2-NEXT: EpilogueStartIndex: 0
59 UNWIND2-NEXT: Opcodes [
60 UNWIND2-NEXT: 0x1f ; add sp, #496
61 UNWIND2-NEXT: 0xd600 ; ldp x19, lr, [sp, #0]
62 UNWIND2-NEXT: 0x01 ; add sp, #16
63 UNWIND2-NEXT: 0xe4 ; end
64 UNWIND2-NEXT: ]
65 UNWIND2-NEXT: }
66 UNWIND2-NEXT: ]
67 UNWIND2-NEXT: }
68
117117
118118 // TODO name the uops more appropriately
119119 const Decoder::RingEntry Decoder::Ring[] = {
120 { 0x80, 0x00, &Decoder::opcode_0xxxxxxx }, // UOP_STACK_FREE (16-bit)
121 { 0xc0, 0x80, &Decoder::opcode_10Lxxxxx }, // UOP_POP (32-bit)
122 { 0xf0, 0xc0, &Decoder::opcode_1100xxxx }, // UOP_STACK_SAVE (16-bit)
123 { 0xf8, 0xd0, &Decoder::opcode_11010Lxx }, // UOP_POP (16-bit)
124 { 0xf8, 0xd8, &Decoder::opcode_11011Lxx }, // UOP_POP (32-bit)
125 { 0xf8, 0xe0, &Decoder::opcode_11100xxx }, // UOP_VPOP (32-bit)
126 { 0xfc, 0xe8, &Decoder::opcode_111010xx }, // UOP_STACK_FREE (32-bit)
127 { 0xfe, 0xec, &Decoder::opcode_1110110L }, // UOP_POP (16-bit)
128 { 0xff, 0xee, &Decoder::opcode_11101110 }, // UOP_MICROSOFT_SPECIFIC (16-bit)
120 { 0x80, 0x00, 1, &Decoder::opcode_0xxxxxxx }, // UOP_STACK_FREE (16-bit)
121 { 0xc0, 0x80, 2, &Decoder::opcode_10Lxxxxx }, // UOP_POP (32-bit)
122 { 0xf0, 0xc0, 1, &Decoder::opcode_1100xxxx }, // UOP_STACK_SAVE (16-bit)
123 { 0xf8, 0xd0, 1, &Decoder::opcode_11010Lxx }, // UOP_POP (16-bit)
124 { 0xf8, 0xd8, 1, &Decoder::opcode_11011Lxx }, // UOP_POP (32-bit)
125 { 0xf8, 0xe0, 1, &Decoder::opcode_11100xxx }, // UOP_VPOP (32-bit)
126 { 0xfc, 0xe8, 2, &Decoder::opcode_111010xx }, // UOP_STACK_FREE (32-bit)
127 { 0xfe, 0xec, 2, &Decoder::opcode_1110110L }, // UOP_POP (16-bit)
128 { 0xff, 0xee, 2, &Decoder::opcode_11101110 }, // UOP_MICROSOFT_SPECIFIC (16-bit)
129129 // UOP_PUSH_MACHINE_FRAME
130130 // UOP_PUSH_CONTEXT
131131 // UOP_PUSH_TRAP_FRAME
132132 // UOP_REDZONE_RESTORE_LR
133 { 0xff, 0xef, &Decoder::opcode_11101111 }, // UOP_LDRPC_POSTINC (32-bit)
134 { 0xff, 0xf5, &Decoder::opcode_11110101 }, // UOP_VPOP (32-bit)
135 { 0xff, 0xf6, &Decoder::opcode_11110110 }, // UOP_VPOP (32-bit)
136 { 0xff, 0xf7, &Decoder::opcode_11110111 }, // UOP_STACK_RESTORE (16-bit)
137 { 0xff, 0xf8, &Decoder::opcode_11111000 }, // UOP_STACK_RESTORE (16-bit)
138 { 0xff, 0xf9, &Decoder::opcode_11111001 }, // UOP_STACK_RESTORE (32-bit)
139 { 0xff, 0xfa, &Decoder::opcode_11111010 }, // UOP_STACK_RESTORE (32-bit)
140 { 0xff, 0xfb, &Decoder::opcode_11111011 }, // UOP_NOP (16-bit)
141 { 0xff, 0xfc, &Decoder::opcode_11111100 }, // UOP_NOP (32-bit)
142 { 0xff, 0xfd, &Decoder::opcode_11111101 }, // UOP_NOP (16-bit) / END
143 { 0xff, 0xfe, &Decoder::opcode_11111110 }, // UOP_NOP (32-bit) / END
144 { 0xff, 0xff, &Decoder::opcode_11111111 }, // UOP_END
133 { 0xff, 0xef, 2, &Decoder::opcode_11101111 }, // UOP_LDRPC_POSTINC (32-bit)
134 { 0xff, 0xf5, 2, &Decoder::opcode_11110101 }, // UOP_VPOP (32-bit)
135 { 0xff, 0xf6, 2, &Decoder::opcode_11110110 }, // UOP_VPOP (32-bit)
136 { 0xff, 0xf7, 3, &Decoder::opcode_11110111 }, // UOP_STACK_RESTORE (16-bit)
137 { 0xff, 0xf8, 4, &Decoder::opcode_11111000 }, // UOP_STACK_RESTORE (16-bit)
138 { 0xff, 0xf9, 3, &Decoder::opcode_11111001 }, // UOP_STACK_RESTORE (32-bit)
139 { 0xff, 0xfa, 4, &Decoder::opcode_11111010 }, // UOP_STACK_RESTORE (32-bit)
140 { 0xff, 0xfb, 1, &Decoder::opcode_11111011 }, // UOP_NOP (16-bit)
141 { 0xff, 0xfc, 1, &Decoder::opcode_11111100 }, // UOP_NOP (32-bit)
142 { 0xff, 0xfd, 1, &Decoder::opcode_11111101 }, // UOP_NOP (16-bit) / END
143 { 0xff, 0xfe, 1, &Decoder::opcode_11111110 }, // UOP_NOP (32-bit) / END
144 { 0xff, 0xff, 1, &Decoder::opcode_11111111 }, // UOP_END
145 };
146
147
148 // Unwind opcodes for ARM64.
149 // https://docs.microsoft.com/en-us/cpp/build/arm64-exception-handling
150 const Decoder::RingEntry Decoder::Ring64[] = {
151 { 0xe0, 0x00, 1, &Decoder::opcode_alloc_s },
152 { 0xe0, 0x20, 1, &Decoder::opcode_save_r19r20_x },
153 { 0xc0, 0x40, 1, &Decoder::opcode_save_fplr },
154 { 0xc0, 0x80, 1, &Decoder::opcode_save_fplr_x },
155 { 0xf8, 0xc0, 2, &Decoder::opcode_alloc_m },
156 { 0xfc, 0xc8, 2, &Decoder::opcode_save_regp },
157 { 0xfc, 0xcc, 2, &Decoder::opcode_save_regp_x },
158 { 0xfc, 0xd0, 2, &Decoder::opcode_save_reg },
159 { 0xfe, 0xd4, 2, &Decoder::opcode_save_reg_x },
160 { 0xfe, 0xd6, 2, &Decoder::opcode_save_lrpair },
161 { 0xfe, 0xd8, 2, &Decoder::opcode_save_fregp },
162 { 0xfe, 0xda, 2, &Decoder::opcode_save_fregp_x },
163 { 0xfe, 0xdc, 2, &Decoder::opcode_save_freg },
164 { 0xff, 0xde, 2, &Decoder::opcode_save_freg_x },
165 { 0xff, 0xe0, 4, &Decoder::opcode_alloc_l },
166 { 0xff, 0xe1, 1, &Decoder::opcode_setfp },
167 { 0xff, 0xe2, 2, &Decoder::opcode_addfp },
168 { 0xff, 0xe3, 1, &Decoder::opcode_nop },
169 { 0xff, 0xe4, 1, &Decoder::opcode_end },
170 { 0xff, 0xe5, 1, &Decoder::opcode_end_c },
145171 };
146172
147173 void Decoder::printRegisters(const std::pair &RegisterMask) {
492518 return true;
493519 }
494520
521 // ARM64 unwind codes start here.
522 bool Decoder::opcode_alloc_s(const uint8_t *OC, unsigned &Offset,
523 unsigned Length, bool Prologue) {
524 uint32_t NumBytes = (OC[Offset] & 0x1F) << 4;
525 SW.startLine() << format("0x%02x ; %s sp, #%u\n", OC[Offset],
526 static_cast(Prologue ? "sub" : "add"),
527 NumBytes);
528 ++Offset;
529 return false;
530 }
531
532 bool Decoder::opcode_save_r19r20_x(const uint8_t *OC, unsigned &Offset,
533 unsigned Length, bool Prologue) {
534 uint32_t Off = (OC[Offset] & 0x1F) << 3;
535 if (Prologue)
536 SW.startLine() << format(
537 "0x%02x ; stp x19, x20, [sp, #-%u]!\n", OC[Offset], Off);
538 else
539 SW.startLine() << format(
540 "0x%02x ; ldp x19, x20, [sp], #%u\n", OC[Offset], Off);
541 ++Offset;
542 return false;
543 }
544
545 bool Decoder::opcode_save_fplr(const uint8_t *OC, unsigned &Offset,
546 unsigned Length, bool Prologue) {
547 uint32_t Off = (OC[Offset] & 0x3F) << 3;
548 SW.startLine() << format(
549 "0x%02x ; %s x29, x30, [sp, #%u]\n", OC[Offset],
550 static_cast(Prologue ? "stp" : "ldp"), Off);
551 ++Offset;
552 return false;
553 }
554
555 bool Decoder::opcode_save_fplr_x(const uint8_t *OC, unsigned &Offset,
556 unsigned Length, bool Prologue) {
557 uint32_t Off = ((OC[Offset] & 0x3F) + 1) << 3;
558 if (Prologue)
559 SW.startLine() << format(
560 "0x%02x ; stp x29, x30, [sp, #-%u]!\n", OC[Offset], Off);
561 else
562 SW.startLine() << format(
563 "0x%02x ; ldp x29, x30, [sp], #%u\n", OC[Offset], Off);
564 ++Offset;
565 return false;
566 }
567
568 bool Decoder::opcode_alloc_m(const uint8_t *OC, unsigned &Offset,
569 unsigned Length, bool Prologue) {
570 uint32_t NumBytes = ((OC[Offset] & 0x07) << 8);
571 NumBytes |= (OC[Offset + 1] & 0xFF);
572 NumBytes <<= 4;
573 SW.startLine() << format("0x%02x%02x ; %s sp, #%u\n",
574 OC[Offset], OC[Offset + 1],
575 static_cast(Prologue ? "sub" : "add"),
576 NumBytes);
577 Offset += 2;
578 return false;
579 }
580
581 bool Decoder::opcode_save_regp(const uint8_t *OC, unsigned &Offset,
582 unsigned Length, bool Prologue) {
583 uint32_t Reg = ((OC[Offset] & 0x03) << 8);
584 Reg |= (OC[Offset + 1] & 0xC0);
585 Reg >>= 6;
586 Reg += 19;
587 uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
588 SW.startLine() << format(
589 "0x%02x%02x ; %s x%u, x%u, [sp, #%u]\n",
590 OC[Offset], OC[Offset + 1],
591 static_cast(Prologue ? "stp" : "ldp"), Reg, Reg + 1, Off);
592 Offset += 2;
593 return false;
594 }
595
596 bool Decoder::opcode_save_regp_x(const uint8_t *OC, unsigned &Offset,
597 unsigned Length, bool Prologue) {
598 uint32_t Reg = ((OC[Offset] & 0x03) << 8);
599 Reg |= (OC[Offset + 1] & 0xC0);
600 Reg >>= 6;
601 Reg += 19;
602 uint32_t Off = ((OC[Offset + 1] & 0x3F) + 1) << 3;
603 if (Prologue)
604 SW.startLine() << format(
605 "0x%02x%02x ; stp x%u, x%u, [sp, #-%u]!\n",
606 OC[Offset], OC[Offset + 1], Reg,
607 Reg + 1, Off);
608 else
609 SW.startLine() << format(
610 "0x%02x%02x ; ldp x%u, x%u, [sp], #%u\n",
611 OC[Offset], OC[Offset + 1], Reg,
612 Reg + 1, Off);
613 Offset += 2;
614 return false;
615 }
616
617 bool Decoder::opcode_save_reg(const uint8_t *OC, unsigned &Offset,
618 unsigned Length, bool Prologue) {
619 uint32_t Reg = (OC[Offset] & 0x03) << 8;
620 Reg |= (OC[Offset + 1] & 0xC0);
621 Reg >>= 6;
622 Reg += 19;
623 uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
624 SW.startLine() << format("0x%02x%02x ; %s x%u, [sp, #%u]\n",
625 OC[Offset], OC[Offset + 1],
626 static_cast(Prologue ? "str" : "ldr"),
627 Reg, Off);
628 Offset += 2;
629 return false;
630 }
631
632 bool Decoder::opcode_save_reg_x(const uint8_t *OC, unsigned &Offset,
633 unsigned Length, bool Prologue) {
634 uint32_t Reg = (OC[Offset] & 0x01) << 8;
635 Reg |= (OC[Offset + 1] & 0xE0);
636 Reg >>= 5;
637 Reg += 19;
638 uint32_t Off = ((OC[Offset + 1] & 0x1F) + 1) << 3;
639 if (Prologue)
640 SW.startLine() << format("0x%02x%02x ; str x%u, [sp, #%u]!\n",
641 OC[Offset], OC[Offset + 1], Reg, Off);
642 else
643 SW.startLine() << format("0x%02x%02x ; ldr x%u, [sp], #%u\n",
644 OC[Offset], OC[Offset + 1], Reg, Off);
645 Offset += 2;
646 return false;
647 }
648
649 bool Decoder::opcode_save_lrpair(const uint8_t *OC, unsigned &Offset,
650 unsigned Length, bool Prologue) {
651 uint32_t Reg = (OC[Offset] & 0x01) << 8;
652 Reg |= (OC[Offset + 1] & 0xC0);
653 Reg >>= 6;
654 Reg *= 2;
655 Reg += 19;
656 uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
657 SW.startLine() << format("0x%02x%02x ; %s x%u, lr, [sp, #%u]\n",
658 OC[Offset], OC[Offset + 1],
659 static_cast(Prologue ? "stp" : "ldp"),
660 Reg, Off);
661 Offset += 2;
662 return false;
663 }
664
665 bool Decoder::opcode_save_fregp(const uint8_t *OC, unsigned &Offset,
666 unsigned Length, bool Prologue) {
667 uint32_t Reg = (OC[Offset] & 0x01) << 8;
668 Reg |= (OC[Offset + 1] & 0xC0);
669 Reg >>= 6;
670 Reg += 8;
671 uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
672 SW.startLine() << format("0x%02x%02x ; %s d%u, d%u, [sp, #%u]\n",
673 OC[Offset], OC[Offset + 1],
674 static_cast(Prologue ? "stp" : "ldp"),
675 Reg, Reg + 1, Off);
676 Offset += 2;
677 return false;
678 }
679
680 bool Decoder::opcode_save_fregp_x(const uint8_t *OC, unsigned &Offset,
681 unsigned Length, bool Prologue) {
682 uint32_t Reg = (OC[Offset] & 0x01) << 8;
683 Reg |= (OC[Offset + 1] & 0xC0);
684 Reg >>= 6;
685 Reg += 8;
686 uint32_t Off = ((OC[Offset + 1] & 0x3F) + 1) << 3;
687 if (Prologue)
688 SW.startLine() << format(
689 "0x%02x%02x ; stp d%u, d%u, [sp, #-%u]!\n", OC[Offset],
690 OC[Offset + 1], Reg, Reg + 1, Off);
691 else
692 SW.startLine() << format(
693 "0x%02x%02x ; ldp d%u, d%u, [sp], #%u\n", OC[Offset],
694 OC[Offset + 1], Reg, Reg + 1, Off);
695 Offset += 2;
696 return false;
697 }
698
699 bool Decoder::opcode_save_freg(const uint8_t *OC, unsigned &Offset,
700 unsigned Length, bool Prologue) {
701 uint32_t Reg = (OC[Offset] & 0x01) << 8;
702 Reg |= (OC[Offset + 1] & 0xC0);
703 Reg >>= 6;
704 Reg += 8;
705 uint32_t Off = (OC[Offset + 1] & 0x3F) << 3;
706 SW.startLine() << format("0x%02x%02x ; %s d%u, [sp, #%u]\n",
707 OC[Offset], OC[Offset + 1],
708 static_cast(Prologue ? "str" : "ldr"),
709 Reg, Off);
710 Offset += 2;
711 return false;
712 }
713
714 bool Decoder::opcode_save_freg_x(const uint8_t *OC, unsigned &Offset,
715 unsigned Length, bool Prologue) {
716 uint32_t Reg = ((OC[Offset + 1] & 0xE0) >> 5) + 8;
717 uint32_t Off = ((OC[Offset + 1] & 0x1F) + 1) << 3;
718 if (Prologue)
719 SW.startLine() << format(
720 "0x%02x%02x ; str d%u, [sp, #-%u]!\n", OC[Offset],
721 OC[Offset + 1], Reg, Off);
722 else
723 SW.startLine() << format(
724 "0x%02x%02x ; ldr d%u, [sp], #%u\n", OC[Offset],
725 OC[Offset + 1], Reg, Off);
726 Offset += 2;
727 return false;
728 }
729
730 bool Decoder::opcode_alloc_l(const uint8_t *OC, unsigned &Offset,
731 unsigned Length, bool Prologue) {
732 unsigned Off =
733 (OC[Offset + 1] << 16) | (OC[Offset + 2] << 8) | (OC[Offset + 3] << 0);
734 Off <<= 4;
735 SW.startLine() << format(
736 "0x%02x%02x%02x%02x ; %s sp, #%u\n", OC[Offset], OC[Offset + 1],
737 OC[Offset + 2], OC[Offset + 3],
738 static_cast(Prologue ? "sub" : "add"), Off);
739 Offset += 4;
740 return false;
741 }
742
743 bool Decoder::opcode_setfp(const uint8_t *OC, unsigned &Offset, unsigned Length,
744 bool Prologue) {
745 SW.startLine() << format("0x%02x ; mov fp, sp\n", OC[Offset]);
746 ++Offset;
747 return false;
748 }
749
750 bool Decoder::opcode_addfp(const uint8_t *OC, unsigned &Offset, unsigned Length,
751 bool Prologue) {
752 unsigned NumBytes = OC[Offset + 1] << 3;
753 SW.startLine() << format("0x%02x%02x ; add fp, sp, #%u\n",
754 OC[Offset], OC[Offset + 1], NumBytes);
755 Offset += 2;
756 return false;
757 }
758
759 bool Decoder::opcode_nop(const uint8_t *OC, unsigned &Offset, unsigned Length,
760 bool Prologue) {
761 SW.startLine() << format("0x%02x ; nop\n", OC[Offset]);
762 ++Offset;
763 return false;
764 }
765
766 bool Decoder::opcode_end(const uint8_t *OC, unsigned &Offset, unsigned Length,
767 bool Prologue) {
768 SW.startLine() << format("0x%02x ; end\n", OC[Offset]);
769 ++Offset;
770 return true;
771 }
772
773 bool Decoder::opcode_end_c(const uint8_t *OC, unsigned &Offset, unsigned Length,
774 bool Prologue) {
775 SW.startLine() << format("0x%02x ; end_c\n", OC[Offset]);
776 ++Offset;
777 return true;
778 }
779
495780 void Decoder::decodeOpcodes(ArrayRef Opcodes, unsigned Offset,
496781 bool Prologue) {
497782 assert((!Prologue || Offset == 0) && "prologue should always use offset 0");
498
783 const RingEntry* DecodeRing = isAArch64 ? Ring64 : Ring;
499784 bool Terminated = false;
500785 for (unsigned OI = Offset, OE = Opcodes.size(); !Terminated && OI < OE; ) {
501786 for (unsigned DI = 0;; ++DI) {
502 if ((Opcodes[OI] & Ring[DI].Mask) == Ring[DI].Value) {
503 Terminated = (this->*Ring[DI].Routine)(Opcodes.data(), OI, 0, Prologue);
787 if ((isAArch64 && (DI >= array_lengthof(Ring64))) ||
788 (!isAArch64 && (DI >= array_lengthof(Ring)))) {
789 SW.startLine() << format("0x%02x ; Bad opcode!\n",
790 Opcodes.data()[Offset]);
791 ++OI;
504792 break;
505793 }
506 assert(DI < array_lengthof(Ring) && "unhandled opcode");
794
795 if ((Opcodes[OI] & DecodeRing[DI].Mask) == DecodeRing[DI].Value) {
796 if (OI + DecodeRing[DI].Length > OE) {
797 SW.startLine() << format("Opcode 0x%02x goes past the unwind data\n",
798 Opcodes[OI]);
799 OI += DecodeRing[DI].Length;
800 break;
801 }
802 Terminated =
803 (this->*DecodeRing[DI].Routine)(Opcodes.data(), OI, 0, Prologue);
804 break;
805 }
507806 }
508807 }
509808 }
519818 uint64_t Offset = VA - SectionVA;
520819 const ulittle32_t *Data =
521820 reinterpret_cast(Contents.data() + Offset);
522 const ExceptionDataRecord XData(Data);
523
821
822 // Sanity check to ensure that the .xdata header is present.
823 // A header is one or two words, followed by at least one word to describe
824 // the unwind codes. Applicable to both ARM and AArch64.
825 if (Contents.size() - Offset < 8)
826 report_fatal_error(".xdata must be at least 8 bytes in size");
827
828 const ExceptionDataRecord XData(Data, isAArch64);
524829 DictScope XRS(SW, "ExceptionData");
525 SW.printNumber("FunctionLength", XData.FunctionLength() << 1);
830 SW.printNumber("FunctionLength",
831 isAArch64 ? XData.FunctionLengthInBytesAArch64() :
832 XData.FunctionLengthInBytesARM());
526833 SW.printNumber("Version", XData.Vers());
527834 SW.printBoolean("ExceptionData", XData.X());
528835 SW.printBoolean("EpiloguePacked", XData.E());
529 SW.printBoolean("Fragment", XData.F());
836 if (!isAArch64)
837 SW.printBoolean("Fragment", XData.F());
530838 SW.printNumber(XData.E() ? "EpilogueOffset" : "EpilogueScopes",
531839 XData.EpilogueCount());
532 SW.printNumber("ByteCodeLength",
533 static_cast(XData.CodeWords() * sizeof(uint32_t)));
840 uint64_t ByteCodeLength = XData.CodeWords() * sizeof(uint32_t);
841 SW.printNumber("ByteCodeLength", ByteCodeLength);
842
843 if ((int64_t)(Contents.size() - Offset - 4 * HeaderWords(XData) -
844 (XData.E() ? 0 : XData.EpilogueCount() * 4) -
845 (XData.X() ? 8 : 0)) < (int64_t)ByteCodeLength)
846 report_fatal_error("Malformed unwind data");
534847
535848 if (XData.E()) {
536849 ArrayRef UC = XData.UnwindByteCode();
537 if (!XData.F()) {
850 if (isAArch64 || !XData.F()) {
538851 ListScope PS(SW, "Prologue");
539852 decodeOpcodes(UC, 0, /*Prologue=*/true);
540853 }
543856 decodeOpcodes(UC, XData.EpilogueCount(), /*Prologue=*/false);
544857 }
545858 } else {
859 {
860 ListScope PS(SW, "Prologue");
861 decodeOpcodes(XData.UnwindByteCode(), 0, /*Prologue=*/true);
862 }
546863 ArrayRef EpilogueScopes = XData.EpilogueScopes();
547864 ListScope ESS(SW, "EpilogueScopes");
548865 for (const EpilogueScope ES : EpilogueScopes) {
549866 DictScope ESES(SW, "EpilogueScope");
550867 SW.printNumber("StartOffset", ES.EpilogueStartOffset());
551 SW.printNumber("Condition", ES.Condition());
552 SW.printNumber("EpilogueStartIndex", ES.EpilogueStartIndex());
868 if (!isAArch64)
869 SW.printNumber("Condition", ES.Condition());
870 SW.printNumber("EpilogueStartIndex",
871 isAArch64 ? ES.EpilogueStartIndexAArch64()
872 : ES.EpilogueStartIndexARM());
553873
554874 ListScope Opcodes(SW, "Opcodes");
555 decodeOpcodes(XData.UnwindByteCode(), ES.EpilogueStartIndex(),
875 decodeOpcodes(XData.UnwindByteCode(),
876 isAArch64 ? ES.EpilogueStartIndexAArch64()
877 : ES.EpilogueStartIndexARM(),
556878 /*Prologue=*/false);
557879 }
558880 }
7241046 }
7251047
7261048 SW.printString("Function", formatSymbol(FunctionName, FunctionAddress));
727 SW.printBoolean("Fragment",
728 RF.Flag() == RuntimeFunctionFlag::RFF_PackedFragment);
1049 if (!isAArch64)
1050 SW.printBoolean("Fragment",
1051 RF.Flag() == RuntimeFunctionFlag::RFF_PackedFragment);
7291052 SW.printNumber("FunctionLength", RF.FunctionLength());
7301053 SW.startLine() << "ReturnType: " << RF.Ret() << '\n';
7311054 SW.printBoolean("HomedParameters", RF.H());
7481071 DictScope RFS(SW, "RuntimeFunction");
7491072 if (Entry.Flag() == RuntimeFunctionFlag::RFF_Unpacked)
7501073 return dumpUnpackedEntry(COFF, Section, Offset, Index, Entry);
1074 if (isAArch64) {
1075 llvm::errs() << "Packed unwind data not yet supported for ARM64\n";
1076 return false;
1077 }
7511078 return dumpPackedEntry(COFF, Section, Offset, Index, Entry);
7521079 }
7531080
2323
2424 ScopedPrinter &SW;
2525 raw_ostream &OS;
26 bool isAArch64;
2627
2728 struct RingEntry {
2829 uint8_t Mask;
2930 uint8_t Value;
31 uint8_t Length;
3032 bool (Decoder::*Routine)(const uint8_t *, unsigned &, unsigned, bool);
3133 };
3234 static const RingEntry Ring[];
35 static const RingEntry Ring64[];
3336
3437 bool opcode_0xxxxxxx(const uint8_t *Opcodes, unsigned &Offset,
3538 unsigned Length, bool Prologue);
7477 bool opcode_11111111(const uint8_t *Opcodes, unsigned &Offset,
7578 unsigned Length, bool Prologue);
7679
80 // ARM64 unwind codes start here.
81 bool opcode_alloc_s(const uint8_t *Opcodes, unsigned &Offset, unsigned Length,
82 bool Prologue);
83 bool opcode_save_r19r20_x(const uint8_t *Opcodes, unsigned &Offset,
84 unsigned Length, bool Prologue);
85 bool opcode_save_fplr(const uint8_t *Opcodes, unsigned &Offset,
86 unsigned Length, bool Prologue);
87 bool opcode_save_fplr_x(const uint8_t *Opcodes, unsigned &Offset,
88 unsigned Length, bool Prologue);
89 bool opcode_alloc_m(const uint8_t *Opcodes, unsigned &Offset, unsigned Length,
90 bool Prologue);
91 bool opcode_save_regp(const uint8_t *Opcodes, unsigned &Offset,
92 unsigned Length, bool Prologue);
93 bool opcode_save_regp_x(const uint8_t *Opcodes, unsigned &Offset,
94 unsigned Length, bool Prologue);
95 bool opcode_save_reg(const uint8_t *Opcodes, unsigned &Offset,
96 unsigned Length, bool Prologue);
97 bool opcode_save_reg_x(const uint8_t *Opcodes, unsigned &Offset,
98 unsigned Length, bool Prologue);
99 bool opcode_save_lrpair(const uint8_t *Opcodes, unsigned &Offset,
100 unsigned Length, bool Prologue);
101 bool opcode_save_fregp(const uint8_t *Opcodes, unsigned &Offset,
102 unsigned Length, bool Prologue);
103 bool opcode_save_fregp_x(const uint8_t *Opcodes, unsigned &Offset,
104 unsigned Length, bool Prologue);
105 bool opcode_save_freg(const uint8_t *Opcodes, unsigned &Offset,
106 unsigned Length, bool Prologue);
107 bool opcode_save_freg_x(const uint8_t *Opcodes, unsigned &Offset,
108 unsigned Length, bool Prologue);
109 bool opcode_alloc_l(const uint8_t *Opcodes, unsigned &Offset, unsigned Length,
110 bool Prologue);
111 bool opcode_setfp(const uint8_t *Opcodes, unsigned &Offset, unsigned Length,
112 bool Prologue);
113 bool opcode_addfp(const uint8_t *Opcodes, unsigned &Offset, unsigned Length,
114 bool Prologue);
115 bool opcode_nop(const uint8_t *Opcodes, unsigned &Offset, unsigned Length,
116 bool Prologue);
117 bool opcode_end(const uint8_t *Opcodes, unsigned &Offset, unsigned Length,
118 bool Prologue);
119 bool opcode_end_c(const uint8_t *Opcodes, unsigned &Offset, unsigned Length,
120 bool Prologue);
121 bool opcode_save_next(const uint8_t *Opcodes, unsigned &Offset,
122 unsigned Length, bool Prologue);
123
77124 void decodeOpcodes(ArrayRef Opcodes, unsigned Offset,
78125 bool Prologue);
79126
106153 const object::SectionRef Section);
107154
108155 public:
109 Decoder(ScopedPrinter &SW) : SW(SW), OS(SW.getOStream()) {}
156 Decoder(ScopedPrinter &SW, bool isAArch64) : SW(SW),
157 OS(SW.getOStream()),
158 isAArch64(isAArch64) {}
110159 std::error_code dumpProcedureData(const object::COFFObjectFile &COFF);
111160 };
112161 }
15481548 Dumper.printData(Ctx);
15491549 break;
15501550 }
1551 case COFF::IMAGE_FILE_MACHINE_ARM64:
15511552 case COFF::IMAGE_FILE_MACHINE_ARMNT: {
1552 ARM::WinEH::Decoder Decoder(W);
1553 ARM::WinEH::Decoder Decoder(W, Obj->getMachine() ==
1554 COFF::IMAGE_FILE_MACHINE_ARM64);
15531555 Decoder.dumpProcedureData(*Obj);
15541556 break;
15551557 }