Some solutions (to the more general question "how to share values โโbetween C and LD script?"):
1) (NOTE: for this solution, I assume that you are using the latest version of GNU ld, docs here: http://www.math.utah.edu/docs/info/ld_3.html )
You can associate an absolute value with a character inside a script by specifying it outside the section connections (for example, at the very beginning of the script). Than you can import a character in C, specifying it as extern. Remember that the value you need in C is the ADDRESS character:
Script:
StackSize = 0x00003000 * 4; .co_stack : { _fstackptr = ORIGIN(ram) + LENGTH(ram) - 4; _fstacksize = StackSize; . = (_fstackptr - _fstacksize); *(.co_stack .co_stack.*) }
WITH
extern long StackSize; #define STACK_SIZE (((size_t)&StackSize)/sizeof(long))
This decision may impose limitations when using the resulting value, for example. most compilers do not accept this line:
long my_stack[STACK_SIZE];
but you donโt need it anymore, because you can define the symbol "my_stack" inside the script and import it as "extern long my_stack [];". I think this is an acceptable limitation anyway.
2) Another way is to define two characters in the script located at the beginning and in the section and, and import them as an "extern char" in C. The size of the section in bytes is the difference between their two addresses. This solution has the same restriction (1)
3) If your linker is not smart enough, you can use the C preprocessor to create a suitable script at compile time by expanding the STACK_SIZE macro, as in C. You should
a) create a file "stacksize.h" containing
#define STACK_SIZE 0x3000
b) Add the line #include "stacksize.h" at the beginning of the script and use STACK_SIZE whenever you need. Save the script as "ldscript.c".
c) a preprocessor is called in your makefile (or equivalent compilation procedure). If you have a GCC team:
gcc -P -E ldscript.c -o ldscript.ld
Note that some versions of gcc emphasize the extension of the input file. Therefore, I suggest using ".c", this is the safest way.
d) Use the "ldscript.ld" file created by the C preprocessor as a script link