Im trying to leverage zig’s excellent cross compile ability to start tinkering with my jailbroken kindle but im encountering weird segfaults hopefully someone here can help me. (zig ver 0.15.2)
main.zig
const std = @import("std");
pub fn main() !void {
std.log.info("test zig", .{});
}
build.zig
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.resolveTargetQuery(.{
.cpu_arch = .arm,
.cpu_model = .{ .explicit = &std.Target.arm.cpu.cortex_a9 },
.os_tag = .linux,
.abi = .gnueabi,
.glibc_version = std.SemanticVersion.parse("2.20.0") catch unreachable,
});
const optimize = .Debug;
const exe = b.addExecutable(.{
.name = "zigkindle",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
// .link_libc = true,
// .strip = true,
}),
.use_lld = true,
.use_llvm = true,
});
}
Here are some of the weirdness i observed:
- That compiles, and running it with the kindle’s gdb shows the stack trace (just for example i set a breakpoint at syscall)
Catchpoint 1 (call to syscall prlimit64), 0x000815e8 in os.linux.arm.syscall4 (number=prlimit64, arg1=0, arg2=3, arg3=0, arg4=3204446856)
at C:\Users\mojoj\scoop\apps\zig\0.15.2\lib\std\os\linux/arm.zig:51
51 in C:\Users\mojoj\scoop\apps\zig\0.15.2\lib\std\os\linux/arm.zig
#0 0x000815e8 in os.linux.arm.syscall4 (number=prlimit64, arg1=0, arg2=3, arg3=0, arg4=3204446856) at C:\Users\mojoj\scoop\apps\zig\0.15.2\lib\std\os\linux/arm.zig:51
#1 0x000cb53c in os.linux.prlimit (pid=0, resource=STACK, new_limit=0x0, old_limit=0xbefffa88) at C:\Users\mojoj\scoop\apps\zig\0.15.2\lib\std\os/linux.zig:2564
#2 0x000cb268 in os.linux.getrlimit (resource=STACK, rlim=0xbefffa88) at C:\Users\mojoj\scoop\apps\zig\0.15.2\lib\std\os/linux.zig:2555
#3 0x000caf80 in posix.getrlimit (resource=STACK) at C:\Users\mojoj\scoop\apps\zig\0.15.2\lib\std/posix.zig:6966
#4 0x000ca894 in start.expandStackSize (phdrs=...) at C:\Users\mojoj\scoop\apps\zig\0.15.2\lib\std/start.zig:553
#5 0x000ca284 in start.posixCallMainAndExit (argc_argv_ptr=0xbefffcc0) at C:\Users\mojoj\scoop\apps\zig\0.15.2\lib\std/start.zig:525
#6 0x00000000 in ?? ()
- But if i set
.link_libc = trueit just segfaults and gdbbtis weird
Catchpoint 1 (returned from syscall mprotect), 0x4001952c in ?? () from /lib/ld-linux.so.3
#0 0x4001952c in ?? () from /lib/ld-linux.so.3
#1 0x4000bb78 in ?? () from /lib/ld-linux.so.3
#2 0x4000c208 in ?? () from /lib/ld-linux.so.3
#3 0x00000000 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)
Program received signal SIGSEGV, Segmentation fault.
0x4000b8bc in ?? () from /lib/ld-linux.so.3
- If is set
.link_libc = trueand.strip = trueit executes fine, but the back trace is still weird
info: test zig
Catchpoint 1 (returned from syscall writev), 0x40127cbc in writev () from /lib/libc.so.6
#0 0x40127cbc in writev () from /lib/libc.so.6
#1 0x00055b74 in ?? ()
#2 0x00054f84 in ?? ()
#3 0x00030e28 in ?? ()
#4 0x00035568 in ?? ()
#5 0x00032878 in ?? ()
#6 0x000301dc in ?? ()
#7 0x00051ba4 in ?? ()
#8 0x00051a28 in ?? ()
#9 0x0005112c in ?? ()
#10 0x00051118 in ?? ()
#11 0x0005137c in ?? ()
#12 0x400774fc in __libc_start_main () from /lib/libc.so.6
#13 0x00028124 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)
Catchpoint 1 (call to syscall exit_group), 0x400fb720 in _exit () from /lib/libc.so.6
#0 0x400fb720 in _exit () from /lib/libc.so.6
#1 0x40091d80 in ?? () from /lib/libc.so.6
#2 0x40077500 in __libc_start_main () from /lib/libc.so.6
#3 0x00028124 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)
[Inferior 1 (process 4244) exited normally]
- While the the previous setup works, its unstable and calling other zig std lib functions could segfault it (e.g.
std.process.Child.run)
I think i could proceed without linking libc but i need it if i want to link x11, cairo, etc. Can anyone help me?