When the compiler finds a static initializer then it puts it in a special section. The name of the section is similar to ".CRT$XCU". The linker will locate all sections in alphabetic order of the section names.
When I want to implement the initialization function. For that purpose I introduce two sections:
typedef void(*PVF)();
#pragma data_seg(".CRT$XAA")
PVF _init_begin[] = { 0 };
#pragma data_seg(".CRT$XZZ")
PVF _init_end[] = { 0 };
When the linker orders the section correctly then there will a pointer to the initializer between _init_begin
and _init_end
.
The problem is that the sections are created with the attributes 0xC0300040
, i.e. there is write/read/execute access possible. But the compiler emits the sections ".CRT$XCU" with the attribute 0x40300040
. This causes the linker to place the sections together.
I already tried to define the section with this pragma:
#pragma section(".CRT$XAA",execute,read)
But this doesn't create any section ".CRT$XAA" in the .obj file.
How do I create the section with the required attributes?