Porting third-party programs to TempleOS
jwhitham.org
jwhitham.org
I decided that the quickest way to port a C program to TempleOS was to compile it on Linux, but arrange for all of the Linux system calls to be translated to TempleOS system calls.
To do this, I compiled a C program against the embedded C library uClibc, generating a position-independent binary file. uClibc provides all the runtime services that C programs expect, implementing functions like "printf" and "malloc".
I modified uClibc so that each system call would be directed to a particular location within the header of the binary. Each instance of the "syscall" instruction instead became a indirect "jmp" instruction.
Then I wrote a loader program in HolyC which was able to start programs compiled in this special way. The loader's functions are as follows:
1. copy the binary into memory within TempleOS,
2. modify it so that all system calls are directed to a HolyC function,
3. set up the environment and program arguments (argc, argv, etc.),
4. start the program,
5. clean up afterwards e.g. freeing memory.
The loader is a 500 line program written in HolyC and x86_64 assembly code.
This approach enables a C program - and, in principle, any program that can be compiled on Linux - to be executed on TempleOS.