[PATCH] fix PR 46376, mix LTO, MinGW and virtual base classes

Rodrigo Rivas rodrigorivascosta@gmail.com
Tue Nov 9 17:32:00 GMT 2010


>> Instead their DECL_COMDAT_GROUP is set, but curiously DECL_COMDAT is still 0.
>
> Hm, that sounds like the bug to me.  Can you check why this is so?

Yeah, that was what I though at first, but there are several things
that made me doubt.
First of all: this only fails when using -flto. When linking normally
it works, so it must be something related to LTO.
Second, this text from tree.h:

/* Used in a DECL to indicate that, even if it TREE_PUBLIC, it need
   not be put out unless it is needed in this translation unit.
   Entities like this are shared across translation units (like weak
   entities), but are guaranteed to be generated by any translation
   unit that needs them, and therefore need not be put out anywhere
   where they are not needed.  DECL_COMDAT is just a hint to the
   back-end; it is up to front-ends which set this flag to ensure
   that there will never be any harm, other than bloat, in putting out
   something which is DECL_COMDAT.  */
#define DECL_COMDAT(NODE) \
  (DECL_WITH_VIS_CHECK (NODE)->decl_with_vis.comdat_flag)

#define DECL_COMDAT_GROUP(NODE) \
  (DECL_WITH_VIS_CHECK (NODE)->decl_with_vis.comdat_group)

/* Used in TREE_PUBLIC decls to indicate that copies of this DECL in
   multiple translation units should be merged.  */
#define DECL_ONE_ONLY(NODE) (DECL_COMDAT_GROUP (NODE) != NULL_TREE)


If we are to believe the comments, the DECL_ONE_ONLY is the one that
should be true in the case of a virtual base thunk.

Third, there are some other hints to this hidden in comments, here and
there. See in varasm.c the comment to decl_replaceable_p.

/* A replaceable function or variable is one which may be replaced
   at link-time with an entirely different definition, provided that the
   replacement has the same type.  For example, functions declared
   with __attribute__((weak)) on most systems are replaceable.

   COMDAT functions are not replaceable, since all definitions of the
   function must be equivalent.  It is important that COMDAT functions
   not be treated as replaceable so that use of C++ template
   instantiations is not penalized.  */

And since my patched function is named
lto_symtab_resolve_replaceable_p... well changing COMDAT to TRUE might
be seen as risky.

And four and last, the function decl_replaceable_p returns FALSE when
DECL_COMDAT is TRUE, so...


Also, virtual thunk functions pass through "make_decl_one_only" in
varasm.c, that uses the MAKE_DECL_ONE_ONLY if defined.
I tried to define it for MinGW the same as other targets:

#define MAKE_DECL_ONE_ONLY(DECL) (DECL_WEAK (DECL) = 1)

This solved the bug, but also made all my vtables disappear!

Regards.
Rodrigo



More information about the Gcc-patches mailing list