Skip to content

[zig cc] unable to dynamically link musl executables #11909

Description

@motiejus

Zig Version

0.10.0-dev.2473+e498fb155

Like the title says: zig cc frontend cannot compile a dynaimcally-linked musl binary. Created a separate issue as requested by @ifreund in #5364 (comment)

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviormiscompilationThe compiler reports success but produces semantically incorrect code.os-linuxLinuxzig ccZig as a drop-in C compiler feature

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions