I'm trying to figuring out how memory layout is generated by linker. I have basic knowledge about memory sections and their attributes. My question is how linker place symbols inside a specific section?
For example; a C-like array elements are guaranteed to place consecutively in the data
section.
+---------+
| A[0] | 0x0000 <- An arbitrary memory address
+---------+
| A[1] | 0x0004
+---------+
| A[2] | 0x0008
+---------+
| |
| ... |
| |
+---------+
Apart from the local variables that placed in stack, how other variables are placed by the linker?
Let's assume I have two separate module foo
and bar
, and all of their variables will be placed to the memory without discarding by optimizations.
Module foo:
var a;
var b;
Module bar:
var a;
var b;
Is there any rule or convention for this case such as; same module variables are placed consecutively or symbol names generated by the compiler are placed alphabetically?
+-----------+
| a(foo) | \
+-----------+ -> Same module variables placed consecutively
| b(foo) | /
+-----------+
| a(bar) | \
+-----------+ -> Same module variables placed consecutively
| b(bar) | /
+-----------+
| |
| ... |
| |
+-----------+
+-----------+
| a(foo) | \
+-----------+ -> Variables placed alphabetically
| a(bar) | /
+-----------+
| b(foo) | \
+-----------+ -> Variables placed alphabetically
| b(bar) | /
+-----------+
| |
| ... |
| |
+-----------+
I've checked both GCC and LLVM documentations but couldn't find any information about this topic. Is there any place where I can look for more detailed information about address assignments or memory layout optimizations?