]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: if -B option was used, pass it to the external linker
authorIan Lance Taylor <iant@golang.org>
Wed, 15 Jul 2015 07:18:51 +0000 (00:18 -0700)
committerIan Lance Taylor <iant@golang.org>
Tue, 21 Jul 2015 03:57:55 +0000 (03:57 +0000)
Fixes #10607.

Change-Id: Ib294cf65b07103b60f62b86b21ff2d07b25136fe
Reviewed-on: https://go-review.googlesource.com/12242
Reviewed-by: Russ Cox <rsc@golang.org>
src/cmd/link/internal/ld/lib.go
test/fixedbugs/issue10607.go [new file with mode: 0644]
test/fixedbugs/issue10607a.go [new file with mode: 0644]

index 866eb67961eabd16143528cc967c4c66f6726ea4..1ce034860232e99cdc908cfd413c1cda5dcc0e07 100644 (file)
@@ -980,6 +980,10 @@ func hostlink() {
                argv = append(argv, "-Wl,-znow")
        }
 
+       if Iself && len(buildinfo) > 0 {
+               argv = append(argv, fmt.Sprintf("-Wl,--build-id=0x%x", buildinfo))
+       }
+
        argv = append(argv, "-o")
        argv = append(argv, outfile)
 
diff --git a/test/fixedbugs/issue10607.go b/test/fixedbugs/issue10607.go
new file mode 100644 (file)
index 0000000..4903a6a
--- /dev/null
@@ -0,0 +1,31 @@
+// +build linux
+// run
+
+// Copyright 2015 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.
+
+// Test that a -B option is passed through when using both internal
+// and external linking mode.
+
+package main
+
+import (
+       "fmt"
+       "os"
+       "os/exec"
+       "path/filepath"
+)
+
+func main() {
+       test("internal")
+       test("external")
+}
+
+func test(linkmode string) {
+       out, err := exec.Command("go", "run", "-ldflags", "-B=0x12345678 -linkmode="+linkmode, filepath.Join("fixedbugs", "issue10607a.go")).CombinedOutput()
+       if err != nil {
+               fmt.Printf("BUG: linkmode=%s %v\n%s\n", linkmode, err, out)
+               os.Exit(1)
+       }
+}
diff --git a/test/fixedbugs/issue10607a.go b/test/fixedbugs/issue10607a.go
new file mode 100644 (file)
index 0000000..18bf1a2
--- /dev/null
@@ -0,0 +1,81 @@
+// skip
+
+// Copyright 2015 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.
+
+// This is built by issue10607.go with a -B option.
+// Verify that we have one build-id note with the expected value.
+
+package main
+
+import (
+       "bytes"
+       "debug/elf"
+       "fmt"
+       "os"
+)
+
+func main() {
+       f, err := elf.Open("/proc/self/exe")
+       if err != nil {
+               if os.IsNotExist(err) {
+                       return
+               }
+               fmt.Fprintln(os.Stderr, "opening /proc/self/exe:", err)
+               os.Exit(1)
+       }
+
+       c := 0
+       fail := false
+       for i, s := range f.Sections {
+               if s.Type != elf.SHT_NOTE {
+                       continue
+               }
+
+               d, err := s.Data()
+               if err != nil {
+                       fmt.Fprintln(os.Stderr, "reading data of note section %d: %v", i, err)
+                       continue
+               }
+
+               for len(d) > 0 {
+                       namesz := f.ByteOrder.Uint32(d)
+                       descsz := f.ByteOrder.Uint32(d[4:])
+                       typ := f.ByteOrder.Uint32(d[8:])
+
+                       an := (namesz + 3) &^ 3
+                       ad := (descsz + 3) &^ 3
+
+                       if int(12+an+ad) > len(d) {
+                               fmt.Fprintf(os.Stderr, "note section %d too short for header (%d < 12 + align(%d,4) + align(%d,4))\n", i, len(d), namesz, descsz)
+                               break
+                       }
+
+                       // 3 == NT_GNU_BUILD_ID
+                       if typ == 3 && namesz == 4 && bytes.Equal(d[12:16], []byte("GNU\000")) {
+                               id := string(d[12+an:12+an+descsz])
+                               if id == "\x12\x34\x56\x78" {
+                                       c++
+                               } else {
+                                       fmt.Fprintf(os.Stderr, "wrong build ID data: %q\n", id)
+                                       fail = true
+                               }
+                       }
+
+                       d = d[12+an+ad:]
+               }
+       }
+
+       if c == 0 {
+               fmt.Fprintln(os.Stderr, "no build-id note")
+               fail = true
+       } else if c > 1 {
+               fmt.Fprintln(os.Stderr, c, "build-id notes")
+               fail = true
+       }
+
+       if fail {
+               os.Exit(1)
+       }
+}