Repository navigation
Conversation
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.
Member
|
Thanks, but what is a motivation to make the Android version pure Go? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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=1on Android. Building withGOOS=android GOARCH=arm64 CGO_ENABLED=0fails: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.gokeeps only theRTLD_*constants. The existing Cgo implementation moves unchanged todlfcn_cgo_android.go(//go:build cgo).dlfcn.goimplementation as Linux and the other Unix platforms.dlfcn_nocgo_android.goimportsdlopen,dlsym,dlerroranddlclosefrom Bionic'slibdl.so;dlfcn_nocgo_linux.go(which importslibdl.so.2) is excluded on Android. The existingdlfcn_stubs.sis shared.gen.gonow also generateszsymbols_android.go. It has the same symbols as Linux, but imports them from Bionic'slibc.so, which also contains the pthread functions (there is nolibpthread.soon Android). Bionic has no__errno_location; its equivalent is__errno, so the generator gained a smallRenamesmap to import a symbol under a different name.zsymbols_linux.gogets!android, andztrampolines_linux.sis shared because the Go-side symbol names do not change. Runninggo generatechanges nothing else.CGO_ENABLED=0works on Android 10 (API level 29) and later.GOOS=android GOARCH=arm64 CGO_ENABLED=0.TestDlopenAndDlsymErrorscovers the error paths ofDlopenandDlsym, which on Android without Cgo now go throughDlerror.Testing
PUREGO_TEST_PREBUILT_LIBDIR,-test.skip 'TestSetuidEtc|TestNestedDlopenCall'): the root package test binary built withCGO_ENABLED=0passes, with exactly the same set of passed and skipped tests as the same binary built withCGO_ENABLED=1and the NDK's clang. Theinternal/fakecgotest binary built withCGO_ENABLED=0also passes there. TheCGO_ENABLED=0binary's onlyNEEDEDentries arelibdl.soandlibc.so, and it imports__errno.go buildandgo vetforGOOS=android GOARCH=arm64withCGO_ENABLED=0, and withCGO_ENABLED=1plus the NDK's clang.CGO_ENABLED=0 go generate ./internal/fakecgoleaves the tree clean.go test . ./internal/...withCGO_ENABLED=1andCGO_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).go vet ./...andgo test .with and without Cgo, andgo test ./internal/fakecgowithout Cgo.Limitations
android/amd64 requires external (cgo) linking, but cgo is not enabled). The code itself is not arm64-specific.CGO_ENABLED=0requires Android 10 (API level 29) or later. runtime/cgo'sgcc_android.csearches for a usable TLS slot forgon older Android versions; fakecgo does not, so it relies on the runtime's defaulttls_goffset, which isTLS_SLOT_APPand is only free on Android 10 and later. Older devices keep needingCGO_ENABLED=1.