Skip to content

Commit 37d5d2e

Browse files
committed
update
1 parent 02fa1b3 commit 37d5d2e

13 files changed

+71
-106
lines changed

src/runtime/cgo.go

-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import "unsafe"
2020
//go:linkname _cgo_pthread_key_created _cgo_pthread_key_created
2121
//go:linkname _cgo_bindm _cgo_bindm
2222
//go:linkname _cgo_getstackbound _cgo_getstackbound
23-
//go:linkname _cgo_is_musl _cgo_is_musl
2423

2524
var (
2625
_cgo_init unsafe.Pointer
@@ -33,7 +32,6 @@ var (
3332
_cgo_pthread_key_created unsafe.Pointer
3433
_cgo_bindm unsafe.Pointer
3534
_cgo_getstackbound unsafe.Pointer
36-
_cgo_is_musl unsafe.Pointer
3735
)
3836

3937
// iscgo is set to true by the runtime/cgo package

src/runtime/cgo/callbacks.go

-7
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,3 @@ var _cgo_yield unsafe.Pointer
151151
var x_cgo_getstackbound byte
152152
var _cgo_getstackbound = &x_cgo_getstackbound
153153

154-
// x_cgo_is_musl is set to 1 if the C library is musl.
155-
156-
//go:cgo_import_static x_cgo_is_musl
157-
//go:linkname x_cgo_is_musl x_cgo_is_musl
158-
//go:linkname _cgo_is_musl _cgo_is_musl
159-
var x_cgo_is_musl byte
160-
var _cgo_is_musl = &x_cgo_is_musl
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2016 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build linux
6+
7+
package cgo
8+
9+
import _ "unsafe" // for go:linkname
10+
11+
// x_cgo_is_musl is set to 1 if the C library is musl.
12+
13+
//go:cgo_import_static x_cgo_is_musl
14+
//go:linkname x_cgo_is_musl x_cgo_is_musl
15+
//go:linkname _cgo_is_musl _cgo_is_musl
16+
var x_cgo_is_musl byte
17+
var _cgo_is_musl = &x_cgo_is_musl

src/runtime/cgo/gcc_libinit.c

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <stdlib.h>
1818
#include <string.h> // strerror
1919
#include <time.h>
20-
#include <limits.h>
2120
#include "libcgo.h"
2221
#include "libcgo_unix.h"
2322

src/runtime/cgo/gcc_linux.c

+1-11
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,4 @@ threadentry(void *v)
6464

6565
crosscall1(ts.fn, setg_gcc, ts.g);
6666
return nil;
67-
}
68-
69-
// x_cgo_is_musl reports whether the C library is musl.
70-
int
71-
x_cgo_is_musl() {
72-
#if defined(__GLIBC__) || defined(__UCLIBC__)
73-
return 0;
74-
#else
75-
return 1;
76-
#endif
77-
}
67+
}

src/runtime/cgo/gcc_linux_amd64.c

-10
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,3 @@ threadentry(void *v)
9090
crosscall1(ts.fn, setg_gcc, (void*)ts.g);
9191
return nil;
9292
}
93-
94-
// x_cgo_is_musl reports whether the C library is musl.
95-
int
96-
x_cgo_is_musl() {
97-
#if defined(__GLIBC__) || defined(__UCLIBC__)
98-
return 0;
99-
#else
100-
return 1;
101-
#endif
102-
}

src/runtime/cgo/gcc_linux_arm64.c

-10
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,3 @@ x_cgo_init(G *g, void (*setg)(void*), void **tlsg, void **tlsbase)
8686
x_cgo_inittls(tlsg, tlsbase);
8787
}
8888
}
89-
90-
// x_cgo_is_musl reports whether the C library is musl.
91-
int
92-
x_cgo_is_musl() {
93-
#if defined(__GLIBC__) || defined(__UCLIBC__)
94-
return 0;
95-
#else
96-
return 1;
97-
#endif
98-
}

src/runtime/cgo/gcc_musl_linux.c

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2009 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build linux
6+
7+
// x_cgo_is_musl reports whether the C library is musl.
8+
int x_cgo_is_musl() {
9+
#if defined(__GLIBC__) || defined(__UCLIBC__)
10+
return 0;
11+
#else
12+
return 1;
13+
#endif
14+
}

src/runtime/cgo_musl_linux.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2015 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build linux
6+
7+
package runtime
8+
9+
import "unsafe"
10+
11+
//go:linkname _cgo_is_musl _cgo_is_musl
12+
var _cgo_is_musl unsafe.Pointer

src/runtime/os_linux.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ var auxvreadbuf [128]uintptr
233233
func sysargs(argc int32, argv **byte) {
234234
n := argc + 1
235235

236-
// auxv on argv is not available on musl library/archive. avoid for musl.
237-
if !((islibrary || isarchive) && GOOS == "linux" && isMusl()) {
236+
// auxv on argv is not available on musl library/archive.
237+
if !libmusl {
238238
// skip over argv, envp to get to auxv
239239
for argv_index(argv, n) != nil {
240240
n++
@@ -360,6 +360,11 @@ func readRandom(r []byte) int {
360360
}
361361

362362
func goenvs() {
363+
if libmusl {
364+
// Read envs from /proc/self/environ instead
365+
envs = readNullTerminatedStringsFromFile(procEnviron)
366+
return
367+
}
363368
goenvs_unix()
364369
}
365370

@@ -370,6 +375,7 @@ func goenvs() {
370375
//go:nosplit
371376
//go:nowritebarrierrec
372377
func libpreinit() {
378+
libmusl = asmcgocall(_cgo_is_musl, nil) == 1
373379
initsig(true)
374380
}
375381

src/runtime/proc.go

+15-51
Original file line numberDiff line numberDiff line change
@@ -728,13 +728,21 @@ const (
728728
_GoidCacheBatch = 16
729729
)
730730

731-
// cpuinit sets up CPU feature flags and calls internal/cpu.Initialize. env should be the complete
732-
// value of the GODEBUG environment variable.
733-
func cpuinit(env string) {
731+
// cpuinit sets up CPU feature flags and calls internal/cpu.Initialize.
732+
func cpuinit() {
734733
switch GOOS {
735734
case "aix", "darwin", "ios", "dragonfly", "freebsd", "netbsd", "openbsd", "illumos", "solaris", "linux":
736735
cpu.DebugOptions = true
737736
}
737+
738+
// find GODEBUG in envs
739+
var env string // env should be the complete value of the GODEBUG environment variable.
740+
for _, value := range envs {
741+
if stringslite.HasPrefix(value, "GODEBUG=") {
742+
env = value
743+
break
744+
}
745+
}
738746
cpu.Initialize(env)
739747

740748
// Support cpu feature variables are used in code generated by the compiler
@@ -753,49 +761,6 @@ func cpuinit(env string) {
753761
}
754762
}
755763

756-
// getGodebugEarly extracts the environment variable GODEBUG from the environment on
757-
// Unix-like operating systems and returns it. This function exists to extract GODEBUG
758-
// early before much of the runtime is initialized.
759-
func getGodebugEarly() string {
760-
const prefix = "GODEBUG="
761-
var env string
762-
switch GOOS {
763-
case "aix", "darwin", "ios", "dragonfly", "freebsd", "netbsd", "openbsd", "illumos", "solaris", "linux":
764-
// Similar to goenv_unix but extracts the environment value for
765-
// GODEBUG directly.
766-
// TODO(moehrmann): remove when general goenvs() can be called before cpuinit()
767-
768-
// If the binary is an archive or a library, the operating system is Linux,
769-
// and the system uses Musl, then read the environment variables from the
770-
// /proc/self/environ file. Iterate over each null-terminated string read
771-
// from the file. If any string has the specified prefix, return that string.
772-
if (isarchive || islibrary) && GOOS == "linux" && isMusl() {
773-
for _, value := range readNullTerminatedStringsFromFile(procEnviron) {
774-
if stringslite.HasPrefix(value, prefix) {
775-
return value
776-
}
777-
}
778-
return env
779-
}
780-
781-
n := int32(0)
782-
for argv_index(argv, argc+1+n) != nil {
783-
n++
784-
}
785-
786-
for i := int32(0); i < n; i++ {
787-
p := argv_index(argv, argc+1+i)
788-
s := unsafe.String(p, findnull(p))
789-
790-
if stringslite.HasPrefix(s, prefix) {
791-
env = gostring(p)[len(prefix):]
792-
break
793-
}
794-
}
795-
}
796-
return env
797-
}
798-
799764
// The bootstrap sequence is:
800765
//
801766
// call osinit
@@ -841,10 +806,10 @@ func schedinit() {
841806
moduledataverify()
842807
stackinit()
843808
mallocinit()
844-
godebug := getGodebugEarly()
845-
cpuinit(godebug) // must run before alginit
846-
randinit() // must run before alginit, mcommoninit
847-
alginit() // maps, hash, rand must not be used before this call
809+
goenvs()
810+
cpuinit() // must run before alginit
811+
randinit() // must run before alginit, mcommoninit
812+
alginit() // maps, hash, rand must not be used before this call
848813
mcommoninit(gp.m, -1)
849814
modulesinit() // provides activeModules
850815
typelinksinit() // uses maps, activeModules
@@ -855,7 +820,6 @@ func schedinit() {
855820
initSigmask = gp.m.sigmask
856821

857822
goargs()
858-
goenvs()
859823
secure()
860824
checkfds()
861825
parsedebugvars()

src/runtime/runtime1.go

+1-12
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@ var (
5959
argv **byte
6060
)
6161

62-
// isMusl reports whether the Go program is linked with musl libc.
63-
func isMusl() bool {
64-
return asmcgocall(_cgo_is_musl, nil) == 1
65-
}
66-
6762
// nosplit for use in linux startup sysargs.
6863
//
6964
//go:nosplit
@@ -83,7 +78,7 @@ func goargs() {
8378
}
8479

8580
// musl-linux library: Read argv from /proc/self/cmdline instead
86-
if (isarchive || islibrary) && GOOS == "linux" && isMusl() {
81+
if libmusl {
8782
argslice = readNullTerminatedStringsFromFile(procCmdline)
8883
return
8984
}
@@ -95,12 +90,6 @@ func goargs() {
9590
}
9691

9792
func goenvs_unix() {
98-
// musl-linux library: Read envs from /proc/self/environ instead
99-
if (isarchive || islibrary) && GOOS == "linux" && isMusl() {
100-
envs = readNullTerminatedStringsFromFile(procEnviron)
101-
return
102-
}
103-
10493
// TODO(austin): ppc64 in dynamic linking mode doesn't
10594
// guarantee env[] will immediately follow argv. Might cause
10695
// problems.

src/runtime/runtime2.go

+3
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,9 @@ var (
12481248
isarchive bool // -buildmode=c-archive
12491249
)
12501250

1251+
// libmusl is set if the Go archive/library is linked against linux musl libc.
1252+
var libmusl bool // set by os_linux libpreinit
1253+
12511254
// Must agree with internal/buildcfg.FramePointerEnabled.
12521255
const framepointer_enabled = GOARCH == "amd64" || GOARCH == "arm64"
12531256

0 commit comments

Comments
 (0)