The first Go-OS external C program

From Go-OS
Jump to: navigation, search
五OS running for the first time an external ELF program, at revision 179

This is the first external C program ever run in Go-OS. It was compiled using GCC :

gcc -static -m32 -Os -Iinclude -fno-builtin -ffreestanding --std=c99 -nostartfiles -nodefaultlibs -nostdlib -o test.elf test.c

And here is the program itself :

#include <stdint.h>
#include <kernel/syscall.h>

// TODO: We need a userland printf() function ...
// But for that we'll first need linking and stuff like that
#define MY_PUTS(_x) write(0, _x, sizeof(_x)-1)

void foo() {
        MY_PUTS("Called in function foo() in " __FILE__ " (userland)\n");
}

int _start() {
        int fd;
        MY_PUTS("\n");
        foo();
        MY_PUTS("\n");
        fd = open("/sample.txt", 0); // flags aren't used yet
        if (!fd) {
                MY_PUTS("couldn't open file :(\n");
        } else {
                char data[512];
                int s = read(fd, (void *)&data, 512);
                MY_PUTS("File contents:\n");
                write(0, (void *)&data, s);
                close(fd);
        }
        MY_PUTS("\n\n");
        return 0;
}
Personal tools