I've been following the porting Newlib and OS specific toolchain guides. I've got everything needed kernel side, and I wanted to have a go at writing usermode applications although I've hit a bit of a wall.
I've ported newlib and built the OS specific toolchain, both seem to work fine, although whenever I try to call a function from usermode, it page faults. It would page fault as soon as the application starts, but adding 'e main' to the linker options causes it not to crash until I call a C function, although calling things like 'getpid' work fine, and trigger a syscall. It looks a bit like a linking issue of some kind, could anyone point me in the right direction with what's causing this?
The test C program:
Code: Select all
#include <stdlib.h>
#include <unistd.h>
int main()
{
volatile int c = getpid(); // Works fine (with '-e main' as a linker option), triggers a syscall
volatile int *b = malloc(20); //Page faults, without even triggering a syscall
return 0;
}
I'm using the crti and crtn provided by the wiki:
Crti.s:
Code: Select all
/* x86 crti.s */
.section .init
.global _init
.type _init, @function
_init:
push %ebp
movl %esp, %ebp
.section .fini
.global _fini
.type _fini, @function
_fini:
push %ebp
movl %esp, %ebp
Code: Select all
/* x86 crtn.s */
.section .init
popl %ebp
ret
.section .fini
popl %ebp
ret
Code: Select all
i686-fros-gcc -c crtn.s -o crtn.o
Crt0.c:
Code: Select all
#include <fcntl.h>
extern void exit(int code);
extern int main ();
void _start() {
_init_signal();
int ex = main();
exit(ex);
}