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-04-08 19:30:41 +10:00
|
|
|
Bso = bufio.NewWriter(os.Stdout)
|
2016-04-09 12:54:45 +10:00
|
|
|
|
2016-08-19 22:40:38 -04:00
|
|
|
ctxt := linknew(SysArch)
|
|
|
|
|
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)
|
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)
|
|
|
|
|
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)
|
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) })
|
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-04-22 18:49:59 -07:00
|
|
|
flag.BoolVar(&flag_dumpdep, "dumpdep", false, "dump symbol dependency graph")
|
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-08-19 22:40:38 -04:00
|
|
|
ctxt.Bso = Bso
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
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')
|
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()
|
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())
|
2016-08-19 22:40:38 -04:00
|
|
|
fmt.Fprintf(Bso, "%d symbols\n", len(ctxt.Allsym))
|
2016-04-09 12:54:45 +10:00
|
|
|
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
|
|
|
}
|