public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Xi Ruoyao <xry111@mengyan1223.wang>
To: Tom Kacvinsky <tkacvins@gmail.com>, gcc-help <gcc-help@gcc.gnu.org>
Subject: Re: GCC 8.3.0, -flto and violation of C++ One Definition Rule
Date: Wed, 29 Dec 2021 20:55:35 +0800	[thread overview]
Message-ID: <c5f64bb96e01912f434d43f5dfbc11d48e5d1a70.camel@mengyan1223.wang> (raw)
In-Reply-To: <CAG_eJLf1GyCTi+KW29Nqt19oV44jRMam8WbBsCEfhwEvf8jjkw@mail.gmail.com>

On Wed, 2021-12-29 at 06:44 -0500, Tom Kacvinsky via Gcc-help wrote:
> Hi,
> 
> First, using GCC 8.3.0 and binutils 2.37.I am trying to increase
> performance of linking our product, so I thought I'd give LTO a try.  So
> I am compiling all object files with -flto, and passing -flto to g++
> (which we use as our link driver).  However, what I have found is that
> some of our code violates the C++ One Definition Rule (-Werror=odr). This
> only happens when building with LTO - without LTO, the C++ rule is
> not violated.

No, it's violated all the time.  -Wodr only works with LTO because it's
impossible to detect ODR volations (across multiple TUs) just by linking
normal (non-LTO) object files.

> The problem exists with LTO using both the BFD and gold
> linkers.
> 
> So, my question is, since the LTO object files are now such that one
> needs to use gcc-nm to examine them (which I know is a wrapper around nm,
> and passes an option to load the LTO plugin). how can I leverage that to
> see if there are other translation units that define the class that ODR
> violation is complaining about?
> I did do a fairly thorough analysis of
> the object files and did not see there the particular class and methods
> would be multiply defined, but that was just based on symbol names from
> gcc-nm output.

Generally you can't find ODR violations this way.  ODR violation does
not mean multiple definitions of a symbol.  For example:

// in 1.cpp

enum A
{
	a = 1,
};

// in 2.cpp

enum A
{
	a = 2,
};

If you link the objects compiled from these TUs together, you will
violate ODR.  But these TUs won't produce any symbols for A or A::a at
all.

> I suspect there is more to this since the object files
> have LTO information now, and that is what I'd like to examine.
> 
> Any hints on how to move forward with diagnosing LTO link errors?

With GCC-11, the diagnostic shows the exact location of ODR violation,
like:

> t-1.cc:1:6: warning: type 'A' violates the C++ One Definition Rule [-Wodr]
>     1 | enum A
>       |      ^
> t-2.cc:3:6: note: an enum with different value name is defined in another translation unit
>     3 | enum A
>       |      ^
> t-1.cc:3:9: note: name 'a' is defined to 1 while another translation unit defines it as 2
>     3 |         a = 1,
>       |         ^
> t-2.cc:5:9: note: mismatching definition
>     5 |         a = 2,
>       |         ^
> 

I'm not sure how GCC 8 behaves.

-- 
Xi Ruoyao <xry111@mengyan1223.wang>
School of Aerospace Science and Technology, Xidian University

  reply	other threads:[~2021-12-29 12:55 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-29 11:44 Tom Kacvinsky
2021-12-29 12:55 ` Xi Ruoyao [this message]
2021-12-29 13:34   ` Tom Kacvinsky
2021-12-29 15:38 ` Jonathan Wakely
2021-12-29 16:15   ` Tom Kacvinsky
2021-12-29 17:01     ` Jonathan Wakely
2021-12-29 17:04       ` Jonathan Wakely
2021-12-29 18:18         ` Tom Kacvinsky
2021-12-29 19:36           ` Jonathan Wakely

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c5f64bb96e01912f434d43f5dfbc11d48e5d1a70.camel@mengyan1223.wang \
    --to=xry111@mengyan1223.wang \
    --cc=gcc-help@gcc.gnu.org \
    --cc=tkacvins@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).