aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/libinit.go
diff options
context:
space:
mode:
authorMichael Pratt <mpratt@google.com>2026-03-31 16:34:59 -0400
committerMichael Pratt <mpratt@google.com>2026-04-02 09:58:36 -0700
commit78d5260426899e934a4d680910b1484953e78087 (patch)
tree720363c8910cf1a4cf3b6e8318b2cc4e7c463742 /src/runtime/libinit.go
parentef90a565b50af191c4f20b62770b084d6978a88d (diff)
downloadgo-78d5260426899e934a4d680910b1484953e78087.tar.xz
runtime: use asmcgocall_no_g in libInit
libInit runs before rt0_go, which is where TLS setup occurs. Thus the contents of the TLS may not be defined, so the g lookup in asmcgocall is not safe. Concretely, android-386 c-shared builds crash without this change because asmcgocall reads an invalid non-zero g from the TLS. Move libInit to a file limited to GOARCH that support c-archive or c-shared so that only those require asmcgocall_no_g. In addition, loong64 and s390x need asmcgocall_no_g implementations. Fixes #78480. Cq-Include-Trybots: luci.golang.try:gotip-linux-loong64,gotip-linux-s390x Change-Id: I175e6d020339af89c9b576535d79c1e76a6a6964 Reviewed-on: https://go-review.googlesource.com/c/go/+/761541 Reviewed-by: Quim Muntal <quimmuntal@gmail.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
Diffstat (limited to 'src/runtime/libinit.go')
-rw-r--r--src/runtime/libinit.go34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/runtime/libinit.go b/src/runtime/libinit.go
new file mode 100644
index 0000000000..7e298a2eca
--- /dev/null
+++ b/src/runtime/libinit.go
@@ -0,0 +1,34 @@
+// Copyright 2026 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+//go:build 386 || amd64 || arm || arm64 || loong64 || ppc64 || ppc64le || riscv64 || s390x
+
+package runtime
+
+import (
+ "internal/abi"
+ "unsafe"
+)
+
+// libInit is common startup code for most architectures when
+// using -buildmode=c-archive or -buildmode=c-shared.
+//
+// May run with m.p==nil, so write barriers are not allowed.
+//
+//go:nowritebarrierrec
+//go:nosplit
+func libInit() {
+ // Synchronous initialization.
+ libpreinit()
+
+ // Asynchronous initialization.
+ // Prefer creating a thread via cgo if it is available.
+ if _cgo_sys_thread_create != nil {
+ // No g because the TLS is not set up until later in rt0_go.
+ asmcgocall_no_g(_cgo_sys_thread_create, unsafe.Pointer(abi.FuncPCABIInternal(rt0_lib_go)))
+ } else {
+ const stackSize = 0x800000 // 8192KB
+ newosproc0(stackSize, unsafe.Pointer(abi.FuncPCABIInternal(rt0_lib_go)))
+ }
+}