2.8. Pragmas¶
The following pragmas are supported by the c29clang compiler:
2.8.1. clang section text¶
The clang section text pragma places enclosed functions within a named section, which can then be placed with the linker using a linker command file.
Syntax
#pragma clang section text=”scn_name”
The setting is reset to the default section name using
#pragma clang section text=””
Example
The following use of the clang section text pragma causes the enclosed function to be included in a section called .text.functions
#include <stdio.h>
#pragma clang section text=".text.functions"
int main() {
emit_msg();
emit_msg();
emit_msg();
}
#pragma clang section text=""
2.8.2. clang section data¶
The clang section data pragma places enclosed variables within a named section, which can then be placed with the linker using a linker command file.
Syntax
#pragma clang section data=”scn_name”
The setting is reset to the default section name using
#pragma clang section data=””
Example
The following use of the clang section data pragma causes the enclosed variables to be included in a section called .data.variables
#include <stdio.h>
#pragma clang section data=".data.variables"
int var1 = 39;
char *myString = "this is a test";
#pragma clang section data=""
extern void func(int, char*);
int main() {
func(var1, myString);
}
Note
Variables that are not initialized with a constant expression are not defined in .data
For example, in the above example, if either of the var1 or myString definitions were uninitialized, then they would not be defined in .data.variables. They would instead be defined in .bss.var1 and/or .bss.myString”.
2.8.3. clang section bss¶
The clang section bss pragma places enclosed variables within a named section, which can then be placed with the linker using a linker command file.
Syntax
#pragma clang section bss=”scn_name”
The setting is reset to the default section name using
#pragma clang section bss=””
Example
The following use of the clang section bss pragma causes the definition of myString to be included in a section called .bss.variables
#include <stdio.h>
#pragma clang section bss=".bss.variables"
int var1 = 39;
char *myString;
#pragma clang section bss=""
extern void init_myString(const char*);
extern void func(int, char*);
int main() {
init_myString("hello world");
func(var1, myString);
}
Note
Variables that are initialized with a constant expression are not defined in .bss
For example, in the above example, myString is defined in .bss.variables, but var1 is defined in .data.var1 since it is initialized with a constant expression.
2.8.4. clang section rodata¶
The clang section rodata pragma places enclosed variables within a named section, which can then be placed with the linker using a linker command file.
Syntax
#pragma clang section rodata=”scn_name”
The setting is reset to the default section name using
#pragma clang section rodata=””
Example
The following use of the clang section rodata pragma causes the enclosed const qualified data object definitions to be included in a section called MyRodata
#include <stdio.h>
#pragma clang section rodata="MyRodata"
const int var1 = 39;
const char *myString = "this is a test";
#pragma clang section rodata=""
extern void func(const int, const char*);
int main() {
func(var1, myString);
}
Note
A General Note About clang section Pragmas
In general, only variable definitions that match the type of the preceding #pragma clang section <type>=”scn_name” are affected by that clang section pragma.
You can specify more than one section type in a clang section pragma. For example,
#pragma clang section bss="myBSS" data="myData" rodata="myRodata"
int x2 = 5; // Goes in myData section.
int y2; // Goes in myBss section.
const int z2 = 42; // Goes in myRodata section.
If you were to turn off the clang section rodata between definitions of const qualified data objects:
#pragma clang section bss="myBSS" data="myData" rodata="myRodata"
int x2 = 5; // Goes in myData section.
int y2; // Goes in myBss section.
const int z2 = 42; // Goes in myRodata section.
#pragma clang section rodata="" // Use default name for rodata section.
int x3 = 5; // Goes in myData section.
int y3; // Goes in myBss section.
const int z3 = 42; // Goes in .rodata section
Note that z3 is not defined in myBSS or myData because it does not match the bss or data type specified in the first clang section pragma.