diff --git a/USAGE.md b/USAGE.md index 8f2f31ed..6c173b33 100755 --- a/USAGE.md +++ b/USAGE.md @@ -44,3 +44,6 @@ Only on build with trace enabled. * 0 : Default, the XMM (i.e. SSE/SSE2) register will not be logged with the general and x87 registers * 1 : Dump the XMM registers +#### BOX86_LOAD_ADDR +Try to load at 0xXXXXXX main binaray (if binary is a PIE) +* 0xXXXXXXXX the load address (only active on PIE programs) \ No newline at end of file diff --git a/src/elfloader.c b/src/elfloader.c index 8da520e6..bca6e85e 100755 --- a/src/elfloader.c +++ b/src/elfloader.c @@ -84,34 +84,29 @@ const char* ElfName(elfheader_t* head) return head->name; } -int AllocElfMemory(elfheader_t* head) +int AllocElfMemory(elfheader_t* head, int mainbin) { - #if 0 - printf_log(LOG_DEBUG, "Allocating memory for Elf \"%s\"\n", head->name); - if (posix_memalign((void**)&head->memory, head->align, head->memsz)) { - printf_log(LOG_NONE, "Cannot allocate aligned memory (0x%x/0x%x) for elf \"%s\"\n", head->memsz, head->align, head->name); - return 1; + uintptr_t offs = 0; + if(mainbin && head->vaddr==0) { + char* load_addr = getenv("BOX86_LOAD_ADDR"); + if(load_addr) + if(sscanf(load_addr, "0x%x", &offs)!=1) + offs = 0; } - printf_log(LOG_DEBUG, "Address is %p\n", head->memory); - printf_log(LOG_DEBUG, "And setting memory access to PROT_READ | PROT_WRITE | PROT_EXEC\n"); - if (mprotect(head->memory, head->memsz, PROT_READ | PROT_WRITE | PROT_EXEC)) { - printf_log(LOG_NONE, "Cannot protect memory for elf \"%s\"\n", head->name); - // memory protect error not fatal for now.... - } - #else - printf_log(LOG_DEBUG, "Allocating 0x%x memory @%p for Elf \"%s\"\n", head->memsz, (void*)head->vaddr, head->name); - void* p = mmap((void*)head->vaddr, head->memsz - , PROT_READ | PROT_WRITE | PROT_EXEC, MAP_SHARED | MAP_ANONYMOUS | ((head->vaddr)?MAP_FIXED:0) + if(!offs) + offs = head->vaddr; + printf_log(LOG_DEBUG, "Allocating 0x%x memory @%p for Elf \"%s\"\n", head->memsz, (void*)offs, head->name); + void* p = mmap((void*)offs, head->memsz + , PROT_READ | PROT_WRITE | PROT_EXEC, MAP_SHARED | MAP_ANONYMOUS | ((offs)?MAP_FIXED:0) , -1, 0); if(p==MAP_FAILED) { - printf_log(LOG_NONE, "Cannot create memory map (@%p 0x%x/0x%x) for elf \"%s\"\n", (void*)head->vaddr, head->memsz, head->align, head->name); + printf_log(LOG_NONE, "Cannot create memory map (@%p 0x%x/0x%x) for elf \"%s\"\n", (void*)offs, head->memsz, head->align, head->name); return 1; } head->memory = p; memset(p, 0, head->memsz); head->delta = (intptr_t)p - (intptr_t)head->vaddr; printf_log(LOG_DEBUG, "Got %p (delta=%p)\n", p, (void*)head->delta); - #endif return 0; } diff --git a/src/elfloader.h b/src/elfloader.h index 893f240c..c96ca089 100755 --- a/src/elfloader.h +++ b/src/elfloader.h @@ -14,7 +14,7 @@ const char* ElfName(elfheader_t* head); // return 0 if OK int CalcLoadAddr(elfheader_t* head); -int AllocElfMemory(elfheader_t* head); +int AllocElfMemory(elfheader_t* head, int mainbin); void FreeElfMemory(elfheader_t* head); int LoadElfMemory(FILE* f, elfheader_t* head); int RelocateElf(lib_t *maplib, elfheader_t* head); diff --git a/src/library.c b/src/library.c index 3d9cb376..4ee4b782 100755 --- a/src/library.c +++ b/src/library.c @@ -194,7 +194,7 @@ library_t *NewLibrary(const char* path, box86context_t* context) return NULL; } // allocate memory - if(AllocElfMemory(elf_header)) { + if(AllocElfMemory(elf_header, 0)) { printf_log(LOG_NONE, "Error: allocating memory for elf %s\n", libname); fclose(f); return NULL; diff --git a/src/main.c b/src/main.c index 4ff2a454..21c16ce2 100755 --- a/src/main.c +++ b/src/main.c @@ -166,6 +166,7 @@ void PrintHelp() { #endif printf(" BOX86_TRACE_FILE with FileName to redirect logs in a file"); printf(" BOX86_DLSYM_ERROR with 1 to log dlsym errors\n"); + printf(" BOX86_LOAD_ADDR=0xXXXXXX try to load at 0xXXXXXX main binaray (if binary is a PIE)\n"); } int main(int argc, const char **argv, const char **env) { @@ -284,7 +285,7 @@ int main(int argc, const char **argv, const char **env) { return -1; } // allocate memory - if(AllocElfMemory(elf_header)) { + if(AllocElfMemory(elf_header, 1)) { printf_log(LOG_NONE, "Error: allocating memory for elf %s\n", context->argv[0]); fclose(f); FreeBox86Context(&context);