]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: actually generate .debug_gdb_scripts section on windows
authorAlex Brainman <alex.brainman@gmail.com>
Thu, 11 May 2017 01:55:59 +0000 (11:55 +1000)
committerAlex Brainman <alex.brainman@gmail.com>
Mon, 15 May 2017 06:16:19 +0000 (06:16 +0000)
Adjust finddebugruntimepath to look for runtime/debug.go file
instead of runtime/runtime.go. This actually finds runtime.GOMAXPROCS
in every Go executable (including windows).

I also included "-Wl,-T,fix_debug_gdb_scripts.ld" parameter to gcc
invocation on windows to work around gcc bug (see #20183 for details).

This CL only fixes windows -buildmode=exe, buildmode=c-archive
is still broken.

Thanks to Egon Elbre and Nick Clifton for investigation.

Fixes #20183
Fixes #20218

Change-Id: I5369a4db3913226aef3d9bd6317446856b0a1c34
Reviewed-on: https://go-review.googlesource.com/43331
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/cmd/link/internal/ld/dwarf.go
src/cmd/link/internal/ld/lib.go
src/debug/pe/file_test.go

index 35c450904ad8ec12a915737f930344a78d4a650c..785fe374d210c6e9acff91b98e69a9c28127a508 100644 (file)
@@ -872,7 +872,7 @@ func finddebugruntimepath(s *Symbol) {
 
        for i := range s.FuncInfo.File {
                f := s.FuncInfo.File[i]
-               if i := strings.Index(f.Name, "runtime/runtime.go"); i >= 0 {
+               if i := strings.Index(f.Name, "runtime/debug.go"); i >= 0 {
                        gdbscript = f.Name[:i] + "runtime/runtime-gdb.py"
                        break
                }
@@ -1450,9 +1450,13 @@ func writearanges(ctxt *Link, syms []*Symbol) []*Symbol {
 }
 
 func writegdbscript(ctxt *Link, syms []*Symbol) []*Symbol {
-       if Linkmode == LinkExternal && Headtype == objabi.Hwindows {
+       if Linkmode == LinkExternal && Headtype == objabi.Hwindows && Buildmode == BuildmodeCArchive {
                // gcc on Windows places .debug_gdb_scripts in the wrong location, which
                // causes the program not to run. See https://golang.org/issue/20183
+               // Non c-archives can avoid this issue via a linker script
+               // (see fix near writeGDBLinkerScript).
+               // c-archive users would need to specify the linker script manually.
+               // For UX it's better not to deal with this.
                return syms
        }
 
index a7821ba32ffcc80b704441fad86a8bb8fb99f4c9..8906d2f91a7fcb4b0d7e7e46baf8eff7aae8020b 100644 (file)
@@ -986,6 +986,29 @@ func hostobjCopy() (paths []string) {
        return paths
 }
 
+// writeGDBLinkerScript creates gcc linker script file in temp
+// directory. writeGDBLinkerScript returns created file path.
+// The script is used to work around gcc bug
+// (see https://golang.org/issue/20183 for details).
+func writeGDBLinkerScript() string {
+       name := "fix_debug_gdb_scripts.ld"
+       path := filepath.Join(*flagTmpdir, name)
+       src := `SECTIONS
+{
+  .debug_gdb_scripts BLOCK(__section_alignment__) (NOLOAD) :
+  {
+    *(.debug_gdb_scripts)
+  }
+}
+INSERT AFTER .debug_types;
+`
+       err := ioutil.WriteFile(path, []byte(src), 0666)
+       if err != nil {
+               Errorf(nil, "WriteFile %s failed: %v", name, err)
+       }
+       return path
+}
+
 // archive builds a .a archive from the hostobj object files.
 func (ctxt *Link) archive() {
        if Buildmode != BuildmodeCArchive {
@@ -1247,6 +1270,10 @@ func (l *Link) hostlink() {
                }
        }
        if Headtype == objabi.Hwindows {
+               // use gcc linker script to work around gcc bug
+               // (see https://golang.org/issue/20183 for details).
+               p := writeGDBLinkerScript()
+               argv = append(argv, "-Wl,-T,"+p)
                // libmingw32 and libmingwex have some inter-dependencies,
                // so must use linker groups.
                argv = append(argv, "-Wl,--start-group", "-lmingwex", "-lmingw32", "-Wl,--end-group")
index 100516f0ba7be55abb41df8876a4e0bbd639c2b2..8645d676b752b2cf9624ff6ab040a7a5a8cea4c9 100644 (file)
@@ -363,6 +363,16 @@ func testDWARF(t *testing.T, linktype int) {
        }
        defer f.Close()
 
+       var foundDebugGDBScriptsSection bool
+       for _, sect := range f.Sections {
+               if sect.Name == ".debug_gdb_scripts" {
+                       foundDebugGDBScriptsSection = true
+               }
+       }
+       if !foundDebugGDBScriptsSection {
+               t.Error(".debug_gdb_scripts section is not found")
+       }
+
        d, err := f.DWARF()
        if err != nil {
                t.Fatal(err)