From 3d6a345b39afa454f0f33fc8d48b96229971e6ed Mon Sep 17 00:00:00 2001 From: John Hodge Date: Thu, 24 Mar 2011 10:28:28 +0800 Subject: [PATCH] Usermode/ld-acess - Reworked build structure and syscall files - Fixed up many places that used Uint in place of a pointer - Converted the *.asm files into common and architecture-dependent parts > Some hackery with $(CPP) was used :) --- Usermode/Libraries/ld-acess.so_src/Makefile | 15 +- .../ld-acess.so_src/arch/syscalls.s.h | 52 ++ .../Libraries/ld-acess.so_src/arch/x86.asm.h | 191 +++++ Usermode/Libraries/ld-acess.so_src/common.h | 20 +- Usermode/Libraries/ld-acess.so_src/core.asm | 111 --- Usermode/Libraries/ld-acess.so_src/elf.c | 785 +++++++++--------- Usermode/Libraries/ld-acess.so_src/export.c | 4 +- Usermode/Libraries/ld-acess.so_src/loadlib.c | 40 +- Usermode/Libraries/ld-acess.so_src/main.c | 45 +- Usermode/Libraries/ld-acess.so_src/mm.asm | 11 - Usermode/Libraries/ld-acess.so_src/pe.c | 33 +- .../ld-acess.so_src/syscalls.inc.asm | 141 ---- Usermode/Libraries/ld-acess.so_src/vfs.asm | 25 - 13 files changed, 727 insertions(+), 746 deletions(-) create mode 100644 Usermode/Libraries/ld-acess.so_src/arch/syscalls.s.h create mode 100644 Usermode/Libraries/ld-acess.so_src/arch/x86.asm.h delete mode 100644 Usermode/Libraries/ld-acess.so_src/core.asm delete mode 100644 Usermode/Libraries/ld-acess.so_src/mm.asm delete mode 100644 Usermode/Libraries/ld-acess.so_src/syscalls.inc.asm delete mode 100644 Usermode/Libraries/ld-acess.so_src/vfs.asm diff --git a/Usermode/Libraries/ld-acess.so_src/Makefile b/Usermode/Libraries/ld-acess.so_src/Makefile index 668460a0..64a120c2 100644 --- a/Usermode/Libraries/ld-acess.so_src/Makefile +++ b/Usermode/Libraries/ld-acess.so_src/Makefile @@ -5,7 +5,7 @@ -include ../Makefile.cfg OBJ := main.o lib.o loadlib.o export.o elf.o pe.o -OBJ += core.ao vfs.ao mm.ao +OBJ += arch/$(ARCHDIR).ao BIN = ld-acess.so EXTRABIN := libld-acess.so @@ -16,7 +16,20 @@ LDFLAGS = -T link.ld -Map map.txt --export-dynamic include ../Makefile.tpl +# create libld-acess.so $(_XBIN): $(_BIN) @echo [LD] -o -shared libld-acess.so @$(LD) $(LDFLAGS) -shared -o $@ $(OBJ) +# Override .ao to look in the object prefix for the source +%.ao: %.asm + @echo [AS] -o $@ + @mkdir -p $(dir $@) + @$(AS) $(ASFLAGS) -o $@ $< + +# Preprocessing objects if needed +$(_OBJPREFIX)%: %.h + @echo [CPP] -o $@ + @mkdir -p $(dir $@) + @$(CPP) $(CPPFLAGS) -P -D_ASM_ $< -o $@ + diff --git a/Usermode/Libraries/ld-acess.so_src/arch/syscalls.s.h b/Usermode/Libraries/ld-acess.so_src/arch/syscalls.s.h new file mode 100644 index 00000000..1c7a5bb0 --- /dev/null +++ b/Usermode/Libraries/ld-acess.so_src/arch/syscalls.s.h @@ -0,0 +1,52 @@ +// --- Process Control --- +SYSCALL1(_exit, SYS_EXIT) + +SYSCALL2(clone, SYS_CLONE) +SYSCALL2(kill, SYS_KILL) +SYSCALL0(yield, SYS_YIELD) +SYSCALL0(sleep, SYS_SLEEP) +SYSCALL2(waittid, SYS_WAITTID) + +SYSCALL0(gettid, SYS_GETTID) +SYSCALL0(getpid, SYS_GETPID) +SYSCALL0(getuid, SYS_GETUID) +SYSCALL0(getgid, SYS_GETGID) + +SYSCALL1(setuid, SYS_SETUID) +SYSCALL1(setgid, SYS_SETGID) + +SYSCALL1(SysSetName, SYS_SETNAME) +SYSCALL2(SysGetName, SYS_GETNAME) + +SYSCALL1(SysSetPri, SYS_SETPRI) + +SYSCALL3(SysSendMessage, SYS_SENDMSG) +SYSCALL3(SysGetMessage, SYS_GETMSG) + +SYSCALL3(SysSpawn, SYS_SPAWN) +SYSCALL3(execve, SYS_EXECVE) +SYSCALL2(SysLoadBin, SYS_LOADBIN) +SYSCALL1(SysUnloadBin, SYS_UNLOADBIN) + +SYSCALL1(_SysSetFaultHandler, SYS_SETFAULTHANDLER) + +SYSCALL6(_SysDebug, 0x100) +SYSCALL1(_SysGetPhys, SYS_GETPHYS) // uint64_t _SysGetPhys(uint addr) +SYSCALL1(_SysAllocate, SYS_ALLOCATE) // uint64_t _SysAllocate(uint addr) +// VFS System calls +SYSCALL2(open, SYS_OPEN) // char*, int +SYSCALL3(reopen, SYS_REOPEN) // int, char*, int +SYSCALL1(close, SYS_CLOSE) // int +SYSCALL3(read, SYS_READ) // int, uint, void* +SYSCALL3(write, SYS_WRITE) // int, uint, void* +SYSCALL4(seek, SYS_SEEK) // int, uint64_t, int +SYSCALL1(tell, SYS_TELL) // int +SYSCALL3(finfo, SYS_FINFO) // int, void*, int +SYSCALL2(readdir, SYS_READDIR) // int, char* +SYSCALL2(_SysGetACL,SYS_GETACL) // int, void* +SYSCALL1(chdir, SYS_CHDIR) // char* +SYSCALL3(ioctl, SYS_IOCTL) // int, int, void* +SYSCALL4(_SysMount, SYS_MOUNT) // char*, char*, char*, char* +SYSCALL5(select, SYS_SELECT) // int, fd_set*, fd_set*, fd_set*, tTime* + +SYSCALL3(_SysOpenChild, SYS_OPENCHILD) diff --git a/Usermode/Libraries/ld-acess.so_src/arch/x86.asm.h b/Usermode/Libraries/ld-acess.so_src/arch/x86.asm.h new file mode 100644 index 00000000..282ff40e --- /dev/null +++ b/Usermode/Libraries/ld-acess.so_src/arch/x86.asm.h @@ -0,0 +1,191 @@ +; ======================== +; AcssMicro - System Calls +; ======================== + +%include "../../../Kernel/include/syscalls.inc.asm" + +[bits 32] +[section .data] +_errno: dw 0 + +[section .text] +; DEST +; SRC +_memcpy: + push ebp + mov ebp, esp + push edi + push esi ; // DI and SI must be maintained, CX doesn't + + mov ecx, [ebp+16] + mov esi, [ebp+12] + mov edi, [ebp+8] + rep movsb + + pop esi + pop edi + pop ebp + ret + +#define SYSCALL0(_name,_num) SYSCALL0 _name, _num +#define SYSCALL1(_name,_num) SYSCALL1 _name, _num +#define SYSCALL2(_name,_num) SYSCALL2 _name, _num +#define SYSCALL3(_name,_num) SYSCALL3 _name, _num +#define SYSCALL4(_name,_num) SYSCALL4 _name, _num +#define SYSCALL5(_name,_num) SYSCALL5 _name, _num +#define SYSCALL6(_name,_num) SYSCALL6 _name, _num + +;%define SYSCALL_OP jmp 0xCFFF0000 +%define SYSCALL_OP int 0xAC + +; System Call - No Arguments +%macro SYSCALL0 2 +[global %1:func] +%1: + push ebx + mov eax, %2 + SYSCALL_OP + mov [_errno], ebx + pop ebx + ret +%endmacro + +%macro _SYSCALL_HEAD 2 +[global %1:func] +%1: + push ebp + mov ebp, esp + push ebx + mov eax, %2 +%endmacro +%macro _SYSCALL_TAIL 0 + mov [_errno], ebx + pop ebx + pop ebp + ret +%endmacro + +; System Call - 1 Argument +%macro SYSCALL1 2 +_SYSCALL_HEAD %1, %2 + mov ebx, [ebp+8] + SYSCALL_OP +_SYSCALL_TAIL +%endmacro + +; System Call - 2 Arguments +%macro SYSCALL2 2 +_SYSCALL_HEAD %1, %2 + mov eax, %2 + mov ebx, [ebp+8] + mov ecx, [ebp+12] + SYSCALL_OP +_SYSCALL_TAIL +%endmacro + +; System Call - 3 Arguments +%macro SYSCALL3 2 +_SYSCALL_HEAD %1, %2 + mov ebx, [ebp+8] + mov ecx, [ebp+12] + mov edx, [ebp+16] + SYSCALL_OP +_SYSCALL_TAIL +%endmacro + +; System Call - 4 Arguments +%macro SYSCALL4 2 +_SYSCALL_HEAD %1, %2 + push edi + mov ebx, [ebp+8] + mov ecx, [ebp+12] + mov edx, [ebp+16] + mov edi, [ebp+20] + SYSCALL_OP + pop edi +_SYSCALL_TAIL +%endmacro + +; System Call - 5 Arguments +%macro SYSCALL5 2 +_SYSCALL_HEAD %1, %2 + push edi + push esi + mov ebx, [ebp+8] + mov ecx, [ebp+12] + mov edx, [ebp+16] + mov edi, [ebp+20] + mov esi, [ebp+24] + SYSCALL_OP + pop esi + pop edi +_SYSCALL_TAIL +%endmacro + +; System Call - 6 Arguments +%macro SYSCALL6 2 +_SYSCALL_HEAD %1, %2 + push edi + push esi + mov ebx, [ebp+8] + mov ecx, [ebp+12] + mov edx, [ebp+16] + mov edi, [ebp+20] + mov esi, [ebp+24] + mov ebp, [ebp+28] + SYSCALL_OP + pop esi + pop edi +_SYSCALL_TAIL +%endmacro + +; // Override the clone syscall +#define clone _clone_raw +#include "syscalls.s.h" +#undef clone + +[global clone:func] +clone: + push ebp + mov ebp, esp + push ebx + + mov ebx, [ebp+12] ; Get new stack pointer + + ; Check if the new stack is being used + test ebx, ebx + jz .doCall + ; Modify it to include the calling function (and this) + %if 0 + mov eax, [ebp] ; Get old stack frame + sub eax, ebp ; Get size + sub ebx, eax ; Alter new stack pointer + push eax ; < Size + push DWORD [ebp] ; < Source + push ebx ; < Dest + call _memcpy + add esp, 4*3 ; Restore stack + ; EBX should still be the new stack pointer + mov eax, [ebp] ; Save old stack frame pointer in new stack + mov [ebx-4], eax + mov eax, [ebp-4] ; Save EBX there too + mov [ebx-8], eax + sub ebx, 8 ; Update stack pointer for system + %else + ; Quick hack, just this stack frame + mov eax, [ebp+4] + mov [ebx-4], eax ; Return + mov [ebx-8], ebx ; EBP + mov DWORD [ebx-12], 0 ; EBX + sub ebx, 12 + %endif +.doCall: + mov eax, SYS_CLONE + mov ecx, ebx ; Stack + mov ebx, [ebp+8] ; Flags + SYSCALL_OP + mov [_errno], ebx + pop ebx + pop ebp + ret + diff --git a/Usermode/Libraries/ld-acess.so_src/common.h b/Usermode/Libraries/ld-acess.so_src/common.h index fad0344a..3eef946c 100644 --- a/Usermode/Libraries/ld-acess.so_src/common.h +++ b/Usermode/Libraries/ld-acess.so_src/common.h @@ -29,7 +29,7 @@ typedef signed short Sint16; typedef signed long Sint32; typedef struct { - Uint Base; + void *Base; char *Name; } tLoadedLib; @@ -37,13 +37,13 @@ typedef struct { extern tLoadedLib gLoadedLibraries[MAX_LOADED_LIBRARIES]; // === Main === -extern int DoRelocate( Uint base, char **envp, char *Filename ); +extern void *DoRelocate(void *Base, char **envp, char *Filename); // === Library/Symbol Manipulation == -extern Uint LoadLibrary(char *filename, char *SearchDir, char **envp); -extern void AddLoaded(char *File, Uint base); -extern Uint GetSymbol(char *name); -extern int GetSymbolFromBase(Uint base, char *name, Uint *ret); +extern void *LoadLibrary(char *filename, char *SearchDir, char **envp); +extern void AddLoaded(char *File, void *base); +extern void *GetSymbol(char *name); +extern int GetSymbolFromBase(void *base, char *name, void **ret); // === Library Functions === extern char *strcpy(char *dest, const char *src); @@ -56,16 +56,16 @@ extern int file_exists(char *filename); extern void _exit(int retval); extern void SysDebug(const char *fmt, ...); //!< Now implemented in main.c extern void SysDebugV(const char *fmt, ...); -extern Uint SysLoadBin(const char *path, Uint *entry); -extern Uint SysUnloadBin(Uint Base); +extern void *SysLoadBin(const char *path, void **entry); +extern int SysUnloadBin(void *Base); extern void SysSetFaultHandler(int (*Hanlder)(int)); extern int open(const char *filename, int flags); extern void close(int fd); // === ELF Loader === -extern int ElfGetSymbol(Uint Base, char *name, Uint *ret); +extern int ElfGetSymbol(void *Base, char *name, void **ret); // === PE Loader === -extern int PE_GetSymbol(Uint Base, char *Name, Uint *ret); +extern int PE_GetSymbol(void *Base, char *Name, void **ret); #endif diff --git a/Usermode/Libraries/ld-acess.so_src/core.asm b/Usermode/Libraries/ld-acess.so_src/core.asm deleted file mode 100644 index 0bf80c12..00000000 --- a/Usermode/Libraries/ld-acess.so_src/core.asm +++ /dev/null @@ -1,111 +0,0 @@ -; -; Acess2 System Interface -; -%include "syscalls.inc.asm" - -[BITS 32] -[section .data] -[global _errno:data (4)] -_errno: - dd 0 - -[section .text] -; DEST -; SRC -_memcpy: - push ebp - mov ebp, esp - push edi - push esi ; DI and SI must be maintained, CX doesn't - - mov ecx, [ebp+16] - mov esi, [ebp+12] - mov edi, [ebp+8] - rep movsb - - pop esi - pop edi - pop ebp - ret - -; --- Process Control --- -SYSCALL1 _exit, SYS_EXIT - -%if 0 -SYSCALL2 clone, SYS_CLONE -%else -[global clone:func] -clone: - push ebp - mov ebp, esp - push ebx - - mov ebx, [ebp+12] ; Get new stack pointer - - ; Check if the new stack is being used - test ebx, ebx - jz .doCall - ; Modify it to include the calling function (and this) - %if 0 - mov eax, [ebp] ; Get old stack frame - sub eax, ebp ; Get size - sub ebx, eax ; Alter new stack pointer - push eax ; < Size - push DWORD [ebp] ; < Source - push ebx ; < Dest - call _memcpy - add esp, 4*3 ; Restore stack - ; EBX should still be the new stack pointer - mov eax, [ebp] ; Save old stack frame pointer in new stack - mov [ebx-4], eax - mov eax, [ebp-4] ; Save EBX there too - mov [ebx-8], eax - sub ebx, 8 ; Update stack pointer for system - %else - ; Quick hack, just this stack frame - mov eax, [ebp+4] - mov [ebx-4], eax ; Return - mov [ebx-8], ebx ; EBP - mov DWORD [ebx-12], 0 ; EBX - sub ebx, 12 - %endif -.doCall: - mov eax, SYS_CLONE - mov ecx, ebx ; Stack - mov ebx, [ebp+8] ; Flags - SYSCALL_OP - mov [_errno], ebx - pop ebx - pop ebp - ret -%endif - -SYSCALL2 kill, SYS_KILL -SYSCALL0 yield, SYS_YIELD -SYSCALL0 sleep, SYS_SLEEP -SYSCALL2 waittid, SYS_WAITTID - -SYSCALL0 gettid, SYS_GETTID -SYSCALL0 getpid, SYS_GETPID -SYSCALL0 getuid, SYS_GETUID -SYSCALL0 getgid, SYS_GETGID - -SYSCALL1 setuid, SYS_SETUID -SYSCALL1 setgid, SYS_SETGID - -SYSCALL1 SysSetName, SYS_SETNAME -SYSCALL2 SysGetName, SYS_GETNAME - -SYSCALL1 SysSetPri, SYS_SETPRI - -SYSCALL3 SysSendMessage, SYS_SENDMSG -SYSCALL3 SysGetMessage, SYS_GETMSG - -SYSCALL3 SysSpawn, SYS_SPAWN -SYSCALL3 execve, SYS_EXECVE -SYSCALL2 SysLoadBin, SYS_LOADBIN -SYSCALL1 SysUnloadBin, SYS_UNLOADBIN - -SYSCALL1 _SysSetFaultHandler, SYS_SETFAULTHANDLER - -SYSCALL6 _SysDebug, 0x100 diff --git a/Usermode/Libraries/ld-acess.so_src/elf.c b/Usermode/Libraries/ld-acess.so_src/elf.c index dcde746d..bcec1dc6 100644 --- a/Usermode/Libraries/ld-acess.so_src/elf.c +++ b/Usermode/Libraries/ld-acess.so_src/elf.c @@ -1,425 +1,426 @@ -/* - * AcessOS 1 - Dynamic Loader - * By thePowersGang - */ -#include "common.h" -#include "elf32.h" +/* + * AcessOS 1 - Dynamic Loader + * By thePowersGang + */ +#include "common.h" +#include +#include "elf32.h" #define DEBUG 0 -#if DEBUG -# define DEBUGS(v...) SysDebug(v) +#if DEBUG +# define DEBUGS(v...) SysDebug(v) #else # define DEBUGS(...) #endif - + // === CONSTANTS === -#if DEBUG -//static const char *csaDT_NAMES[] = {"DT_NULL", "DT_NEEDED", "DT_PLTRELSZ", "DT_PLTGOT", "DT_HASH", "DT_STRTAB", "DT_SYMTAB", "DT_RELA", "DT_RELASZ", "DT_RELAENT", "DT_STRSZ", "DT_SYMENT", "DT_INIT", "DT_FINI", "DT_SONAME", "DT_RPATH", "DT_SYMBOLIC", "DT_REL", "DT_RELSZ", "DT_RELENT", "DT_PLTREL", "DT_DEBUG", "DT_TEXTREL", "DT_JMPREL"}; +#if DEBUG +//static const char *csaDT_NAMES[] = {"DT_NULL", "DT_NEEDED", "DT_PLTRELSZ", "DT_PLTGOT", "DT_HASH", "DT_STRTAB", "DT_SYMTAB", "DT_RELA", "DT_RELASZ", "DT_RELAENT", "DT_STRSZ", "DT_SYMENT", "DT_INIT", "DT_FINI", "DT_SONAME", "DT_RPATH", "DT_SYMBOLIC", "DT_REL", "DT_RELSZ", "DT_RELENT", "DT_PLTREL", "DT_DEBUG", "DT_TEXTREL", "DT_JMPREL"}; static const char *csaR_NAMES[] = {"R_386_NONE", "R_386_32", "R_386_PC32", "R_386_GOT32", "R_386_PLT32", "R_386_COPY", "R_386_GLOB_DAT", "R_386_JMP_SLOT", "R_386_RELATIVE", "R_386_GOTOFF", "R_386_GOTPC", "R_386_LAST"}; -#endif - -// === PROTOTYPES === -void elf_doRelocate(Uint r_info, Uint32 *ptr, Uint32 addend, Elf32_Sym *symtab, Uint base); -Uint ElfHashString(char *name); - -// === CODE === -/** - \fn int ElfRelocate(void *Base, char **envp, char *Filename) - \brief Relocates a loaded ELF Executable -*/ -int ElfRelocate(void *Base, char **envp, char *Filename) -{ - Elf32_Ehdr *hdr = Base; - Elf32_Phdr *phtab; - int i, j; // Counters - char *libPath; - Uint iRealBase = -1; - Uint iBaseDiff; - int iSegmentCount; - int iSymCount; - Elf32_Rel *rel = NULL; - Elf32_Rela *rela = NULL; - Uint32 *pltgot = NULL; - void *plt = NULL; - int relSz=0, relEntSz=8; - int relaSz=0, relaEntSz=8; - int pltSz=0, pltType=0; - Elf32_Dyn *dynamicTab = NULL; // Dynamic Table Pointer - char *dynstrtab = NULL; // .dynamic String Table - Elf32_Sym *dynsymtab; - - DEBUGS("ElfRelocate: (Base=0x%x)\n", Base); - - // Check magic header - - - // Parse Program Header to get Dynamic Table - phtab = Base + hdr->phoff; - iSegmentCount = hdr->phentcount; - for(i=0;i phtab[i].VAddr) - iRealBase = phtab[i].VAddr; - - // Find Dynamic Section - if(phtab[i].Type == PT_DYNAMIC) { - if(dynamicTab) { - DEBUGS(" WARNING - elf_relocate: Multiple PT_DYNAMIC segments\n"); - continue; - } - dynamicTab = (void *) phtab[i].VAddr; - j = i; // Save Dynamic Table ID - } - } - - // Page Align real base - iRealBase &= ~0xFFF; - DEBUGS(" elf_relocate: True Base = 0x%x, Compiled Base = 0x%x\n", Base, iRealBase); - - // Adjust "Real" Base - iBaseDiff = (Uint)Base - iRealBase; - - hdr->entrypoint += iBaseDiff; // Adjust Entrypoint - - // Check if a PT_DYNAMIC segement was found - if(!dynamicTab) { - SysDebug(" elf_relocate: No PT_DYNAMIC segment in image, returning\n"); - return hdr->entrypoint; - } - - // Adjust Dynamic Table - dynamicTab = (void *) ((Uint)dynamicTab + iBaseDiff); +#endif + +// === PROTOTYPES === +void elf_doRelocate(Uint r_info, Uint32 *ptr, Uint32 addend, Elf32_Sym *symtab, Uint base); +Uint ElfHashString(char *name); + +// === CODE === +/** + \fn int ElfRelocate(void *Base, char **envp, char *Filename) + \brief Relocates a loaded ELF Executable +*/ +void *ElfRelocate(void *Base, char **envp, char *Filename) +{ + Elf32_Ehdr *hdr = Base; + Elf32_Phdr *phtab; + int i, j; // Counters + char *libPath; + Uint iRealBase = -1; + Uint iBaseDiff; + int iSegmentCount; + int iSymCount; + Elf32_Rel *rel = NULL; + Elf32_Rela *rela = NULL; + Uint32 *pltgot = NULL; + void *plt = NULL; + int relSz=0, relEntSz=8; + int relaSz=0, relaEntSz=8; + int pltSz=0, pltType=0; + Elf32_Dyn *dynamicTab = NULL; // Dynamic Table Pointer + char *dynstrtab = NULL; // .dynamic String Table + Elf32_Sym *dynsymtab; + + DEBUGS("ElfRelocate: (Base=0x%x)\n", Base); + + // Check magic header + + + // Parse Program Header to get Dynamic Table + phtab = Base + hdr->phoff; + iSegmentCount = hdr->phentcount; + for(i=0;i phtab[i].VAddr) + iRealBase = phtab[i].VAddr; + + // Find Dynamic Section + if(phtab[i].Type == PT_DYNAMIC) { + if(dynamicTab) { + DEBUGS(" WARNING - elf_relocate: Multiple PT_DYNAMIC segments\n"); + continue; + } + dynamicTab = (void *) (intptr_t) phtab[i].VAddr; + j = i; // Save Dynamic Table ID + } + } + + // Page Align real base + iRealBase &= ~0xFFF; + DEBUGS(" elf_relocate: True Base = 0x%x, Compiled Base = 0x%x\n", Base, iRealBase); + + // Adjust "Real" Base + iBaseDiff = (intptr_t)Base - iRealBase; + + hdr->entrypoint += iBaseDiff; // Adjust Entrypoint + + // Check if a PT_DYNAMIC segement was found + if(!dynamicTab) { + SysDebug(" elf_relocate: No PT_DYNAMIC segment in image, returning\n"); + return (void *)hdr->entrypoint; + } - // === Get Symbol table and String Table === - for( j = 0; dynamicTab[j].d_tag != DT_NULL; j++) + // Adjust Dynamic Table + dynamicTab = (void *)( (intptr_t)dynamicTab + iBaseDiff ); + + // === Get Symbol table and String Table === + for( j = 0; dynamicTab[j].d_tag != DT_NULL; j++) { switch(dynamicTab[j].d_tag) { - // --- Symbol Table --- - case DT_SYMTAB: - DEBUGS(" elf_relocate: DYNAMIC Symbol Table 0x%x (0x%x)\n", - dynamicTab[j].d_val, dynamicTab[j].d_val + iBaseDiff); - dynamicTab[j].d_val += iBaseDiff; - dynsymtab = (void*)(dynamicTab[j].d_val); - hdr->misc.SymTable = dynamicTab[j].d_val; // Saved in unused bytes of ident - break; - // --- String Table --- - case DT_STRTAB: - DEBUGS(" elf_relocate: DYNAMIC String Table 0x%x (0x%x)\n", - dynamicTab[j].d_val, dynamicTab[j].d_val + iBaseDiff); - dynamicTab[j].d_val += iBaseDiff; - dynstrtab = (void*)(dynamicTab[j].d_val); - break; - // --- Hash Table -- - case DT_HASH: - dynamicTab[j].d_val += iBaseDiff; - iSymCount = ((Uint*)(dynamicTab[j].d_val))[1]; - hdr->misc.HashTable = dynamicTab[j].d_val; // Saved in unused bytes of ident + // --- Symbol Table --- + case DT_SYMTAB: + DEBUGS(" elf_relocate: DYNAMIC Symbol Table 0x%x (0x%x)\n", + dynamicTab[j].d_val, dynamicTab[j].d_val + iBaseDiff); + dynamicTab[j].d_val += iBaseDiff; + dynsymtab = (void*)(dynamicTab[j].d_val); + hdr->misc.SymTable = dynamicTab[j].d_val; // Saved in unused bytes of ident + break; + // --- String Table --- + case DT_STRTAB: + DEBUGS(" elf_relocate: DYNAMIC String Table 0x%x (0x%x)\n", + dynamicTab[j].d_val, dynamicTab[j].d_val + iBaseDiff); + dynamicTab[j].d_val += iBaseDiff; + dynstrtab = (void*)(dynamicTab[j].d_val); + break; + // --- Hash Table -- + case DT_HASH: + dynamicTab[j].d_val += iBaseDiff; + iSymCount = ((Uint*)(dynamicTab[j].d_val))[1]; + hdr->misc.HashTable = dynamicTab[j].d_val; // Saved in unused bytes of ident break; } } - - if(dynsymtab == NULL) { - SysDebug("ld-acess.so - WARNING: No Dynamic Symbol table, returning\n"); - return hdr->entrypoint; - } - - // Alter Symbols to true base - for(i=0;ientrypoint; + } + + // Alter Symbols to true base + for(i=0;i DT_JMPREL) continue; - //DEBUGS(" elf_relocate: %i-%i = %s,0x%x\n", - // i,j, csaDT_NAMES[dynamicTab[j].d_tag],dynamicTab[j].d_val); - break; - } - } - - DEBUGS(" elf_relocate: Beginning Relocation\n"); - - // Parse Relocation Entries - if(rel && relSz) - { - Uint32 *ptr; - DEBUGS(" elf_relocate: rel=0x%x, relSz=0x%x, relEntSz=0x%x\n", rel, relSz, relEntSz); - j = relSz / relEntSz; - for( i = 0; i < j; i++ ) + // === Parse Relocation Data === + DEBUGS(" elf_relocate: dynamicTab = 0x%x\n", dynamicTab); + for( j = 0; dynamicTab[j].d_tag != DT_NULL; j++) + { + switch(dynamicTab[j].d_tag) + { + // --- Shared Library Name --- + case DT_SONAME: + DEBUGS(" elf_relocate: .so Name '%s'\n", dynstrtab+dynamicTab[j].d_val); + break; + // --- Needed Library --- + case DT_NEEDED: + libPath = dynstrtab + dynamicTab[j].d_val; + DEBUGS(" Required Library '%s'\n", libPath); + if(LoadLibrary(libPath, NULL, envp) == 0) { + #if DEBUG + DEBUGS(" elf_relocate: Unable to load '%s'\n", libPath); + #else + SysDebug("Unable to load required library '%s'\n", libPath); + #endif + return 0; + } + break; + // --- PLT/GOT --- + case DT_PLTGOT: pltgot = (void*)(iBaseDiff + dynamicTab[j].d_val); break; + case DT_JMPREL: plt = (void*)(iBaseDiff + dynamicTab[j].d_val); break; + case DT_PLTREL: pltType = dynamicTab[j].d_val; break; + case DT_PLTRELSZ: pltSz = dynamicTab[j].d_val; break; + + // --- Relocation --- + case DT_REL: rel = (void*)(iBaseDiff + dynamicTab[j].d_val); break; + case DT_RELSZ: relSz = dynamicTab[j].d_val; break; + case DT_RELENT: relEntSz = dynamicTab[j].d_val; break; + case DT_RELA: rela = (void*)(iBaseDiff + dynamicTab[j].d_val); break; + case DT_RELASZ: relaSz = dynamicTab[j].d_val; break; + case DT_RELAENT: relaEntSz = dynamicTab[j].d_val; break; + + // --- Symbol Table --- + case DT_SYMTAB: + // --- Hash Table --- + case DT_HASH: + // --- String Table --- + case DT_STRTAB: + break; + + // --- Unknown --- + default: + if(dynamicTab[j].d_tag > DT_JMPREL) continue; + //DEBUGS(" elf_relocate: %i-%i = %s,0x%x\n", + // i,j, csaDT_NAMES[dynamicTab[j].d_tag],dynamicTab[j].d_val); + break; + } + } + + DEBUGS(" elf_relocate: Beginning Relocation\n"); + + // Parse Relocation Entries + if(rel && relSz) + { + Uint32 *ptr; + DEBUGS(" elf_relocate: rel=0x%x, relSz=0x%x, relEntSz=0x%x\n", rel, relSz, relEntSz); + j = relSz / relEntSz; + for( i = 0; i < j; i++ ) + { + //DEBUGS(" Rel %i: 0x%x+0x%x\n", i, iBaseDiff, rel[i].r_offset); + ptr = (void*)(iBaseDiff + rel[i].r_offset); + elf_doRelocate(rel[i].r_info, ptr, *ptr, dynsymtab, iBaseDiff); + } + } + // Parse Relocation Entries + if(rela && relaSz) + { + Uint32 *ptr; + DEBUGS(" elf_relocate: rela=0x%x, relaSz=0x%x, relaEntSz=0x%x\n", rela, relaSz, relaEntSz); + j = relaSz / relaEntSz; + for( i = 0; i < j; i++ ) + { + ptr = (void*)(iBaseDiff + rela[i].r_offset); + elf_doRelocate(rel[i].r_info, ptr, rela[i].r_addend, dynsymtab, iBaseDiff); + } + } + + // === Process PLT (Procedure Linkage Table) === + if(plt && pltSz) + { + Uint32 *ptr; + DEBUGS(" elf_relocate: Relocate PLT, plt=0x%x\n", plt); + if(pltType == DT_REL) + { + Elf32_Rel *pltRel = plt; + j = pltSz / sizeof(Elf32_Rel); + DEBUGS(" elf_relocate: PLT Reloc Type = Rel, %i entries\n", j); + for(i=0;ientrypoint); - return hdr->entrypoint; -} - -void elf_doRelocate(Uint r_info, Uint32 *ptr, Uint32 addend, Elf32_Sym *symtab, Uint base) -{ - int type = ELF32_R_TYPE(r_info); - int sym = ELF32_R_SYM(r_info); - Uint32 val; - switch( type ) - { - // Standard 32 Bit Relocation (S+A) - case R_386_32: - val = GetSymbol( symtab[sym].name ); - DEBUGS(" elf_doRelocate: R_386_32 *0x%x += 0x%x('%s')\n", - ptr, val, symtab[sym].name); - *ptr = val + addend; - break; - - // 32 Bit Relocation wrt. Offset (S+A-P) - case R_386_PC32: - DEBUGS(" elf_doRelocate: #%i: '%s'\n", sym, symtab[sym].name); - val = GetSymbol( symtab[sym].name ); - DEBUGS(" elf_doRelocate: R_386_PC32 *0x%x = 0x%x + 0x%x - 0x%x\n", - ptr, *ptr, val, (Uint)ptr ); - *ptr = val + addend - (Uint)ptr; - //*ptr = val + addend - ((Uint)ptr - base); + Elf32_Rela *pltRela = plt; + j = pltSz / sizeof(Elf32_Rela); + DEBUGS(" elf_relocate: PLT Reloc Type = Rela, %i entries\n", j); + for(i=0;ientrypoint); + return (void*)hdr->entrypoint; +} + +void elf_doRelocate(Uint r_info, Uint32 *ptr, Uint32 addend, Elf32_Sym *symtab, Uint base) +{ + int type = ELF32_R_TYPE(r_info); + int sym = ELF32_R_SYM(r_info); + Uint32 val; + switch( type ) + { + // Standard 32 Bit Relocation (S+A) + case R_386_32: + val = (intptr_t) GetSymbol( symtab[sym].name ); + DEBUGS(" elf_doRelocate: R_386_32 *0x%x += 0x%x('%s')\n", + ptr, val, symtab[sym].name); + *ptr = val + addend; + break; + + // 32 Bit Relocation wrt. Offset (S+A-P) + case R_386_PC32: + DEBUGS(" elf_doRelocate: #%i: '%s'\n", sym, symtab[sym].name); + val = (intptr_t) GetSymbol( symtab[sym].name ); + DEBUGS(" elf_doRelocate: R_386_PC32 *0x%x = 0x%x + 0x%x - 0x%x\n", + ptr, *ptr, val, (Uint)ptr ); + *ptr = val + addend - (intptr_t)ptr; + //*ptr = val + addend - ((Uint)ptr - base); break; - - // Absolute Value of a symbol (S) - case R_386_GLOB_DAT: + + // Absolute Value of a symbol (S) + case R_386_GLOB_DAT: case R_386_JMP_SLOT: - DEBUGS(" elf_doRelocate: #%i: '%s'\n", sym, symtab[sym].name); - val = GetSymbol( symtab[sym].name ); - DEBUGS(" elf_doRelocate: %s *0x%x = 0x%x\n", csaR_NAMES[type], ptr, val); - *ptr = val; - break; - - // Base Address (B+A) - case R_386_RELATIVE: - DEBUGS(" elf_doRelocate: R_386_RELATIVE *0x%x = 0x%x + 0x%x\n", ptr, base, addend); - *ptr = base + addend; - break; - - default: - DEBUGS(" elf_doRelocate: Rel 0x%x: 0x%x,%s\n", ptr, sym, csaR_NAMES[type]); - break; - } - + DEBUGS(" elf_doRelocate: #%i: '%s'\n", sym, symtab[sym].name); + val = (intptr_t) GetSymbol( symtab[sym].name ); + DEBUGS(" elf_doRelocate: %s *0x%x = 0x%x\n", csaR_NAMES[type], ptr, val); + *ptr = val; + break; + + // Base Address (B+A) + case R_386_RELATIVE: + DEBUGS(" elf_doRelocate: R_386_RELATIVE *0x%x = 0x%x + 0x%x\n", ptr, base, addend); + *ptr = base + addend; + break; + + default: + DEBUGS(" elf_doRelocate: Rel 0x%x: 0x%x,%s\n", ptr, sym, csaR_NAMES[type]); + break; + } + } - -/** - * \fn int ElfGetSymbol(Uint Base, char *name, Uint *ret) + +/** + * \fn int ElfGetSymbol(Uint Base, char *name, void **ret) */ -int ElfGetSymbol(Uint Base, char *Name, Uint *ret) +int ElfGetSymbol(void *Base, char *Name, void **ret) { - Elf32_Ehdr *hdr = (void*)Base; + Elf32_Ehdr *hdr = Base; Elf32_Sym *symtab; - int nbuckets = 0; + int nbuckets = 0; int iSymCount = 0; - int i; - Uint *pBuckets; - Uint *pChains; + int i; + Uint *pBuckets; + Uint *pChains; Uint iNameHash; - - //DEBUGS("ElfGetSymbol: (Base=0x%x, Name='%s')\n", Base, Name); - - // Catch the current executable - #if 0 - if( !hdr->misc.HashTable ) - { - Elf32_Phdr *phtab; - Elf32_Dyn *dynTab = NULL; - int j; - - // Locate the tables - phtab = (void*)( Base + hdr->phoff ); - for( i = 0; i < hdr->phentcount; i ++ ) - { - if( phtab[i].Type == PT_DYNAMIC ) { - dynTab = (void*)phtab[i].VAddr; - break ; - } - } - if( !dynTab ) { - SysDebug("ERROR - Unable to find DYNAMIC segment in %p", (void*)Base); - return 0; - } - - for( j = 0; dynTab[j].d_tag != DT_NULL; j++) - { - switch(dynTab[j].d_tag) - { - // --- Symbol Table --- - case DT_SYMTAB: - hdr->misc.SymTable = dynTab[j].d_val; - break; - // --- Hash Table -- - case DT_HASH: - hdr->misc.HashTable = dynTab[j].d_val; - break; - } - } - } - #endif - - if( !hdr->misc.SymTable || !hdr->misc.HashTable ) { - return 0; - } - - pBuckets = (void *) hdr->misc.HashTable; - symtab = (void *) hdr->misc.SymTable; + + //DEBUGS("ElfGetSymbol: (Base=0x%x, Name='%s')\n", Base, Name); + + // Catch the current executable + #if 0 + if( !hdr->misc.HashTable ) + { + Elf32_Phdr *phtab; + Elf32_Dyn *dynTab = NULL; + int j; + + // Locate the tables + phtab = (void*)( Base + hdr->phoff ); + for( i = 0; i < hdr->phentcount; i ++ ) + { + if( phtab[i].Type == PT_DYNAMIC ) { + dynTab = (void*)phtab[i].VAddr; + break ; + } + } + if( !dynTab ) { + SysDebug("ERROR - Unable to find DYNAMIC segment in %p", (void*)Base); + return 0; + } + + for( j = 0; dynTab[j].d_tag != DT_NULL; j++) + { + switch(dynTab[j].d_tag) + { + // --- Symbol Table --- + case DT_SYMTAB: + hdr->misc.SymTable = dynTab[j].d_val; + break; + // --- Hash Table -- + case DT_HASH: + hdr->misc.HashTable = dynTab[j].d_val; + break; + } + } + } + #endif + + if( !hdr->misc.SymTable || !hdr->misc.HashTable ) { + return 0; + } + + pBuckets = (void *) (intptr_t) hdr->misc.HashTable; + symtab = (void *) (intptr_t) hdr->misc.SymTable; + + nbuckets = pBuckets[0]; + iSymCount = pBuckets[1]; + pBuckets = &pBuckets[2]; + pChains = &pBuckets[ nbuckets ]; - nbuckets = pBuckets[0]; - iSymCount = pBuckets[1]; - pBuckets = &pBuckets[2]; - pChains = &pBuckets[ nbuckets ]; - // Get hash - iNameHash = ElfHashString(Name); - iNameHash %= nbuckets; - //DEBUGS(" ElfGetSymbol: iNameHash = 0x%x\n", iNameHash); + iNameHash = ElfHashString(Name); + iNameHash %= nbuckets; + //DEBUGS(" ElfGetSymbol: iNameHash = 0x%x\n", iNameHash); - // Walk Chain - i = pBuckets[ iNameHash ]; - //DEBUGS(" ElfGetSymbol: strcmp(Name, \"%s\")\n", symtab[i].name); - if(symtab[i].shndx != SHN_UNDEF && strcmp(symtab[i].name, Name) == 0) { - *ret = symtab[ i ].value; - return 1; - } - - //DEBUGS(" ElfGetSymbol: Hash of first = 0x%x\n", ElfHashString( symtab[i].name ) % nbuckets); - while(pChains[i] != STN_UNDEF) - { - //DEBUGS(" pChains[%i] = %i\n", i, pChains[i]); - i = pChains[i]; - //DEBUGS(" ElfGetSymbol: strcmp(Name, \"%s\")\n", symtab[ i ].name); - if(symtab[i].shndx != SHN_UNDEF && strcmp(symtab[ i ].name, Name) == 0) { - //DEBUGS("ElfGetSymbol: RETURN 1, '%s' = 0x%x\n", symtab[ i ].name, symtab[ i ].value); - *ret = symtab[ i ].value; - return 1; - } - } - + // Walk Chain + i = pBuckets[ iNameHash ]; + //DEBUGS(" ElfGetSymbol: strcmp(Name, \"%s\")\n", symtab[i].name); + if(symtab[i].shndx != SHN_UNDEF && strcmp(symtab[i].name, Name) == 0) { + *ret = (void*) (intptr_t) symtab[ i ].value; + return 1; + } + + //DEBUGS(" ElfGetSymbol: Hash of first = 0x%x\n", ElfHashString( symtab[i].name ) % nbuckets); + while(pChains[i] != STN_UNDEF) + { + //DEBUGS(" pChains[%i] = %i\n", i, pChains[i]); + i = pChains[i]; + //DEBUGS(" ElfGetSymbol: strcmp(Name, \"%s\")\n", symtab[ i ].name); + if(symtab[i].shndx != SHN_UNDEF && strcmp(symtab[ i ].name, Name) == 0) { + //DEBUGS("ElfGetSymbol: RETURN 1, '%s' = 0x%x\n", symtab[ i ].name, symtab[ i ].value); + *ret = (void*)(intptr_t)symtab[ i ].value; + return 1; + } + } + //DEBUGS("ElfGetSymbol: RETURN 0, Symbol '%s' not found\n", Name); return 0; -} - -Uint ElfHashString(char *name) -{ - Uint h = 0, g; - while(*name) - { - h = (h << 4) + *name++; - if( (g = h & 0xf0000000) ) - h ^= g >> 24; - h &= ~g; - } - return h; -} - -#if 0 -unsigned long elf_hash(const unsigned char *name) -{ - unsigned long h = 0, g; - while (*name) - { - h = (h << 4) + *name++; - if (g = h & 0xf0000000) - h ^= g >> 24; - h &= ~g; - } - return h; -} -#endif +} + +Uint ElfHashString(char *name) +{ + Uint h = 0, g; + while(*name) + { + h = (h << 4) + *name++; + if( (g = h & 0xf0000000) ) + h ^= g >> 24; + h &= ~g; + } + return h; +} + +#if 0 +unsigned long elf_hash(const unsigned char *name) +{ + unsigned long h = 0, g; + while (*name) + { + h = (h << 4) + *name++; + if (g = h & 0xf0000000) + h ^= g >> 24; + h &= ~g; + } + return h; +} +#endif diff --git a/Usermode/Libraries/ld-acess.so_src/export.c b/Usermode/Libraries/ld-acess.so_src/export.c index 83a1c7a8..d69c6d24 100644 --- a/Usermode/Libraries/ld-acess.so_src/export.c +++ b/Usermode/Libraries/ld-acess.so_src/export.c @@ -6,11 +6,11 @@ #define _STR(x) #x #define STR(x) _STR(x) -#define EXP(sym) {(Uint)&sym, STR(sym)} +#define EXP(sym) {&sym, STR(sym)} // === CONSTANTS === const struct { - Uint Value; + void *Value; char *Name; } caLocalExports[] = { EXP(gLoadedLibraries), diff --git a/Usermode/Libraries/ld-acess.so_src/loadlib.c b/Usermode/Libraries/ld-acess.so_src/loadlib.c index a5f0bc26..da74b824 100644 --- a/Usermode/Libraries/ld-acess.so_src/loadlib.c +++ b/Usermode/Libraries/ld-acess.so_src/loadlib.c @@ -13,12 +13,12 @@ #endif // === PROTOTYPES === -Uint IsFileLoaded(char *file); - int GetSymbolFromBase(Uint base, char *name, Uint *ret); +void *IsFileLoaded(char *file); + int GetSymbolFromBase(void *base, char *name, void **ret); // === IMPORTS === extern const struct { - Uint Value; + void *Value; char *Name; } caLocalExports[]; extern const int ciNumLocalExports; @@ -54,12 +54,12 @@ char *FindLibrary(char *DestBuf, char *SoName, char *ExtraSearchDir) /** */ -Uint LoadLibrary(char *SoName, char *SearchDir, char **envp) +void *LoadLibrary(char *SoName, char *SearchDir, char **envp) { char sTmpName[1024]; char *filename; - Uint iArg; - void (*fEntry)(int, int, char *[], char**); + void *base; + void (*fEntry)(void *, int, char *[], char**); DEBUGS("LoadLibrary: (filename='%s', envp=0x%x)\n", filename, envp); @@ -71,34 +71,34 @@ Uint LoadLibrary(char *SoName, char *SearchDir, char **envp) } DEBUGS(" LoadLibrary: filename='%s'\n", filename); - if( (iArg = IsFileLoaded(filename)) ) - return iArg; + if( (base = IsFileLoaded(filename)) ) + return base; // Load Library - iArg = SysLoadBin(filename, (Uint*)&fEntry); - if(iArg == 0) { + base = SysLoadBin(filename, (void**)&fEntry); + if(!base) { DEBUGS("LoadLibrary: RETURN 0\n"); return 0; } - DEBUGS(" LoadLibrary: iArg=0x%x, iEntry=0x%x\n", iArg, fEntry); + DEBUGS(" LoadLibrary: iArg=%p, iEntry=0x%x\n", base, fEntry); // Load Symbols - fEntry = (void*)DoRelocate( iArg, envp, filename ); + fEntry = DoRelocate( base, envp, filename ); // Call Entrypoint DEBUGS(" LoadLibrary: '%s' Entry 0x%x\n", SoName, fEntry); - fEntry(iArg, 0, NULL, envp); + fEntry(base, 0, NULL, envp); DEBUGS("LoadLibrary: RETURN 1\n"); - return iArg; + return base; } /** * \fn Uint IsFileLoaded(char *file) * \brief Determine if a file is already loaded */ -Uint IsFileLoaded(char *file) +void *IsFileLoaded(char *file) { int i; DEBUGS("IsFileLoaded: (file='%s')", file); @@ -119,7 +119,7 @@ Uint IsFileLoaded(char *file) * \fn void AddLoaded(char *File, Uint base) * \brief Add a file to the loaded list */ -void AddLoaded(char *File, Uint base) +void AddLoaded(char *File, void *base) { int i, length; char *name = gsNextAvailString; @@ -155,7 +155,7 @@ void AddLoaded(char *File, Uint base) /** * \fn void Unload(Uint Base) */ -void Unload(Uint Base) +void Unload(void *Base) { int i, j; int id; @@ -195,10 +195,10 @@ void Unload(Uint Base) \fn Uint GetSymbol(char *name) \brief Gets a symbol value from a loaded library */ -Uint GetSymbol(char *name) +void *GetSymbol(char *name) { int i; - Uint ret; + void *ret; //SysDebug("ciNumLocalExports = %i", ciNumLocalExports); for(i=0;i #include "common.h" // === PROTOTYPES === - int DoRelocate( Uint base, char **envp, char *Filename ); - int CallUser(Uint entry, Uint SP); - int ElfRelocate(void *Base, char *envp[], char *Filename); - int PE_Relocate(void *Base, char *envp[], char *Filename); +void *DoRelocate(void *base, char **envp, char *Filename); + int CallUser(void *Entry, void *SP); +void *ElfRelocate(void *Base, char *envp[], char *Filename); +void *PE_Relocate(void *Base, char *envp[], char *Filename); // === Imports === extern void gLinkedBase; @@ -20,24 +21,25 @@ extern tLoadedLib gLoadedLibraries[]; \brief Library entry point \note This is the entrypoint for the library */ -int SoMain(Uint base, int arg1) +int SoMain(void *base, void *arg1) { - int ret; + void *ret; // - Assume that the file pointer will be less than 4096 - if(base < 0x1000) { + if((intptr_t)base < 0x1000) { SysDebug("ld-acess - SoMain: Passed file pointer %i\n", base); _exit(-1); for(;;); } // Check if we are being called directly - if(base == (Uint)&gLinkedBase) { + if(base == &gLinkedBase) { SysDebug("ld-acess should not be directly called\n"); _exit(1); for(;;); } - gLoadedLibraries[0].Base = (Uint)&gLinkedBase; + gLoadedLibraries[0].Base = &gLinkedBase; + // 'libld-acess.so' because that is what applications link against gLoadedLibraries[0].Name = "/Acess/Libs/libld-acess.so"; // Otherwise do relocations @@ -51,22 +53,22 @@ int SoMain(Uint base, int arg1) // And call user //SysDebug("Calling User at 0x%x\n", ret); - CallUser( ret, (Uint)&arg1 ); + CallUser( ret, &arg1 ); return 0; } /** - \fn int DoRelocate(Uint base, char **envp) + \fn int DoRelocate(void *base, char **envp) \brief Relocates an in-memory image */ -int DoRelocate( Uint base, char **envp, char *Filename ) +void *DoRelocate(void *base, char **envp, char *Filename) { // Load Executable - if(*(Uint*)base == (0x7F|('E'<<8)|('L'<<16)|('F'<<24))) - return ElfRelocate((void*)base, envp, Filename); + if(*(Uint32*)base == (0x7F|('E'<<8)|('L'<<16)|('F'<<24))) + return ElfRelocate(base, envp, Filename); if(*(Uint16*)base == ('M'|('Z'<<8))) - return PE_Relocate((void*)base, envp, Filename); + return PE_Relocate(base, envp, Filename); SysDebug("ld-acess - DoRelocate: Unkown file format '0x%x 0x%x 0x%x 0x%x'\n", *(Uint8*)(base), *(Uint8*)(base+1), *(Uint8*)(base+2), *(Uint8*)(base+3) ); @@ -78,13 +80,20 @@ int DoRelocate( Uint base, char **envp, char *Filename ) /** \fn int CallUser(Uint entry, Uint sp) */ -int CallUser(Uint entry, Uint sp) +int CallUser(void *entry, void *sp) { - //SysDebug("CallUser: (entry=0x%x, sp=0x%x)", entry, sp); - *(Uint*)(sp-4) = 0; // Clear return address + #if ARCHDIR_IS_x86_64 + ((void **)sp)[-1] = 0; // Clear return address + __asm__ __volatile__ ( + "mov %%rax, %%rsp;\n\t" + "jmp *%%rcx" + : : "a"(sp), "c"(entry)); + #elif ARCHDIR_IS_x86 + ((void **)sp)[-1] = 0; // Clear return address __asm__ __volatile__ ( "mov %%eax, %%esp;\n\t" "jmp *%%ecx" : : "a"(sp), "c"(entry)); + #endif for(;;); } diff --git a/Usermode/Libraries/ld-acess.so_src/mm.asm b/Usermode/Libraries/ld-acess.so_src/mm.asm deleted file mode 100644 index bede9aef..00000000 --- a/Usermode/Libraries/ld-acess.so_src/mm.asm +++ /dev/null @@ -1,11 +0,0 @@ -; -; Acess2 System Interface -; -%include "syscalls.inc.asm" - -[BITS 32] -[extern _errno] - -[section .text] -SYSCALL1 _SysGetPhys, SYS_GETPHYS ; uint64_t _SysGetPhys(uint addr) -SYSCALL1 _SysAllocate, SYS_ALLOCATE ; uint64_t _SysAllocate(uint addr) diff --git a/Usermode/Libraries/ld-acess.so_src/pe.c b/Usermode/Libraries/ld-acess.so_src/pe.c index ec3b6b84..afcb4cef 100644 --- a/Usermode/Libraries/ld-acess.so_src/pe.c +++ b/Usermode/Libraries/ld-acess.so_src/pe.c @@ -3,6 +3,7 @@ * Portable Executable Loader */ #include "common.h" +#include #include "pe.h" #define PE_DEBUG 0 @@ -20,7 +21,7 @@ // === PROTOTYPES === int PE_Relocate(void *Base, char **envp, char *Filename); char *PE_int_GetTrueFile(char *file); - int PE_int_GetForwardSymbol(char *Fwd, Uint *Value); + int PE_int_GetForwardSymbol(char *Fwd, void **Value); // === CODE === int PE_Relocate(void *Base, char *envp[], char *Filename) @@ -32,8 +33,8 @@ int PE_Relocate(void *Base, char *envp[], char *Filename) tPE_HINT_NAME *name; Uint32 *importTab, *aIAT; int i, j; - Uint iBase = (Uint)Base; - Uint iLibBase; + intptr_t iBase = (intptr_t)Base; + void *pLibBase; DEBUGS("PE_Relocate: (Base=0x%x)\n", Base); @@ -49,12 +50,12 @@ int PE_Relocate(void *Base, char *envp[], char *Filename) impDir[i].ImportLookupTable += iBase/4; impDir[i].ImportAddressTable += iBase/4; DEBUGS(" PE_Relocate: DLL Required '%s'(0x%x)\n", impDir[i].DLLName, impDir[i].DLLName); - iLibBase = LoadLibrary(PE_int_GetTrueFile(impDir[i].DLLName), DLL_BASE_PATH, envp); - if(iLibBase == 0) { + pLibBase = LoadLibrary(PE_int_GetTrueFile(impDir[i].DLLName), DLL_BASE_PATH, envp); + if(pLibBase == 0) { SysDebug("Unable to load required library '%s'\n", impDir[i].DLLName); return 0; } - DEBUGS(" PE_Relocate: Loaded as 0x%x\n", iLibBase); + DEBUGS(" PE_Relocate: Loaded as 0x%x\n", pLibBase); importTab = impDir[i].ImportLookupTable; aIAT = impDir[i].ImportAddressTable; for( j = 0; importTab[j] != 0; j++ ) @@ -63,12 +64,14 @@ int PE_Relocate(void *Base, char *envp[], char *Filename) DEBUGS(" PE_Relocate: Import Ordinal %i\n", importTab[j] & 0x7FFFFFFF); else { + void *symPtr = 0; name = (void*)( iBase + importTab[j] ); DEBUGS(" PE_Relocate: Import Name '%s', Hint 0x%x\n", name->Name, name->Hint); - if( GetSymbolFromBase(iLibBase, name->Name, (Uint*)&aIAT[j]) == 0 ) { + if( GetSymbolFromBase(pLibBase, name->Name, symPtr) == 0 ) { SysDebug("Unable to find symbol '%s' in library '%s'\n", name->Name, impDir[i].DLLName); return 0; } + aIAT[j] = (intptr_t)symPtr; } } } @@ -88,9 +91,9 @@ int PE_Relocate(void *Base, char *envp[], char *Filename) /** * \fn int PE_GetSymbol(Uint Base, char *Name, Uint *Ret) */ -int PE_GetSymbol(Uint Base, char *Name, Uint *Ret) +int PE_GetSymbol(void *Base, char *Name, void **Ret) { - tPE_DOS_HEADER *dosHdr = (void*)Base; + tPE_DOS_HEADER *dosHdr = Base; tPE_IMAGE_HEADERS *peHeaders; tPE_DATA_DIR *directory; tPE_EXPORT_DIR *expDir; @@ -99,7 +102,7 @@ int PE_GetSymbol(Uint Base, char *Name, Uint *Ret) int i; int symbolCount; char *name; - Uint retVal; + intptr_t retVal; Uint expLen; peHeaders = (void*)( Base + dosHdr->PeHdrOffs ); @@ -118,14 +121,14 @@ int PE_GetSymbol(Uint Base, char *Name, Uint *Ret) //DEBUGS(" PE_GetSymbol: '%s' = 0x%x\n", name, Base + addrTable[ ordTable[i] ]); if(strcmp(name, Name) == 0) { - retVal = Base + addrTable[ ordTable[i] ]; + retVal = (intptr_t) Base + addrTable[ ordTable[i] ]; // Check for forwarding - if((Uint)expDir < retVal && retVal < (Uint)expDir + expLen) { + if( (intptr_t)expDir < retVal && retVal < (intptr_t)expDir + expLen) { char *fwd = (char*)retVal; DEBUGS(" PE_GetSymbol: '%s' forwards to '%s'\n", name, fwd); return PE_int_GetForwardSymbol(fwd, Ret); } - *Ret = retVal; + *Ret = (void*)retVal; return 1; } } @@ -149,12 +152,12 @@ char *PE_int_GetTrueFile(char *file) return &file[1]; } -int PE_int_GetForwardSymbol(char *Fwd, Uint *Value) +int PE_int_GetForwardSymbol(char *Fwd, void **Value) { char *libname; char *sym; int i; - Uint libbase; + void *libbase; int ret; // -- Find seperator diff --git a/Usermode/Libraries/ld-acess.so_src/syscalls.inc.asm b/Usermode/Libraries/ld-acess.so_src/syscalls.inc.asm deleted file mode 100644 index eea5573a..00000000 --- a/Usermode/Libraries/ld-acess.so_src/syscalls.inc.asm +++ /dev/null @@ -1,141 +0,0 @@ -; ======================== -; AcssMicro - System Calls -; ======================== - -%include "../../../Kernel/include/syscalls.inc.asm" - -;%define SYSCALL_OP jmp 0xCFFF0000 -%define SYSCALL_OP int 0xAC - -; System Call - No Arguments -%macro SYSCALL0 2 -[global %1:func] -%1: - push ebx - mov eax, %2 - SYSCALL_OP - mov [_errno], ebx - pop ebx - ret -%endmacro - -; System Call - 1 Argument -%macro SYSCALL1 2 -[global %1:func] -%1: - push ebp - mov ebp, esp - push ebx - mov eax, %2 - mov ebx, [ebp+8] - SYSCALL_OP - mov [_errno], ebx - pop ebx - pop ebp - ret -%endmacro - -; System Call - 2 Arguments -%macro SYSCALL2 2 -[global %1:func] -%1: - push ebp - mov ebp, esp - push ebx - mov eax, %2 - mov ebx, [ebp+8] - mov ecx, [ebp+12] - SYSCALL_OP - mov [_errno], ebx - pop ebx - pop ebp - ret -%endmacro - -; System Call - 3 Arguments -%macro SYSCALL3 2 -[global %1:func] -%1: - push ebp - mov ebp, esp - push ebx - mov eax, %2 - mov ebx, [ebp+8] - mov ecx, [ebp+12] - mov edx, [ebp+16] - SYSCALL_OP - mov [_errno], ebx - pop ebx - pop ebp - ret -%endmacro - -; System Call - 4 Arguments -%macro SYSCALL4 2 -[global %1:func] -%1: - push ebp - mov ebp, esp - push ebx - push edi - mov eax, %2 - mov ebx, [ebp+8] - mov ecx, [ebp+12] - mov edx, [ebp+16] - mov edi, [ebp+20] - SYSCALL_OP - mov [_errno], ebx - pop edi - pop ebx - pop ebp - ret -%endmacro - -; System Call - 5 Arguments -%macro SYSCALL5 2 -[global %1:func] -%1: - push ebp - mov ebp, esp - push ebx - push edi - push esi - mov eax, %2 - mov ebx, [ebp+8] - mov ecx, [ebp+12] - mov edx, [ebp+16] - mov edi, [ebp+20] - mov esi, [ebp+24] - SYSCALL_OP - mov [_errno], ebx - pop esi - pop edi - pop ebx - pop ebp - ret -%endmacro - -; System Call - 6 Arguments -%macro SYSCALL6 2 -[global %1:func] -%1: - push ebp - mov ebp, esp - push ebx - push edi - push esi - mov eax, %2 - mov ebx, [ebp+8] - mov ecx, [ebp+12] - mov edx, [ebp+16] - mov edi, [ebp+20] - mov esi, [ebp+24] - mov ebp, [ebp+28] - SYSCALL_OP - mov [_errno], ebx - pop esi - pop edi - pop ebx - pop ebp - ret -%endmacro diff --git a/Usermode/Libraries/ld-acess.so_src/vfs.asm b/Usermode/Libraries/ld-acess.so_src/vfs.asm deleted file mode 100644 index 6afd7c7f..00000000 --- a/Usermode/Libraries/ld-acess.so_src/vfs.asm +++ /dev/null @@ -1,25 +0,0 @@ -; -; Acess2 System Interface -; -%include "syscalls.inc.asm" - -[BITS 32] -[extern _errno] - -[section .text] -SYSCALL2 open, SYS_OPEN ; char*, int -SYSCALL3 reopen, SYS_REOPEN ; int, char*, int -SYSCALL1 close, SYS_CLOSE ; int -SYSCALL3 read, SYS_READ ; int, uint, void* -SYSCALL3 write, SYS_WRITE ; int, uint, void* -SYSCALL4 seek, SYS_SEEK ; int, uint64_t, int -SYSCALL1 tell, SYS_TELL ; int -SYSCALL3 finfo, SYS_FINFO ; int, void*, int -SYSCALL2 readdir, SYS_READDIR ; int, char* -SYSCALL2 _SysGetACL, SYS_GETACL ; int, void* -SYSCALL1 chdir, SYS_CHDIR ; char* -SYSCALL3 ioctl, SYS_IOCTL ; int, int, void* -SYSCALL4 _SysMount, SYS_MOUNT ; char*, char*, char*, char* -SYSCALL5 select, SYS_SELECT ; int, fd_set*, fd_set*, fd_set*, tTime* - -SYSCALL3 _SysOpenChild, SYS_OPENCHILD -- 2.20.1