github.com/dannin/go@v0.0.0-20161031215817-d35dfd405eaa/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/obj" 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 ) 48 49 func init() { 50 flag.Var(&Linkmode, "linkmode", "set link `mode`") 51 flag.Var(&Buildmode, "buildmode", "set build `mode`") 52 flag.Var(&Headtype, "H", "set header `type`") 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 FlagRound = flag.Int("R", -1, "set address rounding `quantum`") 92 FlagTextAddr = flag.Int64("T", -1, "set text segment `address`") 93 FlagDataAddr = flag.Int64("D", -1, "set data segment `address`") 94 flagEntrySymbol = flag.String("E", "", "set `entry` symbol name") 95 96 cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`") 97 memprofile = flag.String("memprofile", "", "write memory profile to `file`") 98 memprofilerate = flag.Int64("memprofilerate", 0, "set runtime.MemProfileRate to `rate`") 99 ) 100 101 // Main is the main entry point for the linker code. 102 func Main() { 103 ctxt := linknew(SysArch) 104 ctxt.Bso = bufio.NewWriter(os.Stdout) 105 106 // For testing behavior of go command when tools crash silently. 107 // Undocumented, not in standard flag parser to avoid 108 // exposing in usage message. 109 for _, arg := range os.Args { 110 if arg == "-crash_for_testing" { 111 os.Exit(2) 112 } 113 } 114 115 // TODO(matloob): define these above and then check flag values here 116 if SysArch.Family == sys.AMD64 && obj.GOOS == "plan9" { 117 flag.BoolVar(&Flag8, "8", false, "use 64-bit addresses in symbol table") 118 } 119 obj.Flagfn1("B", "add an ELF NT_GNU_BUILD_ID `note` when using ELF", addbuildinfo) 120 obj.Flagfn1("L", "add specified `directory` to library path", func(a string) { Lflag(ctxt, a) }) 121 obj.Flagfn0("V", "print version and exit", doversion) 122 obj.Flagfn1("X", "add string value `definition` of the form importpath.name=value", func(s string) { addstrdata1(ctxt, s) }) 123 obj.Flagcount("v", "print link trace", &ctxt.Debugvlog) 124 125 obj.Flagparse(usage) 126 127 startProfile() 128 if Buildmode == BuildmodeUnset { 129 Buildmode = BuildmodeExe 130 } 131 132 if Buildmode != BuildmodeShared && flag.NArg() != 1 { 133 usage() 134 } 135 136 if *flagOutfile == "" { 137 *flagOutfile = "a.out" 138 if Headtype == obj.Hwindows || Headtype == obj.Hwindowsgui { 139 *flagOutfile += ".exe" 140 } 141 } 142 143 interpreter = *flagInterpreter 144 145 libinit(ctxt) // creates outfile 146 147 if Headtype == obj.Hunknown { 148 Headtype.Set(obj.GOOS) 149 } 150 151 ctxt.computeTLSOffset() 152 Thearch.Archinit(ctxt) 153 154 if *FlagLinkshared && !Iself { 155 Exitf("-linkshared can only be used on elf systems") 156 } 157 158 if ctxt.Debugvlog != 0 { 159 ctxt.Logf("HEADER = -H%d -T0x%x -D0x%x -R0x%x\n", Headtype, uint64(*FlagTextAddr), uint64(*FlagDataAddr), uint32(*FlagRound)) 160 } 161 162 switch Buildmode { 163 case BuildmodeShared: 164 for i := 0; i < flag.NArg(); i++ { 165 arg := flag.Arg(i) 166 parts := strings.SplitN(arg, "=", 2) 167 var pkgpath, file string 168 if len(parts) == 1 { 169 pkgpath, file = "main", arg 170 } else { 171 pkgpath, file = parts[0], parts[1] 172 } 173 pkglistfornote = append(pkglistfornote, pkgpath...) 174 pkglistfornote = append(pkglistfornote, '\n') 175 addlibpath(ctxt, "command line", "command line", file, pkgpath, "") 176 } 177 case BuildmodePlugin: 178 addlibpath(ctxt, "command line", "command line", flag.Arg(0), *flagPluginPath, "") 179 default: 180 addlibpath(ctxt, "command line", "command line", flag.Arg(0), "main", "") 181 } 182 ctxt.loadlib() 183 184 ctxt.checkstrdata() 185 deadcode(ctxt) 186 fieldtrack(ctxt) 187 ctxt.callgraph() 188 189 ctxt.doelf() 190 if Headtype == obj.Hdarwin { 191 ctxt.domacho() 192 } 193 ctxt.dostkcheck() 194 if Headtype == obj.Hwindows || Headtype == obj.Hwindowsgui { 195 ctxt.dope() 196 } 197 ctxt.addexport() 198 Thearch.Gentext(ctxt) // trampolines, call stubs, etc. 199 ctxt.textbuildid() 200 ctxt.textaddress() 201 ctxt.pclntab() 202 ctxt.findfunctab() 203 ctxt.typelink() 204 ctxt.symtab() 205 ctxt.dodata() 206 ctxt.address() 207 ctxt.reloc() 208 Thearch.Asmb(ctxt) 209 ctxt.undef() 210 ctxt.hostlink() 211 ctxt.archive() 212 if ctxt.Debugvlog != 0 { 213 ctxt.Logf("%5.2f cpu time\n", obj.Cputime()) 214 ctxt.Logf("%d symbols\n", len(ctxt.Syms.Allsym)) 215 ctxt.Logf("%d liveness data\n", liveness) 216 } 217 218 ctxt.Bso.Flush() 219 220 errorexit() 221 } 222 223 type Rpath struct { 224 set bool 225 val string 226 } 227 228 func (r *Rpath) Set(val string) error { 229 r.set = true 230 r.val = val 231 return nil 232 } 233 234 func (r *Rpath) String() string { 235 return r.val 236 } 237 238 func startProfile() { 239 if *cpuprofile != "" { 240 f, err := os.Create(*cpuprofile) 241 if err != nil { 242 log.Fatalf("%v", err) 243 } 244 if err := pprof.StartCPUProfile(f); err != nil { 245 log.Fatalf("%v", err) 246 } 247 AtExit(pprof.StopCPUProfile) 248 } 249 if *memprofile != "" { 250 if *memprofilerate != 0 { 251 runtime.MemProfileRate = int(*memprofilerate) 252 } 253 f, err := os.Create(*memprofile) 254 if err != nil { 255 log.Fatalf("%v", err) 256 } 257 AtExit(func() { 258 runtime.GC() // profile all outstanding allocations 259 if err := pprof.WriteHeapProfile(f); err != nil { 260 log.Fatalf("%v", err) 261 } 262 }) 263 } 264 }