123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- package obj
- import (
- "github.com/twitchyliquid64/golang-asm/objabi"
- "log"
- "math"
- )
- func (s *LSym) Grow(lsiz int64) {
- siz := int(lsiz)
- if int64(siz) != lsiz {
- log.Fatalf("LSym.Grow size %d too long", lsiz)
- }
- if len(s.P) >= siz {
- return
- }
- s.P = append(s.P, make([]byte, siz-len(s.P))...)
- }
- func (s *LSym) GrowCap(c int64) {
- if int64(cap(s.P)) >= c {
- return
- }
- if s.P == nil {
- s.P = make([]byte, 0, c)
- return
- }
- b := make([]byte, len(s.P), c)
- copy(b, s.P)
- s.P = b
- }
- func (s *LSym) prepwrite(ctxt *Link, off int64, siz int) {
- if off < 0 || siz < 0 || off >= 1<<30 {
- ctxt.Diag("prepwrite: bad off=%d siz=%d s=%v", off, siz, s)
- }
- switch s.Type {
- case objabi.Sxxx, objabi.SBSS:
- s.Type = objabi.SDATA
- case objabi.SNOPTRBSS:
- s.Type = objabi.SNOPTRDATA
- case objabi.STLSBSS:
- ctxt.Diag("cannot supply data for %v var %v", s.Type, s.Name)
- }
- l := off + int64(siz)
- s.Grow(l)
- if l > s.Size {
- s.Size = l
- }
- }
- func (s *LSym) WriteFloat32(ctxt *Link, off int64, f float32) {
- s.prepwrite(ctxt, off, 4)
- ctxt.Arch.ByteOrder.PutUint32(s.P[off:], math.Float32bits(f))
- }
- func (s *LSym) WriteFloat64(ctxt *Link, off int64, f float64) {
- s.prepwrite(ctxt, off, 8)
- ctxt.Arch.ByteOrder.PutUint64(s.P[off:], math.Float64bits(f))
- }
- func (s *LSym) WriteInt(ctxt *Link, off int64, siz int, i int64) {
- s.prepwrite(ctxt, off, siz)
- switch siz {
- default:
- ctxt.Diag("WriteInt: bad integer size: %d", siz)
- case 1:
- s.P[off] = byte(i)
- case 2:
- ctxt.Arch.ByteOrder.PutUint16(s.P[off:], uint16(i))
- case 4:
- ctxt.Arch.ByteOrder.PutUint32(s.P[off:], uint32(i))
- case 8:
- ctxt.Arch.ByteOrder.PutUint64(s.P[off:], uint64(i))
- }
- }
- func (s *LSym) writeAddr(ctxt *Link, off int64, siz int, rsym *LSym, roff int64, rtype objabi.RelocType) {
-
- if siz != ctxt.Arch.PtrSize && siz != 4 {
- ctxt.Diag("WriteAddr: bad address size %d in %s", siz, s.Name)
- }
- s.prepwrite(ctxt, off, siz)
- r := Addrel(s)
- r.Off = int32(off)
- if int64(r.Off) != off {
- ctxt.Diag("WriteAddr: off overflow %d in %s", off, s.Name)
- }
- r.Siz = uint8(siz)
- r.Sym = rsym
- r.Type = rtype
- r.Add = roff
- }
- func (s *LSym) WriteAddr(ctxt *Link, off int64, siz int, rsym *LSym, roff int64) {
- s.writeAddr(ctxt, off, siz, rsym, roff, objabi.R_ADDR)
- }
- func (s *LSym) WriteCURelativeAddr(ctxt *Link, off int64, rsym *LSym, roff int64) {
- s.writeAddr(ctxt, off, ctxt.Arch.PtrSize, rsym, roff, objabi.R_ADDRCUOFF)
- }
- func (s *LSym) WriteOff(ctxt *Link, off int64, rsym *LSym, roff int64) {
- s.prepwrite(ctxt, off, 4)
- r := Addrel(s)
- r.Off = int32(off)
- if int64(r.Off) != off {
- ctxt.Diag("WriteOff: off overflow %d in %s", off, s.Name)
- }
- r.Siz = 4
- r.Sym = rsym
- r.Type = objabi.R_ADDROFF
- r.Add = roff
- }
- func (s *LSym) WriteWeakOff(ctxt *Link, off int64, rsym *LSym, roff int64) {
- s.prepwrite(ctxt, off, 4)
- r := Addrel(s)
- r.Off = int32(off)
- if int64(r.Off) != off {
- ctxt.Diag("WriteOff: off overflow %d in %s", off, s.Name)
- }
- r.Siz = 4
- r.Sym = rsym
- r.Type = objabi.R_WEAKADDROFF
- r.Add = roff
- }
- func (s *LSym) WriteString(ctxt *Link, off int64, siz int, str string) {
- if siz < len(str) {
- ctxt.Diag("WriteString: bad string size: %d < %d", siz, len(str))
- }
- s.prepwrite(ctxt, off, siz)
- copy(s.P[off:off+int64(siz)], str)
- }
- func (s *LSym) WriteBytes(ctxt *Link, off int64, b []byte) int64 {
- s.prepwrite(ctxt, off, len(b))
- copy(s.P[off:], b)
- return off + int64(len(b))
- }
- func Addrel(s *LSym) *Reloc {
- if s.R == nil {
- s.R = make([]Reloc, 0, 4)
- }
- s.R = append(s.R, Reloc{})
- return &s.R[len(s.R)-1]
- }
|