-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Closed
Labels
bugObserved behavior contradicts documented or intended behaviorObserved behavior contradicts documented or intended behaviorstandard libraryThis issue involves writing Zig code for the standard library.This issue involves writing Zig code for the standard library.
Milestone
Description
Zig Version
0.11.0-dev.3893+0783dc87f
Steps to Reproduce and Observed Behavior
I discovered this while writing some standalone tests for the DWARF unwinder.
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const shared_lib = b.addSharedLibrary(.{
.name = "shared_lib",
.root_source_file = .{ .path = "src/shared_lib.zig" },
.target = target,
.optimize = optimize,
});
const exe = b.addExecutable(.{
.name = "test",
.target = target,
.optimize = optimize,
});
exe.addCSourceFile("src/main.c", &[_][]const u8{"-std=c99"});
exe.linkLibrary(shared_lib);
exe.linkLibC();
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
}main.c
#include <assert.h>
#include <stdio.h>
#include <stdint.h>
size_t getBaseAddress();
int main(int argc, char **argv) {
size_t base_address = getBaseAddress();
assert(base_address != 0);
printf("base_address: %x\n", base_address);
}
shared_lib.zig
const std = @import("std");
export fn getBaseAddress() usize {
_ = std.debug.getSelfDebugInfo() catch @panic("failed to openSelfDebugInfo");
return std.process.getBaseAddress();
}
$ zig build run
thread 37113 panic: integer overflow
The issue is that phdr is 0 when called in this context (zig shared lib that was linked with a C program).
.linux => {
const base = os.system.getauxval(std.elf.AT_BASE);
if (base != 0) {
return base;
}
const phdr = os.system.getauxval(std.elf.AT_PHDR);
return phdr - @sizeOf(std.elf.Ehdr);
},
This also occurs if the main program is a zig program, which links a c shared lib, that links a zig shared lib that calls this function.
If you add shared_lib.linkLibC(); the problem goes away.
The problem doesn't happen if the main program is a zig program.
Expected Behavior
This function should not crash and return a valid result.
Metadata
Metadata
Assignees
Labels
bugObserved behavior contradicts documented or intended behaviorObserved behavior contradicts documented or intended behaviorstandard libraryThis issue involves writing Zig code for the standard library.This issue involves writing Zig code for the standard library.