Skip to content

Commit ae38b03

Browse files
committed
cmd/go: skip writing dwarf debug info for ephemeral binaries
Update #6853 For an ephemeral binary - one created, run, and then deleted - there is no need to write dwarf debug information, since the binary will not be used with gdb. In this case, instruct the linker not to spend time and disk space generating the debug information by passing the -w flag to the linker. Omitting dwarf information reduces the size of most binaries by 25%. We may be more aggressive about this in the future. LGTM=bradfitz, r R=r, bradfitz CC=golang-codereviews https://golang.org/cl/65890043
1 parent 2541cc8 commit ae38b03

File tree

5 files changed

+8
-1
lines changed

5 files changed

+8
-1
lines changed

src/cmd/go/build.go

+4
Original file line numberDiff line numberDiff line change
@@ -1714,6 +1714,10 @@ func (gcToolchain) ld(b *builder, p *Package, out string, allactions []*action,
17141714
if buildContext.InstallSuffix != "" {
17151715
ldflags = append(ldflags, "-installsuffix", buildContext.InstallSuffix)
17161716
}
1717+
if p.omitDWARF {
1718+
ldflags = append(ldflags, "-w")
1719+
}
1720+
17171721
// If the user has not specified the -extld option, then specify the
17181722
// appropriate linker. In case of C++ code, use the compiler named
17191723
// by the CXX environment variable or defaultCXX if CXX is not set.

src/cmd/go/pkg.go

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ type Package struct {
8989
exeName string // desired name for temporary executable
9090
coverMode string // preprocess Go source files with the coverage tool in this mode
9191
coverVars map[string]*CoverVar // variables created by coverage analysis
92+
omitDWARF bool // tell linker not to write DWARF information
9293
}
9394

9495
// CoverVar holds the name of the generated coverage variables targeting the named file.

src/cmd/go/run.go

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func runRun(cmd *Command, args []string) {
5858
if p.Error != nil {
5959
fatalf("%s", p.Error)
6060
}
61+
p.omitDWARF = true
6162
for _, err := range p.DepsErrors {
6263
errorf("%s", err)
6364
}

src/cmd/go/test.go

+1
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,7 @@ func (b *builder) test(p *Package) (buildAction, runAction, printAction *action,
654654
pkgdir: testDir,
655655
fake: true,
656656
Stale: true,
657+
omitDWARF: !testC && !testNeedBinary,
657658
}
658659
if pxtest != nil {
659660
pmain.imports = append(pmain.imports, pxtest)

test/run.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func compileInDir(runcmd runCmd, dir string, names ...string) (out []byte, err e
200200

201201
func linkFile(runcmd runCmd, goname string) (err error) {
202202
pfile := strings.Replace(goname, ".go", "."+letter, -1)
203-
_, err = runcmd("go", "tool", ld, "-o", "a.exe", "-L", ".", pfile)
203+
_, err = runcmd("go", "tool", ld, "-w", "-o", "a.exe", "-L", ".", pfile)
204204
return
205205
}
206206

0 commit comments

Comments
 (0)