This is the corresponding llvm part for the clang patch https://reviews.llvm.org/D33412 that is also under review.
This patch adds support for a '#pragma clang section' directive in clang. An RFC was sent out earlier by my colleague James Molloy: http://lists.llvm.org/pipermail/cfe-dev/2017-March/053100.html
Purpose:
The purpose of this is to provide to developers a means to specify section-names for global variables, functions and static variables, using #pragma directives. This will provide a migration path towards clang for developers in the embedded and automotive domain. For example, AUTOSAR, an automotive standard, mandates the use of a #pragma in header files to determine in which sections initialised and uninitialized data get put into.
This feature is implemented in our legacy ARM Compiler 5 toolchain and GCC forks used across the automotive space that have this feature implemented compatible with the ARM Compiler 5 implementation. The documentation is here:
http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0472m/chr1359124985290.html
User-Visible Behavior:
The developer can specify section names as:
#pragma clang section bss="myBSS" data="myData" rodata="myRodata" text="myText"
The developer can "unspecify" a section name with empty string e.g.
#pragma clang section bss="" data="" text="" rodata=""
- The pragma applies to all global variable, statics and function declarations from the pragma to the end of the translation unit.
- The pragma clang section is enabled automatically, without need of any flags.
- This feature is only defined to work sensibly for ELF targets.
- If section name is specified through _attribute_((section("myname"))), then the attribute name gains precedence over any applicable section name via pragma directives.
- Global variables, including - basic types, arrays, struct - that are initialized to zero e.g. int x = 0; will be placed in the named bss section, if one is present.
- The section type using '#pragma clang section' approach does not does NOT try to infer section-kind from the name. For example, assigning a section ".bss.mysec" does NOT mean it will be placed in BSS.
Design:
The decision about which section-kind applies to each global is not taken by this implementation but is purely inquired from the default back-end implementation in LLVM. Once the section-kind is known, appropriate section name as specified by the user using pragma directive, is applied to that global. Note that this is more effective approach than choosing the section name in the front-end when optimisation passes have not been run and the final proper section is not known.