Fork me on GitHub
#babashka-sci-dev
<
2022-02-25
>
Crispin14:02:41

Hey @rahul080327 @thiagokokada what is the reason to prefer musl static linking in native images over standard libc?

lispyclouds14:02:32

Hey @retrogradeorbit essentially glibc cannot be fully static linked in a way musl can be, see this https://stackoverflow.com/a/57478728 pretty much the reason why musl was built. its a general problem for any native binaries, not specific to GraalVM native images

lispyclouds14:02:41

Rust for instance only does static linked binaries when used with musl+linux targets.

lispyclouds14:02:42

Or the other way to go is Go 😛 where none of the system libs are used in the same way other binaries link with

kokada14:02:45

The SO link posted by lispyclouds pretty much nails it

kokada14:02:44

glibc static is something that kinda works most of the time, however may results in segfaults if you for example use a non-glibc distro or uses a glibc distro with an older version of glibc

kokada14:02:01

Or an older version of the kernel, or using on the wrong phase of the moon

lispyclouds14:02:36

yeah one of the main reasons for the segfaults being a mismatch of the ABI expectations the binary has with the kernel

kokada14:02:47

You can have trully static binaries with glibc, but AFAIK this needs a specially compiled version of glibc with some flags that are completely unsupported by upstream

Crispin14:02:22

yeah... i dunno. I get that glibc static linking is a bit of a hack, and doesnt even work with all toolchains...

kokada14:02:45

Also, musl has some other advantages

borkdude14:02:49

I've had quite some problems with the glibc-ed static versions of babashka and clj-kondo and will never go back doing that: those segfaults are a major headache.

kokada14:02:49

It results in smaller binaries

Crispin14:02:51

but the graal static glibc image doesn't seem to match what that SO answer is talking about at all

kokada14:02:28

Like, I may be wrong, but the static babashka with musl is smaller than the dynamically babashka with glibc

borkdude14:02:35

that is true

Crispin14:02:36

I don't see any lib deps, no dlopens, no nss, no iconv

Crispin14:02:48

works on alpine

Crispin14:02:56

I'll go have a compare of sizes

borkdude15:02:23

I recommend either don't pretend to be static, or use musl.

Crispin15:02:27

SO post may be talking in a more general sense of why static linking glibc is problematic?

kokada15:02:36

Yeah, but for example

kokada15:02:47

The old babashka static version (with glibc) didn't work in NixOS at all

1
Crispin15:02:49

I dont think its pretending to be static

kokada15:02:52

The new one does

lispyclouds15:02:57

> I don't see any lib deps, no dlopens, no nss, no iconv well thats some of the possible calls the binary may make, the graalvm one doesnt i guess?

kokada15:02:23

> well thats some of the possible calls the binary may make, the graalvm one doesnt i guess? Yeah, Java itself may end up calling nss for example

borkdude15:02:25

For aarch64, musl is not supported. There I use the mostly static option: --static with dynamic glibc. that works fine.

kokada15:02:32

So Java -> GraalVM can call it too

Crispin15:02:34

yeah. and I experimented with musl versions and graal. And the new musl versions don't seem to work with graal at all

kokada15:02:16

> For aarch64, musl is not supported. There I use the mostly static option: --static with dynamic glibc. that works fine. This is also a good point, static building glibc is not supported at all with GraalVM Native Image either

kokada15:02:23

It works but it is not documented

Crispin15:02:48

hmm but I just built a static glibc with graal

Crispin15:02:57

not mostly static. fully static

kokada15:02:57

The documentation explicitly says: • Dynamically compilation • Static compilation with musl • Static compilation with glibc dynamically linked

kokada15:02:20

> not mostly static. fully static Like I said, it works. It is NOT supported though

Crispin15:02:47

is it my graal version?

Crispin15:02:52

Im using 21.3.0

kokada15:02:55

It was never supported

kokada15:02:04

But was never documented

kokada15:02:11

You can see in the documentation I posted above

kokada15:02:30

It either recommends you to build with musl OR dynamically loaded glibc

☝️ 1
Crispin15:02:17

yeah it does

borkdude15:02:26

This musl or dynamic documentation showed up around a year or so ago, before then the only option was --static but this was in hindsight a mistake on their end.

lispyclouds15:02:07

yeah thats how we initially built the bb and kondo images with quite the suffering later on

lispyclouds15:02:46

its pretty much undefined behaviour when the image is "mostly static"

Crispin15:02:13

ok. i think I get it

Crispin15:02:24

its always been a hack until musl

Crispin15:02:24

just curiously do you remember how the problem of static glibc manifested on other platforms?

borkdude15:02:46

@rahul080327 I refer to "mostly static" as a supported option explictly by the GraalVM docs. This is supported and gives defined behavior. https://www.graalvm.org/22.0/reference-manual/native-image/StaticImages/#build-mostly-static-native-image

1
Crispin15:02:26

do you remember what platform?

borkdude15:02:06

don't remember, but I think it happened on systems that had a different (old?) glibc than the ubuntu I compiled it on

borkdude15:02:15

maybe search in babashka's issue

Crispin15:02:24

awesome. this is great info. thanks

lispyclouds15:02:15

if i rack my brains it was debian 9 i think ?

Crispin15:02:30

reading that ticket... wow that is horrific

lispyclouds15:02:26

yeah when it hit us it was very nasty head scratcher for us 😕

borkdude15:02:04

I now use the -H:+StaticExecutableWithDynamicLibC option for aarch64

kokada15:02:57

Yeah, I took this issue exactly from Babashka source code (describing why we are using -H:+StaticExecutableWithDynamicLibC) 😅

kokada15:02:09

BTW, does those binaries work on Android?

kokada15:02:16

I assume no?

borkdude15:02:22

yes, they do on android tablets

kokada15:02:48

Weird, Android shouldn't have glibc

kokada15:02:54

Does it work in Alpine?

borkdude15:02:43

Dunno, try it ;)

borkdude15:02:51

I expect no, since it requires glibc

kokada15:02:16

With termux I can understand

kokada15:02:21

Termux may bring glibc