From 12040654ff2053cc97de492180b582027b6de60b Mon Sep 17 00:00:00 2001 From: John Hodge Date: Wed, 20 Feb 2013 17:34:47 +0800 Subject: [PATCH] AcessNative - Working on possible recompilation emulation --- AcessNative/ld-acess.so_src/Makefile | 46 +++++++++ AcessNative/ld-acess.so_src/exports.c | 1 + AcessNative/ld-acess.so_src/main.c | 41 ++++++++ AcessNative/ld-acess.so_src/memory.c | 93 +++++++++++++++++++ AcessNative/ld-acess.so_src/request.c | 1 + AcessNative/ld-acess.so_src/syscalls.c | 1 + BuildConf/native/Makefile.cfg | 16 ++++ BuildConf/native/default.mk | 0 Usermode/Libraries/Makefile.tpl | 2 +- Usermode/Libraries/ld-acess.so_src/Makefile | 11 ++- .../Libraries/libc.so_src/arch/native.asm | 10 ++ 11 files changed, 220 insertions(+), 2 deletions(-) create mode 100644 AcessNative/ld-acess.so_src/Makefile create mode 100644 AcessNative/ld-acess.so_src/exports.c create mode 100644 AcessNative/ld-acess.so_src/main.c create mode 100644 AcessNative/ld-acess.so_src/memory.c create mode 100644 AcessNative/ld-acess.so_src/request.c create mode 100644 AcessNative/ld-acess.so_src/syscalls.c create mode 100644 BuildConf/native/Makefile.cfg create mode 100644 BuildConf/native/default.mk create mode 100644 Usermode/Libraries/libc.so_src/arch/native.asm diff --git a/AcessNative/ld-acess.so_src/Makefile b/AcessNative/ld-acess.so_src/Makefile new file mode 100644 index 00000000..661e6749 --- /dev/null +++ b/AcessNative/ld-acess.so_src/Makefile @@ -0,0 +1,46 @@ +# +# + +ifeq ($(PLATFORM),) + PLATFORM := lin +endif + +OBJ := main.o syscalls.o request.o memory.o exports.o +OBJ := $(addprefix obj-$(PLATFORM)/,$(OBJ)) + +ifeq ($(PLATFORM),win) + BIN := ../ld-acess.dll + LDFLAGS += -lws2_32 +endif +ifeq ($(PLATFORM),lin) + BIN := ../ld-acess.so +endif + +CFLAGS += -Wall +CFLAGS += -Werror +CFLAGS += -g -shared -fPIC +CPPFLAGS += -DARCHDIR_is_x86_64=1 +LDFLAGS += -g -shared -Wl,--no-undefined + +DEPFILES = $(filter %.o,$(OBJ)) +DEPFILES := $(DEPFILES:%=%.dep) + +.PHONY: all clean + +all: $(BIN) + +clean: + $(RM) $(BIN) $(OBJ) $(DEPFILES) + +$(BIN): $(OBJ) + $(CC) -o $@ $(OBJ) $(LDFLAGS) + objdump -S $@ > $@.dsm + +obj-$(PLATFORM)/%.o: %.c + @mkdir -p $(dir $@) + @echo [CC] -o $@ + @$(CC) -c $< -o $@ $(CFLAGS) $(CPPFLAGS) + @$(CC) -M $(CPPFLAGS) -MT $@ -o $@.dep $< + +-include $(DEPFILES) + diff --git a/AcessNative/ld-acess.so_src/exports.c b/AcessNative/ld-acess.so_src/exports.c new file mode 100644 index 00000000..cf38b5bc --- /dev/null +++ b/AcessNative/ld-acess.so_src/exports.c @@ -0,0 +1 @@ +#include "../ld-acess_src/exports.c" diff --git a/AcessNative/ld-acess.so_src/main.c b/AcessNative/ld-acess.so_src/main.c new file mode 100644 index 00000000..4bf908bf --- /dev/null +++ b/AcessNative/ld-acess.so_src/main.c @@ -0,0 +1,41 @@ +/* + */ +#include +#include + +#ifdef __WINDOWS__ +int DllMain(void) +{ + return 0; +} + +#endif + +#ifdef __linux__ +int main(int argc, char *argv[], char **envp) +{ + return 0; +} +#endif + + +void Debug(const char *format, ...) +{ + va_list args; + printf("Debug: "); + va_start(args, format); + vfprintf(stdout, format, args); + va_end(args); + printf("\n"); +} + +void Warning(const char *format, ...) +{ + va_list args; + printf("Warning: "); + va_start(args, format); + vfprintf(stdout, format, args); + va_end(args); + printf("\n"); +} + diff --git a/AcessNative/ld-acess.so_src/memory.c b/AcessNative/ld-acess.so_src/memory.c new file mode 100644 index 00000000..3a9ef1af --- /dev/null +++ b/AcessNative/ld-acess.so_src/memory.c @@ -0,0 +1,93 @@ +/* + */ +#include +#include +#include +#if __WIN32__ +# include +#else +# include +# include +#endif + +// === PROTOTYPES === + int AllocateMemory(uintptr_t VirtAddr, size_t ByteCount); +uintptr_t FindFreeRange(size_t ByteCount, int MaxBits); + +// === CODE === +int AllocateMemory(uintptr_t VirtAddr, size_t ByteCount) +{ + uintptr_t base = (VirtAddr >> 12) << 12; + size_t size = (VirtAddr & 0xFFF) + ByteCount; + void *tmp; + #if __WIN32__ + do + { + MEMORY_BASIC_INFORMATION info; + VirtualQuery( (void*)base, &info, sizeof(info) ); + if( info.State != MEM_FREE ) { + printf("ERROR: Unable to allocate memory %p+0x%x, already allocated\n", + (void*)base, size); + base += 0x1000; + if( size < 0x1000 ) + return 0; + size -= 0x1000; + } + else + break; + } while( size >= 0x1000 ); + tmp = VirtualAlloc((void*)base, size, MEM_RESERVE|MEM_COMMIT, PAGE_EXECUTE_READWRITE); + if( tmp == NULL ) { + printf("ERROR: Unable to allocate memory %p+%x (0x%x)\n", + (void*)base, size, + (int)GetLastError()); + return -1; + } + #else +// printf("AllocateMemory: mmap(%p, 0x%lx, ...)\n", (void*)base, ByteCount); + tmp = mmap((void*)base, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0); + if( tmp == MAP_FAILED ) { + printf("ERROR: Unable to allocate memory\n"); + perror("AllocateMemory"); + return -1; + } +// printf("AllocateMemory: RETURN 0\n"); + #endif + return 0; +} + +uintptr_t FindFreeRange(size_t ByteCount, int MaxBits) +{ + uintptr_t base, ofs, size; + uintptr_t end = -1; + static const int PAGE_SIZE = 0x1000; + + size = (ByteCount + PAGE_SIZE - 1) / PAGE_SIZE; + size *= PAGE_SIZE; + + end <<= (sizeof(intptr_t)*8-MaxBits); + end >>= (sizeof(intptr_t)*8-MaxBits); +// printf("end = %p\n", (void*)end); + +// for( base = 0; base < end - size; base -= PAGE_SIZE ) + for( base = end - size + 1; base > 0; base -= PAGE_SIZE ) + { + for( ofs = 0; ofs < size; ofs += PAGE_SIZE ) { + #if __WIN32__ + MEMORY_BASIC_INFORMATION info; + VirtualQuery( (void*)(base + ofs), &info, sizeof(info) ); + if( info.State != MEM_FREE ) + break; + #else + if( msync( (void*)(base+ofs), 1, 0) == 0 ) + break; + if( errno != ENOMEM ) + perror("FindFreeRange, msync"); + #endif + } + if( ofs >= size ) { + return base; + } + } + return 0; +} diff --git a/AcessNative/ld-acess.so_src/request.c b/AcessNative/ld-acess.so_src/request.c new file mode 100644 index 00000000..b892d6c1 --- /dev/null +++ b/AcessNative/ld-acess.so_src/request.c @@ -0,0 +1 @@ +#include "../ld-acess_src/request.c" diff --git a/AcessNative/ld-acess.so_src/syscalls.c b/AcessNative/ld-acess.so_src/syscalls.c new file mode 100644 index 00000000..33c89509 --- /dev/null +++ b/AcessNative/ld-acess.so_src/syscalls.c @@ -0,0 +1 @@ +#include "../ld-acess_src/syscalls.c" diff --git a/BuildConf/native/Makefile.cfg b/BuildConf/native/Makefile.cfg new file mode 100644 index 00000000..389a7388 --- /dev/null +++ b/BuildConf/native/Makefile.cfg @@ -0,0 +1,16 @@ +# +# Acess2 Native config +# - Used for forcing the architecture +# + +ARCHDIR := native + +LD ?= $(CC) -print-prog-name=ld + +OBJDUMP := objdump -S + +ifeq ($(HOST_ARCH),x86) +CC_SUFFIX := -m32 +LD_SUFFIX := -melf_i386 +endif + diff --git a/BuildConf/native/default.mk b/BuildConf/native/default.mk new file mode 100644 index 00000000..e69de29b diff --git a/Usermode/Libraries/Makefile.tpl b/Usermode/Libraries/Makefile.tpl index 1731f393..c18d04f7 100644 --- a/Usermode/Libraries/Makefile.tpl +++ b/Usermode/Libraries/Makefile.tpl @@ -3,7 +3,7 @@ # -_BIN := $(OUTPUTDIR)Libs/$(BIN) +_BIN := $(addprefix $(OUTPUTDIR)Libs/,$(BIN)) _XBIN := $(addprefix $(OUTPUTDIR)Libs/,$(EXTRABIN)) _OBJPREFIX := obj-$(ARCH)/ diff --git a/Usermode/Libraries/ld-acess.so_src/Makefile b/Usermode/Libraries/ld-acess.so_src/Makefile index 5475fdc2..5deab5fa 100644 --- a/Usermode/Libraries/ld-acess.so_src/Makefile +++ b/Usermode/Libraries/ld-acess.so_src/Makefile @@ -8,7 +8,7 @@ OBJ := main.o lib.o loadlib.o export.o elf.o pe.o OBJ += arch/$(ARCHDIR).ao_ BIN = ld-acess.so EXTRABIN := libld-acess.so -EXTRACLEAN := $(_OBJPREFIX)_stublib.o +EXTRACLEAN = $(_OBJPREFIX)_stublib.o INCFILES := sys/sys.h CFLAGS = -g -Wall -fno-builtin -fno-stack-protector -fPIC @@ -16,6 +16,15 @@ CFLAGS = -g -Wall -fno-builtin -fno-stack-protector -fPIC CFLAGS += $(CPPFLAGS) -Werror LDFLAGS = -g -T arch/$(ARCHDIR).ld -Map map.txt --export-dynamic +ifeq ($(ARCH),native) +XBIN := $(addprefix $(OUTPUTDIR)Libs/,$(EXTRABIN)) +$(XBIN): obj-$(ARCH)/_stublib.o +all: $(XBIN) +LDFLAGS := +BIN := +OBJ := +endif + include ../Makefile.tpl # create libld-acess.so diff --git a/Usermode/Libraries/libc.so_src/arch/native.asm b/Usermode/Libraries/libc.so_src/arch/native.asm new file mode 100644 index 00000000..27cdc062 --- /dev/null +++ b/Usermode/Libraries/libc.so_src/arch/native.asm @@ -0,0 +1,10 @@ +; +; Acess2 C Library +; - By John Hodge (thePowersGang) +; +; arch/x86.asm +; - x86 specific code +[bits 32] +[section .text] +unused_code: + jmp $ -- 2.20.1