Commit b397f633 authored by Heschi Kreinick's avatar Heschi Kreinick

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 <heschi@google.com>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
Reviewed-by: default avatarAustin Clements <austin@google.com>
parent 3b0b3a02
...@@ -1221,6 +1221,11 @@ func (ctxt *Link) hostlink() { ...@@ -1221,6 +1221,11 @@ func (ctxt *Link) hostlink() {
argv = append(argv, "-Qunused-arguments") argv = append(argv, "-Qunused-arguments")
} }
const compressDWARF = "-Wl,--compress-debug-sections=zlib-gnu"
if linkerFlagSupported(argv[0], compressDWARF) {
argv = append(argv, compressDWARF)
}
argv = append(argv, filepath.Join(*flagTmpdir, "go.o")) argv = append(argv, filepath.Join(*flagTmpdir, "go.o"))
argv = append(argv, hostobjCopy()...) argv = append(argv, hostobjCopy()...)
...@@ -1267,21 +1272,9 @@ func (ctxt *Link) hostlink() { ...@@ -1267,21 +1272,9 @@ func (ctxt *Link) hostlink() {
// issue #17847. To avoid this problem pass -no-pie to the // issue #17847. To avoid this problem pass -no-pie to the
// toolchain if it is supported. // toolchain if it is supported.
if ctxt.BuildMode == BuildModeExe && !ctxt.linkShared { if ctxt.BuildMode == BuildModeExe && !ctxt.linkShared {
src := filepath.Join(*flagTmpdir, "trivial.c")
if err := ioutil.WriteFile(src, []byte("int main() { return 0; }"), 0666); err != nil {
Errorf(nil, "WriteFile trivial.c failed: %v", err)
}
// GCC uses -no-pie, clang uses -nopie. // GCC uses -no-pie, clang uses -nopie.
for _, nopie := range []string{"-no-pie", "-nopie"} { for _, nopie := range []string{"-no-pie", "-nopie"} {
cmd := exec.Command(argv[0], nopie, "trivial.c") if linkerFlagSupported(argv[0], nopie) {
cmd.Dir = *flagTmpdir
cmd.Env = append([]string{"LC_ALL=C"}, os.Environ()...)
out, err := cmd.CombinedOutput()
// GCC says "unrecognized command line option ‘-no-pie’"
// clang says "unknown argument: '-no-pie'"
supported := err == nil && !bytes.Contains(out, []byte("unrecognized")) && !bytes.Contains(out, []byte("unknown"))
if supported {
argv = append(argv, nopie) argv = append(argv, nopie)
break break
} }
...@@ -1356,6 +1349,25 @@ func (ctxt *Link) hostlink() { ...@@ -1356,6 +1349,25 @@ func (ctxt *Link) hostlink() {
} }
} }
var createTrivialCOnce sync.Once
func linkerFlagSupported(linker, flag string) bool {
createTrivialCOnce.Do(func() {
src := filepath.Join(*flagTmpdir, "trivial.c")
if err := ioutil.WriteFile(src, []byte("int main() { return 0; }"), 0666); err != nil {
Errorf(nil, "WriteFile trivial.c failed: %v", err)
}
})
cmd := exec.Command(linker, flag, "trivial.c")
cmd.Dir = *flagTmpdir
cmd.Env = append([]string{"LC_ALL=C"}, os.Environ()...)
out, err := cmd.CombinedOutput()
// GCC says "unrecognized command line option ‘-no-pie’"
// clang says "unknown argument: '-no-pie'"
return err == nil && !bytes.Contains(out, []byte("unrecognized")) && !bytes.Contains(out, []byte("unknown"))
}
// hostlinkArchArgs returns arguments to pass to the external linker // hostlinkArchArgs returns arguments to pass to the external linker
// based on the architecture. // based on the architecture.
func hostlinkArchArgs(arch *sys.Arch) []string { func hostlinkArchArgs(arch *sys.Arch) []string {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment