github.com/sean-/go@v0.0.0-20151219100004-97f854cd7bb6/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("extld", "use `linker` when linking in external mode", &extld) 93 obj.Flagstr("extldflags", "pass `flags` to external linker", &extldflags) 94 obj.Flagcount("f", "ignore version mismatch", &Debug['f']) 95 obj.Flagcount("g", "disable go package data checks", &Debug['g']) 96 obj.Flagcount("h", "halt on error", &Debug['h']) 97 obj.Flagstr("installsuffix", "set package directory `suffix`", &flag_installsuffix) 98 obj.Flagstr("k", "set field tracking `symbol`", &tracksym) 99 obj.Flagstr("libgcc", "compiler support lib for internal linking; use \"none\" to disable", &libgccfile) 100 obj.Flagfn1("linkmode", "set link `mode` (internal, external, auto)", setlinkmode) 101 flag.BoolVar(&Linkshared, "linkshared", false, "link against installed Go shared libraries") 102 obj.Flagcount("msan", "enable MSan interface", &flag_msan) 103 obj.Flagcount("n", "dump symbol table", &Debug['n']) 104 obj.Flagstr("o", "write output to `file`", &outfile) 105 flag.Var(&rpath, "r", "set the ELF dynamic linker search `path` to dir1:dir2:...") 106 obj.Flagcount("race", "enable race detector", &flag_race) 107 obj.Flagcount("s", "disable symbol table", &Debug['s']) 108 var flagShared int 109 if Thearch.Thechar == '5' || Thearch.Thechar == '6' { 110 obj.Flagcount("shared", "generate shared object (implies -linkmode external)", &flagShared) 111 } 112 obj.Flagstr("tmpdir", "use `directory` for temporary files", &tmpdir) 113 obj.Flagcount("u", "reject unsafe packages", &Debug['u']) 114 obj.Flagcount("v", "print link trace", &Debug['v']) 115 obj.Flagcount("w", "disable DWARF generation", &Debug['w']) 116 117 obj.Flagstr("cpuprofile", "write cpu profile to `file`", &cpuprofile) 118 obj.Flagstr("memprofile", "write memory profile to `file`", &memprofile) 119 obj.Flagint64("memprofilerate", "set runtime.MemProfileRate to `rate`", &memprofilerate) 120 121 // Clumsy hack to preserve old two-argument -X name val syntax for old scripts. 122 // Rewrite that syntax into new syntax -X name=val. 123 // TODO(rsc): Delete this hack in Go 1.6 or later. 124 var args []string 125 for i := 0; i < len(os.Args); i++ { 126 arg := os.Args[i] 127 if (arg == "-X" || arg == "--X") && i+2 < len(os.Args) && !strings.Contains(os.Args[i+1], "=") { 128 fmt.Fprintf(os.Stderr, "link: warning: option %s %s %s may not work in future releases; use %s %s=%s\n", 129 arg, os.Args[i+1], os.Args[i+2], 130 arg, os.Args[i+1], os.Args[i+2]) 131 args = append(args, arg) 132 args = append(args, os.Args[i+1]+"="+os.Args[i+2]) 133 i += 2 134 continue 135 } 136 if (strings.HasPrefix(arg, "-X=") || strings.HasPrefix(arg, "--X=")) && i+1 < len(os.Args) && strings.Count(arg, "=") == 1 { 137 fmt.Fprintf(os.Stderr, "link: warning: option %s %s may not work in future releases; use %s=%s\n", 138 arg, os.Args[i+1], 139 arg, os.Args[i+1]) 140 args = append(args, arg+"="+os.Args[i+1]) 141 i++ 142 continue 143 } 144 args = append(args, arg) 145 } 146 os.Args = args 147 148 obj.Flagparse(usage) 149 150 startProfile() 151 Ctxt.Bso = &Bso 152 Ctxt.Debugvlog = int32(Debug['v']) 153 if flagShared != 0 { 154 if Buildmode == BuildmodeUnset { 155 Buildmode = BuildmodeCShared 156 } else if Buildmode != BuildmodeCShared { 157 Exitf("-shared and -buildmode=%s are incompatible", Buildmode.String()) 158 } 159 } 160 if Buildmode == BuildmodeUnset { 161 Buildmode = BuildmodeExe 162 } 163 164 if Buildmode != BuildmodeShared && flag.NArg() != 1 { 165 usage() 166 } 167 168 if outfile == "" { 169 outfile = "a.out" 170 if HEADTYPE == obj.Hwindows { 171 outfile += ".exe" 172 } 173 } 174 175 libinit() // creates outfile 176 177 if HEADTYPE == -1 { 178 HEADTYPE = int32(headtype(goos)) 179 } 180 Ctxt.Headtype = int(HEADTYPE) 181 if headstring == "" { 182 headstring = Headstr(int(HEADTYPE)) 183 } 184 185 Thearch.Archinit() 186 187 if Linkshared && !Iself { 188 Exitf("-linkshared can only be used on elf systems") 189 } 190 191 if Debug['v'] != 0 { 192 fmt.Fprintf(&Bso, "HEADER = -H%d -T0x%x -D0x%x -R0x%x\n", HEADTYPE, uint64(INITTEXT), uint64(INITDAT), uint32(INITRND)) 193 } 194 Bso.Flush() 195 196 if Buildmode == BuildmodeShared { 197 for i := 0; i < flag.NArg(); i++ { 198 arg := flag.Arg(i) 199 parts := strings.SplitN(arg, "=", 2) 200 var pkgpath, file string 201 if len(parts) == 1 { 202 pkgpath, file = "main", arg 203 } else { 204 pkgpath, file = parts[0], parts[1] 205 } 206 pkglistfornote = append(pkglistfornote, pkgpath...) 207 pkglistfornote = append(pkglistfornote, '\n') 208 addlibpath(Ctxt, "command line", "command line", file, pkgpath, "") 209 } 210 } else { 211 addlibpath(Ctxt, "command line", "command line", flag.Arg(0), "main", "") 212 } 213 loadlib() 214 215 if Thearch.Thechar == '5' { 216 // mark some functions that are only referenced after linker code editing 217 if Ctxt.Goarm == 5 { 218 mark(Linkrlookup(Ctxt, "_sfloat", 0)) 219 } 220 mark(Linklookup(Ctxt, "runtime.read_tls_fallback", 0)) 221 } 222 223 checkgo() 224 checkstrdata() 225 deadcode() 226 callgraph() 227 228 doelf() 229 if HEADTYPE == obj.Hdarwin { 230 domacho() 231 } 232 dostkcheck() 233 if HEADTYPE == obj.Hwindows { 234 dope() 235 } 236 addexport() 237 Thearch.Gentext() // trampolines, call stubs, etc. 238 textbuildid() 239 textaddress() 240 pclntab() 241 findfunctab() 242 symtab() 243 dodata() 244 address() 245 doweak() 246 reloc() 247 Thearch.Asmb() 248 undef() 249 hostlink() 250 archive() 251 if Debug['v'] != 0 { 252 fmt.Fprintf(&Bso, "%5.2f cpu time\n", obj.Cputime()) 253 fmt.Fprintf(&Bso, "%d symbols\n", Ctxt.Nsymbol) 254 fmt.Fprintf(&Bso, "%d liveness data\n", liveness) 255 } 256 257 Bso.Flush() 258 259 errorexit() 260 }