Skip to content

Commit ad4e637

Browse files
cmd/link: remove -rdynamic if -static appears in cgo LDFLAGS
We already remove -rdynamic if -static appears in -extldflags. Extend that to apply to CGO_LDFLAGS and #cgo LDFLAGS as well. Updates #26197 Change-Id: Ibb62d1b20726916a12fd889acb05c1c559a5ace2 Reviewed-on: https://go-review.googlesource.com/122135 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent abaf53f commit ad4e637

File tree

1 file changed

+21
-15
lines changed
  • src/cmd/link/internal/ld

1 file changed

+21
-15
lines changed

src/cmd/link/internal/ld/lib.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,7 +1262,26 @@ func (ctxt *Link) hostlink() {
12621262
}
12631263
}
12641264

1265-
argv = append(argv, ldflag...)
1265+
// clang, unlike GCC, passes -rdynamic to the linker
1266+
// even when linking with -static, causing a linker
1267+
// error when using GNU ld. So take out -rdynamic if
1268+
// we added it. We do it in this order, rather than
1269+
// only adding -rdynamic later, so that -*extldflags
1270+
// can override -rdynamic without using -static.
1271+
checkStatic := func(arg string) {
1272+
if ctxt.IsELF && arg == "-static" {
1273+
for i := range argv {
1274+
if argv[i] == "-rdynamic" {
1275+
argv[i] = "-static"
1276+
}
1277+
}
1278+
}
1279+
}
1280+
1281+
for _, p := range ldflag {
1282+
argv = append(argv, p)
1283+
checkStatic(p)
1284+
}
12661285

12671286
// When building a program with the default -buildmode=exe the
12681287
// gc compiler generates code requires DT_TEXTREL in a
@@ -1283,20 +1302,7 @@ func (ctxt *Link) hostlink() {
12831302

12841303
for _, p := range strings.Fields(*flagExtldflags) {
12851304
argv = append(argv, p)
1286-
1287-
// clang, unlike GCC, passes -rdynamic to the linker
1288-
// even when linking with -static, causing a linker
1289-
// error when using GNU ld. So take out -rdynamic if
1290-
// we added it. We do it in this order, rather than
1291-
// only adding -rdynamic later, so that -*extldflags
1292-
// can override -rdynamic without using -static.
1293-
if ctxt.IsELF && p == "-static" {
1294-
for i := range argv {
1295-
if argv[i] == "-rdynamic" {
1296-
argv[i] = "-static"
1297-
}
1298-
}
1299-
}
1305+
checkStatic(p)
13001306
}
13011307
if ctxt.HeadType == objabi.Hwindows {
13021308
// use gcc linker script to work around gcc bug

0 commit comments

Comments
 (0)