On 22/06/13 10:38, Jeffrey Walton wrote:
Hi All, I recently encountered a problem with a function was marked as a constructor. The source file was compiled with -O0, but it appears the function was optimized to the point it skipped some of the startup code and jumped into the failure state (which called exit). I was able to restore desired behavior with '#pragma GCC optimize("O0")' around the function (even volatile tricks did not help). The startup code had to do with an integrity check. The expected fingerprint was back-patched after compiling, and then recalculated at runtime. Then, a memcmp was made. It appears the compiler deduced that the allocation was a string of 0's and could never be equal to the runtime fingerprint, so its just omitted the code. Is this expected behavior for functions marked as constructors (compiled with -O0)? Jeff
The usual method is to mark that kind of back-patched memory as "volatile const". And you probably also want to force volatile access of the data you are reading through and checking. Then the code should work as expected even for high optimisation levels.
It also seems unlikely that the compiler could deduce that the run-time fingerprint was never all zeros. gcc's optimisers are very impressive, but there is a limit to how much the compiler will pre-calculate. So I suspect there is another error of some kind in your code.