Black Lives Matter. Support the Equal Justice Initiative.

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

Documentation: cmd/link/internal/ld

     1  // Inferno utils/6l/obj.c
     2  // https://bitbucket.org/inferno-os/inferno-os/src/master/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/goobj"
    36  	"cmd/internal/objabi"
    37  	"cmd/internal/sys"
    38  	"cmd/link/internal/benchmark"
    39  	"flag"
    40  	"log"
    41  	"os"
    42  	"runtime"
    43  	"runtime/pprof"
    44  	"strings"
    45  )
    46  
    47  var (
    48  	pkglistfornote []byte
    49  	windowsgui     bool // writes a "GUI binary" instead of a "console binary"
    50  	ownTmpDir      bool // set to true if tmp dir created by linker (e.g. no -tmpdir)
    51  )
    52  
    53  func init() {
    54  	flag.Var(&rpath, "r", "set the ELF dynamic linker search `path` to dir1:dir2:...")
    55  }
    56  
    57  // Flags used by the linker. The exported flags are used by the architecture-specific packages.
    58  var (
    59  	flagBuildid = flag.String("buildid", "", "record `id` as Go toolchain build id")
    60  
    61  	flagOutfile    = flag.String("o", "", "write output to `file`")
    62  	flagPluginPath = flag.String("pluginpath", "", "full path name for plugin")
    63  
    64  	flagInstallSuffix = flag.String("installsuffix", "", "set package directory `suffix`")
    65  	flagDumpDep       = flag.Bool("dumpdep", false, "dump symbol dependency graph")
    66  	flagRace          = flag.Bool("race", false, "enable race detector")
    67  	flagMsan          = flag.Bool("msan", false, "enable MSan interface")
    68  	flagAslr          = flag.Bool("aslr", true, "enable ASLR for buildmode=c-shared on windows")
    69  
    70  	flagFieldTrack = flag.String("k", "", "set field tracking `symbol`")
    71  	flagLibGCC     = flag.String("libgcc", "", "compiler support lib for internal linking; use \"none\" to disable")
    72  	flagTmpdir     = flag.String("tmpdir", "", "use `directory` for temporary files")
    73  
    74  	flagExtld      = flag.String("extld", "", "use `linker` when linking in external mode")
    75  	flagExtldflags = flag.String("extldflags", "", "pass `flags` to external linker")
    76  	flagExtar      = flag.String("extar", "", "archive program for buildmode=c-archive")
    77  
    78  	flagA             = flag.Bool("a", false, "no-op (deprecated)")
    79  	FlagC             = flag.Bool("c", false, "dump call graph")
    80  	FlagD             = flag.Bool("d", false, "disable dynamic executable")
    81  	flagF             = flag.Bool("f", false, "ignore version mismatch")
    82  	flagG             = flag.Bool("g", false, "disable go package data checks")
    83  	flagH             = flag.Bool("h", false, "halt on error")
    84  	flagN             = flag.Bool("n", false, "dump symbol table")
    85  	FlagS             = flag.Bool("s", false, "disable symbol table")
    86  	FlagW             = flag.Bool("w", false, "disable DWARF generation")
    87  	flag8             bool // use 64-bit addresses in symbol table
    88  	flagInterpreter   = flag.String("I", "", "use `linker` as ELF dynamic linker")
    89  	FlagDebugTramp    = flag.Int("debugtramp", 0, "debug trampolines")
    90  	FlagDebugTextSize = flag.Int("debugppc64textsize", 0, "debug PPC64 text section max")
    91  	FlagStrictDups    = flag.Int("strictdups", 0, "sanity check duplicate symbol contents during object file reading (1=warn 2=err).")
    92  	FlagRound         = flag.Int("R", -1, "set address rounding `quantum`")
    93  	FlagTextAddr      = flag.Int64("T", -1, "set text segment `address`")
    94  	flagEntrySymbol   = flag.String("E", "", "set `entry` symbol name")
    95  
    96  	cpuprofile     = flag.String("cpuprofile", "", "write cpu profile to `file`")
    97  	memprofile     = flag.String("memprofile", "", "write memory profile to `file`")
    98  	memprofilerate = flag.Int64("memprofilerate", 0, "set runtime.MemProfileRate to `rate`")
    99  
   100  	benchmarkFlag     = flag.String("benchmark", "", "set to 'mem' or 'cpu' to enable phase benchmarking")
   101  	benchmarkFileFlag = flag.String("benchmarkprofile", "", "emit phase profiles to `base`_phase.{cpu,mem}prof")
   102  )
   103  
   104  // Main is the main entry point for the linker code.
   105  func Main(arch *sys.Arch, theArch Arch) {
   106  	thearch = theArch
   107  	ctxt := linknew(arch)
   108  	ctxt.Bso = bufio.NewWriter(os.Stdout)
   109  
   110  	// For testing behavior of go command when tools crash silently.
   111  	// Undocumented, not in standard flag parser to avoid
   112  	// exposing in usage message.
   113  	for _, arg := range os.Args {
   114  		if arg == "-crash_for_testing" {
   115  			os.Exit(2)
   116  		}
   117  	}
   118  
   119  	final := gorootFinal()
   120  	addstrdata1(ctxt, "runtime/internal/sys.DefaultGoroot="+final)
   121  	addstrdata1(ctxt, "cmd/internal/objabi.defaultGOROOT="+final)
   122  
   123  	// TODO(matloob): define these above and then check flag values here
   124  	if ctxt.Arch.Family == sys.AMD64 && objabi.GOOS == "plan9" {
   125  		flag.BoolVar(&flag8, "8", false, "use 64-bit addresses in symbol table")
   126  	}
   127  	flagHeadType := flag.String("H", "", "set header `type`")
   128  	flag.BoolVar(&ctxt.linkShared, "linkshared", false, "link against installed Go shared libraries")
   129  	flag.Var(&ctxt.LinkMode, "linkmode", "set link `mode`")
   130  	flag.Var(&ctxt.BuildMode, "buildmode", "set build `mode`")
   131  	flag.BoolVar(&ctxt.compressDWARF, "compressdwarf", true, "compress DWARF if possible")
   132  	objabi.Flagfn1("B", "add an ELF NT_GNU_BUILD_ID `note` when using ELF", addbuildinfo)
   133  	objabi.Flagfn1("L", "add specified `directory` to library path", func(a string) { Lflag(ctxt, a) })
   134  	objabi.AddVersionFlag() // -V
   135  	objabi.Flagfn1("X", "add string value `definition` of the form importpath.name=value", func(s string) { addstrdata1(ctxt, s) })
   136  	objabi.Flagcount("v", "print link trace", &ctxt.Debugvlog)
   137  	objabi.Flagfn1("importcfg", "read import configuration from `file`", ctxt.readImportCfg)
   138  
   139  	objabi.Flagparse(usage)
   140  
   141  	if ctxt.Debugvlog > 0 {
   142  		// dump symbol info on crash
   143  		defer func() { ctxt.loader.Dump() }()
   144  	}
   145  
   146  	switch *flagHeadType {
   147  	case "":
   148  	case "windowsgui":
   149  		ctxt.HeadType = objabi.Hwindows
   150  		windowsgui = true
   151  	default:
   152  		if err := ctxt.HeadType.Set(*flagHeadType); err != nil {
   153  			Errorf(nil, "%v", err)
   154  			usage()
   155  		}
   156  	}
   157  	if ctxt.HeadType == objabi.Hunknown {
   158  		ctxt.HeadType.Set(objabi.GOOS)
   159  	}
   160  
   161  	if !*flagAslr && ctxt.BuildMode != BuildModeCShared {
   162  		Errorf(nil, "-aslr=false is only allowed for -buildmode=c-shared")
   163  		usage()
   164  	}
   165  
   166  	checkStrictDups = *FlagStrictDups
   167  
   168  	startProfile()
   169  	if ctxt.BuildMode == BuildModeUnset {
   170  		ctxt.BuildMode.Set("exe")
   171  	}
   172  
   173  	if ctxt.BuildMode != BuildModeShared && flag.NArg() != 1 {
   174  		usage()
   175  	}
   176  
   177  	if *flagOutfile == "" {
   178  		*flagOutfile = "a.out"
   179  		if ctxt.HeadType == objabi.Hwindows {
   180  			*flagOutfile += ".exe"
   181  		}
   182  	}
   183  
   184  	interpreter = *flagInterpreter
   185  
   186  	if *flagBuildid == "" && ctxt.Target.IsOpenbsd() {
   187  		// TODO(jsing): Remove once direct syscalls are no longer in use.
   188  		// OpenBSD 6.7 onwards will not permit direct syscalls from a
   189  		// dynamically linked binary unless it identifies the binary
   190  		// contains a .note.go.buildid ELF note. See issue #36435.
   191  		*flagBuildid = "go-openbsd"
   192  	}
   193  
   194  	// enable benchmarking
   195  	var bench *benchmark.Metrics
   196  	if len(*benchmarkFlag) != 0 {
   197  		if *benchmarkFlag == "mem" {
   198  			bench = benchmark.New(benchmark.GC, *benchmarkFileFlag)
   199  		} else if *benchmarkFlag == "cpu" {
   200  			bench = benchmark.New(benchmark.NoGC, *benchmarkFileFlag)
   201  		} else {
   202  			Errorf(nil, "unknown benchmark flag: %q", *benchmarkFlag)
   203  			usage()
   204  		}
   205  	}
   206  
   207  	bench.Start("libinit")
   208  	libinit(ctxt) // creates outfile
   209  	bench.Start("computeTLSOffset")
   210  	ctxt.computeTLSOffset()
   211  	bench.Start("Archinit")
   212  	thearch.Archinit(ctxt)
   213  
   214  	if ctxt.linkShared && !ctxt.IsELF {
   215  		Exitf("-linkshared can only be used on elf systems")
   216  	}
   217  
   218  	if ctxt.Debugvlog != 0 {
   219  		ctxt.Logf("HEADER = -H%d -T0x%x -R0x%x\n", ctxt.HeadType, uint64(*FlagTextAddr), uint32(*FlagRound))
   220  	}
   221  
   222  	zerofp := goobj.FingerprintType{}
   223  	switch ctxt.BuildMode {
   224  	case BuildModeShared:
   225  		for i := 0; i < flag.NArg(); i++ {
   226  			arg := flag.Arg(i)
   227  			parts := strings.SplitN(arg, "=", 2)
   228  			var pkgpath, file string
   229  			if len(parts) == 1 {
   230  				pkgpath, file = "main", arg
   231  			} else {
   232  				pkgpath, file = parts[0], parts[1]
   233  			}
   234  			pkglistfornote = append(pkglistfornote, pkgpath...)
   235  			pkglistfornote = append(pkglistfornote, '\n')
   236  			addlibpath(ctxt, "command line", "command line", file, pkgpath, "", zerofp)
   237  		}
   238  	case BuildModePlugin:
   239  		addlibpath(ctxt, "command line", "command line", flag.Arg(0), *flagPluginPath, "", zerofp)
   240  	default:
   241  		addlibpath(ctxt, "command line", "command line", flag.Arg(0), "main", "", zerofp)
   242  	}
   243  	bench.Start("loadlib")
   244  	ctxt.loadlib()
   245  
   246  	bench.Start("deadcode")
   247  	deadcode(ctxt)
   248  
   249  	bench.Start("linksetup")
   250  	ctxt.linksetup()
   251  
   252  	bench.Start("dostrdata")
   253  	ctxt.dostrdata()
   254  	if objabi.Fieldtrack_enabled != 0 {
   255  		bench.Start("fieldtrack")
   256  		fieldtrack(ctxt.Arch, ctxt.loader)
   257  	}
   258  
   259  	bench.Start("dwarfGenerateDebugInfo")
   260  	dwarfGenerateDebugInfo(ctxt)
   261  
   262  	bench.Start("callgraph")
   263  	ctxt.callgraph()
   264  
   265  	bench.Start("dostkcheck")
   266  	ctxt.dostkcheck()
   267  
   268  	bench.Start("mangleTypeSym")
   269  	ctxt.mangleTypeSym()
   270  
   271  	if ctxt.IsELF {
   272  		bench.Start("doelf")
   273  		ctxt.doelf()
   274  	}
   275  	if ctxt.IsDarwin() {
   276  		bench.Start("domacho")
   277  		ctxt.domacho()
   278  	}
   279  	if ctxt.IsWindows() {
   280  		bench.Start("dope")
   281  		ctxt.dope()
   282  		bench.Start("windynrelocsyms")
   283  		ctxt.windynrelocsyms()
   284  	}
   285  	if ctxt.IsAIX() {
   286  		bench.Start("doxcoff")
   287  		ctxt.doxcoff()
   288  	}
   289  
   290  	bench.Start("textbuildid")
   291  	ctxt.textbuildid()
   292  	bench.Start("addexport")
   293  	setupdynexp(ctxt)
   294  	ctxt.setArchSyms()
   295  	ctxt.addexport()
   296  	bench.Start("Gentext")
   297  	thearch.Gentext(ctxt, ctxt.loader) // trampolines, call stubs, etc.
   298  
   299  	bench.Start("textaddress")
   300  	ctxt.textaddress()
   301  	bench.Start("typelink")
   302  	ctxt.typelink()
   303  	bench.Start("buildinfo")
   304  	ctxt.buildinfo()
   305  	bench.Start("pclntab")
   306  	containers := ctxt.findContainerSyms()
   307  	pclnState := ctxt.pclntab(containers)
   308  	bench.Start("findfunctab")
   309  	ctxt.findfunctab(pclnState, containers)
   310  	bench.Start("dwarfGenerateDebugSyms")
   311  	dwarfGenerateDebugSyms(ctxt)
   312  	bench.Start("symtab")
   313  	symGroupType := ctxt.symtab(pclnState)
   314  	bench.Start("dodata")
   315  	ctxt.dodata(symGroupType)
   316  	bench.Start("address")
   317  	order := ctxt.address()
   318  	bench.Start("dwarfcompress")
   319  	dwarfcompress(ctxt)
   320  	bench.Start("layout")
   321  	filesize := ctxt.layout(order)
   322  
   323  	// Write out the output file.
   324  	// It is split into two parts (Asmb and Asmb2). The first
   325  	// part writes most of the content (sections and segments),
   326  	// for which we have computed the size and offset, in a
   327  	// mmap'd region. The second part writes more content, for
   328  	// which we don't know the size.
   329  	if ctxt.Arch.Family != sys.Wasm {
   330  		// Don't mmap if we're building for Wasm. Wasm file
   331  		// layout is very different so filesize is meaningless.
   332  		if err := ctxt.Out.Mmap(filesize); err != nil {
   333  			panic(err)
   334  		}
   335  	}
   336  	// asmb will redirect symbols to the output file mmap, and relocations
   337  	// will be applied directly there.
   338  	bench.Start("Asmb")
   339  	asmb(ctxt)
   340  
   341  	exitIfErrors()
   342  
   343  	// Generate additional symbols for the native symbol table just prior
   344  	// to code generation.
   345  	bench.Start("GenSymsLate")
   346  	if thearch.GenSymsLate != nil {
   347  		thearch.GenSymsLate(ctxt, ctxt.loader)
   348  	}
   349  
   350  	bench.Start("Asmb2")
   351  	asmb2(ctxt)
   352  
   353  	bench.Start("Munmap")
   354  	ctxt.Out.Close() // Close handles Munmapping if necessary.
   355  
   356  	bench.Start("hostlink")
   357  	ctxt.hostlink()
   358  	if ctxt.Debugvlog != 0 {
   359  		ctxt.Logf("%s", ctxt.loader.Stat())
   360  		ctxt.Logf("%d liveness data\n", liveness)
   361  	}
   362  	bench.Start("Flush")
   363  	ctxt.Bso.Flush()
   364  	bench.Start("archive")
   365  	ctxt.archive()
   366  	bench.Report(os.Stdout)
   367  
   368  	errorexit()
   369  }
   370  
   371  type Rpath struct {
   372  	set bool
   373  	val string
   374  }
   375  
   376  func (r *Rpath) Set(val string) error {
   377  	r.set = true
   378  	r.val = val
   379  	return nil
   380  }
   381  
   382  func (r *Rpath) String() string {
   383  	return r.val
   384  }
   385  
   386  func startProfile() {
   387  	if *cpuprofile != "" {
   388  		f, err := os.Create(*cpuprofile)
   389  		if err != nil {
   390  			log.Fatalf("%v", err)
   391  		}
   392  		if err := pprof.StartCPUProfile(f); err != nil {
   393  			log.Fatalf("%v", err)
   394  		}
   395  		AtExit(pprof.StopCPUProfile)
   396  	}
   397  	if *memprofile != "" {
   398  		if *memprofilerate != 0 {
   399  			runtime.MemProfileRate = int(*memprofilerate)
   400  		}
   401  		f, err := os.Create(*memprofile)
   402  		if err != nil {
   403  			log.Fatalf("%v", err)
   404  		}
   405  		AtExit(func() {
   406  			// Profile all outstanding allocations.
   407  			runtime.GC()
   408  			// compilebench parses the memory profile to extract memstats,
   409  			// which are only written in the legacy pprof format.
   410  			// See golang.org/issue/18641 and runtime/pprof/pprof.go:writeHeap.
   411  			const writeLegacyFormat = 1
   412  			if err := pprof.Lookup("heap").WriteTo(f, writeLegacyFormat); err != nil {
   413  				log.Fatalf("%v", err)
   414  			}
   415  		})
   416  	}
   417  }
   418  

View as plain text