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
|
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"
|
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-08-19 22:40:38 -04:00
|
|
|
ctxt := linknew(SysArch)
|
2016-08-21 13:52:23 -04:00
|
|
|
ctxt.Bso = bufio.NewWriter(os.Stdout)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-08-21 18:25:28 -04:00
|
|
|
Debug = [128]bool{}
|
2015-02-27 22:57:28 -05:00
|
|
|
nerrors = 0
|
|
|
|
|
HEADTYPE = -1
|
|
|
|
|
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" {
|
2016-08-21 18:25:28 -04:00
|
|
|
flag.BoolVar(&Debug['8'], "8", false, "use 64-bit addresses in symbol table")
|
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)
|
2016-08-21 18:25:28 -04:00
|
|
|
flag.BoolVar(&Debug['C'], "C", false, "check Go calls to C code")
|
|
|
|
|
flag.Int64Var(&INITDAT, "D", -1, "set data segment `address`")
|
|
|
|
|
flag.StringVar(&INITENTRY, "E", "", "set `entry` symbol name")
|
2015-05-01 16:14:50 -07:00
|
|
|
obj.Flagfn1("I", "use `linker` as ELF dynamic linker", setinterp)
|
2016-08-19 22:40:38 -04:00
|
|
|
obj.Flagfn1("L", "add specified `directory` to library path", func(a string) { Lflag(ctxt, a) })
|
2015-05-01 16:14:50 -07:00
|
|
|
obj.Flagfn1("H", "set header `type`", setheadtype)
|
2016-08-21 18:25:28 -04:00
|
|
|
flag.IntVar(&INITRND, "R", -1, "set address rounding `quantum`")
|
|
|
|
|
flag.Int64Var(&INITTEXT, "T", -1, "set text segment `address`")
|
2015-02-27 22:57:28 -05:00
|
|
|
obj.Flagfn0("V", "print version and exit", doversion)
|
2016-08-19 22:40:38 -04:00
|
|
|
obj.Flagfn1("X", "add string value `definition` of the form importpath.name=value", func(s string) { addstrdata1(ctxt, s) })
|
2016-08-21 18:25:28 -04:00
|
|
|
flag.BoolVar(&Debug['a'], "a", false, "disassemble output")
|
|
|
|
|
flag.StringVar(&buildid, "buildid", "", "record `id` as Go toolchain build id")
|
2015-05-01 16:14:50 -07:00
|
|
|
flag.Var(&Buildmode, "buildmode", "set build `mode`")
|
2016-08-21 18:25:28 -04:00
|
|
|
flag.BoolVar(&Debug['c'], "c", false, "dump call graph")
|
|
|
|
|
flag.BoolVar(&Debug['d'], "d", false, "disable dynamic executable")
|
2016-04-22 18:49:59 -07:00
|
|
|
flag.BoolVar(&flag_dumpdep, "dumpdep", false, "dump symbol dependency graph")
|
2016-08-21 18:25:28 -04:00
|
|
|
flag.StringVar(&extar, "extar", "", "archive program for buildmode=c-archive")
|
|
|
|
|
flag.StringVar(&extld, "extld", "", "use `linker` when linking in external mode")
|
|
|
|
|
flag.StringVar(&extldflags, "extldflags", "", "pass `flags` to external linker")
|
|
|
|
|
flag.BoolVar(&Debug['f'], "f", false, "ignore version mismatch")
|
|
|
|
|
flag.BoolVar(&Debug['g'], "g", false, "disable go package data checks")
|
|
|
|
|
flag.BoolVar(&Debug['h'], "h", false, "halt on error")
|
|
|
|
|
flag.StringVar(&flag_installsuffix, "installsuffix", "", "set package directory `suffix`")
|
|
|
|
|
flag.StringVar(&tracksym, "k", "", "set field tracking `symbol`")
|
|
|
|
|
flag.StringVar(&libgccfile, "libgcc", "", "compiler support lib for internal linking; use \"none\" to disable")
|
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")
|
2016-08-21 18:25:28 -04:00
|
|
|
flag.BoolVar(&flag_msan, "msan", false, "enable MSan interface")
|
|
|
|
|
flag.BoolVar(&Debug['n'], "n", false, "dump symbol table")
|
|
|
|
|
flag.StringVar(&outfile, "o", "", "write output to `file`")
|
2015-05-01 16:14:50 -07:00
|
|
|
flag.Var(&rpath, "r", "set the ELF dynamic linker search `path` to dir1:dir2:...")
|
2016-08-21 18:25:28 -04:00
|
|
|
flag.BoolVar(&flag_race, "race", false, "enable race detector")
|
|
|
|
|
flag.BoolVar(&Debug['s'], "s", false, "disable symbol table")
|
|
|
|
|
var flagShared bool
|
2016-04-06 12:01:40 -07:00
|
|
|
if SysArch.InFamily(sys.ARM, sys.AMD64) {
|
2016-08-21 18:25:28 -04:00
|
|
|
flag.BoolVar(&flagShared, "shared", false, "generate shared object (implies -linkmode external)")
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2016-08-21 18:25:28 -04:00
|
|
|
flag.StringVar(&tmpdir, "tmpdir", "", "use `directory` for temporary files")
|
|
|
|
|
flag.BoolVar(&Debug['u'], "u", false, "reject unsafe packages")
|
|
|
|
|
obj.Flagcount("v", "print link trace", &ctxt.Debugvlog)
|
|
|
|
|
flag.BoolVar(&Debug['w'], "w", false, "disable DWARF generation")
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-08-21 18:25:28 -04:00
|
|
|
flag.StringVar(&cpuprofile, "cpuprofile", "", "write cpu profile to `file`")
|
|
|
|
|
flag.StringVar(&memprofile, "memprofile", "", "write memory profile to `file`")
|
|
|
|
|
flag.Int64Var(&memprofilerate, "memprofilerate", 0, "set runtime.MemProfileRate to `rate`")
|
2015-05-21 14:35:02 -04:00
|
|
|
|
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-08-21 13:52:23 -04:00
|
|
|
ctxt.Bso = ctxt.Bso
|
2016-08-21 18:25:28 -04:00
|
|
|
if flagShared {
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-19 22:40:38 -04:00
|
|
|
libinit(ctxt) // creates outfile
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
if HEADTYPE == -1 {
|
|
|
|
|
HEADTYPE = int32(headtype(goos))
|
|
|
|
|
}
|
2016-08-19 22:40:38 -04:00
|
|
|
ctxt.Headtype = int(HEADTYPE)
|
2015-02-27 22:57:28 -05:00
|
|
|
if headstring == "" {
|
|
|
|
|
headstring = Headstr(int(HEADTYPE))
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-21 13:52:23 -04:00
|
|
|
Thearch.Archinit(ctxt)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2016-08-21 18:25:28 -04:00
|
|
|
if ctxt.Debugvlog != 0 {
|
2016-08-21 13:52:23 -04:00
|
|
|
fmt.Fprintf(ctxt.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
|
|
|
}
|
2016-08-21 13:52:23 -04:00
|
|
|
ctxt.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')
|
2016-08-19 22:40:38 -04:00
|
|
|
addlibpath(ctxt, "command line", "command line", file, pkgpath, "")
|
2015-03-30 02:59:10 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
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
|
|
|
|
2016-08-19 22:40:38 -04:00
|
|
|
ctxt.checkstrdata()
|
|
|
|
|
deadcode(ctxt)
|
|
|
|
|
fieldtrack(ctxt)
|
|
|
|
|
ctxt.callgraph()
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-08-19 22:40:38 -04:00
|
|
|
ctxt.doelf()
|
2015-04-19 19:33:58 -07:00
|
|
|
if HEADTYPE == obj.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()
|
2015-04-19 19:33:58 -07:00
|
|
|
if HEADTYPE == obj.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()
|
|
|
|
|
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 {
|
2016-08-21 13:52:23 -04:00
|
|
|
fmt.Fprintf(ctxt.Bso, "%5.2f cpu time\n", obj.Cputime())
|
|
|
|
|
fmt.Fprintf(ctxt.Bso, "%d symbols\n", len(ctxt.Allsym))
|
|
|
|
|
fmt.Fprintf(ctxt.Bso, "%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
|
|
|
}
|