github.com/bir3/gocompiler@v0.9.2202/src/cmd/link/internal/ld/main.go (about)

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