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