Skip to content

self-hosted compiler has issues with -fPIC static libraries #25784

@JAicewizard

Description

@JAicewizard

Zig Version

0.15.2

Steps to Reproduce and Observed Behavior

I can possibly provide a full reproducer repo, but it is quite big. Here is the build.zig however, I think it should give a decent amount of insight into the build process:

const std = @import("std");
const vkgen = @import("vulkan_zig");

const version = "0.0.1";

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const lib = b.addLibrary(.{
        .linkage = .dynamic,
        .name = "strix",
        .root_module = b.createModule(.{
            .root_source_file = b.path("src/strix.zig"),
            .target = target,
            .optimize = optimize,
            .pic = true,
        }),
    });

    lib.addIncludePath(b.path("duckdb/src/include"));

    // Allow Zig to find and parse the bridge C ABI
    lib.addIncludePath(b.path("src/include"));

    // Generate vulkan bingings
    const vkzig_dep = b.dependency("vulkan_zig", .{
        .registry = b.dependency("vulkan_headers", .{}).path("registry/vk.xml"),
    });
    const vkzig_bindings = vkzig_dep.module("vulkan-zig");
    lib.root_module.addImport("vulkan", vkzig_bindings);

    const comp1_cmd = b.addSystemCommand(&.{ "glslc", "--target-env=vulkan1.0", "-o" });
    const comp1_spv = comp1_cmd.addOutputFileArg("comp1.spv");
    comp1_cmd.addFileArg(b.path("shaders/minimal.comp"));

    lib.root_module.addAnonymousImport("comp1", .{ .root_source_file = comp1_spv });
    const comp2_cmd = b.addSystemCommand(&.{ "glslc", "--target-env=vulkan1.0", "-o" });
    const comp2_spv = comp2_cmd.addOutputFileArg("comp2.spv");
    comp2_cmd.addFileArg(b.path("shaders/minimal2.comp"));
    lib.root_module.addAnonymousImport("comp2", .{ .root_source_file = comp2_spv });

    // compile c++ exxtension
    const cc = b.addSystemCommand(&.{ "g++", "-c", "-Iduckdb/src/include", "-fPIC", "src++/strix_extension.cpp", "-o", "hello.o" });
    lib.step.dependOn(&cc.step);

    // ----------------------------
    // EXTERNAL LINKING FOR EXTENSION
    // ----------------------------

    lib.addLibraryPath(b.path("duckdb/build/reldebug/src"));
    lib.addLibraryPath(b.path("duckdb/build/reldebug/third_party/fmt"));
    lib.addLibraryPath(b.path("duckdb/build/reldebug/third_party/libpg_query/"));
    lib.addLibraryPath(b.path("duckdb/build/reldebug/third_party/utf8proc/"));
    lib.addLibraryPath(b.path("duckdb/build/reldebug/third_party/re2/"));
    lib.addLibraryPath(b.path("duckdb/build/reldebug/third_party/mbedtls/"));
    lib.addLibraryPath(b.path("duckdb/build/reldebug/third_party/skiplist/"));
    lib.addLibraryPath(b.path("duckdb/build/reldebug/third_party/yyjson/"));
    lib.addLibraryPath(b.path("duckdb/build/reldebug/third_party/hyperloglog/"));
    lib.addLibraryPath(b.path("duckdb/build/reldebug/third_party/miniz/"));
    lib.addLibraryPath(b.path("duckdb/build/reldebug/third_party/fastpforlib/"));
    lib.addLibraryPath(b.path("duckdb/build/reldebug/third_party/fsst/"));
    lib.addLibraryPath(b.path("duckdb/build/reldebug/third_party/zstd"));
    lib.addLibraryPath(b.path("duckdb/build/reldebug/extension/parquet"));
    lib.addLibraryPath(b.path("duckdb/build/reldebug/extension/core_functions"));
    lib.addLibraryPath(b.path("duckdb/build/reldebug/extension/jemalloc/"));


    // libc is needed to link most C libraries, including most drivers
    lib.linkLibC();
    // Link against the version of libduckdb built by the Nix derivation
    lib.linkSystemLibrary("duckdb_static");
    lib.linkSystemLibrary("duckdb_fmt");
    lib.linkSystemLibrary("duckdb_pg_query");
    lib.linkSystemLibrary("duckdb_utf8proc");
    lib.linkSystemLibrary("duckdb_re2");
    lib.linkSystemLibrary("duckdb_mbedtls");
    lib.linkSystemLibrary("duckdb_skiplistlib");
    lib.linkSystemLibrary("duckdb_yyjson");
    lib.linkSystemLibrary("duckdb_hyperloglog");
    lib.linkSystemLibrary("duckdb_miniz");
    lib.linkSystemLibrary("duckdb_fastpforlib");
    lib.linkSystemLibrary("duckdb_fsst");
    lib.linkSystemLibrary("duckdb_zstd");
    lib.linkSystemLibrary("parquet_extension");
    lib.linkSystemLibrary("core_functions_extension");
    lib.linkSystemLibrary("jemalloc_extension");
    lib.addObjectFile(b.path("hello.o"));

    const install_lib = b.addInstallArtifact(
        lib,
        .{ .dest_sub_path = "strix.duckdb_extension" },
    );

    const tool_run = b.addSystemCommand(&.{"cmake"});
    tool_run.addArgs(&.{
        "-DABI_TYPE=CPP",
        "-DEXTENSION=zig-out/lib/strix.duckdb_extension",
        "-DPLATFORM_FILE=duckdb/build/reldebug/duckdb_platform_out",
        "-DVERSION_FIELD=v1.3.0",
        "-DEXTENSION_VERSION=" ++ version,
        "-DNULL_FILE=duckdb/scripts/null.txt",
        "-P",
        "duckdb/scripts/append_metadata.cmake",
    });
    tool_run.step.dependOn(&install_lib.step);
    b.getInstallStep().dependOn(&tool_run.step);
}

After running zig build the observed behavior is:

error: relocation at offset 0xfea against symbol '_ZTIN6duckdb17InternalExceptionE' cannot be used
    note: in /.../duckdb/build/reldebug/third_party/mbedtls/libduckdb_mbedtls.a(mbedtls_wrapper.cpp.o/):.text
    note: recompile with -fPIC
error: relocation at offset 0x16ce against symbol '_ZTIN6duckdb17InternalExceptionE' cannot be used
    note: in /.../duckdb/build/reldebug/third_party/mbedtls/libduckdb_mbedtls.a(mbedtls_wrapper.cpp.o/):.text
    note: recompile with -fPIC
error: relocation at offset 0x172b against symbol '_ZTIN6duckdb21InvalidInputExceptionE' cannot be used
    note: in /.../duckdb/build/reldebug/third_party/mbedtls/libduckdb_mbedtls.a(mbedtls_wrapper.cpp.o/):.text
    note: recompile with -fPIC

Expected Behavior

The behavior should be the same as zig build -Doptimize=ReleaseSafe, which does produce a successful build. In 0.14 the debug builds also produced a working output, leading me to beleive this is the self-hosted compiler.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions