github.com/tidwall/go@v0.0.0-20170415222209-6694a6888b7d/src/cmd/compile/internal/gc/obj.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package gc 6 7 import ( 8 "cmd/compile/internal/types" 9 "cmd/internal/bio" 10 "cmd/internal/obj" 11 "crypto/sha256" 12 "fmt" 13 "io" 14 "strconv" 15 ) 16 17 // architecture-independent object file output 18 const ( 19 ArhdrSize = 60 20 ) 21 22 func formathdr(arhdr []byte, name string, size int64) { 23 copy(arhdr[:], fmt.Sprintf("%-16s%-12d%-6d%-6d%-8o%-10d`\n", name, 0, 0, 0, 0644, size)) 24 } 25 26 // These modes say which kind of object file to generate. 27 // The default use of the toolchain is to set both bits, 28 // generating a combined compiler+linker object, one that 29 // serves to describe the package to both the compiler and the linker. 30 // In fact the compiler and linker read nearly disjoint sections of 31 // that file, though, so in a distributed build setting it can be more 32 // efficient to split the output into two files, supplying the compiler 33 // object only to future compilations and the linker object only to 34 // future links. 35 // 36 // By default a combined object is written, but if -linkobj is specified 37 // on the command line then the default -o output is a compiler object 38 // and the -linkobj output is a linker object. 39 const ( 40 modeCompilerObj = 1 << iota 41 modeLinkerObj 42 ) 43 44 func dumpobj() { 45 if !dolinkobj { 46 dumpobj1(outfile, modeCompilerObj) 47 return 48 } 49 if linkobj == "" { 50 dumpobj1(outfile, modeCompilerObj|modeLinkerObj) 51 return 52 } 53 dumpobj1(outfile, modeCompilerObj) 54 dumpobj1(linkobj, modeLinkerObj) 55 } 56 57 func dumpobj1(outfile string, mode int) { 58 var err error 59 bout, err = bio.Create(outfile) 60 if err != nil { 61 flusherrors() 62 fmt.Printf("can't create %s: %v\n", outfile, err) 63 errorexit() 64 } 65 66 startobj := int64(0) 67 var arhdr [ArhdrSize]byte 68 if writearchive { 69 bout.WriteString("!<arch>\n") 70 arhdr = [ArhdrSize]byte{} 71 bout.Write(arhdr[:]) 72 startobj = bout.Offset() 73 } 74 75 printheader := func() { 76 fmt.Fprintf(bout, "go object %s %s %s %s\n", obj.GOOS, obj.GOARCH, obj.Version, obj.Expstring()) 77 if buildid != "" { 78 fmt.Fprintf(bout, "build id %q\n", buildid) 79 } 80 if localpkg.Name == "main" { 81 fmt.Fprintf(bout, "main\n") 82 } 83 if safemode { 84 fmt.Fprintf(bout, "safe\n") 85 } else { 86 fmt.Fprintf(bout, "----\n") // room for some other tool to write "safe" 87 } 88 fmt.Fprintf(bout, "\n") // header ends with blank line 89 } 90 91 printheader() 92 93 if mode&modeCompilerObj != 0 { 94 dumpexport() 95 } 96 97 if writearchive { 98 bout.Flush() 99 size := bout.Offset() - startobj 100 if size&1 != 0 { 101 bout.WriteByte(0) 102 } 103 bout.Seek(startobj-ArhdrSize, 0) 104 formathdr(arhdr[:], "__.PKGDEF", size) 105 bout.Write(arhdr[:]) 106 bout.Flush() 107 bout.Seek(startobj+size+(size&1), 0) 108 } 109 110 if mode&modeLinkerObj == 0 { 111 bout.Close() 112 return 113 } 114 115 if writearchive { 116 // start object file 117 arhdr = [ArhdrSize]byte{} 118 bout.Write(arhdr[:]) 119 startobj = bout.Offset() 120 printheader() 121 } 122 123 if pragcgobuf != "" { 124 if writearchive { 125 // write empty export section; must be before cgo section 126 fmt.Fprintf(bout, "\n$$\n\n$$\n\n") 127 } 128 129 fmt.Fprintf(bout, "\n$$ // cgo\n") 130 fmt.Fprintf(bout, "%s\n$$\n\n", pragcgobuf) 131 } 132 133 fmt.Fprintf(bout, "\n!\n") 134 135 externs := len(externdcl) 136 137 dumpglobls() 138 dumpptabs() 139 dumptypestructs() 140 141 // Dump extra globals. 142 tmp := externdcl 143 144 if externdcl != nil { 145 externdcl = externdcl[externs:] 146 } 147 dumpglobls() 148 externdcl = tmp 149 150 if zerosize > 0 { 151 zero := mappkg.Lookup("zero") 152 ggloblsym(zero, int32(zerosize), obj.DUPOK|obj.RODATA) 153 } 154 155 obj.WriteObjFile(Ctxt, bout.Writer) 156 157 if writearchive { 158 bout.Flush() 159 size := bout.Offset() - startobj 160 if size&1 != 0 { 161 bout.WriteByte(0) 162 } 163 bout.Seek(startobj-ArhdrSize, 0) 164 formathdr(arhdr[:], "_go_.o", size) 165 bout.Write(arhdr[:]) 166 } 167 168 bout.Close() 169 } 170 171 func dumpptabs() { 172 if !Ctxt.Flag_dynlink || localpkg.Name != "main" { 173 return 174 } 175 for _, exportn := range exportlist { 176 s := exportn.Sym 177 n := asNode(s.Def) 178 if n == nil { 179 continue 180 } 181 if n.Op != ONAME { 182 continue 183 } 184 if !exportname(s.Name) { 185 continue 186 } 187 if s.Pkg.Name != "main" { 188 continue 189 } 190 if n.Type.Etype == TFUNC && n.Class == PFUNC { 191 // function 192 ptabs = append(ptabs, ptabEntry{s: s, t: asNode(s.Def).Type}) 193 } else { 194 // variable 195 ptabs = append(ptabs, ptabEntry{s: s, t: types.NewPtr(asNode(s.Def).Type)}) 196 } 197 } 198 } 199 200 func dumpglobls() { 201 // add globals 202 for _, n := range externdcl { 203 if n.Op != ONAME { 204 continue 205 } 206 207 if n.Type == nil { 208 Fatalf("external %v nil type\n", n) 209 } 210 if n.Class == PFUNC { 211 continue 212 } 213 if n.Sym.Pkg != localpkg { 214 continue 215 } 216 dowidth(n.Type) 217 ggloblnod(n) 218 } 219 220 for _, s := range funcsyms { 221 sf := s.Pkg.Lookup(funcsymname(s)) 222 dsymptr(sf, 0, s, 0) 223 ggloblsym(sf, int32(Widthptr), obj.DUPOK|obj.RODATA) 224 } 225 226 // Do not reprocess funcsyms on next dumpglobls call. 227 funcsyms = nil 228 } 229 230 func linksymname(s *types.Sym) string { 231 if isblanksym(s) { 232 return "_" 233 } 234 if s.Linkname != "" { 235 return s.Linkname 236 } 237 return s.Pkg.Prefix + "." + s.Name 238 } 239 240 func Linksym(s *types.Sym) *obj.LSym { 241 if s == nil { 242 return nil 243 } 244 if s.Lsym == nil { 245 s.Lsym = Ctxt.Lookup(linksymname(s), 0) 246 } 247 return s.Lsym 248 } 249 250 func duintxx(s *types.Sym, off int, v uint64, wid int) int { 251 return duintxxLSym(Linksym(s), off, v, wid) 252 } 253 254 func duintxxLSym(s *obj.LSym, off int, v uint64, wid int) int { 255 // Update symbol data directly instead of generating a 256 // DATA instruction that liblink will have to interpret later. 257 // This reduces compilation time and memory usage. 258 off = int(Rnd(int64(off), int64(wid))) 259 260 return int(obj.Setuintxx(Ctxt, s, int64(off), v, int64(wid))) 261 } 262 263 func duint8(s *types.Sym, off int, v uint8) int { 264 return duintxx(s, off, uint64(v), 1) 265 } 266 267 func duint16(s *types.Sym, off int, v uint16) int { 268 return duintxx(s, off, uint64(v), 2) 269 } 270 271 func duint32(s *types.Sym, off int, v uint32) int { 272 return duintxx(s, off, uint64(v), 4) 273 } 274 275 func duintptr(s *types.Sym, off int, v uint64) int { 276 return duintxx(s, off, v, Widthptr) 277 } 278 279 func dbvec(s *types.Sym, off int, bv bvec) int { 280 // Runtime reads the bitmaps as byte arrays. Oblige. 281 for j := 0; int32(j) < bv.n; j += 8 { 282 word := bv.b[j/32] 283 off = duint8(s, off, uint8(word>>(uint(j)%32))) 284 } 285 return off 286 } 287 288 func stringsym(s string) (data *obj.LSym) { 289 var symname string 290 if len(s) > 100 { 291 // Huge strings are hashed to avoid long names in object files. 292 // Indulge in some paranoia by writing the length of s, too, 293 // as protection against length extension attacks. 294 h := sha256.New() 295 io.WriteString(h, s) 296 symname = fmt.Sprintf(".gostring.%d.%x", len(s), h.Sum(nil)) 297 } else { 298 // Small strings get named directly by their contents. 299 symname = strconv.Quote(s) 300 } 301 302 const prefix = "go.string." 303 symdataname := prefix + symname 304 305 symdata := Ctxt.Lookup(symdataname, 0) 306 307 if !symdata.SeenGlobl() { 308 // string data 309 off := dsnameLSym(symdata, 0, s) 310 ggloblLSym(symdata, int32(off), obj.DUPOK|obj.RODATA|obj.LOCAL) 311 } 312 313 return symdata 314 } 315 316 var slicebytes_gen int 317 318 func slicebytes(nam *Node, s string, len int) { 319 slicebytes_gen++ 320 symname := fmt.Sprintf(".gobytes.%d", slicebytes_gen) 321 sym := localpkg.Lookup(symname) 322 sym.Def = asTypesNode(newname(sym)) 323 324 off := dsname(sym, 0, s) 325 ggloblsym(sym, int32(off), obj.NOPTR|obj.LOCAL) 326 327 if nam.Op != ONAME { 328 Fatalf("slicebytes %v", nam) 329 } 330 off = int(nam.Xoffset) 331 off = dsymptr(nam.Sym, off, sym, 0) 332 off = duintxx(nam.Sym, off, uint64(len), Widthint) 333 duintxx(nam.Sym, off, uint64(len), Widthint) 334 } 335 336 func dsname(s *types.Sym, off int, t string) int { 337 return dsnameLSym(Linksym(s), off, t) 338 } 339 340 func dsnameLSym(s *obj.LSym, off int, t string) int { 341 s.WriteString(Ctxt, int64(off), len(t), t) 342 return off + len(t) 343 } 344 345 func dsymptr(s *types.Sym, off int, x *types.Sym, xoff int) int { 346 return dsymptrLSym(Linksym(s), off, Linksym(x), xoff) 347 } 348 349 func dsymptrLSym(s *obj.LSym, off int, x *obj.LSym, xoff int) int { 350 off = int(Rnd(int64(off), int64(Widthptr))) 351 s.WriteAddr(Ctxt, int64(off), Widthptr, x, int64(xoff)) 352 off += Widthptr 353 return off 354 } 355 356 func dsymptrOffLSym(s *obj.LSym, off int, x *obj.LSym, xoff int) int { 357 s.WriteOff(Ctxt, int64(off), x, int64(xoff)) 358 off += 4 359 return off 360 } 361 362 func dsymptrWeakOffLSym(s *obj.LSym, off int, x *obj.LSym) int { 363 s.WriteWeakOff(Ctxt, int64(off), x, 0) 364 off += 4 365 return off 366 } 367 368 func gdata(nam *Node, nr *Node, wid int) { 369 if nam.Op != ONAME { 370 Fatalf("gdata nam op %v", nam.Op) 371 } 372 if nam.Sym == nil { 373 Fatalf("gdata nil nam sym") 374 } 375 s := Linksym(nam.Sym) 376 377 switch nr.Op { 378 case OLITERAL: 379 switch u := nr.Val().U.(type) { 380 case bool: 381 i := int64(obj.Bool2int(u)) 382 s.WriteInt(Ctxt, nam.Xoffset, wid, i) 383 384 case *Mpint: 385 s.WriteInt(Ctxt, nam.Xoffset, wid, u.Int64()) 386 387 case *Mpflt: 388 f := u.Float64() 389 switch nam.Type.Etype { 390 case TFLOAT32: 391 s.WriteFloat32(Ctxt, nam.Xoffset, float32(f)) 392 case TFLOAT64: 393 s.WriteFloat64(Ctxt, nam.Xoffset, f) 394 } 395 396 case *Mpcplx: 397 r := u.Real.Float64() 398 i := u.Imag.Float64() 399 switch nam.Type.Etype { 400 case TCOMPLEX64: 401 s.WriteFloat32(Ctxt, nam.Xoffset, float32(r)) 402 s.WriteFloat32(Ctxt, nam.Xoffset+4, float32(i)) 403 case TCOMPLEX128: 404 s.WriteFloat64(Ctxt, nam.Xoffset, r) 405 s.WriteFloat64(Ctxt, nam.Xoffset+8, i) 406 } 407 408 case string: 409 symdata := stringsym(u) 410 s.WriteAddr(Ctxt, nam.Xoffset, Widthptr, symdata, 0) 411 s.WriteInt(Ctxt, nam.Xoffset+int64(Widthptr), Widthint, int64(len(u))) 412 413 default: 414 Fatalf("gdata unhandled OLITERAL %v", nr) 415 } 416 417 case OADDR: 418 if nr.Left.Op != ONAME { 419 Fatalf("gdata ADDR left op %v", nr.Left.Op) 420 } 421 to := nr.Left 422 s.WriteAddr(Ctxt, nam.Xoffset, wid, Linksym(to.Sym), to.Xoffset) 423 424 case ONAME: 425 if nr.Class != PFUNC { 426 Fatalf("gdata NAME not PFUNC %d", nr.Class) 427 } 428 s.WriteAddr(Ctxt, nam.Xoffset, wid, Linksym(funcsym(nr.Sym)), nr.Xoffset) 429 430 default: 431 Fatalf("gdata unhandled op %v %v\n", nr, nr.Op) 432 } 433 }