2015-02-27 22:57:28 -05:00
|
|
|
// Inferno utils/6l/obj.c
|
|
|
|
|
// http://code.google.com/p/inferno-os/source/browse/utils/6l/obj.c
|
|
|
|
|
//
|
|
|
|
|
// Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved.
|
|
|
|
|
// Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
|
|
|
|
|
// Portions Copyright © 1997-1999 Vita Nuova Limited
|
|
|
|
|
// Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
|
|
|
|
|
// Portions Copyright © 2004,2006 Bruce Ellis
|
|
|
|
|
// Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
|
|
|
|
|
// Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
|
|
|
|
|
// Portions Copyright © 2009 The Go Authors. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
|
|
|
|
|
package ld
|
|
|
|
|
|
|
|
|
|
import (
|
2016-04-06 21:45:29 -07:00
|
|
|
"cmd/internal/bio"
|
2015-02-27 22:57:28 -05:00
|
|
|
"cmd/internal/obj"
|
2016-04-06 12:01:40 -07:00
|
|
|
"cmd/internal/sys"
|
2015-02-27 22:57:28 -05:00
|
|
|
"flag"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2015-06-04 14:31:05 -04:00
|
|
|
var (
|
|
|
|
|
pkglistfornote []byte
|
|
|
|
|
buildid string
|
|
|
|
|
)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
func Ldmain() {
|
2016-04-09 12:54:45 +10:00
|
|
|
Bso = bio.BufWriter(os.Stdout)
|
|
|
|
|
|
2016-04-06 12:01:40 -07:00
|
|
|
Ctxt = linknew(SysArch)
|
2015-02-27 22:57:28 -05:00
|
|
|
Ctxt.Diag = Diag
|
2016-04-09 12:54:45 +10:00
|
|
|
Ctxt.Bso = Bso
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
Debug = [128]int{}
|
|
|
|
|
nerrors = 0
|
|
|
|
|
outfile = ""
|
|
|
|
|
HEADTYPE = -1
|
|
|
|
|
INITTEXT = -1
|
|
|
|
|
INITDAT = -1
|
|
|
|
|
INITRND = -1
|
|
|
|
|
INITENTRY = ""
|
|
|
|
|
Linkmode = LinkAuto
|
|
|
|
|
|
2015-04-20 13:34:22 -04:00
|
|
|
// For testing behavior of go command when tools crash silently.
|
2015-02-27 22:57:28 -05:00
|
|
|
// Undocumented, not in standard flag parser to avoid
|
|
|
|
|
// exposing in usage message.
|
|
|
|
|
for _, arg := range os.Args {
|
|
|
|
|
if arg == "-crash_for_testing" {
|
2015-04-20 13:34:22 -04:00
|
|
|
os.Exit(2)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-06 12:01:40 -07:00
|
|
|
if SysArch.Family == sys.AMD64 && obj.Getgoos() == "plan9" {
|
2015-05-21 14:42:14 -04:00
|
|
|
obj.Flagcount("8", "use 64-bit addresses in symbol table", &Debug['8'])
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2015-05-01 16:14:50 -07:00
|
|
|
obj.Flagfn1("B", "add an ELF NT_GNU_BUILD_ID `note` when using ELF", addbuildinfo)
|
2015-02-27 22:57:28 -05:00
|
|
|
obj.Flagcount("C", "check Go calls to C code", &Debug['C'])
|
2015-05-01 16:14:50 -07:00
|
|
|
obj.Flagint64("D", "set data segment `address`", &INITDAT)
|
|
|
|
|
obj.Flagstr("E", "set `entry` symbol name", &INITENTRY)
|
|
|
|
|
obj.Flagfn1("I", "use `linker` as ELF dynamic linker", setinterp)
|
|
|
|
|
obj.Flagfn1("L", "add specified `directory` to library path", Lflag)
|
|
|
|
|
obj.Flagfn1("H", "set header `type`", setheadtype)
|
|
|
|
|
obj.Flagint32("R", "set address rounding `quantum`", &INITRND)
|
|
|
|
|
obj.Flagint64("T", "set text segment `address`", &INITTEXT)
|
2015-02-27 22:57:28 -05:00
|
|
|
obj.Flagfn0("V", "print version and exit", doversion)
|
2015-05-21 14:35:02 -04:00
|
|
|
obj.Flagfn1("X", "add string value `definition` of the form importpath.name=value", addstrdata1)
|
2015-02-27 22:57:28 -05:00
|
|
|
obj.Flagcount("a", "disassemble output", &Debug['a'])
|
2015-06-04 14:31:05 -04:00
|
|
|
obj.Flagstr("buildid", "record `id` as Go toolchain build id", &buildid)
|
2015-05-01 16:14:50 -07:00
|
|
|
flag.Var(&Buildmode, "buildmode", "set build `mode`")
|
2015-02-27 22:57:28 -05:00
|
|
|
obj.Flagcount("c", "dump call graph", &Debug['c'])
|
|
|
|
|
obj.Flagcount("d", "disable dynamic executable", &Debug['d'])
|
2016-01-25 11:44:02 -08:00
|
|
|
obj.Flagstr("extar", "archive program for buildmode=c-archive", &extar)
|
2015-05-01 16:14:50 -07:00
|
|
|
obj.Flagstr("extld", "use `linker` when linking in external mode", &extld)
|
|
|
|
|
obj.Flagstr("extldflags", "pass `flags` to external linker", &extldflags)
|
2015-02-27 22:57:28 -05:00
|
|
|
obj.Flagcount("f", "ignore version mismatch", &Debug['f'])
|
|
|
|
|
obj.Flagcount("g", "disable go package data checks", &Debug['g'])
|
2015-06-04 14:30:30 -04:00
|
|
|
obj.Flagcount("h", "halt on error", &Debug['h'])
|
2015-05-01 16:14:50 -07:00
|
|
|
obj.Flagstr("installsuffix", "set package directory `suffix`", &flag_installsuffix)
|
|
|
|
|
obj.Flagstr("k", "set field tracking `symbol`", &tracksym)
|
2015-11-16 18:11:35 -08:00
|
|
|
obj.Flagstr("libgcc", "compiler support lib for internal linking; use \"none\" to disable", &libgccfile)
|
2015-05-01 16:14:50 -07:00
|
|
|
obj.Flagfn1("linkmode", "set link `mode` (internal, external, auto)", setlinkmode)
|
2015-04-01 14:57:34 +13:00
|
|
|
flag.BoolVar(&Linkshared, "linkshared", false, "link against installed Go shared libraries")
|
2015-10-21 07:11:01 -07:00
|
|
|
obj.Flagcount("msan", "enable MSan interface", &flag_msan)
|
2015-02-27 22:57:28 -05:00
|
|
|
obj.Flagcount("n", "dump symbol table", &Debug['n'])
|
2015-05-01 16:14:50 -07:00
|
|
|
obj.Flagstr("o", "write output to `file`", &outfile)
|
|
|
|
|
flag.Var(&rpath, "r", "set the ELF dynamic linker search `path` to dir1:dir2:...")
|
2015-02-27 22:57:28 -05:00
|
|
|
obj.Flagcount("race", "enable race detector", &flag_race)
|
|
|
|
|
obj.Flagcount("s", "disable symbol table", &Debug['s'])
|
2015-03-27 02:48:27 +00:00
|
|
|
var flagShared int
|
2016-04-06 12:01:40 -07:00
|
|
|
if SysArch.InFamily(sys.ARM, sys.AMD64) {
|
2015-03-27 02:48:27 +00:00
|
|
|
obj.Flagcount("shared", "generate shared object (implies -linkmode external)", &flagShared)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2015-05-01 16:14:50 -07:00
|
|
|
obj.Flagstr("tmpdir", "use `directory` for temporary files", &tmpdir)
|
2015-02-27 22:57:28 -05:00
|
|
|
obj.Flagcount("u", "reject unsafe packages", &Debug['u'])
|
|
|
|
|
obj.Flagcount("v", "print link trace", &Debug['v'])
|
|
|
|
|
obj.Flagcount("w", "disable DWARF generation", &Debug['w'])
|
|
|
|
|
|
2015-05-21 14:35:02 -04:00
|
|
|
obj.Flagstr("cpuprofile", "write cpu profile to `file`", &cpuprofile)
|
|
|
|
|
obj.Flagstr("memprofile", "write memory profile to `file`", &memprofile)
|
|
|
|
|
obj.Flagint64("memprofilerate", "set runtime.MemProfileRate to `rate`", &memprofilerate)
|
|
|
|
|
|
2015-02-27 22:57:28 -05:00
|
|
|
obj.Flagparse(usage)
|
2015-05-21 14:35:02 -04:00
|
|
|
|
2015-02-27 22:57:28 -05:00
|
|
|
startProfile()
|
2016-04-09 12:54:45 +10:00
|
|
|
Ctxt.Bso = Bso
|
2015-02-27 22:57:28 -05:00
|
|
|
Ctxt.Debugvlog = int32(Debug['v'])
|
2015-03-27 02:48:27 +00:00
|
|
|
if flagShared != 0 {
|
2015-05-21 14:42:14 -04:00
|
|
|
if Buildmode == BuildmodeUnset {
|
2015-03-27 02:48:27 +00:00
|
|
|
Buildmode = BuildmodeCShared
|
|
|
|
|
} else if Buildmode != BuildmodeCShared {
|
2015-04-09 07:37:17 -04:00
|
|
|
Exitf("-shared and -buildmode=%s are incompatible", Buildmode.String())
|
2015-03-27 02:48:27 +00:00
|
|
|
}
|
|
|
|
|
}
|
2015-05-21 14:42:14 -04:00
|
|
|
if Buildmode == BuildmodeUnset {
|
|
|
|
|
Buildmode = BuildmodeExe
|
|
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-03-30 02:59:10 +00:00
|
|
|
if Buildmode != BuildmodeShared && flag.NArg() != 1 {
|
2015-02-27 22:57:28 -05:00
|
|
|
usage()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if outfile == "" {
|
2015-05-21 13:28:17 -04:00
|
|
|
outfile = "a.out"
|
2015-04-19 19:33:58 -07:00
|
|
|
if HEADTYPE == obj.Hwindows {
|
2015-05-21 13:28:17 -04:00
|
|
|
outfile += ".exe"
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
libinit() // creates outfile
|
|
|
|
|
|
|
|
|
|
if HEADTYPE == -1 {
|
|
|
|
|
HEADTYPE = int32(headtype(goos))
|
|
|
|
|
}
|
|
|
|
|
Ctxt.Headtype = int(HEADTYPE)
|
|
|
|
|
if headstring == "" {
|
|
|
|
|
headstring = Headstr(int(HEADTYPE))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Thearch.Archinit()
|
|
|
|
|
|
2015-04-01 14:57:34 +13:00
|
|
|
if Linkshared && !Iself {
|
2015-04-09 07:37:17 -04:00
|
|
|
Exitf("-linkshared can only be used on elf systems")
|
2015-04-01 14:57:34 +13:00
|
|
|
}
|
|
|
|
|
|
2015-02-27 22:57:28 -05:00
|
|
|
if Debug['v'] != 0 {
|
2016-04-09 12:54:45 +10:00
|
|
|
fmt.Fprintf(Bso, "HEADER = -H%d -T0x%x -D0x%x -R0x%x\n", HEADTYPE, uint64(INITTEXT), uint64(INITDAT), uint32(INITRND))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2015-05-02 12:44:49 +10:00
|
|
|
Bso.Flush()
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-03-30 02:59:10 +00:00
|
|
|
if Buildmode == BuildmodeShared {
|
|
|
|
|
for i := 0; i < flag.NArg(); i++ {
|
|
|
|
|
arg := flag.Arg(i)
|
|
|
|
|
parts := strings.SplitN(arg, "=", 2)
|
|
|
|
|
var pkgpath, file string
|
|
|
|
|
if len(parts) == 1 {
|
|
|
|
|
pkgpath, file = "main", arg
|
|
|
|
|
} else {
|
|
|
|
|
pkgpath, file = parts[0], parts[1]
|
|
|
|
|
}
|
2015-04-29 22:58:52 +12:00
|
|
|
pkglistfornote = append(pkglistfornote, pkgpath...)
|
|
|
|
|
pkglistfornote = append(pkglistfornote, '\n')
|
2015-04-01 14:57:34 +13:00
|
|
|
addlibpath(Ctxt, "command line", "command line", file, pkgpath, "")
|
2015-03-30 02:59:10 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2015-04-01 14:57:34 +13:00
|
|
|
addlibpath(Ctxt, "command line", "command line", flag.Arg(0), "main", "")
|
2015-03-30 02:59:10 +00:00
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
loadlib()
|
|
|
|
|
|
2015-06-29 13:03:11 -04:00
|
|
|
checkstrdata()
|
cmd/link: prune unused methods
Today the linker keeps all methods of reachable types. This is
necessary if a program uses reflect.Value.Call. But while use of
reflection is widespread in Go for encoders and decoders, using
it to call a method is rare.
This CL looks for the use of reflect.Value.Call in a program, and
if it is absent, adopts a (reasonably conservative) method pruning
strategy as part of dead code elimination. Any method that is
directly called is kept, and any method that matches a used
interface's method signature is kept.
Whether or not a method body is kept is determined by the relocation
from its receiver's *rtype to its *rtype. A small change in the
compiler marks these relocations as R_METHOD so they can be easily
collected and manipulated by the linker.
As a bonus, this technique removes the text segment of methods that
have been inlined. Looking at the output of building cmd/objdump with
-ldflags=-v=2 shows that inlined methods like
runtime.(*traceAllocBlockPtr).ptr are removed from the program.
Relatively little work is necessary to do this. Linking two
examples, jujud and cmd/objdump show no more than +2% link time.
Binaries that do not use reflect.Call.Value drop 4 - 20% in size:
addr2line: -793KB (18%)
asm: -346KB (8%)
cgo: -490KB (10%)
compile: -564KB (4%)
dist: -736KB (17%)
fix: -404KB (12%)
link: -328KB (7%)
nm: -827KB (19%)
objdump: -712KB (16%)
pack: -327KB (14%)
yacc: -350KB (10%)
Binaries that do use reflect.Call.Value see a modest size decrease
of 2 - 6% thanks to pruning of unexported methods:
api: -151KB (3%)
cover: -222KB (4%)
doc: -106KB (2.5%)
pprof: -314KB (3%)
trace: -357KB (4%)
vet: -187KB (2.7%)
jujud: -4.4MB (5.8%)
cmd/go: -384KB (3.4%)
The trivial Hello example program goes from 2MB to 1.68MB:
package main
import "fmt"
func main() {
fmt.Println("Hello, 世界")
}
Method pruning also helps when building small binaries with
"-ldflags=-s -w". The above program goes from 1.43MB to 1.2MB.
Unfortunately the linker can only tell if reflect.Value.Call has been
statically linked, not if it is dynamically used. And while use is
rare, it is linked into a very common standard library package,
text/template. The result is programs like cmd/go, which don't use
reflect.Value.Call, see limited benefit from this CL. If binary size
is important enough it may be possible to address this in future work.
For #6853.
Change-Id: Iabe90e210e813b08c3f8fd605f841f0458973396
Reviewed-on: https://go-review.googlesource.com/20483
Reviewed-by: Russ Cox <rsc@golang.org>
2016-03-07 23:45:04 -05:00
|
|
|
deadcode(Ctxt)
|
|
|
|
|
fieldtrack(Ctxt)
|
2015-02-27 22:57:28 -05:00
|
|
|
callgraph()
|
|
|
|
|
|
|
|
|
|
doelf()
|
2015-04-19 19:33:58 -07:00
|
|
|
if HEADTYPE == obj.Hdarwin {
|
2015-02-27 22:57:28 -05:00
|
|
|
domacho()
|
|
|
|
|
}
|
|
|
|
|
dostkcheck()
|
2015-04-19 19:33:58 -07:00
|
|
|
if HEADTYPE == obj.Hwindows {
|
2015-02-27 22:57:28 -05:00
|
|
|
dope()
|
|
|
|
|
}
|
|
|
|
|
addexport()
|
|
|
|
|
Thearch.Gentext() // trampolines, call stubs, etc.
|
2015-06-04 15:15:48 -04:00
|
|
|
textbuildid()
|
2015-02-27 22:57:28 -05:00
|
|
|
textaddress()
|
|
|
|
|
pclntab()
|
|
|
|
|
findfunctab()
|
|
|
|
|
symtab()
|
|
|
|
|
dodata()
|
|
|
|
|
address()
|
|
|
|
|
reloc()
|
|
|
|
|
Thearch.Asmb()
|
|
|
|
|
undef()
|
|
|
|
|
hostlink()
|
2015-04-09 10:44:05 -04:00
|
|
|
archive()
|
2015-02-27 22:57:28 -05:00
|
|
|
if Debug['v'] != 0 {
|
2016-04-09 12:54:45 +10:00
|
|
|
fmt.Fprintf(Bso, "%5.2f cpu time\n", obj.Cputime())
|
|
|
|
|
fmt.Fprintf(Bso, "%d symbols\n", len(Ctxt.Allsym))
|
|
|
|
|
fmt.Fprintf(Bso, "%d liveness data\n", liveness)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2015-05-02 12:44:49 +10:00
|
|
|
Bso.Flush()
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-04-09 07:37:17 -04:00
|
|
|
errorexit()
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|