github.com/slayercat/go@v0.0.0-20170428012452-c51559813f61/src/cmd/link/internal/ld/main.go (about) 1 // Inferno utils/6l/obj.c 2 // https://bitbucket.org/inferno-os/inferno-os/src/default/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/objabi" 36 "cmd/internal/sys" 37 "flag" 38 "log" 39 "os" 40 "runtime" 41 "runtime/pprof" 42 "strings" 43 ) 44 45 var ( 46 pkglistfornote []byte 47 windowsgui bool // writes a "GUI binary" instead of a "console binary" 48 ) 49 50 func init() { 51 flag.Var(&Linkmode, "linkmode", "set link `mode`") 52 flag.Var(&Buildmode, "buildmode", "set build `mode`") 53 flag.Var(&rpath, "r", "set the ELF dynamic linker search `path` to dir1:dir2:...") 54 } 55 56 // Flags used by the linker. The exported flags are used by the architecture-specific packages. 57 var ( 58 flagBuildid = flag.String("buildid", "", "record `id` as Go toolchain build id") 59 60 flagOutfile = flag.String("o", "", "write output to `file`") 61 flagPluginPath = flag.String("pluginpath", "", "full path name for plugin") 62 FlagLinkshared = flag.Bool("linkshared", false, "link against installed Go shared libraries") 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 69 flagFieldTrack = flag.String("k", "", "set field tracking `symbol`") 70 flagLibGCC = flag.String("libgcc", "", "compiler support lib for internal linking; use \"none\" to disable") 71 flagTmpdir = flag.String("tmpdir", "", "use `directory` for temporary files") 72 73 flagExtld = flag.String("extld", "", "use `linker` when linking in external mode") 74 flagExtldflags = flag.String("extldflags", "", "pass `flags` to external linker") 75 flagExtar = flag.String("extar", "", "archive program for buildmode=c-archive") 76 77 flagA = flag.Bool("a", false, "disassemble output") 78 FlagC = flag.Bool("c", false, "dump call graph") 79 FlagD = flag.Bool("d", false, "disable dynamic executable") 80 flagF = flag.Bool("f", false, "ignore version mismatch") 81 flagG = flag.Bool("g", false, "disable go package data checks") 82 flagH = flag.Bool("h", false, "halt on error") 83 flagN = flag.Bool("n", false, "dump symbol table") 84 FlagS = flag.Bool("s", false, "disable symbol table") 85 flagU = flag.Bool("u", false, "reject unsafe packages") 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 91 flagHeadtype = flag.String("H", "", "set header `type`") 92 FlagRound = flag.Int("R", -1, "set address rounding `quantum`") 93 FlagTextAddr = flag.Int64("T", -1, "set text segment `address`") 94 FlagDataAddr = flag.Int64("D", -1, "set data segment `address`") 95 flagEntrySymbol = flag.String("E", "", "set `entry` symbol name") 96 97 cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`") 98 memprofile = flag.String("memprofile", "", "write memory profile to `file`") 99 memprofilerate = flag.Int64("memprofilerate", 0, "set runtime.MemProfileRate to `rate`") 100 ) 101 102 // Main is the main entry point for the linker code. 103 func Main() { 104 ctxt := linknew(SysArch) 105 ctxt.Bso = bufio.NewWriter(os.Stdout) 106 107 // For testing behavior of go command when tools crash silently. 108 // Undocumented, not in standard flag parser to avoid 109 // exposing in usage message. 110 for _, arg := range os.Args { 111 if arg == "-crash_for_testing" { 112 os.Exit(2) 113 } 114 } 115 116 // TODO(matloob): define these above and then check flag values here 117 if SysArch.Family == sys.AMD64 && objabi.GOOS == "plan9" { 118 flag.BoolVar(&Flag8, "8", false, "use 64-bit addresses in symbol table") 119 } 120 objabi.Flagfn1("B", "add an ELF NT_GNU_BUILD_ID `note` when using ELF", addbuildinfo) 121 objabi.Flagfn1("L", "add specified `directory` to library path", func(a string) { Lflag(ctxt, a) }) 122 objabi.Flagfn0("V", "print version and exit", doversion) 123 objabi.Flagfn1("X", "add string value `definition` of the form importpath.name=value", func(s string) { addstrdata1(ctxt, s) }) 124 objabi.Flagcount("v", "print link trace", &ctxt.Debugvlog) 125 126 objabi.Flagparse(usage) 127 128 switch *flagHeadtype { 129 case "": 130 case "windowsgui": 131 Headtype = objabi.Hwindows 132 windowsgui = true 133 default: 134 if err := Headtype.Set(*flagHeadtype); err != nil { 135 Errorf(nil, "%v", err) 136 usage() 137 } 138 } 139 140 startProfile() 141 if Buildmode == BuildmodeUnset { 142 Buildmode = BuildmodeExe 143 } 144 145 if Buildmode != BuildmodeShared && flag.NArg() != 1 { 146 usage() 147 } 148 149 if *flagOutfile == "" { 150 *flagOutfile = "a.out" 151 if Headtype == objabi.Hwindows { 152 *flagOutfile += ".exe" 153 } 154 } 155 156 interpreter = *flagInterpreter 157 158 libinit(ctxt) // creates outfile 159 160 if Headtype == objabi.Hunknown { 161 Headtype.Set(objabi.GOOS) 162 } 163 164 ctxt.computeTLSOffset() 165 Thearch.Archinit(ctxt) 166 167 if *FlagLinkshared && !Iself { 168 Exitf("-linkshared can only be used on elf systems") 169 } 170 171 if ctxt.Debugvlog != 0 { 172 ctxt.Logf("HEADER = -H%d -T0x%x -D0x%x -R0x%x\n", Headtype, uint64(*FlagTextAddr), uint64(*FlagDataAddr), uint32(*FlagRound)) 173 } 174 175 switch Buildmode { 176 case BuildmodeShared: 177 for i := 0; i < flag.NArg(); i++ { 178 arg := flag.Arg(i) 179 parts := strings.SplitN(arg, "=", 2) 180 var pkgpath, file string 181 if len(parts) == 1 { 182 pkgpath, file = "main", arg 183 } else { 184 pkgpath, file = parts[0], parts[1] 185 } 186 pkglistfornote = append(pkglistfornote, pkgpath...) 187 pkglistfornote = append(pkglistfornote, '\n') 188 addlibpath(ctxt, "command line", "command line", file, pkgpath, "") 189 } 190 case BuildmodePlugin: 191 addlibpath(ctxt, "command line", "command line", flag.Arg(0), *flagPluginPath, "") 192 default: 193 addlibpath(ctxt, "command line", "command line", flag.Arg(0), "main", "") 194 } 195 ctxt.loadlib() 196 197 ctxt.checkstrdata() 198 deadcode(ctxt) 199 fieldtrack(ctxt) 200 ctxt.callgraph() 201 202 ctxt.doelf() 203 if Headtype == objabi.Hdarwin { 204 ctxt.domacho() 205 } 206 ctxt.dostkcheck() 207 if Headtype == objabi.Hwindows { 208 ctxt.dope() 209 } 210 ctxt.addexport() 211 Thearch.Gentext(ctxt) // trampolines, call stubs, etc. 212 ctxt.textbuildid() 213 ctxt.textaddress() 214 ctxt.pclntab() 215 ctxt.findfunctab() 216 ctxt.typelink() 217 ctxt.symtab() 218 ctxt.dodata() 219 ctxt.address() 220 ctxt.reloc() 221 Thearch.Asmb(ctxt) 222 ctxt.undef() 223 ctxt.hostlink() 224 ctxt.archive() 225 if ctxt.Debugvlog != 0 { 226 ctxt.Logf("%5.2f cpu time\n", Cputime()) 227 ctxt.Logf("%d symbols\n", len(ctxt.Syms.Allsym)) 228 ctxt.Logf("%d liveness data\n", liveness) 229 } 230 231 ctxt.Bso.Flush() 232 233 errorexit() 234 } 235 236 type Rpath struct { 237 set bool 238 val string 239 } 240 241 func (r *Rpath) Set(val string) error { 242 r.set = true 243 r.val = val 244 return nil 245 } 246 247 func (r *Rpath) String() string { 248 return r.val 249 } 250 251 func startProfile() { 252 if *cpuprofile != "" { 253 f, err := os.Create(*cpuprofile) 254 if err != nil { 255 log.Fatalf("%v", err) 256 } 257 if err := pprof.StartCPUProfile(f); err != nil { 258 log.Fatalf("%v", err) 259 } 260 AtExit(pprof.StopCPUProfile) 261 } 262 if *memprofile != "" { 263 if *memprofilerate != 0 { 264 runtime.MemProfileRate = int(*memprofilerate) 265 } 266 f, err := os.Create(*memprofile) 267 if err != nil { 268 log.Fatalf("%v", err) 269 } 270 AtExit(func() { 271 runtime.GC() // profile all outstanding allocations 272 if err := pprof.WriteHeapProfile(f); err != nil { 273 log.Fatalf("%v", err) 274 } 275 }) 276 } 277 }