在NX位关闭的情况下在Linux中分配数据页 [英] Allocating a data page in linux with NX bit turned off

查看:64
本文介绍了在NX位关闭的情况下在Linux中分配数据页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在程序中生成一些机器代码,然后运行它.一种方法是写一个.so文件,然后将其加载到程序中,但这似乎太昂贵了.

I would like to generate some machine code in my program and then run it. One way to do it would be to write out a .so file and then load it in the program but that seems too expensive.

Linux中是否有一种方法可以让我在数据页中写出代码,然后在其中设置函数ointer并进行调用?我在Windows上看到了类似的内容,您可以在该页面上分配一个页面,而该页面的NX保护已关闭,但是我找不到类似的Linux操作系统调用.

IS there a way in linux for me to write out the code in my data pages and then set my function ointer there and just call it? I've seen something similar on windows where you can allocate a page with the NX protection turned off for that page, but I can't find a similar OS call for linux.

推荐答案

系统调用是基本操作.您要PROT_EXEC

The mmap(2) (with munmap(2)) and mprotect(2) syscalls are the elementary operations to do that. Recall that syscalls are elementary operations from the point of view of an application. You want PROT_EXEC

由于动态链接程序ld.so正在使用它们,因此您可以strace任何动态链接的可执行文件以获取有关如何调用它们的线索.

You could just strace any dynamically linked executable to get a clue about how you might call them, since the dynamic linker ld.so is using them.

生成共享对象可能比您想象的要便宜.实际上,即使您进行交互工作,生成C代码,运行编译器,然后dlopen-生成的共享对象也具有一定意义.我的 MELT 域特定语言(以扩展GCC)正在执行此操作.回想一下,您可以做很多dlopen -s而没有问题.

Generating a shared object might be less expensive than you imagine. Actually, generating C code, running the compiler, then dlopen-ing the resulting shared object has some sense, even when you work interactively. My MELT domain specific language (to extend GCC) is doing this. Recall that you can do a big lot of dlopen-s without issues.

如果要在内存中生成机器代码,则可以使用 GNU lightning (快速生成缓慢的机器代码),libjit来自 dotgnu (生成的不良机器代码更少), LuaJit LLVM (缓慢生成优化的机器代码).顺便说一句, SBCL Common Lisp实现正在动态编译到内存并在运行时产生良好的机器代码(而且还有所有JVM的JIT就是这样做的.

If you want to generate machine code in memory, you could use GNU lightning (quick generation of slow machine code), libjit from dotgnu (generate less bad machine code), LuaJit, asmjit (x86 or amd64 specific), LLVM (slowly generate optimized machine code). BTW, the SBCL Common Lisp implementation is dynamically compiling to memory and produces good machine code at runtime (and there is also all the JIT for JVMs doing that).

这篇关于在NX位关闭的情况下在Linux中分配数据页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆