如何使用 GCC 在绝对地址处声明变量? [英] How can I declare a variable at an absolute address with 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
、define 和任何我能想到的尝试,但我现在出于想法.我在别处读到这只能通过创建链接器脚本来实现,但事实并非如此,因为我的一些同行已经用 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
...
Whem 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屋!