Skip to content

Commit

Permalink
cmd/go: skip writing dwarf debug info for ephemeral binaries
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rsc committed Feb 19, 2014
1 parent 2541cc8 commit ae38b03
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/cmd/go/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -1714,6 +1714,10 @@ func (gcToolchain) ld(b *builder, p *Package, out string, allactions []*action,
if buildContext.InstallSuffix != "" {
ldflags = append(ldflags, "-installsuffix", buildContext.InstallSuffix)
}
if p.omitDWARF {
ldflags = append(ldflags, "-w")
}

// If the user has not specified the -extld option, then specify the
// appropriate linker. In case of C++ code, use the compiler named
// by the CXX environment variable or defaultCXX if CXX is not set.
Expand Down
1 change: 1 addition & 0 deletions src/cmd/go/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ type Package struct {
exeName string // desired name for temporary executable
coverMode string // preprocess Go source files with the coverage tool in this mode
coverVars map[string]*CoverVar // variables created by coverage analysis
omitDWARF bool // tell linker not to write DWARF information
}

// CoverVar holds the name of the generated coverage variables targeting the named file.
Expand Down
1 change: 1 addition & 0 deletions src/cmd/go/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func runRun(cmd *Command, args []string) {
if p.Error != nil {
fatalf("%s", p.Error)
}
p.omitDWARF = true
for _, err := range p.DepsErrors {
errorf("%s", err)
}
Expand Down
1 change: 1 addition & 0 deletions src/cmd/go/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ func (b *builder) test(p *Package) (buildAction, runAction, printAction *action,
pkgdir: testDir,
fake: true,
Stale: true,
omitDWARF: !testC && !testNeedBinary,
}
if pxtest != nil {
pmain.imports = append(pmain.imports, pxtest)
Expand Down
2 changes: 1 addition & 1 deletion test/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func compileInDir(runcmd runCmd, dir string, names ...string) (out []byte, err e

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

Expand Down

0 comments on commit ae38b03

Please sign in to comment.