ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.9k stars 2.55k forks source link

Freestanding code failing silently #14752

Closed kulkarniniraj closed 1 year ago

kulkarniniraj commented 1 year ago

Zig Version

0.10.0

Steps to Reproduce and Observed Behavior

I am trying to write a simple user mode code in xv6 OS. Following simple C code works perfectly

#include "types.h"
#include "stat.h"
#include "user.h"

int main(){
    int fd, out;
    struct stat st;
    fd = open(".", 0);
    printf(2, "fd: %d\n", fd);

    out = fstat(fd, &st);
    printf(2, "out: %d %d\n", out, st.type);

    exit();
    return 0;
}

But in zig equivalent I get trap-6 error and program is killed.

const user = @cImport({
    @cInclude("types.h");
    @cInclude("stat.h");
    @cInclude("user.h");
});

export fn main() i32 {
    var fd: i32 = 0;
    fd = user.open(".", 0);
    user.printf(2, "fd: %d\n", fd);

    var st: user.struct_stat = undefined;
    var out: i32 = -1;

    out = user.fstat(fd, &st);

    user.printf(2, "out: %d\n", out);

    user.exit();            
    return 0;
}

Build commands:

zig build-obj  zig/scratchpad.zig -O ReleaseSafe -target i386-freestanding -I .
$(LD) $(LDFLAGS) -N -e main -Ttext 0 -o $@ scratchpad.o ulib.o usys.o printf.o umalloc.o 

This is especially frustrating since compiler does not give warning and I cannot use Debug mode (some error about zig probe stack not found)

Expected Behavior

Program does not crash

andrewrk commented 1 year ago

I cannot use Debug mode (some error about zig probe stack not found)

You can use -fcompiler-rt or -fno-stack-check to resolve this issue, or use Zig as the linker.

But in zig equivalent I get trap-6 error and program is killed.

Can you share the difference in machine code?

andrewrk commented 1 year ago

I'm sorry but this is not enough information to confirm the bug. If you're still willing to give this a try, share more information about the generated machine code of Zig and why it is problematic, and I will re-open this.

kulkarniniraj commented 1 year ago

Sure, I have created C and Zig object files. Can't upload it here. But if you want me to run any command, let me know.

xfxpositions commented 1 year ago
pub fn put_chr(self: Tty, x: u8, y: u8, entry: VgaEntry) void {
        _ = y;
        _ = x;
        self.buffer[0] = entry;
    }

works fine.

pub fn put_chr(self: Tty, x: u8, y: u8, entry: VgaEntry) void {
        self.buffer[x*y] = entry;
    }

error.

run.sh

build=build # build dir
isoname=xfxos.iso # isoname
mkdir -p $build # create build dir

zig build # build with build.zig file 
nasm src/boot.s -f elf32 -o build/boot.o # compile multiboot header
ld -m elf_i386 -T linker.ld -o build/isofiles/boot/kernel.bin build/boot.o build/kernel.o # link the kernel

d=build/isofiles/boot/grub
mkdir -p "$d" && cp grub.cfg "$d"

sudo grub-mkrescue -o build/os.iso build/isofiles # create iso
qemu-system-i386 build/os.iso # run with qemu

error:

ld: build/kernel.o: in function `debug.panicExtra__anon_1055':
/home/tengri/.zvm/master/lib/std/debug.zig:358:(.text+0x14b): undefined reference to `__zig_probe_stack'
ld: /home/tengri/.zvm/master/lib/std/debug.zig:363:(.text+0x1bb): undefined reference to `memset'
ld: build/kernel.o: in function `io.writer.Writer(*io.fixed_buffer_stream.FixedBufferStream([]u8),error{NoSpaceLeft},(function 'write')).writeByteNTimes':
/home/tengri/.zvm/master/lib/std/io/writer.zig:37:(.text+0x1442): undefined reference to `memset'
ld: /home/tengri/.zvm/master/lib/std/io/writer.zig:38:(.text+0x1465): undefined reference to `memset'
ld: build/kernel.o: in function `fmt.formatInt__anon_1726':
/home/tengri/.zvm/master/lib/std/fmt.zig:1418:(.text+0x1633): undefined reference to `memset'
ld: build/kernel.o: in function `debug.panicExtra__anon_1812':
/home/tengri/.zvm/master/lib/std/debug.zig:358:(.text+0x1d6b): undefined reference to `__zig_probe_stack'
ld: /home/tengri/.zvm/master/lib/std/debug.zig:363:(.text+0x1ddb): undefined reference to `memset'
ld: build/kernel.o: in function `io.fixed_buffer_stream.FixedBufferStream([]u8).write':
/home/tengri/.zvm/master/lib/std/io/fixed_buffer_stream.zig:70:(.text+0x21b1): undefined reference to `memcpy'

source repo: https://github.com/xfxpositions/xfxos2

i tried

ld -m elf_i386 -fno-stack-check  -T linker.ld -o build/isofile
s/boot/kernel.bin build/boot.o build/kernel.o

> ld: -f may not be used without -shared