github.com/sbinet/go@v0.0.0-20160827155028-54d7de7dd62b/src/cmd/link/internal/ld/main.go (about)

     1  // Inferno utils/6l/obj.c
     2  // http://code.google.com/p/inferno-os/source/browse/utils/6l/obj.c
     3  //
     4  //	Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
     5  //	Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
     6  //	Portions Copyright © 1997-1999 Vita Nuova Limited
     7  //	Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
     8  //	Portions Copyright © 2004,2006 Bruce Ellis
     9  //	Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
    10  //	Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
    11  //	Portions Copyright © 2009 The Go Authors. All rights reserved.
    12  //
    13  // Permission is hereby granted, free of charge, to any person obtaining a copy
    14  // of this software and associated documentation files (the "Software"), to deal
    15  // in the Software without restriction, including without limitation the rights
    16  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    17  // copies of the Software, and to permit persons to whom the Software is
    18  // furnished to do so, subject to the following conditions:
    19  //
    20  // The above copyright notice and this permission notice shall be included in
    21  // all copies or substantial portions of the Software.
    22  //
    23  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    24  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    25  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    26  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    27  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    28  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    29  // THE SOFTWARE.
    30  
    31  package ld
    32  
    33  import (
    34  	"bufio"
    35  	"cmd/internal/obj"
    36  	"cmd/internal/sys"
    37  	"flag"
    38  	"fmt"
    39  	"log"
    40  	"os"
    41  	"runtime"
    42  	"runtime/pprof"
    43  	"strings"
    44  )
    45  
    46  var (
    47  	pkglistfornote []byte
    48  )
    49  
    50  func init() {
    51  	flag.Var(&Buildmode, "buildmode", "set build `mode`")
    52  	flag.Var(&rpath, "r", "set the ELF dynamic linker search `path` to dir1:dir2:...")
    53  }
    54  
    55  // Flags used by the linker. The exported flags are used by the architecture-specific packages.
    56  var (
    57  	flagBuildid = flag.String("buildid", "", "record `id` as Go toolchain build id")
    58  
    59  	flagOutfile    = flag.String("o", "", "write output to `file`")
    60  	FlagLinkshared = flag.Bool("linkshared", false, "link against installed Go shared libraries")
    61  	Buildmode      BuildMode
    62  
    63  	flagInstallSuffix = flag.String("installsuffix", "", "set package directory `suffix`")
    64  	flagDumpDep       = flag.Bool("dumpdep", false, "dump symbol dependency graph")
    65  	flagRace          = flag.Bool("race", false, "enable race detector")
    66  	flagMsan          = flag.Bool("msan", false, "enable MSan interface")
    67  
    68  	flagFieldTrack = flag.String("k", "", "set field tracking `symbol`")
    69  	flagLibGCC     = flag.String("libgcc", "", "compiler support lib for internal linking; use \"none\" to disable")
    70  	flagTmpdir     = flag.String("tmpdir", "", "use `directory` for temporary files")
    71  
    72  	flagExtld      = flag.String("extld", "", "use `linker` when linking in external mode")
    73  	flagExtldflags = flag.String("extldflags", "", "pass `flags` to external linker")
    74  	flagExtar      = flag.String("extar", "", "archive program for buildmode=c-archive")
    75  
    76  	flagA           = flag.Bool("a", false, "disassemble output")
    77  	FlagC           = flag.Bool("c", false, "dump call graph")
    78  	FlagD           = flag.Bool("d", false, "disable dynamic executable")
    79  	flagF           = flag.Bool("f", false, "ignore version mismatch")
    80  	flagG           = flag.Bool("g", false, "disable go package data checks")
    81  	flagH           = flag.Bool("h", false, "halt on error")
    82  	flagN           = flag.Bool("n", false, "dump symbol table")
    83  	FlagS           = flag.Bool("s", false, "disable symbol table")
    84  	flagU           = flag.Bool("u", false, "reject unsafe packages")
    85  	FlagW           = flag.Bool("w", false, "disable DWARF generation")
    86  	Flag8           bool // use 64-bit addresses in symbol table
    87  	flagInterpreter = flag.String("I", "", "use `linker` as ELF dynamic linker")
    88  
    89  	FlagRound       = flag.Int("R", -1, "set address rounding `quantum`")
    90  	FlagTextAddr    = flag.Int64("T", -1, "set text segment `address`")
    91  	FlagDataAddr    = flag.Int64("D", -1, "set data segment `address`")
    92  	flagEntrySymbol = flag.String("E", "", "set `entry` symbol name")
    93  
    94  	cpuprofile     = flag.String("cpuprofile", "", "write cpu profile to `file`")
    95  	memprofile     = flag.String("memprofile", "", "write memory profile to `file`")
    96  	memprofilerate = flag.Int64("memprofilerate", 0, "set runtime.MemProfileRate to `rate`")
    97  )
    98  
    99  // Main is the main entry point for the linker code.
   100  func Main() {
   101  	ctxt := linknew(SysArch)
   102  	ctxt.Bso = bufio.NewWriter(os.Stdout)
   103  
   104  	nerrors = 0
   105  	HEADTYPE = -1
   106  	Linkmode = LinkAuto
   107  
   108  	// For testing behavior of go command when tools crash silently.
   109  	// Undocumented, not in standard flag parser to avoid
   110  	// exposing in usage message.
   111  	for _, arg := range os.Args {
   112  		if arg == "-crash_for_testing" {
   113  			os.Exit(2)
   114  		}
   115  	}
   116  
   117  	// TODO(matloob): define these above and then check flag values here
   118  	if SysArch.Family == sys.AMD64 && obj.Getgoos() == "plan9" {
   119  		flag.BoolVar(&Flag8, "8", false, "use 64-bit addresses in symbol table")
   120  	}
   121  	obj.Flagfn1("B", "add an ELF NT_GNU_BUILD_ID `note` when using ELF", addbuildinfo)
   122  	obj.Flagfn1("L", "add specified `directory` to library path", func(a string) { Lflag(ctxt, a) })
   123  	obj.Flagfn1("H", "set header `type`", setheadtype)
   124  	obj.Flagfn0("V", "print version and exit", doversion)
   125  	obj.Flagfn1("X", "add string value `definition` of the form importpath.name=value", func(s string) { addstrdata1(ctxt, s) })
   126  	obj.Flagcount("v", "print link trace", &ctxt.Debugvlog)
   127  	obj.Flagfn1("linkmode", "set link `mode` (internal, external, auto)", setlinkmode)
   128  	var flagShared bool
   129  	if SysArch.InFamily(sys.ARM, sys.AMD64) {
   130  		flag.BoolVar(&flagShared, "shared", false, "generate shared object (implies -linkmode external)")
   131  	}
   132  
   133  	obj.Flagparse(usage)
   134  
   135  	startProfile()
   136  	if flagShared {
   137  		if Buildmode == BuildmodeUnset {
   138  			Buildmode = BuildmodeCShared
   139  		} else if Buildmode != BuildmodeCShared {
   140  			Exitf("-shared and -buildmode=%s are incompatible", Buildmode.String())
   141  		}
   142  	}
   143  	if Buildmode == BuildmodeUnset {
   144  		Buildmode = BuildmodeExe
   145  	}
   146  
   147  	if Buildmode != BuildmodeShared && flag.NArg() != 1 {
   148  		usage()
   149  	}
   150  
   151  	if *flagOutfile == "" {
   152  		*flagOutfile = "a.out"
   153  		if HEADTYPE == obj.Hwindows {
   154  			*flagOutfile += ".exe"
   155  		}
   156  	}
   157  
   158  	interpreter = *flagInterpreter
   159  
   160  	libinit(ctxt) // creates outfile
   161  
   162  	if HEADTYPE == -1 {
   163  		HEADTYPE = int32(headtype(goos))
   164  	}
   165  	ctxt.Headtype = int(HEADTYPE)
   166  	if headstring == "" {
   167  		headstring = Headstr(int(HEADTYPE))
   168  	}
   169  
   170  	Thearch.Archinit(ctxt)
   171  
   172  	if *FlagLinkshared && !Iself {
   173  		Exitf("-linkshared can only be used on elf systems")
   174  	}
   175  
   176  	if ctxt.Debugvlog != 0 {
   177  		ctxt.Logf("HEADER = -H%d -T0x%x -D0x%x -R0x%x\n", HEADTYPE, uint64(*FlagTextAddr), uint64(*FlagDataAddr), uint32(*FlagRound))
   178  	}
   179  
   180  	if Buildmode == BuildmodeShared {
   181  		for i := 0; i < flag.NArg(); i++ {
   182  			arg := flag.Arg(i)
   183  			parts := strings.SplitN(arg, "=", 2)
   184  			var pkgpath, file string
   185  			if len(parts) == 1 {
   186  				pkgpath, file = "main", arg
   187  			} else {
   188  				pkgpath, file = parts[0], parts[1]
   189  			}
   190  			pkglistfornote = append(pkglistfornote, pkgpath...)
   191  			pkglistfornote = append(pkglistfornote, '\n')
   192  			addlibpath(ctxt, "command line", "command line", file, pkgpath, "")
   193  		}
   194  	} else {
   195  		addlibpath(ctxt, "command line", "command line", flag.Arg(0), "main", "")
   196  	}
   197  	ctxt.loadlib()
   198  
   199  	ctxt.checkstrdata()
   200  	deadcode(ctxt)
   201  	fieldtrack(ctxt)
   202  	ctxt.callgraph()
   203  
   204  	ctxt.doelf()
   205  	if HEADTYPE == obj.Hdarwin {
   206  		ctxt.domacho()
   207  	}
   208  	ctxt.dostkcheck()
   209  	if HEADTYPE == obj.Hwindows {
   210  		ctxt.dope()
   211  	}
   212  	ctxt.addexport()
   213  	Thearch.Gentext(ctxt) // trampolines, call stubs, etc.
   214  	ctxt.textbuildid()
   215  	ctxt.textaddress()
   216  	ctxt.pclntab()
   217  	ctxt.findfunctab()
   218  	ctxt.symtab()
   219  	ctxt.dodata()
   220  	ctxt.address()
   221  	ctxt.reloc()
   222  	Thearch.Asmb(ctxt)
   223  	ctxt.undef()
   224  	ctxt.hostlink()
   225  	ctxt.archive()
   226  	if ctxt.Debugvlog != 0 {
   227  		ctxt.Logf("%5.2f cpu time\n", obj.Cputime())
   228  		ctxt.Logf("%d symbols\n", len(ctxt.Allsym))
   229  		ctxt.Logf("%d liveness data\n", liveness)
   230  	}
   231  
   232  	ctxt.Bso.Flush()
   233  
   234  	errorexit()
   235  }
   236  
   237  // A BuildMode indicates the sort of object we are building:
   238  //   "exe": build a main package and everything it imports into an executable.
   239  //   "c-shared": build a main package, plus all packages that it imports, into a
   240  //     single C shared library. The only callable symbols will be those functions
   241  //     marked as exported.
   242  //   "shared": combine all packages passed on the command line, and their
   243  //     dependencies, into a single shared library that will be used when
   244  //     building with the -linkshared option.
   245  type BuildMode uint8
   246  
   247  const (
   248  	BuildmodeUnset BuildMode = iota
   249  	BuildmodeExe
   250  	BuildmodePIE
   251  	BuildmodeCArchive
   252  	BuildmodeCShared
   253  	BuildmodeShared
   254  )
   255  
   256  func (mode *BuildMode) Set(s string) error {
   257  	goos := obj.Getgoos()
   258  	goarch := obj.Getgoarch()
   259  	badmode := func() error {
   260  		return fmt.Errorf("buildmode %s not supported on %s/%s", s, goos, goarch)
   261  	}
   262  	switch s {
   263  	default:
   264  		return fmt.Errorf("invalid buildmode: %q", s)
   265  	case "exe":
   266  		*mode = BuildmodeExe
   267  	case "pie":
   268  		switch goos {
   269  		case "android", "linux":
   270  		default:
   271  			return badmode()
   272  		}
   273  		*mode = BuildmodePIE
   274  	case "c-archive":
   275  		switch goos {
   276  		case "darwin", "linux":
   277  		case "windows":
   278  			switch goarch {
   279  			case "amd64", "386":
   280  			default:
   281  				return badmode()
   282  			}
   283  		default:
   284  			return badmode()
   285  		}
   286  		*mode = BuildmodeCArchive
   287  	case "c-shared":
   288  		switch goarch {
   289  		case "386", "amd64", "arm", "arm64":
   290  		default:
   291  			return badmode()
   292  		}
   293  		*mode = BuildmodeCShared
   294  	case "shared":
   295  		switch goos {
   296  		case "linux":
   297  			switch goarch {
   298  			case "386", "amd64", "arm", "arm64", "ppc64le", "s390x":
   299  			default:
   300  				return badmode()
   301  			}
   302  		default:
   303  			return badmode()
   304  		}
   305  		*mode = BuildmodeShared
   306  	}
   307  	return nil
   308  }
   309  
   310  func (mode *BuildMode) String() string {
   311  	switch *mode {
   312  	case BuildmodeUnset:
   313  		return "" // avoid showing a default in usage message
   314  	case BuildmodeExe:
   315  		return "exe"
   316  	case BuildmodePIE:
   317  		return "pie"
   318  	case BuildmodeCArchive:
   319  		return "c-archive"
   320  	case BuildmodeCShared:
   321  		return "c-shared"
   322  	case BuildmodeShared:
   323  		return "shared"
   324  	}
   325  	return fmt.Sprintf("BuildMode(%d)", uint8(*mode))
   326  }
   327  
   328  type Rpath struct {
   329  	set bool
   330  	val string
   331  }
   332  
   333  func (r *Rpath) Set(val string) error {
   334  	r.set = true
   335  	r.val = val
   336  	return nil
   337  }
   338  
   339  func (r *Rpath) String() string {
   340  	return r.val
   341  }
   342  
   343  func startProfile() {
   344  	if *cpuprofile != "" {
   345  		f, err := os.Create(*cpuprofile)
   346  		if err != nil {
   347  			log.Fatalf("%v", err)
   348  		}
   349  		if err := pprof.StartCPUProfile(f); err != nil {
   350  			log.Fatalf("%v", err)
   351  		}
   352  		AtExit(pprof.StopCPUProfile)
   353  	}
   354  	if *memprofile != "" {
   355  		if *memprofilerate != 0 {
   356  			runtime.MemProfileRate = int(*memprofilerate)
   357  		}
   358  		f, err := os.Create(*memprofile)
   359  		if err != nil {
   360  			log.Fatalf("%v", err)
   361  		}
   362  		AtExit(func() {
   363  			runtime.GC() // profile all outstanding allocations
   364  			if err := pprof.WriteHeapProfile(f); err != nil {
   365  				log.Fatalf("%v", err)
   366  			}
   367  		})
   368  	}
   369  }