Skip to content

Commit 06c22ba

Browse files
committed
Add llvm libunwind callback to suppress exceptions on apple silicon
1 parent 244d1ab commit 06c22ba

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

Diff for: lib/Interpreter/Compatibility.h

+52
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@
1313
#include "llvm/ADT/SmallString.h"
1414
#include "llvm/ADT/StringRef.h"
1515
#include "llvm/ADT/Twine.h"
16+
#include "llvm/BinaryFormat/MachO.h"
1617
#include "llvm/Config/llvm-config.h"
1718
#include "llvm/ExecutionEngine/JITSymbol.h"
1819
#include "llvm/ExecutionEngine/Orc/LLJIT.h"
20+
#include "llvm/Object/MachO.h"
1921
#include "llvm/Support/Casting.h"
2022
#include "llvm/Support/Path.h"
2123

24+
/home/maximus/cppyy-interop-dev/CppInterOp/llvm-project/compiler-rt/lib/orc/macho_platform.cpp
25+
2226
#ifdef USE_CLING
2327

2428
#include "cling/Interpreter/DynamicLibraryManager.h"
@@ -36,6 +40,45 @@ namespace compat {
3640

3741
using Interpreter = cling::Interpreter;
3842

43+
#ifdef __APPLE__
44+
#include <mach-o/dyld.h>
45+
#include <sys/stat.h>
46+
// Define a minimal mach header for JIT'd code.
47+
static MachO::mach_header_64 fake_mach_header = {
48+
.magic = MachO::MH_MAGIC_64,
49+
.cputype = MachO::CPU_TYPE_ARM64,
50+
.cpusubtype = MachO::CPU_SUBTYPE_ARM64_ALL,
51+
.filetype = MachO::MH_DYLIB,
52+
.ncmds = 0,
53+
.sizeofcmds = 0,
54+
.flags = 0,
55+
.reserved = 0};
56+
57+
// Declare libunwind SPI types and functions.
58+
struct unw_dynamic_unwind_sections {
59+
uintptr_t dso_base;
60+
uintptr_t dwarf_section;
61+
size_t dwarf_section_length;
62+
uintptr_t compact_unwind_section;
63+
size_t compact_unwind_section_length;
64+
};
65+
66+
int find_dynamic_unwind_sections(uintptr_t addr,
67+
unw_dynamic_unwind_sections* info) {
68+
info->dso_base = (uintptr_t)&fake_mach_header;
69+
info->dwarf_section = 0;
70+
info->dwarf_section_length = 0;
71+
info->compact_unwind_section = 0;
72+
info->compact_unwind_section_length = 0;
73+
return 1;
74+
}
75+
76+
// Typedef for callback above.
77+
typedef int (*unw_find_dynamic_unwind_sections)(
78+
uintptr_t addr, struct unw_dynamic_unwind_sections* info);
79+
80+
#endif
81+
3982
inline void maybeMangleDeclName(const clang::GlobalDecl& GD,
4083
std::string& mangledName) {
4184
cling::utils::Analyze::maybeMangleDeclName(GD, mangledName);
@@ -151,6 +194,15 @@ createClangInterpreter(std::vector<const char*>& args) {
151194
return std::move(*innerOrErr);
152195
}
153196

197+
#ifdef __APPLE__
198+
inline void removeFindDynamicUnwindSections() {
199+
if (auto* unw_remove_find_dynamic_unwind_sections = (int (*)(
200+
unw_find_dynamic_unwind_sections find_dynamic_unwind_sections))
201+
dlsym(RTLD_DEFAULT, "__unw_remove_find_dynamic_unwind_sections"))
202+
unw_remove_find_dynamic_unwind_sections(find_dynamic_unwind_sections);
203+
}
204+
#endif
205+
154206
inline void maybeMangleDeclName(const clang::GlobalDecl& GD,
155207
std::string& mangledName) {
156208
// copied and adapted from CodeGen::CodeGenModule::getMangledName

Diff for: lib/Interpreter/CppInterOp.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,12 @@ namespace Cpp {
6262
// This might fix the issue https://reviews.llvm.org/D107087
6363
// FIXME: For now we just leak the Interpreter.
6464
struct InterpDeleter {
65-
~InterpDeleter() { sInterpreter.release(); }
65+
~InterpDeleter() {
66+
#ifdef __APPLE__
67+
compat::removeFindDynamicUnwindSections();
68+
#endif
69+
sInterpreter.release();
70+
}
6671
} Deleter;
6772

6873
static compat::Interpreter& getInterp() {

0 commit comments

Comments
 (0)