1

I had a sudden doubt yesterday. I have been programming in C for a long time. My question is this:

Suppose there is a variable

static uint32_t count = 0;

This variable should be stored in the data segment.For the sake of this example let us assume that data segment starts at offset 0x08000000 in the 4GB virtual memory space of the process.

I know that somewhere after the offset 0x08000000 there is 4 byte reserved for the variable 'count' with the value 0.

My question is how is this 4-byte value in memory associated with the name "count" and the type "uint32_t" (or the fact that it is 4-bytes long and not 6 bytes long for example.)

From the answers below it seems that the mapping information is stored in the "symbol table"

It seems to suggest that it has to be a part of the final executable in memory.If so where is the symbol table stored? is it in the code/text segment?

liv2hak
  • 14,472
  • 53
  • 157
  • 270
  • Blessed in the linker and the subsequent loader, for they are the ones that sort all this out for you. (all except the type-part, which is left to the compiler). – WhozCraig Feb 07 '13 at 17:54

3 Answers3

3

As far as the generated machine code is concerned, the name 'count' does not exist. It merely knows an address at which to load and store data. Similarly, the type information is not preserved because the machine code that was generated to manipulate this variable was generated to operate on the correct size and signed-ness, so it does 4-byte loads and stores, and unsigned 32-bit arithmetic with that data.

For debugging and dynamic linking purposes, that information may be available elsewhere. Binaries with symbols will have a table that lists the addresses or offsets at which global variables are located, as well as their type.

Variable Length Coder
  • 7,958
  • 2
  • 25
  • 29
  • Exactly. Unless otherwise specified (Such as with the -g flag on GCC) then the binary has no concept of labels like `count`. Any references to the variable use the raw address with 32 bit operations used on it. – Kaslai Feb 07 '13 at 19:26
2

Association with the name: either the executable has a symbol table that gives the dynamic linker information (essentially a map) about names and the corresponding addresses, or if the executable is stripped, there's no name at all and only the raw address is referred to.

Association with the type: simply the compiler generates code that treats that address as the beginning of a 4-byte unsigned integer (e. g. instructions operating on DWORDs, etc.)

1

When the compiler parses your code, it keeps data structures with information about types and variables it sees in your program. So, when the parser sees:

static uint32_t count = 0;

it records that there is a variable count with type uint32_t with static storage duration initialized to zero. When the parser encounters the identifier count again, it knows that it refers to the aforementioned variable. All this information is used when generating object code, so the compiler backend knows to allocate space for it, and to treat it as an unsigned integer. This information may end up on the object file, to be used for debugging, or if it hadn't been a static variable, it may end up on a section for use by the dynamic linker.

The loader does not need this information, so if it still exists on the object file it doesn't need to load it. The linker and the dynamic linker may need the information for exported symbols, so they load it from the appropriate sections.

ninjalj
  • 42,493
  • 9
  • 106
  • 148