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