Page 1 of 1
.data section
Posted: Wed Aug 06, 2025 12:50 am
by karim
So now, if I set in the linker script that the .data section starts at an address like 0x200000, and in the C file I write int x = 5, does that mean the linker will write the value 5 at, say, address 0x200002? And that x will then point to that address 0x200002? So basically, is the job of the linker script to make the linker calculate the actual addresses where things like the start of the .data section should go? Is what I’m saying correct?
Re: .data section
Posted: Wed Aug 06, 2025 10:49 am
by MichaelPetch
It depends, but in general yes. for example it would depend on where `int x = 5` appears. If it is at global scope (outside a function) or was defined inside a function as `static` it most likely would appear in the `.data` section. If defined inside a function, `x` will be on the stack if it isn't marked as static. In that case it won't appear in the `.data` section. Optimizations could potentially eliminate `x` altogether so `x` doesn't appear anywhere in the resulting executable (using link time optimizations/LTO). With LTO the linker could optimize out `x` if it found it wasn't referenced by any object file.
It would be the job of the linker do give the symbol `x` its address.