Skip to content

all: support Android arm64 without Cgo - #572

Open
houko wants to merge 1 commit into
ebitengine:mainfrom
houko:android-nocgo
Open

houko wants to merge 1 commit into
ebitengine:mainfrom
houko:android-nocgo

Conversation

@houko

@houko houko commented Nov 23, 2026

Copy link
Copy Markdown

What issue is this addressing?

There is no existing issue. Related: #565 (Go 1.28's Cgo without a C compiler), which would eventually cover the same need from the toolchain side.

What type of issue is this addressing?

feature

What this PR does | solves

PureGo currently requires CGO_ENABLED=1 on Android. Building with GOOS=android GOARCH=arm64 CGO_ENABLED=0 fails:

./dlfcn_android.go:21:13: undefined: cgo.Dlopen
./dlfcn_android.go:25:13: undefined: cgo.Dlsym
./dlfcn_android.go:29:13: undefined: cgo.Dlclose

A cross-platform UI framework I work on builds its Android arm64 apps with CGO_ENABLED=0 (Go links android/arm64 internally, so no NDK is needed to build the app) and needs to load native shared libraries at run time. Today that means vendoring a patched PureGo; this PR makes it work upstream instead.

Changes:

  • dlfcn_android.go keeps only the RTLD_* constants. The existing Cgo implementation moves unchanged to dlfcn_cgo_android.go (//go:build cgo).
  • Without Cgo, Android uses the same dlfcn.go implementation as Linux and the other Unix platforms. dlfcn_nocgo_android.go imports dlopen, dlsym, dlerror and dlclose from Bionic's libdl.so; dlfcn_nocgo_linux.go (which imports libdl.so.2) is excluded on Android. The existing dlfcn_stubs.s is shared.
  • fakecgo: gen.go now also generates zsymbols_android.go. It has the same symbols as Linux, but imports them from Bionic's libc.so, which also contains the pthread functions (there is no libpthread.so on Android). Bionic has no __errno_location; its equivalent is __errno, so the generator gained a small Renames map to import a symbol under a different name. zsymbols_linux.go gets !android, and ztrampolines_linux.s is shared because the Go-side symbol names do not change. Running go generate changes nothing else.
  • README: Android arm64 now has its own footnote saying CGO_ENABLED=0 works on Android 10 (API level 29) and later.
  • CI: a cross-compile check for GOOS=android GOARCH=arm64 CGO_ENABLED=0.
  • TestDlopenAndDlsymErrors covers the error paths of Dlopen and Dlsym, which on Android without Cgo now go through Dlerror.

Testing

  • On a physical Pixel 3 XL (Android 12, API 31, arm64), with the same prebuilt C libraries and skip list as the Android CI job (PUREGO_TEST_PREBUILT_LIBDIR, -test.skip 'TestSetuidEtc|TestNestedDlopenCall'): the root package test binary built with CGO_ENABLED=0 passes, with exactly the same set of passed and skipped tests as the same binary built with CGO_ENABLED=1 and the NDK's clang. The internal/fakecgo test binary built with CGO_ENABLED=0 also passes there. The CGO_ENABLED=0 binary's only NEEDED entries are libdl.so and libc.so, and it imports __errno.
  • go build and go vet for GOOS=android GOARCH=arm64 with CGO_ENABLED=0, and with CGO_ENABLED=1 plus the NDK's clang.
  • CGO_ENABLED=0 go generate ./internal/fakecgo leaves the tree clean.
  • On linux/amd64: go test . ./internal/... with CGO_ENABLED=1 and CGO_ENABLED=0, go vet ./..., gofmt -s -l ., and the cross builds from the workflow (linux 386/arm/arm64/loong64/ppc64le/riscv64/s390x, darwin, windows, freebsd with -std, netbsd, illumos).
  • On darwin/arm64: go vet ./... and go test . with and without Cgo, and go test ./internal/fakecgo without Cgo.

Limitations

  • Only android/arm64 can be built without Cgo, because Go requires external linking for the other Android architectures (android/amd64 requires external (cgo) linking, but cgo is not enabled). The code itself is not arm64-specific.
  • CGO_ENABLED=0 requires Android 10 (API level 29) or later. runtime/cgo's gcc_android.c searches for a usable TLS slot for g on older Android versions; fakecgo does not, so it relies on the runtime's default tls_g offset, which is TLS_SLOT_APP and is only free on Android 10 and later. Older devices keep needing CGO_ENABLED=1.
  • The Android CI job runs only on the amd64 emulator, which needs Cgo, so CI checks that the Cgo-free arm64 configuration builds but cannot run it.

On Android, Dlopen, Dlsym and Dlclose always went through internal/cgo, so building with CGO_ENABLED=0 failed with "undefined: cgo.Dlopen". Even with that fixed, fakecgo imported its libc and pthread functions from glibc's "libc.so.6" and "libpthread.so.0", which do not exist on Android.

Use the Cgo-free dlfcn.go implementation on Android when Cgo is disabled, importing the dlfcn functions from Bionic's "libdl.so". The Cgo implementation moves to dlfcn_cgo_android.go unchanged.

Generate zsymbols_android.go for fakecgo with the same symbols as Linux, imported from Bionic's "libc.so", which also contains the pthread functions. Bionic has no __errno_location; its equivalent is __errno, so the generator can now import a symbol under a different name. zsymbols_linux.go is excluded on Android, and ztrampolines_linux.s is shared.

Go links only android/arm64 internally, so this is the only Android architecture that can build without Cgo. fakecgo does not do runtime/cgo's TLS slot discovery for Android versions before 10 and relies on the runtime's default slot, so CGO_ENABLED=0 requires Android 10 (API level 29) or later. Document this in the README, add a cross-compile check to CI, and add a test for the Dlopen and Dlsym error paths, which now go through Dlerror on Android without Cgo.
@hajimehoshi

Copy link
Copy Markdown
Member

Thanks, but what is a motivation to make the Android version pure Go?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants