73
73
74
74
#include < stack>
75
75
76
+ #ifdef __APPLE__
77
+ // Define a minimal mach header for JIT'd code, to support exceptions on osx 14
78
+ // and later. See llvm/llvm-project#49036
79
+ static llvm::MachO::mach_header_64 fake_mach_header = {
80
+ .magic = llvm::MachO::MH_MAGIC_64,
81
+ .cputype = llvm::MachO::CPU_TYPE_ARM64,
82
+ .cpusubtype = llvm::MachO::CPU_SUBTYPE_ARM64_ALL,
83
+ .filetype = llvm::MachO::MH_DYLIB,
84
+ .ncmds = 0 ,
85
+ .sizeofcmds = 0 ,
86
+ .flags = 0 ,
87
+ .reserved = 0 };
88
+
89
+ // Declare libunwind SPI types and functions.
90
+ struct unw_dynamic_unwind_sections {
91
+ uintptr_t dso_base;
92
+ uintptr_t dwarf_section;
93
+ size_t dwarf_section_length;
94
+ uintptr_t compact_unwind_section;
95
+ size_t compact_unwind_section_length;
96
+ };
97
+
98
+ int find_dynamic_unwind_sections (uintptr_t addr,
99
+ unw_dynamic_unwind_sections* info) {
100
+ info->dso_base = (uintptr_t )&fake_mach_header;
101
+ info->dwarf_section = 0 ;
102
+ info->dwarf_section_length = 0 ;
103
+ info->compact_unwind_section = 0 ;
104
+ info->compact_unwind_section_length = 0 ;
105
+ return 1 ;
106
+ }
107
+
108
+ // Typedef for callback above.
109
+ typedef int (*unw_find_dynamic_unwind_sections)(
110
+ uintptr_t addr, struct unw_dynamic_unwind_sections * info);
111
+
112
+ #endif // __APPLE__
113
+
76
114
namespace Cpp {
77
115
78
116
using namespace clang ;
@@ -88,7 +126,15 @@ static compat::Interpreter* sInterpreter = nullptr;
88
126
// This might fix the issue https://reviews.llvm.org/D107087
89
127
// FIXME: For now we just leak the Interpreter.
90
128
struct InterpDeleter {
91
- ~InterpDeleter () = default ;
129
+ ~InterpDeleter () {
130
+ #ifdef __APPLE__
131
+ if (auto * unw_remove_find_dynamic_unwind_sections = (int (*)(
132
+ unw_find_dynamic_unwind_sections find_dynamic_unwind_sections))
133
+ dlsym (RTLD_DEFAULT, " __unw_remove_find_dynamic_unwind_sections" ))
134
+ unw_remove_find_dynamic_unwind_sections (find_dynamic_unwind_sections);
135
+ #endif
136
+ // sInterpreter.release();
137
+ }
92
138
} Deleter;
93
139
94
140
static compat::Interpreter& getInterp () {
@@ -2865,6 +2911,8 @@ TInterp_t CreateInterpreter(const std::vector<const char*>& Args /*={}*/,
2865
2911
#ifdef _WIN32
2866
2912
// FIXME : Workaround Sema::PushDeclContext assert on windows
2867
2913
ClingArgv.push_back (" -fno-delayed-template-parsing" );
2914
+ #elif __APPLE__
2915
+ ClingArgv.push_back (" -fforce-dwarf-frame" );
2868
2916
#endif
2869
2917
ClingArgv.insert (ClingArgv.end (), Args.begin (), Args.end ());
2870
2918
// To keep the Interpreter creation interface between cling and clang-repl
@@ -2923,6 +2971,14 @@ TInterp_t CreateInterpreter(const std::vector<const char*>& Args /*={}*/,
2923
2971
// FIXME: Enable this assert once we figure out how to fix the multiple
2924
2972
// calls to CreateInterpreter.
2925
2973
// assert(!sInterpreter && "Interpreter already set.");
2974
+ #ifdef __APPLE__
2975
+ // Add a handler to support exceptions from interpreted code.
2976
+ // See llvm/llvm-project#49036
2977
+ if (auto * unw_add_find_dynamic_unwind_sections = (int (*)(
2978
+ unw_find_dynamic_unwind_sections find_dynamic_unwind_sections))
2979
+ dlsym (RTLD_DEFAULT, " __unw_add_find_dynamic_unwind_sections" ))
2980
+ unw_add_find_dynamic_unwind_sections (find_dynamic_unwind_sections);
2981
+ #endif // __APPLE__
2926
2982
sInterpreter = I;
2927
2983
return I;
2928
2984
}
0 commit comments