1

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?

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • So you're asking for C source code for a program such that, when `nm` is run on its compiled form, the output will include an address for a variable named "A"? – John Bollinger Oct 28 '15 at 20:41
  • Yes, I want calling nm on the object file from the source code to output: 000000000000002a A A. The "000000000000002a" is not important however –  Oct 28 '15 at 20:43
  • 1
    The C language does not in any way address the question of *which* address is assigned to any particular symbol, nor the question of symbol relocation. Everything the `nm` utility does is quite outside its scope. Specific compilers may recognize extensions that allow you to do what you want; what compiler are you using? – John Bollinger Oct 28 '15 at 20:49

1 Answers1

0

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
Jens
  • 69,818
  • 15
  • 125
  • 179
  • Oh wow, didn't even know you could do that, guess that is why it was extra credit, thank you! Just noticed the hitchhiker reference, nice. –  Oct 28 '15 at 21:13
  • could also use 'A org 0x42' to place the 'A' label at address 0x42 – user3629249 Oct 28 '15 at 22:56