1
0
mirror of https://github.com/golang/go synced 2024-10-02 02:28:32 -06:00

cmd/go, runtime/cgo: rewrite darwin/arm panicmem setup to avoid init function

Init functions are problematic because we want cmd/link to be
able to insert an import of runtime/cgo for external linking.
For all the other systems that just means putting runtime/cgo into
the binary. The linker is not set up to generate calls to init functions,
and luckily this one can be avoided entirely.

This means people don't have to import _ "runtime/cgo" in their
iOS programs anymore. The linker's default import is now enough.

This CL also adjusts cmd/go to record the linker's default import,
now that the explicit import is gone.

Change-Id: I81d23476663e03664f90d531c24db2e4f2e6c66b
Reviewed-on: https://go-review.googlesource.com/68490
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: David Crawshaw <crawshaw@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Russ Cox 2017-10-05 11:11:29 -04:00
parent f22ef70254
commit 69b0b3ad22
7 changed files with 39 additions and 44 deletions

View File

@ -131,7 +131,24 @@ func isGOROOT(path string) bool {
// ExternalLinkingForced reports whether external linking is being
// forced even for programs that do not use cgo.
func ExternalLinkingForced() bool {
func ExternalLinkingForced(inGoroot bool) bool {
// Some targets must use external linking even inside GOROOT.
switch BuildContext.GOOS {
case "android":
return true
case "darwin":
switch BuildContext.GOARCH {
case "arm", "arm64":
return true
}
}
// Otherwise we disable forcing of external linking for GOROOT binaries.
// This is primarily for cgo, so we will be able to relax this soon.
if inGoroot {
return false
}
if !BuildContext.CgoEnabled {
return false
}
@ -151,5 +168,6 @@ func ExternalLinkingForced() bool {
linkmodeExternal = true
}
}
return BuildBuildmode == "c-shared" || BuildBuildmode == "plugin" || pieCgo || BuildLinkshared || linkmodeExternal
}

View File

@ -946,7 +946,7 @@ func (p *Package) load(stk *ImportStack, bp *build.Package, err error) {
importPaths = append(importPaths, "syscall")
}
if p.Name == "main" && !p.Goroot && cfg.ExternalLinkingForced() {
if p.Name == "main" && cfg.ExternalLinkingForced(p.Goroot) {
importPaths = append(importPaths, "runtime/cgo")
}

View File

@ -815,16 +815,10 @@ func builderTest(b *work.Builder, p *load.Package) (buildAction, runAction, prin
},
}
// The generated main also imports testing, regexp, os, and maybe runtime/cgo.
// The generated main also imports testing, regexp, and os.
stk.Push("testmain")
forceCgo := false
if cfg.BuildContext.GOOS == "darwin" {
if cfg.BuildContext.GOARCH == "arm" || cfg.BuildContext.GOARCH == "arm64" {
forceCgo = true
}
}
deps := testMainDeps
if cfg.ExternalLinkingForced() || forceCgo {
if cfg.ExternalLinkingForced(pmain.Goroot) {
deps = str.StringList(deps, "runtime/cgo")
}
for _, dep := range deps {
@ -889,8 +883,6 @@ func builderTest(b *work.Builder, p *load.Package) (buildAction, runAction, prin
recompileForTest(pmain, p, ptest)
}
t.NeedCgo = forceCgo
for _, cp := range pmain.Internal.Imports {
if len(cp.Internal.CoverVars) > 0 {
t.Cover = append(t.Cover, coverInfo{cp, cp.Internal.CoverVars})
@ -1319,7 +1311,6 @@ type testFuncs struct {
NeedTest bool
ImportXtest bool
NeedXtest bool
NeedCgo bool
Cover []coverInfo
}
@ -1448,10 +1439,6 @@ import (
{{range $i, $p := .Cover}}
_cover{{$i}} {{$p.Package.ImportPath | printf "%q"}}
{{end}}
{{if .NeedCgo}}
_ "runtime/cgo"
{{end}}
)
var tests = []testing.InternalTest{

View File

@ -0,0 +1,13 @@
// Copyright 2017 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.
// +build lldb
// +build darwin
// +build arm arm64
// Used by gcc_signal_darwin_armx.c when doing the test build during cgo.
// We hope that for real binaries the definition provided by Go will take precedence
// and the linker will drop this .o file altogether, which is why this definition
// is all by itself in its own file.
void xx_cgo_panicmem(void) {}

View File

@ -39,7 +39,8 @@
#include "libcgo.h"
#include "libcgo_unix.h"
uintptr_t x_cgo_panicmem;
void xx_cgo_panicmem(void);
uintptr_t x_cgo_panicmem = (uintptr_t)xx_cgo_panicmem;
static pthread_mutex_t mach_exception_handler_port_set_mu;
static mach_port_t mach_exception_handler_port_set = MACH_PORT_NULL;

View File

@ -8,7 +8,5 @@
#include <stdint.h>
uintptr_t x_cgo_panicmem;
void darwin_arm_init_thread_exception_port() {}
void darwin_arm_init_mach_exception_handler() {}

View File

@ -7,29 +7,7 @@
package cgo
import "unsafe"
//go:cgo_import_static x_cgo_panicmem
//go:linkname x_cgo_panicmem x_cgo_panicmem
var x_cgo_panicmem uintptr
// use a pointer to avoid relocation of external symbol in __TEXT
// make linker happy
var _cgo_panicmem = &x_cgo_panicmem
// TODO(crawshaw): move this into x_cgo_init, it will not run until
// runtime has finished loading, which may be after its use.
func init() {
*_cgo_panicmem = funcPC(panicmem)
}
func funcPC(f interface{}) uintptr {
var ptrSize = unsafe.Sizeof(uintptr(0))
return **(**uintptr)(add(unsafe.Pointer(&f), ptrSize))
}
func add(p unsafe.Pointer, x uintptr) unsafe.Pointer {
return unsafe.Pointer(uintptr(p) + x)
}
import _ "unsafe"
//go:cgo_export_static panicmem xx_cgo_panicmem
func panicmem()