Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do global variables get initialized by the elf loader

For global variables in C like

int aglobal = 5;

When does the 5 get transferred into aglobal by the loader and how does it know to put 5 in aglobal.

Same situation with a static declaration in a function. Like

int afunc() { static int astatic = 8; return astatic; }

like image 224
newguy Avatar asked Apr 15 '10 22:04

newguy


People also ask

How are global variables initialized?

Global variables are automatically initialized to 0 at the time of declaration. Global variables are generally written before main() function. In line 4, a and b are declared as two global variables of type int . The variable a will be automatically initialized to 0.

Where are global variables stored in Elf?

By excluding the static keyword, your global variables are placed in the export list and can be referred to by other object modules and the linkage editor will be able to find the symbols when creating the executable.

Are global variables stored in BSS?

text section, initialized global or static data is stored in . data section, and uninitialized global or static data is stored in . bss section.

Are global variables automatically initialized to 0?

Global and static variables are initialized to their default values because it is in the C or C++ standards and it is free to assign a value by zero at compile time. Both static and global variable behave same to the generated object code.


1 Answers

An int-sized space is made in a data section, with the value 5 encoded in it and a global non-function symbol named 'aglobal' is added to the symbol table pointing at it. References to aglobal are turned into relocations that are resolved at link-time to point to that data block, so in a fully-linked image instructions will load directly from that spot in memory that holds the 5 value

For example, the (x86) assembly might look something like:

.data
.globl aglobal
aglobal: .long 5

.text
main:
    mov eax, aglobal

In an object file, the mov instruction will turn into mov eax, 0 with a relocation R_386_32 aglobal+0, because the object file doesn't know for sure where the data section will be in memory.

In a fully-linked image, it might be something like:

mov eax, 0x804a010

Now the actual address of the 4 bytes in the data section is known, so it's specified directly

like image 127
Michael Mrozek Avatar answered Nov 17 '22 10:11

Michael Mrozek