I'm trying to generate a string during compile-time that contains a constant string and a couple calculated integers.
This string is to be used in GCC's __attribute__((section(""))) directive.
The purpose of the whole thing is to put a few variables in an ELF file, each with a unique section name.
I used to do this by compiling each object file with -DSOME_SYMBOL=<file_source_CRC> to differentiate between object files, and __COUNTER__ to differentiate between variables inside of a single object file. (We use this because of a requirement from our logging solution)
So the resulting code would be used using something like this:
#define SOME_MACRO(msg) {\
static const char *messageBuffer __section__((section(".msg" ## #SOME_SYMBOL ## #__COUNTER__))) = {msg};\
} // Approximation
SOME_MACRO("This is a string");
This solution works great, but it requires support from the build-system (calculating the CRC and injecting it as a GCC -D flag), and it became a bit of an overhead when we moved from Makefile to SCons.
So I searched for another solution, and found this compile time CRC solution, but I got a bit lost when trying to figure out how to append it to the string.
After a bit more searching, I found the following answer, which explains how to convert an integer to a string using template metaprogramming, but I still couldn't figure out how to append the strings (again, during compile time).
I'd love to find a solution for this problem.
Unfortunately this is not possible - according to code in c-attribs.c GCC requires sections operand to be a string literal:
if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
{
error ("section attribute argument not a string constant");
goto fail;
}
Section attribute is normally used on embedded systems where build systems already do a lot of additional work (generation of linker scripts, overlays, etc.) so there's little incentive to make it more intelligent. Relying on build system is your best bet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With