]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/internal/obj/data.go
all: make copyright headers consistent with one space after period
[gostls13.git] / src / cmd / internal / obj / data.go
1 // Derived from Inferno utils/6l/obj.c and utils/6l/span.c
2 // http://code.google.com/p/inferno-os/source/browse/utils/6l/obj.c
3 // http://code.google.com/p/inferno-os/source/browse/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         "log"
36         "math"
37 )
38
39 // Grow increases the length of s.P to lsiz.
40 func (s *LSym) Grow(lsiz int64) {
41         siz := int(lsiz)
42         if int64(siz) != lsiz {
43                 log.Fatalf("LSym.Grow size %d too long", lsiz)
44         }
45         if len(s.P) >= siz {
46                 return
47         }
48         // TODO(dfc) append cap-len at once, rather than
49         // one byte at a time.
50         for cap(s.P) < siz {
51                 s.P = append(s.P[:cap(s.P)], 0)
52         }
53         s.P = s.P[:siz]
54 }
55
56 // GrowCap increases the capacity of s.P to c.
57 func (s *LSym) GrowCap(c int64) {
58         if int64(cap(s.P)) >= c {
59                 return
60         }
61         if s.P == nil {
62                 s.P = make([]byte, 0, c)
63                 return
64         }
65         b := make([]byte, len(s.P), c)
66         copy(b, s.P)
67         s.P = b
68 }
69
70 // prepwrite prepares to write data of size siz into s at offset off.
71 func (s *LSym) prepwrite(ctxt *Link, off int64, siz int) {
72         if off < 0 || siz < 0 || off >= 1<<30 {
73                 log.Fatalf("prepwrite: bad off=%d siz=%d", off, siz)
74         }
75         if s.Type == SBSS || s.Type == STLSBSS {
76                 ctxt.Diag("cannot supply data for BSS var")
77         }
78         l := off + int64(siz)
79         s.Grow(l)
80         if l > s.Size {
81                 s.Size = l
82         }
83 }
84
85 // WriteFloat32 writes f into s at offset off.
86 func (s *LSym) WriteFloat32(ctxt *Link, off int64, f float32) {
87         s.prepwrite(ctxt, off, 4)
88         ctxt.Arch.ByteOrder.PutUint32(s.P[off:], math.Float32bits(f))
89 }
90
91 // WriteFloat64 writes f into s at offset off.
92 func (s *LSym) WriteFloat64(ctxt *Link, off int64, f float64) {
93         s.prepwrite(ctxt, off, 8)
94         ctxt.Arch.ByteOrder.PutUint64(s.P[off:], math.Float64bits(f))
95 }
96
97 // WriteInt writes an integer i of size siz into s at offset off.
98 func (s *LSym) WriteInt(ctxt *Link, off int64, siz int, i int64) {
99         s.prepwrite(ctxt, off, siz)
100         switch siz {
101         default:
102                 ctxt.Diag("WriteInt: bad integer size: %d", siz)
103         case 1:
104                 s.P[off] = byte(i)
105         case 2:
106                 ctxt.Arch.ByteOrder.PutUint16(s.P[off:], uint16(i))
107         case 4:
108                 ctxt.Arch.ByteOrder.PutUint32(s.P[off:], uint32(i))
109         case 8:
110                 ctxt.Arch.ByteOrder.PutUint64(s.P[off:], uint64(i))
111         }
112 }
113
114 // WriteAddr writes an address of size siz into s at offset off.
115 // rsym and roff specify the relocation for the address.
116 func (s *LSym) WriteAddr(ctxt *Link, off int64, siz int, rsym *LSym, roff int64) {
117         if siz != ctxt.Arch.PtrSize {
118                 ctxt.Diag("WriteAddr: bad address size %d in %s", siz, s.Name)
119         }
120         s.prepwrite(ctxt, off, siz)
121         r := Addrel(s)
122         r.Off = int32(off)
123         if int64(r.Off) != off {
124                 ctxt.Diag("WriteAddr: off overflow %d in %s", off, s.Name)
125         }
126         r.Siz = uint8(siz)
127         r.Sym = rsym
128         r.Type = R_ADDR
129         r.Add = roff
130 }
131
132 // WriteOff writes a 4 byte offset to rsym+roff into s at offset off.
133 // After linking the 4 bytes stored at s+off will be
134 // rsym+roff-(start of section that s is in).
135 func (s *LSym) WriteOff(ctxt *Link, off int64, rsym *LSym, roff int64) {
136         s.prepwrite(ctxt, off, 4)
137         r := Addrel(s)
138         r.Off = int32(off)
139         if int64(r.Off) != off {
140                 ctxt.Diag("WriteOff: off overflow %d in %s", off, s.Name)
141         }
142         r.Siz = 4
143         r.Sym = rsym
144         r.Type = R_ADDROFF
145         r.Add = roff
146 }
147
148 // WriteString writes a string of size siz into s at offset off.
149 func (s *LSym) WriteString(ctxt *Link, off int64, siz int, str string) {
150         if siz < len(str) {
151                 ctxt.Diag("WriteString: bad string size: %d < %d", siz, len(str))
152         }
153         s.prepwrite(ctxt, off, siz)
154         copy(s.P[off:off+int64(siz)], str)
155 }
156
157 // WriteBytes writes a slice of bytes into s at offset off.
158 func (s *LSym) WriteBytes(ctxt *Link, off int64, b []byte) int64 {
159         s.prepwrite(ctxt, off, len(b))
160         copy(s.P[off:], b)
161         return off + int64(len(b))
162 }
163
164 func Addrel(s *LSym) *Reloc {
165         s.R = append(s.R, Reloc{})
166         return &s.R[len(s.R)-1]
167 }
168
169 func Setuintxx(ctxt *Link, s *LSym, off int64, v uint64, wid int64) int64 {
170         if s.Type == 0 {
171                 s.Type = SDATA
172         }
173         if s.Size < off+wid {
174                 s.Size = off + wid
175                 s.Grow(s.Size)
176         }
177
178         switch wid {
179         case 1:
180                 s.P[off] = uint8(v)
181         case 2:
182                 ctxt.Arch.ByteOrder.PutUint16(s.P[off:], uint16(v))
183         case 4:
184                 ctxt.Arch.ByteOrder.PutUint32(s.P[off:], uint32(v))
185         case 8:
186                 ctxt.Arch.ByteOrder.PutUint64(s.P[off:], v)
187         }
188
189         return off + wid
190 }