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

419 lines
13 KiB
Go
Raw Normal View History

// Inferno utils/6l/obj.c
// https://bitbucket.org/inferno-os/inferno-os/src/default/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 (
"bufio"
"cmd/internal/objabi"
"cmd/internal/sys"
"cmd/link/internal/benchmark"
"cmd/link/internal/sym"
"flag"
"log"
"os"
"runtime"
"runtime/pprof"
"strings"
)
var (
pkglistfornote []byte
windowsgui bool // writes a "GUI binary" instead of a "console binary"
ownTmpDir bool // set to true if tmp dir created by linker (e.g. no -tmpdir)
)
func init() {
flag.Var(&rpath, "r", "set the ELF dynamic linker search `path` to dir1:dir2:...")
}
// Flags used by the linker. The exported flags are used by the architecture-specific packages.
var (
flagBuildid = flag.String("buildid", "", "record `id` as Go toolchain build id")
flagOutfile = flag.String("o", "", "write output to `file`")
flagPluginPath = flag.String("pluginpath", "", "full path name for plugin")
flagInstallSuffix = flag.String("installsuffix", "", "set package directory `suffix`")
flagDumpDep = flag.Bool("dumpdep", false, "dump symbol dependency graph")
flagRace = flag.Bool("race", false, "enable race detector")
flagMsan = flag.Bool("msan", false, "enable MSan interface")
flagFieldTrack = flag.String("k", "", "set field tracking `symbol`")
flagLibGCC = flag.String("libgcc", "", "compiler support lib for internal linking; use \"none\" to disable")
flagTmpdir = flag.String("tmpdir", "", "use `directory` for temporary files")
flagExtld = flag.String("extld", "", "use `linker` when linking in external mode")
flagExtldflags = flag.String("extldflags", "", "pass `flags` to external linker")
flagExtar = flag.String("extar", "", "archive program for buildmode=c-archive")
flagA = flag.Bool("a", false, "disassemble output")
FlagC = flag.Bool("c", false, "dump call graph")
FlagD = flag.Bool("d", false, "disable dynamic executable")
flagF = flag.Bool("f", false, "ignore version mismatch")
flagG = flag.Bool("g", false, "disable go package data checks")
flagH = flag.Bool("h", false, "halt on error")
flagN = flag.Bool("n", false, "dump symbol table")
FlagS = flag.Bool("s", false, "disable symbol table")
flagU = flag.Bool("u", false, "reject unsafe packages")
FlagW = flag.Bool("w", false, "disable DWARF generation")
Flag8 bool // use 64-bit addresses in symbol table
flagInterpreter = flag.String("I", "", "use `linker` as ELF dynamic linker")
cmd/link: insert trampolines for too-far jumps on ARM ARM direct CALL/JMP instruction has 24 bit offset, which can only encodes jumps within +/-32M. When the target is too far, the top bits get truncated and the program jumps wild. This CL detects too-far jumps and automatically insert trampolines, currently only internal linking on ARM. It is necessary to make the following changes to the linker: - Resolve direct jump relocs when assigning addresses to functions. this allows trampoline insertion without moving all code that already laid down. - Lay down packages in dependency order, so that when resolving a inter-package direct jump reloc, the target address is already known. Intra-package jumps are assumed never too far. - a linker flag -debugtramp is added for debugging trampolines: "-debugtramp=1 -v" prints trampoline debug message "-debugtramp=2" forces all inter-package jump to use trampolines (currently ARM only) "-debugtramp=2 -v" does both - Some data structures are changed for bookkeeping. On ARM, pseudo DIV/DIVU/MOD/MODU instructions now clobber R8 (unfortunate). In the standard library there is no ARM assembly code that uses these instructions, and the compiler no longer emits them (CL 29390). all.bash passes with -debugtramp=2, except a disassembly test (this is unavoidable as we changed the instruction). TBD: debug info of trampolines? Fixes #17028. Change-Id: Idcce347ea7e0af77c4079041a160b2f6e114b474 Reviewed-on: https://go-review.googlesource.com/29397 Reviewed-by: David Crawshaw <crawshaw@golang.org> Run-TryBot: Cherry Zhang <cherryyz@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-09-14 14:47:12 -04:00
FlagDebugTramp = flag.Int("debugtramp", 0, "debug trampolines")
cmd/link: add optional sanity checking for duplicate symbols Introduce a new linker command line option "-strictdups", which enables sanity checking of "ok to duplicate" symbols, especially DWARF info symbols. Acceptable values are 0 (no checking) 1 (issue warnings) and 2 (issue a fatal error checks fail). Currently if we read a DWARF symbol (such as "go.info.PKG.FUNCTION") from one object file, and then encounter the same symbol later on while reading another object file, we simply discard the second one and move on with the link, since the two should in theory be identical. If as a result of a compiler bug we wind up with symbols that are not identical, this tends to (silently) result in incorrect DWARF generation, which may or may not be discovered depending on who is consuming the DWARF and what's being done with it. When this option is turned on, at the point where a duplicate symbol is detected in the object file reader, we check to make sure that the length/contents of the symbol are the same as the previously read symbol, and print a descriptive warning (or error) if not. For the time being this can be used for one-off testing to find problems; at some point it would be nice if we can enable it by default. Updates #30908. Change-Id: I64c4e07c326b4572db674ff17c93307e2eec607c Reviewed-on: https://go-review.googlesource.com/c/go/+/168410 Run-TryBot: Than McIntosh <thanm@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Keith Randall <khr@golang.org>
2019-03-21 09:20:11 -04:00
FlagStrictDups = flag.Int("strictdups", 0, "sanity check duplicate symbol contents during object file reading (1=warn 2=err).")
FlagNewDw = flag.Bool("newdw", true, "DWARF gen with new loader")
FlagRound = flag.Int("R", -1, "set address rounding `quantum`")
FlagTextAddr = flag.Int64("T", -1, "set text segment `address`")
flagEntrySymbol = flag.String("E", "", "set `entry` symbol name")
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`")
memprofile = flag.String("memprofile", "", "write memory profile to `file`")
memprofilerate = flag.Int64("memprofilerate", 0, "set runtime.MemProfileRate to `rate`")
benchmarkFlag = flag.String("benchmark", "", "set to 'mem' or 'cpu' to enable phase benchmarking")
)
// Main is the main entry point for the linker code.
func Main(arch *sys.Arch, theArch Arch) {
thearch = theArch
ctxt := linknew(arch)
ctxt.Bso = bufio.NewWriter(os.Stdout)
// For testing behavior of go command when tools crash silently.
// Undocumented, not in standard flag parser to avoid
// exposing in usage message.
for _, arg := range os.Args {
if arg == "-crash_for_testing" {
os.Exit(2)
}
}
cmd/link: set runtime.GOROOT default during link Suppose you build the Go toolchain in directory A, move the whole thing to directory B, and then use it from B to build a new program hello.exe, and then run hello.exe, and hello.exe crashes with a stack trace into the standard library. Long ago, you'd have seen hello.exe print file names in the A directory tree, even though the files had moved to the B directory tree. About two years ago we changed the compiler to write down these files with the name "$GOROOT" (that literal string) instead of A, so that the final link from B could replace "$GOROOT" with B, so that hello.exe's crash would show the correct source file paths in the stack trace. (golang.org/cl/18200) Now suppose that you do the same thing but hello.exe doesn't crash: it prints fmt.Println(runtime.GOROOT()). And you run hello.exe after clearing $GOROOT from the environment. Long ago, you'd have seen hello.exe print A instead of B. Before this CL, you'd still see hello.exe print A instead of B. This case is the one instance where a moved toolchain still divulges its origin. Not anymore. After this CL, hello.exe will print B, because the linker sets runtime/internal/sys.DefaultGoroot with the effective GOROOT from link time. This makes the default result of runtime.GOROOT once again match the file names recorded in the binary, after two years of divergence. With that cleared up, we can reintroduce GOROOT into the link action ID and also reenable TestExecutableGOROOT/RelocatedExe. When $GOROOT_FINAL is set during link, it is used in preference to $GOROOT, as always, but it was easier to explain the behavior above without introducing that complication. Fixes #22155. Fixes #20284. Fixes #22475. Change-Id: Ifdaeb77fd4678fdb337cf59ee25b2cd873ec1016 Reviewed-on: https://go-review.googlesource.com/86835 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2018-01-08 11:59:29 -05:00
final := gorootFinal()
addstrdata1(ctxt, "runtime/internal/sys.DefaultGoroot="+final)
addstrdata1(ctxt, "cmd/internal/objabi.defaultGOROOT="+final)
// TODO(matloob): define these above and then check flag values here
if ctxt.Arch.Family == sys.AMD64 && objabi.GOOS == "plan9" {
flag.BoolVar(&Flag8, "8", false, "use 64-bit addresses in symbol table")
}
flagHeadType := flag.String("H", "", "set header `type`")
flag.BoolVar(&ctxt.linkShared, "linkshared", false, "link against installed Go shared libraries")
flag.Var(&ctxt.LinkMode, "linkmode", "set link `mode`")
flag.Var(&ctxt.BuildMode, "buildmode", "set build `mode`")
flag.BoolVar(&ctxt.compressDWARF, "compressdwarf", true, "compress DWARF if possible")
objabi.Flagfn1("B", "add an ELF NT_GNU_BUILD_ID `note` when using ELF", addbuildinfo)
objabi.Flagfn1("L", "add specified `directory` to library path", func(a string) { Lflag(ctxt, a) })
objabi.AddVersionFlag() // -V
objabi.Flagfn1("X", "add string value `definition` of the form importpath.name=value", func(s string) { addstrdata1(ctxt, s) })
objabi.Flagcount("v", "print link trace", &ctxt.Debugvlog)
objabi.Flagfn1("importcfg", "read import configuration from `file`", ctxt.readImportCfg)
objabi.Flagparse(usage)
switch *flagHeadType {
case "":
case "windowsgui":
ctxt.HeadType = objabi.Hwindows
windowsgui = true
default:
if err := ctxt.HeadType.Set(*flagHeadType); err != nil {
Errorf(nil, "%v", err)
usage()
}
}
if objabi.Fieldtrack_enabled != 0 {
ctxt.Reachparent = make(map[*sym.Symbol]*sym.Symbol)
}
checkStrictDups = *FlagStrictDups
startProfile()
if ctxt.BuildMode == BuildModeUnset {
ctxt.BuildMode = BuildModeExe
}
if ctxt.BuildMode != BuildModeShared && flag.NArg() != 1 {
usage()
}
if *flagOutfile == "" {
*flagOutfile = "a.out"
if ctxt.HeadType == objabi.Hwindows {
*flagOutfile += ".exe"
}
}
interpreter = *flagInterpreter
// enable benchmarking
var bench *benchmark.Metrics
if len(*benchmarkFlag) != 0 {
if *benchmarkFlag == "mem" {
bench = benchmark.New(benchmark.GC)
} else if *benchmarkFlag == "cpu" {
bench = benchmark.New(benchmark.NoGC)
} else {
Errorf(nil, "unknown benchmark flag: %q", *benchmarkFlag)
usage()
}
}
bench.Start("libinit")
libinit(ctxt) // creates outfile
if ctxt.HeadType == objabi.Hunknown {
ctxt.HeadType.Set(objabi.GOOS)
}
bench.Start("computeTLSOffset")
ctxt.computeTLSOffset()
bench.Start("Archinit")
thearch.Archinit(ctxt)
if ctxt.linkShared && !ctxt.IsELF {
Exitf("-linkshared can only be used on elf systems")
}
if ctxt.Debugvlog != 0 {
ctxt.Logf("HEADER = -H%d -T0x%x -R0x%x\n", ctxt.HeadType, uint64(*FlagTextAddr), uint32(*FlagRound))
}
switch ctxt.BuildMode {
case 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]
}
pkglistfornote = append(pkglistfornote, pkgpath...)
pkglistfornote = append(pkglistfornote, '\n')
addlibpath(ctxt, "command line", "command line", file, pkgpath, "")
}
case BuildModePlugin:
addlibpath(ctxt, "command line", "command line", flag.Arg(0), *flagPluginPath, "")
default:
addlibpath(ctxt, "command line", "command line", flag.Arg(0), "main", "")
}
bench.Start("loadlib")
ctxt.loadlib()
bench.Start("deadcode")
deadcode(ctxt)
bench.Start("linksetup")
ctxt.linksetup()
bench.Start("dostrdata")
ctxt.dostrdata()
if objabi.Fieldtrack_enabled != 0 {
bench.Start("fieldtrack")
fieldtrack(ctxt.Arch, ctxt.loader)
}
if *FlagNewDw {
bench.Start("dwarfGenerateDebugInfo")
// DWARF-gen requires that the unit Textp2 slices be populated,
// so that it can walk the functions in each unit. Call into
// the loader to do this (requires that we collect the set of
// internal libraries first). NB: might be simpler if we moved
// isRuntimeDepPkg to cmd/internal and then did the test
// in loader.AssignTextSymbolOrder.
ctxt.Library = postorder(ctxt.Library)
intlibs := []bool{}
for _, lib := range ctxt.Library {
intlibs = append(intlibs, isRuntimeDepPkg(lib.Pkg))
}
ctxt.loader.AssignTextSymbolOrder(ctxt.Library, intlibs)
dwarfGenerateDebugInfo(ctxt)
}
bench.Start("loadlibfull")
ctxt.loadlibfull() // XXX do it here for now
if !*FlagNewDw {
bench.Start("dwarfGenerateDebugInfo")
dwarfGenerateDebugInfo(ctxt)
}
bench.Start("mangleTypeSym")
ctxt.mangleTypeSym()
bench.Start("callgraph")
ctxt.callgraph()
bench.Start("doelf")
ctxt.doelf()
if ctxt.HeadType == objabi.Hdarwin {
bench.Start("domacho")
ctxt.domacho()
}
bench.Start("dostkcheck")
ctxt.dostkcheck()
if ctxt.HeadType == objabi.Hwindows {
bench.Start("dope")
ctxt.dope()
bench.Start("windynrelocsyms")
ctxt.windynrelocsyms()
}
if ctxt.HeadType == objabi.Haix {
bench.Start("doxcoff")
ctxt.doxcoff()
}
bench.Start("addexport")
ctxt.addexport()
bench.Start("Gentext")
thearch.Gentext(ctxt) // trampolines, call stubs, etc.
bench.Start("textbuildid")
ctxt.textbuildid()
bench.Start("textaddress")
ctxt.textaddress()
bench.Start("pclntab")
ctxt.pclntab()
bench.Start("findfunctab")
ctxt.findfunctab()
bench.Start("typelink")
ctxt.typelink()
bench.Start("symtab")
ctxt.symtab()
bench.Start("buildinfo")
ctxt.buildinfo()
bench.Start("dodata")
ctxt.dodata()
bench.Start("address")
order := ctxt.address()
bench.Start("dwarfcompress")
cmd/link: compress DWARF sections in ELF binaries Forked from CL 111895. The trickiest part of this is that the binary layout code (blk, elfshbits, and various other things) assumes a constant offset between symbols' and sections' file locations and their virtual addresses. Compression, of course, breaks this constant offset. But we need to assign virtual addresses to everything before compression in order to resolve relocations before compression. As a result, compression needs to re-compute the "address" of the DWARF sections and symbols based on their compressed size. Luckily, these are at the end of the file, so this doesn't perturb any other sections or symbols. (And there is, of course, a surprising amount of code that assumes the DWARF segment comes last, so what's one more place?) Relevant benchmarks: name old time/op new time/op delta StdCmd 10.3s ± 2% 10.8s ± 1% +5.43% (p=0.000 n=30+30) name old text-bytes new text-bytes delta HelloSize 746kB ± 0% 746kB ± 0% ~ (all equal) CmdGoSize 8.41MB ± 0% 8.41MB ± 0% ~ (all equal) [Geo mean] 2.50MB 2.50MB +0.00% name old data-bytes new data-bytes delta HelloSize 10.6kB ± 0% 10.6kB ± 0% ~ (all equal) CmdGoSize 252kB ± 0% 252kB ± 0% ~ (all equal) [Geo mean] 51.5kB 51.5kB +0.00% name old bss-bytes new bss-bytes delta HelloSize 125kB ± 0% 125kB ± 0% ~ (all equal) CmdGoSize 145kB ± 0% 145kB ± 0% ~ (all equal) [Geo mean] 135kB 135kB +0.00% name old exe-bytes new exe-bytes delta HelloSize 1.60MB ± 0% 1.05MB ± 0% -34.39% (p=0.000 n=30+30) CmdGoSize 16.5MB ± 0% 11.3MB ± 0% -31.76% (p=0.000 n=30+30) [Geo mean] 5.14MB 3.44MB -33.08% Fixes #11799. Updates #6853. Change-Id: I64197afe4c01a237523a943088051ee056331c6f Reviewed-on: https://go-review.googlesource.com/118276 Run-TryBot: Heschi Kreinick <heschi@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Austin Clements <austin@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2018-05-05 21:49:40 -04:00
dwarfcompress(ctxt)
bench.Start("layout")
filesize := ctxt.layout(order)
// Write out the output file.
// It is split into two parts (Asmb and Asmb2). The first
// part writes most of the content (sections and segments),
// for which we have computed the size and offset, in a
// mmap'd region. The second part writes more content, for
// which we don't know the size.
var outputMmapped bool
if ctxt.Arch.Family != sys.Wasm {
// Don't mmap if we're building for Wasm. Wasm file
// layout is very different so filesize is meaningless.
err := ctxt.Out.Mmap(filesize)
outputMmapped = err == nil
}
if outputMmapped {
// Asmb will redirect symbols to the output file mmap, and relocations
// will be applied directly there.
bench.Start("Asmb")
thearch.Asmb(ctxt)
bench.Start("reloc")
ctxt.reloc()
bench.Start("Munmap")
ctxt.Out.Munmap()
} else {
// If we don't mmap, we need to apply relocations before
// writing out.
bench.Start("reloc")
ctxt.reloc()
bench.Start("Asmb")
thearch.Asmb(ctxt)
}
bench.Start("Asmb2")
thearch.Asmb2(ctxt)
bench.Start("undef")
ctxt.undef()
bench.Start("hostlink")
ctxt.hostlink()
if ctxt.Debugvlog != 0 {
ctxt.Logf("%d symbols\n", len(ctxt.Syms.Allsym))
ctxt.Logf("%d liveness data\n", liveness)
}
bench.Start("Flush")
ctxt.Bso.Flush()
bench.Start("archive")
ctxt.archive()
bench.Report(os.Stdout)
errorexit()
}
type Rpath struct {
set bool
val string
}
func (r *Rpath) Set(val string) error {
r.set = true
r.val = val
return nil
}
func (r *Rpath) String() string {
return r.val
}
func startProfile() {
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatalf("%v", err)
}
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatalf("%v", err)
}
AtExit(pprof.StopCPUProfile)
}
if *memprofile != "" {
if *memprofilerate != 0 {
runtime.MemProfileRate = int(*memprofilerate)
}
f, err := os.Create(*memprofile)
if err != nil {
log.Fatalf("%v", err)
}
AtExit(func() {
// Profile all outstanding allocations.
runtime.GC()
// compilebench parses the memory profile to extract memstats,
// which are only written in the legacy pprof format.
// See golang.org/issue/18641 and runtime/pprof/pprof.go:writeHeap.
const writeLegacyFormat = 1
if err := pprof.Lookup("heap").WriteTo(f, writeLegacyFormat); err != nil {
log.Fatalf("%v", err)
}
})
}
}