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

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

问题描述

我想产生一个文件,我可以加载到内存中(例如,使用 MMAP ),然后跳转到该内存的开始运行$ C $角

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.

在理想情况下,我想无论是使得code重新定位(这可能是低效的),或者指定的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

请参阅男人objcopy把您的平台的更多细节。

See man objcopy on your platform for more details.

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

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