Added option to load PIE Program at a specific address (to help debugging)

This commit is contained in:
ptitSeb 2019-03-18 17:28:21 +01:00
parent ac441ab136
commit 0af1112020
5 changed files with 20 additions and 21 deletions

View File

@ -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)

View File

@ -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;
}

View File

@ -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);

View File

@ -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;

View File

@ -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);