.data section

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
karim
Posts: 21
Joined: Mon Jun 23, 2025 8:03 pm

.data section

Post 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?
MichaelPetch
Member
Member
Posts: 857
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: .data section

Post 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.
Post Reply