This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Disable optimizations on one function (was: 'pragma optimize' ...)
- From: Martin Sebor <msebor at gmail dot com>
- To: noloader at gmail dot com, Markus Trippelsdorf <markus at trippelsdorf dot de>
- Cc: "gcc-help at gcc dot gnu dot org" <gcc-help at gcc dot gnu dot org>
- Date: Thu, 16 Jul 2015 10:31:51 -0600
- Subject: Re: Disable optimizations on one function (was: 'pragma optimize' ...)
- Authentication-results: sourceware.org; auth=none
- References: <CAH8yC8kU9JXSyFHrW9neztA4btnjXuxv+m3KyPWp84F=Y1Rv7A at mail dot gmail dot com>
On 07/15/2015 11:58 PM, Jeffrey Walton wrote:
I have one more question related to the use of '#pragma GCC optimize'.
I have a zeroizer that must execute due to Certification and
Accreditation (C&A) requirements. When the zeroizer is removed as a
dead store, the compiler folks usually say, "but you asked for
optimizations...".
If we cannot use '#pragma GCC optimize' to turn off the optimizer for
a function, then how do we tell the compiler to *not* remove the code?
It is hard to tell without seeing the code. But see noclone, noinline
entries at:
http://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html
As a simplified example, imagine I have a function ClearAndFree:
void ClearAndFree(void* ptr, size_t n)
{
if(!ptr) return;
memset(ptr, 0x00, n);
free(ptr);
}
We've seen these type of functions have the memset optimized away.
(This is why the memset_s function was added to the C standard.)
The call to memset is optimized away because GCC knows both its
semantics and those of free. To prevent the memset from being
optimized away you must make it "believe" the semantics could
be different. There are a number of ways to do that: compiling
the function with the -fno-builtin-memset option (or one it's
enabled by) is one. Another is by hiding the memset call somehow,
e.g., by calling the function via a pointer initialized outside
the calling function, or by calling the function from an asm
statement.
Martin