Const objects not in rdata / rodata section

I can't get msvc10 to put my const object in the .rdata section. It always ends with .data, it is perfectly initialized (which means that dynamic execution of initialization / execution is not performed). (compiled with standard project settings for the "release" build). I do not understand why the following code cannot put "obj1" in the .rdata PE section:

typedef struct _Struct1 { int m1; _Struct1(int p1): m1(p1) {}; _Struct1() {}; } Struct1; class Class1 { public: Class1() {}; Class1(int p1, int p2): m1(p1), m2_struct(p2) {}; int m1; Struct1 m2_struct; }; const Class1 obj1(1, 2); int main() { return 0; } 

Why is obj1 not going to rdata (checked in the IDA) and how to force it in the current situation? Tnx.

+4
source share
1 answer

These objects have non-trivial constructors, so they need to be initialized dynamically, not statically. Because of this, they are located in the .data section (where all dynamically initialized objects are located, since their memory must be mutated during initialization), although the compiler was able to optimize the constructor call in this case.

In fact, nothing prevents the compiler from using .rdata in this case. It is simply that its developers did not implement this.

+3
source

Source: https://habr.com/ru/post/1390475/


All Articles