5

In the man page of nm. It says

“A” The symbol's value is absolute, and will not be changed by further linking.

However, I don't know what that means. How can I define a variable or something else to make its value absolute in C?

If I declare a variable in test.c in its file scope

int a;

Then in the output of nm, the entry for a will be the following on my machine

0000000000000004 C a

So I'm wondering what can I do to make the nm output “A” for a variable. And I don't know what “absolute” means.

wildplasser
  • 43,142
  • 8
  • 66
  • 109
KenKenKen
  • 467
  • 1
  • 5
  • 18
  • 2
    This is not related to C – too honest for this site Oct 24 '15 at 22:36
  • 4
    @Olaf why is this not related to C? Have you ever read the title? – KenKenKen Oct 24 '15 at 22:41
  • I believe [this](http://stackoverflow.com/a/29110226/1627113) may be of interest. – Lefteris Oct 24 '15 at 23:20
  • @Lefteris Thank you for the comment. I've read that post already. However, the answer says I need to use linker script to do it. Do you know if there's any way I can do it without linker script? – KenKenKen Oct 24 '15 at 23:22
  • Because the C standard does not even mentin absolute or relative symbols. This is a matter of the environment. – too honest for this site Oct 24 '15 at 23:23
  • You have to use a linker script anyway. What is your actual issue? – too honest for this site Oct 24 '15 at 23:24
  • @Olaf Sorry I don't know the problem is actually not about C. I just want to know how can I achieve it in plain C code, without the help of linker script. Thank you for the reply! – KenKenKen Oct 25 '15 at 01:01
  • Did you eve read my previous comments? You cannot! – too honest for this site Oct 25 '15 at 01:36
  • @Olaf Saying you cannot do this C is like saying you cannot implement a binary tree in C because C standard does not mention it either. – KenKenKen Jul 20 '17 at 08:30
  • @KenKenKen: Unrelated != cannot! After almost 2 years you really should know what I meant. It still is not related to C and your question is not clear either. Feel free to provide a reference to the C standard (there is none). The accepted answer makes this clear. If you did not understand it, why do you accept it? – too honest for this site Jul 20 '17 at 12:10
  • @Olaf I agree that "Unrelated != cannot". However, you first said "This is not related to C", then you said "You cannot!". Do you see the contradiction here? – KenKenKen Jul 20 '17 at 20:53
  • @KenKenKen: Yes, because this is not part of the C standard. Maybe I should have been more specific, but I assume it was clear from the context. – too honest for this site Jul 20 '17 at 20:59
  • 1
    @Olaf Yes I know it's not related to C standard, but this fact does not make the question unrelated to C. If I asked a question about how to implement something in C, then that is related to C because how it's done in C will be different from how it's done in other languages. If I was just asking about the concept of absolute symbol or binary tree, then sure it's not related to C at all. Do you see my point? – KenKenKen Jul 20 '17 at 21:12

1 Answers1

4

When C compiler compiles your program, it produces a list of symbols in addition to the binary code of your program. The most common types that you are going to see are Us (for "undefined"), Ds and Ss (for global data), and Ts (for "text" segment, which is where the executable code goes).

As, or absolute (un-moveable) symbols are there to support embedded development, where placement of things at absolute addresses in memory is required. Normally you would produce such symbols only when cross-compiling for an embedded system, using C language extensions that let you specify the absolute address. A typical syntax would look like this:

unsigned char buf[128]@0x2000;

This is not a standard C, though, it's an extension for embedded systems. The code like this would produce an absolute symbol buf set at address 0x2000, which cannot be moved by linker.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • This is compiker-specific. Not even every embedded compiler will accept this. And objects with absolute addresses will likely have to be `volatile`, too. – too honest for this site Oct 25 '15 at 01:38
  • @Olaf Absolutely correct, that's not even close to being standard. That's why I wrote "C language extension". – Sergey Kalinichenko Oct 25 '15 at 01:48
  • "... , it's an extension for embedded systems" Sound like it is standard for embedded systems, so such compilers will support this. That's why I commented. Strange as it sounds, just properly casting an integer constant is often the more "portable" way (considering we are outside of the standard anyway). – too honest for this site Oct 25 '15 at 14:55
  • 1
    Note that dos commonly used this too. keyboard access and textmode video buffer access (for more than simple line based usage) commonly used such variables. – Marco van de Voort Oct 29 '15 at 16:29
  • Which C compilers support this extension with that syntax? Some compilers support the feature but using syntax which is less non-standard, for example see `__attribute__((at(0x40001000)))` supported by RealView ARM C compiler – https://stackoverflow.com/q/4067811 – Simon Kissane Jul 29 '23 at 23:45
  • @SimonKissane Take a look at [this article](https://mcuoneclipse.com/2012/11/01/defining-variables-at-absolute-addresses-with-gcc/) for info on this and other ways of doing it. – Sergey Kalinichenko Jul 30 '23 at 01:30