2013-12-15 09:09:20 +01: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;
|
|
|
|
int reserved;
|
|
|
|
void *__idata_start;
|
|
|
|
void *__idata_end;
|
|
|
|
int (*main)(int argc, char **argv, char **envp);
|
|
|
|
};
|
|
|
|
|
2014-11-27 02:02:55 +01:00
|
|
|
void _pei386_runtime_relocator (void);
|
|
|
|
void init_loader(void *libc_image);
|
|
|
|
void init_reent();
|
2013-12-15 09:09:20 +01:00
|
|
|
|
2014-11-27 02:02:55 +01:00
|
|
|
int link_app();
|
2013-12-15 09:09:20 +01:00
|
|
|
void* get_entry_point(void *raw);
|
|
|
|
int (*entry)(int, char **, char **);
|
|
|
|
|
2014-11-27 02:02:55 +01:00
|
|
|
char __appcwd[1024];
|
|
|
|
int __appcwdlen;
|
|
|
|
char* __appenv;
|
|
|
|
int __appenv_size;
|
2013-12-15 09:09:20 +01:00
|
|
|
|
2014-11-27 02:02:55 +01:00
|
|
|
extern char _tls_map[128];
|
2013-12-15 09:09:20 +01:00
|
|
|
|
|
|
|
void __attribute__((noreturn))
|
|
|
|
__thread_startup (int (*entry)(void*), void *param,
|
|
|
|
void *stacklow, void *stackhigh)
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
|
2014-11-27 02:02:55 +01:00
|
|
|
// asm volatile ( "xchgw %bx, %bx");
|
2013-12-15 09:09:20 +01:00
|
|
|
|
|
|
|
__asm__ __volatile__( // save stack limits
|
|
|
|
"movl %0, %%fs:8 \n\t" // use TLS
|
|
|
|
"movl %1, %%fs:12 \n\t"
|
|
|
|
::"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;
|
|
|
|
}
|
|
|
|
|
2014-11-27 02:02:55 +01:00
|
|
|
static int split_cmdline(char *cmdline, char **argv)
|
|
|
|
{
|
|
|
|
enum quote_state
|
|
|
|
{
|
|
|
|
QUOTE_NONE, /* no " active in current parm */
|
|
|
|
QUOTE_DELIMITER, /* " was first char and must be last */
|
|
|
|
QUOTE_STARTED /* " was seen, look for a match */
|
|
|
|
};
|
2013-12-15 09:09:20 +01:00
|
|
|
|
2014-11-27 02:02:55 +01:00
|
|
|
enum quote_state state;
|
|
|
|
unsigned int argc;
|
|
|
|
char *p = cmdline;
|
|
|
|
char *new_arg, *start;
|
2013-12-15 09:09:20 +01:00
|
|
|
|
2014-11-27 02:02:55 +01:00
|
|
|
argc = 0;
|
2013-12-15 09:09:20 +01:00
|
|
|
|
2014-11-27 02:02:55 +01:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
/* skip over spaces and tabs */
|
|
|
|
if ( *p )
|
|
|
|
{
|
|
|
|
while (*p == ' ' || *p == '\t')
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*p == '\0')
|
|
|
|
break;
|
|
|
|
|
|
|
|
state = QUOTE_NONE;
|
|
|
|
if( *p == '\"' )
|
|
|
|
{
|
|
|
|
p++;
|
|
|
|
state = QUOTE_DELIMITER;
|
|
|
|
}
|
|
|
|
new_arg = start = p;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if( *p == '\"' )
|
|
|
|
{
|
|
|
|
p++;
|
|
|
|
if( state == QUOTE_NONE )
|
|
|
|
{
|
|
|
|
state = QUOTE_STARTED;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
state = QUOTE_NONE;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( *p == ' ' || *p == '\t' )
|
|
|
|
{
|
|
|
|
if( state == QUOTE_NONE )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( *p == '\0' )
|
|
|
|
break;
|
|
|
|
|
|
|
|
if( *p == '\\' )
|
|
|
|
{
|
|
|
|
if( p[1] == '\"' )
|
|
|
|
{
|
|
|
|
++p;
|
|
|
|
if( p[-2] == '\\' )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( argv )
|
|
|
|
{
|
|
|
|
*(new_arg++) = *p;
|
|
|
|
}
|
|
|
|
++p;
|
|
|
|
};
|
|
|
|
|
|
|
|
if( argv )
|
|
|
|
{
|
|
|
|
argv[ argc ] = start;
|
|
|
|
++argc;
|
|
|
|
|
|
|
|
/*
|
|
|
|
The *new = '\0' is req'd in case there was a \" to "
|
|
|
|
translation. It must be after the *p check against
|
|
|
|
'\0' because new and p could point to the same char
|
|
|
|
in which case the scan would be terminated too soon.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if( *p == '\0' )
|
|
|
|
{
|
|
|
|
*new_arg = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*new_arg = '\0';
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++argc;
|
|
|
|
if( *p == '\0' )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return argc;
|
|
|
|
};
|
2013-12-15 09:09:20 +01:00
|
|
|
|
|
|
|
void __attribute__((noreturn))
|
|
|
|
libc_crt_startup (void *libc_base)
|
|
|
|
{
|
|
|
|
struct app_hdr *header = NULL;
|
|
|
|
int retval = 0;
|
|
|
|
|
2014-11-27 02:02:55 +01:00
|
|
|
char **argv;
|
|
|
|
int argc;
|
|
|
|
|
2013-12-15 09:09:20 +01:00
|
|
|
_pei386_runtime_relocator();
|
|
|
|
|
|
|
|
memset(_tls_map, 0xFF, 32*4);
|
|
|
|
_tls_map[0] = 0xE0;
|
|
|
|
init_reent();
|
2014-05-11 00:12:19 +02:00
|
|
|
init_stdio();
|
2013-12-15 09:09:20 +01:00
|
|
|
|
|
|
|
// __appenv = load_file("/sys/system.env", &__appenv_size);
|
|
|
|
|
|
|
|
init_loader(libc_base);
|
|
|
|
|
|
|
|
if( link_app() == 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
__appcwdlen = strrchr(header->path, '/') - header->path;
|
|
|
|
__appcwdlen = __appcwdlen > 1022 ? 1022 : __appcwdlen;
|
|
|
|
memcpy(__appcwd, header->path, __appcwdlen);
|
|
|
|
set_cwd(__appcwd);
|
|
|
|
|
|
|
|
if( header->cmdline[0] != 0)
|
|
|
|
{
|
2014-11-27 02:02:55 +01:00
|
|
|
argc = split_cmdline(header->cmdline, NULL) + 1;
|
|
|
|
argv = alloca((argc+1)*sizeof(char*));
|
|
|
|
argv[0] = header->path;
|
2013-12-15 09:09:20 +01:00
|
|
|
|
2014-11-27 02:02:55 +01:00
|
|
|
split_cmdline(header->cmdline, argv + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
argc = 1;
|
|
|
|
argv = alloca((argc+1)*sizeof(char*));
|
|
|
|
argv[0] = header->path;
|
|
|
|
}
|
|
|
|
argv[argc] = NULL;
|
2013-12-15 09:09:20 +01:00
|
|
|
|
2014-11-27 02:02:55 +01:00
|
|
|
retval = header->main(argc, argv, NULL);
|
2013-12-15 09:09:20 +01:00
|
|
|
done:
|
|
|
|
exit (retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
|