Using Linker Script Values: Difference between revisions

From OSDev.wiki
Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content added Content deleted
(First draft)
 
No edit summary
 
Line 9: Line 9:
The solution to this is to take the address of <tt>_ebss</tt> either by using it as <tt>&amp;_ebss</tt> or by defining it as an unsized array (<tt>extern char _ebss[];</tt>) and casting to an integer. (The array notation prevents accidental reads from <tt>_ebss</tt> as arrays must be explicitly dereferenced)
The solution to this is to take the address of <tt>_ebss</tt> either by using it as <tt>&amp;_ebss</tt> or by defining it as an unsized array (<tt>extern char _ebss[];</tt>) and casting to an integer. (The array notation prevents accidental reads from <tt>_ebss</tt> as arrays must be explicitly dereferenced)


[[Category:Linkers]]
[[Category:FAQ]]
[[Category:FAQ]]

Latest revision as of 20:28, 10 July 2023

Q. Whenever I use a value defined in my linker script, I get garbage.

A. You're not taking the address of the symbol. Define it in C as extern char symbolname[]; instead of extern int symbolname;


Long Answer

A common problem is getting garbage data when trying to use a value defined in a linker script. This is usually because they're dereferencing the symbol. A symbol defined in a linker script (e.g. _ebss = .;) is only a symbol, not a variable. If you access the symbol using extern uint32_t _ebss; and then try to use _ebss the code will try to read a 32-bit integer from the address indicated by _ebss.

The solution to this is to take the address of _ebss either by using it as &_ebss or by defining it as an unsized array (extern char _ebss[];) and casting to an integer. (The array notation prevents accidental reads from _ebss as arrays must be explicitly dereferenced)