a multiple definition link error when using gcc10.3.1

1 环境说明

硬件环境:LS1046A   arm64 

系统环境:openEuler release 22.03 (LTS-SP1)   gcc10.3.1

2 问题现象

一个客户在设备上编译 Suricata 3.1.1 时发现链接时有很多  multiple definition 的错误,

类似如下:

/usr/bin/ld: decode-sctp.o:/root/suricata-3.1.1/src/suricata.h:129: multiple definition of `data_queues';
 alert-debuglog.o:/root/suricata-3.1.1/src/suricata.h:129: first defined here
/usr/bin/ld: decode-sctp.o:/root/suricata-3.1.1/src/suricata.h:127: multiple definition of `trans_q'; alert-debuglog.o:/root/suricata-3.1.1/src/suricata.h:127: first defined here

3 复现步骤

wget http://www.openinfosecfoundation.org/download/suricata-3.1.1.tar.gz

yum makecache


dnf install -y gcc libpcap-devel pcre-devel libyaml-devel file-devel \
  zlib-devel jansson-devel nss-devel libcap-ng-devel libnet-devel tar make \
  libnetfilter_queue-devel lua-devel  libmaxminddb-devel rustc cargo \
  lz4-devel libnfnetlink-devel 
  

修改suricata的一个内核兼容性问题,否则会报错:
  在文件src/source-af-packet.c 添加 #include <linux/sockios.h>


cd suricata-3.1.1
./configure
make -j8

--->
问题multiple definition 海量出现

/usr/bin/ld: decode-sctp.o:/root/suricata-3.1.1/src/suricata.h:129: multiple definition of `data_queues';
 alert-debuglog.o:/root/suricata-3.1.1/src/suricata.h:129: first defined here
/usr/bin/ld: decode-sctp.o:/root/suricata-3.1.1/src/suricata.h:127: multiple definition of `trans_q'; alert-debuglog.o:/root/suricata-3.1.1/src/suricata.h:127: first defined here

4 测试和排查

Ubuntu 20.04.6 LTS  +  gcc version 9.4.0  (X86/ARM64)+ suricata-3.1.1  编译测试OK

openEuler release 22.03 (LTS-SP1) 原装系统内核 + gcc-10.3.1   + suricata-3.1.1  编译测试失败

openEuler release 22.03 (LTS-SP1)   + gcc-9.3 + + suricata-3.1.1  编译测试 OK

 关于如何给gcc降低版本,可以参考如下:

dnf search gcc 看不到低版本gcc 所以只能手动安装

https://archives.openeuler.openatom.cn/openEuler-20.09/source/Packages/

wget https://archives.openeuler.openatom.cn/openEuler-20.09/OS/aarch64/Packages/gcc-9.3.1-20200922.12.oe1.aarch64.rpm
wget https://archives.openeuler.openatom.cn/openEuler-20.09/OS/aarch64/Packages/gcc-c%2B%2B-9.3.1-20200922.12.oe1.aarch64.rpm
wget https://archives.openeuler.openatom.cn/openEuler-20.09/OS/aarch64/Packages/gcc-gfortran-9.3.1-20200922.12.oe1.aarch64.rpm
wget https://archives.openeuler.openatom.cn/openEuler-20.09/OS/aarch64/Packages/libquadmath-9.3.1-20200922.12.oe1.aarch64.rpm
wget https://archives.openeuler.openatom.cn/openEuler-20.09/OS/aarch64/Packages/libquadmath-devel-9.3.1-20200922.12.oe1.aarch64.rpm
wget https://archives.openeuler.openatom.cn/openEuler-20.09/OS/aarch64/Packages/libstdc%2B%2B-9.3.1-20200922.12.oe1.aarch64.rpm
wget https://archives.openeuler.openatom.cn/openEuler-20.09/OS/aarch64/Packages/libgomp-9.3.1-20200922.12.oe1.aarch64.rpm
wget https://archives.openeuler.openatom.cn/openEuler-20.09/OS/aarch64/Packages/cpp-9.3.1-20200922.12.oe1.aarch64.rpm
wget https://archives.openeuler.openatom.cn/openEuler-20.09/OS/aarch64/Packages/libstdc%2B%2B-devel-9.3.1-20200922.12.oe1.aarch64.rpm
wget https://archives.openeuler.openatom.cn/openEuler-20.09/OS/aarch64/Packages/libgfortran-9.3.1-20200922.12.oe1.aarch64.rpm

dnf remove gcc

rpm -Uvh --force *.rpm

综上应该是和gcc有关,查看 gcc10的 changelog 

GCC 10 Release Series — Changes, New Features, and Fixes- GNU Project

That documents the change in default behaviour from -fcommon (before 10.1.0) to -fno-common (10.1.0 onwards). 

 GCC now defaults to -fno-common. As a result, global variable accesses are more efficient on various targets. In C, global variables with multiple tentative definitions now result in linker errors. With -fcommon such definitions are silently merged during linking.

关于 gcc 编译选项 -fcommon    -fno-common的解释:

-fcommon
In C code, this option controls the placement of global variables defined without an initializer, known as tentative definitions in the C standard. Tentative definitions are distinct from declarations of a variable with the extern keyword, which do not allocate storage.

The default is -fno-common, which specifies that the compiler places uninitialized global variables in the BSS section of the object file. This inhibits the merging of tentative definitions by the linker so you get a multiple-definition error if the same variable is accidentally defined in more than one compilation unit.

The -fcommon places uninitialized global variables in a common block.This allows the linker to resolve all tentative definitions of the same variable in different compilation units to the same object,  or to a non-tentative definition. This behavior is inconsistent with C++, and on many targets implies a speed and code size penalty on global variable references. It is mainly useful to enable legacy code to link without errors.

 

5 解决方法

编译前执行 export CFLAGS+=-fcommon

  • 22
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值