跳转到数据段
我正在测试我正在编写的生成 X86 指令的汇编程序。我想做这样的事情来测试说明是否有效。
#include<stdio.h>
unsigned char code[2] = {0xc9, 0xc3};
int main() {
void (*foo)();
foo = &code;
foo();
return 0;
}
然而,由于 DEP,OS X 似乎阻止了这种情况。有没有办法(a)为此程序禁用 DEP 或(b)以另一种格式输入字节,以便我可以跳转到它们。
I am testing an assembler I am writing which generates X86 instructions. I would like to do something like this to test whether the instructions work or not.
#include<stdio.h>
unsigned char code[2] = {0xc9, 0xc3};
int main() {
void (*foo)();
foo = &code;
foo();
return 0;
}
However it seems that OS X is preventing this due to DEP. Is there a way to either (a) disable DEP for this program or (b) enter the bytes in another format such that I can jump to them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只需要测试,请尝试这个,它神奇...
const
关键字使编译器将其放置在const
部分(警告!这是一个实现细节!),它与text
部分位于同一段中。整个段应该是可执行的。这样做可能更便携:并且您始终可以在汇编文件中执行此操作,
但是:如果您想在运行时更改代码,则需要使用
mprotect< /代码>。默认情况下,内存中不存在同时具有写入和执行权限的映射。
下面是一个示例:
mprotect
规范指出,如果内存最初不是使用mmap
映射的,则其行为是未定义的,但您正在测试,而不是发货,所以只需知道它在 OS X 上工作得很好,因为 OS Xmalloc
在幕后使用mmap
(我认为是唯一的)。If you just need to test, try this instead, it's magic...
The
const
keyword causes the compiler to place it in theconst
section (warning! this is an implementation detail!), which is in the same segment as thetext
section. The entire segment should be executable. It is probably more portable to do it this way:And you can always do it in an assembly file,
However: If you want to change the code at runtime, you need to use
mprotect
. By default, there are no mappings in memory with both write and execute permissions.Here is an example:
The
mprotect
specification states that its behavior is undefined if the memory was not originally mapped withmmap
, but you're testing, not shipping, so just know that it works just fine on OS X because the OS Xmalloc
usesmmap
behind the scenes (exclusively, I think).不知道 OSX 上的 DEP,但您可以做的另一件事是对您编写代码的内存进行 malloc(),然后跳转到此 malloc 区域。至少在 Linux 上,该内存不会受到 exec 保护(事实上,JIT 通常就是这样做的)。
Don't know about your DEP on OSX, but another thing you could do would be to malloc() the memory you write the code to and then jump into this malloc'ed area. At least on Linux this memory would not be exec-protected (and in fact that's how a JIT usually does the trick).