Newer gcc swallow version control keywords

Newer gcc swallow version control keywords

Elazar Leibovich elazarl at gmail.com
Tue Oct 18 11:16:38 IST 2011


I guess that it doesn't apply to libraries, which must include this global
variable.

Excuse the idiotic solution, but can't you just add an option to print it
out?

int main(int argc,char**argv) {if (argc == 2 && strcmp(argv[1],"--ident")
puts(ident);...}

The only trick the optimizer can play here, is inlining puts and the input
string to assembly commands, but I really don't think it can happen in
practice.

It seems like a good idea anyhow, to have a stable way of extracting this
ident string from the executable (what happens for instance if by accident
you have rcsident and rcs_ident? How would you know from the stripped
executable which one to trust?).

On Mon, Oct 17, 2011 at 6:29 PM, Oleg Goldshmidt <pub at goldshmidt.org> wrote:

> Hi,
>
> I have a gcc-related question. Problematic platform is Fedora 15 with
> gcc 4.6.1, as well as Fedora 14 with gcc 4.5.1.
>
> I am used to keeping RCS/CVS/SVN keywords (e.g., $Id$) in all my code.
> In the case of C/C++ this normally amounts to
>
> static const char foo_src_id[] = "$Id$";
>
> in the source code. For those who have never encountered it: SVN and
> friends happily substitute the relevant information for $Id$, and the
> information then can be retrieved using ident(1), even from binaries,
> libraries, and such - incredibly useful.
>
> Now, put the above line in a C or C++ file, say foo.cc, and do the
> following:
>
> $ g++ -g -O2 foo.cc -c -o foo.o
> $ ident foo.o
> foo.o:
>     $Id: foo.cc 673 2011-10-17 09:48:11Z oleg $
>
> This works up to and including gcc 4.4, but it does not work with gcc
> 4.5.1 or gcc 4.6.1 (the ident part does not show any keywords). The
> reason seems to be that the optimizer realizes that the static const
> is not used and eliminates it (remove -O2 and ident works fine).
>
> I can fool gcc by
>
> static const char foo_src_id[] __attribute__((used)) = "$Id$";
>
> but this is non-portable, and preprocessor acrobatics such as
>
> #if (__GNUC__ >= 4) && (__GNUC_MINOR__ > 4)
> #define USED(x) x __attribute__((used))
> #else
> #define USED(x) x
> #endif
>
> static const char USED(foo_src_id[]) = "$Id$";
>
> is incredibly ugly and verbose, IMHO (even with #ifdef __GNUC__ only).
> So is any contortion to use the constant somewhere in each file.
>
> Does anyone know of a gcc option that will keep unused static
> constants even with optimization or or any other way to keep the
> keywords in a portable way? I tried different things that came to mind
> (such as -fkeep-static-consts) but they did not work, e.g.,
> -fkeep-static-consts emits static consts only when optimization is not
> on.
>
> A bonus question: can anyone with Debian/Ubuntu/OpenSuSE/whatever
> check whether it works? If it is a RedHat-specific or generic for all
> GCCs is important.
>
> --
> Oleg Goldshmidt | pub at goldshmidt.org
>
> _______________________________________________
> Linux-il mailing list
> Linux-il at cs.huji.ac.il
> http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.cs.huji.ac.il/pipermail/linux-il/attachments/20111018/1633d822/attachment.html>


More information about the Linux-il mailing list