Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support dynamic linking musl libc #5364

Closed
necessarily-equal opened this issue May 17, 2020 · 8 comments · Fixed by #7406
Closed

support dynamic linking musl libc #5364

necessarily-equal opened this issue May 17, 2020 · 8 comments · Fixed by #7406
Labels
accepted This proposal is planned. bug Observed behavior contradicts documented or intended behavior enhancement Solving this issue will likely involve adding new logic or components to the codebase. os-linux proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@necessarily-equal
Copy link

downstream bug report https://gitlab.alpinelinux.org/alpine/aports/-/issues/11399.

Programs built by zig can't initialize video output (audio is fine, though): SDL_Init(SDL_INIT_VIDEO) fails.

I wrote a minimal C program to test this further, but I originally noticed the issue when compiling zig code.

to test this, I used the following:

  • sdl.c
  • zig-logo.bmp (I'm not using attachments because github refuses bmp images)
  • Install zig, sdl2+headers, sdl_image+headers, gcc and clang. (apk add -t .zig zig sdl2 sdl2-dev sdl2_image-dev build-base clang on Alpine)

(using the SDL_image pkg-config to ensure correctness)

> zig cc sdl.c $(pkg-config SDL2_image --cflags --libs) && ./a.out
Could not initialize SDL. SDL_Error: No available video device

Yet the C code is functional:

> gcc sdl.c $(pkg-config SDL2_image --cflags --libs) && ./a.out
# works

and clang (LLVM) also works:

> clang sdl.c $(pkg-config SDL2_image --cflags --libs) && ./a.out
# works

The cause seems to reside at link time:

> zig cc sdl.c $(pkg-config SDL2_image --cflags)
> gcc sdl.o $(pkg-config SDL2_image --libs)
> ./a.out
# works

The issue was tested with Zig 0.6, and was already present with Zig 0.5. The same code works with Zig on Debian.

I don't know how to debug this further, but feel free to ask any question.

@necessarily-equal necessarily-equal changed the title dynamic linking issues on musl libc linux distributions linking issues on musl libc linux distributions May 24, 2020
@necessarily-equal
Copy link
Author

necessarily-equal commented May 24, 2020

Using the static version of the SDL also doesn't work, unfortunately

EDIT: note that the executable wasn't fully static though

@andrewrk andrewrk added downstream An issue with a third party project that uses Zig. accepted This proposal is planned. enhancement Solving this issue will likely involve adding new logic or components to the codebase. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. and removed downstream An issue with a third party project that uses Zig. labels Oct 4, 2020
@andrewrk andrewrk changed the title linking issues on musl libc linux distributions support dynamic linking musl libc Oct 4, 2020
@andrewrk andrewrk added this to the 0.8.0 milestone Oct 4, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.7.1 Dec 12, 2020
@andrewrk andrewrk added the bug Observed behavior contradicts documented or intended behavior label Dec 12, 2020
@andrewrk
Copy link
Member

I think it is fair to also call this a bug because it makes zig unusable on musl-based linux distros for applications that require dynamic linking.

@alexrp
Copy link
Member

alexrp commented Jul 9, 2021

@ifreund from what I can gather, #7406 only allows dynamically linking musl when doing native builds, right? Is there a way to extend this support for cross scenarios? (I'm not overly familiar with how musl distros work - I just happened to notice that cross-compiling an x86_64-linux-musl executable always results in musl being statically linked.)

@andrewrk
Copy link
Member

andrewrk commented Jul 9, 2021

Static is the default; you need to add the -dynamic flag.

@alexrp
Copy link
Member

alexrp commented Jul 9, 2021

Ah, the issue was that I tried -lc and -dynamic separately, not together. Combining them did the trick, thanks!

@alexrp
Copy link
Member

alexrp commented Jul 9, 2021

Is there a way to achieve the same with zig cc?

$ zig cc main.c -target x86_64-linux-musl -fPIE -lc
$ wsl musl-ldd a.out
        musl-ldd (0x7f5ffe36e000)

@motiejus
Copy link
Contributor

motiejus commented Jun 22, 2022

Full repro:

main.c

#include <stdio.h>
#include <features.h>

int main() {
    #ifdef __GLIBC__
    printf("glibc_%d.%d\n", __GLIBC__, __GLIBC_MINOR__);
    #else
    printf("non-glibc\n");
    #endif
    return 0;
}

Compiling:

$ zig cc -v -target x86_64-linux-musl main.c -fPIE -lc -dynamic -o main
<...>
LLD Link... ld.lld -error-limit=0 -O0 -z stack-size=16777216 --gc-sections --eh-frame-hdr -znow -m elf_x86_64 -static -pie -o /home/motiejus/.cache/zig/o/503c6bf2ee43b2d33014a8d71f6bc1d2/main /home/motiejus/.cache/zig/o/60b545a849e93516862f973801e6307b/rcrt1.o /home/motiejus/.cache/zig/o/1f9266dba05df20689bccc6b184442ec/crti.o /home/motiejus/.cache/zig/o/6471a2fd822202eae06273e85f08f5c6/main.o /home/motiejus/.cache/zig/o/baf786ce8c76ce866d19a60d75637d88/libcompiler_rt.a --as-needed /home/motiejus/.cache/zig/o/a111f160cb9815a5d7c8128bcf0c7af8/libc.a /home/motiejus/.cache/zig/o/c8866bc9544d3faae13fa04a4728ac91/crtn.o --allow-shlib-undefined

Inspect the resulting main. It is static:

$ objdump --dynamic-syms main
main:     file format elf64-x86-64

DYNAMIC SYMBOL TABLE:
no symbols


$ file main
main: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), static-pie linked, with debug_info, not stripped

Looking at the linker line above, you can see that -static is passed to the linker, and at least --dynamic-linker option is missing. So I believe this should still be open, at least for binaries produced with zig cc.

@ifreund
Copy link
Member

ifreund commented Jun 22, 2022

@motiejus Would you mind opening a new issue for that with that example? It seems like a bug in the zig cc frontend not in the underlying feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted This proposal is planned. bug Observed behavior contradicts documented or intended behavior enhancement Solving this issue will likely involve adding new logic or components to the codebase. os-linux proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants