简体   繁体   English

在Linux上的C中分配可执行RAM

[英]Allocate executable ram in c on linux

I want to make a simple just-in-time compiler with c on Linux. 我想在Linux上用c创建一个简单的即时编译器。

How can I allocate memory such that I can write out raw x86 code to it and execute it as any other function? 如何分配内存,以便可以向其中写出原始的x86代码并将其作为其他函数执行?

See mprotect(). 请参见mprotect()。 Once you have filled a (n-)page-sized memory region (allocated with mmap()) with code, change its permissions to disallow writes and allow execution. 一旦用代码填充了(n)页大小的内存区域(与mmap()一起分配),请更改其权限以禁止写入并允许执行。

In addition to using mprotect correctly to provide first write and then execute permission, on some OS/hardware operations you may need to flush the I-cache . 除了正确使用mprotect来提供首先写入然后执行的权限外,在某些OS /硬件操作上,您可能还需要刷新I-cache At this moment (mid-2010), all recent x86 processors have separate level 1 caches for instructions and data, and somebody has to make sure that if you write new instructions into memory (which will update the D-cache), you don't then try to execute stale bits from the I-cache. 目前(2010年中),所有最新的x86处理器都具有用于指令和数据的单独的1级缓存,并且有人必须确保如果将新指令写入内存(这将更新D缓存),则不会然后尝试从I缓存执行过时的位。 Exactly how to flush the I-cache from userspace will depend on both your hardware and the OS. 究竟如何从用户空间刷新I缓存将取决于您的硬件和操作系统。 My advice would be to read Intel's documentation on "self-modifying code" for their IA-32 multiprocessors. 我的建议是阅读有关 IA-32多处理器的“自动修改代码”的英特尔文档 This should be enough to get you through. 这应该足以使您通过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM