github.com/aloncn/graphics-go@v0.0.1/src/cmd/link/internal/ld/pobj.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  	"cmd/internal/obj"
    35  	"flag"
    36  	"fmt"
    37  	"os"
    38  	"strings"
    39  )
    40  
    41  var (
    42  	pkglistfornote []byte
    43  	buildid        string
    44  )
    45  
    46  func Ldmain() {
    47  	Ctxt = linknew(Thelinkarch)
    48  	Ctxt.Thechar = int32(Thearch.Thechar)
    49  	Ctxt.Thestring = Thestring
    50  	Ctxt.Diag = Diag
    51  	Ctxt.Bso = &Bso
    52  
    53  	Bso = *obj.Binitw(os.Stdout)
    54  	Debug = [128]int{}
    55  	nerrors = 0
    56  	outfile = ""
    57  	HEADTYPE = -1
    58  	INITTEXT = -1
    59  	INITDAT = -1
    60  	INITRND = -1
    61  	INITENTRY = ""
    62  	Linkmode = LinkAuto
    63  
    64  	// For testing behavior of go command when tools crash silently.
    65  	// Undocumented, not in standard flag parser to avoid
    66  	// exposing in usage message.
    67  	for _, arg := range os.Args {
    68  		if arg == "-crash_for_testing" {
    69  			os.Exit(2)
    70  		}
    71  	}
    72  
    73  	if Thearch.Thechar == '6' && obj.Getgoos() == "plan9" {
    74  		obj.Flagcount("8", "use 64-bit addresses in symbol table", &Debug['8'])
    75  	}
    76  	obj.Flagfn1("B", "add an ELF NT_GNU_BUILD_ID `note` when using ELF", addbuildinfo)
    77  	obj.Flagcount("C", "check Go calls to C code", &Debug['C'])
    78  	obj.Flagint64("D", "set data segment `address`", &INITDAT)
    79  	obj.Flagstr("E", "set `entry` symbol name", &INITENTRY)
    80  	obj.Flagfn1("I", "use `linker` as ELF dynamic linker", setinterp)
    81  	obj.Flagfn1("L", "add specified `directory` to library path", Lflag)
    82  	obj.Flagfn1("H", "set header `type`", setheadtype)
    83  	obj.Flagint32("R", "set address rounding `quantum`", &INITRND)
    84  	obj.Flagint64("T", "set text segment `address`", &INITTEXT)
    85  	obj.Flagfn0("V", "print version and exit", doversion)
    86  	obj.Flagfn1("X", "add string value `definition` of the form importpath.name=value", addstrdata1)
    87  	obj.Flagcount("a", "disassemble output", &Debug['a'])
    88  	obj.Flagstr("buildid", "record `id` as Go toolchain build id", &buildid)
    89  	flag.Var(&Buildmode, "buildmode", "set build `mode`")
    90  	obj.Flagcount("c", "dump call graph", &Debug['c'])
    91  	obj.Flagcount("d", "disable dynamic executable", &Debug['d'])
    92  	obj.Flagstr("extar", "archive program for buildmode=c-archive", &extar)
    93  	obj.Flagstr("extld", "use `linker` when linking in external mode", &extld)
    94  	obj.Flagstr("extldflags", "pass `flags` to external linker", &extldflags)
    95  	obj.Flagcount("f", "ignore version mismatch", &Debug['f'])
    96  	obj.Flagcount("g", "disable go package data checks", &Debug['g'])
    97  	obj.Flagcount("h", "halt on error", &Debug['h'])
    98  	obj.Flagstr("installsuffix", "set package directory `suffix`", &flag_installsuffix)
    99  	obj.Flagstr("k", "set field tracking `symbol`", &tracksym)
   100  	obj.Flagstr("libgcc", "compiler support lib for internal linking; use \"none\" to disable", &libgccfile)
   101  	obj.Flagfn1("linkmode", "set link `mode` (internal, external, auto)", setlinkmode)
   102  	flag.BoolVar(&Linkshared, "linkshared", false, "link against installed Go shared libraries")
   103  	obj.Flagcount("msan", "enable MSan interface", &flag_msan)
   104  	obj.Flagcount("n", "dump symbol table", &Debug['n'])
   105  	obj.Flagstr("o", "write output to `file`", &outfile)
   106  	flag.Var(&rpath, "r", "set the ELF dynamic linker search `path` to dir1:dir2:...")
   107  	obj.Flagcount("race", "enable race detector", &flag_race)
   108  	obj.Flagcount("s", "disable symbol table", &Debug['s'])
   109  	var flagShared int
   110  	if Thearch.Thechar == '5' || Thearch.Thechar == '6' {
   111  		obj.Flagcount("shared", "generate shared object (implies -linkmode external)", &flagShared)
   112  	}
   113  	obj.Flagstr("tmpdir", "use `directory` for temporary files", &tmpdir)
   114  	obj.Flagcount("u", "reject unsafe packages", &Debug['u'])
   115  	obj.Flagcount("v", "print link trace", &Debug['v'])
   116  	obj.Flagcount("w", "disable DWARF generation", &Debug['w'])
   117  
   118  	obj.Flagstr("cpuprofile", "write cpu profile to `file`", &cpuprofile)
   119  	obj.Flagstr("memprofile", "write memory profile to `file`", &memprofile)
   120  	obj.Flagint64("memprofilerate", "set runtime.MemProfileRate to `rate`", &memprofilerate)
   121  
   122  	// Clumsy hack to preserve old two-argument -X name val syntax for old scripts.
   123  	// Rewrite that syntax into new syntax -X name=val.
   124  	// TODO(rsc): Delete this hack in Go 1.6 or later.
   125  	var args []string
   126  	for i := 0; i < len(os.Args); i++ {
   127  		arg := os.Args[i]
   128  		if (arg == "-X" || arg == "--X") && i+2 < len(os.Args) && !strings.Contains(os.Args[i+1], "=") {
   129  			fmt.Fprintf(os.Stderr, "link: warning: option %s %s %s may not work in future releases; use %s %s=%s\n",
   130  				arg, os.Args[i+1], os.Args[i+2],
   131  				arg, os.Args[i+1], os.Args[i+2])
   132  			args = append(args, arg)
   133  			args = append(args, os.Args[i+1]+"="+os.Args[i+2])
   134  			i += 2
   135  			continue
   136  		}
   137  		if (strings.HasPrefix(arg, "-X=") || strings.HasPrefix(arg, "--X=")) && i+1 < len(os.Args) && strings.Count(arg, "=") == 1 {
   138  			fmt.Fprintf(os.Stderr, "link: warning: option %s %s may not work in future releases; use %s=%s\n",
   139  				arg, os.Args[i+1],
   140  				arg, os.Args[i+1])
   141  			args = append(args, arg+"="+os.Args[i+1])
   142  			i++
   143  			continue
   144  		}
   145  		args = append(args, arg)
   146  	}
   147  	os.Args = args
   148  
   149  	obj.Flagparse(usage)
   150  
   151  	startProfile()
   152  	Ctxt.Bso = &Bso
   153  	Ctxt.Debugvlog = int32(Debug['v'])
   154  	if flagShared != 0 {
   155  		if Buildmode == BuildmodeUnset {
   156  			Buildmode = BuildmodeCShared
   157  		} else if Buildmode != BuildmodeCShared {
   158  			Exitf("-shared and -buildmode=%s are incompatible", Buildmode.String())
   159  		}
   160  	}
   161  	if Buildmode == BuildmodeUnset {
   162  		Buildmode = BuildmodeExe
   163  	}
   164  
   165  	if Buildmode != BuildmodeShared && flag.NArg() != 1 {
   166  		usage()
   167  	}
   168  
   169  	if outfile == "" {
   170  		outfile = "a.out"
   171  		if HEADTYPE == obj.Hwindows {
   172  			outfile += ".exe"
   173  		}
   174  	}
   175  
   176  	libinit() // creates outfile
   177  
   178  	if HEADTYPE == -1 {
   179  		HEADTYPE = int32(headtype(goos))
   180  	}
   181  	Ctxt.Headtype = int(HEADTYPE)
   182  	if headstring == "" {
   183  		headstring = Headstr(int(HEADTYPE))
   184  	}
   185  
   186  	Thearch.Archinit()
   187  
   188  	if Linkshared && !Iself {
   189  		Exitf("-linkshared can only be used on elf systems")
   190  	}
   191  
   192  	if Debug['v'] != 0 {
   193  		fmt.Fprintf(&Bso, "HEADER = -H%d -T0x%x -D0x%x -R0x%x\n", HEADTYPE, uint64(INITTEXT), uint64(INITDAT), uint32(INITRND))
   194  	}
   195  	Bso.Flush()
   196  
   197  	if Buildmode == BuildmodeShared {
   198  		for i := 0; i < flag.NArg(); i++ {
   199  			arg := flag.Arg(i)
   200  			parts := strings.SplitN(arg, "=", 2)
   201  			var pkgpath, file string
   202  			if len(parts) == 1 {
   203  				pkgpath, file = "main", arg
   204  			} else {
   205  				pkgpath, file = parts[0], parts[1]
   206  			}
   207  			pkglistfornote = append(pkglistfornote, pkgpath...)
   208  			pkglistfornote = append(pkglistfornote, '\n')
   209  			addlibpath(Ctxt, "command line", "command line", file, pkgpath, "")
   210  		}
   211  	} else {
   212  		addlibpath(Ctxt, "command line", "command line", flag.Arg(0), "main", "")
   213  	}
   214  	loadlib()
   215  
   216  	if Thearch.Thechar == '5' {
   217  		// mark some functions that are only referenced after linker code editing
   218  		if Ctxt.Goarm == 5 {
   219  			mark(Linkrlookup(Ctxt, "_sfloat", 0))
   220  		}
   221  		mark(Linklookup(Ctxt, "runtime.read_tls_fallback", 0))
   222  	}
   223  
   224  	checkgo()
   225  	checkstrdata()
   226  	deadcode()
   227  	callgraph()
   228  
   229  	doelf()
   230  	if HEADTYPE == obj.Hdarwin {
   231  		domacho()
   232  	}
   233  	dostkcheck()
   234  	if HEADTYPE == obj.Hwindows {
   235  		dope()
   236  	}
   237  	addexport()
   238  	Thearch.Gentext() // trampolines, call stubs, etc.
   239  	textbuildid()
   240  	textaddress()
   241  	pclntab()
   242  	findfunctab()
   243  	symtab()
   244  	dodata()
   245  	address()
   246  	doweak()
   247  	reloc()
   248  	Thearch.Asmb()
   249  	undef()
   250  	hostlink()
   251  	archive()
   252  	if Debug['v'] != 0 {
   253  		fmt.Fprintf(&Bso, "%5.2f cpu time\n", obj.Cputime())
   254  		fmt.Fprintf(&Bso, "%d symbols\n", Ctxt.Nsymbol)
   255  		fmt.Fprintf(&Bso, "%d liveness data\n", liveness)
   256  	}
   257  
   258  	Bso.Flush()
   259  
   260  	errorexit()
   261  }