Using Linker Script Values

From OSDev Wiki
Jump to navigation Jump to search

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)