I remember asking a similar question once before, but resolved my previous issue. Now I have a different issue that I cannot seem to figure out.
As a quick recap, I was asking about how to obtain the first 64mb of address space with in the 32-bit address space. At first it didn't work because I forgot to tell XCode I'm building an i386 app instead of x86_64. This is the code I was using to test it:
Now, the call to mmap() was successful, but it causes a crash soon after. The call to printf also crashes, same with std::cout. Even if I comment this out, the program will crash also. I'm not sure why. This code works without issues on Linux, but not for Mac. I've done a similar trick on Windows, but I had to explicitly force the .exe 's base address to 0x10000 and create a static array of 64mb.
Why am I doing this? This is for an emulation experiment. In order for this to work, I need to have at least a few megabytes after 0x10000. No other base address will do as I am dealing with absolute memory address jumps most of the time.
Is there a way to set/change the base address of the app in XCode? I looked through the options and didn't see any way to change this. Maybe that would help in case I am interfering with some delicate processes.
Any ideas? Thanks.
Shogun
As a quick recap, I was asking about how to obtain the first 64mb of address space with in the 32-bit address space. At first it didn't work because I forgot to tell XCode I'm building an i386 app instead of x86_64. This is the code I was using to test it:
Code:
#include <sys/mman.h>
#include <iostream>
#include <stdio.h>
namespace {
void * const MEMORY_ADDRESS = reinterpret_cast<void *>(0x10000);
size_t const MEMORY_SIZE = 64*1024*1024;
}
int main() {
void * p = mmap(MEMORY_ADDRESS, MEMORY_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
if (p != reinterpret_cast<void *>(-1)) {
//std::cout << "Memory allocated " << std::endl;
printf( "Memory allocated" );
munmap(p, MEMORY_SIZE);
} else {
//std::cout << "Memory could not be allocated (error: " << errno << ")" << std::endl;
printf( "Memory could not be allocated (error: %d)", errno );
}
}
Now, the call to mmap() was successful, but it causes a crash soon after. The call to printf also crashes, same with std::cout. Even if I comment this out, the program will crash also. I'm not sure why. This code works without issues on Linux, but not for Mac. I've done a similar trick on Windows, but I had to explicitly force the .exe 's base address to 0x10000 and create a static array of 64mb.
Why am I doing this? This is for an emulation experiment. In order for this to work, I need to have at least a few megabytes after 0x10000. No other base address will do as I am dealing with absolute memory address jumps most of the time.
Is there a way to set/change the base address of the app in XCode? I looked through the options and didn't see any way to change this. Maybe that would help in case I am interfering with some delicate processes.
Any ideas? Thanks.
Shogun