[PATCHv2,6/9] make: Compile with -fno-common everything (criu/pie/etc)

Submitted by Dmitry Safonov on March 28, 2018, 4:19 p.m.


Message ID 20180328161953.16776-7-dima@arista.com
State New
Series "Large pages support for aarch64/ppc64"
Headers show

Commit Message

Dmitry Safonov March 28, 2018, 4:19 p.m.
With -fcommon gcc puts global variables without an initializer into
a common block. This block is later resolved by linker with splitting
*definitions* of variables of the same variable in different compilation
units to the same object.

In my opinion, we should disable this, as it's done in the kernel.
We'll have a compilation error if we *define* a global in different
.c files with the same name, rather silently merge it.
It also simplifies compel by omitting handling common block.

Here is a simple example:
   [test]$ ls
   a.c  b.c  Makefile
   [test]$ head *
   ==> a.c <==
   unsigned global;

   ==> b.c <==
   int global;

   int main()
   	return 0;

   ==> Makefile <==
   .o: .c
   	gcc ${CFLAGS} -c $^ -o $@

   all: a.o b.o
   	gcc ${CFLAGS} -o test a.o b.o

   	rm -f a.o b.o test

   PHONY: all clean
   [test]$ make
   cc    -c -o a.o a.c
   cc    -c -o b.o b.c
   gcc  -o test a.o b.o
   [test]$ make clean
   rm -f a.o b.o test
   [test]$ CFLAGS=-fno-common make
   cc -fno-common   -c -o a.o a.c
   cc -fno-common   -c -o b.o b.c
   gcc -fno-common -o test a.o b.o
   b.o:(.bss+0x0): multiple definition of `global'
   a.o:(.bss+0x0): first defined here
   collect2: error: ld returned 1 exit status
   make: *** [Makefile:5: all] Error 1

Also might be worth to read this issue:

Cc: Cyrill Gorcunov <gorcunov@openvz.org>
Signed-off-by: Dmitry Safonov <dima@arista.com>
 Makefile | 1 +
 1 file changed, 1 insertion(+)

Patch hide | download patch | download mbox

diff --git a/Makefile b/Makefile
index bdfb55b26921..0daf9c856ea3 100644
--- a/Makefile
+++ b/Makefile
@@ -88,6 +88,7 @@  export PROTOUFIX DEFINES
 # Independent options for all tools.
+DEFINES			+= -fno-common
 WARNINGS		:= -Wall -Wformat-security