TechBuzzPro

Cross Compile for ARM on FreeBSD: Step-by-Step Guide

· 5 min read · Apps & Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

You cross compile for ARM on FreeBSD by installing the aarch64 (or armv7) cross-binutils and cross-clang from ports or packages, then building with CC=clang --target=aarch64-unknown-freebsd and pointing the linker at the ARM sysroot. The shortest working path is: pkg install llvm lld binutils-aarch64, then make CC=clang CFLAGS="--target=aarch64-unknown-freebsd14.0" LDFLAGS="-fuse-ld=lld --target=aarch64-unknown-freebsd14.0". The rest of this guide explains the sysroot, the exact toolchain flags, and the mistakes that produce broken ARM binaries. Menu labels and package names can differ slightly by FreeBSD version and architecture, so check pkg search if a name is not found.

What You'll Need

  • A FreeBSD host (amd64 or arm64) running a supported release, with root or doas/sudo access to install packages.
  • The llvm and lld packages (clang and the LLVM linker), plus the cross binutils for your target: binutils-aarch64 for 64-bit ARM or binutils-armv7 for 32-bit ARM.
  • An ARM sysroot: the target's headers and libraries. The easiest source is a FreeBSD ARM release image's base.txz, extracted into a directory such as /usr/local/aarch64-freebsd.
  • The source code you intend to build, and a build system that respects CC, CFLAGS and LDFLAGS (autotools, CMake, plain Makefiles).
  • Enough disk space for the sysroot and build tree, and a working network connection for pkg.

Step-by-Step

  1. Install the cross toolchain packages. Run pkg install llvm lld binutils-aarch64 for 64-bit ARM, or replace the last package with binutils-armv7 for 32-bit ARM. If pkg reports the package is missing, run pkg search binutils to see the exact name in your repository.
  2. Obtain an ARM sysroot. Download a FreeBSD ARM release image that matches your host's major version, mount or extract it, and unpack base.txz into a dedicated directory, for example mkdir -p /usr/local/aarch64-freebsd then tar -xpf base.txz -C /usr/local/aarch64-freebsd. This gives you /usr/local/aarch64-freebsd/usr/include and /usr/local/aarch64-freebsd/usr/lib. Do not point the compiler at the host's own /usr/include, or you will mix amd64 headers into ARM objects.
  3. Confirm the target triple. Use aarch64-unknown-freebsd14.0 for 64-bit ARM on FreeBSD 14, or armv7-unknown-freebsd14.0 for 32-bit ARM. Match the version number to the sysroot you extracted; a mismatch causes missing-symbol and header errors at link time.
  4. Set the compiler and linker flags. A reliable baseline is: CC=clang, CFLAGS="--target=aarch64-unknown-freebsd14.0 --sysroot=/usr/local/aarch64-freebsd -O2 -pipe", and LDFLAGS="-fuse-ld=lld --target=aarch64-unknown-freebsd14.0 --sysroot=/usr/local/aarch64-freebsd". Export them in your shell or pass them on the make command line.
  5. Build with the flags applied. For autotools projects run ./configure --host=aarch64-unknown-freebsd14.0 CC=clang CFLAGS="..." LDFLAGS="..." then make. For CMake, create a toolchain file that sets CMAKE_SYSTEM_NAME to FreeBSD, CMAKE_SYSTEM_PROCESSOR to aarch64, CMAKE_C_COMPILER to clang, and the target/sysroot flags in CMAKE_C_FLAGS. For plain Makefiles, pass the same variables on the make line.
  6. Verify the output architecture. Run file ./yourbinary and confirm it reports ELF 64-bit LSB executable, ARM aarch64 (or ARM, EABI5 for armv7). If it says x86-64, the flags were not picked up by the build system.
  7. Test on real ARM hardware or an emulator. Copy the binary to an ARM FreeBSD machine, or run it under qemu-aarch64-static with the sysroot passed via -L. Check that shared libraries resolve with ldd on the target; a binary that links on the host but cannot find libc.so.7 on the target usually has a sysroot path baked in incorrectly.

Common Mistakes

  • Forgetting --sysroot on the link step. Compiling succeeds but linking picks up host amd64 libraries, producing either an x86-64 binary or a flood of undefined symbols.
  • Using GNU ld instead of lld. The base FreeBSD linker is not a cross linker. Always pass -fuse-ld=lld or invoke ld.lld explicitly.
  • Mixing target triples. Building some objects for armv7 and others for aarch64 in the same tree gives confusing ABI errors. Clean the build directory after changing the triple.
  • Pointing at the host include path. Adding -I/usr/include drags in amd64-specific definitions and breaks the build in subtle ways.
  • Assuming build systems honour CC. Some hardcode gcc; override the compiler variable the project actually uses, and check the configure output for the compiler it selected.

Tips

  • Keep the sysroot version aligned with the target's running FreeBSD major version to avoid ABI surprises.
  • Put the flags in a small shell script or a CMake toolchain file so every build uses the same values.
  • Use -pipe and a parallel make -j$(sysctl -n hw.ncpu) to speed up large builds.
  • When a build fails, inspect the actual command line with make V=1 or make VERBOSE=1; it shows whether your flags reached the compiler.
  • For ports, the PkgBase and cross-build infrastructure in /usr/src can build world and kernel for ARM, but for individual applications the manual toolchain above is simpler and more predictable.

Troubleshooting

If the build fails immediately with "unknown target" or "unsupported option", your clang is too old or the triple is malformed; update llvm and re-check the spelling of the triple. If linking reports undefined references to libc symbols, the sysroot is incomplete or the link step is missing --sysroot. If the binary runs on the host but not on the target, check with file and readelf -d for the interpreter and needed libraries, and confirm the target has the same FreeBSD major version. If pkg install cannot find the binutils package, search the repository with pkg search; package names occasionally change between releases. If a configure script insists on a native compiler, set both CC and --host, and remove any cached config.cache before re-running.

Frequently Asked Questions

Do I need a full ARM sysroot, or will headers alone work?

You need both headers and libraries. Compiling only needs headers, but linking needs the target's libc, libm and startup objects, which come from the sysroot's usr/lib.

Can I use GCC instead of clang?

Yes, if you install a cross GCC package that targets FreeBSD ARM. Clang with lld is the simpler route because the base system already ships clang and the target triple is handled directly.

Why does file still show x86-64?

The build system ignored your CC or CFLAGS. Run the build verbosely and confirm the compiler command line contains --target and --sysroot.

Can I cross compile the FreeBSD kernel this way?

Kernel and world builds use the in-tree cross-build infrastructure under /usr/src with make buildworld TARGET=aarch64, not the manual application toolchain described here. Use that path for system components.

How do I test the binary without ARM hardware?

Install qemu-user-static from packages and run the binary with qemu-aarch64-static -L /usr/local/aarch64-freebsd ./yourbinary. It is slower than real hardware but sufficient for smoke tests.

Does the sysroot version have to match the host?

No, but it should match the target system's FreeBSD major version. A sysroot newer than the target can introduce symbols the target's libc does not provide.