https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111134
Bug ID: 111134 Summary: Sections for static data declared in functions with section attribute Product: gcc Version: 13.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: manuelhohmann at online dot de Target Milestone: --- Using the section attribute, functions can be placed in specific sections. However, this only places the code in the named section. However, static variables, static constants and string literals are not affected by this. Example: void __attribute__ ((section ".my.text")) testfunc(void) { static char data[] = {0, 1, 2, 3}; static const char rodata[] = {4, 5, 6, 7}; puts("test"); } This will place data in .data, rodata in .rodata and "test" in .rodata.strM.N (where M and N is related to alignment). The function code itself is in .my.text. For the two arrays, one can change the section: void __attribute__ ((section ".my.text")) testfunc(void) { static char __attribute__ ((section ".my.data")) data[] = {0, 1, 2, 3}; static const char __attribute__ ((section ".my.rodata")) rodata[] = {4, 5, 6, 7}; puts("test"); } This will place them in .my.data and .my.rodata, respectively. However, there does not seem to be a possibility to do this with string literals. One could define them as constant char arrays as a workaround. Also there does not seem to be a possibility to change the section of all static data items in a function at once. Would it be possible to allow such functionality e.g. by introducing function attributes such as data_section rodata_section string_section which change the names of the sections, but otherwise retain their attributes (such as mergeable, string for the string literals)? Or / and e.g. a function attribute along the lines of __attribute__ ((section_prefix ".my")) that will prefix all section names (.text, .data, .rodata, .rodata.strM.N) with the given string for the function and its associated data? --- Rationale / example use: For certain environments, such as embedded code or operating system kernels, a boot loader will load a single executable into memory, containing both initialization and permanent code. Placing initialization code into a separate section allows to free the occupied memory once initialization is completed and the code is no longer used. It would be helpful if along with a function's code also all of its associated data could be assigned a custom section.