Home | History | Annotate | Download | only in gc
      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 	"cmd/internal/objabi"
     12 	"cmd/internal/src"
     13 	"crypto/sha256"
     14 	"fmt"
     15 	"io"
     16 	"strconv"
     17 )
     18 
     19 // architecture-independent object file output
     20 const ArhdrSize = 60
     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 	bout, err := bio.Create(outfile)
     59 	if err != nil {
     60 		flusherrors()
     61 		fmt.Printf("can't create %s: %v\n", outfile, err)
     62 		errorexit()
     63 	}
     64 	defer bout.Close()
     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", objabi.GOOS, objabi.GOARCH, objabi.Version, objabi.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(bout)
     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 		return
    112 	}
    113 
    114 	if writearchive {
    115 		// start object file
    116 		arhdr = [ArhdrSize]byte{}
    117 		bout.Write(arhdr[:])
    118 		startobj = bout.Offset()
    119 		printheader()
    120 	}
    121 
    122 	if pragcgobuf != "" {
    123 		if writearchive {
    124 			// write empty export section; must be before cgo section
    125 			fmt.Fprintf(bout, "\n$$\n\n$$\n\n")
    126 		}
    127 
    128 		fmt.Fprintf(bout, "\n$$  // cgo\n")
    129 		fmt.Fprintf(bout, "%s\n$$\n\n", pragcgobuf)
    130 	}
    131 
    132 	fmt.Fprintf(bout, "\n!\n")
    133 
    134 	externs := len(externdcl)
    135 
    136 	dumpglobls()
    137 	addptabs()
    138 	addsignats(externdcl)
    139 	dumpsignats()
    140 	dumptabs()
    141 	dumpimportstrings()
    142 	dumpbasictypes()
    143 
    144 	// Dump extra globals.
    145 	tmp := externdcl
    146 
    147 	if externdcl != nil {
    148 		externdcl = externdcl[externs:]
    149 	}
    150 	dumpglobls()
    151 	externdcl = tmp
    152 
    153 	if zerosize > 0 {
    154 		zero := mappkg.Lookup("zero")
    155 		ggloblsym(zero.Linksym(), int32(zerosize), obj.DUPOK|obj.RODATA)
    156 	}
    157 
    158 	addGCLocals()
    159 
    160 	obj.WriteObjFile(Ctxt, bout.Writer)
    161 
    162 	if writearchive {
    163 		bout.Flush()
    164 		size := bout.Offset() - startobj
    165 		if size&1 != 0 {
    166 			bout.WriteByte(0)
    167 		}
    168 		bout.Seek(startobj-ArhdrSize, 0)
    169 		formathdr(arhdr[:], "_go_.o", size)
    170 		bout.Write(arhdr[:])
    171 	}
    172 }
    173 
    174 func addptabs() {
    175 	if !Ctxt.Flag_dynlink || localpkg.Name != "main" {
    176 		return
    177 	}
    178 	for _, exportn := range exportlist {
    179 		s := exportn.Sym
    180 		n := asNode(s.Def)
    181 		if n == nil {
    182 			continue
    183 		}
    184 		if n.Op != ONAME {
    185 			continue
    186 		}
    187 		if !exportname(s.Name) {
    188 			continue
    189 		}
    190 		if s.Pkg.Name != "main" {
    191 			continue
    192 		}
    193 		if n.Type.Etype == TFUNC && n.Class() == PFUNC {
    194 			// function
    195 			ptabs = append(ptabs, ptabEntry{s: s, t: asNode(s.Def).Type})
    196 		} else {
    197 			// variable
    198 			ptabs = append(ptabs, ptabEntry{s: s, t: types.NewPtr(asNode(s.Def).Type)})
    199 		}
    200 	}
    201 }
    202 
    203 func dumpGlobal(n *Node) {
    204 	if n.Type == nil {
    205 		Fatalf("external %v nil type\n", n)
    206 	}
    207 	if n.Class() == PFUNC {
    208 		return
    209 	}
    210 	if n.Sym.Pkg != localpkg {
    211 		return
    212 	}
    213 	dowidth(n.Type)
    214 	ggloblnod(n)
    215 }
    216 
    217 func dumpGlobalConst(n *Node) {
    218 	// only export typed constants
    219 	t := n.Type
    220 	if t == nil {
    221 		return
    222 	}
    223 	if n.Sym.Pkg != localpkg {
    224 		return
    225 	}
    226 	// only export integer constants for now
    227 	switch t.Etype {
    228 	case TINT8:
    229 	case TINT16:
    230 	case TINT32:
    231 	case TINT64:
    232 	case TINT:
    233 	case TUINT8:
    234 	case TUINT16:
    235 	case TUINT32:
    236 	case TUINT64:
    237 	case TUINT:
    238 	case TUINTPTR:
    239 		// ok
    240 	case TIDEAL:
    241 		if !Isconst(n, CTINT) {
    242 			return
    243 		}
    244 		x := n.Val().U.(*Mpint)
    245 		if x.Cmp(minintval[TINT]) < 0 || x.Cmp(maxintval[TINT]) > 0 {
    246 			return
    247 		}
    248 		// Ideal integers we export as int (if they fit).
    249 		t = types.Types[TINT]
    250 	default:
    251 		return
    252 	}
    253 	Ctxt.DwarfIntConst(myimportpath, n.Sym.Name, typesymname(t), n.Int64())
    254 }
    255 
    256 func dumpglobls() {
    257 	// add globals
    258 	for _, n := range externdcl {
    259 		switch n.Op {
    260 		case ONAME:
    261 			dumpGlobal(n)
    262 		case OLITERAL:
    263 			dumpGlobalConst(n)
    264 		}
    265 	}
    266 
    267 	obj.SortSlice(funcsyms, func(i, j int) bool {
    268 		return funcsyms[i].LinksymName() < funcsyms[j].LinksymName()
    269 	})
    270 	for _, s := range funcsyms {
    271 		sf := s.Pkg.Lookup(funcsymname(s)).Linksym()
    272 		dsymptr(sf, 0, s.Linksym(), 0)
    273 		ggloblsym(sf, int32(Widthptr), obj.DUPOK|obj.RODATA)
    274 	}
    275 
    276 	// Do not reprocess funcsyms on next dumpglobls call.
    277 	funcsyms = nil
    278 }
    279 
    280 // addGCLocals adds gcargs and gclocals symbols to Ctxt.Data.
    281 // It takes care not to add any duplicates.
    282 // Though the object file format handles duplicates efficiently,
    283 // storing only a single copy of the data,
    284 // failure to remove these duplicates adds a few percent to object file size.
    285 func addGCLocals() {
    286 	seen := make(map[string]bool)
    287 	for _, s := range Ctxt.Text {
    288 		if s.Func == nil {
    289 			continue
    290 		}
    291 		for _, gcsym := range []*obj.LSym{&s.Func.GCArgs, &s.Func.GCLocals} {
    292 			if seen[gcsym.Name] {
    293 				continue
    294 			}
    295 			Ctxt.Data = append(Ctxt.Data, gcsym)
    296 			seen[gcsym.Name] = true
    297 		}
    298 	}
    299 }
    300 
    301 func duintxx(s *obj.LSym, off int, v uint64, wid int) int {
    302 	if off&(wid-1) != 0 {
    303 		Fatalf("duintxxLSym: misaligned: v=%d wid=%d off=%d", v, wid, off)
    304 	}
    305 	s.WriteInt(Ctxt, int64(off), wid, int64(v))
    306 	return off + wid
    307 }
    308 
    309 func duint8(s *obj.LSym, off int, v uint8) int {
    310 	return duintxx(s, off, uint64(v), 1)
    311 }
    312 
    313 func duint16(s *obj.LSym, off int, v uint16) int {
    314 	return duintxx(s, off, uint64(v), 2)
    315 }
    316 
    317 func duint32(s *obj.LSym, off int, v uint32) int {
    318 	return duintxx(s, off, uint64(v), 4)
    319 }
    320 
    321 func duintptr(s *obj.LSym, off int, v uint64) int {
    322 	return duintxx(s, off, v, Widthptr)
    323 }
    324 
    325 func dbvec(s *obj.LSym, off int, bv bvec) int {
    326 	// Runtime reads the bitmaps as byte arrays. Oblige.
    327 	for j := 0; int32(j) < bv.n; j += 8 {
    328 		word := bv.b[j/32]
    329 		off = duint8(s, off, uint8(word>>(uint(j)%32)))
    330 	}
    331 	return off
    332 }
    333 
    334 func stringsym(pos src.XPos, s string) (data *obj.LSym) {
    335 	var symname string
    336 	if len(s) > 100 {
    337 		// Huge strings are hashed to avoid long names in object files.
    338 		// Indulge in some paranoia by writing the length of s, too,
    339 		// as protection against length extension attacks.
    340 		h := sha256.New()
    341 		io.WriteString(h, s)
    342 		symname = fmt.Sprintf(".gostring.%d.%x", len(s), h.Sum(nil))
    343 	} else {
    344 		// Small strings get named directly by their contents.
    345 		symname = strconv.Quote(s)
    346 	}
    347 
    348 	const prefix = "go.string."
    349 	symdataname := prefix + symname
    350 
    351 	symdata := Ctxt.Lookup(symdataname)
    352 
    353 	if !symdata.SeenGlobl() {
    354 		// string data
    355 		off := dsname(symdata, 0, s, pos, "string")
    356 		ggloblsym(symdata, int32(off), obj.DUPOK|obj.RODATA|obj.LOCAL)
    357 	}
    358 
    359 	return symdata
    360 }
    361 
    362 var slicebytes_gen int
    363 
    364 func slicebytes(nam *Node, s string, len int) {
    365 	slicebytes_gen++
    366 	symname := fmt.Sprintf(".gobytes.%d", slicebytes_gen)
    367 	sym := localpkg.Lookup(symname)
    368 	sym.Def = asTypesNode(newname(sym))
    369 
    370 	lsym := sym.Linksym()
    371 	off := dsname(lsym, 0, s, nam.Pos, "slice")
    372 	ggloblsym(lsym, int32(off), obj.NOPTR|obj.LOCAL)
    373 
    374 	if nam.Op != ONAME {
    375 		Fatalf("slicebytes %v", nam)
    376 	}
    377 	nsym := nam.Sym.Linksym()
    378 	off = int(nam.Xoffset)
    379 	off = dsymptr(nsym, off, lsym, 0)
    380 	off = duintptr(nsym, off, uint64(len))
    381 	duintptr(nsym, off, uint64(len))
    382 }
    383 
    384 func dsname(s *obj.LSym, off int, t string, pos src.XPos, what string) int {
    385 	// Objects that are too large will cause the data section to overflow right away,
    386 	// causing a cryptic error message by the linker. Check for oversize objects here
    387 	// and provide a useful error message instead.
    388 	if int64(len(t)) > 2e9 {
    389 		yyerrorl(pos, "%v with length %v is too big", what, len(t))
    390 		return 0
    391 	}
    392 
    393 	s.WriteString(Ctxt, int64(off), len(t), t)
    394 	return off + len(t)
    395 }
    396 
    397 func dsymptr(s *obj.LSym, off int, x *obj.LSym, xoff int) int {
    398 	off = int(Rnd(int64(off), int64(Widthptr)))
    399 	s.WriteAddr(Ctxt, int64(off), Widthptr, x, int64(xoff))
    400 	off += Widthptr
    401 	return off
    402 }
    403 
    404 func dsymptrOff(s *obj.LSym, off int, x *obj.LSym, xoff int) int {
    405 	s.WriteOff(Ctxt, int64(off), x, int64(xoff))
    406 	off += 4
    407 	return off
    408 }
    409 
    410 func dsymptrWeakOff(s *obj.LSym, off int, x *obj.LSym) int {
    411 	s.WriteWeakOff(Ctxt, int64(off), x, 0)
    412 	off += 4
    413 	return off
    414 }
    415 
    416 func gdata(nam *Node, nr *Node, wid int) {
    417 	if nam.Op != ONAME {
    418 		Fatalf("gdata nam op %v", nam.Op)
    419 	}
    420 	if nam.Sym == nil {
    421 		Fatalf("gdata nil nam sym")
    422 	}
    423 	s := nam.Sym.Linksym()
    424 
    425 	switch nr.Op {
    426 	case OLITERAL:
    427 		switch u := nr.Val().U.(type) {
    428 		case bool:
    429 			i := int64(obj.Bool2int(u))
    430 			s.WriteInt(Ctxt, nam.Xoffset, wid, i)
    431 
    432 		case *Mpint:
    433 			s.WriteInt(Ctxt, nam.Xoffset, wid, u.Int64())
    434 
    435 		case *Mpflt:
    436 			f := u.Float64()
    437 			switch nam.Type.Etype {
    438 			case TFLOAT32:
    439 				s.WriteFloat32(Ctxt, nam.Xoffset, float32(f))
    440 			case TFLOAT64:
    441 				s.WriteFloat64(Ctxt, nam.Xoffset, f)
    442 			}
    443 
    444 		case *Mpcplx:
    445 			r := u.Real.Float64()
    446 			i := u.Imag.Float64()
    447 			switch nam.Type.Etype {
    448 			case TCOMPLEX64:
    449 				s.WriteFloat32(Ctxt, nam.Xoffset, float32(r))
    450 				s.WriteFloat32(Ctxt, nam.Xoffset+4, float32(i))
    451 			case TCOMPLEX128:
    452 				s.WriteFloat64(Ctxt, nam.Xoffset, r)
    453 				s.WriteFloat64(Ctxt, nam.Xoffset+8, i)
    454 			}
    455 
    456 		case string:
    457 			symdata := stringsym(nam.Pos, u)
    458 			s.WriteAddr(Ctxt, nam.Xoffset, Widthptr, symdata, 0)
    459 			s.WriteInt(Ctxt, nam.Xoffset+int64(Widthptr), Widthptr, int64(len(u)))
    460 
    461 		default:
    462 			Fatalf("gdata unhandled OLITERAL %v", nr)
    463 		}
    464 
    465 	case OADDR:
    466 		if nr.Left.Op != ONAME {
    467 			Fatalf("gdata ADDR left op %v", nr.Left.Op)
    468 		}
    469 		to := nr.Left
    470 		s.WriteAddr(Ctxt, nam.Xoffset, wid, to.Sym.Linksym(), to.Xoffset)
    471 
    472 	case ONAME:
    473 		if nr.Class() != PFUNC {
    474 			Fatalf("gdata NAME not PFUNC %d", nr.Class())
    475 		}
    476 		s.WriteAddr(Ctxt, nam.Xoffset, wid, funcsym(nr.Sym).Linksym(), nr.Xoffset)
    477 
    478 	default:
    479 		Fatalf("gdata unhandled op %v %v\n", nr, nr.Op)
    480 	}
    481 }
    482