Bug 22395 - ODR violations
Summary: ODR violations
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: build (show other bugs)
Version: HEAD
: P2 normal
Target Milestone: 14.1
Assignee: Not yet assigned to anyone
URL:
Keywords:
: 28444 (view as bug list)
Depends on: 24835 30751 30757 30839
Blocks:
  Show dependency treegraph
 
Reported: 2017-11-04 00:41 UTC by Hi-Angel
Modified: 2023-09-11 20:02 UTC (History)
7 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed: 2021-03-11 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Hi-Angel 2017-11-04 00:41:11 UTC
Upon building gdb with -flto I'm getting dozens of errors like:

	nat/linux-btrace.h:85:8: warning: type ‘struct btrace_target_info’ violates the C++ One Definition Rule [-Wodr]
	 struct btrace_target_info
			^
	remote.c:13157:8: note: a different type is defined in another translation unit
	 struct btrace_target_info
			^
	nat/linux-btrace.h:102:5: note: the first difference of corresponding definitions is field ‘variant’
	   } variant;
		 ^
	remote.c:13157:8: note: a type with different number of fields is defined in another translation unit
	 struct btrace_target_info
			^

I didn't dig through all of them, but the primary reason seems to be that build-toolchain picks up "g++" as the compiler, whereas the gdb source code does not have .cpp files.
Comment 1 Pedro Alves 2017-11-06 10:34:58 UTC
Actually, GDB _is_ a C++ program nowadays [1].  The ODR violations should be fixed.

[1] - https://sourceware.org/gdb/wiki/cxx-conversion
Comment 2 Tom Tromey 2021-01-09 17:18:46 UTC
The 'compile' component refers to the 'compile' feature, not building gdb.
Comment 3 Tom Tromey 2021-03-11 19:47:17 UTC
I got bit debugging an ODR problem in gdb today.
I'd accidentally given a struct a name that was already in use.
This would be nice to fix.
Comment 4 Christian Biesinger 2021-03-12 17:03:35 UTC
When using gold as the linker, could try --detect-odr-violations
Comment 5 Pedro Alves 2021-03-12 17:46:45 UTC
(In reply to Tom Tromey from comment #3)
> I got bit debugging an ODR problem in gdb today.
> I'd accidentally given a struct a name that was already in use.
> This would be nice to fix.

Note that in C, it's valid to have different definitions of structures in different translation units.  If GDB isn't coping with that, then I'd say it's a GDB bug.

(Not saying we shouldn't fix the ODR violation, of course.)
Comment 6 Tom Tromey 2021-03-12 18:29:46 UTC
(In reply to Pedro Alves from comment #5)
> (In reply to Tom Tromey from comment #3)
> > I got bit debugging an ODR problem in gdb today.
> > I'd accidentally given a struct a name that was already in use.
> > This would be nice to fix.
> 
> Note that in C, it's valid to have different definitions of structures in
> different translation units.  If GDB isn't coping with that, then I'd say
> it's a GDB bug.
> 
> (Not saying we shouldn't fix the ODR violation, of course.)

What happened was that I accidentally introduced a violation into gdb.
The code was working for a while, but then I added another field to
the structure in question, and I was puzzled because vector.push_back was
crashing.  It took me a while to figure out why this could possibly
be happening.

I don't know if -Wodr would really catch this though.

(In reply to Christian Biesinger from comment #4)
> When using gold as the linker, could try --detect-odr-violations

I tried this just now but it didn't report anything.
That seems incorrect though.
Comment 7 Andreas Schwab 2021-03-12 18:58:21 UTC
-Wodr works together with -flto.
Comment 8 Tom Tromey 2022-03-06 17:10:47 UTC
*** Bug 28444 has been marked as a duplicate of this bug. ***
Comment 9 Tom Tromey 2022-03-06 17:11:28 UTC
The dup points out another ODR violation, not sure if it
is platform-specific or not.
Comment 10 Tom Tromey 2022-05-20 15:04:19 UTC
More fixes here:
https://sourceware.org/pipermail/gdb-patches/2022-May/189194.html
Comment 11 Tom Tromey 2022-06-02 15:30:27 UTC
I checked in those fixes, but there are still more ODR violations remaining,
so this bug should stay open.
Comment 12 Agostino Sarubbo 2022-11-08 11:01:31 UTC
We are still reproducing it in gentoo with 12.1:

https://bugs.gentoo.org/853898

ada-exp.c.tmp:576:7: error: type ‘union YYSTYPE’ violates the C++ One Definition Rule [-Werror=odr]
Comment 13 Tom Tromey 2022-11-10 22:12:33 UTC
I looked into this a bit, but fixing the YYSTYPE one is
a real pain unless perhaps we are willing to require bison
so that we can use a bison-specific feature.

The usual remapping approach doesn't work because
the generated .c file checks:

#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
union YYSTYPE
{
...

I guess maybe we could write out the union by hand but
that seems pretty ugly.
Comment 14 Tom de Vries 2023-08-14 09:30:52 UTC
(In reply to Hi-Angel from comment #0)
> 	nat/linux-btrace.h:85:8: warning: type ‘struct btrace_target_info’ violates
> the C++ One Definition Rule [-Wodr]
> 	 struct btrace_target_info
> 			^
> 	remote.c:13157:8: note: a different type is defined in another translation
> unit
> 	 struct btrace_target_info
> 			^
> 	nat/linux-btrace.h:102:5: note: the first difference of corresponding
> definitions is field ‘variant’
> 	   } variant;
> 		 ^
> 	remote.c:13157:8: note: a type with different number of fields is defined
> in another translation unit
> 	 struct btrace_target_info
> 			^

I've filed this particular one as PR30751, and cc-ed the btrace maintainer.
Comment 15 Tom de Vries 2023-08-14 12:25:52 UTC
(In reply to Tom Tromey from comment #13)
> I looked into this a bit, but fixing the YYSTYPE one is
> a real pain unless perhaps we are willing to require bison
> so that we can use a bison-specific feature.
> 
> The usual remapping approach doesn't work because
> the generated .c file checks:
> 
> #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
> union YYSTYPE
> {
> ...
> 
> I guess maybe we could write out the union by hand but
> that seems pretty ugly.

I've proposed a patch for this ( https://sourceware.org/pipermail/gdb-patches/2023-August/201600.html ).
Comment 16 Sourceware Commits 2023-08-14 16:32:44 UTC
The master branch has been updated by Tom de Vries <vries@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=6a93ab8af49be41a44af1b4651e9ab2ebc6d2f77

commit 6a93ab8af49be41a44af1b4651e9ab2ebc6d2f77
Author: Tom de Vries <tdevries@suse.de>
Date:   Mon Aug 14 18:32:29 2023 +0200

    [gdb/build] Fix enum param_types odr violation
    
    When building gdb with -O2 -flto, I run into:
    ...
    gdb/guile/scm-param.c:121:6: warning: type 'param_types' violates the C++ \
      One Definition Rule [-Wodr]
     enum param_types
          ^
    gdb/python/py-param.c:33:6: note: an enum with different value name is \
      defined in another translation unit
     enum param_types
          ^
    ...
    
    Fix this by renaming to enum scm_param_types and py_param_types.
    
    Tested on x86_64-linux.
    
    Approved-By: Tom Tromey <tom@tromey.com>
    
    PR build/22395
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=22395
Comment 17 Sourceware Commits 2023-08-14 16:32:49 UTC
The master branch has been updated by Tom de Vries <vries@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=9972aac27d5d664a29abc88acd3a84c1e72064c4

commit 9972aac27d5d664a29abc88acd3a84c1e72064c4
Author: Tom de Vries <tdevries@suse.de>
Date:   Mon Aug 14 18:32:29 2023 +0200

    [gdb/build] Fix struct token_and_value odr violation
    
    When build gdb with -O2 -flto I run into:
    ...
    gdb/c-exp.y:3003:8: warning: type 'struct token_and_value' violates the C++ \
      One Definition Rule [-Wodr]
     struct token_and_value
            ^
    gdb/d-exp.y:1310:8: note: a different type is defined in another translation \
      unit
     struct token_and_value
            ^
    ...
    
    Fix this by renaming to c_token_and_value and d_token_and_value.
    
    Likewise in gdb/go-exp.y, renaming to go_token_and_value.
    
    Tested on x86_64-linux.
    
    Approved-By: Tom Tromey <tom@tromey.com>
    
    PR build/22395
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=22395
Comment 18 Sourceware Commits 2023-08-14 16:32:54 UTC
The master branch has been updated by Tom de Vries <vries@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=e72b937dddaf24d99ec1bf3beda4d8ecf3cd368c

commit e72b937dddaf24d99ec1bf3beda4d8ecf3cd368c
Author: Tom de Vries <tdevries@suse.de>
Date:   Mon Aug 14 18:32:29 2023 +0200

    [gdb/build] Fix struct token odr violation
    
    When building gdb with -O2 -flto I run into:
    ...
    /data/vries/gdb/src/gdb/c-exp.y:2450:8: warning: type 'struct token' \
      violates the C++ One Definition Rule [-Wodr]
     struct token
            ^
    /data/vries/gdb/src/gdb/d-exp.y:939:8: note: a different type is defined in \
      another translation unit
     struct token
            ^
    ...
    
    Fix this by renaming to c_token and d_token.
    
    Likewise in:
    - fortran-exp.y, renaming to f_token,
    - go-exp.y, renaming to go_token, and
    - p-exp.y, renaming to p_token.
    
    Tested on x86_64-linux.
    
    Approved-By: Tom Tromey <tom@tromey.com>
    
    PR build/22395
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=22395
Comment 19 Sourceware Commits 2023-08-14 20:53:01 UTC
The master branch has been updated by Tom de Vries <vries@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=bc6c74b1406cd9f71b432ac61799c1d1bea60a39

commit bc6c74b1406cd9f71b432ac61799c1d1bea60a39
Author: Tom de Vries <tdevries@suse.de>
Date:   Mon Aug 14 22:52:52 2023 +0200

    [gdb/build] Fix YYSTYPE and yyalloc odr violation
    
    When building gdb with -O2 -flto I run into:
    ...
    ada-exp.c.tmp:576:7: error: type âunion YYSTYPEâ violates the C++ One \
      Definition Rule [-Werror=odr]
    ...
    
    Fix this by renaming to ada_exp_YYSTYPE and likewise for other .y files.
    
    Likewise for yyalloc.
    
    Tested on x86_64-linux.  Also tested with byacc rather than bison on
    suggestion of Tom Tromey.
    
    Approved-By: Tom Tromey <tom@tromey.com>
    
    PR build/22395
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=22395
Comment 20 Sourceware Commits 2023-08-17 15:10:12 UTC
The master branch has been updated by Tom de Vries <vries@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=0c9546b152f6b01756475ce259c895d3fa446774

commit 0c9546b152f6b01756475ce259c895d3fa446774
Author: Tom de Vries <tdevries@suse.de>
Date:   Thu Aug 17 17:09:39 2023 +0200

    [gdb/build] Fix yysymbol_kind_t odr violation
    
    When building gdb with -O2 -flto on openSUSE Tumbleweed (using bison 3.8.2) I
    run into:
    ...
    ada-exp.c.tmp:653: warning: type 'yysymbol_kind_t' violates the C++ One \
      Definition Rule [-Wodr]
    c-exp.c.tmp:398: note: an enum with different value name is defined in \
      another translation unit
    ada-exp.c.tmp:660: note: name 'YYSYMBOL_NULL_PTR' differs from name \
      'YYSYMBOL_COMPLEX_INT' defined in another translation unit
    c-exp.c.tmp:405: note: mismatching definition
    ...
    
    Fix this by renaming to ada_exp_yysymbol_kind_t and likewise for other .y
    files.
    
    Tested on x86_64-linux.
    
    PR build/22395
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=22395
Comment 21 Tom de Vries 2023-09-11 07:29:36 UTC
AFAICT, no more ODR warnings, so I'm marking this fixed.