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