Skip to content

zig cc will not link to an external libunwind.so #10933

@sfackler

Description

@sfackler

Zig Version

0.9.1

Steps to Reproduce

  • Compile libunwind 1.6.2.

  • Make a simple C program using it:

#include <libunwind.h>
#include <stdio.h>

void show_backtrace (void) {
  unw_cursor_t cursor; unw_context_t uc;
  unw_word_t ip, sp;

  unw_getcontext(&uc);
  unw_init_local(&cursor, &uc);
  while (unw_step(&cursor) > 0) {
    unw_get_reg(&cursor, UNW_REG_IP, &ip);
    unw_get_reg(&cursor, UNW_REG_SP, &sp);
    printf ("ip = %lx, sp = %lx\n", (long) ip, (long) sp);
  }
}

int main() {
        show_backtrace();
}
  • Attempt to compile it with zig cc:
$ zig cc -I $PWD/libunwind/include -L $PWD/libunwind/lib test.c -lunwind -lunwind-generic

Expected Behavior

The program compiles and links to the libunwind copy that I specified.

Actual Behavior

ld.lld: error: undefined symbol: _Ux86_64_getcontext
>>> referenced by test.c
>>>               /home/sfackler/.cache/zig/o/99be576589c1a0c38e86af89b1154754/test.o:(show_backtrace)

ld.lld: error: /home/sfackler/zig-testing/libunwind/lib/libunwind-generic.so: undefined reference to _Ux86_64_setcontext [--no-allow-shlib-undefined]

The issue here is that zig cc special cases -lunwind, to always use its internal vendored copy: https://github.com/ziglang/zig/blob/master/src/main.zig#L2031-L2035.

However, the vendored version is LLVM's stripped down version and isn't actually compatible with the libunwind I'm trying to use! For reference, I care about using specifically this libunwind because it is safe to use in a signal handler, unlike (AFAIK) LLVM's version.

Would it be possible to add an extra CLI flag to suppress the libunwind special handling here? I can work around it right now by creating a libunwind-real.so symlink to libunwind.so and passing -lunwind-real, but that feels a bit hacky.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorfrontendTokenization, parsing, AstGen, Sema, and Liveness.zig ccZig as a drop-in C compiler feature

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions