2011-03-11 21:52:24 +03:00
|
|
|
|
|
|
|
#include <_ansi.h>
|
|
|
|
#include <reent.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <sys/kos_io.h>
|
|
|
|
|
|
|
|
struct app_hdr
|
|
|
|
{
|
|
|
|
char banner[8];
|
|
|
|
int version;
|
|
|
|
int start;
|
|
|
|
int iend;
|
|
|
|
int memsize;
|
|
|
|
int stacktop;
|
|
|
|
char *cmdline;
|
|
|
|
char *path;
|
2013-07-10 23:26:24 +04:00
|
|
|
int reserved;
|
|
|
|
void *__idata_start;
|
|
|
|
void *__idata_end;
|
|
|
|
void (*main)(int argc, char **argv, char **envp);
|
2011-03-11 21:52:24 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
int _argc;
|
|
|
|
char **_argv;
|
|
|
|
|
|
|
|
|
|
|
|
void* get_entry_point(void *raw);
|
|
|
|
int (*entry)(int, char **, char **);
|
|
|
|
|
2013-07-10 23:26:24 +04:00
|
|
|
void init_loader(void *libc_image);
|
|
|
|
|
2011-03-11 21:52:24 +03:00
|
|
|
|
|
|
|
void init_reent();
|
|
|
|
|
|
|
|
jmp_buf loader_env;
|
|
|
|
|
|
|
|
void __attribute__((noreturn))
|
|
|
|
__thread_startup (int (*entry)(void*), void *param,
|
|
|
|
void *stacklow, void *stackhigh)
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
asm volatile ( "xchgw %bx, %bx");
|
|
|
|
|
|
|
|
__asm__ __volatile__( // save stack limits
|
2013-07-10 23:26:24 +04:00
|
|
|
"movl %0, %%fs:8 \n\t" // use TLS
|
|
|
|
"movl %1, %%fs:12 \n\t"
|
2011-03-11 21:52:24 +03:00
|
|
|
::"r"(stacklow), "r"(stackhigh));
|
|
|
|
|
|
|
|
init_reent(); // initialize thread reentry structure
|
|
|
|
|
|
|
|
retval = entry(param); // call user thread function
|
|
|
|
|
|
|
|
_exit(retval);
|
|
|
|
};
|
|
|
|
|
|
|
|
char * __libc_getenv(const char *name)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-07-10 23:26:24 +04:00
|
|
|
void _pei386_runtime_relocator (void);
|
|
|
|
int link_app();
|
|
|
|
|
2011-03-11 21:52:24 +03:00
|
|
|
char __appcwd[1024];
|
|
|
|
int __appcwdlen;
|
|
|
|
char* __appenv;
|
|
|
|
int __appenv_size;
|
|
|
|
|
2013-07-10 23:26:24 +04:00
|
|
|
static char *arg[2];
|
|
|
|
|
|
|
|
extern char _tls_map[128];
|
|
|
|
|
2011-03-11 21:52:24 +03:00
|
|
|
void __attribute__((noreturn))
|
2013-07-10 23:26:24 +04:00
|
|
|
libc_crt_startup (void *libc_base)
|
2011-03-11 21:52:24 +03:00
|
|
|
{
|
2013-07-10 23:26:24 +04:00
|
|
|
struct app_hdr *header = NULL;
|
2011-03-11 21:52:24 +03:00
|
|
|
|
|
|
|
int len;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
void *my_app;
|
|
|
|
int retval = 0;
|
|
|
|
|
2013-07-10 23:26:24 +04:00
|
|
|
_pei386_runtime_relocator();
|
2011-03-11 21:52:24 +03:00
|
|
|
|
2013-07-10 23:26:24 +04:00
|
|
|
memset(_tls_map, 0xFF, 32*4);
|
|
|
|
_tls_map[0] = 0xE0;
|
2011-03-11 21:52:24 +03:00
|
|
|
init_reent();
|
|
|
|
__initPOSIXHandles();
|
2013-07-10 23:26:24 +04:00
|
|
|
|
2013-07-10 13:35:47 +04:00
|
|
|
// __appenv = load_file("/sys/system.env", &__appenv_size);
|
2011-03-11 21:52:24 +03:00
|
|
|
|
|
|
|
init_loader(libc_base);
|
|
|
|
|
2013-07-10 23:26:24 +04:00
|
|
|
if( link_app() == 0)
|
2011-03-11 21:52:24 +03:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
__appcwdlen = strrchr(header->path, '/') - header->path;
|
|
|
|
__appcwdlen = __appcwdlen > 1022 ? 1022 : __appcwdlen;
|
|
|
|
memcpy(__appcwd, header->path, __appcwdlen);
|
|
|
|
set_cwd(__appcwd);
|
|
|
|
|
|
|
|
arg[0] = header->path;
|
|
|
|
|
2013-07-10 23:26:24 +04:00
|
|
|
if( header->cmdline[0] != 0)
|
2011-03-11 21:52:24 +03:00
|
|
|
{
|
|
|
|
_argc = 2;
|
|
|
|
arg[1] = header->cmdline;
|
2013-07-10 23:26:24 +04:00
|
|
|
}
|
|
|
|
else _argc = 1;
|
2011-03-11 21:52:24 +03:00
|
|
|
|
|
|
|
_argv = arg;
|
|
|
|
|
2013-07-10 23:26:24 +04:00
|
|
|
header->main(_argc, _argv, NULL);
|
2011-03-11 21:52:24 +03:00
|
|
|
done:
|
2013-07-10 23:26:24 +04:00
|
|
|
_exit (retval);
|
2011-03-11 21:52:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|