2

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.

Community
  • 1
  • 1
Gilad Naaman
  • 6,390
  • 15
  • 52
  • 82

1 Answers1

1

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.

yugr
  • 19,769
  • 3
  • 51
  • 96