From 234f06285076f6a57bfa7f9c1edb40be25dbe681 Mon Sep 17 00:00:00 2001 From: midipix Date: Oct 19 2017 22:27:30 +0000 Subject: _start(): implemented __libc_loader_init() for the ldso and vrfs flavors. --- diff --git a/arch/nt32/peldso.h b/arch/nt32/peldso.h new file mode 100644 index 0000000..bbe3e22 --- /dev/null +++ b/arch/nt32/peldso.h @@ -0,0 +1,52 @@ +#ifndef _PELDSO_H_ +#define _PELDSO_H_ + +/* ldso flags (pemagine)*/ +#define PE_LDSO_INTEGRAL_ONLY 0x00000000 +#define PE_LDSO_DEFAULT_EXECUTABLE 0x00000001 +#define PE_LDSO_STANDALONE_EXECUTABLE 0x00000002 + +/* error status (ntapi) */ +#define NT_STATUS_NOINTERFACE 0xC00002B9 + +/* rtdata guid (ntapi) */ +#define NT_PROCESS_GUID_RTDATA {0x3e43ec84,0x1af1,0x4ede,{0xac,0xd8,0xc3,0xd9,0x20,0xaf,0xc8,0x68}} + +/* abi guid */ +struct __guid { + unsigned int data1; + unsigned short data2; + unsigned short data3; + unsigned char data4[8]; +}; + +/* loader interfaces, statically linked (libldso.a) */ +__attribute__((__visibility__("hidden"))) int __ldso_terminate_current_process( + int estatus); + +__attribute__((__visibility__("hidden"))) void * __ldso_get_procedure_address( + const void * base, + const char * name); + +__attribute__((__visibility__("hidden"))) int __ldso_load_framework_loader_ex( + void ** baseaddr, + void ** hroot, + void ** hdsodir, + const struct __guid * abi, + const unsigned short * basename, + const unsigned short * rrelname, + void * refaddr, + unsigned long * buffer, + unsigned int bufsize, + unsigned int flags, + unsigned int * sysflags); + +__attribute__((__visibility__("hidden"))) int __ldso_load_framework_library( + void ** baseaddr, + void * hat, + const unsigned short * atrelname, + unsigned long * buffer, + unsigned int bufsize, + unsigned int * sysflags); + +#endif diff --git a/arch/nt64/peldso.h b/arch/nt64/peldso.h new file mode 100644 index 0000000..bbe3e22 --- /dev/null +++ b/arch/nt64/peldso.h @@ -0,0 +1,52 @@ +#ifndef _PELDSO_H_ +#define _PELDSO_H_ + +/* ldso flags (pemagine)*/ +#define PE_LDSO_INTEGRAL_ONLY 0x00000000 +#define PE_LDSO_DEFAULT_EXECUTABLE 0x00000001 +#define PE_LDSO_STANDALONE_EXECUTABLE 0x00000002 + +/* error status (ntapi) */ +#define NT_STATUS_NOINTERFACE 0xC00002B9 + +/* rtdata guid (ntapi) */ +#define NT_PROCESS_GUID_RTDATA {0x3e43ec84,0x1af1,0x4ede,{0xac,0xd8,0xc3,0xd9,0x20,0xaf,0xc8,0x68}} + +/* abi guid */ +struct __guid { + unsigned int data1; + unsigned short data2; + unsigned short data3; + unsigned char data4[8]; +}; + +/* loader interfaces, statically linked (libldso.a) */ +__attribute__((__visibility__("hidden"))) int __ldso_terminate_current_process( + int estatus); + +__attribute__((__visibility__("hidden"))) void * __ldso_get_procedure_address( + const void * base, + const char * name); + +__attribute__((__visibility__("hidden"))) int __ldso_load_framework_loader_ex( + void ** baseaddr, + void ** hroot, + void ** hdsodir, + const struct __guid * abi, + const unsigned short * basename, + const unsigned short * rrelname, + void * refaddr, + unsigned long * buffer, + unsigned int bufsize, + unsigned int flags, + unsigned int * sysflags); + +__attribute__((__visibility__("hidden"))) int __ldso_load_framework_library( + void ** baseaddr, + void * hat, + const unsigned short * atrelname, + unsigned long * buffer, + unsigned int bufsize, + unsigned int * sysflags); + +#endif diff --git a/crt/nt32/crtldso.c b/crt/nt32/crtldso.c index c3130b8..bdc376e 100644 --- a/crt/nt32/crtldso.c +++ b/crt/nt32/crtldso.c @@ -1,3 +1,57 @@ #include "psxglue.h" +#include "peldso.h" const int __crtopt_ldso = __PSXOPT_LDSO; + +/* framework (rtdata) abi */ +static const struct __guid __ldsoabi = NT_PROCESS_GUID_RTDATA; + +/* loader root-relative name */ +static const unsigned short __rrldso[] = {'l','i','b','\\', + 'l','i','b','p','s','x','s','c','l', + '.','s','o',0}; + +/* loader root-relative name */ +static const unsigned short __rrlibc[] = {'l','i','b','\\', + 'l','i','b','c', + '.','s','o',0}; + + +static unsigned long __attribute__((section(".dsodata"))) + __dsodata[65536/sizeof(unsigned long)]; + +void __libc_loader_init(void * __main, int flags) +{ + int status; + void * hroot; + void * hdsodir; + void * ldsobase; + void * libcbase; + int (*__psx_init)(int *,char ***,char ***,void *); + void (*__libc_entry_routine)(void *,void *,int); + + if ((status = __ldso_load_framework_loader_ex( + &ldsobase,&hroot,&hdsodir, + &__ldsoabi, + 0,__rrldso,__main, + __dsodata,sizeof(__dsodata), + PE_LDSO_DEFAULT_EXECUTABLE, + &(unsigned int){0}))) + __ldso_terminate_current_process(status); + + if ((status = __ldso_load_framework_library( + &libcbase,hroot,__rrlibc, + __dsodata,sizeof(__dsodata), + &(unsigned int){0}))) + __ldso_terminate_current_process(status); + + if (!(__psx_init = __ldso_get_procedure_address( + ldsobase,"__psx_init"))) + __ldso_terminate_current_process(NT_STATUS_NOINTERFACE); + + if (!(__libc_entry_routine = __ldso_get_procedure_address( + libcbase,"__libc_entry_routine"))) + __ldso_terminate_current_process(NT_STATUS_NOINTERFACE); + + __libc_entry_routine(__main,__psx_init,flags); +} diff --git a/crt/nt32/crtvrfs.c b/crt/nt32/crtvrfs.c index d22d444..3608ab8 100644 --- a/crt/nt32/crtvrfs.c +++ b/crt/nt32/crtvrfs.c @@ -1,3 +1,55 @@ #include "psxglue.h" +#include "peldso.h" const int __crtopt_vrfs = __PSXOPT_VRFS; + +/* framework (rtdata) abi */ +static const struct __guid __ldsoabi = NT_PROCESS_GUID_RTDATA; + +/* loader root-relative name */ +static const unsigned short __sdldso[] = {'l','i','b','p','s','x','s','c','l', + '.','s','o',0}; + +/* loader root-relative name */ +static const unsigned short __sdlibc[] = {'l','i','b','c', + '.','s','o',0}; + + +static unsigned long __attribute__((section(".dsodata"))) + __dsodata[65536/sizeof(unsigned long)]; + +void __libc_loader_init(void * __main, int flags) +{ + int status; + void * hroot; + void * hdsodir; + void * ldsobase; + void * libcbase; + int (*__psx_init)(int *,char ***,char ***,void *); + void (*__libc_entry_routine)(void *,void *,int); + + if ((status = __ldso_load_framework_loader_ex( + &ldsobase,&hroot,&hdsodir, + &__ldsoabi, + __sdldso,0,__main, + __dsodata,sizeof(__dsodata), + PE_LDSO_STANDALONE_EXECUTABLE, + &(unsigned int){0}))) + __ldso_terminate_current_process(status); + + if ((status = __ldso_load_framework_library( + &libcbase,hdsodir,__sdlibc, + __dsodata,sizeof(__dsodata), + &(unsigned int){0}))) + __ldso_terminate_current_process(status); + + if (!(__psx_init = __ldso_get_procedure_address( + ldsobase,"__psx_init"))) + __ldso_terminate_current_process(NT_STATUS_NOINTERFACE); + + if (!(__libc_entry_routine = __ldso_get_procedure_address( + libcbase,"__libc_entry_routine"))) + __ldso_terminate_current_process(NT_STATUS_NOINTERFACE); + + __libc_entry_routine(__main,__psx_init,flags); +} diff --git a/crt/nt64/crtldso.c b/crt/nt64/crtldso.c index c3130b8..bdc376e 100644 --- a/crt/nt64/crtldso.c +++ b/crt/nt64/crtldso.c @@ -1,3 +1,57 @@ #include "psxglue.h" +#include "peldso.h" const int __crtopt_ldso = __PSXOPT_LDSO; + +/* framework (rtdata) abi */ +static const struct __guid __ldsoabi = NT_PROCESS_GUID_RTDATA; + +/* loader root-relative name */ +static const unsigned short __rrldso[] = {'l','i','b','\\', + 'l','i','b','p','s','x','s','c','l', + '.','s','o',0}; + +/* loader root-relative name */ +static const unsigned short __rrlibc[] = {'l','i','b','\\', + 'l','i','b','c', + '.','s','o',0}; + + +static unsigned long __attribute__((section(".dsodata"))) + __dsodata[65536/sizeof(unsigned long)]; + +void __libc_loader_init(void * __main, int flags) +{ + int status; + void * hroot; + void * hdsodir; + void * ldsobase; + void * libcbase; + int (*__psx_init)(int *,char ***,char ***,void *); + void (*__libc_entry_routine)(void *,void *,int); + + if ((status = __ldso_load_framework_loader_ex( + &ldsobase,&hroot,&hdsodir, + &__ldsoabi, + 0,__rrldso,__main, + __dsodata,sizeof(__dsodata), + PE_LDSO_DEFAULT_EXECUTABLE, + &(unsigned int){0}))) + __ldso_terminate_current_process(status); + + if ((status = __ldso_load_framework_library( + &libcbase,hroot,__rrlibc, + __dsodata,sizeof(__dsodata), + &(unsigned int){0}))) + __ldso_terminate_current_process(status); + + if (!(__psx_init = __ldso_get_procedure_address( + ldsobase,"__psx_init"))) + __ldso_terminate_current_process(NT_STATUS_NOINTERFACE); + + if (!(__libc_entry_routine = __ldso_get_procedure_address( + libcbase,"__libc_entry_routine"))) + __ldso_terminate_current_process(NT_STATUS_NOINTERFACE); + + __libc_entry_routine(__main,__psx_init,flags); +} diff --git a/crt/nt64/crtvrfs.c b/crt/nt64/crtvrfs.c index d22d444..3608ab8 100644 --- a/crt/nt64/crtvrfs.c +++ b/crt/nt64/crtvrfs.c @@ -1,3 +1,55 @@ #include "psxglue.h" +#include "peldso.h" const int __crtopt_vrfs = __PSXOPT_VRFS; + +/* framework (rtdata) abi */ +static const struct __guid __ldsoabi = NT_PROCESS_GUID_RTDATA; + +/* loader root-relative name */ +static const unsigned short __sdldso[] = {'l','i','b','p','s','x','s','c','l', + '.','s','o',0}; + +/* loader root-relative name */ +static const unsigned short __sdlibc[] = {'l','i','b','c', + '.','s','o',0}; + + +static unsigned long __attribute__((section(".dsodata"))) + __dsodata[65536/sizeof(unsigned long)]; + +void __libc_loader_init(void * __main, int flags) +{ + int status; + void * hroot; + void * hdsodir; + void * ldsobase; + void * libcbase; + int (*__psx_init)(int *,char ***,char ***,void *); + void (*__libc_entry_routine)(void *,void *,int); + + if ((status = __ldso_load_framework_loader_ex( + &ldsobase,&hroot,&hdsodir, + &__ldsoabi, + __sdldso,0,__main, + __dsodata,sizeof(__dsodata), + PE_LDSO_STANDALONE_EXECUTABLE, + &(unsigned int){0}))) + __ldso_terminate_current_process(status); + + if ((status = __ldso_load_framework_library( + &libcbase,hdsodir,__sdlibc, + __dsodata,sizeof(__dsodata), + &(unsigned int){0}))) + __ldso_terminate_current_process(status); + + if (!(__psx_init = __ldso_get_procedure_address( + ldsobase,"__psx_init"))) + __ldso_terminate_current_process(NT_STATUS_NOINTERFACE); + + if (!(__libc_entry_routine = __ldso_get_procedure_address( + libcbase,"__libc_entry_routine"))) + __ldso_terminate_current_process(NT_STATUS_NOINTERFACE); + + __libc_entry_routine(__main,__psx_init,flags); +}