我正在尝试使用 LLVM 传递来转换 C/C++ 程序,以便所有内存分配仅发生在我可以决定的特定内存区域中。
例如对于以下代码,我想使用 LLVM 传递来启动地址 0x10000 处的所有内存分配:
// Test.cpp
int main()
{
int x = 5; // I want to allocate memory for this variable
// starting at address 0x10000
int y = 6 // Memory allocated at 0x10004, since it's the second int
return 0;
}
有谁知道这是否可以通过 LLVM channel 来实现?我已经知道我可以使用 pass 循环程序中的所有指令来查找所有 alloca 指令,但我不知道如何转换程序以便 alloca 在特定地址分配内存。
最佳答案
您可以使用 linker script将某个部分放置在该地址,然后使用 LLVM 传递到 place the relevant global/static variables at in that section .
但是,您问题中的 x 和 y 是 main() 本地的,并且往往存在于堆栈中,甚至可能存在于机器寄存器中。你知道,我也知道 main() 不会递归地调用自身,但编译器会尽力允许递归。在一个过程中解决这个问题并不简单,因此这些变量可能不容易放在特定的部分中。
关于c++ - LLVM Pass 在特定地址分配内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52618222/