diff options
author | Nicholas Noll <nbnoll@eml.cc> | 2021-11-20 09:53:11 -0800 |
---|---|---|
committer | Nicholas Noll <nbnoll@eml.cc> | 2021-11-20 09:53:11 -0800 |
commit | 1c8d4e69205fd875f6bec3fa3bd929c2e7f52f62 (patch) | |
tree | 8bb6233e8f59d574e99a55a8c9aaf4d90396a2ce /sys/rt/boot.c | |
parent | 59d87ccc99f431da06a0ffab0d3e702d9ef82f48 (diff) |
Feature: self hosting prototype implemented
This is a large change. In order to remove myself from libc's arcane
interface, I implemented an independent runtime layer. It is based on
musl's wonderful implementation mostly. Critically, if libc is linked to
the program, then we cooperate. Namely, we call start main and let libc
do all initialization. If not, then we have a noop defined in rt3.a.
The general structure of the file is:
1. sys/$os/$arch contains all architecture dependent code
2. sys/$os/port contains all code that depends on the os, but is
portable
3. rt/$arch contains all the runtime architecture dependent code
4. rt/* contains the portable runtime code.
Obviously testing is needed. Specifically, while code is checked in for
the most popular architectures, it only has been tested on one computer!
Overall this is exciting and as been educational.
Diffstat (limited to 'sys/rt/boot.c')
-rw-r--r-- | sys/rt/boot.c | 57 |
1 files changed, 32 insertions, 25 deletions
diff --git a/sys/rt/boot.c b/sys/rt/boot.c index 197e861..76f5599 100644 --- a/sys/rt/boot.c +++ b/sys/rt/boot.c @@ -1,34 +1,41 @@ +#include <u.h> #include <rt.h> +#include <elf.h> -int -rt·boot( - int (*main)(int,char **,char **), int argc, char **argv, - int (*init)(int, char **, char **), void (*fini)(void), - void (*exit)(void) -) +/* tell linker to go find */ +int __libc_start_main(int (*main)(int,char **,char **), int argc, char **argv); + +#define NAUX 38 +static void +rt·init(char **env, char *program) { - char **envp = argv+argc+1; - rt·environ = envp; + uintptr i, *auxv, aux[NAUX]; + for(i=0; env[i]; i++) + ; + rt·context.auxv = auxv = (void *)(env+i+1); + for(i=0; auxv[i]; i+=2) + if(auxv[i]<NAUX) + aux[auxv[i]] = auxv[i+1]; - if(exit) - rt·context.exit = exit; + rt·context.sysinfo = aux[AT_SYSINFO]; + rt·context.pagesize = aux[AT_PAGESZ]; +} - if(fini) - rt·context.fini = fini; +int +rt·boot(int (*main)(), int argc, char **argv, void (*init)(), void (*fini)(), void (*exit)()) +{ + char **env = argv+argc+1; + rt·init(env, argv[0]); - if(init){ - rt·context.init = init; - init(argc, argv, envp); - } + /* ring libc, anyone home? */ + __libc_start_main(main, argc, argv); - /* XXX: - * we could call __libc_start_main of musl here: - * this would give us the normal C runtime along with ours - * which would allow seamless linking to other libraries - * or we can implement a compatibility layer - */ - rt·exit(main(argc, argv, envp)); + /* no? ok we continue on if there is no libc linked */ + init(); + if(fini) + rt·context.fini = fini; + if(exit) + rt·context.exit = exit; - /* should never get here */ - return 0; + rt·exit(main(argc, argv, env)); } |