Skip to content

Commit bf3b97e

Browse files
committed
win64: add Julia personality
1 parent 558f44a commit bf3b97e

File tree

5 files changed

+27
-13
lines changed

5 files changed

+27
-13
lines changed

src/codegen.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
#include "llvm-version.h"
44
#include "platform.h"
55
#include "options.h"
6-
#if defined(_OS_WINDOWS_) && !defined(LLVM38)
7-
// trick pre-llvm38 into skipping the generation of _chkstk calls
6+
#if defined(_OS_WINDOWS_) && !defined(LLVM39)
7+
// trick pre-llvm39 into skipping the generation of _chkstk calls
88
// since it has some codegen issues associated with them:
99
// (a) assumed to be within 32-bit offset
1010
// (b) bad asm is generated for certain code patterns:
1111
// see https://github.com/JuliaLang/julia/pull/11644#issuecomment-112276813
1212
// also, use ELF because RuntimeDyld COFF I686 support didn't exist
13+
// also, use ELF because RuntimeDyld COFF X86_64 doesn't seem to work (fails to generate function pointers)?
1314
#define FORCE_ELF
1415
#endif
1516
#if defined(_CPU_X86_)
@@ -413,6 +414,9 @@ static Function *jlgetnthfieldchecked_func;
413414
//static Function *jlsetnthfield_func;
414415
#ifdef _OS_WINDOWS_
415416
static Function *resetstkoflw_func;
417+
#if defined(_CPU_X86_64_)
418+
static Function *juliapersonality_func;
419+
#endif
416420
#endif
417421
static Function *diff_gc_total_bytes_func;
418422
static Function *jlarray_data_owner_func;
@@ -5410,6 +5414,11 @@ static void init_julia_llvm_env(Module *m)
54105414
resetstkoflw_func = Function::Create(FunctionType::get(T_int32, false),
54115415
Function::ExternalLinkage, "_resetstkoflw", m);
54125416
add_named_global(resetstkoflw_func, &_resetstkoflw);
5417+
#if defined(_CPU_X86_64_)
5418+
juliapersonality_func = Function::Create(FunctionType::get(T_int32, true),
5419+
Function::ExternalLinkage, "__julia_personality", m);
5420+
add_named_global(juliapersonality_func, &__julia_personality);
5421+
#endif
54135422
#ifndef FORCE_ELF
54145423
#if defined(_CPU_X86_64_)
54155424
#if defined(_COMPILER_MINGW_)

src/debuginfo.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,6 @@ void jl_add_linfo_in_flight(StringRef name, jl_lambda_info_t *linfo, const DataL
128128
}
129129

130130
#if defined(_OS_WINDOWS_)
131-
#if defined(_CPU_X86_64_)
132-
extern "C" EXCEPTION_DISPOSITION _seh_exception_handler(PEXCEPTION_RECORD ExceptionRecord,void *EstablisherFrame, PCONTEXT ContextRecord, void *DispatcherContext);
133-
#endif
134131
static void create_PRUNTIME_FUNCTION(uint8_t *Code, size_t Size, StringRef fnname,
135132
uint8_t *Section, size_t Allocated, uint8_t *UnwindData)
136133
{
@@ -143,7 +140,7 @@ static void create_PRUNTIME_FUNCTION(uint8_t *Code, size_t Size, StringRef fnnam
143140
if (!catchjmp[0]) {
144141
catchjmp[0] = 0x48;
145142
catchjmp[1] = 0xb8; // mov RAX, QWORD PTR [...]
146-
*(uint64_t*)(&catchjmp[2]) = (uint64_t)&_seh_exception_handler;
143+
*(uint64_t*)(&catchjmp[2]) = (uint64_t)&__julia_personality;
147144
catchjmp[10] = 0xff;
148145
catchjmp[11] = 0xe0; // jmp RAX
149146
UnwindData[0] = 0x09; // version info, UNW_FLAG_EHANDLER
@@ -421,9 +418,9 @@ class JuliaJITEventListener: public JITEventListener
421418
assert(SectionAddrCheck);
422419
assert(SectionLoadOffset != 1);
423420
catchjmp[SectionLoadOffset] = 0x48;
424-
catchjmp[SectionLoadOffset + 1] = 0xb8; // mov RAX, QWORD PTR [&_seh_exception_handle]
421+
catchjmp[SectionLoadOffset + 1] = 0xb8; // mov RAX, QWORD PTR [&__julia_personality]
425422
*(uint64_t*)(&catchjmp[SectionLoadOffset + 2]) =
426-
(uint64_t)&_seh_exception_handler;
423+
(uint64_t)&__julia_personality;
427424
catchjmp[SectionLoadOffset + 10] = 0xff;
428425
catchjmp[SectionLoadOffset + 11] = 0xe0; // jmp RAX
429426
UnwindData[SectionLoadOffset] = 0x09; // version info, UNW_FLAG_EHANDLER

src/jitlayers.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,28 +532,35 @@ ExecutionEngine *jl_ExecutionEngine;
532532
#endif
533533

534534
// MSVC's link.exe requires each function declaration to have a Comdat section
535-
// rather than litter the code with conditionals,
535+
// So rather than litter the code with conditionals,
536536
// all global values that get emitted call this function
537537
// and it decides whether the definition needs a Comdat section and adds the appropriate declaration
538538
// TODO: consider moving this into jl_add_to_shadow or jl_dump_shadow? the JIT doesn't care, so most calls are now no-ops
539539
template<class T> // for GlobalObject's
540540
static T *addComdat(T *G)
541541
{
542-
#if defined(_OS_WINDOWS_) && defined(_COMPILER_MICROSOFT_)
542+
#if defined(_OS_WINDOWS_) && defined(LLVM35)
543543
if (imaging_mode && !G->isDeclaration()) {
544-
#ifdef LLVM35
545544
// Add comdat information to make MSVC link.exe happy
545+
// it's valid to emit this for ld.exe too,
546+
// but makes it very slow to link for no benefit
547+
#if defined(_COMPILER_MICROSOFT_)
546548
if (G->getParent() == shadow_output) {
547549
Comdat *jl_Comdat = G->getParent()->getOrInsertComdat(G->getName());
548550
// ELF only supports Comdat::Any
549551
jl_Comdat->setSelectionKind(Comdat::NoDuplicates);
550552
G->setComdat(jl_Comdat);
551553
}
554+
#endif
552555
// add __declspec(dllexport) to everything marked for export
553556
if (G->getLinkage() == GlobalValue::ExternalLinkage)
554557
G->setDLLStorageClass(GlobalValue::DLLExportStorageClass);
555558
else
556559
G->setDLLStorageClass(GlobalValue::DefaultStorageClass);
560+
#if defined(_CPU_X86_64_)
561+
// Add unwind exception personalities to functions to handle async exceptions
562+
if (Function *F = dyn_cast<Function>(G))
563+
F->setPersonalityFn(juliapersonality_func);
557564
#endif
558565
}
559566
#endif

src/julia_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,8 @@ typedef struct {
343343
uint64_t jl_getUnwindInfo(uint64_t dwBase);
344344
#ifdef _OS_WINDOWS_
345345
#include <dbghelp.h>
346+
JL_DLLEXPORT EXCEPTION_DISPOSITION __julia_personality(
347+
PEXCEPTION_RECORD ExceptionRecord, void *EstablisherFrame, PCONTEXT ContextRecord, void *DispatcherContext);
346348
extern HANDLE hMainThread;
347349
typedef CONTEXT bt_context_t;
348350
#if defined(_CPU_X86_64_)

src/signals-win.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,7 @@ static LONG WINAPI exception_handler(struct _EXCEPTION_POINTERS *ExceptionInfo)
278278
}
279279

280280
#if defined(_CPU_X86_64_)
281-
EXCEPTION_DISPOSITION _seh_exception_handler(PEXCEPTION_RECORD ExceptionRecord, void *EstablisherFrame, PCONTEXT ContextRecord, void *DispatcherContext)
282-
{
281+
JL_DLLEXPORT EXCEPTION_DISPOSITION __julia_personality(PEXCEPTION_RECORD ExceptionRecord, void *EstablisherFrame, PCONTEXT ContextRecord, void *DispatcherContext) {
283282
EXCEPTION_POINTERS ExceptionInfo;
284283
ExceptionInfo.ExceptionRecord = ExceptionRecord;
285284
ExceptionInfo.ContextRecord = ContextRecord;

0 commit comments

Comments
 (0)