构建ARM交叉工具链时libgcov.a缺失及功能异常的解决咨询
ARM交叉编译工具链缺失libgcov.a的解决方法
构建环境
- msys2-mingw64
- gcc版本:8.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)
- 源码包:binutils-2.34、gcc-11.4.0、gmp-6.1.0、isl-0.18、mpc-1.0.3、mpfr-3.1.4、newlib-4.1.0
- 配置脚本:
#! /bin/bash TOP_PATH="$(pwd)" TARGET_GCC=arm-ACoreOS-eabi INSTALL_PATH=${TOP_PATH}/install BUILD_PATH=${TOP_PATH}/build set -e set -x #Clean up the compilation process file rm -rf ${INSTALL_PATH} ${BUILD_PATH} mkdir ${INSTALL_PATH} ${BUILD_PATH} #Libs mkdir ${BUILD_PATH}/gmp cd ${BUILD_PATH}/gmp ../../src/gmp-6.1.0/configure --build=x86-w64-mingw32 --prefix=${INSTALL_PATH} --enable-static --disable-shared make all -j16 make install mkdir ${BUILD_PATH}/isl cd ${BUILD_PATH}/isl ../../src/isl-0.18/configure --build=x86-w64-mingw32 --prefix=${INSTALL_PATH} --enable-static --disable-shared --with-gmp-prefix=${INSTALL_PATH} make all -j16 make install mkdir ${BUILD_PATH}/mpfr cd ${BUILD_PATH}/mpfr ../../src/mpfr-3.1.4/configure --build=x86-w64-mingw32 --prefix=${INSTALL_PATH} --enable-static --disable-shared --with-gmp=${INSTALL_PATH} make all -j16 make install mkdir ${BUILD_PATH}/mpc cd ${BUILD_PATH}/mpc ../../src/mpc-1.0.3/configure --build=x86-w64-mingw32 --prefix=${INSTALL_PATH} --enable-static --disable-shared --with-gmp=${INSTALL_PATH} --with-mpfr=${INSTALL_PATH} make all -j16 make install #binutils mkdir ${BUILD_PATH}/binutils cd ${BUILD_PATH}/binutils ../../src/binutils-2.34/configure --build=x86-w64-mingw32 --target=${TARGET_GCC} --prefix=${INSTALL_PATH} --disable-multilib --disable-werror --enable-static --enable-shared make all -j16 make install #gcc all-gcc mkdir ${BUILD_PATH}/gcc cd ${BUILD_PATH}/gcc ../../src/gcc-11.4.0/configure --target=${TARGET_GCC} --prefix=${INSTALL_PATH} --enable-languages=c,c++ --enable-gcov --enable-plugin --enable-sanitizer --disable-shared --disable-nls --disable-libquadmath --disable-libssp --enable-version-specific-runtime-libs --with-gnu-as --with-gnu-ld --with-pkgversion='Anban ABFast GCC 2024-04-28' --disable-threads --disable-libstdcxx-pch --disable-bootstrap --disable-multilib --with-gmp=${INSTALL_PATH} --with-mpfr=${INSTALL_PATH} --with-mpc=${INSTALL_PATH} --with-isl=${INSTALL_PATH} --with-newlib --with-headers=/home/anban/src/newlib-4.1.0/newlib/libc/include --with-native-system-header=${INSTALL_PATH}/arm-ACoreOS-eabi/sys-include 'CFLAGS_FOR_TARGET=-O2 -g -fno-short-enums -mhard-float -mfpu=vfpv3 ' 'CXXFLAGS_FOR_TARGET=-O2 -g -fno-short-enums -mhard-float -mfpu=vfpv3' 'GOCFLAGS_FOR_TARGET=-O2 -g -fno-short-enums -mhard-float -mfpu=vfpv3' -v make all-gcc -j16 make install all-gcc #newlib export PATH=$PATH:${INSTALL_PATH}/bin mkdir ${BUILD_PATH}/newlib cd ${BUILD_PATH}/newlib ../../src/newlib-4.1.0/configure --target=${TARGET_GCC} --prefix=${INSTALL_PATH} --disable-multilib --disable-werror --disable-threads 'CFLAGS_FOR_TARGET=-O2 -g -fno-short-enums -mhard-float -mfpu=vfpv3 ' 'CXXFLAGS_FOR_TARGET=-O2 -g -fno-short-enums -mhard-float -mfpu=vfpv3 ' 'GOCFLAGS_FOR_TARGET=-O2 -g -fno-short-enums -mhard-float -mfpu=vfpv3' make all -j16 make install #gcc all cd ${BUILD_PATH}/gcc make all -j16 make install all echo ${TARGET_GCC} Compile complete!!!
问题现象
工具链构建成功,但产物中缺失libgcov.a。用该工具链编译后的目标代码反汇编显示__gcov_merge_add和__gcov_init都是空实现:
__gcov_merge_add: 0000000084016480: bx lr 33 void __gcov_init (struct gcov_info *p __attribute__ ((unused))) {} __gcov_init: 0000000084016484: bx lr
解决方案
1. 调整GCC配置参数
当前配置中的--disable-shared会影响libgcov的构建,需优化配置项:
- 保留依赖库的
--enable-static --disable-shared,但GCC配置中需确保gcov相关组件能正常编译 - 添加
--enable-gcov-support(GCC 11+专属的gcov完整支持开关) - 在
CFLAGS_FOR_TARGET和CXXFLAGS_FOR_TARGET中加入-fprofile-arcs -ftest-coverage,确保目标端编译时默认包含gcov所需的编译标记 - 确认
--with-newlib配合配置:newlib需开启必要的IO支持,可在newlib配置时添加--enable-newlib-io-long-long
修改后的GCC配置命令片段:
../../src/gcc-11.4.0/configure --target=${TARGET_GCC} --prefix=${INSTALL_PATH} \ --enable-languages=c,c++ --enable-gcov --enable-gcov-support \ --enable-plugin --enable-sanitizer --enable-static --disable-shared \ --disable-nls --disable-libquadmath --disable-libssp \ --enable-version-specific-runtime-libs --with-gnu-as --with-gnu-ld \ --with-pkgversion='Anban ABFast GCC 2024-04-28' --disable-threads \ --disable-libstdcxx-pch --disable-bootstrap --disable-multilib \ --with-gmp=${INSTALL_PATH} --with-mpfr=${INSTALL_PATH} --with-mpc=${INSTALL_PATH} \ --with-isl=${INSTALL_PATH} --with-newlib \ --with-headers=/home/anban/src/newlib-4.1.0/newlib/libc/include \ --with-native-system-header=${INSTALL_PATH}/arm-ACoreOS-eabi/sys-include \ 'CFLAGS_FOR_TARGET=-O2 -g -fno-short-enums -mhard-float -mfpu=vfpv3 -fprofile-arcs -ftest-coverage' \ 'CXXFLAGS_FOR_TARGET=-O2 -g -fno-short-enums -mhard-float -mfpu=vfpv3 -fprofile-arcs -ftest-coverage' \ -v
2. 手动调整GCC源码宏定义
若配置参数调整后仍无效,需修改GCC源码中的宏:
- 在
gcc/config/arm/t-arm文件中添加LIBGCOV = -lgcov,确保链接阶段自动关联libgcov - 检查
gcc/gcov-io.h,确保HAVE_GCOV_FLUSH宏已定义,若未定义则添加#define HAVE_GCOV_FLUSH 1 - 在
gcc/config/newlib.h中添加#define TARGET_GCOV_SUPPORT 1,开启newlib环境下的gcov支持
3. 重新编译工具链
清理旧构建目录,执行修改后的编译脚本:
rm -rf ${INSTALL_PATH} ${BUILD_PATH} mkdir ${INSTALL_PATH} ${BUILD_PATH} # 执行完整编译流程
4. 验证功能
编译测试代码时添加-fprofile-arcs -ftest-coverage选项,调用__gcov_flush()后检查是否生成.gcda文件,确认libgcov功能正常。
内容的提问来源于stack exchange,提问作者Liu yishou
相关产品推荐
相关产品推荐

