Stream: compiler development

Topic: Windows Surgical Linker Bug


view this post on Zulip Luke Boswell (Jan 08 2024 at 02:34):

Some debugging Windows; I'm trying to track down why roc is segfaulting on the most basic examples.

If I build and/or run using roc cli then it segfaults at the point where the host calls into Roc.

However if I emit the LLVM IR for the application and then use zig to build an executable against the platform it runs as expected.

So I'm wondering if there may be something specific in the Roc cli build toolchain that might be causing this? I suspect there is a build configuration or something in the roc cli causing this??

The below shows this workflow, note that I had to vendor the zig builtins into the platform so that it can build the host without roc, and I also included a couple of print debugs in the platform to see where things fail

PS C:\Users\bosyl\Documents\GitHub\ROC> .\target\debug\roc.exe build .\examples\platform-switching\rocLovesZig.roc
🔨 Rebuilding platform...
0 errors and 0 warnings found in 3786 ms while successfully building:

    .\examples\platform-switching\rocLovesZig.exe
PS C:\Users\bosyl\Documents\GitHub\ROC> .\examples\platform-switching\rocLovesZig.exe
STARTING
<-- SEGFAULTS HERE when zig calls roc__mainForHost_1_exposed_generic(&callresult);-->
PS C:\Users\bosyl\Documents\GitHub\ROC> .\target\debug\roc.exe build --emit-llvm-ir .\examples\platform-switching\rocLovesZig.roc
🔨 Rebuilding platform...
Emitting LLVM IR to .\examples\platform-switching\rocLovesZig.ll
0 errors and 0 warnings found in 3762 ms while successfully building:

    .\examples\platform-switching\rocLovesZig.exe
PS C:\Users\bosyl\Documents\GitHub\ROC> zig build-exe .\examples\platform-switching\rocLovesZig.ll .\examples\platform-switching\zig-platform\host.zig -lc
PS C:\Users\bosyl\Documents\GitHub\ROC> .\host.exe
STARTING
HERE
Roc <3 Zig!
runtime: 0.020ms

I'll continue digging...

view this post on Zulip Luke Boswell (Jan 08 2024 at 03:19):

Ok the issue here is in the Windows surgical linker; using --linker=legacy is working ok ... but only if you build the app and then run the executable, something fishy is going on when you use roc run

PS C:\Users\bosyl\Documents\GitHub\ROC> .\target\release\roc.exe build --linker=legacy .\examples\platform-switching\rocLovesZig.roc
🔨 Rebuilding platform...
0 errors and 0 warnings found in 4630 ms while successfully building:

    .\examples\platform-switching\rocLovesZig.exe
PS C:\Users\bosyl\Documents\GitHub\ROC> .\examples\platform-switching\rocLovesZig.exe
STARTINGHERERoc <3 Zig!
runtime: 0.022ms
PS C:\Users\bosyl\Documents\GitHub\ROC> .\target\release\roc.exe build .\examples\platform-switching\rocLovesZig.roc
🔨 Rebuilding platform...
0 errors and 0 warnings found in 4706 ms while successfully building:

    .\examples\platform-switching\rocLovesZig.exe
PS C:\Users\bosyl\Documents\GitHub\ROC> .\examples\platform-switching\rocLovesZig.exe
STARTING

view this post on Zulip Luke Boswell (Jan 08 2024 at 03:26):

Totally irrelevant for debugging... but just noticed this and thought it was interesing

                Surgical Linker Legacy Linker
DEV backend     96 ms           3219 ms
LLVM backend    184 ms          3244 ms
PS C:\Users\bosyl\Documents\GitHub\ROC> .\target\release\roc.exe build --dev --prebuilt-platform .\examples\platform-switching\rocLovesZig.roc
0 errors and 0 warnings found in 96 ms while successfully building:

    .\examples\platform-switching\rocLovesZig.exe
PS C:\Users\bosyl\Documents\GitHub\ROC> .\target\release\roc.exe build --dev --linker=legacy --prebuilt-platform .\examples\platform-switching\rocLovesZig.roc
0 errors and 0 warnings found in 3219 ms while successfully building:

    .\examples\platform-switching\rocLovesZig.exe
PS C:\Users\bosyl\Documents\GitHub\ROC> .\target\release\roc.exe build --prebuilt-platform .\examples\platform-switching\rocLovesZig.roc
0 errors and 0 warnings found in 184 ms while successfully building:

    .\examples\platform-switching\rocLovesZig.exe
PS C:\Users\bosyl\Documents\GitHub\ROC> .\target\release\roc.exe build --linker=legacy --prebuilt-platform .\examples\platform-switching\rocLovesZig.roc
0 errors and 0 warnings found in 3244 ms while successfully building:

    .\examples\platform-switching\rocLovesZig.exe

view this post on Zulip Luke Boswell (Jan 08 2024 at 03:30):

Using the legacy linker both DEV and LLVM backends work correctly :check:
Using the surgical linker both crash with a segfault :lady_beetle:

view this post on Zulip Richard Feldman (Jan 08 2024 at 03:58):

3300% speed improvement for surgical linker :joy:

Screenshot-2024-01-07-at-10.58.28-PM.png


Last updated: Jul 06 2025 at 12:14 UTC