cmd/link: fix in-package syso linking

CL 146297 ignored archive members with short names that don't have
the .o suffix, however, it also ignored .syso files as well.
This change restores the original .syso behavior and adds a test.

As the test is basically following a shell script, we make use of
the existing cmd/go/testdata/script framework. To support running
C compiler in the script, we added a `cc` command, which runs the
C compiler along with correct platform specific arguments.

Fixes #29253.

Change-Id: If8520151c4d6a74ab9fe84d34bff9a4480688815
Reviewed-on: https://go-review.googlesource.com/c/154109
Run-TryBot: Minux Ma <minux@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
Shenghou Ma 2018-12-13 23:55:22 -05:00 committed by Minux Ma
parent fd323a8cff
commit 281ce28c50
4 changed files with 49 additions and 2 deletions

View file

@ -27,6 +27,7 @@ import (
"cmd/go/internal/imports"
"cmd/go/internal/par"
"cmd/go/internal/txtar"
"cmd/go/internal/work"
)
// TestScript runs the tests in testdata/script/*.txt.
@ -343,6 +344,7 @@ Script:
//
var scriptCmds = map[string]func(*testScript, bool, []string){
"addcrlf": (*testScript).cmdAddcrlf,
"cc": (*testScript).cmdCc,
"cd": (*testScript).cmdCd,
"chmod": (*testScript).cmdChmod,
"cmp": (*testScript).cmdCmp,
@ -378,6 +380,17 @@ func (ts *testScript) cmdAddcrlf(neg bool, args []string) {
}
}
// cc runs the C compiler along with platform specific options.
func (ts *testScript) cmdCc(neg bool, args []string) {
if len(args) < 1 || (len(args) == 1 && args[0] == "&") {
ts.fatalf("usage: cc args... [&]")
}
var b work.Builder
b.Init()
ts.cmdExec(neg, append(b.GccCmd(".", ""), args...))
}
// cd changes to a different directory.
func (ts *testScript) cmdCd(neg bool, args []string) {
if neg {

View file

@ -84,6 +84,10 @@ when testing.Short() is false.
The commands are:
- [!] cc args... [&]
Run the C compiler, the platform specific flags (i.e. `go env GOGCCFLAGS`) will be
added automatically before args.
- cd dir
Change to the given directory for future commands.

View file

@ -0,0 +1,28 @@
# This test tests that we can link in-package syso files that provides symbols
# for cgo. See issue 29253.
[!cgo] stop
[!gc] stop
cc -c -o pkg/o.syso ext.c
go build main.go
-- ext.c --
// +build ignore
int f() { return 42; }
-- pkg/pkg.go --
package pkg
// extern int f(void);
import "C"
func init() {
if v := C.f(); v != 42 {
panic(v)
}
}
-- main.go --
package main
import _ "pkg"
func main() {}

View file

@ -872,8 +872,10 @@ func loadobjfile(ctxt *Link, lib *sym.Library) {
// Skip other special (non-object-file) sections that
// build tools may have added. Such sections must have
// short names so that the suffix is not truncated.
if len(arhdr.name) < 16 && !strings.HasSuffix(arhdr.name, ".o") {
continue
if len(arhdr.name) < 16 {
if ext := filepath.Ext(arhdr.name); ext != ".o" && ext != ".syso" {
continue
}
}
pname := fmt.Sprintf("%s(%s)", lib.File, arhdr.name)