I have an issue where when I try to access a member of a class in a certain file, it does not get the actual value of that member. But when I try to access it in other places I do.
File A:
find_func_wrapper ( Func_Container * rules, char * func_name ) {
ulong count = rules->function_count;
cout << "A count: " << count << endl;
B::find_func( rules, func_name );
}
main () {
Func_Container *rules = get_rules();
find_func_wrapper( rules, func_name );
}
File B:
B::find_func ( Func_Container * rules, char * func_name ) {
ulong count = rules->function_count;
cout << "B count: " << count << endl;
}
When I run this, I get:
A count: 2
B count: 0
When the count member is set to 2. Stepping through the code with gdb, in both A and B when I use print rules->function_count I get 2.
Disassembling the code, in A find_func_wrapper.
1885 ulong count = rules->function_count;
=> 0x0000000006004be5 <+294>: mov -0xa8(%rbp),%rax
0x0000000006004bec <+301>: mov 0x60a8(%rax),%rax
0x0000000006004bf3 <+308>: mov %rax,-0x38(%rbp)
Also print &rules->function_count = 0x11684158 and print rules = 0x1167e0b0
And in B::find_func
2652 ulong count = rules->function_count;
0x00000000062494a1 <+75>: mov -0x4f8(%rbp),%rax
0x00000000062494a8 <+82>: mov 0x60e8(%rax),%rax
0x00000000062494af <+89>: mov %rax,-0x50(%rbp)
Printing the addresses of rules and ->function_count return the same addresses as expected. To me, it looks like the culprit is in the second mov instruction where the offset used in B, 0x60e8, is incorrect. Why would this be happening?
get_rules() returns a pointer to a global object that is initialized earlier and kept around till the program ends.
This is being compiled with gcc 4.4.7. The project is extremely large. Additionally, this only happens in debug builds, release builds or non-optimized builds don't seem to have this issue.
Sizeof in find_func_wrapper: 24968
Offset: 3093
Sizeof in B::find_func: 25032
Offset: 3101
Offset calculated by ((&rules->function_count) - rules)
I was able to narrow down the source of my issue be rearranging header includes. After placing the #include "Func_Container.h" before other includes in file B, I found that the container became the correct size. I proceeded to move other headers before Func_Container until I found which one was causing the issue. I found that the offending header had the _GLIBCXX_DEBUG flag defined. This caused extra debug members in certain std types which changed their sizes, so when my definition for Func_Container was loaded in later member addresses changed as a result of the larger types.
An example of the issue is available in this mailing list: https://gcc.gnu.org/ml/libstdc++/2012-10/msg00077.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With