2015-02-27 22:57:28 -05:00
|
|
|
// Inferno utils/6l/obj.c
|
2016-08-28 17:04:46 -07:00
|
|
|
// https://bitbucket.org/inferno-os/inferno-os/src/default/utils/6l/obj.c
|
2015-02-27 22:57:28 -05:00
|
|
|
//
|
|
|
|
|
// 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
|
2016-04-10 14:32:26 -07:00
|
|
|
// Portions Copyright © 2009 The Go Authors. All rights reserved.
|
2015-02-27 22:57:28 -05:00
|
|
|
//
|
|
|
|
|
// 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-08 19:30:41 +10:00
|
|
|
"bufio"
|
2017-04-18 12:53:25 -07:00
|
|
|
"cmd/internal/objabi"
|
2016-04-06 12:01:40 -07:00
|
|
|
"cmd/internal/sys"
|
2015-02-27 22:57:28 -05:00
|
|
|
"flag"
|
2016-08-21 18:34:24 -04:00
|
|
|
"log"
|
2015-02-27 22:57:28 -05:00
|
|
|
"os"
|
2016-08-21 18:34:24 -04:00
|
|
|
"runtime"
|
|
|
|
|
"runtime/pprof"
|
2015-02-27 22:57:28 -05:00
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2015-06-04 14:31:05 -04:00
|
|
|
var (
|
|
|
|
|
pkglistfornote []byte
|
2017-03-27 15:58:14 +11:00
|
|
|
windowsgui bool // writes a "GUI binary" instead of a "console binary"
|
2015-06-04 14:31:05 -04:00
|
|
|
)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-08-21 18:34:24 -04:00
|
|
|
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`")
|
2016-10-30 15:31:21 -04:00
|
|
|
flagPluginPath = flag.String("pluginpath", "", "full path name for plugin")
|
2016-08-21 18:34:24 -04:00
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
2016-08-22 22:29:24 -07:00
|
|
|
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")
|
2016-09-14 14:47:12 -04:00
|
|
|
FlagDebugTramp = flag.Int("debugtramp", 0, "debug trampolines")
|
2016-08-21 18:34:24 -04:00
|
|
|
|
|
|
|
|
FlagRound = flag.Int("R", -1, "set address rounding `quantum`")
|
|
|
|
|
FlagTextAddr = flag.Int64("T", -1, "set text segment `address`")
|
|
|
|
|
FlagDataAddr = flag.Int64("D", -1, "set data 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`")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Main is the main entry point for the linker code.
|
2017-09-30 21:10:49 +00:00
|
|
|
func Main(arch *sys.Arch, theArch Arch) {
|
|
|
|
|
Thearch = theArch
|
|
|
|
|
ctxt := linknew(arch)
|
2016-08-21 13:52:23 -04:00
|
|
|
ctxt.Bso = bufio.NewWriter(os.Stdout)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
|
2016-08-21 18:34:24 -04:00
|
|
|
// TODO(matloob): define these above and then check flag values here
|
2017-09-30 21:10:49 +00:00
|
|
|
if ctxt.Arch.Family == sys.AMD64 && objabi.GOOS == "plan9" {
|
2016-08-21 18:34:24 -04:00
|
|
|
flag.BoolVar(&Flag8, "8", false, "use 64-bit addresses in symbol table")
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2017-10-07 13:49:44 -04:00
|
|
|
flagHeadType := flag.String("H", "", "set header `type`")
|
2017-10-07 13:28:51 -04:00
|
|
|
flag.BoolVar(&ctxt.linkShared, "linkshared", false, "link against installed Go shared libraries")
|
2017-10-05 10:20:17 -04:00
|
|
|
flag.Var(&ctxt.LinkMode, "linkmode", "set link `mode`")
|
|
|
|
|
flag.Var(&ctxt.BuildMode, "buildmode", "set build `mode`")
|
2017-04-18 12:53:25 -07:00
|
|
|
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) })
|
2017-10-05 15:37:13 -04:00
|
|
|
objabi.AddVersionFlag() // -V
|
2017-04-18 12:53:25 -07:00
|
|
|
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)
|
2017-05-31 11:35:29 -04:00
|
|
|
objabi.Flagfn1("importcfg", "read import configuration from `file`", ctxt.readImportCfg)
|
2015-05-21 14:35:02 -04:00
|
|
|
|
2017-04-18 12:53:25 -07:00
|
|
|
objabi.Flagparse(usage)
|
2015-05-21 14:35:02 -04:00
|
|
|
|
2017-10-07 13:49:44 -04:00
|
|
|
switch *flagHeadType {
|
2017-03-27 15:58:14 +11:00
|
|
|
case "":
|
|
|
|
|
case "windowsgui":
|
2017-10-07 13:49:44 -04:00
|
|
|
ctxt.HeadType = objabi.Hwindows
|
2017-03-27 15:58:14 +11:00
|
|
|
windowsgui = true
|
|
|
|
|
default:
|
2017-10-07 13:49:44 -04:00
|
|
|
if err := ctxt.HeadType.Set(*flagHeadType); err != nil {
|
2017-03-27 15:58:14 +11:00
|
|
|
Errorf(nil, "%v", err)
|
|
|
|
|
usage()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 22:57:28 -05:00
|
|
|
startProfile()
|
2017-10-05 10:20:17 -04:00
|
|
|
if ctxt.BuildMode == BuildModeUnset {
|
|
|
|
|
ctxt.BuildMode = BuildModeExe
|
2015-05-21 14:42:14 -04:00
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2017-10-05 10:20:17 -04:00
|
|
|
if ctxt.BuildMode != BuildModeShared && flag.NArg() != 1 {
|
2015-02-27 22:57:28 -05:00
|
|
|
usage()
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-21 18:34:24 -04:00
|
|
|
if *flagOutfile == "" {
|
|
|
|
|
*flagOutfile = "a.out"
|
2017-10-07 13:49:44 -04:00
|
|
|
if ctxt.HeadType == objabi.Hwindows {
|
2016-08-21 18:34:24 -04:00
|
|
|
*flagOutfile += ".exe"
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-22 22:29:24 -07:00
|
|
|
interpreter = *flagInterpreter
|
|
|
|
|
|
2016-08-19 22:40:38 -04:00
|
|
|
libinit(ctxt) // creates outfile
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2017-10-07 13:49:44 -04:00
|
|
|
if ctxt.HeadType == objabi.Hunknown {
|
|
|
|
|
ctxt.HeadType.Set(objabi.GOOS)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-09-09 06:20:44 -04:00
|
|
|
ctxt.computeTLSOffset()
|
2016-08-21 13:52:23 -04:00
|
|
|
Thearch.Archinit(ctxt)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2017-10-07 13:43:38 -04:00
|
|
|
if ctxt.linkShared && !ctxt.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
|
|
|
}
|
|
|
|
|
|
2016-08-21 18:25:28 -04:00
|
|
|
if ctxt.Debugvlog != 0 {
|
2017-10-07 13:49:44 -04:00
|
|
|
ctxt.Logf("HEADER = -H%d -T0x%x -D0x%x -R0x%x\n", ctxt.HeadType, uint64(*FlagTextAddr), uint64(*FlagDataAddr), uint32(*FlagRound))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2017-10-05 10:20:17 -04:00
|
|
|
switch ctxt.BuildMode {
|
|
|
|
|
case BuildModeShared:
|
2015-03-30 02:59:10 +00:00
|
|
|
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')
|
2016-08-19 22:40:38 -04:00
|
|
|
addlibpath(ctxt, "command line", "command line", file, pkgpath, "")
|
2015-03-30 02:59:10 +00:00
|
|
|
}
|
2017-10-05 10:20:17 -04:00
|
|
|
case BuildModePlugin:
|
2016-10-30 15:31:21 -04:00
|
|
|
addlibpath(ctxt, "command line", "command line", flag.Arg(0), *flagPluginPath, "")
|
2016-08-25 21:58:45 -04:00
|
|
|
default:
|
2016-08-19 22:40:38 -04:00
|
|
|
addlibpath(ctxt, "command line", "command line", flag.Arg(0), "main", "")
|
2015-03-30 02:59:10 +00:00
|
|
|
}
|
2016-08-19 22:40:38 -04:00
|
|
|
ctxt.loadlib()
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2018-01-09 10:10:46 -05:00
|
|
|
ctxt.dostrdata()
|
2016-08-19 22:40:38 -04:00
|
|
|
deadcode(ctxt)
|
|
|
|
|
fieldtrack(ctxt)
|
|
|
|
|
ctxt.callgraph()
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-08-19 22:40:38 -04:00
|
|
|
ctxt.doelf()
|
2017-10-07 13:49:44 -04:00
|
|
|
if ctxt.HeadType == objabi.Hdarwin {
|
2016-08-19 22:40:38 -04:00
|
|
|
ctxt.domacho()
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2016-08-19 22:40:38 -04:00
|
|
|
ctxt.dostkcheck()
|
2017-10-07 13:49:44 -04:00
|
|
|
if ctxt.HeadType == objabi.Hwindows {
|
2016-08-19 22:40:38 -04:00
|
|
|
ctxt.dope()
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2016-08-19 22:40:38 -04:00
|
|
|
ctxt.addexport()
|
2016-08-21 13:52:23 -04:00
|
|
|
Thearch.Gentext(ctxt) // trampolines, call stubs, etc.
|
2016-08-19 22:40:38 -04:00
|
|
|
ctxt.textbuildid()
|
|
|
|
|
ctxt.textaddress()
|
|
|
|
|
ctxt.pclntab()
|
|
|
|
|
ctxt.findfunctab()
|
2016-10-19 07:33:16 +03:00
|
|
|
ctxt.typelink()
|
2016-08-19 22:40:38 -04:00
|
|
|
ctxt.symtab()
|
|
|
|
|
ctxt.dodata()
|
|
|
|
|
ctxt.address()
|
|
|
|
|
ctxt.reloc()
|
|
|
|
|
Thearch.Asmb(ctxt)
|
|
|
|
|
ctxt.undef()
|
|
|
|
|
ctxt.hostlink()
|
2016-08-21 13:52:23 -04:00
|
|
|
ctxt.archive()
|
2016-08-21 18:25:28 -04:00
|
|
|
if ctxt.Debugvlog != 0 {
|
2017-04-18 12:53:25 -07:00
|
|
|
ctxt.Logf("%5.2f cpu time\n", Cputime())
|
2016-09-20 14:59:39 +12:00
|
|
|
ctxt.Logf("%d symbols\n", len(ctxt.Syms.Allsym))
|
2016-08-25 12:32:42 +10:00
|
|
|
ctxt.Logf("%d liveness data\n", liveness)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-08-21 13:52:23 -04:00
|
|
|
ctxt.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
|
|
|
}
|
2016-08-21 18:34:24 -04:00
|
|
|
|
|
|
|
|
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() {
|
|
|
|
|
runtime.GC() // profile all outstanding allocations
|
|
|
|
|
if err := pprof.WriteHeapProfile(f); err != nil {
|
|
|
|
|
log.Fatalf("%v", err)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|