Project:Toolchain/Gcc 10 porting notes/fno common

From Gentoo Wiki
Jump to:navigation Jump to:search

Overview

gcc-10 and above flipped a default from -fcommon to -fno-common.

This changed gcc code generator to emit globals without explicit initializer from .bss (via COMMON symbol type) to .data (via DEFAULT symbol type).

Example

The problem

GCC will reject multiple definitions of global variables starting from gcc-10:

CODE a.c
int a = 42;
CODE main.c
int a;
int main(){}
user $gcc a.c main.c -o main
ld: a.o:(.bss+0x0): multiple definition of `a'; main.o:(.data+0x0): first defined here
collect2: error: ld returned 1 exit status

The fix (source changes, preferred)

Explicitly mark declarations as such and avoid multiple definitions in order to fix the bug.

CODE a.c
int a = 42;
CODE main.c (PATCHED)
extern int a;
int main(){}

The -fcommon workaround (discouraged)

If the problem is not that easy to fix or you are afraid it can break the program you then can report a bug upstream to make the decision on a correct fix.

As a local workaround you can revert back to old behaviour:

CODE
src_configure() {
  # discouraged, source change fix is preferred
  append-cflags -fcommon # https://link/to/upstream/bug/report
  econf ...
}

Reproducing the build failure with older gcc versions

The following command can also be used to test your patch

user $CFLAGS="-fno-common" ebuild mycat/mypkg-1.2.3 clean compile install

Links