I’m trying to cross-compile my C++ project from x86_64 to arm using zig. The resulting binary segfaults which can be reproduced with qemu.
I reduced the example. I use WSL2 Ubuntu 22.04 and zig 0.14.0-dev.1457+7e3180487 (a recent one).
Steps to reproduce
- Create file main.cpp
#include <iostream> int main() { std::cout << "All your codebase" << std::endl; return 0; }
- Build
zig build-exe main.cpp -target arm-linux-gnueabi -mcpu cortex_a72 -OReleaseSafe -lc++
- Run
qemy-arm -L /usr/arm-linux-gnueabi main
- Get signal 11 (Segmentation fault).
Things to note
- I’m building for Cortex-A72 in a 32-bit mode. This CPU supports 32 and 64-bit modes. Looks like I can’t emulate this CPU in a 32-bit mode with qemu, so it’s a generic
qemu-arm
. Not sure whether it’s important. - Building in Debug makes it work. All Release* modes segfault.
- Changing the
std::cout
line tostd::puts("All your codebase");
makes it work. - Adding
puts
beforecout
like so
also segfaults and doesn’t print any of those strings.std::puts("All your codebase"); std::cout << "are belong to us" << std::endl;
- Building for arm-linux-musleabi works in all build modes.
It’s my first time using qemu. Do I use it correctly? Maybe it’s a libc++ issue? If so, why does Debug work?