1
2
3
4
5 package loader
6
7 import (
8 "cmd/internal/goobj"
9 "cmd/internal/objabi"
10 "cmd/internal/sys"
11 "cmd/link/internal/sym"
12 "cmp"
13 "slices"
14 )
15
16
17
18 type SymbolBuilder struct {
19 *extSymPayload
20 symIdx Sym
21 l *Loader
22 }
23
24
25
26 func (l *Loader) MakeSymbolBuilder(name string) *SymbolBuilder {
27
28 symIdx := l.CreateStaticSym(name)
29 sb := &SymbolBuilder{l: l, symIdx: symIdx}
30 sb.extSymPayload = l.getPayload(symIdx)
31 return sb
32 }
33
34
35
36
37
38
39 func (l *Loader) MakeSymbolUpdater(symIdx Sym) *SymbolBuilder {
40 if symIdx == 0 {
41 panic("can't update the null symbol")
42 }
43 if !l.IsExternal(symIdx) {
44
45 l.cloneToExternal(symIdx)
46 }
47
48
49 sb := &SymbolBuilder{l: l, symIdx: symIdx}
50 sb.extSymPayload = l.getPayload(symIdx)
51 return sb
52 }
53
54
55
56
57 func (l *Loader) CreateSymForUpdate(name string, version int) *SymbolBuilder {
58 s := l.LookupOrCreateSym(name, version)
59 l.SetAttrReachable(s, true)
60 return l.MakeSymbolUpdater(s)
61 }
62
63
64
65 func (sb *SymbolBuilder) Sym() Sym { return sb.symIdx }
66 func (sb *SymbolBuilder) Name() string { return sb.name }
67 func (sb *SymbolBuilder) Version() int { return sb.ver }
68 func (sb *SymbolBuilder) Type() sym.SymKind { return sb.kind }
69 func (sb *SymbolBuilder) Size() int64 { return sb.size }
70 func (sb *SymbolBuilder) Data() []byte { return sb.data }
71 func (sb *SymbolBuilder) Value() int64 { return sb.l.SymValue(sb.symIdx) }
72 func (sb *SymbolBuilder) Align() int32 { return sb.l.SymAlign(sb.symIdx) }
73 func (sb *SymbolBuilder) Localentry() uint8 { return sb.l.SymLocalentry(sb.symIdx) }
74 func (sb *SymbolBuilder) OnList() bool { return sb.l.AttrOnList(sb.symIdx) }
75 func (sb *SymbolBuilder) External() bool { return sb.l.AttrExternal(sb.symIdx) }
76 func (sb *SymbolBuilder) Extname() string { return sb.l.SymExtname(sb.symIdx) }
77 func (sb *SymbolBuilder) CgoExportDynamic() bool { return sb.l.AttrCgoExportDynamic(sb.symIdx) }
78 func (sb *SymbolBuilder) Dynimplib() string { return sb.l.SymDynimplib(sb.symIdx) }
79 func (sb *SymbolBuilder) Dynimpvers() string { return sb.l.SymDynimpvers(sb.symIdx) }
80 func (sb *SymbolBuilder) SubSym() Sym { return sb.l.SubSym(sb.symIdx) }
81 func (sb *SymbolBuilder) GoType() Sym { return sb.l.SymGoType(sb.symIdx) }
82 func (sb *SymbolBuilder) VisibilityHidden() bool { return sb.l.AttrVisibilityHidden(sb.symIdx) }
83 func (sb *SymbolBuilder) Sect() *sym.Section { return sb.l.SymSect(sb.symIdx) }
84
85
86
87 func (sb *SymbolBuilder) SetType(kind sym.SymKind) { sb.kind = kind }
88 func (sb *SymbolBuilder) SetSize(size int64) { sb.size = size }
89 func (sb *SymbolBuilder) SetData(data []byte) { sb.data = data }
90 func (sb *SymbolBuilder) SetOnList(v bool) { sb.l.SetAttrOnList(sb.symIdx, v) }
91 func (sb *SymbolBuilder) SetExternal(v bool) { sb.l.SetAttrExternal(sb.symIdx, v) }
92 func (sb *SymbolBuilder) SetValue(v int64) { sb.l.SetSymValue(sb.symIdx, v) }
93 func (sb *SymbolBuilder) SetAlign(align int32) { sb.l.SetSymAlign(sb.symIdx, align) }
94 func (sb *SymbolBuilder) SetLocalentry(value uint8) { sb.l.SetSymLocalentry(sb.symIdx, value) }
95 func (sb *SymbolBuilder) SetExtname(value string) { sb.l.SetSymExtname(sb.symIdx, value) }
96 func (sb *SymbolBuilder) SetDynimplib(value string) { sb.l.SetSymDynimplib(sb.symIdx, value) }
97 func (sb *SymbolBuilder) SetDynimpvers(value string) { sb.l.SetSymDynimpvers(sb.symIdx, value) }
98 func (sb *SymbolBuilder) SetPlt(value int32) { sb.l.SetPlt(sb.symIdx, value) }
99 func (sb *SymbolBuilder) SetGot(value int32) { sb.l.SetGot(sb.symIdx, value) }
100 func (sb *SymbolBuilder) SetSpecial(value bool) { sb.l.SetAttrSpecial(sb.symIdx, value) }
101 func (sb *SymbolBuilder) SetLocal(value bool) { sb.l.SetAttrLocal(sb.symIdx, value) }
102 func (sb *SymbolBuilder) SetVisibilityHidden(value bool) {
103 sb.l.SetAttrVisibilityHidden(sb.symIdx, value)
104 }
105 func (sb *SymbolBuilder) SetNotInSymbolTable(value bool) {
106 sb.l.SetAttrNotInSymbolTable(sb.symIdx, value)
107 }
108 func (sb *SymbolBuilder) SetSect(sect *sym.Section) { sb.l.SetSymSect(sb.symIdx, sect) }
109
110 func (sb *SymbolBuilder) AddBytes(data []byte) {
111 if sb.kind == 0 {
112 sb.kind = sym.SDATA
113 }
114 sb.data = append(sb.data, data...)
115 sb.size = int64(len(sb.data))
116 }
117
118 func (sb *SymbolBuilder) Relocs() Relocs {
119 return sb.l.Relocs(sb.symIdx)
120 }
121
122
123 func (sb *SymbolBuilder) ResetRelocs() {
124 sb.relocs = sb.relocs[:0]
125 }
126
127
128 func (sb *SymbolBuilder) SetRelocType(i int, t objabi.RelocType) {
129 sb.relocs[i].SetType(uint16(t))
130 }
131
132
133 func (sb *SymbolBuilder) SetRelocSym(i int, tgt Sym) {
134 sb.relocs[i].SetSym(goobj.SymRef{PkgIdx: 0, SymIdx: uint32(tgt)})
135 }
136
137
138 func (sb *SymbolBuilder) SetRelocAdd(i int, a int64) {
139 sb.relocs[i].SetAdd(a)
140 }
141
142
143 func (sb *SymbolBuilder) AddRelocs(n int) Relocs {
144 sb.relocs = append(sb.relocs, make([]goobj.Reloc, n)...)
145 return sb.l.Relocs(sb.symIdx)
146 }
147
148
149
150 func (sb *SymbolBuilder) AddRel(typ objabi.RelocType) (Reloc, int) {
151 j := len(sb.relocs)
152 sb.relocs = append(sb.relocs, goobj.Reloc{})
153 sb.relocs[j].SetType(uint16(typ))
154 relocs := sb.Relocs()
155 return relocs.At(j), j
156 }
157
158
159 func (sb *SymbolBuilder) SortRelocs() {
160 slices.SortFunc(sb.extSymPayload.relocs, func(a, b goobj.Reloc) int {
161 return cmp.Compare(a.Off(), b.Off())
162 })
163 }
164
165 func (sb *SymbolBuilder) Reachable() bool {
166 return sb.l.AttrReachable(sb.symIdx)
167 }
168
169 func (sb *SymbolBuilder) SetReachable(v bool) {
170 sb.l.SetAttrReachable(sb.symIdx, v)
171 }
172
173 func (sb *SymbolBuilder) ReadOnly() bool {
174 return sb.l.AttrReadOnly(sb.symIdx)
175 }
176
177 func (sb *SymbolBuilder) SetReadOnly(v bool) {
178 sb.l.SetAttrReadOnly(sb.symIdx, v)
179 }
180
181 func (sb *SymbolBuilder) DuplicateOK() bool {
182 return sb.l.AttrDuplicateOK(sb.symIdx)
183 }
184
185 func (sb *SymbolBuilder) SetDuplicateOK(v bool) {
186 sb.l.SetAttrDuplicateOK(sb.symIdx, v)
187 }
188
189 func (sb *SymbolBuilder) Outer() Sym {
190 return sb.l.OuterSym(sb.symIdx)
191 }
192
193 func (sb *SymbolBuilder) Sub() Sym {
194 return sb.l.SubSym(sb.symIdx)
195 }
196
197 func (sb *SymbolBuilder) SortSub() {
198 sb.l.SortSub(sb.symIdx)
199 }
200
201 func (sb *SymbolBuilder) AddInteriorSym(sub Sym) {
202 sb.l.AddInteriorSym(sb.symIdx, sub)
203 }
204
205 func (sb *SymbolBuilder) AddUint8(v uint8) int64 {
206 off := sb.size
207 if sb.kind == 0 {
208 sb.kind = sym.SDATA
209 }
210 sb.size++
211 sb.data = append(sb.data, v)
212 return off
213 }
214
215 func (sb *SymbolBuilder) AddUintXX(arch *sys.Arch, v uint64, wid int) int64 {
216 off := sb.size
217 sb.setUintXX(arch, off, v, int64(wid))
218 return off
219 }
220
221 func (sb *SymbolBuilder) setUintXX(arch *sys.Arch, off int64, v uint64, wid int64) int64 {
222 if sb.kind == 0 {
223 sb.kind = sym.SDATA
224 }
225 if sb.size < off+wid {
226 sb.size = off + wid
227 sb.Grow(sb.size)
228 }
229
230 switch wid {
231 case 1:
232 sb.data[off] = uint8(v)
233 case 2:
234 arch.ByteOrder.PutUint16(sb.data[off:], uint16(v))
235 case 4:
236 arch.ByteOrder.PutUint32(sb.data[off:], uint32(v))
237 case 8:
238 arch.ByteOrder.PutUint64(sb.data[off:], v)
239 }
240
241 return off + wid
242 }
243
244 func (sb *SymbolBuilder) AddUint16(arch *sys.Arch, v uint16) int64 {
245 return sb.AddUintXX(arch, uint64(v), 2)
246 }
247
248 func (sb *SymbolBuilder) AddUint32(arch *sys.Arch, v uint32) int64 {
249 return sb.AddUintXX(arch, uint64(v), 4)
250 }
251
252 func (sb *SymbolBuilder) AddUint64(arch *sys.Arch, v uint64) int64 {
253 return sb.AddUintXX(arch, v, 8)
254 }
255
256 func (sb *SymbolBuilder) AddUint(arch *sys.Arch, v uint64) int64 {
257 return sb.AddUintXX(arch, v, arch.PtrSize)
258 }
259
260 func (sb *SymbolBuilder) SetUint8(arch *sys.Arch, r int64, v uint8) int64 {
261 return sb.setUintXX(arch, r, uint64(v), 1)
262 }
263
264 func (sb *SymbolBuilder) SetUint16(arch *sys.Arch, r int64, v uint16) int64 {
265 return sb.setUintXX(arch, r, uint64(v), 2)
266 }
267
268 func (sb *SymbolBuilder) SetUint32(arch *sys.Arch, r int64, v uint32) int64 {
269 return sb.setUintXX(arch, r, uint64(v), 4)
270 }
271
272 func (sb *SymbolBuilder) SetUint(arch *sys.Arch, r int64, v uint64) int64 {
273 return sb.setUintXX(arch, r, v, int64(arch.PtrSize))
274 }
275
276 func (sb *SymbolBuilder) SetUintptr(arch *sys.Arch, r int64, v uintptr) int64 {
277 return sb.setUintXX(arch, r, uint64(v), int64(arch.PtrSize))
278 }
279
280 func (sb *SymbolBuilder) SetAddrPlus(arch *sys.Arch, off int64, tgt Sym, add int64) int64 {
281 if sb.Type() == 0 {
282 sb.SetType(sym.SDATA)
283 }
284 if off+int64(arch.PtrSize) > sb.size {
285 sb.size = off + int64(arch.PtrSize)
286 sb.Grow(sb.size)
287 }
288 r, _ := sb.AddRel(objabi.R_ADDR)
289 r.SetSym(tgt)
290 r.SetOff(int32(off))
291 r.SetSiz(uint8(arch.PtrSize))
292 r.SetAdd(add)
293 return off + int64(r.Siz())
294 }
295
296 func (sb *SymbolBuilder) SetAddr(arch *sys.Arch, off int64, tgt Sym) int64 {
297 return sb.SetAddrPlus(arch, off, tgt, 0)
298 }
299
300 func (sb *SymbolBuilder) AddStringAt(off int64, str string) int64 {
301 strLen := int64(len(str))
302 if off+strLen > int64(len(sb.data)) {
303 panic("attempt to write past end of buffer")
304 }
305 copy(sb.data[off:off+strLen], str)
306 return off + strLen
307 }
308
309
310 func (sb *SymbolBuilder) AddCStringAt(off int64, str string) int64 {
311 strLen := int64(len(str))
312 if off+strLen+1 > int64(len(sb.data)) {
313 panic("attempt to write past end of buffer")
314 }
315 copy(sb.data[off:off+strLen], str)
316 sb.data[off+strLen] = 0
317 return off + strLen + 1
318 }
319
320 func (sb *SymbolBuilder) Addstring(str string) int64 {
321 if sb.kind == 0 {
322 sb.kind = sym.SNOPTRDATA
323 }
324 r := sb.size
325 sb.data = append(sb.data, str...)
326 sb.data = append(sb.data, 0)
327 sb.size = int64(len(sb.data))
328 return r
329 }
330
331 func (sb *SymbolBuilder) SetBytesAt(off int64, b []byte) int64 {
332 datLen := int64(len(b))
333 if off+datLen > int64(len(sb.data)) {
334 panic("attempt to write past end of buffer")
335 }
336 copy(sb.data[off:off+datLen], b)
337 return off + datLen
338 }
339
340 func (sb *SymbolBuilder) addSymRef(tgt Sym, add int64, typ objabi.RelocType, rsize int) int64 {
341 if sb.kind == 0 {
342 sb.kind = sym.SDATA
343 }
344 i := sb.size
345
346 sb.size += int64(rsize)
347 sb.Grow(sb.size)
348
349 r, _ := sb.AddRel(typ)
350 r.SetSym(tgt)
351 r.SetOff(int32(i))
352 r.SetSiz(uint8(rsize))
353 r.SetAdd(add)
354
355 return i + int64(rsize)
356 }
357
358
359
360 func (sb *SymbolBuilder) AddSymRef(arch *sys.Arch, tgt Sym, add int64, typ objabi.RelocType, rsize int) int64 {
361 return sb.addSymRef(tgt, add, typ, rsize)
362 }
363
364 func (sb *SymbolBuilder) AddAddrPlus(arch *sys.Arch, tgt Sym, add int64) int64 {
365 return sb.addSymRef(tgt, add, objabi.R_ADDR, arch.PtrSize)
366 }
367
368 func (sb *SymbolBuilder) AddAddrPlus4(arch *sys.Arch, tgt Sym, add int64) int64 {
369 return sb.addSymRef(tgt, add, objabi.R_ADDR, 4)
370 }
371
372 func (sb *SymbolBuilder) AddAddr(arch *sys.Arch, tgt Sym) int64 {
373 return sb.AddAddrPlus(arch, tgt, 0)
374 }
375
376 func (sb *SymbolBuilder) AddPEImageRelativeAddrPlus(arch *sys.Arch, tgt Sym, add int64) int64 {
377 return sb.addSymRef(tgt, add, objabi.R_PEIMAGEOFF, 4)
378 }
379
380 func (sb *SymbolBuilder) AddPCRelPlus(arch *sys.Arch, tgt Sym, add int64) int64 {
381 return sb.addSymRef(tgt, add, objabi.R_PCREL, 4)
382 }
383
384 func (sb *SymbolBuilder) AddCURelativeAddrPlus(arch *sys.Arch, tgt Sym, add int64) int64 {
385 return sb.addSymRef(tgt, add, objabi.R_ADDRCUOFF, arch.PtrSize)
386 }
387
388 func (sb *SymbolBuilder) AddSize(arch *sys.Arch, tgt Sym) int64 {
389 return sb.addSymRef(tgt, 0, objabi.R_SIZE, arch.PtrSize)
390 }
391
392
393
394
395
396
397
398
399
400 func GenAddAddrPlusFunc(internalExec bool) func(s *SymbolBuilder, arch *sys.Arch, tgt Sym, add int64) int64 {
401 if internalExec {
402 return func(s *SymbolBuilder, arch *sys.Arch, tgt Sym, add int64) int64 {
403 if v := s.l.SymValue(tgt); v != 0 {
404 return s.AddUint(arch, uint64(v+add))
405 }
406 return s.AddAddrPlus(arch, tgt, add)
407 }
408 } else {
409 return (*SymbolBuilder).AddAddrPlus
410 }
411 }
412
413 func (sb *SymbolBuilder) MakeWritable() {
414 if sb.ReadOnly() {
415 sb.data = append([]byte(nil), sb.data...)
416 sb.l.SetAttrReadOnly(sb.symIdx, false)
417 }
418 }
419
420 func (sb *SymbolBuilder) AddUleb(v uint64) {
421 if v < 128 {
422 sb.AddUint8(uint8(v))
423 return
424 }
425 for {
426 c := uint8(v & 0x7f)
427 v >>= 7
428 if v != 0 {
429 c |= 0x80
430 }
431 sb.AddUint8(c)
432 if c&0x80 == 0 {
433 break
434 }
435 }
436 }
437
View as plain text