From abe9f20840b7774b2d84c205f66dfab0991ab867 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Thu, 25 Nov 2010 22:36:55 +0800 Subject: [PATCH] Combined ld-acess and libacess --- Usermode/Libraries/acess.ld_src/acess.ld.h | 2 +- .../Libraries/ld-acess.old.so_src/Makefile | 39 ++ .../Libraries/ld-acess.old.so_src/common.h | 68 ++++ Usermode/Libraries/ld-acess.old.so_src/elf.c | 382 ++++++++++++++++++ .../Libraries/ld-acess.old.so_src/elf32.h | 215 ++++++++++ .../helpers.asm | 0 Usermode/Libraries/ld-acess.old.so_src/lib.c | 62 +++ .../Libraries/ld-acess.old.so_src/link.ld | 27 ++ .../Libraries/ld-acess.old.so_src/loadlib.c | 233 +++++++++++ Usermode/Libraries/ld-acess.old.so_src/main.c | 86 ++++ Usermode/Libraries/ld-acess.old.so_src/pe.c | 180 +++++++++ Usermode/Libraries/ld-acess.old.so_src/pe.h | 150 +++++++ Usermode/Libraries/ld-acess.so_src/Makefile | 74 ++-- Usermode/Libraries/ld-acess.so_src/common.h | 123 +++--- Usermode/Libraries/ld-acess.so_src/core.asm | 44 ++ Usermode/Libraries/ld-acess.so_src/lib.c | 108 ++--- Usermode/Libraries/ld-acess.so_src/link.ld | 52 +-- Usermode/Libraries/ld-acess.so_src/main.c | 8 +- Usermode/Libraries/ld-acess.so_src/mm.asm | 11 + .../ld-acess.so_src/syscalls.inc.asm | 141 +++++++ Usermode/Libraries/ld-acess.so_src/vfs.asm | 24 ++ 21 files changed, 1847 insertions(+), 182 deletions(-) create mode 100644 Usermode/Libraries/ld-acess.old.so_src/Makefile create mode 100644 Usermode/Libraries/ld-acess.old.so_src/common.h create mode 100644 Usermode/Libraries/ld-acess.old.so_src/elf.c create mode 100644 Usermode/Libraries/ld-acess.old.so_src/elf32.h rename Usermode/Libraries/{ld-acess.so_src => ld-acess.old.so_src}/helpers.asm (100%) create mode 100644 Usermode/Libraries/ld-acess.old.so_src/lib.c create mode 100644 Usermode/Libraries/ld-acess.old.so_src/link.ld create mode 100644 Usermode/Libraries/ld-acess.old.so_src/loadlib.c create mode 100644 Usermode/Libraries/ld-acess.old.so_src/main.c create mode 100644 Usermode/Libraries/ld-acess.old.so_src/pe.c create mode 100644 Usermode/Libraries/ld-acess.old.so_src/pe.h create mode 100644 Usermode/Libraries/ld-acess.so_src/core.asm create mode 100644 Usermode/Libraries/ld-acess.so_src/mm.asm create mode 100644 Usermode/Libraries/ld-acess.so_src/syscalls.inc.asm create mode 100644 Usermode/Libraries/ld-acess.so_src/vfs.asm diff --git a/Usermode/Libraries/acess.ld_src/acess.ld.h b/Usermode/Libraries/acess.ld_src/acess.ld.h index 71cbde2d..54e56f1b 100644 --- a/Usermode/Libraries/acess.ld_src/acess.ld.h +++ b/Usermode/Libraries/acess.ld_src/acess.ld.h @@ -2,7 +2,7 @@ OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") OUTPUT_ARCH(i386) ENTRY(start) SEARCH_DIR(ACESSDIR/Usermode/Libraries) -INPUT(-lacess crt0.o) +INPUT(crt0.o) SECTIONS { /* Read-only sections, merged into text segment: */ diff --git a/Usermode/Libraries/ld-acess.old.so_src/Makefile b/Usermode/Libraries/ld-acess.old.so_src/Makefile new file mode 100644 index 00000000..7c3a4b8c --- /dev/null +++ b/Usermode/Libraries/ld-acess.old.so_src/Makefile @@ -0,0 +1,39 @@ +# Acess Dynamic Linker (ELF) Version 1 +# LD-ACESS.SO +# Makefile + +-include ../Makefile.cfg + +COBJ = main.o lib.o loadlib.o elf.o pe.o +AOBJ = helpers.ao +BIN = ../ld-acess.so + +CFLAGS = -Wall -fno-builtin -fleading-underscore -fno-stack-protector +ASFLAGS = -felf +LDFLAGS = -T link.ld -Map map.txt -Bstatic + + +.PHONY: all clean install + +all: $(BIN) + +clean: + $(RM) $(BIN) $(AOBJ) $(COBJ) ld-acess.dmp ld-acess.dsm link.txt map.txt + +install: $(BIN) + $(xCP) $(BIN) $(DISTROOT)/Libs/ + +$(BIN): $(AOBJ) $(COBJ) + @echo --- $(LD) -shared -o $@ + @$(LD) $(LDFLAGS) -o $(BIN) $(AOBJ) $(COBJ) > link.txt + $(OBJDUMP) -x $(BIN) > ld-acess.dmp + $(OBJDUMP) -d $(BIN) > ld-acess.dsm + +$(COBJ): %.o: %.c + @echo $(CC) -o $@ + @$(CC) $(CFLAGS) -o $@ -c $< + +$(AOBJ): %.ao: %.asm + @echo $(AS) -o $@ + @$(AS) $(ASFLAGS) -o $@ $< + diff --git a/Usermode/Libraries/ld-acess.old.so_src/common.h b/Usermode/Libraries/ld-acess.old.so_src/common.h new file mode 100644 index 00000000..81913d50 --- /dev/null +++ b/Usermode/Libraries/ld-acess.old.so_src/common.h @@ -0,0 +1,68 @@ +/* + AcessOS v1 + By thePowersGang + ld-acess.so + COMMON.H +*/ +#ifndef _COMMON_H +#define _COMMON_H + +#define NULL ((void*)0) + +#include + +// === CONSTANTS === +#define MAX_LOADED_LIBRARIES 64 +#define MAX_STRINGS_BYTES 4096 +#define SYSTEM_LIB_DIR "/Acess/Libs/" + +// === Types === +typedef unsigned int Uint; +typedef unsigned char Uint8; +typedef unsigned short Uint16; +typedef unsigned long Uint32; +typedef signed char Sint8; +typedef signed short Sint16; +typedef signed long Sint32; + +typedef struct { + Uint Base; + char *Name; +} tLoadedLib; + +// === GLOBALS === +extern tLoadedLib gLoadedLibraries[MAX_LOADED_LIBRARIES]; + +// === Main === +extern int DoRelocate( Uint 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); + +// === Library Functions === +extern char *strcpy(char *dest, const char *src); +extern char *strcat(char *dest, const char *src); +extern int strcmp(const char *s1, const char *s2); +extern int strlen(const char *str); +extern int file_exists(char *filename); + +// === System Calls === +extern void SysExit(); +extern void SysDebug(char *fmt, ...); //!< Now implemented in main.c +extern void SysDebugV(char *fmt, ...); +extern Uint SysLoadBin(char *path, Uint *entry); +extern Uint SysUnloadBin(Uint Base); +extern void SysSetFaultHandler(int (*Hanlder)(int)); +extern int open(char *filename, int flags); +extern void close(int fd); + +// === ELF Loader === +extern int ElfGetSymbol(Uint Base, char *name, Uint *ret); + +// === PE Loader === +extern int PE_GetSymbol(Uint Base, char *Name, Uint *ret); + +#endif diff --git a/Usermode/Libraries/ld-acess.old.so_src/elf.c b/Usermode/Libraries/ld-acess.old.so_src/elf.c new file mode 100644 index 00000000..e1fcd2d4 --- /dev/null +++ b/Usermode/Libraries/ld-acess.old.so_src/elf.c @@ -0,0 +1,382 @@ +/* + * AcessOS 1 - Dynamic Loader + * By thePowersGang + */ +#include "common.h" +#include "elf32.h" + +#define DEBUG 0 + +#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"}; +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); + + // === 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 + 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;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++ ) + { + //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); + break; + + // 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; + } + +} + +/** + * \fn int ElfGetSymbol(Uint Base, char *name, Uint *ret) + */ +int ElfGetSymbol(Uint Base, char *Name, Uint *ret) +{ + Elf32_Ehdr *hdr = (void*)Base; + Elf32_Sym *symtab; + int nbuckets = 0; + int iSymCount = 0; + int i; + Uint *pBuckets; + Uint *pChains; + Uint iNameHash; + + //DEBUGS("ElfGetSymbol: (Base=0x%x, Name='%s')\n", Base, Name); + + pBuckets = (void *) hdr->misc.HashTable; + symtab = (void *) hdr->misc.SymTable; + + 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); + + // 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; + } + } + + //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 diff --git a/Usermode/Libraries/ld-acess.old.so_src/elf32.h b/Usermode/Libraries/ld-acess.old.so_src/elf32.h new file mode 100644 index 00000000..eededf4e --- /dev/null +++ b/Usermode/Libraries/ld-acess.old.so_src/elf32.h @@ -0,0 +1,215 @@ +/** + Acess v1 + \file elf32.h + \brief ELF Exeutable Loader +*/ +#ifndef _ELF32_H +#define _ELF32_H + +/** + \struct elf_header_s + \brief ELF File Header +*/ +struct sElf32_Ehdr { + union { + char ident[16]; //!< Identifier Bytes + struct { + Uint Ident1; + Uint Ident2; + Uint HashTable; + Uint SymTable; + } misc; + }; + Uint16 filetype; //!< File Type + Uint16 machine; //!< Machine / Arch + Uint32 version; //!< Version (File?) + Uint32 entrypoint; //!< Entry Point + Uint32 phoff; //!< Program Header Offset + Uint32 shoff; //!< Section Header Offset + Uint32 flags; //!< Flags + Uint16 headersize; //!< Header Size + Uint16 phentsize; //!< Program Header Entry Size + Uint16 phentcount; //!< Program Header Entry Count + Uint16 shentsize; //!< Section Header Entry Size + Uint16 shentcount; //!< Section Header Entry Count + Uint16 shstrindex; //!< Section Header String Table Index +}; + +/** + \name Executable Types + \{ +*/ +#define ET_NONE 0 //!< NULL Type +#define ET_REL 1 //!< Relocatable (Object) +#define ET_EXEC 2 //!< Executable +#define ET_DYN 3 //!< Dynamic Library +#define ET_CORE 4 //!< Core? +#define ET_LOPROC 0xFF00 //!< Low Impl Defined +#define ET_HIPROC 0xFFFF //!< High Impl Defined +//! \} + +/** + \name Section IDs + \{ +*/ +#define SHN_UNDEF 0 //!< Undefined Section +#define SHN_LORESERVE 0xFF00 //!< Low Reserved +#define SHN_LOPROC 0xFF00 //!< Low Impl Defined +#define SHN_HIPROC 0xFF1F //!< High Impl Defined +#define SHN_ABS 0xFFF1 //!< Absolute Address (Base: 0, Size: -1) +#define SHN_COMMON 0xFFF2 //!< Common +#define SHN_HIRESERVE 0xFFFF //!< High Reserved +//! \} + +/** + \enum eElfSectionTypes + \brief ELF Section Types +*/ +enum eElfSectionTypes { + SHT_NULL, //0 + SHT_PROGBITS, //1 + SHT_SYMTAB, //2 + SHT_STRTAB, //3 + SHT_RELA, //4 + SHT_HASH, //5 + SHT_DYNAMIC, //6 + SHT_NOTE, //7 + SHT_NOBITS, //8 + SHT_REL, //9 + SHT_SHLIB, //A + SHT_DYNSYM, //B + SHT_LAST, //C + SHT_LOPROC = 0x70000000, + SHT_HIPROC = 0x7fffffff, + SHT_LOUSER = 0x80000000, + SHT_HIUSER = 0xffffffff +}; + +#define SHF_WRITE 0x1 +#define SHF_ALLOC 0x2 +#define SHF_EXECINSTR 0x4 +#define SHF_MASKPROC 0xf0000000 + +struct sElf32_Shent { + Uint32 name; + Uint32 type; + Uint32 flags; + Uint32 address; + Uint32 offset; + Uint32 size; + Uint32 link; + Uint32 info; + Uint32 addralign; + Uint32 entsize; +}; //sizeof = 40 + +struct elf_sym_s { + union { + Uint32 nameOfs; + char *name; + }; + Uint32 value; //Address + Uint32 size; + Uint8 info; + Uint8 other; + Uint16 shndx; +}; +#define STN_UNDEF 0 // Undefined Symbol + +enum { + PT_NULL, //0 + PT_LOAD, //1 + PT_DYNAMIC, //2 + PT_INTERP, //3 + PT_NOTE, //4 + PT_SHLIB, //5 + PT_PHDR, //6 + PT_LOPROC = 0x70000000, + PT_HIPROC = 0x7fffffff +}; + +struct sElf32_Phdr { + Uint32 Type; + Uint Offset; + Uint VAddr; + Uint PAddr; + Uint32 FileSize; + Uint32 MemSize; + Uint32 Flags; + Uint32 Align; +}; + +struct elf32_rel_s { + Uint32 r_offset; + Uint32 r_info; +}; + +struct elf32_rela_s { + Uint32 r_offset; + Uint32 r_info; + Sint32 r_addend; +}; + +enum { + R_386_NONE=0, // none + R_386_32, // S+A + R_386_PC32, // S+A-P + R_386_GOT32, // G+A-P + R_386_PLT32, // L+A-P + R_386_COPY, // none + R_386_GLOB_DAT, // S + R_386_JMP_SLOT, // S + R_386_RELATIVE, // B+A + R_386_GOTOFF, // S+A-GOT + R_386_GOTPC, // GOT+A-P + R_386_LAST // none +}; + +#define ELF32_R_SYM(i) ((i)>>8) // Takes an info value and returns a symbol index +#define ELF32_R_TYPE(i) ((i)&0xFF) // Takes an info value and returns a type +#define ELF32_R_INFO(s,t) (((s)<<8)+((t)&0xFF)) // Takes a type and symbol index and returns an info value + +struct elf32_dyn_s { + Uint16 d_tag; + Uint32 d_val; //Also d_ptr +}; + +enum { + DT_NULL, //!< Marks End of list + DT_NEEDED, //!< Offset in strtab to needed library + DT_PLTRELSZ, //!< Size in bytes of PLT + DT_PLTGOT, //!< Address of PLT/GOT + DT_HASH, //!< Address of symbol hash table + DT_STRTAB, //!< String Table address + DT_SYMTAB, //!< Symbol Table address + DT_RELA, //!< Relocation table address + DT_RELASZ, //!< Size of relocation table + DT_RELAENT, //!< Size of entry in relocation table + DT_STRSZ, //!< Size of string table + DT_SYMENT, //!< Size of symbol table entry + DT_INIT, //!< Address of initialisation function + DT_FINI, //!< Address of termination function + DT_SONAME, //!< String table offset of so name + DT_RPATH, //!< String table offset of library path + DT_SYMBOLIC,//!< Reverse order of symbol searching for library, search libs first then executable + DT_REL, //!< Relocation Entries (Elf32_Rel instead of Elf32_Rela) + DT_RELSZ, //!< Size of above table (bytes) + DT_RELENT, //!< Size of entry in above table + DT_PLTREL, //!< Relocation entry of PLT + DT_DEBUG, //!< Debugging Entry - Unknown contents + DT_TEXTREL, //!< Indicates that modifcations to a non-writeable segment may occur + DT_JMPREL, //!< Address of PLT only relocation entries + DT_LOPROC = 0x70000000, //!< Low Definable + DT_HIPROC = 0x7FFFFFFF //!< High Definable +}; + +typedef struct sElf32_Ehdr Elf32_Ehdr; +typedef struct sElf32_Phdr Elf32_Phdr; +typedef struct sElf32_Shent Elf32_Shent; +typedef struct elf_sym_s elf_symtab; +typedef struct elf_sym_s Elf32_Sym; +typedef struct elf32_rel_s Elf32_Rel; +typedef struct elf32_rela_s Elf32_Rela; +typedef struct elf32_dyn_s Elf32_Dyn; + +#endif // defined(_EXE_ELF_H) diff --git a/Usermode/Libraries/ld-acess.so_src/helpers.asm b/Usermode/Libraries/ld-acess.old.so_src/helpers.asm similarity index 100% rename from Usermode/Libraries/ld-acess.so_src/helpers.asm rename to Usermode/Libraries/ld-acess.old.so_src/helpers.asm diff --git a/Usermode/Libraries/ld-acess.old.so_src/lib.c b/Usermode/Libraries/ld-acess.old.so_src/lib.c new file mode 100644 index 00000000..3acc129d --- /dev/null +++ b/Usermode/Libraries/ld-acess.old.so_src/lib.c @@ -0,0 +1,62 @@ +/* + AcessOS 1 + Dynamic Loader + By thePowersGang +*/ +#include "common.h" + +// === CODE === +char *strcpy(char *dest, const char *src) +{ + char *ret = dest; + while(*src) { + *dest = *src; + src ++; dest ++; + } + *dest = '\0'; + return ret; +} + +char *strcat(char *dest, const char *src) +{ + char *ret = dest; + while(*dest) dest++; + while(*src) *dest++ = *src++; + *dest = '\0'; + return ret; +} + +/** + * \fn int strcmp(const char *s1, const char *s2) + * \brief Compare two strings + */ +int strcmp(const char *s1, const char *s2) +{ + while(*s1 && *s1 == *s2) s1++,s2++; + return *s1-*s2; +} + +/** + * \fn int strlen(const char *str) + * \brief + */ +int strlen(const char *str) +{ + int len = 0; + while(*str) len++,str++; + return len; +} + +/** + * \fn int file_exists(char *filename) + * \brief Checks if a file exists + */ +int file_exists(char *filename) +{ + int fd; + //fd = open(filename, OPENFLAG_READ); + fd = open(filename, 0); + if(fd == -1) return 0; + close(fd); + return 1; +} diff --git a/Usermode/Libraries/ld-acess.old.so_src/link.ld b/Usermode/Libraries/ld-acess.old.so_src/link.ld new file mode 100644 index 00000000..8ddc2346 --- /dev/null +++ b/Usermode/Libraries/ld-acess.old.so_src/link.ld @@ -0,0 +1,27 @@ +ENTRY(_SoMain) +OUTPUT_FORMAT(elf32-i386) + +SECTIONS { + . = 0xBBFF0000; + _gLinkedBase = .; + + .text : AT(ADDR(.text)) { + code = .; + *(.text) + *(.rodata*) + } + + .data ALIGN (0x1000) : AT(ADDR(.data)) { + data = .; + *(.data) + } + + .bss ALIGN (0x1000) : AT(ADDR(.bss)) { + _sbss = .; + *(COMMON) + *(.bss) + _ebss = .; + bss = .; + } + _end = .; +} diff --git a/Usermode/Libraries/ld-acess.old.so_src/loadlib.c b/Usermode/Libraries/ld-acess.old.so_src/loadlib.c new file mode 100644 index 00000000..e5090ddd --- /dev/null +++ b/Usermode/Libraries/ld-acess.old.so_src/loadlib.c @@ -0,0 +1,233 @@ +/* + AcessOS 1 - Dynamic Loader + By thePowersGang +*/ +#include "common.h" + +#define DEBUG 0 + +#if DEBUG +# define DEBUGS(v...) SysDebug(v) +#else +# define DEBUGS(v...) +#endif + +// === PROTOTYPES === +Uint IsFileLoaded(char *file); + int GetSymbolFromBase(Uint base, char *name, Uint *ret); + +// === CONSTANTS === +const struct { + Uint Value; + char *Name; +} caLocalExports[] = { + {(Uint)gLoadedLibraries, "gLoadedLibraries"} +}; + +// === GLOABLS === +tLoadedLib gLoadedLibraries[MAX_LOADED_LIBRARIES]; +char gsLoadedStrings[MAX_STRINGS_BYTES]; +char *gsNextAvailString = gsLoadedStrings; +//tLoadLib *gpLoadedLibraries = NULL; + +// === CODE === +char *FindLibrary(char *DestBuf, char *SoName, char *ExtraSearchDir) +{ + // -- #1: Executable Specified + if(ExtraSearchDir) + { + strcpy(DestBuf, ExtraSearchDir); + strcat(DestBuf, "/"); + strcat(DestBuf, SoName); + if(file_exists(DestBuf)) return DestBuf; + } + + // -- #2: System + strcpy(DestBuf, SYSTEM_LIB_DIR); + strcat(DestBuf, SoName); + if(file_exists(DestBuf)) return DestBuf; + + // -- #3: Current Directory + if(file_exists(SoName)) return SoName; + + return NULL; +} + +/** + */ +Uint LoadLibrary(char *SoName, char *SearchDir, char **envp) +{ + char sTmpName[1024]; + char *filename; + Uint iArg; + void (*fEntry)(int, int, char *[], char**); + + DEBUGS("LoadLibrary: (filename='%s', envp=0x%x)\n", filename, envp); + + // Create Temp Name + filename = FindLibrary(sTmpName, SoName, SearchDir); + if(filename == NULL) { + DEBUGS("LoadLibrary: RETURN 0\n"); + return 0; + } + DEBUGS(" LoadLibrary: filename='%s'\n", filename); + + if( (iArg = IsFileLoaded(filename)) ) + return iArg; + + // Load Library + iArg = SysLoadBin(filename, (Uint*)&fEntry); + if(iArg == 0) { + DEBUGS("LoadLibrary: RETURN 0\n"); + return 0; + } + + DEBUGS(" LoadLibrary: iArg=0x%x, iEntry=0x%x\n", iArg, fEntry); + + // Load Symbols + fEntry = (void*)DoRelocate( iArg, envp, filename ); + + // Call Entrypoint + DEBUGS(" LoadLibrary: '%s' Entry 0x%x\n", SoName, fEntry); + fEntry(iArg, 0, NULL, envp); + + DEBUGS("LoadLibrary: RETURN 1\n"); + return iArg; +} + +/** + * \fn Uint IsFileLoaded(char *file) + * \brief Determine if a file is already loaded + */ +Uint IsFileLoaded(char *file) +{ + int i; + DEBUGS("IsFileLoaded: (file='%s')", file); + for( i = 0; i < MAX_LOADED_LIBRARIES; i++ ) + { + if(gLoadedLibraries[i].Base == 0) break; // Last entry has Base set to NULL + DEBUGS(" strcmp('%s', '%s')", gLoadedLibraries[i].Name, file); + if(strcmp(gLoadedLibraries[i].Name, file) == 0) { + DEBUGS("IsFileLoaded: Found %i (0x%x)", i, gLoadedLibraries[i].Base); + return gLoadedLibraries[i].Base; + } + } + DEBUGS("IsFileLoaded: Not Found"); + return 0; +} + +/** + * \fn void AddLoaded(char *File, Uint base) + * \brief Add a file to the loaded list + */ +void AddLoaded(char *File, Uint base) +{ + int i, length; + char *name = gsNextAvailString; + + DEBUGS("AddLoaded: (File='%s', base=0x%x)", File, base); + + // Find a free slot + for( i = 0; i < MAX_LOADED_LIBRARIES; i ++ ) + { + if(gLoadedLibraries[i].Base == 0) break; + } + if(i == MAX_LOADED_LIBRARIES) { + SysDebug("ERROR - ld-acess.so has run out of load slots!"); + return; + } + + // Check space in string buffer + length = strlen(File); + if(&name[length+1] >= &gsLoadedStrings[MAX_STRINGS_BYTES]) { + SysDebug("ERROR - ld-acess.so has run out of string buffer memory!"); + return; + } + + // Set information + gLoadedLibraries[i].Base = base; + strcpy(name, File); + gLoadedLibraries[i].Name = name; + gsNextAvailString = &name[length+1]; + DEBUGS("'%s' (0x%x) loaded as %i\n", name, base, i); + return; +} + +/** + * \fn void Unload(Uint Base) + */ +void Unload(Uint Base) +{ + int i, j; + int id; + char *str; + for( id = 0; id < MAX_LOADED_LIBRARIES; id++ ) + { + if(gLoadedLibraries[id].Base == Base) break; + } + if(id == MAX_LOADED_LIBRARIES) return; + + // Unload Binary + SysUnloadBin( Base ); + // Save String Pointer + str = gLoadedLibraries[id].Name; + + // Compact Loaded List + j = id; + for( i = j + 1; i < MAX_LOADED_LIBRARIES; i++, j++ ) + { + if(gLoadedLibraries[i].Base == 0) break; + // Compact String + strcpy(str, gLoadedLibraries[i].Name); + str += strlen(str)+1; + // Compact Entry + gLoadedLibraries[j].Base = gLoadedLibraries[i].Base; + gLoadedLibraries[j].Name = str; + } + + // NULL Last Entry + gLoadedLibraries[j].Base = 0; + gLoadedLibraries[j].Name = NULL; + // Save next string + gsNextAvailString = str; +} + +/** + \fn Uint GetSymbol(char *name) + \brief Gets a symbol value from a loaded library +*/ +Uint GetSymbol(char *name) +{ + int i; + Uint ret; + for(i=0;iPeHdrOffs; + + directory = peHeaders->OptHeader.Directory; + + // === Load Import Tables + impDir = Base + directory[PE_DIR_IMPORT].RVA; + for( i = 0; impDir[i].DLLName != NULL; i++ ) + { + impDir[i].DLLName += iBase; + 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) { + SysDebug("Unable to load required library '%s'\n", impDir[i].DLLName); + return 0; + } + DEBUGS(" PE_Relocate: Loaded as 0x%x\n", iLibBase); + importTab = impDir[i].ImportLookupTable; + aIAT = impDir[i].ImportAddressTable; + for( j = 0; importTab[j] != 0; j++ ) + { + if( importTab[j] & 0x80000000 ) + DEBUGS(" PE_Relocate: Import Ordinal %i\n", importTab[j] & 0x7FFFFFFF); + else + { + 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 ) { + SysDebug("Unable to find symbol '%s' in library '%s'\n", name->Name, impDir[i].DLLName); + return 0; + } + } + } + } + + #if DEBUG + for(i=0;iOptHeader.EntryPoint); + + return iBase + peHeaders->OptHeader.EntryPoint; +} + +/** + * \fn int PE_GetSymbol(Uint Base, char *Name, Uint *Ret) + */ +int PE_GetSymbol(Uint Base, char *Name, Uint *Ret) +{ + tPE_DOS_HEADER *dosHdr = (void*)Base; + tPE_IMAGE_HEADERS *peHeaders; + tPE_DATA_DIR *directory; + tPE_EXPORT_DIR *expDir; + Uint32 *nameTable, *addrTable; + Uint16 *ordTable; + int i; + int symbolCount; + char *name; + Uint retVal; + Uint expLen; + + peHeaders = (void*)( Base + dosHdr->PeHdrOffs ); + directory = peHeaders->OptHeader.Directory; + + expDir = (void*)( Base + directory[PE_DIR_EXPORT].RVA ); + expLen = directory[PE_DIR_EXPORT].Size; + symbolCount = expDir->NumNamePointers; + nameTable = (void*)( Base + expDir->NamePointerRVA ); + ordTable = (void*)( Base + expDir->OrdinalTableRVA ); + addrTable = (void*)( Base + expDir->AddressRVA ); + //DEBUGS(" PE_GetSymbol: %i Symbols\n", symbolCount); + for(i=0;i link.txt - $(OBJDUMP) -x $(BIN) > ld-acess.dmp +# Acess Dynamic Linker (ELF) Version 1 +# LD-ACESS.SO +# Makefile + +-include ../Makefile.cfg + +COBJ = main.o lib.o loadlib.o elf.o pe.o +AOBJ = core.ao vfs.ao mm.ao +BIN = ../ld-acess.so + +CFLAGS = -Wall -fno-builtin -fno-leading-underscore -fno-stack-protector +ASFLAGS = -felf +LDFLAGS = -T link.ld -Map map.txt -Bstatic + + +.PHONY: all clean install + +all: $(BIN) + +clean: + $(RM) $(BIN) $(AOBJ) $(COBJ) ld-acess.dmp ld-acess.dsm link.txt map.txt + +install: $(BIN) + $(xCP) $(BIN) $(DISTROOT)/Libs/ + +$(BIN): $(AOBJ) $(COBJ) + @echo --- $(LD) -shared -o $@ + @$(LD) $(LDFLAGS) -o $(BIN) $(AOBJ) $(COBJ) > link.txt + $(OBJDUMP) -x $(BIN) > ld-acess.dmp $(OBJDUMP) -d $(BIN) > ld-acess.dsm - -$(COBJ): %.o: %.c - @echo $(CC) -o $@ - @$(CC) $(CFLAGS) -o $@ -c $< - -$(AOBJ): %.ao: %.asm - @echo $(AS) -o $@ - @$(AS) $(ASFLAGS) -o $@ $< + +$(COBJ): %.o: %.c + @echo $(CC) -o $@ + @$(CC) $(CFLAGS) -o $@ -c $< + +$(AOBJ): %.ao: %.asm + @echo $(AS) -o $@ + @$(AS) $(ASFLAGS) -o $@ $< diff --git a/Usermode/Libraries/ld-acess.so_src/common.h b/Usermode/Libraries/ld-acess.so_src/common.h index 81913d50..3356a27d 100644 --- a/Usermode/Libraries/ld-acess.so_src/common.h +++ b/Usermode/Libraries/ld-acess.so_src/common.h @@ -1,68 +1,71 @@ -/* - AcessOS v1 - By thePowersGang - ld-acess.so - COMMON.H -*/ -#ifndef _COMMON_H -#define _COMMON_H - -#define NULL ((void*)0) - -#include - -// === CONSTANTS === -#define MAX_LOADED_LIBRARIES 64 -#define MAX_STRINGS_BYTES 4096 -#define SYSTEM_LIB_DIR "/Acess/Libs/" - -// === Types === -typedef unsigned int Uint; -typedef unsigned char Uint8; -typedef unsigned short Uint16; -typedef unsigned long Uint32; -typedef signed char Sint8; -typedef signed short Sint16; -typedef signed long Sint32; - -typedef struct { - Uint Base; - char *Name; -} tLoadedLib; - -// === GLOBALS === -extern tLoadedLib gLoadedLibraries[MAX_LOADED_LIBRARIES]; +/* + AcessOS v1 + By thePowersGang + ld-acess.so + COMMON.H +*/ +#ifndef _COMMON_H +#define _COMMON_H + +#define NULL ((void*)0) + +#include + +// HACK: Replace with underscored +#define SysDebug _SysDebug + +// === CONSTANTS === +#define MAX_LOADED_LIBRARIES 64 +#define MAX_STRINGS_BYTES 4096 +#define SYSTEM_LIB_DIR "/Acess/Libs/" + +// === Types === +typedef unsigned int Uint; +typedef unsigned char Uint8; +typedef unsigned short Uint16; +typedef unsigned long Uint32; +typedef signed char Sint8; +typedef signed short Sint16; +typedef signed long Sint32; + +typedef struct { + Uint Base; + char *Name; +} tLoadedLib; + +// === GLOBALS === +extern tLoadedLib gLoadedLibraries[MAX_LOADED_LIBRARIES]; // === Main === extern int DoRelocate( Uint base, char **envp, char *Filename ); - -// === Library/Symbol Manipulation == + +// === 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); - -// === Library Functions === -extern char *strcpy(char *dest, const char *src); +extern Uint GetSymbol(char *name); +extern int GetSymbolFromBase(Uint base, char *name, Uint *ret); + +// === Library Functions === +extern char *strcpy(char *dest, const char *src); extern char *strcat(char *dest, const char *src); -extern int strcmp(const char *s1, const char *s2); -extern int strlen(const char *str); -extern int file_exists(char *filename); - -// === System Calls === -extern void SysExit(); -extern void SysDebug(char *fmt, ...); //!< Now implemented in main.c -extern void SysDebugV(char *fmt, ...); -extern Uint SysLoadBin(char *path, Uint *entry); -extern Uint SysUnloadBin(Uint Base); -extern void SysSetFaultHandler(int (*Hanlder)(int)); -extern int open(char *filename, int flags); -extern void close(int fd); +extern int strcmp(const char *s1, const char *s2); +extern int strlen(const char *str); +extern int file_exists(char *filename); + +// === System Calls === +extern void _exit(int retval); +extern void SysDebug(char *fmt, ...); //!< Now implemented in main.c +extern void SysDebugV(char *fmt, ...); +extern Uint SysLoadBin(char *path, Uint *entry); +extern Uint SysUnloadBin(Uint Base); +extern void SysSetFaultHandler(int (*Hanlder)(int)); +extern int open(char *filename, int flags); +extern void close(int fd); // === ELF Loader === -extern int ElfGetSymbol(Uint Base, char *name, Uint *ret); - -// === PE Loader === -extern int PE_GetSymbol(Uint Base, char *Name, Uint *ret); - -#endif +extern int ElfGetSymbol(Uint Base, char *name, Uint *ret); + +// === PE Loader === +extern int PE_GetSymbol(Uint Base, char *Name, Uint *ret); + +#endif diff --git a/Usermode/Libraries/ld-acess.so_src/core.asm b/Usermode/Libraries/ld-acess.so_src/core.asm new file mode 100644 index 00000000..c44dc535 --- /dev/null +++ b/Usermode/Libraries/ld-acess.so_src/core.asm @@ -0,0 +1,44 @@ +; +; Acess2 System Interface +; +%include "syscalls.inc.asm" + +[BITS 32] +[section .data] +[global _errno:data (4)] +_errno: + dd 0 + +[section .text] +; --- Process Controll --- +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 diff --git a/Usermode/Libraries/ld-acess.so_src/lib.c b/Usermode/Libraries/ld-acess.so_src/lib.c index 3acc129d..28fb4672 100644 --- a/Usermode/Libraries/ld-acess.so_src/lib.c +++ b/Usermode/Libraries/ld-acess.so_src/lib.c @@ -1,62 +1,62 @@ -/* - AcessOS 1 - Dynamic Loader - By thePowersGang -*/ -#include "common.h" - -// === CODE === -char *strcpy(char *dest, const char *src) -{ - char *ret = dest; - while(*src) { - *dest = *src; - src ++; dest ++; - } - *dest = '\0'; - return ret; -} - -char *strcat(char *dest, const char *src) -{ - char *ret = dest; - while(*dest) dest++; - while(*src) *dest++ = *src++; - *dest = '\0'; - return ret; +/* + AcessOS 1 + Dynamic Loader + By thePowersGang +*/ +#include "common.h" + +// === CODE === +char *strcpy(char *dest, const char *src) +{ + char *ret = dest; + while(*src) { + *dest = *src; + src ++; dest ++; + } + *dest = '\0'; + return ret; +} + +char *strcat(char *dest, const char *src) +{ + char *ret = dest; + while(*dest) dest++; + while(*src) *dest++ = *src++; + *dest = '\0'; + return ret; } - -/** - * \fn int strcmp(const char *s1, const char *s2) - * \brief Compare two strings + +/** + * \fn int strcmp(const char *s1, const char *s2) + * \brief Compare two strings */ int strcmp(const char *s1, const char *s2) { while(*s1 && *s1 == *s2) s1++,s2++; return *s1-*s2; -} - -/** - * \fn int strlen(const char *str) - * \brief - */ -int strlen(const char *str) -{ - int len = 0; - while(*str) len++,str++; - return len; } - -/** - * \fn int file_exists(char *filename) - * \brief Checks if a file exists + +/** + * \fn int strlen(const char *str) + * \brief + */ +int strlen(const char *str) +{ + int len = 0; + while(*str) len++,str++; + return len; +} + +/** + * \fn int file_exists(char *filename) + * \brief Checks if a file exists */ -int file_exists(char *filename) -{ - int fd; - //fd = open(filename, OPENFLAG_READ); - fd = open(filename, 0); - if(fd == -1) return 0; - close(fd); - return 1; -} +int file_exists(char *filename) +{ + int fd; + //fd = open(filename, OPENFLAG_READ); + fd = open(filename, 0); + if(fd == -1) return 0; + close(fd); + return 1; +} diff --git a/Usermode/Libraries/ld-acess.so_src/link.ld b/Usermode/Libraries/ld-acess.so_src/link.ld index 8ddc2346..c318850d 100644 --- a/Usermode/Libraries/ld-acess.so_src/link.ld +++ b/Usermode/Libraries/ld-acess.so_src/link.ld @@ -1,27 +1,27 @@ -ENTRY(_SoMain) -OUTPUT_FORMAT(elf32-i386) - -SECTIONS { - . = 0xBBFF0000; - _gLinkedBase = .; - - .text : AT(ADDR(.text)) { - code = .; - *(.text) - *(.rodata*) - } - - .data ALIGN (0x1000) : AT(ADDR(.data)) { - data = .; - *(.data) - } - - .bss ALIGN (0x1000) : AT(ADDR(.bss)) { - _sbss = .; - *(COMMON) - *(.bss) - _ebss = .; - bss = .; - } - _end = .; +ENTRY(SoMain) +OUTPUT_FORMAT(elf32-i386) + +SECTIONS { + . = 0xBBFF0000; + gLinkedBase = .; + + .text : AT(ADDR(.text)) { + code = .; + *(.text) + *(.rodata*) + } + + .data ALIGN (0x1000) : AT(ADDR(.data)) { + data = .; + *(.data) + } + + .bss ALIGN (0x1000) : AT(ADDR(.bss)) { + _sbss = .; + *(COMMON) + *(.bss) + _ebss = .; + bss = .; + } + _end = .; } diff --git a/Usermode/Libraries/ld-acess.so_src/main.c b/Usermode/Libraries/ld-acess.so_src/main.c index 678dc9a6..c3a7c278 100644 --- a/Usermode/Libraries/ld-acess.so_src/main.c +++ b/Usermode/Libraries/ld-acess.so_src/main.c @@ -26,13 +26,13 @@ int SoMain(Uint base, int arg1) // - Assume that the file pointer will be less than 4096 if(base < 0x1000) { SysDebug("ld-acess - SoMain: Passed file pointer %i\n", base); - SysExit(); + _exit(-1); for(;;); } // Check if we are being called directly if(base == (Uint)&gLinkedBase) { SysDebug("ld-acess should not be directly called\n"); - SysExit(); + _exit(1); for(;;); } @@ -41,7 +41,7 @@ int SoMain(Uint base, int arg1) ret = DoRelocate( base, NULL, "Executable" ); if( ret == 0 ) { SysDebug("ld-acess - SoMain: Relocate failed, base=0x%x\n", base); - SysExit(); + _exit(-1); for(;;); } @@ -67,7 +67,7 @@ int DoRelocate( Uint base, char **envp, char *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) ); SysDebug("ld-acess - DoRelocate: File '%s'\n", Filename); - SysExit(); + _exit(-1); for(;;); } diff --git a/Usermode/Libraries/ld-acess.so_src/mm.asm b/Usermode/Libraries/ld-acess.so_src/mm.asm new file mode 100644 index 00000000..bede9aef --- /dev/null +++ b/Usermode/Libraries/ld-acess.so_src/mm.asm @@ -0,0 +1,11 @@ +; +; 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/syscalls.inc.asm b/Usermode/Libraries/ld-acess.so_src/syscalls.inc.asm new file mode 100644 index 00000000..eea5573a --- /dev/null +++ b/Usermode/Libraries/ld-acess.so_src/syscalls.inc.asm @@ -0,0 +1,141 @@ +; ======================== +; 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 new file mode 100644 index 00000000..44bce6b2 --- /dev/null +++ b/Usermode/Libraries/ld-acess.so_src/vfs.asm @@ -0,0 +1,24 @@ +; +; 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* + +SYSCALL3 _SysOpenChild, SYS_OPENCHILD -- 2.20.1