github.com/zebozhuang/go@v0.0.0-20200207033046-f8a98f6f5c5d/src/cmd/internal/obj/data.go (about) 1 // Derived from Inferno utils/6l/obj.c and utils/6l/span.c 2 // https://bitbucket.org/inferno-os/inferno-os/src/default/utils/6l/obj.c 3 // https://bitbucket.org/inferno-os/inferno-os/src/default/utils/6l/span.c 4 // 5 // Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved. 6 // Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net) 7 // Portions Copyright © 1997-1999 Vita Nuova Limited 8 // Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com) 9 // Portions Copyright © 2004,2006 Bruce Ellis 10 // Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net) 11 // Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others 12 // Portions Copyright © 2009 The Go Authors. All rights reserved. 13 // 14 // Permission is hereby granted, free of charge, to any person obtaining a copy 15 // of this software and associated documentation files (the "Software"), to deal 16 // in the Software without restriction, including without limitation the rights 17 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 18 // copies of the Software, and to permit persons to whom the Software is 19 // furnished to do so, subject to the following conditions: 20 // 21 // The above copyright notice and this permission notice shall be included in 22 // all copies or substantial portions of the Software. 23 // 24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 25 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 27 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 29 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 30 // THE SOFTWARE. 31 32 package obj 33 34 import ( 35 "cmd/internal/objabi" 36 "log" 37 "math" 38 ) 39 40 // Grow increases the length of s.P to lsiz. 41 func (s *LSym) Grow(lsiz int64) { 42 siz := int(lsiz) 43 if int64(siz) != lsiz { 44 log.Fatalf("LSym.Grow size %d too long", lsiz) 45 } 46 if len(s.P) >= siz { 47 return 48 } 49 // TODO(dfc) append cap-len at once, rather than 50 // one byte at a time. 51 for cap(s.P) < siz { 52 s.P = append(s.P[:cap(s.P)], 0) 53 } 54 s.P = s.P[:siz] 55 } 56 57 // GrowCap increases the capacity of s.P to c. 58 func (s *LSym) GrowCap(c int64) { 59 if int64(cap(s.P)) >= c { 60 return 61 } 62 if s.P == nil { 63 s.P = make([]byte, 0, c) 64 return 65 } 66 b := make([]byte, len(s.P), c) 67 copy(b, s.P) 68 s.P = b 69 } 70 71 // prepwrite prepares to write data of size siz into s at offset off. 72 func (s *LSym) prepwrite(ctxt *Link, off int64, siz int) { 73 if off < 0 || siz < 0 || off >= 1<<30 { 74 ctxt.Diag("prepwrite: bad off=%d siz=%d s=%v", off, siz, s) 75 } 76 switch s.Type { 77 case objabi.Sxxx, objabi.SBSS: 78 s.Type = objabi.SDATA 79 case objabi.SNOPTRBSS: 80 s.Type = objabi.SNOPTRDATA 81 case objabi.STLSBSS: 82 ctxt.Diag("cannot supply data for %v var %v", s.Type, s.Name) 83 } 84 l := off + int64(siz) 85 s.Grow(l) 86 if l > s.Size { 87 s.Size = l 88 } 89 } 90 91 // WriteFloat32 writes f into s at offset off. 92 func (s *LSym) WriteFloat32(ctxt *Link, off int64, f float32) { 93 s.prepwrite(ctxt, off, 4) 94 ctxt.Arch.ByteOrder.PutUint32(s.P[off:], math.Float32bits(f)) 95 } 96 97 // WriteFloat64 writes f into s at offset off. 98 func (s *LSym) WriteFloat64(ctxt *Link, off int64, f float64) { 99 s.prepwrite(ctxt, off, 8) 100 ctxt.Arch.ByteOrder.PutUint64(s.P[off:], math.Float64bits(f)) 101 } 102 103 // WriteInt writes an integer i of size siz into s at offset off. 104 func (s *LSym) WriteInt(ctxt *Link, off int64, siz int, i int64) { 105 s.prepwrite(ctxt, off, siz) 106 switch siz { 107 default: 108 ctxt.Diag("WriteInt: bad integer size: %d", siz) 109 case 1: 110 s.P[off] = byte(i) 111 case 2: 112 ctxt.Arch.ByteOrder.PutUint16(s.P[off:], uint16(i)) 113 case 4: 114 ctxt.Arch.ByteOrder.PutUint32(s.P[off:], uint32(i)) 115 case 8: 116 ctxt.Arch.ByteOrder.PutUint64(s.P[off:], uint64(i)) 117 } 118 } 119 120 // WriteAddr writes an address of size siz into s at offset off. 121 // rsym and roff specify the relocation for the address. 122 func (s *LSym) WriteAddr(ctxt *Link, off int64, siz int, rsym *LSym, roff int64) { 123 // Allow 4-byte addresses for DWARF. 124 if siz != ctxt.Arch.PtrSize && siz != 4 { 125 ctxt.Diag("WriteAddr: bad address size %d in %s", siz, s.Name) 126 } 127 s.prepwrite(ctxt, off, siz) 128 r := Addrel(s) 129 r.Off = int32(off) 130 if int64(r.Off) != off { 131 ctxt.Diag("WriteAddr: off overflow %d in %s", off, s.Name) 132 } 133 r.Siz = uint8(siz) 134 r.Sym = rsym 135 r.Type = objabi.R_ADDR 136 r.Add = roff 137 } 138 139 // WriteOff writes a 4 byte offset to rsym+roff into s at offset off. 140 // After linking the 4 bytes stored at s+off will be 141 // rsym+roff-(start of section that s is in). 142 func (s *LSym) WriteOff(ctxt *Link, off int64, rsym *LSym, roff int64) { 143 s.prepwrite(ctxt, off, 4) 144 r := Addrel(s) 145 r.Off = int32(off) 146 if int64(r.Off) != off { 147 ctxt.Diag("WriteOff: off overflow %d in %s", off, s.Name) 148 } 149 r.Siz = 4 150 r.Sym = rsym 151 r.Type = objabi.R_ADDROFF 152 r.Add = roff 153 } 154 155 // WriteWeakOff writes a weak 4 byte offset to rsym+roff into s at offset off. 156 // After linking the 4 bytes stored at s+off will be 157 // rsym+roff-(start of section that s is in). 158 func (s *LSym) WriteWeakOff(ctxt *Link, off int64, rsym *LSym, roff int64) { 159 s.prepwrite(ctxt, off, 4) 160 r := Addrel(s) 161 r.Off = int32(off) 162 if int64(r.Off) != off { 163 ctxt.Diag("WriteOff: off overflow %d in %s", off, s.Name) 164 } 165 r.Siz = 4 166 r.Sym = rsym 167 r.Type = objabi.R_WEAKADDROFF 168 r.Add = roff 169 } 170 171 // WriteString writes a string of size siz into s at offset off. 172 func (s *LSym) WriteString(ctxt *Link, off int64, siz int, str string) { 173 if siz < len(str) { 174 ctxt.Diag("WriteString: bad string size: %d < %d", siz, len(str)) 175 } 176 s.prepwrite(ctxt, off, siz) 177 copy(s.P[off:off+int64(siz)], str) 178 } 179 180 // WriteBytes writes a slice of bytes into s at offset off. 181 func (s *LSym) WriteBytes(ctxt *Link, off int64, b []byte) int64 { 182 s.prepwrite(ctxt, off, len(b)) 183 copy(s.P[off:], b) 184 return off + int64(len(b)) 185 } 186 187 func Addrel(s *LSym) *Reloc { 188 s.R = append(s.R, Reloc{}) 189 return &s.R[len(s.R)-1] 190 }