Skip to content

cmd/link: internal linking for -buildmode=c-shared on android/arm64 #82111

Open

Description

@kolkov

Follow-up to #81450 (see cherrymui's comment: in scope of that project, tracked here). Narrower successor of #38918, which @eliasnaur closed as a subset of #81450.

What did you do

$ GOOS=android GOARCH=arm64 CGO_ENABLED=0 go build -buildmode=c-shared -o libapp.so .

Go version: go1.27.2

What did you see

-buildmode=c-shared requires external (cgo) linking, but cgo is not enabled

What did you expect to see

A loadable ET_DYN .so without a C toolchain, as internal linking already does for -buildmode=pie on android/arm64.

mustLinkExternal returns true, "buildmode=c-shared" for every non-wasm target. With CGO_ENABLED=0 the build is refused; with CGO_ENABLED=1 the build requires CC pointing at the NDK cross-compiler (clang/lld) even though the module has no C source files, so a Go library for Android (System.loadLibrary) always needs NDK. android/arm64 already supports internal PIE linking, so this is the delta on top of that: emit ET_DYN with a dynamic section, .init_array for _rt0_arm64_linux_lib, and dynsym exports. (#38918 shows what happens if only the mustLinkExternal check is removed: bionic rejects the result with unexpected e_type: 2.)

Acceptance criteria

  • GOOS=android GOARCH=arm64 go build -buildmode=c-shared with no C toolchain produces a .so that System.loadLibrary accepts
  • entry symbols come from //export in binding mode (proposal: cmd/cgo: cgo without a C toolchain #81450, no .c files); whether that mode is reachable with an explicit CGO_ENABLED=0 is being decided there
  • runtime starts from .init_array, survives Activity recreation and entry from Android-owned threads
  • LOAD segments 16 KB-aligned (Google Play requirement since Nov 2025)

Evidence

besmpl/go@d31f0b5f + https://github.com/besmpl/go-android-cgo-free-prototype: a CGO_ENABLED=0 .so with one exported JNI symbol loads on API 29/30/36 arm64 emulators, survives Activity recreation and concurrent entry, and renders through a real SurfaceView. The prototype still links with NDK lld, so it covers the compiler/runtime side, not the linker; the APK-level harness is available as a test bed for the internal-linking work.

Activity

  1. added this to the Go1.28 milestone on Oct 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

compiler/runtimeIssues related to the Go compiler and/or runtime.

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions