Skip to content

Commit b397f63

Browse files
committed
cmd/link: compress debug sections in external linking mode
Forked from CL 111895. For #11799. Change-Id: Ie1346ac2c9122de494823b9058df3a0971e9dfe1 Reviewed-on: https://go-review.googlesource.com/118277 Run-TryBot: Heschi Kreinick <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Austin Clements <[email protected]>
1 parent 3b0b3a0 commit b397f63

File tree

1 file changed

+25
-13
lines changed
  • src/cmd/link/internal/ld

1 file changed

+25
-13
lines changed

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

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,6 +1221,11 @@ func (ctxt *Link) hostlink() {
12211221
argv = append(argv, "-Qunused-arguments")
12221222
}
12231223

1224+
const compressDWARF = "-Wl,--compress-debug-sections=zlib-gnu"
1225+
if linkerFlagSupported(argv[0], compressDWARF) {
1226+
argv = append(argv, compressDWARF)
1227+
}
1228+
12241229
argv = append(argv, filepath.Join(*flagTmpdir, "go.o"))
12251230
argv = append(argv, hostobjCopy()...)
12261231

@@ -1267,21 +1272,9 @@ func (ctxt *Link) hostlink() {
12671272
// issue #17847. To avoid this problem pass -no-pie to the
12681273
// toolchain if it is supported.
12691274
if ctxt.BuildMode == BuildModeExe && !ctxt.linkShared {
1270-
src := filepath.Join(*flagTmpdir, "trivial.c")
1271-
if err := ioutil.WriteFile(src, []byte("int main() { return 0; }"), 0666); err != nil {
1272-
Errorf(nil, "WriteFile trivial.c failed: %v", err)
1273-
}
1274-
12751275
// GCC uses -no-pie, clang uses -nopie.
12761276
for _, nopie := range []string{"-no-pie", "-nopie"} {
1277-
cmd := exec.Command(argv[0], nopie, "trivial.c")
1278-
cmd.Dir = *flagTmpdir
1279-
cmd.Env = append([]string{"LC_ALL=C"}, os.Environ()...)
1280-
out, err := cmd.CombinedOutput()
1281-
// GCC says "unrecognized command line option ‘-no-pie’"
1282-
// clang says "unknown argument: '-no-pie'"
1283-
supported := err == nil && !bytes.Contains(out, []byte("unrecognized")) && !bytes.Contains(out, []byte("unknown"))
1284-
if supported {
1277+
if linkerFlagSupported(argv[0], nopie) {
12851278
argv = append(argv, nopie)
12861279
break
12871280
}
@@ -1356,6 +1349,25 @@ func (ctxt *Link) hostlink() {
13561349
}
13571350
}
13581351

1352+
var createTrivialCOnce sync.Once
1353+
1354+
func linkerFlagSupported(linker, flag string) bool {
1355+
createTrivialCOnce.Do(func() {
1356+
src := filepath.Join(*flagTmpdir, "trivial.c")
1357+
if err := ioutil.WriteFile(src, []byte("int main() { return 0; }"), 0666); err != nil {
1358+
Errorf(nil, "WriteFile trivial.c failed: %v", err)
1359+
}
1360+
})
1361+
1362+
cmd := exec.Command(linker, flag, "trivial.c")
1363+
cmd.Dir = *flagTmpdir
1364+
cmd.Env = append([]string{"LC_ALL=C"}, os.Environ()...)
1365+
out, err := cmd.CombinedOutput()
1366+
// GCC says "unrecognized command line option ‘-no-pie’"
1367+
// clang says "unknown argument: '-no-pie'"
1368+
return err == nil && !bytes.Contains(out, []byte("unrecognized")) && !bytes.Contains(out, []byte("unknown"))
1369+
}
1370+
13591371
// hostlinkArchArgs returns arguments to pass to the external linker
13601372
// based on the architecture.
13611373
func hostlinkArchArgs(arch *sys.Arch) []string {

0 commit comments

Comments
 (0)