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