使用NDK21编译Android版Valgrind遇问题求助
为Android编译Valgrind遇到的问题
背景
官方文档显示AARCH64/Android是Valgrind的支持平台,但编译相关文档最后更新于2014年。操作环境为WSL2 Ubuntu系统,执行的编译配置步骤如下:
git clone git://sourceware.org/git/valgrind.git cd valgrind export NDK_ROOT=/opt/ndk/android-ndk-r21e export TOOLCHAIN_ROOT=${NDK_ROOT}/toolchains/llvm/prebuilt/linux-x86_64/bin ANDROID_API=23 TARGET=aarch64-linux-android PREFIX=${TARGET}${ANDROID_API} export CC=${TOOLCHAIN_ROOT}/${PREFIX}-clang export CXX=${TOOLCHAIN_ROOT}/${PREFIX}-clang++ export AR=${TOOLCHAIN_ROOT}/${TARGET}-ar export LD=${TOOLCHAIN_ROOT}/${TARGET}-ld export STRIPE=${TOOLCHAIN_ROOT}/${TARGET}-strip export NM=${TOOLCHAIN_ROOT}/${TARGET}-nm export RANLIB=${TOOLCHAIN_ROOT}/${TARGET}-ranlib ./autogen.sh mkdir build && cd build ../configure --host=${TARGET} --target=${TARGET} --prefix=${PWD}/inst
编译异常现象
- Valgrind似乎仅适配GCC,但当前使用了clang
- 出现不应存在的
-m64编译参数 - 编译日志中同时出现
-m32和-m64参数 - 汇编代码出现clang无法兼容的语法错误
问题
- 我的操作存在哪些错误?
- 是否可以使用现代NDK编译Android版Valgrind?
- 有哪些可用的替代方案?
配置输出及编译日志
Configure输出
Version: 3.21.0.GIT Maximum build arch: arm64 Primary build arch: arm64 Secondary build arch: arm Build OS: linux Link Time Optimisation: no Primary build target: ARM64_LINUX Secondary build target: ARM_LINUX Platform variant: android Primary -DVGPV string: -DVGPV_arm64_linux_android=1 Default supp files: ../xfree-3.supp ../xfree-4.supp ../bionic.supp
Build日志
[...] make[3]: Entering directory '/home/edukaj/qcom/valgrind/build/VEX' /opt/ndk/android-ndk-r21e/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android23-clang -DHAVE_CONFIG_H -I. -I../../VEX -I.. -I../.. -I../../include -I../include -I../../VEX/pub -I../VEX/pub -DVGA_arm64=1 -DVGO_linux=1 -DVGP_arm64_linux=1 -DVGPV_arm64_linux_android=1 -Ipriv -m64 -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations -Wcast-align -Wcast-qual -Wwrite-strings -Wempty-body -Wformat -Wformat-security -Wignored-qualifiers -Wenum-conversion -finline-functions -fno-stack-protector -fno-strict-aliasing -fno-builtin -Wno-cast-align -Wno-self-assign -Wno-tautological-compare -Wbad-function-cast -fstrict-aliasing -MT priv/libvex_arm64_linux_a-main_globals.o -MD -MP -MF priv/.deps/libvex_arm64_linux_a-main_globals.Tpo -c -o priv/libvex_arm64_linux_a-main_globals.o `test -f 'priv/main_globals.c' || echo '../../VEX/'`priv/main_globals.c mv -f priv/.deps/libvex_arm64_linux_a-main_globals.Tpo priv/.deps/libvex_arm64_linux_a-main_globals.Po /opt/ndk/android-ndk-r21e/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android23-clang -DHAVE_CONFIG_H -I. -I../../VEX -I.. -I../.. -I../../include -I../include -I../../VEX/pub -I../VEX/pub -DVGA_arm64=1 -DVGO_linux=1 -DVGP_arm64_linux=1 -DVGPV_arm64_linux_android=1 -Ipriv -m64 -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations -Wcast-align -Wcast-qual -Wwrite-strings -Wempty-body -Wformat -Wformat-security -Wignored-qualifiers -Wenum-conversion -finline-functions -fno-stack-protector -fno-strict-aliasing -fno-builtin -Wno-cast-align -Wno-self-assign -Wno-tautological-compare -Wbad-function-cast -fstrict-aliasing -MT priv/libvex_arm64_linux_a-main_main.o -MD -MP -MF priv/.deps/libvex_arm64_linux_a-main_main.Tpo -c -o priv/libvex_arm64_linux_a-main_main.o `test -f 'priv/main_main.c' || echo '../../VEX/'`priv/main_main.c mv -f priv/.deps/libvex_arm64_linux_a-main_main.Tpo priv/.deps/libvex_arm64_linux_a-main_main.Po [...] /opt/ndk/android-ndk-r21e/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android23-clang -DHAVE_CONFIG_H -I. -I../../VEX -I.. -I../.. -I../../include -I../include -I../../VEX/pub -I../VEX/pub -DVGA_arm=1 -DVGO_linux=1 -DVGP_arm_linux=1 -DVGPV_arm_linux_android=1 -Ipriv -m32 -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations -Wcast-align -Wcast-qual -Wwrite-strings -Wempty-body -Wformat -Wformat-security -Wignored-qualifiers -Wenum-conversion -finline-functions -fno-stack-protector -fno-strict-aliasing -fno-builtin -Wno-cast-align -Wno-self-assign -Wno-tautological-compare -marm -mcpu=cortex-a8 -Wbad-function-cast -fstrict-aliasing -MT priv/libvex_arm_linux_a-host_ppc_defs.o -MD -MP -MF priv/.deps/libvex_arm_linux_a-host_ppc_defs.Tpo -c -o priv/libvex_arm_linux_a-host_ppc_defs.o `test -f 'priv/host_ppc_defs.c' || echo '../../VEX/'`priv/host_ppc_defs.c mv -f priv/.deps/libvex_arm_linux_a-host_ppc_defs.Tpo priv/.deps/libvex_arm_linux_a-host_ppc_defs.Po [...] /opt/ndk/android-ndk-r21e/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android23-clang -m64 -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations -Wcast-align -Wcast-qual -Wwrite-strings -Wempty-body -Wformat -Wformat-security -Wignored-qualifiers -Wenum-conversion -finline-functions -fno-stack-protector -fno-strict-aliasing -fno-builtin -Wno-cast-align -Wno-self-assign -Wno-tautological-compare -static -m64 -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations -Wcast-align -Wcast-qual -Wwrite-strings -Wempty-body -Wformat -Wformat-security -Wignored-qualifiers -Wenum-conversion -finline-functions -fno-stack-protector -fno-strict-aliasing -fno-builtin -Wno-cast-align -Wno-self-assign -Wno-tautological-compare -o vgdb vgdb-vgdb.o vgdb-vgdb-invoker-none.o /opt/ndk/android-ndk-r21e/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android23-clang -DHAVE_CONFIG_H -I. -I../../coregrind -I.. -I../.. -I../../include -I../include -I../../VEX/pub -I../VEX/pub -DVGA_arm64=1 -DVGO_linux=1 -DVGP_arm64_linux=1 -DVGPV_arm64_linux_android=1 -I../../coregrind -DVG_LIBDIR="\"/home/edukaj/qcom/valgrind/build/inst/libexec/valgrind\"" -DVG_PLATFORM="\"arm64-linux\"" -m64 -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations -Wcast-align -Wcast-qual -Wwrite-strings -Wempty-body -Wformat -Wformat-security -Wignored-qualifiers -Wenum-conversion -finline-functions -fno-stack-protector -fno-strict-aliasing -fno-builtin -Wno-cast-align -Wno-self-assign -Wno-tautological-compare -O -g -fno-omit-frame-pointer -fno-strict-aliasing -fpic -fno-builtin -MT vgpreload_core_arm64_linux_so-vg_preloaded.o -MD -MP -MF .deps/vgpreload_core_arm64_linux_so-vg_preloaded.Tpo -c -o vgpreload_core_arm64_linux_so-vg_preloaded.o `test -f 'vg_preloaded.c' || echo '../../coregrind/'`vg_preloaded.c ../../coregrind/vg_preloaded.c:118:5: warning: inline asm clobber list contains reserved registers: X18 [-Winline-asm] CALL_FN_W_v(result, fn); ^ ../include/valgrind.h:4316:10: note: expanded from macro 'CALL_FN_W_v' VALGRIND_ALIGN_STACK \ ^ ../include/valgrind.h:4301:7: note: expanded from macro 'VALGRIND_ALIGN_STACK' "mov x21, sp\n\t" \ ^ <inline asm>:1:1: note: instantiated into assembly here mov x21, sp ^ ../../coregrind/vg_preloaded.c:118:5: note: Reserved registers on the clobber list may not be preserved across the asm statement, and clobbering them may lead to undefined behaviour. CALL_FN_W_v(result, fn); ^ ../include/valgrind.h:4316:10: note: expanded from macro 'CALL_FN_W_v' VALGRIND_ALIGN_STACK \ ^ ../include/valgrind.h:4301:7: note: expanded from macro 'VALGRIND_ALIGN_STACK' "mov x21, sp\n\t" \ ^ <inline asm>:1:1: note: instantiated into assembly here mov x21, sp ^ 1 warning generated. mv -f .deps/vgpreload_core_arm64_linux_so-vg_preloaded.Tpo .deps/vgpreload_core_arm64_linux_so-vg_preloaded.Po /opt/ndk/android-ndk-r21e/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android23-clang -m64 -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations -Wcast-align -Wcast-qual -Wwrite-strings -Wempty-body -Wformat -Wformat-security -Wignored-qualifiers -Wenum-conversion -finline-functions -fno-stack-protector -fno-strict-aliasing -fno-builtin -Wno-cast-align -Wno-self-assign -Wno-tautological-compare -O -g -fno-omit-frame-pointer -fno-strict-aliasing -fpic -fno-builtin -nodefaultlibs -shared -Wl,-z,interpose,-z,initfirst -nostdlib -m64 -o vgpreload_core-arm64-linux.so vgpreload_core_arm64_linux_so-vg_preloaded.o /opt/ndk/android-ndk-r21e/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android23-clang -DHAVE_CONFIG_H -I. -I../../coregrind -I.. -I../.. -I../../include -I../include -I../../VEX/pub -I../VEX/pub -DVGA_arm=1 -DVGO_linux=1 -DVGP_arm_linux=1 -DVGPV_arm_linux_android=1 -I../../coregrind -DVG_LIBDIR="\"/home/edukaj/qcom/valgrind/build/inst/libexec/valgrind\"" -DVG_PLATFORM="\"arm-linux\"" -m32 -O2 -g -Wall -Wmissing-prototypes -Wshadow -Wpointer-arith -Wstrict-prototypes -Wmissing-declarations -Wcast-align -Wcast-qual -Wwrite-strings -Wempty-body -Wformat -Wformat-security -Wignored-qualifiers -Wenum-conversion -finline-functions -fno-stack-protector -fno-strict-aliasing -fno-builtin -Wno-cast-align -Wno-self-assign -Wno-tautological-compare -marm -mcpu=cortex-a8 -O -g -fno-omit-frame-pointer -fno-strict-aliasing -fpic -fno-builtin -MT vgpreload_core_arm_linux_so-vg_preloaded.o -MD -MP -MF .deps/vgpreload_core_arm_linux_so-vg_preloaded.Tpo -c -o vgpreload_core_arm_linux_so-vg_preloaded.o `test -f 'vg_preloaded.c' || echo '../../coregrind/'`vg_preloaded.c <inline asm>:1:41: error: expected '%<type>' or "<type>" .pushsection ".debug_gdb_scripts", "MS",@progbits,1 ^ <inline asm>:4:13: error: .popsection without corresponding .pushsection .popsection ^ 2 errors generated. make[3]: *** [Makefile:9394: vgpreload_core_arm_linux_so-vg_preloaded.o] Error 1 make[3]: Leaving directory '/home/edukaj/qcom/valgrind/build/coregrind' make[2]: *** [Makefile:2484: all] Error 2 make[2]: Leaving directory '/home/edukaj/qcom/valgrind/build/coregrind' make[1]: *** [Makefile:907: all-recursive] Error 1 make[1]: Leaving directory '/home/edukaj/qcom/valgrind/build' make: *** [Makefile:770: all] Error 2
解答
1. 操作中的错误
- 编译器选型不当:Valgrind的底层汇编和构建逻辑对GCC兼容性更好,你使用了clang,但Valgrind旧代码路径未适配clang的汇编语法(比如日志中
.pushsection的参数格式错误,clang对GNU汇编的部分语法支持与GCC不一致)。 - 多架构编译冗余:配置输出显示同时编译arm64和32位arm架构,导致日志中同时出现
-m64和-m32参数,既无必要又增加出错概率,你只需要aarch64目标,应禁用32位编译。 - 工具链环境不一致:你指定的AR、LD等工具是旧的GCC工具链前缀,却搭配clang编译器使用,导致工具链环境混乱。
- 冗余编译参数:aarch64的clang默认就是64位目标,
-m64参数是Valgrind针对x86架构的逻辑残留,在arm64环境下完全多余,还可能引发问题。
2. 是否可以用现代NDK编译Android版Valgrind?
可以,但需要针对性调整:
- 若使用NDK r21e,优先切换到NDK自带的GCC工具链,将CC、CXX指定为
${TOOLCHAIN_ROOT}/${TARGET}${ANDROID_API}-gcc和对应的g++,避开clang的汇编语法
相关产品推荐
相关产品推荐

