0

在 64 位 Windows 上用 C++ 编写 JIT 编译器,生成的代码有时需要调用用 C++ 编写的运行时函数。目前我正在分配内存来放置生成的代码VirtualAlloc(0, bytes, MEM_COMMIT, PAGE_EXECUTE_READWRITE);最后一个标志很重要,因为分配的内存不可执行。

VirtualAlloc大概可以在 64 位地址空间中的任何位置返回内存,这对于数据来说很好(通常需要超过 4 GB,因此它确实需要 64 位寻址),但 x64call指令的最有效形式需要一个 32 位 IP 相对偏移量,并且由于生成的代码量将小于 4 GB,因此最好将其定位在从 C++ 编译的代码的 32 位位移内。

有没有办法安排这个?

4

1 回答 1

1

You can specify a virtual address near which you want the allocation to happen as the first argument. To increase the chance of getting the allocation within the boundaries you want to could reserve the virtual memory region first and then request for committed memory as and when needed from the reserved space. Allocation by default happens bottom unless MEM_TOP_DOWN is specified or system is configured to perform memory layout top down to catch pointer truncation problems. Gist is that you can only increase the chance of having allocation within the boundary but should have code to handle when allocation is out of boundary.

于 2013-03-19T18:20:48.047 回答