bflattened / bflat

C# as you know it but with Go-inspired tooling (small, selfcontained, and native executables)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Tight inner loop recommended in /src/zerolib doesn't work on Linux (doesn't link 3 SystemNative functions)

lucabol opened this issue · comments

I shall investigate more, but logging it for now in case you know the answer easily.

System.Console.WriteLine("Hello!");
λ  bflat build --stdlib:none
lld: error: undefined symbol: SystemNative_Abort
>>> referenced by Stubs.cs:89 (/home/lucabol/dev/bflat/src/zerolib/Internal/Stubs.cs:89)
>>>               /home/lucabol/dev/bflat/src/zerolib/zerolib.o:(zerolib_Internal_Runtime_CompilerHelpers_StartupCodeHelpers__RhpNewArray)
>>> referenced by Stubs.cs:91 (/home/lucabol/dev/bflat/src/zerolib/Internal/Stubs.cs:91)
>>>               /home/lucabol/dev/bflat/src/zerolib/zerolib.o:(zerolib_Internal_Runtime_CompilerHelpers_StartupCodeHelpers__RhpNewArray)
>>> referenced by Stubs.cs:51 (/home/lucabol/dev/bflat/src/zerolib/Internal/Stubs.cs:51)
>>>               /home/lucabol/dev/bflat/src/zerolib/zerolib.o:(zerolib_Internal_Runtime_CompilerHelpers_ThrowHelpers__ThrowIndexOutOfRangeException)
>>> referenced by String.cs:66 (/home/lucabol/dev/bflat/src/zerolib/System/String.cs:66)
>>>               /home/lucabol/dev/bflat/src/zerolib/zerolib.o:(String__Ctor_0)

lld: error: undefined symbol: SystemNative_Malloc
>>> referenced by Stubs.cs:91 (/home/lucabol/dev/bflat/src/zerolib/Internal/Stubs.cs:91)
>>>               /home/lucabol/dev/bflat/src/zerolib/zerolib.o:(zerolib_Internal_Runtime_CompilerHelpers_StartupCodeHelpers__RhpNewArray)

lld: error: undefined symbol: SystemNative_Log
>>> referenced by Console.Unix.cs:103 (/home/lucabol/dev/bflat/src/zerolib/System/Console.Unix.cs:103)
>>>               /home/lucabol/dev/bflat/src/zerolib/zerolib.o:(zerolib_System_Console__Write)
>>> referenced by Console.Unix.cs:111 (/home/lucabol/dev/bflat/src/zerolib/System/Console.Unix.cs:111)
>>>               /home/lucabol/dev/bflat/src/zerolib/zerolib.o:(zerolib_System_Console__Write)
>>> referenced by Console.Unix.cs:120 (/home/lucabol/dev/bflat/src/zerolib/System/Console.Unix.cs:120)
>>>               /home/lucabol/dev/bflat/src/zerolib/zerolib.o:(zerolib_System_Console__Write)

FYI I tested it on Windows and it works. It appears to be just a Linux thing.

On linux you'll need to add --ldflags path_to_bflat/lib/linux/x64/glibc/libSystem.Native.a. Zerolib doesn't call into libc directly to avoid problems. libSystem.Native.a is a library from the dotnet/runtime repo and the real CoreLib uses it to talk to the system. Defining a p/invoke into libc symbols directly doesn't always work and I didn't want to explore the reasons why dotnet/runtime repo chose a wrapper. Pretty sure there's good reasons.

It almost looks like a feature, so that you can plugin your own 'system'. Perhaps a '--freestanding' option to the compiler?

Leaving the issue open as the compiler should add this automatically in Linux, I think.

BTW: I also wondered why .net doesn't p/invoke to libc directly as you would think ucrt would allow a single p/invoke codepath between linux/win. But I agree, there must be reasons.

Leaving the issue open as the compiler should add this automatically in Linux, I think.

It adds it automatically for stdlib:zero. It doesn't add it for stdlib:none because it could be a stdlib that does imports differently. These .a files are a zerolib implementation detail.