Combined ld-acess and libacess
authorJohn Hodge <[email protected]>
Thu, 25 Nov 2010 14:36:55 +0000 (22:36 +0800)
committerJohn Hodge <[email protected]>
Thu, 25 Nov 2010 14:36:55 +0000 (22:36 +0800)
22 files changed:
Usermode/Libraries/acess.ld_src/acess.ld.h
Usermode/Libraries/ld-acess.old.so_src/Makefile [new file with mode: 0644]
Usermode/Libraries/ld-acess.old.so_src/common.h [new file with mode: 0644]
Usermode/Libraries/ld-acess.old.so_src/elf.c [new file with mode: 0644]
Usermode/Libraries/ld-acess.old.so_src/elf32.h [new file with mode: 0644]
Usermode/Libraries/ld-acess.old.so_src/helpers.asm [new file with mode: 0644]
Usermode/Libraries/ld-acess.old.so_src/lib.c [new file with mode: 0644]
Usermode/Libraries/ld-acess.old.so_src/link.ld [new file with mode: 0644]
Usermode/Libraries/ld-acess.old.so_src/loadlib.c [new file with mode: 0644]
Usermode/Libraries/ld-acess.old.so_src/main.c [new file with mode: 0644]
Usermode/Libraries/ld-acess.old.so_src/pe.c [new file with mode: 0644]
Usermode/Libraries/ld-acess.old.so_src/pe.h [new file with mode: 0644]
Usermode/Libraries/ld-acess.so_src/Makefile
Usermode/Libraries/ld-acess.so_src/common.h
Usermode/Libraries/ld-acess.so_src/core.asm [new file with mode: 0644]
Usermode/Libraries/ld-acess.so_src/helpers.asm [deleted file]
Usermode/Libraries/ld-acess.so_src/lib.c
Usermode/Libraries/ld-acess.so_src/link.ld
Usermode/Libraries/ld-acess.so_src/main.c
Usermode/Libraries/ld-acess.so_src/mm.asm [new file with mode: 0644]
Usermode/Libraries/ld-acess.so_src/syscalls.inc.asm [new file with mode: 0644]
Usermode/Libraries/ld-acess.so_src/vfs.asm [new file with mode: 0644]

index 71cbde2..54e56f1 100644 (file)
@@ -2,7 +2,7 @@ OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386")
 OUTPUT_ARCH(i386)\r
 ENTRY(start)\r
 SEARCH_DIR(ACESSDIR/Usermode/Libraries)\r
-INPUT(-lacess crt0.o)\r
+INPUT(crt0.o)\r
 SECTIONS\r
 {\r
   /* Read-only sections, merged into text segment: */\r
diff --git a/Usermode/Libraries/ld-acess.old.so_src/Makefile b/Usermode/Libraries/ld-acess.old.so_src/Makefile
new file mode 100644 (file)
index 0000000..7c3a4b8
--- /dev/null
@@ -0,0 +1,39 @@
+# Acess Dynamic Linker (ELF) Version 1\r
+#  LD-ACESS.SO\r
+#  Makefile\r
+\r
+-include ../Makefile.cfg\r
+\r
+COBJ = main.o lib.o loadlib.o elf.o pe.o\r
+AOBJ = helpers.ao\r
+BIN = ../ld-acess.so\r
+\r
+CFLAGS   = -Wall -fno-builtin -fleading-underscore -fno-stack-protector\r
+ASFLAGS  = -felf\r
+LDFLAGS  = -T link.ld -Map map.txt -Bstatic\r
+\r
+\r
+.PHONY: all clean install\r
+\r
+all:   $(BIN)\r
+\r
+clean:\r
+       $(RM) $(BIN) $(AOBJ) $(COBJ) ld-acess.dmp ld-acess.dsm link.txt map.txt\r
+\r
+install: $(BIN)\r
+       $(xCP) $(BIN) $(DISTROOT)/Libs/\r
+\r
+$(BIN): $(AOBJ) $(COBJ)\r
+       @echo --- $(LD) -shared -o $@\r
+       @$(LD) $(LDFLAGS) -o $(BIN) $(AOBJ) $(COBJ) > link.txt\r
+       $(OBJDUMP) -x $(BIN) > ld-acess.dmp\r
+       $(OBJDUMP) -d $(BIN) > ld-acess.dsm
+\r
+$(COBJ): %.o: %.c\r
+       @echo $(CC) -o $@\r
+       @$(CC) $(CFLAGS) -o $@ -c $<\r
+\r
+$(AOBJ): %.ao: %.asm\r
+       @echo $(AS) -o $@\r
+       @$(AS) $(ASFLAGS) -o $@ $<\r
+       
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 (file)
index 0000000..81913d5
--- /dev/null
@@ -0,0 +1,68 @@
+/*\r
+ AcessOS v1\r
+ By thePowersGang\r
+ ld-acess.so\r
+ COMMON.H\r
+*/\r
+#ifndef _COMMON_H\r
+#define _COMMON_H\r
+\r
+#define        NULL    ((void*)0)\r
+\r
+#include <stdarg.h>\r
+\r
+// === CONSTANTS ===\r
+#define        MAX_LOADED_LIBRARIES    64\r
+#define        MAX_STRINGS_BYTES       4096\r
+#define        SYSTEM_LIB_DIR  "/Acess/Libs/"\r
+\r
+// === Types ===\r
+typedef unsigned int   Uint;\r
+typedef unsigned char  Uint8;\r
+typedef unsigned short Uint16;\r
+typedef unsigned long  Uint32;\r
+typedef signed char            Sint8;\r
+typedef signed short   Sint16;\r
+typedef signed long            Sint32;\r
+\r
+typedef struct {\r
+       Uint    Base;\r
+       char    *Name;\r
+}      tLoadedLib;\r
+\r
+// === GLOBALS ===\r
+extern tLoadedLib      gLoadedLibraries[MAX_LOADED_LIBRARIES];\r
+
+// === Main ===
+extern int     DoRelocate( Uint base, char **envp, char *Filename );
+\r
+// === Library/Symbol Manipulation ==\r
+extern Uint    LoadLibrary(char *filename, char *SearchDir, char **envp);
+extern void    AddLoaded(char *File, Uint base);
+extern Uint    GetSymbol(char *name);\r
+extern int     GetSymbolFromBase(Uint base, char *name, Uint *ret);\r
+\r
+// === Library Functions ===\r
+extern char    *strcpy(char *dest, const char *src);\r
+extern char    *strcat(char *dest, const char *src);
+extern int     strcmp(const char *s1, const char *s2);\r
+extern int     strlen(const char *str);\r
+extern int     file_exists(char *filename);\r
+\r
+// === System Calls ===\r
+extern void    SysExit();\r
+extern void    SysDebug(char *fmt, ...);       //!< Now implemented in main.c\r
+extern void    SysDebugV(char *fmt, ...);\r
+extern Uint    SysLoadBin(char *path, Uint *entry);\r
+extern Uint    SysUnloadBin(Uint Base);\r
+extern void    SysSetFaultHandler(int (*Hanlder)(int));\r
+extern int     open(char *filename, int flags);\r
+extern void    close(int fd);\r
+
+// === ELF Loader ===
+extern int     ElfGetSymbol(Uint Base, char *name, Uint *ret);\r
+\r
+// === PE Loader ===\r
+extern int     PE_GetSymbol(Uint Base, char *Name, Uint *ret);\r
+\r
+#endif\r
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 (file)
index 0000000..e1fcd2d
--- /dev/null
@@ -0,0 +1,382 @@
+/*\r
+ * AcessOS 1 - Dynamic Loader\r
+ * By thePowersGang\r
+ */\r
+#include "common.h"\r
+#include "elf32.h"\r
+
+#define DEBUG  0
+
+#if DEBUG\r
+# define       DEBUGS(v...)    SysDebug(v)\r
+#else
+# define       DEBUGS(...)     
+#endif
+\r
+// === CONSTANTS ===
+#if DEBUG\r
+//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"};\r
+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\r
+\r
+// === PROTOTYPES ===\r
+void elf_doRelocate(Uint r_info, Uint32 *ptr, Uint32 addend, Elf32_Sym *symtab, Uint base);\r
+Uint ElfHashString(char *name);\r
+\r
+// === CODE ===\r
+/**\r
+ \fn int ElfRelocate(void *Base, char **envp, char *Filename)\r
+ \brief Relocates a loaded ELF Executable\r
+*/\r
+int ElfRelocate(void *Base, char **envp, char *Filename)\r
+{\r
+       Elf32_Ehdr      *hdr = Base;\r
+       Elf32_Phdr      *phtab;\r
+        int    i, j;   // Counters\r
+       char    *libPath;\r
+       Uint    iRealBase = -1;\r
+       Uint    iBaseDiff;\r
+        int    iSegmentCount;\r
+        int    iSymCount;\r
+       Elf32_Rel       *rel = NULL;\r
+       Elf32_Rela      *rela = NULL;\r
+       Uint32  *pltgot = NULL;\r
+       void    *plt = NULL;\r
+        int    relSz=0, relEntSz=8;\r
+        int    relaSz=0, relaEntSz=8;\r
+        int    pltSz=0, pltType=0;\r
+       Elf32_Dyn       *dynamicTab = NULL;     // Dynamic Table Pointer\r
+       char    *dynstrtab = NULL;      // .dynamic String Table\r
+       Elf32_Sym       *dynsymtab;\r
+       \r
+       DEBUGS("ElfRelocate: (Base=0x%x)\n", Base);\r
+       \r
+       // Check magic header\r
+       \r
+       \r
+       // Parse Program Header to get Dynamic Table\r
+       phtab = Base + hdr->phoff;\r
+       iSegmentCount = hdr->phentcount;\r
+       for(i=0;i<iSegmentCount;i++)\r
+       {\r
+               // Determine linked base address\r
+               if(phtab[i].Type == PT_LOAD && iRealBase > phtab[i].VAddr)\r
+                       iRealBase = phtab[i].VAddr;\r
+               \r
+               // Find Dynamic Section\r
+               if(phtab[i].Type == PT_DYNAMIC) {\r
+                       if(dynamicTab) {\r
+                               DEBUGS(" WARNING - elf_relocate: Multiple PT_DYNAMIC segments\n");\r
+                               continue;\r
+                       }\r
+                       dynamicTab = (void *) phtab[i].VAddr;\r
+                       j = i;  // Save Dynamic Table ID\r
+               }\r
+       }\r
+       \r
+       // Page Align real base\r
+       iRealBase &= ~0xFFF;\r
+       DEBUGS(" elf_relocate: True Base = 0x%x, Compiled Base = 0x%x\n", Base, iRealBase);\r
+       \r
+       // Adjust "Real" Base\r
+       iBaseDiff = (Uint)Base - iRealBase;\r
+       \r
+       hdr->entrypoint += iBaseDiff;   // Adjust Entrypoint\r
+       \r
+       // Check if a PT_DYNAMIC segement was found\r
+       if(!dynamicTab) {\r
+               SysDebug(" elf_relocate: No PT_DYNAMIC segment in image, returning\n");\r
+               return hdr->entrypoint;\r
+       }\r
+       \r
+       // Adjust Dynamic Table\r
+       dynamicTab = (void *) ((Uint)dynamicTab + iBaseDiff);\r
+       
+       // === Get Symbol table and String Table ===\r
+       for( j = 0; dynamicTab[j].d_tag != DT_NULL; j++)\r
+       {
+               switch(dynamicTab[j].d_tag)
+               {
+               // --- Symbol Table ---\r
+               case DT_SYMTAB:\r
+                       DEBUGS(" elf_relocate: DYNAMIC Symbol Table 0x%x (0x%x)\n",\r
+                               dynamicTab[j].d_val, dynamicTab[j].d_val + iBaseDiff);\r
+                       dynamicTab[j].d_val += iBaseDiff;\r
+                       dynsymtab = (void*)(dynamicTab[j].d_val);\r
+                       hdr->misc.SymTable = dynamicTab[j].d_val;       // Saved in unused bytes of ident\r
+                       break;\r
+               // --- String Table ---\r
+               case DT_STRTAB:\r
+                       DEBUGS(" elf_relocate: DYNAMIC String Table 0x%x (0x%x)\n",\r
+                               dynamicTab[j].d_val, dynamicTab[j].d_val + iBaseDiff);\r
+                       dynamicTab[j].d_val += iBaseDiff;\r
+                       dynstrtab = (void*)(dynamicTab[j].d_val);\r
+                       break;\r
+               // --- Hash Table --\r
+               case DT_HASH:\r
+                       dynamicTab[j].d_val += iBaseDiff;\r
+                       iSymCount = ((Uint*)(dynamicTab[j].d_val))[1];\r
+                       hdr->misc.HashTable = dynamicTab[j].d_val;      // Saved in unused bytes of ident\r
+                       break;
+               }
+       }
+\r
+       if(dynsymtab == NULL) {\r
+               SysDebug("ld-acess.so - WARNING: No Dynamic Symbol table, returning\n");\r
+               return hdr->entrypoint;\r
+       }\r
+       \r
+       // Alter Symbols to true base\r
+       for(i=0;i<iSymCount;i++)\r
+       {\r
+               dynsymtab[i].value += iBaseDiff;
+               dynsymtab[i].nameOfs += (Uint)dynstrtab;\r
+               //DEBUGS("elf_relocate: Sym '%s' = 0x%x (relocated)\n", dynsymtab[i].name, dynsymtab[i].value);\r
+       }\r
+       
+       // === Add to loaded list (can be imported now) ===
+       AddLoaded( Filename, (Uint)Base );
+
+       // === Parse Relocation Data ===\r
+       DEBUGS(" elf_relocate: dynamicTab = 0x%x\n", dynamicTab);\r
+       for( j = 0; dynamicTab[j].d_tag != DT_NULL; j++)\r
+       {\r
+               switch(dynamicTab[j].d_tag)\r
+               {\r
+               // --- Shared Library Name ---\r
+               case DT_SONAME:\r
+                       DEBUGS(" elf_relocate: .so Name '%s'\n", dynstrtab+dynamicTab[j].d_val);\r
+                       break;\r
+               // --- Needed Library ---\r
+               case DT_NEEDED:\r
+                       libPath = dynstrtab + dynamicTab[j].d_val;\r
+                       DEBUGS(" Required Library '%s'\n", libPath);\r
+                       if(LoadLibrary(libPath, NULL, envp) == 0) {\r
+                               #if DEBUG\r
+                                       DEBUGS(" elf_relocate: Unable to load '%s'\n", libPath);\r
+                               #else\r
+                               SysDebug("Unable to load required library '%s'\n", libPath);\r
+                               #endif\r
+                               return 0;\r
+                       }\r
+                       break;\r
+               // --- PLT/GOT ---\r
+               case DT_PLTGOT: pltgot = (void*)iBaseDiff+(dynamicTab[j].d_val);        break;\r
+               case DT_JMPREL: plt = (void*)(iBaseDiff+dynamicTab[j].d_val);   break;\r
+               case DT_PLTREL: pltType = dynamicTab[j].d_val;  break;\r
+               case DT_PLTRELSZ:       pltSz = dynamicTab[j].d_val;    break;\r
+               \r
+               // --- Relocation ---\r
+               case DT_REL:    rel = (void*)(iBaseDiff + dynamicTab[j].d_val); break;\r
+               case DT_RELSZ:  relSz = dynamicTab[j].d_val;    break;\r
+               case DT_RELENT: relEntSz = dynamicTab[j].d_val; break;\r
+               case DT_RELA:   rela = (void*)(iBaseDiff + dynamicTab[j].d_val);        break;\r
+               case DT_RELASZ: relaSz = dynamicTab[j].d_val;   break;\r
+               case DT_RELAENT:        relaEntSz = dynamicTab[j].d_val;        break;\r
+               \r
+               // --- Symbol Table ---\r
+               case DT_SYMTAB:\r
+               // --- Hash Table ---\r
+               case DT_HASH:\r
+               // --- String Table ---\r
+               case DT_STRTAB:\r
+                       break;\r
+               \r
+               // --- Unknown ---\r
+               default:\r
+                       if(dynamicTab[j].d_tag > DT_JMPREL)     continue;\r
+                       //DEBUGS(" elf_relocate: %i-%i = %s,0x%x\n",\r
+                       //      i,j, csaDT_NAMES[dynamicTab[j].d_tag],dynamicTab[j].d_val);\r
+                       break;\r
+               }\r
+       }\r
+       \r
+       DEBUGS(" elf_relocate: Beginning Relocation\n");\r
+       \r
+       // Parse Relocation Entries\r
+       if(rel && relSz)\r
+       {\r
+               Uint32  *ptr;\r
+               DEBUGS(" elf_relocate: rel=0x%x, relSz=0x%x, relEntSz=0x%x\n", rel, relSz, relEntSz);\r
+               j = relSz / relEntSz;\r
+               for( i = 0; i < j; i++ )\r
+               {
+                       //DEBUGS("  Rel %i: 0x%x+0x%x\n", i, iBaseDiff, rel[i].r_offset);\r
+                       ptr = (void*)(iBaseDiff + rel[i].r_offset);\r
+                       elf_doRelocate(rel[i].r_info, ptr, *ptr, dynsymtab, iBaseDiff);\r
+               }\r
+       }\r
+       // Parse Relocation Entries\r
+       if(rela && relaSz)\r
+       {\r
+               Uint32  *ptr;\r
+               DEBUGS(" elf_relocate: rela=0x%x, relaSz=0x%x, relaEntSz=0x%x\n", rela, relaSz, relaEntSz);\r
+               j = relaSz / relaEntSz;\r
+               for( i = 0; i < j; i++ )\r
+               {\r
+                       ptr = (void*)(iBaseDiff + rela[i].r_offset);\r
+                       elf_doRelocate(rel[i].r_info, ptr, rela[i].r_addend, dynsymtab, iBaseDiff);\r
+               }\r
+       }\r
+       \r
+       // === Process PLT (Procedure Linkage Table) ===\r
+       if(plt && pltSz)\r
+       {\r
+               Uint32  *ptr;\r
+               DEBUGS(" elf_relocate: Relocate PLT, plt=0x%x\n", plt);\r
+               if(pltType == DT_REL)\r
+               {\r
+                       Elf32_Rel       *pltRel = plt;\r
+                       j = pltSz / sizeof(Elf32_Rel);\r
+                       DEBUGS(" elf_relocate: PLT Reloc Type = Rel, %i entries\n", j);\r
+                       for(i=0;i<j;i++)\r
+                       {\r
+                               ptr = (void*)(iBaseDiff + pltRel[i].r_offset);\r
+                               elf_doRelocate(pltRel[i].r_info, ptr, *ptr, dynsymtab, iRealBase);\r
+                       }\r
+               }\r
+               else\r
+               {\r
+                       Elf32_Rela      *pltRela = plt;\r
+                       j = pltSz / sizeof(Elf32_Rela);\r
+                       DEBUGS(" elf_relocate: PLT Reloc Type = Rela, %i entries\n", j);\r
+                       for(i=0;i<j;i++)\r
+                       {\r
+                               ptr = (void*)(iRealBase + pltRela[i].r_offset);\r
+                               elf_doRelocate(pltRela[i].r_info, ptr, pltRela[i].r_addend, dynsymtab, iRealBase);\r
+                       }\r
+               }\r
+       }\r
+       \r
+       DEBUGS("ElfRelocate: RETURN 0x%x", hdr->entrypoint);\r
+       return hdr->entrypoint;\r
+}\r
+\r
+void elf_doRelocate(Uint r_info, Uint32 *ptr, Uint32 addend, Elf32_Sym *symtab, Uint base)\r
+{\r
+        int    type = ELF32_R_TYPE(r_info);\r
+        int    sym = ELF32_R_SYM(r_info);\r
+       Uint32  val;\r
+       switch( type )\r
+       {\r
+       // Standard 32 Bit Relocation (S+A)\r
+       case R_386_32:\r
+               val = GetSymbol( symtab[sym].name );\r
+               DEBUGS(" elf_doRelocate: R_386_32 *0x%x += 0x%x('%s')\n",\r
+                       ptr, val, symtab[sym].name);\r
+               *ptr = val + addend;\r
+               break;\r
+               \r
+       // 32 Bit Relocation wrt. Offset (S+A-P)\r
+       case R_386_PC32:\r
+               DEBUGS(" elf_doRelocate: #%i: '%s'\n", sym, symtab[sym].name);\r
+               val = GetSymbol( symtab[sym].name );\r
+               DEBUGS(" elf_doRelocate: R_386_PC32 *0x%x = 0x%x + 0x%x - 0x%x\n",\r
+                       ptr, *ptr, val, (Uint)ptr );\r
+               *ptr = val + addend - (Uint)ptr;\r
+               //*ptr = val + addend - ((Uint)ptr - base);\r
+               break;
+\r
+       // Absolute Value of a symbol (S)\r
+       case R_386_GLOB_DAT:\r
+       case R_386_JMP_SLOT:
+               DEBUGS(" elf_doRelocate: #%i: '%s'\n", sym, symtab[sym].name);\r
+               val = GetSymbol( symtab[sym].name );\r
+               DEBUGS(" elf_doRelocate: %s *0x%x = 0x%x\n", csaR_NAMES[type], ptr, val);\r
+               *ptr = val;\r
+               break;\r
+\r
+       // Base Address (B+A)\r
+       case R_386_RELATIVE:\r
+               DEBUGS(" elf_doRelocate: R_386_RELATIVE *0x%x = 0x%x + 0x%x\n", ptr, base, addend);\r
+               *ptr = base + addend;\r
+               break;\r
+               \r
+       default:\r
+               DEBUGS(" elf_doRelocate: Rel 0x%x: 0x%x,%s\n", ptr, sym, csaR_NAMES[type]);\r
+               break;\r
+       }\r
+       \r
+}
+\r
+/**\r
+ * \fn int ElfGetSymbol(Uint Base, char *name, Uint *ret)\r
+ */
+int ElfGetSymbol(Uint Base, char *Name, Uint *ret)
+{
+       Elf32_Ehdr      *hdr = (void*)Base;\r
+       Elf32_Sym       *symtab;
+        int    nbuckets = 0;\r
+        int    iSymCount = 0;
+        int    i;\r
+       Uint    *pBuckets;\r
+       Uint    *pChains;\r
+       Uint    iNameHash;
+\r
+       //DEBUGS("ElfGetSymbol: (Base=0x%x, Name='%s')\n", Base, Name);\r
+\r
+       pBuckets = (void *) hdr->misc.HashTable;\r
+       symtab = (void *) hdr->misc.SymTable;\r
+       
+       nbuckets = pBuckets[0];\r
+       iSymCount = pBuckets[1];\r
+       pBuckets = &pBuckets[2];\r
+       pChains = &pBuckets[ nbuckets ];\r
+       \r
+       // Get hash
+       iNameHash = ElfHashString(Name);\r
+       iNameHash %= nbuckets;\r
+       //DEBUGS(" ElfGetSymbol: iNameHash = 0x%x\n", iNameHash);\r
+
+       // Walk Chain\r
+       i = pBuckets[ iNameHash ];\r
+       //DEBUGS(" ElfGetSymbol: strcmp(Name, \"%s\")\n", symtab[i].name);\r
+       if(symtab[i].shndx != SHN_UNDEF && strcmp(symtab[i].name, Name) == 0) {\r
+               *ret = symtab[ i ].value;\r
+               return 1;\r
+       }\r
+       \r
+       //DEBUGS(" ElfGetSymbol: Hash of first = 0x%x\n", ElfHashString( symtab[i].name ) % nbuckets);\r
+       while(pChains[i] != STN_UNDEF)\r
+       {\r
+               //DEBUGS(" pChains[%i] = %i\n", i, pChains[i]);\r
+               i = pChains[i];\r
+               //DEBUGS(" ElfGetSymbol: strcmp(Name, \"%s\")\n", symtab[ i ].name);\r
+               if(symtab[i].shndx != SHN_UNDEF && strcmp(symtab[ i ].name, Name) == 0) {\r
+                       //DEBUGS("ElfGetSymbol: RETURN 1, '%s' = 0x%x\n", symtab[ i ].name, symtab[ i ].value);\r
+                       *ret = symtab[ i ].value;\r
+                       return 1;\r
+               }\r
+       }\r
+       \r
+       //DEBUGS("ElfGetSymbol: RETURN 0, Symbol '%s' not found\n", Name);
+       return 0;
+}\r
+\r
+Uint ElfHashString(char *name)\r
+{\r
+       Uint    h = 0, g;\r
+       while(*name)\r
+       {\r
+               h = (h << 4) + *name++;\r
+               if( (g = h & 0xf0000000) )\r
+                       h ^= g >> 24;\r
+               h &= ~g;\r
+       }\r
+       return h;\r
+}\r
+\r
+#if 0\r
+unsigned long elf_hash(const unsigned char *name)\r
+{\r
+       unsigned long   h = 0, g;\r
+       while (*name)\r
+       {\r
+               h = (h << 4) + *name++;\r
+               if (g = h & 0xf0000000)\r
+                       h ^= g >> 24;\r
+               h &= ~g;\r
+       }\r
+       return h;\r
+}\r
+#endif\r
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 (file)
index 0000000..eededf4
--- /dev/null
@@ -0,0 +1,215 @@
+/**\r
+ Acess v1\r
+ \file elf32.h\r
+ \brief ELF Exeutable Loader\r
+*/\r
+#ifndef _ELF32_H\r
+#define _ELF32_H\r
+\r
+/**\r
+ \struct elf_header_s\r
+ \brief ELF File Header\r
+*/\r
+struct sElf32_Ehdr {\r
+       union {\r
+               char    ident[16];      //!< Identifier Bytes\r
+               struct {\r
+                       Uint    Ident1;\r
+                       Uint    Ident2;\r
+                       Uint    HashTable;\r
+                       Uint    SymTable;\r
+               } misc;\r
+       };\r
+       Uint16  filetype;       //!< File Type\r
+       Uint16  machine;        //!< Machine / Arch\r
+       Uint32  version;        //!< Version (File?)\r
+       Uint32  entrypoint;     //!< Entry Point\r
+       Uint32  phoff;  //!< Program Header Offset\r
+       Uint32  shoff;  //!< Section Header Offset\r
+       Uint32  flags;  //!< Flags\r
+       Uint16  headersize;     //!< Header Size\r
+       Uint16  phentsize;      //!< Program Header Entry Size\r
+       Uint16  phentcount;     //!< Program Header Entry Count\r
+       Uint16  shentsize;      //!< Section Header Entry Size\r
+       Uint16  shentcount;     //!< Section Header Entry Count\r
+       Uint16  shstrindex;     //!< Section Header String Table Index\r
+};\r
+\r
+/**\r
+ \name Executable Types\r
+ \{\r
+*/\r
+#define        ET_NONE         0       //!< NULL Type\r
+#define        ET_REL          1       //!< Relocatable (Object)\r
+#define ET_EXEC                2       //!< Executable\r
+#define ET_DYN         3       //!< Dynamic Library\r
+#define ET_CORE                4       //!< Core?\r
+#define ET_LOPROC      0xFF00  //!< Low Impl Defined\r
+#define ET_HIPROC      0xFFFF  //!< High Impl Defined\r
+//! \}\r
+\r
+/**\r
+ \name Section IDs\r
+ \{\r
+*/\r
+#define SHN_UNDEF              0       //!< Undefined Section\r
+#define SHN_LORESERVE  0xFF00  //!< Low Reserved\r
+#define SHN_LOPROC             0xFF00  //!< Low Impl Defined\r
+#define SHN_HIPROC             0xFF1F  //!< High Impl Defined\r
+#define SHN_ABS                        0xFFF1  //!< Absolute Address (Base: 0, Size: -1)\r
+#define SHN_COMMON             0xFFF2  //!< Common\r
+#define SHN_HIRESERVE  0xFFFF  //!< High Reserved\r
+//! \}\r
+\r
+/**\r
+ \enum eElfSectionTypes\r
+ \brief ELF Section Types\r
+*/\r
+enum eElfSectionTypes {\r
+       SHT_NULL,       //0\r
+       SHT_PROGBITS,   //1\r
+       SHT_SYMTAB,     //2\r
+       SHT_STRTAB,     //3\r
+       SHT_RELA,       //4\r
+       SHT_HASH,       //5\r
+       SHT_DYNAMIC,    //6\r
+       SHT_NOTE,       //7\r
+       SHT_NOBITS,     //8\r
+       SHT_REL,        //9\r
+       SHT_SHLIB,      //A\r
+       SHT_DYNSYM,     //B\r
+       SHT_LAST,       //C\r
+       SHT_LOPROC = 0x70000000,\r
+       SHT_HIPROC = 0x7fffffff,\r
+       SHT_LOUSER = 0x80000000,\r
+       SHT_HIUSER = 0xffffffff\r
+};\r
+\r
+#define SHF_WRITE      0x1\r
+#define SHF_ALLOC      0x2\r
+#define SHF_EXECINSTR  0x4\r
+#define SHF_MASKPROC   0xf0000000\r
+\r
+struct sElf32_Shent {\r
+       Uint32  name;\r
+       Uint32  type;\r
+       Uint32  flags;\r
+       Uint32  address;\r
+       Uint32  offset;\r
+       Uint32  size;\r
+       Uint32  link;\r
+       Uint32  info;\r
+       Uint32  addralign;\r
+       Uint32  entsize;\r
+};     //sizeof = 40\r
+\r
+struct elf_sym_s {
+       union {\r
+               Uint32  nameOfs;
+               char    *name;
+       };\r
+       Uint32  value;  //Address\r
+       Uint32  size;\r
+       Uint8   info;\r
+       Uint8   other;\r
+       Uint16  shndx;\r
+};\r
+#define        STN_UNDEF       0       // Undefined Symbol\r
+\r
+enum {\r
+       PT_NULL,        //0\r
+       PT_LOAD,        //1\r
+       PT_DYNAMIC,     //2\r
+       PT_INTERP,      //3\r
+       PT_NOTE,        //4\r
+       PT_SHLIB,       //5\r
+       PT_PHDR,        //6\r
+       PT_LOPROC = 0x70000000,\r
+       PT_HIPROC = 0x7fffffff\r
+};\r
+\r
+struct sElf32_Phdr {\r
+       Uint32  Type;\r
+       Uint    Offset;\r
+       Uint    VAddr;\r
+       Uint    PAddr;\r
+       Uint32  FileSize;\r
+       Uint32  MemSize;\r
+       Uint32  Flags;\r
+       Uint32  Align;\r
+};\r
+\r
+struct elf32_rel_s {\r
+       Uint32  r_offset;\r
+       Uint32  r_info;\r
+};\r
+\r
+struct elf32_rela_s {\r
+       Uint32  r_offset;\r
+       Uint32  r_info;\r
+       Sint32  r_addend;\r
+};\r
+\r
+enum {\r
+       R_386_NONE=0,   // none\r
+       R_386_32,       // S+A\r
+       R_386_PC32,     // S+A-P\r
+       R_386_GOT32,    // G+A-P\r
+       R_386_PLT32,    // L+A-P\r
+       R_386_COPY,     // none\r
+       R_386_GLOB_DAT, // S\r
+       R_386_JMP_SLOT, // S\r
+       R_386_RELATIVE, // B+A\r
+       R_386_GOTOFF,   // S+A-GOT\r
+       R_386_GOTPC,    // GOT+A-P\r
+       R_386_LAST      // none\r
+};\r
+\r
+#define        ELF32_R_SYM(i)  ((i)>>8)        // Takes an info value and returns a symbol index\r
+#define        ELF32_R_TYPE(i) ((i)&0xFF)      // Takes an info value and returns a type\r
+#define        ELF32_R_INFO(s,t)       (((s)<<8)+((t)&0xFF))   // Takes a type and symbol index and returns an info value\r
+\r
+struct elf32_dyn_s {\r
+       Uint16  d_tag;\r
+       Uint32  d_val;  //Also d_ptr\r
+};\r
+\r
+enum {\r
+       DT_NULL,        //!< Marks End of list\r
+       DT_NEEDED,      //!< Offset in strtab to needed library\r
+       DT_PLTRELSZ,    //!< Size in bytes of PLT\r
+       DT_PLTGOT,      //!< Address of PLT/GOT\r
+       DT_HASH,        //!< Address of symbol hash table\r
+       DT_STRTAB,      //!< String Table address\r
+       DT_SYMTAB,      //!< Symbol Table address\r
+       DT_RELA,        //!< Relocation table address\r
+       DT_RELASZ,      //!< Size of relocation table\r
+       DT_RELAENT,     //!< Size of entry in relocation table\r
+       DT_STRSZ,       //!< Size of string table\r
+       DT_SYMENT,      //!< Size of symbol table entry\r
+       DT_INIT,        //!< Address of initialisation function\r
+       DT_FINI,        //!< Address of termination function\r
+       DT_SONAME,      //!< String table offset of so name\r
+       DT_RPATH,       //!< String table offset of library path\r
+       DT_SYMBOLIC,//!< Reverse order of symbol searching for library, search libs first then executable\r
+       DT_REL,         //!< Relocation Entries (Elf32_Rel instead of Elf32_Rela)\r
+       DT_RELSZ,       //!< Size of above table (bytes)\r
+       DT_RELENT,      //!< Size of entry in above table\r
+       DT_PLTREL,      //!< Relocation entry of PLT\r
+       DT_DEBUG,       //!< Debugging Entry - Unknown contents\r
+       DT_TEXTREL,     //!< Indicates that modifcations to a non-writeable segment may occur\r
+       DT_JMPREL,      //!< Address of PLT only relocation entries\r
+       DT_LOPROC = 0x70000000, //!< Low Definable\r
+       DT_HIPROC = 0x7FFFFFFF  //!< High Definable\r
+};\r
+\r
+typedef struct sElf32_Ehdr     Elf32_Ehdr;\r
+typedef struct sElf32_Phdr     Elf32_Phdr;\r
+typedef struct sElf32_Shent    Elf32_Shent;\r
+typedef struct elf_sym_s       elf_symtab;\r
+typedef struct elf_sym_s       Elf32_Sym;\r
+typedef struct elf32_rel_s     Elf32_Rel;\r
+typedef struct elf32_rela_s    Elf32_Rela;\r
+typedef struct elf32_dyn_s     Elf32_Dyn;\r
+\r
+#endif // defined(_EXE_ELF_H)\r
diff --git a/Usermode/Libraries/ld-acess.old.so_src/helpers.asm b/Usermode/Libraries/ld-acess.old.so_src/helpers.asm
new file mode 100644 (file)
index 0000000..49b9bd8
--- /dev/null
@@ -0,0 +1,88 @@
+; AcessOS 1\r
+; By thePowersGang\r
+; LD-ACESS.SO\r
+; - helpers.asm\r
+\r
+%include "../libacess.so_src/syscalls.inc.asm"\r
+\r
+[global _SysDebug]\r
+[global _SysExit]\r
+[global _SysLoadBin]\r
+[global _SysUnloadBin]\r
+[global _SysSetFaultHandler]\r
+[global _open]\r
+[global _close]\r
+\r
+; void SysDebugV(char *fmt, va_list Args)\r
+_SysDebug:\r
+       ;xchg bx, bx\r
+       push ebp\r
+       mov ebp, esp\r
+       pusha\r
+       \r
+       mov eax, 0x100  ; User Debug\r
+       mov ebx, [ebp+8]        ; Format\r
+       mov ecx, [ebp+12]       ; Arguments\r
+       mov edx, [ebp+16]       ; Arguments\r
+       mov edi, [ebp+20]       ; Arguments\r
+       mov esi, [ebp+24]       ; Arguments\r
+       int     0xAC\r
+       \r
+       popa\r
+       pop ebp\r
+       ret\r
+\r
+; void SysExit()\r
+_SysExit:\r
+       push ebx\r
+       mov eax, SYS_EXIT       ; Exit\r
+       mov ebx, [esp+0x8]      ; Exit Code\r
+       int     0xAC\r
+       pop ebx\r
+       ret\r
+\r
+; Uint SysLoadBin(char *path, Uint *entry)\r
+_SysLoadBin:\r
+       push ebx\r
+       mov eax, SYS_LOADBIN    ; SYS_LDBIN\r
+       mov ebx, [esp+0x8]      ; Path\r
+       mov ecx, [esp+0xC]      ; Entry\r
+       int     0xAC\r
+       pop ebx\r
+       ret\r
+\r
+; Uint SysUnloadBin(Uint Base)\r
+_SysUnloadBin:\r
+       push ebx\r
+       mov eax, SYS_UNLOADBIN  ; SYS_ULDBIN\r
+       mov ebx, [esp+0x8]      ; Base\r
+       int     0xAC\r
+       pop ebx\r
+       ret\r
+\r
+; int close(char *name, int flags)\r
+_open:\r
+       push ebx\r
+       mov eax, SYS_OPEN\r
+       mov ebx, [esp+0x8]      ; Filename\r
+       mov ecx, [esp+0xC]      ; Flags\r
+       int 0xAC\r
+       pop ebx\r
+       ret\r
+\r
+; void close(int fd)\r
+_close:\r
+       push ebx\r
+       mov eax, SYS_CLOSE\r
+       mov ebx, [esp+0x8]      ; File Descriptor\r
+       int 0xAC\r
+       pop ebx\r
+       ret\r
+\r
+_SysSetFaultHandler:\r
+       push ebx\r
+       mov eax, SYS_SETFAULTHANDLER\r
+       mov ebx, [esp+0x8]      ; File Descriptor\r
+       int 0xAC\r
+       pop ebx\r
+       ret\r
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 (file)
index 0000000..3acc129
--- /dev/null
@@ -0,0 +1,62 @@
+/*\r
+ AcessOS 1\r
+ Dynamic Loader\r
+ By thePowersGang\r
+*/\r
+#include "common.h"\r
+\r
+// === CODE ===\r
+char *strcpy(char *dest, const char *src)\r
+{\r
+       char    *ret = dest;\r
+       while(*src) {\r
+               *dest = *src;\r
+               src ++; dest ++;\r
+       }\r
+       *dest = '\0';\r
+       return ret;\r
+}\r
+\r
+char *strcat(char *dest, const char *src)\r
+{\r
+       char    *ret = dest;\r
+       while(*dest)    dest++;\r
+       while(*src)             *dest++ = *src++;\r
+       *dest = '\0';\r
+       return ret;\r
+}
+\r
+/**\r
+ * \fn int strcmp(const char *s1, const char *s2)\r
+ * \brief Compare two strings\r
+ */
+int strcmp(const char *s1, const char *s2)
+{
+       while(*s1 && *s1 == *s2) s1++,s2++;
+       return *s1-*s2;
+}\r
+\r
+/**\r
+ * \fn int strlen(const char *str)\r
+ * \brief \r
+ */\r
+int strlen(const char *str)\r
+{\r
+        int    len = 0;\r
+       while(*str)     len++,str++;\r
+       return len;\r
+}
+\r
+/**\r
+ * \fn int file_exists(char *filename)\r
+ * \brief Checks if a file exists\r
+ */
+int file_exists(char *filename)\r
+{\r
+        int    fd;\r
+        //fd = open(filename, OPENFLAG_READ);\r
+        fd = open(filename, 0);\r
+        if(fd == -1)   return 0;\r
+        close(fd);\r
+        return 1;\r
+}\r
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 (file)
index 0000000..8ddc234
--- /dev/null
@@ -0,0 +1,27 @@
+ENTRY(_SoMain)\r
+OUTPUT_FORMAT(elf32-i386)\r
+\r
+SECTIONS {\r
+    . = 0xBBFF0000;\r
+       _gLinkedBase = .;\r
+\r
+   .text : AT(ADDR(.text)) {\r
+        code = .;\r
+        *(.text)\r
+        *(.rodata*)\r
+   }\r
+\r
+   .data ALIGN (0x1000) : AT(ADDR(.data)) {\r
+        data = .;\r
+        *(.data)\r
+   }\r
+\r
+   .bss ALIGN (0x1000) : AT(ADDR(.bss)) {\r
+        _sbss = .;\r
+        *(COMMON)\r
+        *(.bss)\r
+        _ebss = .;\r
+        bss = .;\r
+   }\r
+   _end = .;\r
+}
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 (file)
index 0000000..e5090dd
--- /dev/null
@@ -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;i<sizeof(caLocalExports)/sizeof(caLocalExports[0]);i++)
+       {
+               if( strcmp(caLocalExports[i].Name, name) == 0 )
+                       return caLocalExports[i].Value;
+       }
+       
+       for(i=0;i<sizeof(gLoadedLibraries)/sizeof(gLoadedLibraries[0]);i++)
+       {
+               if(gLoadedLibraries[i].Base == 0)       break;
+               
+               //SysDebug(" GetSymbol: Trying 0x%x, '%s'\n",
+               //      gLoadedLibraries[i].Base, gLoadedLibraries[i].Name);
+               if(GetSymbolFromBase(gLoadedLibraries[i].Base, name, &ret))     return ret;
+       }
+       SysDebug("GetSymbol: === Symbol '%s' not found ===\n", name);
+       return 0;
+}
+
+/**
+ \fn int GetSymbolFromBase(Uint base, char *name, Uint *ret)
+ \breif Gets a symbol from a specified library
+*/
+int GetSymbolFromBase(Uint base, char *name, Uint *ret)
+{
+       if(*(Uint32*)base == (0x7F|('E'<<8)|('L'<<16)|('F'<<24)))
+               return ElfGetSymbol(base, name, ret);
+       if(*(Uint16*)base == ('M'|('Z'<<8)))
+               return PE_GetSymbol(base, name, ret);
+       return 0;
+}
+
diff --git a/Usermode/Libraries/ld-acess.old.so_src/main.c b/Usermode/Libraries/ld-acess.old.so_src/main.c
new file mode 100644 (file)
index 0000000..678dc9a
--- /dev/null
@@ -0,0 +1,86 @@
+/*\r
+ AcessOS 1 - Dynamic Loader\r
+ By thePowersGang\r
+*/\r
+#include "common.h"\r
+\r
+// === PROTOTYPES ===\r
+ int   DoRelocate( Uint base, char **envp, char *Filename );\r
+ int   CallUser(Uint entry, Uint SP);\r
+ int   ElfRelocate(void *Base, char *envp[], char *Filename);\r
+ int   PE_Relocate(void *Base, char *envp[], char *Filename);\r
+\r
+// === Imports ===\r
+extern void    gLinkedBase;\r
\r
+// === CODE ===\r
+/**\r
+ \fn int SoMain(Uint base, int argc, char *argv[], char **envp)\r
+ \brief Library entry point\r
+ \note This is the entrypoint for the library\r
+*/\r
+int SoMain(Uint base, int arg1)\r
+{\r
+        int    ret;\r
+        \r
+       // - Assume that the file pointer will be less than 4096\r
+       if(base < 0x1000) {\r
+               SysDebug("ld-acess - SoMain: Passed file pointer %i\n", base);\r
+               SysExit();\r
+               for(;;);\r
+       }\r
+       // Check if we are being called directly\r
+       if(base == (Uint)&gLinkedBase) {\r
+               SysDebug("ld-acess should not be directly called\n");\r
+               SysExit();\r
+               for(;;);\r
+       }\r
+       \r
+       // Otherwise do relocations\r
+       //ret = DoRelocate( base, envp, "Executable" );\r
+       ret = DoRelocate( base, NULL, "Executable" );\r
+       if( ret == 0 ) {\r
+               SysDebug("ld-acess - SoMain: Relocate failed, base=0x%x\n", base);\r
+               SysExit();\r
+               for(;;);\r
+       }\r
+       \r
+       // And call user\r
+       //SysDebug("Calling User at 0x%x\n", ret);\r
+       CallUser( ret, (Uint)&arg1 );\r
+       \r
+       return 0;\r
+}\r
+\r
+/**\r
+ \fn int DoRelocate(Uint base, char **envp)\r
+ \brief Relocates an in-memory image\r
+*/\r
+int DoRelocate( Uint base, char **envp, char *Filename )\r
+{\r
+       // Load Executable\r
+       if(*(Uint*)base == (0x7F|('E'<<8)|('L'<<16)|('F'<<24)))\r
+               return ElfRelocate((void*)base, envp, Filename);\r
+       if(*(Uint16*)base == ('M'|('Z'<<8)))\r
+               return PE_Relocate((void*)base, envp, Filename);\r
+       \r
+       SysDebug("ld-acess - DoRelocate: Unkown file format '0x%x 0x%x 0x%x 0x%x'\n",\r
+               *(Uint8*)(base), *(Uint8*)(base+1), *(Uint8*)(base+2), *(Uint8*)(base+3) );\r
+       SysDebug("ld-acess - DoRelocate: File '%s'\n", Filename);\r
+       SysExit();\r
+       for(;;);\r
+}\r
+\r
+/**\r
+ \fn int CallUser(Uint entry, Uint sp)\r
+*/\r
+int CallUser(Uint entry, Uint sp)\r
+{\r
+       //SysDebug("CallUser: (entry=0x%x, sp=0x%x)", entry, sp);\r
+       *(Uint*)(sp-4) = 0;     // Clear return address\r
+       __asm__ __volatile__ (\r
+       "mov %%eax, %%esp;\n\t"\r
+       "jmp *%%ecx"\r
+       : : "a"(sp), "c"(entry));\r
+       for(;;);\r
+}\r
diff --git a/Usermode/Libraries/ld-acess.old.so_src/pe.c b/Usermode/Libraries/ld-acess.old.so_src/pe.c
new file mode 100644 (file)
index 0000000..ec3b6b8
--- /dev/null
@@ -0,0 +1,180 @@
+/*\r
+ * Acess v1\r
+ * Portable Executable Loader\r
+ */\r
+#include "common.h"\r
+#include "pe.h"\r
+\r
+#define PE_DEBUG       0\r
+#define DEBUG  (ACESS_DEBUG|PE_DEBUG)\r
+\r
+//#define DLL_BASE_PATH        "/Acess/Windows/"\r
+#define DLL_BASE_PATH  NULL\r
+\r
+#if DEBUG\r
+# define DEBUGS(v...)  SysDebug(v)\r
+#else\r
+# define DEBUGS(v...)\r
+#endif\r
+\r
+// === PROTOTYPES ===\r
+ int   PE_Relocate(void *Base, char **envp, char *Filename);\r
+char   *PE_int_GetTrueFile(char *file);\r
+ int   PE_int_GetForwardSymbol(char *Fwd, Uint *Value);\r
+\r
+// === CODE ===\r
+int PE_Relocate(void *Base, char *envp[], char *Filename)\r
+{\r
+       tPE_DOS_HEADER          *dosHdr = Base;\r
+       tPE_IMAGE_HEADERS       *peHeaders;\r
+       tPE_DATA_DIR    *directory;\r
+       tPE_IMPORT_DIR  *impDir;\r
+       tPE_HINT_NAME   *name;\r
+       Uint32  *importTab, *aIAT;\r
+        int    i, j;\r
+       Uint    iBase = (Uint)Base;\r
+       Uint    iLibBase;\r
+       \r
+       DEBUGS("PE_Relocate: (Base=0x%x)\n", Base);\r
+       \r
+       peHeaders = Base + dosHdr->PeHdrOffs;\r
+       \r
+       directory = peHeaders->OptHeader.Directory;\r
+       \r
+       // === Load Import Tables\r
+       impDir = Base + directory[PE_DIR_IMPORT].RVA;\r
+       for( i = 0; impDir[i].DLLName != NULL; i++ )\r
+       {\r
+               impDir[i].DLLName += iBase;\r
+               impDir[i].ImportLookupTable += iBase/4;\r
+               impDir[i].ImportAddressTable += iBase/4;\r
+               DEBUGS(" PE_Relocate: DLL Required '%s'(0x%x)\n", impDir[i].DLLName, impDir[i].DLLName);\r
+               iLibBase = LoadLibrary(PE_int_GetTrueFile(impDir[i].DLLName), DLL_BASE_PATH, envp);\r
+               if(iLibBase == 0) {\r
+                       SysDebug("Unable to load required library '%s'\n", impDir[i].DLLName);\r
+                       return 0;\r
+               }\r
+               DEBUGS(" PE_Relocate: Loaded as 0x%x\n", iLibBase);\r
+               importTab = impDir[i].ImportLookupTable;\r
+               aIAT = impDir[i].ImportAddressTable;\r
+               for( j = 0; importTab[j] != 0; j++ )\r
+               {\r
+                       if( importTab[j] & 0x80000000 )\r
+                               DEBUGS(" PE_Relocate: Import Ordinal %i\n", importTab[j] & 0x7FFFFFFF);\r
+                       else\r
+                       {\r
+                               name = (void*)( iBase + importTab[j] );\r
+                               DEBUGS(" PE_Relocate: Import Name '%s', Hint 0x%x\n", name->Name, name->Hint);\r
+                               if( GetSymbolFromBase(iLibBase, name->Name, (Uint*)&aIAT[j]) == 0 ) {\r
+                                       SysDebug("Unable to find symbol '%s' in library '%s'\n", name->Name, impDir[i].DLLName);\r
+                                       return 0;\r
+                               }\r
+                       }\r
+               }\r
+       }\r
+       \r
+       #if DEBUG\r
+       for(i=0;i<PE_DIR_LAST;i++) {\r
+               if(directory[i].RVA == 0)       continue;\r
+               SysDebug("directory[%i] = {RVA=0x%x,Size=0x%x}\n", i, directory[i].RVA, directory[i].Size);\r
+       }\r
+       #endif\r
+       \r
+       DEBUGS("PE_Relocate: RETURN 0x%x\n", iBase + peHeaders->OptHeader.EntryPoint);\r
+       \r
+       return iBase + peHeaders->OptHeader.EntryPoint;\r
+}\r
+\r
+/**\r
+ * \fn int PE_GetSymbol(Uint Base, char *Name, Uint *Ret)\r
+ */\r
+int PE_GetSymbol(Uint Base, char *Name, Uint *Ret)\r
+{\r
+       tPE_DOS_HEADER          *dosHdr = (void*)Base;\r
+       tPE_IMAGE_HEADERS       *peHeaders;\r
+       tPE_DATA_DIR    *directory;\r
+       tPE_EXPORT_DIR  *expDir;\r
+       Uint32  *nameTable, *addrTable;\r
+       Uint16  *ordTable;\r
+        int    i;\r
+        int    symbolCount;\r
+       char    *name;\r
+       Uint    retVal;\r
+       Uint    expLen;\r
+       \r
+       peHeaders = (void*)( Base + dosHdr->PeHdrOffs );\r
+       directory = peHeaders->OptHeader.Directory;\r
+       \r
+       expDir = (void*)( Base + directory[PE_DIR_EXPORT].RVA );\r
+       expLen = directory[PE_DIR_EXPORT].Size;\r
+       symbolCount = expDir->NumNamePointers;\r
+       nameTable = (void*)( Base + expDir->NamePointerRVA );\r
+       ordTable = (void*)( Base + expDir->OrdinalTableRVA );\r
+       addrTable = (void*)( Base + expDir->AddressRVA );\r
+       //DEBUGS(" PE_GetSymbol: %i Symbols\n", symbolCount);\r
+       for(i=0;i<symbolCount;i++)\r
+       {\r
+               name = (char*)( Base + nameTable[i] );\r
+               //DEBUGS(" PE_GetSymbol: '%s' = 0x%x\n", name, Base + addrTable[ ordTable[i] ]);\r
+               if(strcmp(name, Name) == 0)\r
+               {\r
+                       retVal = Base + addrTable[ ordTable[i] ];\r
+                       // Check for forwarding\r
+                       if((Uint)expDir < retVal && retVal < (Uint)expDir + expLen) {\r
+                               char *fwd = (char*)retVal;\r
+                               DEBUGS(" PE_GetSymbol: '%s' forwards to '%s'\n", name, fwd);\r
+                               return PE_int_GetForwardSymbol(fwd, Ret);\r
+                       }\r
+                       *Ret = retVal;\r
+                       return 1;\r
+               }\r
+       }\r
+       return 0;\r
+}\r
+\r
+/**\r
+ * \fn char *PE_int_GetTrueFile(char *file)\r
+ * \brief Get the true file name\r
+ */\r
+char *PE_int_GetTrueFile(char *file)\r
+{\r
+        int    i;\r
+       if(file[0] != ':')      return file;\r
+\r
+       // Translate name\r
+       for(i=1;file[i];i++)\r
+               if(file[i] == '_')\r
+                       file[i] = '.';\r
+       \r
+       return &file[1];\r
+}\r
+\r
+int PE_int_GetForwardSymbol(char *Fwd, Uint *Value)\r
+{\r
+       char    *libname;\r
+       char    *sym;\r
+        int    i;\r
+       Uint    libbase;\r
+        int    ret;\r
+       \r
+       // -- Find seperator\r
+       // PE spec says that there sould only be one, but the string may\r
+       // be mutilated in the loader\r
+       for(i=0;Fwd[i]!='\0';i++);\r
+       for(;i&&Fwd[i]!='.';i--);\r
+       \r
+       Fwd[i] = '\0';\r
+       sym = &Fwd[i+1];\r
+       \r
+       libname = PE_int_GetTrueFile(Fwd);\r
+       \r
+       DEBUGS(" PE_int_GetForwardSymbol: Get '%s' from '%s'\n", sym, libname);\r
+       \r
+       libbase = LoadLibrary(libname, DLL_BASE_PATH, NULL);\r
+       ret = GetSymbolFromBase(libbase, sym, Value);\r
+       if(!ret) {\r
+               SysDebug(" PE_int_GetForwardSymbol: Unable to find '%s' in '%s'\n", sym, libname);\r
+       }\r
+       Fwd[i] = '.';\r
+       return ret;\r
+}\r
diff --git a/Usermode/Libraries/ld-acess.old.so_src/pe.h b/Usermode/Libraries/ld-acess.old.so_src/pe.h
new file mode 100644 (file)
index 0000000..5221f3a
--- /dev/null
@@ -0,0 +1,150 @@
+/*\r
+AcessOS/AcessBasic v1\r
+PE Loader\r
+HEADER\r
+*/\r
+#ifndef _EXE_PE_H\r
+#define _EXE_PE_H\r
+\r
+enum ePE_MACHINES {\r
+       PE_MACHINE_I386 = 0x14c,        // Intel 386+\r
+       PE_MACHINE_IA64 = 0x200         // Intel-64\r
+};\r
+\r
+enum ePE_DIR_INDX {\r
+       PE_DIR_EXPORT,          // 0\r
+       PE_DIR_IMPORT,          // 1\r
+       PE_DIR_RESOURCE,        // 2\r
+       PE_DIR_EXCEPTION,       // 3\r
+       PE_DIR_SECRURITY,       // 4\r
+       PE_DIR_RELOC,           // 5\r
+       PE_DIR_DEBUG,           // 6\r
+       PE_DIR_COPYRIGHT,       // 7\r
+       PE_DIR_ARCHITECTURE,// 8\r
+       PE_DIR_GLOBALPTR,       // 9\r
+       PE_DIR_TLS,                     // 10\r
+       PE_DIR_LOAD_CFG,        // 11\r
+       PE_DIR_BOUND_IMPORT,// 12\r
+       PE_DIR_IAT,                     // 13\r
+       PE_DIR_DELAY_IMPORT,// 14\r
+       PE_DIR_COM_DESCRIPTOR,  //15\r
+       PE_DIR_LAST\r
+};\r
+\r
+typedef struct {\r
+       Uint32  RVA;\r
+       Uint32  Size;\r
+} tPE_DATA_DIR;\r
+\r
+typedef struct {\r
+       Uint32  *ImportLookupTable;     //0x80000000 is Ordninal Flag\r
+       Uint32  TimeStamp;\r
+       Uint32  FowarderChain;\r
+       char    *DLLName;\r
+       Uint32  *ImportAddressTable;    // Array of Addresses - To be edited by loader\r
+} tPE_IMPORT_DIR;\r
+\r
+typedef struct {\r
+       Uint32  Flags;\r
+       Uint32  Timestamp;\r
+       Uint16  MajorVer;\r
+       Uint16  MinorVer;\r
+       Uint32  NameRVA;        // DLL Name\r
+       Uint32  OrdinalBase;\r
+       Uint32  NumAddresses;\r
+       Uint32  NumNamePointers;\r
+       Uint32  AddressRVA;\r
+       Uint32  NamePointerRVA;\r
+       Uint32  OrdinalTableRVA;\r
+} tPE_EXPORT_DIR;\r
+\r
+typedef struct {\r
+       Uint16  Hint;\r
+       char    Name[]; // Zero Terminated String\r
+} tPE_HINT_NAME;\r
+\r
+typedef struct {\r
+       char    Name[8];\r
+       Uint32  VirtualSize;\r
+       Uint32  RVA;\r
+       Uint32  RawSize;\r
+       Uint32  RawOffs;\r
+       Uint32  RelocationsPtr; //Set to 0 in executables\r
+       Uint32  LineNumberPtr;  //Pointer to Line Numbers\r
+       Uint16  RelocationCount;        // Set to 0 in executables\r
+       Uint16  LineNumberCount;\r
+       Uint32  Flags;\r
+} tPE_SECTION_HEADER;\r
+\r
+#define PE_SECTION_FLAG_CODE   0x00000020      // Section contains executable code.\r
+#define PE_SECTION_FLAG_IDATA  0x00000040      // Section contains initialized data.\r
+#define PE_SECTION_FLAG_UDATA  0x00000080      // Section contains uninitialized data.\r
+#define PE_SECTION_FLAG_DISCARDABLE    0x02000000      // Section can be discarded as needed.\r
+#define PE_SECTION_FLAG_MEM_NOT_CACHED 0x04000000      // Section cannot be cached.\r
+#define PE_SECTION_FLAG_MEM_NOT_PAGED  0x08000000      // Section is not pageable.\r
+#define PE_SECTION_FLAG_MEM_SHARED     0x10000000      // Section can be shared in memory.\r
+#define PE_SECTION_FLAG_MEM_EXECUTE    0x20000000      // Section can be executed as code.\r
+#define PE_SECTION_FLAG_MEM_READ       0x40000000      // Section can be read.\r
+#define PE_SECTION_FLAG_MEM_WRITE      0x80000000      // Section can be written to.\r
+\r
+typedef struct {\r
+       Uint32  page;\r
+       Uint32  size;\r
+       Uint16  ents[];\r
+} tPE_FIXUP_BLOCK;\r
+\r
+//File Header\r
+typedef struct {\r
+       Uint16  Machine;\r
+       Uint16  SectionCount;\r
+       Uint32  CreationTimestamp;\r
+       Uint32  SymbolTableOffs;\r
+       Uint32  SymbolCount;\r
+       Uint16  OptHeaderSize;\r
+       Uint16  Flags;\r
+} tPE_FILE_HEADER;\r
+\r
+typedef struct {\r
+       Uint16  Magic;  //0x10b: 32Bit, 0x20b: 64Bit\r
+       Uint16  LinkerVersion;\r
+       Uint32  CodeSize;       //Sum of all Code Segment Sizes\r
+       Uint32  DataSize;       //Sum of all Intialised Data Segments\r
+       Uint32  BssSize;        //Sum of all Unintialised Data Segments\r
+       Uint32  EntryPoint;\r
+       Uint32  CodeRVA;\r
+       Uint32  DataRVA;\r
+       Uint32  ImageBase;      //Prefered Base Address\r
+       Uint32  SectionAlignment;\r
+       Uint32  FileAlignment;\r
+       Uint32  WindowsVersion; //Unused/Irrelevent\r
+       Uint32  ImageVersion;   //Unused/Irrelevent\r
+       Uint32  SubsystemVersion;       //Unused, Set to 4\r
+       Uint32  Win32Version;   //Unused\r
+       Uint32  ImageSize;\r
+       Uint32  HeaderSize;\r
+       Uint32  Checksum;       //Unknown Method, Can be set to 0\r
+       Uint16  Subsystem;      //Required Windows Subsystem (None, GUI, Console)\r
+       Uint16  DllFlags;\r
+       Uint32  MaxStackSize;           //Reserved Stack Size\r
+       Uint32  InitialStackSize;       //Commited Stack Size\r
+       Uint32  InitialReservedHeap;    // Reserved Heap Size\r
+       Uint32  InitialCommitedHeap;    // Commited Heap Size\r
+       Uint32  LoaderFlags;    // Obselete\r
+       Uint32  NumberOfDirEntries;\r
+       tPE_DATA_DIR    Directory[16];\r
+} tPE_OPT_HEADER;\r
+\r
+// Root Header\r
+typedef struct {\r
+       Uint32  Signature;\r
+       tPE_FILE_HEADER FileHeader;\r
+       tPE_OPT_HEADER  OptHeader;\r
+} tPE_IMAGE_HEADERS;\r
+\r
+typedef struct {\r
+       Uint16  Ident;\r
+       Uint16  Resvd[29];\r
+       Uint32  PeHdrOffs;              // File address of new exe header\r
+} tPE_DOS_HEADER;\r
+\r
+#endif\r
index 7c3a4b8..d204771 100644 (file)
@@ -1,39 +1,39 @@
-# Acess Dynamic Linker (ELF) Version 1\r
-#  LD-ACESS.SO\r
-#  Makefile\r
-\r
--include ../Makefile.cfg\r
-\r
-COBJ = main.o lib.o loadlib.o elf.o pe.o\r
-AOBJ = helpers.ao\r
-BIN = ../ld-acess.so\r
-\r
-CFLAGS   = -Wall -fno-builtin -fleading-underscore -fno-stack-protector\r
-ASFLAGS  = -felf\r
-LDFLAGS  = -T link.ld -Map map.txt -Bstatic\r
-\r
-\r
-.PHONY: all clean install\r
-\r
-all:   $(BIN)\r
-\r
-clean:\r
-       $(RM) $(BIN) $(AOBJ) $(COBJ) ld-acess.dmp ld-acess.dsm link.txt map.txt\r
-\r
-install: $(BIN)\r
-       $(xCP) $(BIN) $(DISTROOT)/Libs/\r
-\r
-$(BIN): $(AOBJ) $(COBJ)\r
-       @echo --- $(LD) -shared -o $@\r
-       @$(LD) $(LDFLAGS) -o $(BIN) $(AOBJ) $(COBJ) > link.txt\r
-       $(OBJDUMP) -x $(BIN) > ld-acess.dmp\r
+# 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
-\r
-$(COBJ): %.o: %.c\r
-       @echo $(CC) -o $@\r
-       @$(CC) $(CFLAGS) -o $@ -c $<\r
-\r
-$(AOBJ): %.ao: %.asm\r
-       @echo $(AS) -o $@\r
-       @$(AS) $(ASFLAGS) -o $@ $<\r
+
+$(COBJ): %.o: %.c
+       @echo $(CC) -o $@
+       @$(CC) $(CFLAGS) -o $@ -c $<
+
+$(AOBJ): %.ao: %.asm
+       @echo $(AS) -o $@
+       @$(AS) $(ASFLAGS) -o $@ $<
        
index 81913d5..3356a27 100644 (file)
@@ -1,68 +1,71 @@
-/*\r
- AcessOS v1\r
- By thePowersGang\r
- ld-acess.so\r
- COMMON.H\r
-*/\r
-#ifndef _COMMON_H\r
-#define _COMMON_H\r
-\r
-#define        NULL    ((void*)0)\r
-\r
-#include <stdarg.h>\r
-\r
-// === CONSTANTS ===\r
-#define        MAX_LOADED_LIBRARIES    64\r
-#define        MAX_STRINGS_BYTES       4096\r
-#define        SYSTEM_LIB_DIR  "/Acess/Libs/"\r
-\r
-// === Types ===\r
-typedef unsigned int   Uint;\r
-typedef unsigned char  Uint8;\r
-typedef unsigned short Uint16;\r
-typedef unsigned long  Uint32;\r
-typedef signed char            Sint8;\r
-typedef signed short   Sint16;\r
-typedef signed long            Sint32;\r
-\r
-typedef struct {\r
-       Uint    Base;\r
-       char    *Name;\r
-}      tLoadedLib;\r
-\r
-// === GLOBALS ===\r
-extern tLoadedLib      gLoadedLibraries[MAX_LOADED_LIBRARIES];\r
+/*
+ AcessOS v1
+ By thePowersGang
+ ld-acess.so
+ COMMON.H
+*/
+#ifndef _COMMON_H
+#define _COMMON_H
+
+#define        NULL    ((void*)0)
+
+#include <stdarg.h>
+
+// 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 );
-\r
-// === Library/Symbol Manipulation ==\r
+
+// === Library/Symbol Manipulation ==
 extern Uint    LoadLibrary(char *filename, char *SearchDir, char **envp);
 extern void    AddLoaded(char *File, Uint base);
-extern Uint    GetSymbol(char *name);\r
-extern int     GetSymbolFromBase(Uint base, char *name, Uint *ret);\r
-\r
-// === Library Functions ===\r
-extern char    *strcpy(char *dest, const char *src);\r
+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);\r
-extern int     strlen(const char *str);\r
-extern int     file_exists(char *filename);\r
-\r
-// === System Calls ===\r
-extern void    SysExit();\r
-extern void    SysDebug(char *fmt, ...);       //!< Now implemented in main.c\r
-extern void    SysDebugV(char *fmt, ...);\r
-extern Uint    SysLoadBin(char *path, Uint *entry);\r
-extern Uint    SysUnloadBin(Uint Base);\r
-extern void    SysSetFaultHandler(int (*Hanlder)(int));\r
-extern int     open(char *filename, int flags);\r
-extern void    close(int fd);\r
+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);\r
-\r
-// === PE Loader ===\r
-extern int     PE_GetSymbol(Uint Base, char *Name, Uint *ret);\r
-\r
-#endif\r
+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 (file)
index 0000000..c44dc53
--- /dev/null
@@ -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/helpers.asm b/Usermode/Libraries/ld-acess.so_src/helpers.asm
deleted file mode 100644 (file)
index 49b9bd8..0000000
+++ /dev/null
@@ -1,88 +0,0 @@
-; AcessOS 1\r
-; By thePowersGang\r
-; LD-ACESS.SO\r
-; - helpers.asm\r
-\r
-%include "../libacess.so_src/syscalls.inc.asm"\r
-\r
-[global _SysDebug]\r
-[global _SysExit]\r
-[global _SysLoadBin]\r
-[global _SysUnloadBin]\r
-[global _SysSetFaultHandler]\r
-[global _open]\r
-[global _close]\r
-\r
-; void SysDebugV(char *fmt, va_list Args)\r
-_SysDebug:\r
-       ;xchg bx, bx\r
-       push ebp\r
-       mov ebp, esp\r
-       pusha\r
-       \r
-       mov eax, 0x100  ; User Debug\r
-       mov ebx, [ebp+8]        ; Format\r
-       mov ecx, [ebp+12]       ; Arguments\r
-       mov edx, [ebp+16]       ; Arguments\r
-       mov edi, [ebp+20]       ; Arguments\r
-       mov esi, [ebp+24]       ; Arguments\r
-       int     0xAC\r
-       \r
-       popa\r
-       pop ebp\r
-       ret\r
-\r
-; void SysExit()\r
-_SysExit:\r
-       push ebx\r
-       mov eax, SYS_EXIT       ; Exit\r
-       mov ebx, [esp+0x8]      ; Exit Code\r
-       int     0xAC\r
-       pop ebx\r
-       ret\r
-\r
-; Uint SysLoadBin(char *path, Uint *entry)\r
-_SysLoadBin:\r
-       push ebx\r
-       mov eax, SYS_LOADBIN    ; SYS_LDBIN\r
-       mov ebx, [esp+0x8]      ; Path\r
-       mov ecx, [esp+0xC]      ; Entry\r
-       int     0xAC\r
-       pop ebx\r
-       ret\r
-\r
-; Uint SysUnloadBin(Uint Base)\r
-_SysUnloadBin:\r
-       push ebx\r
-       mov eax, SYS_UNLOADBIN  ; SYS_ULDBIN\r
-       mov ebx, [esp+0x8]      ; Base\r
-       int     0xAC\r
-       pop ebx\r
-       ret\r
-\r
-; int close(char *name, int flags)\r
-_open:\r
-       push ebx\r
-       mov eax, SYS_OPEN\r
-       mov ebx, [esp+0x8]      ; Filename\r
-       mov ecx, [esp+0xC]      ; Flags\r
-       int 0xAC\r
-       pop ebx\r
-       ret\r
-\r
-; void close(int fd)\r
-_close:\r
-       push ebx\r
-       mov eax, SYS_CLOSE\r
-       mov ebx, [esp+0x8]      ; File Descriptor\r
-       int 0xAC\r
-       pop ebx\r
-       ret\r
-\r
-_SysSetFaultHandler:\r
-       push ebx\r
-       mov eax, SYS_SETFAULTHANDLER\r
-       mov ebx, [esp+0x8]      ; File Descriptor\r
-       int 0xAC\r
-       pop ebx\r
-       ret\r
index 3acc129..28fb467 100644 (file)
@@ -1,62 +1,62 @@
-/*\r
- AcessOS 1\r
- Dynamic Loader\r
- By thePowersGang\r
-*/\r
-#include "common.h"\r
-\r
-// === CODE ===\r
-char *strcpy(char *dest, const char *src)\r
-{\r
-       char    *ret = dest;\r
-       while(*src) {\r
-               *dest = *src;\r
-               src ++; dest ++;\r
-       }\r
-       *dest = '\0';\r
-       return ret;\r
-}\r
-\r
-char *strcat(char *dest, const char *src)\r
-{\r
-       char    *ret = dest;\r
-       while(*dest)    dest++;\r
-       while(*src)             *dest++ = *src++;\r
-       *dest = '\0';\r
-       return ret;\r
+/*
+ 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;
 }
-\r
-/**\r
- * \fn int strcmp(const char *s1, const char *s2)\r
- * \brief Compare two strings\r
+
+/**
+ * \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;
-}\r
-\r
-/**\r
- * \fn int strlen(const char *str)\r
- * \brief \r
- */\r
-int strlen(const char *str)\r
-{\r
-        int    len = 0;\r
-       while(*str)     len++,str++;\r
-       return len;\r
 }
-\r
-/**\r
- * \fn int file_exists(char *filename)\r
- * \brief Checks if a file exists\r
+
+/**
+ * \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)\r
-{\r
-        int    fd;\r
-        //fd = open(filename, OPENFLAG_READ);\r
-        fd = open(filename, 0);\r
-        if(fd == -1)   return 0;\r
-        close(fd);\r
-        return 1;\r
-}\r
+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;
+}
index 8ddc234..c318850 100644 (file)
@@ -1,27 +1,27 @@
-ENTRY(_SoMain)\r
-OUTPUT_FORMAT(elf32-i386)\r
-\r
-SECTIONS {\r
-    . = 0xBBFF0000;\r
-       _gLinkedBase = .;\r
-\r
-   .text : AT(ADDR(.text)) {\r
-        code = .;\r
-        *(.text)\r
-        *(.rodata*)\r
-   }\r
-\r
-   .data ALIGN (0x1000) : AT(ADDR(.data)) {\r
-        data = .;\r
-        *(.data)\r
-   }\r
-\r
-   .bss ALIGN (0x1000) : AT(ADDR(.bss)) {\r
-        _sbss = .;\r
-        *(COMMON)\r
-        *(.bss)\r
-        _ebss = .;\r
-        bss = .;\r
-   }\r
-   _end = .;\r
+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 = .;
 }
index 678dc9a..c3a7c27 100644 (file)
@@ -26,13 +26,13 @@ int SoMain(Uint base, int arg1)
        // - Assume that the file pointer will be less than 4096\r
        if(base < 0x1000) {\r
                SysDebug("ld-acess - SoMain: Passed file pointer %i\n", base);\r
-               SysExit();\r
+               _exit(-1);\r
                for(;;);\r
        }\r
        // Check if we are being called directly\r
        if(base == (Uint)&gLinkedBase) {\r
                SysDebug("ld-acess should not be directly called\n");\r
-               SysExit();\r
+               _exit(1);\r
                for(;;);\r
        }\r
        \r
@@ -41,7 +41,7 @@ int SoMain(Uint base, int arg1)
        ret = DoRelocate( base, NULL, "Executable" );\r
        if( ret == 0 ) {\r
                SysDebug("ld-acess - SoMain: Relocate failed, base=0x%x\n", base);\r
-               SysExit();\r
+               _exit(-1);\r
                for(;;);\r
        }\r
        \r
@@ -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",\r
                *(Uint8*)(base), *(Uint8*)(base+1), *(Uint8*)(base+2), *(Uint8*)(base+3) );\r
        SysDebug("ld-acess - DoRelocate: File '%s'\n", Filename);\r
-       SysExit();\r
+       _exit(-1);\r
        for(;;);\r
 }\r
 \r
diff --git a/Usermode/Libraries/ld-acess.so_src/mm.asm b/Usermode/Libraries/ld-acess.so_src/mm.asm
new file mode 100644 (file)
index 0000000..bede9ae
--- /dev/null
@@ -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 (file)
index 0000000..eea5573
--- /dev/null
@@ -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 (file)
index 0000000..44bce6b
--- /dev/null
@@ -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

UCC git Repository :: git.ucc.asn.au