如何让gcc只生成可以直接加载到内存并执行的机器码? [英] How to make gcc generate only machine code that can be loaded directly into memory and executed?

查看:53
本文介绍了如何让gcc只生成可以直接加载到内存并执行的机器码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成一个可以加载到内存中的文件(例如使用 mmap),然后跳转到该内存的开头运行代码.

I would like to produce a file that I can load into memory (for example with mmap) and then jump to the start of that memory to run the code.

理想情况下,我希望选择使代码可重定位(这可能效率低下)或指定代码期望加载的显式地址(这很痛苦),但任何一个都可能工作正常靠自己.

Ideally, I'd like the option of either making the code relocatable (which might be inefficient) or specifying an explicit address that the code expects to be loaded at (which is a pain), but either one would probably work fine on its own.

推荐答案

你可以这样做,但你需要通过目标文件格式.特别是,objcopy 命令可以将可执行文件转换为平面"二进制文件(取决于您的目标平台).也许是这样的:

You can do this but you will need to go through the object file format. In particular, the objcopy command can transform an executable file to a "flat" binary file (depending on your target platform). Perhaps something like this:

gcc -o test test.c
objcopy -O binary test test.bin

有关详细信息,请参阅您平台上的 man objcopy.

See man objcopy on your platform for more details.

这篇关于如何让gcc只生成可以直接加载到内存并执行的机器码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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