How can I declare a variable at an absolute address with GCC?

偶尔善良 提交于 2019-12-13 02:30:09

问题


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;

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 is called on it's object file?


回答1:


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


来源:https://stackoverflow.com/questions/33400783/how-can-i-declare-a-variable-at-an-absolute-address-with-gcc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!