|
1 commit dc6df1b07093ffa0568a581251e8ddd38f707ed6 |
|
2 Author: Shawn Walker-Salas <[email protected]> |
|
3 Date: Thu Sep 17 15:47:20 2015 -0700 |
|
4 |
|
5 cmd/go: elide -rpath when not applicable and used via LDFLAGS |
|
6 |
|
7 Some linker flags should only be applied when performing the final |
|
8 linking step for a shared library or executable, etc. In other |
|
9 contexts, they're either invalid, or meaningless to apply (so should |
|
10 not be specified). |
|
11 |
|
12 When an external linker is used (either directly by Go or by the |
|
13 compiler driver used by cgo), -rpath and -rpath-link should only be |
|
14 specified in the final linking step. On platforms such as Solaris, |
|
15 ld(1) will reject its use in any other scenario (such as when linking |
|
16 relocatable objects). |
|
17 |
|
18 This change is necessary because Go does not currently offer a way to |
|
19 specify LDFLAGS based on when they should be applied. |
|
20 |
|
21 Fixes #12115 |
|
22 |
|
23 Change-Id: If35a18d8eee8ec7ddcca2d4ccd41ab6ffcf93b41 |
|
24 Reviewed-on: https://go-review.googlesource.com/14674 |
|
25 Reviewed-by: Minux Ma <[email protected]> |
|
26 Run-TryBot: Minux Ma <[email protected]> |
|
27 TryBot-Result: Gobot Gobot <[email protected]> |
|
28 Reviewed-by: Ian Lance Taylor <[email protected]> |
|
29 |
|
30 diff --git a/src/cmd/go/build.go b/src/cmd/go/build.go |
|
31 index df74338..0c2bc5f 100644 |
|
32 --- a/src/cmd/go/build.go |
|
33 +++ b/src/cmd/go/build.go |
|
34 @@ -2943,7 +2943,9 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi |
|
35 var linkobj []string |
|
36 |
|
37 var bareLDFLAGS []string |
|
38 - // filter out -lsomelib, -l somelib, *.{so,dll,dylib}, and (on Darwin) -framework X |
|
39 + // When linking relocatable objects, various flags need to be |
|
40 + // filtered out as they are inapplicable and can cause some linkers |
|
41 + // to fail. |
|
42 for i := 0; i < len(cgoLDFLAGS); i++ { |
|
43 f := cgoLDFLAGS[i] |
|
44 switch { |
|
45 @@ -2959,7 +2961,6 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi |
|
46 case strings.HasSuffix(f, ".dylib"), |
|
47 strings.HasSuffix(f, ".so"), |
|
48 strings.HasSuffix(f, ".dll"): |
|
49 - continue |
|
50 // Remove any -fsanitize=foo flags. |
|
51 // Otherwise the compiler driver thinks that we are doing final link |
|
52 // and links sanitizer runtime into the object file. But we are not doing |
|
53 @@ -2968,6 +2969,16 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi |
|
54 // See issue 8788 for details. |
|
55 case strings.HasPrefix(f, "-fsanitize="): |
|
56 continue |
|
57 + // runpath flags not applicable unless building a shared |
|
58 + // object or executable; see issue 12115 for details. This |
|
59 + // is necessary as Go currently does not offer a way to |
|
60 + // specify the set of LDFLAGS that only apply to shared |
|
61 + // objects. |
|
62 + case strings.HasPrefix(f, "-Wl,-rpath"): |
|
63 + if f == "-Wl,-rpath" || f == "-Wl,-rpath-link" { |
|
64 + // Skip following argument to -rpath* too. |
|
65 + i++ |
|
66 + } |
|
67 default: |
|
68 bareLDFLAGS = append(bareLDFLAGS, f) |
|
69 } |