zig icon indicating copy to clipboard operation
zig copied to clipboard

Panic in ziglang/zig/src/link/MachO/Dylib.zig:513:48

Open moosichu opened this issue 1 year ago • 0 comments

Zig Version

0.13.0-dev.3694+dddddcffd

Steps to Reproduce and Observed Behavior

OK, so I did a bad thing in my build.zig, and this isn't a blocker as I fixed my issue, but I will write this up anyway as an error is probably better than a panic.

So in the build.zig, you can do something naughty like:

    const obj = b.addObject(.{
        .name = "zig_root",
        .root_source_file = .{ .path = "src/root.zig" },
        .target = target,
        .optimize = optimize,
    });

    const lib = b.addSharedLibrary(.{
        .name = "lib",
        .root_source_file = .{ .path = "src/lib.zig" },
        .target = target,
        .optimize = optimize,
    });

    // This declares intent for the library to be installed into the standard
    // location when the user invokes the "install" step (the default step when
    // running `zig build`).
    b.installArtifact(lib);

    const exe = b.addExecutable(.{
        .name = "zig-linker-bug",
        .root_source_file = b.path("src/main.zig"),
        .target = target,
        .optimize = optimize,
    });
    exe.addObject(obj);
    exe.linkLibrary(lib);
    obj.linkLibrary(lib);

    // This declares intent for the executable to be installed into the
    // standard location when the user invokes the "install" step (the default
    // step when running `zig build`).
    b.installArtifact(exe);

Basically, creating a shared library, and then linkig it to an object & executable (which also has had the object added).

You just need the library to export a function for this to trigger:

export fn export_test() callconv(.C) usize {
    return 0;
}

The minimal repro can be found here: Archive.zip

Just running zig build on this will trigger a panic when linking!

Expected Behavior

Not a crash

moosichu avatar Apr 21 '24 16:04 moosichu