'Are the C global variables present into data segment before execution or are they put there on-the-fly?

Consider this simple C program :

test.c

int g=10;

int main(){
  g++;
  return 0;
}

Now my question is if g is put into the data segment before execution. A picture may help clarify even better what I mean :

enter image description here

  1. Which scenario is correct ?

  2. Is the g variable put into data segment before execution ? If so who is responsible for doing so ? I mean there must be some sort of STORE[xxxx],10 instruction somewhere during the process.

  3. Does the code segment only contain the main() function translated into machine instructions ?



Solution 1:[1]

To 1.:

It depends on the target system.

To 2.:

For example, on a desktop system commonly all the sections you show are loaded by the OS into RAM. Therefore the OS puts the initial values immediately into the DATA section. This is your left scenario.

Another example, a simple embedded system commonly has no OS. The startup code copies the initial values from non-volatile memory to RAM into the DATA section. This is to some extent your right scenario.

In any case, a conforming C runtime environment has the DATA section initialized before main() starts.

To 3.:

No, the code segment always has startup code, at least to prepare the arguments for main(). Depending on the target system, it does a lot more. Oh, and of course it contains all of your functions that are needed.

Solution 2:[2]

Now my question is if g is put into the data segment before execution.

Yes. Or at least before the execution of main().

If so who is responsible for doing so?

The "C runtime library" which executes before main() is called. This sets the .data and .bss sections where all variables with static storage duration end up. You can think of it as the line g=10; getting executed before main() is called. This is also true for local static variables inside functions.

RAM-based systems like PC desktop ones may load values directly into variables as the executable is copied from the HD into RAM.

The main difference between RAM-based systems and ROM-based systems is that RAM-based ones (like PC) aren't able to execute programs from ROM but upload everything to RAM, and upon doing so they may copy variable values placed at fixed locations too. Whereas a ROM-based system has nothing in RAM from start and needs to copy variable value from ROM to RAM before launching main().

Does the code segment only contain the main() function

It contains all functions present in the project. Usually linkers are smart enough to sort out functions that are included but never called.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 the busybee
Solution 2