I can't force msvc10 to put my const object into .rdata section. It always ends up in .data, perfectly initialised (means there is no dynamic initialisation/runtime constructor execution). (compiled with standard project settings of 'release' build). I dont understand why the following code can't put 'obj1' into .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 obj1 is not going to rdata (checked in IDA) and how to force it in current situation? Tnx.
These objects have nontrivial constructors, so they have to be initialized dynamically rather than statically. Because of that, they're in the .data
section (where all dynamically initialized objects lie, as their memory need to be mutated during initialization), even though the compiler was able to optimize out the constructor call in this case.
Actually, nothing prevents the compiler from using .rdata
in this case. It's just that its developers didn't implement this.