我如何用GCC声明一个绝对地址的变量? [英] How can I declare a variable at an absolute address with GCC?

查看:2614
本文介绍了我如何用GCC声明一个绝对地址的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在研究链接器如何在我的一门课程中工作,其中一项任务是涉及 nm 命令的小练习。基本上我们只想匹配每个变量的类型和由 nm 打印的值。例如:

We are looking at how the linker works in one of my courses and one of the assignments is a little exercise involving the nm command. essentially we just want to match the type and the value printed by nm for each variable. for example:

char* B = NULL;

将给出地址(不相关),然后是B B.我已经成功完成了所有标签我们需要除了A.我已经读过,这只是意味着值是绝对的,不能被链接器改变。我尝试了很多涉及 volatile const static ,定义,以及其他任何我可以想到的尝试,但我目前没有想法。我曾在其他地方读过,这只能通过创建链接脚本才能实现,但事实并非如此,因为我的一些同行已经用C中的一行解决了这个问题。是否有人可以用C输出:

would give the address (irrelevant) then B B. I've done this successfully for all the labels we needed to except for A. I have read that this simply means the value is absolute and cannot be changed by the linker. I have experimented with many combinations involving volatile, const, static, define, and any thing else I could think to try, but I am presently out of ideas. I had read elsewhere that this could only be achieved by creating a linker script to do so, but that is not the case, as some of me peers have solved this with one line in C. Could any one come up with a way in C to output:

some address A A
...

对于它的目标文件调用 nm

Whem nm is called on it's object file?

推荐答案

在标准C中定义绝对符号有点棘手。但您可以使用非标准的内联汇编:

Defining absolute symbols in Standard C is a bit tricky. But you can use non-standard inline assembly:

$ cat x.c
asm (".globl A");
asm ("A = 0x42");
$ clang -c x.c
$ nm x.o
0000000000000042 A A

这篇关于我如何用GCC声明一个绝对地址的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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