Segfault when linking libc on kindle

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:

  1. 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 ?? ()
  1. But if i set .link_libc = true it just segfaults and gdb bt is 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
  1. If is set .link_libc = true and .strip = true it 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]
  1. 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?

That first stack trace immediately makes me suspicious that your kernel version is too old.

See: 0.15.1 Release Notes ⚡ The Zig Programming Language

[root@kindle root]# uname -a
Linux kindle 3.0.35-lab126 #8 PREEMPT Tue Aug 1 12:49:59 UTC 2023 armv7l GNU/Linux

Does this mean there no way for me to get 0.15.x zig working on the kindle? Is there a zig version that still supports that?

There probably is a Zig version that works on it, but I suspect you’ll have to go quite far back since that kernel is from 2012.

I’m seeing a lot of resources for updating a Linux kernel on an already-jailbroken Kindle, that’s probably your best bet.

It won’t just be Zig, you’ll find a lot of software is not prepared to deal correclty with a fifteen-year-old kernel.

Modern kernels should perform just fine on the hardware, too, the kernel developers are pretty careful about that.

after some more tinkering ive decided to just maintain a seperate version of zig stdlib to patch out the syscalls as needed. Oddly i also have to force zig to use std.os.environ since std.c.environ was what was causing it to segfault immedietly for some reason.

can you point me to one of these resources u mentioned? i cant seem to find it on google

1 Like

upgrade linux kernel kindle does it for me on DDG, I can’t imagine Google is all that different?

here’s the first hit

Huh DDG actually gives better first and second page results than google lol thnx for the link.

That does have some resource on how to build n run the kernel, but i will still need to merge amazon’s changes to a later version and looking at the number of github repo that still hasnt done this successfully, i dont think i have a better chance of doing it :sweat_smile: