2015-02-13 14:40:36 -05:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package gc
|
|
|
|
|
|
|
|
|
|
import (
|
2016-04-06 21:45:29 -07:00
|
|
|
"cmd/internal/bio"
|
2015-02-13 14:40:36 -05:00
|
|
|
"cmd/internal/obj"
|
2016-03-10 11:14:22 -08:00
|
|
|
"crypto/sha256"
|
2015-02-13 14:40:36 -05:00
|
|
|
"fmt"
|
2016-03-10 11:14:22 -08:00
|
|
|
"io"
|
2015-03-12 18:45:30 -04:00
|
|
|
"strconv"
|
2015-02-13 14:40:36 -05:00
|
|
|
)
|
|
|
|
|
|
2015-10-22 09:51:12 +09:00
|
|
|
// architecture-independent object file output
|
2015-02-13 14:40:36 -05:00
|
|
|
const (
|
|
|
|
|
ArhdrSize = 60
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func formathdr(arhdr []byte, name string, size int64) {
|
|
|
|
|
copy(arhdr[:], fmt.Sprintf("%-16s%-12d%-6d%-6d%-8o%-10d`\n", name, 0, 0, 0, 0644, size))
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-26 21:50:59 -04:00
|
|
|
// These modes say which kind of object file to generate.
|
|
|
|
|
// The default use of the toolchain is to set both bits,
|
|
|
|
|
// generating a combined compiler+linker object, one that
|
|
|
|
|
// serves to describe the package to both the compiler and the linker.
|
|
|
|
|
// In fact the compiler and linker read nearly disjoint sections of
|
|
|
|
|
// that file, though, so in a distributed build setting it can be more
|
|
|
|
|
// efficient to split the output into two files, supplying the compiler
|
|
|
|
|
// object only to future compilations and the linker object only to
|
|
|
|
|
// future links.
|
|
|
|
|
//
|
|
|
|
|
// By default a combined object is written, but if -linkobj is specified
|
|
|
|
|
// on the command line then the default -o output is a compiler object
|
|
|
|
|
// and the -linkobj output is a linker object.
|
|
|
|
|
const (
|
|
|
|
|
modeCompilerObj = 1 << iota
|
|
|
|
|
modeLinkerObj
|
|
|
|
|
)
|
|
|
|
|
|
2015-02-13 14:40:36 -05:00
|
|
|
func dumpobj() {
|
2016-04-26 21:50:59 -04:00
|
|
|
if linkobj == "" {
|
|
|
|
|
dumpobj1(outfile, modeCompilerObj|modeLinkerObj)
|
|
|
|
|
} else {
|
|
|
|
|
dumpobj1(outfile, modeCompilerObj)
|
|
|
|
|
dumpobj1(linkobj, modeLinkerObj)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func dumpobj1(outfile string, mode int) {
|
2015-02-13 14:40:36 -05:00
|
|
|
var err error
|
2016-04-06 21:45:29 -07:00
|
|
|
bout, err = bio.Create(outfile)
|
2015-02-13 14:40:36 -05:00
|
|
|
if err != nil {
|
|
|
|
|
Flusherrors()
|
|
|
|
|
fmt.Printf("can't create %s: %v\n", outfile, err)
|
|
|
|
|
errorexit()
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-23 16:07:24 -05:00
|
|
|
startobj := int64(0)
|
|
|
|
|
var arhdr [ArhdrSize]byte
|
2016-04-13 18:37:18 -07:00
|
|
|
if writearchive {
|
2016-04-06 21:45:29 -07:00
|
|
|
bout.WriteString("!<arch>\n")
|
2015-02-13 14:40:36 -05:00
|
|
|
arhdr = [ArhdrSize]byte{}
|
2015-05-01 11:51:47 +10:00
|
|
|
bout.Write(arhdr[:])
|
2016-04-08 19:14:03 +10:00
|
|
|
startobj = bout.Offset()
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2016-04-26 21:50:59 -04:00
|
|
|
printheader := func() {
|
|
|
|
|
fmt.Fprintf(bout, "go object %s %s %s %s\n", obj.Getgoos(), obj.Getgoarch(), obj.Getgoversion(), obj.Expstring())
|
|
|
|
|
if buildid != "" {
|
|
|
|
|
fmt.Fprintf(bout, "build id %q\n", buildid)
|
|
|
|
|
}
|
|
|
|
|
if localpkg.Name == "main" {
|
|
|
|
|
fmt.Fprintf(bout, "main\n")
|
|
|
|
|
}
|
|
|
|
|
if safemode {
|
|
|
|
|
fmt.Fprintf(bout, "safe\n")
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Fprintf(bout, "----\n") // room for some other tool to write "safe"
|
|
|
|
|
}
|
|
|
|
|
fmt.Fprintf(bout, "\n") // header ends with blank line
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printheader()
|
|
|
|
|
|
|
|
|
|
if mode&modeCompilerObj != 0 {
|
|
|
|
|
dumpexport()
|
|
|
|
|
}
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2016-04-13 18:37:18 -07:00
|
|
|
if writearchive {
|
2015-05-01 11:51:47 +10:00
|
|
|
bout.Flush()
|
2016-04-08 19:14:03 +10:00
|
|
|
size := bout.Offset() - startobj
|
2015-02-13 14:40:36 -05:00
|
|
|
if size&1 != 0 {
|
2016-04-06 21:45:29 -07:00
|
|
|
bout.WriteByte(0)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2016-04-08 19:14:03 +10:00
|
|
|
bout.Seek(startobj-ArhdrSize, 0)
|
2015-02-13 14:40:36 -05:00
|
|
|
formathdr(arhdr[:], "__.PKGDEF", size)
|
2015-05-01 11:51:47 +10:00
|
|
|
bout.Write(arhdr[:])
|
|
|
|
|
bout.Flush()
|
2016-04-08 19:14:03 +10:00
|
|
|
bout.Seek(startobj+size+(size&1), 0)
|
2016-04-26 21:50:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if mode&modeLinkerObj == 0 {
|
|
|
|
|
bout.Close()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if writearchive {
|
|
|
|
|
// start object file
|
2015-02-13 14:40:36 -05:00
|
|
|
arhdr = [ArhdrSize]byte{}
|
2015-05-01 11:51:47 +10:00
|
|
|
bout.Write(arhdr[:])
|
2016-04-08 19:14:03 +10:00
|
|
|
startobj = bout.Offset()
|
2016-04-26 21:50:59 -04:00
|
|
|
printheader()
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if pragcgobuf != "" {
|
2016-04-13 18:37:18 -07:00
|
|
|
if writearchive {
|
2015-02-13 14:40:36 -05:00
|
|
|
// write empty export section; must be before cgo section
|
|
|
|
|
fmt.Fprintf(bout, "\n$$\n\n$$\n\n")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Fprintf(bout, "\n$$ // cgo\n")
|
|
|
|
|
fmt.Fprintf(bout, "%s\n$$\n\n", pragcgobuf)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Fprintf(bout, "\n!\n")
|
|
|
|
|
|
2015-09-10 15:57:39 +10:00
|
|
|
externs := len(externdcl)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
dumpglobls()
|
|
|
|
|
dumptypestructs()
|
|
|
|
|
|
|
|
|
|
// Dump extra globals.
|
2015-02-23 16:07:24 -05:00
|
|
|
tmp := externdcl
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-09-10 15:57:39 +10:00
|
|
|
if externdcl != nil {
|
|
|
|
|
externdcl = externdcl[externs:]
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
dumpglobls()
|
|
|
|
|
externdcl = tmp
|
|
|
|
|
|
2016-04-19 08:31:04 -07:00
|
|
|
if zerosize > 0 {
|
|
|
|
|
zero := Pkglookup("zero", mappkg)
|
|
|
|
|
ggloblsym(zero, int32(zerosize), obj.DUPOK|obj.RODATA)
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-13 14:40:36 -05:00
|
|
|
dumpdata()
|
2016-04-12 17:58:46 -07:00
|
|
|
obj.Writeobjdirect(Ctxt, bout.Writer)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2016-04-13 18:37:18 -07:00
|
|
|
if writearchive {
|
2015-05-01 11:51:47 +10:00
|
|
|
bout.Flush()
|
2016-04-08 19:14:03 +10:00
|
|
|
size := bout.Offset() - startobj
|
2015-02-13 14:40:36 -05:00
|
|
|
if size&1 != 0 {
|
2016-04-06 21:45:29 -07:00
|
|
|
bout.WriteByte(0)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2016-04-08 19:14:03 +10:00
|
|
|
bout.Seek(startobj-ArhdrSize, 0)
|
2015-05-21 13:28:17 -04:00
|
|
|
formathdr(arhdr[:], "_go_.o", size)
|
2015-05-01 11:51:47 +10:00
|
|
|
bout.Write(arhdr[:])
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2016-04-06 21:45:29 -07:00
|
|
|
bout.Close()
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func dumpglobls() {
|
|
|
|
|
// add globals
|
2015-09-10 15:57:39 +10:00
|
|
|
for _, n := range externdcl {
|
2015-02-13 14:40:36 -05:00
|
|
|
if n.Op != ONAME {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if n.Type == nil {
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("external %v nil type\n", n)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
if n.Class == PFUNC {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if n.Sym.Pkg != localpkg {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
dowidth(n.Type)
|
|
|
|
|
ggloblnod(n)
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-05 16:33:53 -07:00
|
|
|
for _, n := range funcsyms {
|
2015-03-25 19:33:01 -07:00
|
|
|
dsymptr(n.Sym, 0, n.Sym.Def.Func.Shortname.Sym, 0)
|
2015-02-13 14:40:36 -05:00
|
|
|
ggloblsym(n.Sym, int32(Widthptr), obj.DUPOK|obj.RODATA)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Do not reprocess funcsyms on next dumpglobls call.
|
|
|
|
|
funcsyms = nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Linksym(s *Sym) *obj.LSym {
|
|
|
|
|
if s == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
if s.Lsym != nil {
|
|
|
|
|
return s.Lsym
|
|
|
|
|
}
|
2015-03-12 18:45:30 -04:00
|
|
|
var name string
|
2015-02-13 14:40:36 -05:00
|
|
|
if isblanksym(s) {
|
2015-03-12 18:45:30 -04:00
|
|
|
name = "_"
|
2015-02-13 14:40:36 -05:00
|
|
|
} else if s.Linkname != "" {
|
2015-03-12 18:45:30 -04:00
|
|
|
name = s.Linkname
|
2015-02-13 14:40:36 -05:00
|
|
|
} else {
|
2015-03-12 18:45:30 -04:00
|
|
|
name = s.Pkg.Prefix + "." + s.Name
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2015-03-12 18:45:30 -04:00
|
|
|
ls := obj.Linklookup(Ctxt, name, 0)
|
|
|
|
|
s.Lsym = ls
|
|
|
|
|
return ls
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func duintxx(s *Sym, off int, v uint64, wid int) int {
|
2016-03-16 22:22:58 -07:00
|
|
|
return duintxxLSym(Linksym(s), off, v, wid)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func duintxxLSym(s *obj.LSym, off int, v uint64, wid int) int {
|
2015-02-13 14:40:36 -05:00
|
|
|
// Update symbol data directly instead of generating a
|
|
|
|
|
// DATA instruction that liblink will have to interpret later.
|
|
|
|
|
// This reduces compilation time and memory usage.
|
|
|
|
|
off = int(Rnd(int64(off), int64(wid)))
|
|
|
|
|
|
2016-03-16 22:22:58 -07:00
|
|
|
return int(obj.Setuintxx(Ctxt, s, int64(off), v, int64(wid)))
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func duint8(s *Sym, off int, v uint8) int {
|
|
|
|
|
return duintxx(s, off, uint64(v), 1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func duint16(s *Sym, off int, v uint16) int {
|
|
|
|
|
return duintxx(s, off, uint64(v), 2)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func duint32(s *Sym, off int, v uint32) int {
|
|
|
|
|
return duintxx(s, off, uint64(v), 4)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func duintptr(s *Sym, off int, v uint64) int {
|
|
|
|
|
return duintxx(s, off, v, Widthptr)
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-16 22:22:58 -07:00
|
|
|
// stringConstantSyms holds the pair of symbols we create for a
|
|
|
|
|
// constant string.
|
|
|
|
|
type stringConstantSyms struct {
|
|
|
|
|
hdr *obj.LSym // string header
|
|
|
|
|
data *obj.LSym // actual string data
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// stringConstants maps from the symbol name we use for the string
|
|
|
|
|
// contents to the pair of linker symbols for that string.
|
|
|
|
|
var stringConstants = make(map[string]stringConstantSyms, 100)
|
|
|
|
|
|
|
|
|
|
func stringsym(s string) (hdr, data *obj.LSym) {
|
2015-03-06 12:02:24 -08:00
|
|
|
var symname string
|
2015-02-13 14:40:36 -05:00
|
|
|
if len(s) > 100 {
|
2016-03-10 11:14:22 -08:00
|
|
|
// Huge strings are hashed to avoid long names in object files.
|
|
|
|
|
// Indulge in some paranoia by writing the length of s, too,
|
|
|
|
|
// as protection against length extension attacks.
|
|
|
|
|
h := sha256.New()
|
|
|
|
|
io.WriteString(h, s)
|
|
|
|
|
symname = fmt.Sprintf(".gostring.%d.%x", len(s), h.Sum(nil))
|
2015-02-13 14:40:36 -05:00
|
|
|
} else {
|
2016-03-10 11:14:22 -08:00
|
|
|
// Small strings get named directly by their contents.
|
2015-03-12 18:45:30 -04:00
|
|
|
symname = strconv.Quote(s)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-16 22:22:58 -07:00
|
|
|
const prefix = "go.string."
|
|
|
|
|
symdataname := prefix + symname
|
|
|
|
|
|
|
|
|
|
// All the strings have the same prefix, so ignore it for map
|
|
|
|
|
// purposes, but use a slice of the symbol name string to
|
|
|
|
|
// reduce long-term memory overhead.
|
|
|
|
|
key := symdataname[len(prefix):]
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2016-03-16 22:22:58 -07:00
|
|
|
if syms, ok := stringConstants[key]; ok {
|
|
|
|
|
return syms.hdr, syms.data
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
2016-03-16 22:22:58 -07:00
|
|
|
|
|
|
|
|
symhdrname := "go.string.hdr." + symname
|
|
|
|
|
|
|
|
|
|
symhdr := obj.Linklookup(Ctxt, symhdrname, 0)
|
|
|
|
|
symdata := obj.Linklookup(Ctxt, symdataname, 0)
|
|
|
|
|
|
|
|
|
|
stringConstants[key] = stringConstantSyms{symhdr, symdata}
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
// string header
|
cmd/compile: allow linker to drop string headers when not needed
Compiling a simple file containing a slice of 100,000 strings,
the size of the resulting binary dropped from 5,896,224 bytes
to 3,495,968 bytes, which is the expected 2,400,000 bytes,
give or take.
Fixes #7384.
Change-Id: I3e551b5a1395b523a41b33518d81a1bf28da0906
Reviewed-on: https://go-review.googlesource.com/11698
Reviewed-by: Austin Clements <austin@google.com>
2015-06-29 13:50:30 -04:00
|
|
|
off := 0
|
2016-03-16 22:22:58 -07:00
|
|
|
off = dsymptrLSym(symhdr, off, symdata, 0)
|
|
|
|
|
off = duintxxLSym(symhdr, off, uint64(len(s)), Widthint)
|
|
|
|
|
ggloblLSym(symhdr, int32(off), obj.DUPOK|obj.RODATA|obj.LOCAL)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
// string data
|
2016-03-16 22:22:58 -07:00
|
|
|
off = dsnameLSym(symdata, 0, s)
|
|
|
|
|
ggloblLSym(symdata, int32(off), obj.DUPOK|obj.RODATA|obj.LOCAL)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
cmd/compile: allow linker to drop string headers when not needed
Compiling a simple file containing a slice of 100,000 strings,
the size of the resulting binary dropped from 5,896,224 bytes
to 3,495,968 bytes, which is the expected 2,400,000 bytes,
give or take.
Fixes #7384.
Change-Id: I3e551b5a1395b523a41b33518d81a1bf28da0906
Reviewed-on: https://go-review.googlesource.com/11698
Reviewed-by: Austin Clements <austin@google.com>
2015-06-29 13:50:30 -04:00
|
|
|
return symhdr, symdata
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var slicebytes_gen int
|
|
|
|
|
|
|
|
|
|
func slicebytes(nam *Node, s string, len int) {
|
|
|
|
|
slicebytes_gen++
|
2015-03-06 12:02:24 -08:00
|
|
|
symname := fmt.Sprintf(".gobytes.%d", slicebytes_gen)
|
|
|
|
|
sym := Pkglookup(symname, localpkg)
|
2015-02-13 14:40:36 -05:00
|
|
|
sym.Def = newname(sym)
|
|
|
|
|
|
2016-03-11 18:54:37 -08:00
|
|
|
off := dsname(sym, 0, s)
|
2015-04-18 08:14:08 +12:00
|
|
|
ggloblsym(sym, int32(off), obj.NOPTR|obj.LOCAL)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
|
|
|
|
if nam.Op != ONAME {
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("slicebytes %v", nam)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
off = int(nam.Xoffset)
|
|
|
|
|
off = dsymptr(nam.Sym, off, sym, 0)
|
|
|
|
|
off = duintxx(nam.Sym, off, uint64(len), Widthint)
|
|
|
|
|
duintxx(nam.Sym, off, uint64(len), Widthint)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Datastring(s string, a *obj.Addr) {
|
cmd/compile: allow linker to drop string headers when not needed
Compiling a simple file containing a slice of 100,000 strings,
the size of the resulting binary dropped from 5,896,224 bytes
to 3,495,968 bytes, which is the expected 2,400,000 bytes,
give or take.
Fixes #7384.
Change-Id: I3e551b5a1395b523a41b33518d81a1bf28da0906
Reviewed-on: https://go-review.googlesource.com/11698
Reviewed-by: Austin Clements <austin@google.com>
2015-06-29 13:50:30 -04:00
|
|
|
_, symdata := stringsym(s)
|
2015-02-13 14:40:36 -05:00
|
|
|
a.Type = obj.TYPE_MEM
|
|
|
|
|
a.Name = obj.NAME_EXTERN
|
2016-03-16 22:22:58 -07:00
|
|
|
a.Sym = symdata
|
cmd/compile: allow linker to drop string headers when not needed
Compiling a simple file containing a slice of 100,000 strings,
the size of the resulting binary dropped from 5,896,224 bytes
to 3,495,968 bytes, which is the expected 2,400,000 bytes,
give or take.
Fixes #7384.
Change-Id: I3e551b5a1395b523a41b33518d81a1bf28da0906
Reviewed-on: https://go-review.googlesource.com/11698
Reviewed-by: Austin Clements <austin@google.com>
2015-06-29 13:50:30 -04:00
|
|
|
a.Offset = 0
|
2015-09-24 23:21:18 +02:00
|
|
|
a.Etype = uint8(Simtype[TINT])
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2015-03-02 16:03:26 -05:00
|
|
|
func datagostring(sval string, a *obj.Addr) {
|
cmd/compile: allow linker to drop string headers when not needed
Compiling a simple file containing a slice of 100,000 strings,
the size of the resulting binary dropped from 5,896,224 bytes
to 3,495,968 bytes, which is the expected 2,400,000 bytes,
give or take.
Fixes #7384.
Change-Id: I3e551b5a1395b523a41b33518d81a1bf28da0906
Reviewed-on: https://go-review.googlesource.com/11698
Reviewed-by: Austin Clements <austin@google.com>
2015-06-29 13:50:30 -04:00
|
|
|
symhdr, _ := stringsym(sval)
|
2015-02-13 14:40:36 -05:00
|
|
|
a.Type = obj.TYPE_MEM
|
|
|
|
|
a.Name = obj.NAME_EXTERN
|
2016-03-16 22:22:58 -07:00
|
|
|
a.Sym = symhdr
|
cmd/compile: allow linker to drop string headers when not needed
Compiling a simple file containing a slice of 100,000 strings,
the size of the resulting binary dropped from 5,896,224 bytes
to 3,495,968 bytes, which is the expected 2,400,000 bytes,
give or take.
Fixes #7384.
Change-Id: I3e551b5a1395b523a41b33518d81a1bf28da0906
Reviewed-on: https://go-review.googlesource.com/11698
Reviewed-by: Austin Clements <austin@google.com>
2015-06-29 13:50:30 -04:00
|
|
|
a.Offset = 0
|
2015-09-24 23:21:18 +02:00
|
|
|
a.Etype = uint8(TSTRING)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func dsname(s *Sym, off int, t string) int {
|
2016-03-16 22:22:58 -07:00
|
|
|
return dsnameLSym(Linksym(s), off, t)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func dsnameLSym(s *obj.LSym, off int, t string) int {
|
|
|
|
|
s.WriteString(Ctxt, int64(off), len(t), t)
|
2015-02-13 14:40:36 -05:00
|
|
|
return off + len(t)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func dsymptr(s *Sym, off int, x *Sym, xoff int) int {
|
2016-03-16 22:22:58 -07:00
|
|
|
return dsymptrLSym(Linksym(s), off, Linksym(x), xoff)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func dsymptrLSym(s *obj.LSym, off int, x *obj.LSym, xoff int) int {
|
2015-02-13 14:40:36 -05:00
|
|
|
off = int(Rnd(int64(off), int64(Widthptr)))
|
2016-03-16 22:22:58 -07:00
|
|
|
s.WriteAddr(Ctxt, int64(off), Widthptr, x, int64(xoff))
|
2015-02-13 14:40:36 -05:00
|
|
|
off += Widthptr
|
|
|
|
|
return off
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-27 10:21:48 -04:00
|
|
|
func dsymptrOffLSym(s *obj.LSym, off int, x *obj.LSym, xoff int) int {
|
|
|
|
|
s.WriteOff(Ctxt, int64(off), x, int64(xoff))
|
|
|
|
|
off += 4
|
|
|
|
|
return off
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-13 14:40:36 -05:00
|
|
|
func gdata(nam *Node, nr *Node, wid int) {
|
2016-03-13 12:16:43 -07:00
|
|
|
if nam.Op != ONAME {
|
2016-04-22 07:14:10 -07:00
|
|
|
Fatalf("gdata nam op %v", nam.Op)
|
2016-03-13 12:16:43 -07:00
|
|
|
}
|
|
|
|
|
if nam.Sym == nil {
|
|
|
|
|
Fatalf("gdata nil nam sym")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch nr.Op {
|
|
|
|
|
case OLITERAL:
|
2016-04-22 12:27:29 -07:00
|
|
|
switch u := nr.Val().U.(type) {
|
|
|
|
|
case *Mpcplx:
|
|
|
|
|
gdatacomplex(nam, u)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2016-04-22 12:27:29 -07:00
|
|
|
case string:
|
|
|
|
|
gdatastring(nam, u)
|
2016-03-13 12:16:43 -07:00
|
|
|
|
2016-04-22 12:27:29 -07:00
|
|
|
case bool:
|
|
|
|
|
i := int64(obj.Bool2int(u))
|
2016-03-13 19:28:32 -07:00
|
|
|
Linksym(nam.Sym).WriteInt(Ctxt, nam.Xoffset, wid, i)
|
2016-03-13 12:16:43 -07:00
|
|
|
|
2016-04-22 12:27:29 -07:00
|
|
|
case *Mpint:
|
|
|
|
|
Linksym(nam.Sym).WriteInt(Ctxt, nam.Xoffset, wid, u.Int64())
|
|
|
|
|
|
|
|
|
|
case *Mpflt:
|
2016-03-13 12:16:43 -07:00
|
|
|
s := Linksym(nam.Sym)
|
2016-04-22 12:27:29 -07:00
|
|
|
f := u.Float64()
|
2016-03-13 12:16:43 -07:00
|
|
|
switch nam.Type.Etype {
|
|
|
|
|
case TFLOAT32:
|
|
|
|
|
s.WriteFloat32(Ctxt, nam.Xoffset, float32(f))
|
|
|
|
|
case TFLOAT64:
|
|
|
|
|
s.WriteFloat64(Ctxt, nam.Xoffset, f)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
Fatalf("gdata unhandled OLITERAL %v", nr)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-13 12:16:43 -07:00
|
|
|
case OADDR:
|
|
|
|
|
if nr.Left.Op != ONAME {
|
2016-04-22 07:14:10 -07:00
|
|
|
Fatalf("gdata ADDR left op %s", nr.Left.Op)
|
2016-03-13 12:16:43 -07:00
|
|
|
}
|
|
|
|
|
to := nr.Left
|
2016-03-13 19:28:32 -07:00
|
|
|
Linksym(nam.Sym).WriteAddr(Ctxt, nam.Xoffset, wid, Linksym(to.Sym), to.Xoffset)
|
2016-03-13 12:16:43 -07:00
|
|
|
|
|
|
|
|
case ONAME:
|
|
|
|
|
if nr.Class != PFUNC {
|
|
|
|
|
Fatalf("gdata NAME not PFUNC %d", nr.Class)
|
|
|
|
|
}
|
2016-03-13 19:28:32 -07:00
|
|
|
Linksym(nam.Sym).WriteAddr(Ctxt, nam.Xoffset, wid, Linksym(funcsym(nr.Sym)), nr.Xoffset)
|
2016-03-13 12:16:43 -07:00
|
|
|
|
|
|
|
|
default:
|
2016-04-22 07:14:10 -07:00
|
|
|
Fatalf("gdata unhandled op %v %v\n", nr, nr.Op)
|
2016-03-13 12:16:43 -07:00
|
|
|
}
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func gdatacomplex(nam *Node, cval *Mpcplx) {
|
2016-03-13 12:16:43 -07:00
|
|
|
t := Types[cplxsubtype(nam.Type.Etype)]
|
2016-03-20 13:55:42 -07:00
|
|
|
r := cval.Real.Float64()
|
|
|
|
|
i := cval.Imag.Float64()
|
2016-03-13 12:16:43 -07:00
|
|
|
s := Linksym(nam.Sym)
|
|
|
|
|
|
|
|
|
|
switch t.Etype {
|
|
|
|
|
case TFLOAT32:
|
|
|
|
|
s.WriteFloat32(Ctxt, nam.Xoffset, float32(r))
|
|
|
|
|
s.WriteFloat32(Ctxt, nam.Xoffset+4, float32(i))
|
|
|
|
|
case TFLOAT64:
|
|
|
|
|
s.WriteFloat64(Ctxt, nam.Xoffset, r)
|
|
|
|
|
s.WriteFloat64(Ctxt, nam.Xoffset+8, i)
|
|
|
|
|
}
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2015-03-02 16:03:26 -05:00
|
|
|
func gdatastring(nam *Node, sval string) {
|
2016-03-13 12:16:43 -07:00
|
|
|
s := Linksym(nam.Sym)
|
|
|
|
|
_, symdata := stringsym(sval)
|
2016-03-16 22:22:58 -07:00
|
|
|
s.WriteAddr(Ctxt, nam.Xoffset, Widthptr, symdata, 0)
|
2016-03-13 19:28:32 -07:00
|
|
|
s.WriteInt(Ctxt, nam.Xoffset+int64(Widthptr), Widthint, int64(len(sval)))
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|