From 560e97380a10c4a8cd8b14b2b7f5d133e32759e0 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Tue, 25 Jan 2011 21:39:59 +0800 Subject: [PATCH] ld-acess - Fixed bugs due to not testing from AcessNative changes - Possibly some edge cases left in if someone tries to link against libld-acess.so or ld-acess.so and the name does not match the hard coded value. --- Usermode/Libraries/Makefile.tpl | 7 +- Usermode/Libraries/ld-acess.so_src/Makefile | 13 ++-- Usermode/Libraries/ld-acess.so_src/common.h | 8 +-- Usermode/Libraries/ld-acess.so_src/elf.c | 43 +++++++++++++ Usermode/Libraries/ld-acess.so_src/export.c | 67 ++++++++++++++++++++ Usermode/Libraries/ld-acess.so_src/link.ld | 39 ++++++++++++ Usermode/Libraries/ld-acess.so_src/loadlib.c | 18 +++--- Usermode/Libraries/ld-acess.so_src/main.c | 2 +- 8 files changed, 175 insertions(+), 22 deletions(-) create mode 100644 Usermode/Libraries/ld-acess.so_src/export.c diff --git a/Usermode/Libraries/Makefile.tpl b/Usermode/Libraries/Makefile.tpl index 9712bf8a..51e404d4 100644 --- a/Usermode/Libraries/Makefile.tpl +++ b/Usermode/Libraries/Makefile.tpl @@ -5,16 +5,17 @@ DEPFILES := $(addsuffix .d,$(OBJ)) _BIN := $(OUTPUTDIR)Libs/$(BIN) +_XBIN := $(addprefix $(OUTPUTDIR)Libs/,$(EXTRABIN)) .PHONY: all clean install postbuild -all: $(_BIN) postbuild +all: $(_BIN) $(_XBIN) clean: - $(RM) $(_BIN) $(OBJ) $(_BIN).dsm $(DEPFILES) + $(RM) $(_BIN) $(_XBIN) $(OBJ) $(_BIN).dsm $(DEPFILES) install: all - $(xCP) $(_BIN) $(DISTROOT)/Libs/ + $(xCP) $(_BIN) $(_XBIN) $(DISTROOT)/Libs/ $(_BIN): $(OBJ) @mkdir -p $(dir $(_BIN)) diff --git a/Usermode/Libraries/ld-acess.so_src/Makefile b/Usermode/Libraries/ld-acess.so_src/Makefile index 3c02d3db..668460a0 100644 --- a/Usermode/Libraries/ld-acess.so_src/Makefile +++ b/Usermode/Libraries/ld-acess.so_src/Makefile @@ -4,18 +4,19 @@ -include ../Makefile.cfg -OBJ := main.o lib.o loadlib.o elf.o pe.o +OBJ := main.o lib.o loadlib.o export.o elf.o pe.o OBJ += core.ao vfs.ao mm.ao BIN = ld-acess.so +EXTRABIN := libld-acess.so CFLAGS = -Wall -fno-builtin -fno-leading-underscore -fno-stack-protector +CFLAGS += $(CPPFLAGS) ASFLAGS = -felf -LDFLAGS = -T link.ld -Map map.txt -Bstatic -shared +LDFLAGS = -T link.ld -Map map.txt --export-dynamic include ../Makefile.tpl -postbuild: $(OUTPUTDIR)Libs/libld-acess.so - -$(OUTPUTDIR)Libs/libld-acess.so: - ln -s $(_BIN) $(OUTPUTDIR)Libs/libld-acess.so +$(_XBIN): $(_BIN) + @echo [LD] -o -shared libld-acess.so + @$(LD) $(LDFLAGS) -shared -o $@ $(OBJ) diff --git a/Usermode/Libraries/ld-acess.so_src/common.h b/Usermode/Libraries/ld-acess.so_src/common.h index 3356a27d..fad0344a 100644 --- a/Usermode/Libraries/ld-acess.so_src/common.h +++ b/Usermode/Libraries/ld-acess.so_src/common.h @@ -54,12 +54,12 @@ 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 void SysDebug(const char *fmt, ...); //!< Now implemented in main.c +extern void SysDebugV(const char *fmt, ...); +extern Uint SysLoadBin(const char *path, Uint *entry); extern Uint SysUnloadBin(Uint Base); extern void SysSetFaultHandler(int (*Hanlder)(int)); -extern int open(char *filename, int flags); +extern int open(const char *filename, int flags); extern void close(int fd); // === ELF Loader === diff --git a/Usermode/Libraries/ld-acess.so_src/elf.c b/Usermode/Libraries/ld-acess.so_src/elf.c index e1fcd2d4..dcde746d 100644 --- a/Usermode/Libraries/ld-acess.so_src/elf.c +++ b/Usermode/Libraries/ld-acess.so_src/elf.c @@ -315,6 +315,49 @@ int ElfGetSymbol(Uint Base, char *Name, Uint *ret) //DEBUGS("ElfGetSymbol: (Base=0x%x, Name='%s')\n", Base, Name); + // Catch the current executable + #if 0 + if( !hdr->misc.HashTable ) + { + Elf32_Phdr *phtab; + Elf32_Dyn *dynTab = NULL; + int j; + + // Locate the tables + phtab = (void*)( Base + hdr->phoff ); + for( i = 0; i < hdr->phentcount; i ++ ) + { + if( phtab[i].Type == PT_DYNAMIC ) { + dynTab = (void*)phtab[i].VAddr; + break ; + } + } + if( !dynTab ) { + SysDebug("ERROR - Unable to find DYNAMIC segment in %p", (void*)Base); + return 0; + } + + for( j = 0; dynTab[j].d_tag != DT_NULL; j++) + { + switch(dynTab[j].d_tag) + { + // --- Symbol Table --- + case DT_SYMTAB: + hdr->misc.SymTable = dynTab[j].d_val; + break; + // --- Hash Table -- + case DT_HASH: + hdr->misc.HashTable = dynTab[j].d_val; + break; + } + } + } + #endif + + if( !hdr->misc.SymTable || !hdr->misc.HashTable ) { + return 0; + } + pBuckets = (void *) hdr->misc.HashTable; symtab = (void *) hdr->misc.SymTable; diff --git a/Usermode/Libraries/ld-acess.so_src/export.c b/Usermode/Libraries/ld-acess.so_src/export.c new file mode 100644 index 00000000..867e17fb --- /dev/null +++ b/Usermode/Libraries/ld-acess.so_src/export.c @@ -0,0 +1,67 @@ +/* + * Acess2 Dynamic Linker + */ +#include "common.h" +#include + +#define _STR(x) #x +#define STR(x) _STR(x) +#define EXP(sym) {(Uint)&sym, STR(sym)} + +// === CONSTANTS === +const struct { + Uint Value; + char *Name; +} caLocalExports[] = { + EXP(gLoadedLibraries), + EXP(_exit), + EXP(clone), + EXP(kill), + EXP(yield), + EXP(sleep), + EXP(waittid), + EXP(gettid), + EXP(getpid), + EXP(getuid), + EXP(getgid), + + EXP(setuid), + EXP(setgid), + + EXP(SysSetName), + //EXP(SysGetName), + + //EXP(SysSetPri), + + EXP(SysSendMessage), + EXP(SysGetMessage), + + //EXP(SysSpawn), + EXP(execve), + EXP(SysLoadBin), + EXP(SysUnloadBin), + + EXP(_SysSetFaultHandler), + + EXP(open), + EXP(reopen), + EXP(close), + EXP(read), + EXP(write), + EXP(seek), + EXP(tell), + EXP(finfo), + EXP(readdir), + EXP(_SysGetACL), + EXP(chdir), + EXP(ioctl), + EXP(_SysMount), + + EXP(_SysOpenChild), + + EXP(_SysGetPhys), + EXP(_SysAllocate) + +}; + +const int ciNumLocalExports = sizeof(caLocalExports)/sizeof(caLocalExports[0]); diff --git a/Usermode/Libraries/ld-acess.so_src/link.ld b/Usermode/Libraries/ld-acess.so_src/link.ld index c318850d..3ab4f1a9 100644 --- a/Usermode/Libraries/ld-acess.so_src/link.ld +++ b/Usermode/Libraries/ld-acess.so_src/link.ld @@ -4,6 +4,45 @@ OUTPUT_FORMAT(elf32-i386) SECTIONS { . = 0xBBFF0000; gLinkedBase = .; + . += SIZEOF_HEADERS; + .interp : { *(.interp) } + .note.gnu.build-id : { *(.note.gnu.build-id) } + .hash : { *(.hash) } + .gnu.hash : { *(.gnu.hash) } + .dynsym : { *(.dynsym) } + .dynstr : { *(.dynstr) } + .gnu.version : { *(.gnu.version) } + .gnu.version_d : { *(.gnu.version_d) } + .gnu.version_r : { *(.gnu.version_r) } + .rel.dyn : + { + *(.rel.init) + *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*) + *(.rel.fini) + *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*) + *(.rel.data.rel.ro* .rel.gnu.linkonce.d.rel.ro.*) + *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*) + *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*) + *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*) + *(.rel.ctors) + *(.rel.dtors) + *(.rel.got) + *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*) + } + .rela.dyn : + { + *(.rela.init) + *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*) + *(.rela.fini) + *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*) + *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*) + *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*) + *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*) + *(.rela.ctors) + *(.rela.dtors) + *(.rela.got) + *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*) + } .text : AT(ADDR(.text)) { code = .; diff --git a/Usermode/Libraries/ld-acess.so_src/loadlib.c b/Usermode/Libraries/ld-acess.so_src/loadlib.c index e5090ddd..a5f0bc26 100644 --- a/Usermode/Libraries/ld-acess.so_src/loadlib.c +++ b/Usermode/Libraries/ld-acess.so_src/loadlib.c @@ -16,13 +16,12 @@ Uint IsFileLoaded(char *file); int GetSymbolFromBase(Uint base, char *name, Uint *ret); -// === CONSTANTS === -const struct { +// === IMPORTS === +extern const struct { Uint Value; char *Name; -} caLocalExports[] = { - {(Uint)gLoadedLibraries, "gLoadedLibraries"} -}; +} caLocalExports[]; +extern const int ciNumLocalExports; // === GLOABLS === tLoadedLib gLoadedLibraries[MAX_LOADED_LIBRARIES]; @@ -200,7 +199,9 @@ Uint GetSymbol(char *name) { int i; Uint ret; - for(i=0;i