]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/go: put user ldflags at the end of the linker invocation
authorJosh Bleecher Snyder <josharian@gmail.com>
Wed, 31 Dec 2014 21:25:52 +0000 (13:25 -0800)
committerJosh Bleecher Snyder <josharian@gmail.com>
Fri, 2 Jan 2015 18:36:44 +0000 (18:36 +0000)
If the user provided a key but no value via -ldflag -X,
another linker flag was used as the value.

Placing the user's flags at the end avoids this problem.
It also provides the user the opportunity to
override existing linker flags.

Fixes #8810.

Change-Id: I96f4190713dc9a9c29142e56658446fba7fb6bc8
Reviewed-on: https://go-review.googlesource.com/2242
Reviewed-by: Minux Ma <minux@golang.org>
src/cmd/go/build.go
test/linkx_run.go

index 58fc98d84b3fd09154244936ded9babd1086795c..cd17aba7370c5d0c63286d795c534cbd046b8df9 100644 (file)
@@ -1776,9 +1776,7 @@ func (gcToolchain) ld(b *builder, p *Package, out string, allactions []*action,
                        cxx = true
                }
        }
-       ldflags := buildLdflags
-       // Limit slice capacity so that concurrent appends do not race on the shared array.
-       ldflags = ldflags[:len(ldflags):len(ldflags)]
+       var ldflags []string
        if buildContext.InstallSuffix != "" {
                ldflags = append(ldflags, "-installsuffix", buildContext.InstallSuffix)
        }
@@ -1824,6 +1822,7 @@ func (gcToolchain) ld(b *builder, p *Package, out string, allactions []*action,
                        }
                }
        }
+       ldflags = append(ldflags, buildLdflags...)
        return b.run(".", p.ImportPath, nil, tool(archChar+"l"), "-o", out, importArgs, ldflags, mainpkg)
 }
 
index 11b66ed5a91fa78149fbbb4e2ca6daf4e3d06113..f3029f50a97a8269ff3a1878b6b7d9edef9ebd7d 100644 (file)
@@ -16,6 +16,7 @@ import (
 )
 
 func main() {
+       // Successful run
        cmd := exec.Command("go", "run", "-ldflags=-X main.tbd hello -X main.overwrite trumped -X main.nosuchsymbol neverseen", "linkx.go")
        out, err := cmd.CombinedOutput()
        if err != nil {
@@ -30,4 +31,12 @@ func main() {
                fmt.Printf("got %q want %q\n", got, want)
                os.Exit(1)
        }
+
+       // Issue 8810
+       cmd = exec.Command("go", "run", "-ldflags=-X main.tbd", "linkx.go")
+       _, err = cmd.CombinedOutput()
+       if err == nil {
+               fmt.Println("-X linker flag should not accept keys without values")
+               os.Exit(1)
+       }
 }