Post navigation

Overwriting Symbols in the GNU Linker File

I start liking the GNU linker (ld) more and more. Yes, that linker command file syntax needs some time to learn, but it is very powerful. I stumbled over an interesting way how to define linker symbols:

The interesting part is how the HEAP_SIZE and STACK_SIZE symbols are defined.

It checks if e.g. __heap_size__ is DEFINED, and if so, it uses that symbol, otherwise it is using 0x400. Very similar to the C/C++ ‘?’ operator. So I can overwrite the default of 0x400 with my value or symbol. The questions is: from where does the symbol come from?

Return 1 if symbol is in the linker global symbol table and is defined, otherwise return 0. You can use this function to provide default values for symbols. For example, the following command-file fragment shows how to set a global symbol begin to the first location in the .text section–but if a symbol called begin already existed, its value is preserved:

So this is really a cool way to overwrite a symbol in the linker file, without touching the linker file itself. After some digging, I have found a way how this can be done: with options in passed to the linker: It requires the -Xlinker option and the –defsym option:

You can use the same linker file for multiple applications. Then it is up to the application (make file, or even with source files) to configure the behaviour of the linker file. For example one application needs more or less stack space, so this can be configured.