1

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?

1 Answer 1

1
typedef int (__cdecl* _PIFV)(void);

#pragma section(".ABC$XAA", long, read)
#pragma section(".ABC$XZZ", long, read)

__declspec(allocate(".ABC$XAA")) _PIFV init_begin = { 0 };
__declspec(allocate(".ABC$XZZ")) _PIFV init_end   = { 0 };

The initializers should not be marked as executable. You're only storing pointers to functions, not actual functions. The function pointers need not be executable.

Note that the actual CRT initializer sections (int .CRT) are not documented implementation details and may change at any time.

1
  • Why did it put it all in the ".CRT$XCU" group when using data_seg is what I wanted to know in this answer Mar 20, 2021 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.