It is possible to get linker script symbols addresses as compile time constant values in C code?

Lilás picture Lilás · May 30, 2013 · Viewed 8.9k times · Source

I want to get the address of the end of my program and check at compilation/linker time if I have enough space, after the code, to place some random data in execution time.

But as the symbols provided by PROVIDE keyword are like normal variables in C code, I can not verify it in compilation time.

In the linker script I have the symbol :

PROVIDE (__data_end_rom   = _etext + SIZEOF (.data));

So I can use this symbol to get the address of the end of my code :

extern u16 __data_end_rom;

I can calculate the available memory if I suppose the end address to be 0xffff:

#define AVAILABLE_MEM (0Xffff - &__data_end_rom)

And I was thinking to check the available memory with _Static_assert(cond, message) provided in gcc 4.6

_Static_assert(SIZE_I_WANT_TO_ASSURE <= AVAILABLE_MEM, "NOT ENOUGH MEMORY!!!");

My problem is : The macro AVAILABLE_MEM is not calculated at compilation time, so I get the error:

error: expression in static assertion is not constant

Is there any way to provide the __data_end_rom address directly in a label or in another way ?

I know that I can't get it in compilation time because the symbol will just be linked in the linker time, so there is some way to make the linker fails ?

I could check this directly in the linker script but I prefer not doing so because the SIZE_I_WANT_TO_ASSURE is another macro calculated from others macros in a configuration header.

Answer

John Meacham picture John Meacham · Nov 16, 2013

You can create a custom linker script that has assertions in it. see here

http://sourceware.org/binutils/docs/ld/Miscellaneous-Commands.html

you want to use the 'ASSERT' mechanism.