This is an archive of the discontinued LLVM Phabricator instance.

Add support for #pragma clang section
ClosedPublic

Authored by javed.absar on May 22 2017, 8:19 AM.

Details

Summary

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 initialized
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=""

1. The pragma applies to all global variable, statics and function declarations
from the pragma to the end of the translation unit.    
2. The pragma clang section is enabled automatically, without need of any flags.    
3. This feature is only defined to work sensibly for ELF targets.    
4. If section name is specified through _attribute_((section("myname"))), then
the attribute name gains precedence over any applicable section name via pragma directives.    
5. 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.    
6. 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.

There is a llvm corresponding patch with this patch.

Diff Detail

Repository
rL LLVM

Event Timeline

javed.absar created this revision.May 22 2017, 8:19 AM

This clang change looks sensible to me. Can you add documentation in LanguageExtensions.rst describing this new pragma and its semantics? Thanks!

Thanks Roger for the review and the suggestion to add documentation in LanguageExtensions.rst. I have done so now.
--Javed

After the docs update this looks good to me.

@rnk @jroelofs what do you think? Thanks!

rogfer01 accepted this revision.Jun 5 2017, 12:46 AM
This revision is now accepted and ready to land.Jun 5 2017, 12:46 AM
This revision was automatically updated to reflect the committed changes.