This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: How to use __attribute__((used))?
- From: Paul Smith <paul at mad-scientist dot net>
- To: Ian Lance Taylor <iant at google dot com>
- Cc: "gcc-help at gcc dot gnu dot org" <gcc-help at gcc dot gnu dot org>
- Date: Tue, 17 Sep 2013 11:31:12 -0400
- Subject: Re: How to use __attribute__((used))?
- Authentication-results: sourceware.org; auth=none
- References: <1379429670 dot 29912 dot 33 dot camel at pdsdesk> <CAKOQZ8wsERmxYCopnenbD50s+D-OjqzmGsBJk2cEpqx+AUpXiw at mail dot gmail dot com>
- Reply-to: paul at mad-scientist dot net
On Tue, 2013-09-17 at 08:08 -0700, Ian Lance Taylor wrote:
> In your example code you are using the attribute on a local variable.
> The used attribute is only meaningful on a function or global
> variable.
Aha. OK thanks. It would be very helpful if the manual made this more
clear: it simply says "variable" and I see no obvious way to infer that
it applies only to global variables or functions.
> It's intended to be used to force the function or global variable to
> be emitted even if it is not used.
>
> It sounds like you are looking for the ability to disable optimization
> for a local variable. It doesn't make sense to say that you want to
> force a local variable to exist even at high levels of optimization.
> At high levels of optimization local variables more or less disappear,
> though debug info is able to track some cases. In any case, GCC has
> no such ability.
IMO it is not nonsensical to ask the compiler to preserve a variable on
the stack even at high levels of optimization, so that its value can be
queried from a coredump or even live debugging.
The variables I'm referring to are typically of the type in my example
(although their value is not so simply reproduced obviously): they are
local variables which are assigned but never read. They exist solely to
keep track of a value which may be interesting in a debugging session.
Ideally we'd not be debugging highly optimized code but real life is not
always ideal :-).
However, I understand that this capability is not currently available.
Cheers!