Skip to content

Commit 10be254

Browse files
authored
[clang] Fix catching pointers by reference on mingw targets (#162546)
For this specific case, when catching a pointer data type, by reference, Clang generates a special code pattern, which directly accesses the exception data by skipping past the `_Unwind_Exception` manually (rather than using the return value of `__cxa_begin_catch`). On most platforms, `_Unwind_Exception` is 32 bytes, but in some configurations it's different. (ARM EHABI is one preexisting case.) In the case of SEH, it's also different - it is 48 bytes in 32 bit mode and 64 bytes in 64 bit mode. (See the SEH ifdef in `_Unwind_Exception` in `clang/lib/Headers/unwind.h`.) Handle this case in `TargetCodeGenInfo::getSizeOfUnwindException`, fixing the code generation for catching pointers by reference. This fixes mstorsjo/llvm-mingw#522.
1 parent d50423e commit 10be254

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

clang/lib/CodeGen/TargetInfo.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ TargetCodeGenInfo::~TargetCodeGenInfo() = default;
8282
// If someone can figure out a general rule for this, that would be great.
8383
// It's probably just doomed to be platform-dependent, though.
8484
unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
85+
if (getABIInfo().getCodeGenOpts().hasSEHExceptions())
86+
return getABIInfo().getDataLayout().getPointerSizeInBits() > 32 ? 64 : 48;
8587
// Verified for:
8688
// x86-64 FreeBSD, Linux, Darwin
8789
// x86-32 FreeBSD, Linux, Darwin

clang/test/CodeGenCXX/sizeof-unwind-exception.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -emit-llvm -fcxx-exceptions -fexceptions %s -O2 -o - | FileCheck %s --check-prefix=ARM-DARWIN
44
// RUN: %clang_cc1 -triple arm-unknown-gnueabi -emit-llvm -fcxx-exceptions -fexceptions %s -O2 -o - | FileCheck %s --check-prefix=ARM-EABI
55
// RUN: %clang_cc1 -triple mipsel-unknown-unknown -emit-llvm -fcxx-exceptions -fexceptions %s -O2 -o - | FileCheck %s --check-prefix=MIPS
6+
// RUN: %clang_cc1 -triple x86_64-windows-gnu -emit-llvm -fcxx-exceptions -fexceptions -exception-model=seh %s -O2 -o - | FileCheck %s --check-prefix=MINGW-X86-64
7+
// RUN: %clang_cc1 -triple thumbv7-windows-gnu -emit-llvm -fcxx-exceptions -fexceptions -exception-model=seh %s -O2 -o - | FileCheck %s --check-prefix=MINGW-ARMV7
68

79
void foo();
810
void test() {
@@ -25,9 +27,15 @@ void test() {
2527
// ARM-EABI-NEXT: [[T1:%.*]] = getelementptr i8, ptr [[EXN]], i32 88
2628
// MIPS: [[T0:%.*]] = tail call ptr @__cxa_begin_catch(ptr [[EXN:%.*]]) [[NUW:#[0-9]+]]
2729
// MIPS-NEXT: [[T1:%.*]] = getelementptr i8, ptr [[EXN]], i32 24
30+
// MINGW-X86-64: [[T0:%.*]] = tail call ptr @__cxa_begin_catch(ptr [[EXN:%.*]]) [[NUW:#[0-9]+]]
31+
// MINGW-X86-64-NEXT:[[T1:%.*]] = getelementptr i8, ptr [[EXN]], i64 64
32+
// MINGW-ARMV7: [[T0:%.*]] = tail call arm_aapcs_vfpcc ptr @__cxa_begin_catch(ptr [[EXN:%.*]]) [[NUW:#[0-9]+]]
33+
// MINGW-ARMV7-NEXT: [[T1:%.*]] = getelementptr i8, ptr [[EXN]], i32 48
2834

2935
// X86-64: attributes [[NUW]] = { nounwind }
3036
// X86-32: attributes [[NUW]] = { nounwind }
3137
// ARM-DARWIN: attributes [[NUW]] = { nounwind }
3238
// ARM-EABI: attributes [[NUW]] = { nounwind }
3339
// MIPS: attributes [[NUW]] = { nounwind }
40+
// MINGW-X86-64: attributes [[NUW]] = { nounwind }
41+
// MINGW-ARMV7: attributes [[NUW]] = { nounwind }

0 commit comments

Comments
 (0)