]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: fix in-package syso linking
authorShenghou Ma <minux@golang.org>
Fri, 14 Dec 2018 04:55:22 +0000 (23:55 -0500)
committerMinux Ma <minux@golang.org>
Sat, 15 Dec 2018 01:38:39 +0000 (01:38 +0000)
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>
src/cmd/go/script_test.go
src/cmd/go/testdata/script/README
src/cmd/go/testdata/script/cgo_syso_issue29253.txt [new file with mode: 0644]
src/cmd/link/internal/ld/lib.go

index 284b3548c451c724adb4e871cdef82024dc57604..4aa92625dde3cce13fea67ee05d6b5f061af524b 100644 (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 {
index 76d4b36b017ca66edb6e911534cf9acc0ce77ddc..a7b50fff1645456710fa262e580d3d5b3f102781 100644 (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.
 
diff --git a/src/cmd/go/testdata/script/cgo_syso_issue29253.txt b/src/cmd/go/testdata/script/cgo_syso_issue29253.txt
new file mode 100644 (file)
index 0000000..0d18fa9
--- /dev/null
@@ -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() {}
index 755693b27ec74371cc4316c084c07fd1e8bc1cc0..253a9f68475c6144e34d5bc1213821dbb6a02c60 100644 (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)