Skip to content

Commit

Permalink
[release-branch.go1.12] cmd/link: put shlib ".type" functions in inte…
Browse files Browse the repository at this point in the history
…rnal ABI

These functions are compiler generated, and as such are only available
in the internal ABI. Doing this avoids generating an alias symbol.
Doing that avoids confusion between unmangled and mangled type symbols.

Updates #30768
Fixes #33040

Change-Id: I8aba3934ffa994b1a19fc442cfe3e05642792a25
Reviewed-on: https://go-review.googlesource.com/c/go/+/186278
Run-TryBot: Ian Lance Taylor <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Austin Clements <[email protected]>
  • Loading branch information
ianlancetaylor committed Jul 16, 2019
1 parent 8759b53 commit f194c9f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
7 changes: 7 additions & 0 deletions misc/cgo/testshared/shared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -917,3 +917,10 @@ func TestTestInstalledShared(t *testing.T) {
func TestGeneratedMethod(t *testing.T) {
goCmd(t, "install", "-buildmode=shared", "-linkshared", "issue25065")
}

// Test use of shared library struct with generated hash function.
// Issue 30768.
func TestGeneratedHash(t *testing.T) {
goCmd(nil, "install", "-buildmode=shared", "-linkshared", "issue30768/issue30768lib")
goCmd(nil, "test", "-linkshared", "issue30768")
}
11 changes: 11 additions & 0 deletions misc/cgo/testshared/src/issue30768/issue30768lib/lib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2019 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.

package issue30768lib

// S is a struct that requires a generated hash function.
type S struct {
A string
B int
}
22 changes: 22 additions & 0 deletions misc/cgo/testshared/src/issue30768/x_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2019 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.

package issue30768_test

import (
"testing"

"issue30768/issue30768lib"
)

type s struct {
s issue30768lib.S
}

func Test30768(t *testing.T) {
// Calling t.Log will convert S to an empty interface,
// which will force a reference to the generated hash function,
// defined in the shared library.
t.Log(s{})
}
12 changes: 10 additions & 2 deletions src/cmd/link/internal/ld/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -1802,7 +1802,15 @@ func ldshlibsyms(ctxt *Link, shlib string) {
if elf.ST_TYPE(elfsym.Info) == elf.STT_NOTYPE || elf.ST_TYPE(elfsym.Info) == elf.STT_SECTION {
continue
}
lsym := ctxt.Syms.Lookup(elfsym.Name, 0)

// Symbols whose names start with "type." are compiler
// generated, so make functions with that prefix internal.
ver := 0
if elf.ST_TYPE(elfsym.Info) == elf.STT_FUNC && strings.HasPrefix(elfsym.Name, "type.") {
ver = sym.SymVerABIInternal
}

lsym := ctxt.Syms.Lookup(elfsym.Name, ver)
// Because loadlib above loads all .a files before loading any shared
// libraries, any non-dynimport symbols we find that duplicate symbols
// already loaded should be ignored (the symbols from the .a files
Expand Down Expand Up @@ -1830,7 +1838,7 @@ func ldshlibsyms(ctxt *Link, shlib string) {
// the ABIs are actually different. We might have to
// mangle Go function names in the .so to include the
// ABI.
if elf.ST_TYPE(elfsym.Info) == elf.STT_FUNC {
if elf.ST_TYPE(elfsym.Info) == elf.STT_FUNC && ver == 0 {
alias := ctxt.Syms.Lookup(elfsym.Name, sym.SymVerABIInternal)
if alias.Type != 0 {
continue
Expand Down

0 comments on commit f194c9f

Please sign in to comment.