forked from dearplain/goloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdymcode.go
680 lines (617 loc) · 18.1 KB
/
dymcode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
package goloader
import (
"bytes"
"cmd/objfile/goobj"
"encoding/binary"
"errors"
"fmt"
"io"
"os"
"runtime"
"strconv"
"strings"
"sync"
"unsafe"
)
func mustOK(err error) {
if err != nil {
panic(err)
}
}
// copy from $GOROOT/src/cmd/internal/objabi/reloctype.go
const (
// R_TLS_LE, used on 386, amd64, and ARM, resolves to the offset of the
// thread-local symbol from the thread local base and is used to implement the
// "local exec" model for tls access (r.Sym is not set on intel platforms but is
// set to a TLS symbol -- runtime.tlsg -- in the linker when externally linking).
R_TLS_LE = 16
R_CALL = 8
R_CALLARM = 9
R_CALLARM64 = 10
R_CALLIND = 11
R_PCREL = 15
R_ADDR = 1
// R_ADDRARM64 relocates an adrp, add pair to compute the address of the
// referenced symbol.
R_ADDRARM64 = 3
// R_ADDROFF resolves to a 32-bit offset from the beginning of the section
// holding the data being relocated to the referenced symbol.
R_ADDROFF = 5
// R_WEAKADDROFF resolves just like R_ADDROFF but is a weak relocation.
// A weak relocation does not make the symbol it refers to reachable,
// and is only honored by the linker if the symbol is in some other way
// reachable.
R_WEAKADDROFF = 6
// R_METHODOFF resolves to a 32-bit offset from the beginning of the section
// holding the data being relocated to the referenced symbol.
// It is a variant of R_ADDROFF used when linking from the uncommonType of a
// *rtype, and may be set to zero by the linker if it determines the method
// text is unreachable by the linked program.
R_METHODOFF = 24
)
// copy from $GOROOT/src/cmd/internal/objabi/symkind.go
const (
// An otherwise invalid zero value for the type
Sxxx = iota
// Executable instructions
STEXT
// Read only static data
SRODATA
// Static data that does not contain any pointers
SNOPTRDATA
// Static data
SDATA
// Statically data that is initially all 0s
SBSS
// Statically data that is initially all 0s and does not contain pointers
SNOPTRBSS
// Thread-local data that is initally all 0s
STLSBSS
// Debugging data
SDWARFINFO
SDWARFRANGE
)
type SymData struct {
Name string
Kind int
Offset int
Reloc []Reloc
}
type Reloc struct {
Offset int
SymOff int
Size int
Type int
Add int
}
// CodeReloc dispatch and load CodeReloc struct via network is OK
type CodeReloc struct {
Code []byte
Data []byte
Mod Module
Syms []SymData
}
type CodeModule struct {
Syms map[string]uintptr
CodeByte []byte
Module interface{}
pcfuncdata []findfuncbucket
stkmaps [][]byte
itabs []itabReloc
itabSyms []itabSym
typemap map[typeOff]uintptr
}
type itabSym struct {
ptr int
inter int
_type int
}
type itabReloc struct {
locOff int
symOff int
size int
locType int
add int
}
type symFile struct {
sym *goobj.Sym
file *os.File
}
var (
tmpModule interface{}
modules = make(map[interface{}]bool)
modulesLock sync.Mutex
mov32bit = [8]byte{0x00, 0x00, 0x80, 0xD2, 0x00, 0x00, 0xA0, 0xF2}
)
func ReadObj(f *os.File) (*CodeReloc, error) {
obj, err := goobj.Parse(f, "main")
if err != nil {
return nil, fmt.Errorf("read error: %v", err)
}
var syms = make(map[string]symFile)
for _, sym := range obj.Syms {
syms[sym.Name] = symFile{
sym: sym,
file: f,
}
}
var symMap = make(map[string]int)
var gcObjs = make(map[string]uintptr)
var fileTabOffsetMap = make(map[string]int)
var reloc CodeReloc
for _, sym := range obj.Syms {
if sym.Kind == STEXT {
relocSym(&reloc, symFile{sym: sym,
file: f}, syms, symMap,
gcObjs, fileTabOffsetMap)
}
}
return &reloc, nil
}
func ReadObjs(files []string, pkgPath []string) (*CodeReloc, error) {
var fs []*os.File
for _, file := range files {
f, err := os.Open(file)
if err != nil {
return nil, err
}
fs = append(fs, f)
defer f.Close()
}
var allSyms = make(map[string]symFile)
var symMap = make(map[string]int)
var gcObjs = make(map[string]uintptr)
var fileTabOffsetMap = make(map[string]int)
var reloc CodeReloc
var goObjs []*goobj.Package
for i, f := range fs {
if pkgPath[i] == "" {
pkgPath[i] = "main"
}
obj, err := goobj.Parse(f, pkgPath[i])
if err != nil {
return nil, fmt.Errorf("read error: %v", err)
}
for _, sym := range obj.Syms {
allSyms[sym.Name] = symFile{
sym: sym,
file: f,
}
}
goObjs = append(goObjs, obj)
}
for i, obj := range goObjs {
for _, sym := range obj.Syms {
if sym.Kind == STEXT {
relocSym(&reloc, symFile{sym: sym,
file: fs[i]}, allSyms, symMap,
gcObjs, fileTabOffsetMap)
}
}
}
return &reloc, nil
}
func addSym(symMap map[string]int, symArray *[]SymData, rsym *SymData) int {
var offset int
if of, ok := symMap[rsym.Name]; !ok {
offset = len(*symArray)
*symArray = append(*symArray, *rsym)
symMap[rsym.Name] = offset
} else {
offset = of
(*symArray)[offset] = *rsym
}
return offset
}
type readAtSeeker struct {
io.ReadSeeker
}
func (r *readAtSeeker) ReadAt(p []byte, offset int64) (n int, err error) {
_, err = r.Seek(offset, io.SeekStart)
if err != nil {
return
}
return r.Read(p)
}
func relocSym(reloc *CodeReloc, curSym symFile,
allSyms map[string]symFile, symMap map[string]int,
gcObjs map[string]uintptr, fileTabOffsetMap map[string]int) int {
if curSymOffset, ok := symMap[curSym.sym.Name]; ok {
return curSymOffset
}
var rsym SymData
rsym.Name = curSym.sym.Name
rsym.Kind = int(curSym.sym.Kind)
curSymOffset := addSym(symMap, &reloc.Syms, &rsym)
code := make([]byte, curSym.sym.Data.Size)
curSym.file.Seek(curSym.sym.Data.Offset, io.SeekStart)
_, err := curSym.file.Read(code)
mustOK(err)
switch int(curSym.sym.Kind) {
case STEXT:
rsym.Offset = len(reloc.Code)
reloc.Code = append(reloc.Code, code...)
readFuncData(&reloc.Mod, curSym, allSyms, gcObjs,
fileTabOffsetMap, curSymOffset, rsym.Offset)
default:
rsym.Offset = len(reloc.Data)
reloc.Data = append(reloc.Data, code...)
}
addSym(symMap, &reloc.Syms, &rsym)
for _, re := range curSym.sym.Reloc {
symOff := -1
if s, ok := allSyms[re.Sym.Name]; ok {
symOff = relocSym(reloc, s, allSyms, symMap,
gcObjs, fileTabOffsetMap)
} else {
var exSym SymData
exSym.Name = re.Sym.Name
exSym.Offset = -1
if re.Type == R_TLS_LE {
exSym.Name = TLSNAME
exSym.Offset = int(re.Offset)
}
if re.Type == R_CALLIND {
exSym.Offset = 0
exSym.Name = R_CALLIND_NAME
}
if strings.HasPrefix(exSym.Name, "type..importpath.") {
path := strings.TrimLeft(exSym.Name, "type..importpath.")
path = strings.Trim(path, ".")
pathb := []byte(path)
pathb = append(pathb, 0)
exSym.Offset = len(reloc.Data)
reloc.Data = append(reloc.Data, pathb...)
}
symOff = addSym(symMap, &reloc.Syms, &exSym)
}
rsym.Reloc = append(rsym.Reloc,
Reloc{Offset: int(re.Offset) + rsym.Offset, SymOff: symOff,
Type: int(re.Type),
Size: int(re.Size), Add: int(re.Add)})
}
reloc.Syms[curSymOffset].Reloc = rsym.Reloc
return curSymOffset
}
func strWrite(buf *bytes.Buffer, str ...string) {
for _, s := range str {
buf.WriteString(s)
if s != "\n" {
buf.WriteString(" ")
}
}
}
func Load(code *CodeReloc, symPtr map[string]uintptr) (*CodeModule, error) {
pCodeLen := len(code.Code) + len(code.Data)
codeLen := int(float32(pCodeLen) * 1.5)
codeByte, err := Mmap(codeLen)
if err != nil {
return nil, err
}
var codeModule = CodeModule{
Syms: make(map[string]uintptr),
typemap: make(map[typeOff]uintptr),
}
var errBuf bytes.Buffer
base := int((*sliceHeader)(unsafe.Pointer(&codeByte)).Data)
dataBase := base + len(code.Code)
var symAddrs = make([]int, len(code.Syms))
var itabIndexs []int
var funcTypeMap = make(map[string]*int)
for i, sym := range code.Syms {
if sym.Offset == -1 {
if ptr, ok := symPtr[sym.Name]; ok {
symAddrs[i] = int(ptr)
} else {
symAddrs[i] = -1
strWrite(&errBuf, "unresolve external:", sym.Name, "\n")
}
} else if sym.Name == TLSNAME {
RegTLS(symPtr, sym.Offset)
} else if sym.Kind == STEXT {
symAddrs[i] = code.Syms[i].Offset + base
codeModule.Syms[sym.Name] = uintptr(symAddrs[i])
} else if strings.HasPrefix(sym.Name, "go.itab") {
if ptr, ok := symPtr[sym.Name]; ok {
symAddrs[i] = int(ptr)
} else {
itabIndexs = append(itabIndexs, i)
}
} else {
symAddrs[i] = code.Syms[i].Offset + dataBase
if strings.HasPrefix(sym.Name, "type.func") {
funcTypeMap[sym.Name] = &symAddrs[i]
}
if strings.HasPrefix(sym.Name, "type.") {
if ptr, ok := symPtr[sym.Name]; ok {
symAddrs[i] = int(ptr)
}
}
}
}
var itabSymMap = make(map[string]int)
for _, itabIndex := range itabIndexs {
curSym := code.Syms[itabIndex]
sym1 := symAddrs[curSym.Reloc[0].SymOff]
sym2 := symAddrs[curSym.Reloc[1].SymOff]
itabSymMap[curSym.Name] = len(codeModule.itabSyms)
codeModule.itabSyms = append(codeModule.itabSyms, itabSym{inter: sym1, _type: sym2})
if sym1 == -1 || sym2 == -1 {
continue
}
addIFaceSubFuncType(funcTypeMap, codeModule.typemap,
(*interfacetype)(unsafe.Pointer(uintptr(sym1))), base)
}
var armcode = []byte{0x04, 0xF0, 0x1F, 0xE5, 0x00, 0x00, 0x00, 0x00}
var arm64code = []byte{0x43, 0x00, 0x00, 0x58, 0x60, 0x00, 0x1F, 0xD6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
var x86code = []byte{0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
var movcode byte = 0x8b
var leacode byte = 0x8d
var jmpOff = pCodeLen
for _, curSym := range code.Syms {
for _, loc := range curSym.Reloc {
sym := code.Syms[loc.SymOff]
if symAddrs[loc.SymOff] == -1 {
continue
}
if symAddrs[loc.SymOff] == 0 && strings.HasPrefix(sym.Name, "go.itab") {
codeModule.itabs = append(codeModule.itabs,
itabReloc{locOff: loc.Offset, symOff: itabSymMap[sym.Name],
size: loc.Size, locType: loc.Type, add: loc.Add})
continue
}
var offset int
switch loc.Type {
case R_TLS_LE:
binary.LittleEndian.PutUint32(code.Code[loc.Offset:], uint32(symPtr[TLSNAME]))
continue
case R_CALL, R_PCREL:
var relocByte = code.Data
var addrBase = dataBase
if curSym.Kind == STEXT {
addrBase = base
relocByte = code.Code
}
offset = symAddrs[loc.SymOff] - (addrBase + loc.Offset + loc.Size) + loc.Add
if offset > 0x7fffffff || offset < -0x7fffffff {
if jmpOff+8 > codeLen {
strWrite(&errBuf, "len overflow", "sym:", sym.Name, "\n")
continue
}
rb := relocByte[loc.Offset-2:]
if loc.Type == R_CALL {
offset = (base + jmpOff) - (addrBase + loc.Offset + loc.Size)
copy(codeByte[jmpOff:], x86code)
binary.LittleEndian.PutUint32(relocByte[loc.Offset:], uint32(offset))
binary.LittleEndian.PutUint32(codeByte[jmpOff+6:], uint32(symAddrs[loc.SymOff]+loc.Add))
jmpOff += len(x86code)
} else if rb[0] == leacode || rb[0] == movcode {
offset = (base + jmpOff) - (addrBase + loc.Offset + loc.Size)
binary.LittleEndian.PutUint32(relocByte[loc.Offset:], uint32(offset))
rb[0] = movcode
binary.LittleEndian.PutUint32(codeByte[jmpOff:], uint32(symAddrs[loc.SymOff]+loc.Add))
jmpOff += 8
} else {
strWrite(&errBuf, "offset overflow sym:", sym.Name, "\n")
binary.LittleEndian.PutUint32(relocByte[loc.Offset:], uint32(offset))
}
continue
}
binary.LittleEndian.PutUint32(relocByte[loc.Offset:], uint32(offset))
case R_CALLARM, R_CALLARM64:
var add = loc.Add
var pcOff = 0
if loc.Type == R_CALLARM {
add = loc.Add & 0xffffff
if add > 256 {
add = 0
} else {
add += 2
}
pcOff = 8
}
offset = (symAddrs[loc.SymOff] - (base + loc.Offset + pcOff) + add) / 4
if offset > 0x7fffff || offset < -0x7fffff {
if jmpOff+4 > codeLen {
strWrite(&errBuf, "len overflow", "sym:", sym.Name, "\n")
continue
}
align := jmpOff % 4
if align != 0 {
jmpOff += (4 - align)
}
offset = (jmpOff - (loc.Offset + pcOff)) / 4
var v = uint32(offset)
b := code.Code[loc.Offset:]
b[0] = byte(v)
b[1] = byte(v >> 8)
b[2] = byte(v >> 16)
var jmpLocOff = 0
var jmpLen = 0
if loc.Type == R_CALLARM64 {
copy(codeByte[jmpOff:], arm64code)
jmpLen = len(arm64code)
jmpLocOff = 8
} else {
copy(codeByte[jmpOff:], armcode)
jmpLen = len(armcode)
jmpLocOff = 4
}
*(*uintptr)(unsafe.Pointer(&(codeByte[jmpOff+jmpLocOff:][0]))) = uintptr(symAddrs[loc.SymOff] + add*4)
jmpOff += jmpLen
continue
}
var v = uint32(offset)
b := code.Code[loc.Offset:]
b[0] = byte(v)
b[1] = byte(v >> 8)
b[2] = byte(v >> 16)
case R_ADDRARM64:
if curSym.Kind != STEXT {
strWrite(&errBuf, "not in code?\n")
}
relocADRP(code.Code[loc.Offset:], base+loc.Offset, symAddrs[loc.SymOff], sym.Name)
case R_ADDR:
var relocByte = code.Data
if curSym.Kind == STEXT {
relocByte = code.Code
}
offset = symAddrs[loc.SymOff] + loc.Add
*(*uintptr)(unsafe.Pointer(&(relocByte[loc.Offset:][0]))) = uintptr(offset)
case R_CALLIND:
case R_ADDROFF, R_WEAKADDROFF, R_METHODOFF:
var relocByte = code.Data
var addrBase = base
if curSym.Kind == STEXT {
strWrite(&errBuf, "impossible!", sym.Name, "locate on code segment", "\n")
}
offset = symAddrs[loc.SymOff] - addrBase + loc.Add
binary.LittleEndian.PutUint32(relocByte[loc.Offset:], uint32(offset))
default:
strWrite(&errBuf, "unknown reloc type:", strconv.Itoa(loc.Type), sym.Name, "\n")
}
}
}
var module moduledata
module.ftab = make([]functab, len(code.Mod.ftab))
copy(module.ftab, code.Mod.ftab)
pclnOff := len(code.Mod.pclntable)
module.pclntable = make([]byte, len(code.Mod.pclntable)+
(_funcSize+100)*len(code.Mod.ftab))
copy(module.pclntable, code.Mod.pclntable)
module.findfunctab = (uintptr)(unsafe.Pointer(&code.Mod.pcfunc[0]))
module.minpc = (uintptr)(unsafe.Pointer(&codeByte[0]))
module.maxpc = (uintptr)(unsafe.Pointer(&codeByte[len(code.Code)-1])) + 2
module.filetab = code.Mod.filetab
module.typemap = codeModule.typemap
module.types = uintptr(base)
module.etypes = uintptr(base + codeLen)
module.text = uintptr(base)
module.etext = uintptr(base + len(code.Code))
codeModule.pcfuncdata = code.Mod.pcfunc // hold reference
codeModule.stkmaps = code.Mod.stkmaps
for i := range module.ftab {
if i == 0 {
continue
}
module.ftab[i].entry = uintptr(symAddrs[int(code.Mod.ftab[i].entry)])
ptr2 := (uintptr)(unsafe.Pointer(&module.pclntable[pclnOff]))
if PtrSize == 8 && ptr2&4 != 0 {
pclnOff += 4
}
module.ftab[i].funcoff = uintptr(pclnOff)
fi := code.Mod.funcinfo[i-1]
fi.entry = module.ftab[i].entry
copy2Slice(module.pclntable[pclnOff:],
unsafe.Pointer(&fi._func), _funcSize)
pclnOff += _funcSize
if len(fi.pcdata) > 0 {
size := int(4 * fi.npcdata)
copy2Slice(module.pclntable[pclnOff:],
unsafe.Pointer(&fi.pcdata[0]), size)
pclnOff += size
}
var funcdata = make([]uintptr, len(fi.funcdata))
copy(funcdata, fi.funcdata)
for i, v := range funcdata {
funcdata[i] = (uintptr)(unsafe.Pointer(&(code.Mod.stkmaps[v][0])))
}
ptr := (uintptr)(unsafe.Pointer(&module.pclntable[pclnOff-1])) + 1
if PtrSize == 8 && ptr&4 != 0 {
t := [4]byte{}
copy(module.pclntable[pclnOff:], t[:])
pclnOff += len(t)
}
funcDataSize := int(PtrSize * fi.nfuncdata)
copy2Slice(module.pclntable[pclnOff:],
unsafe.Pointer(&funcdata[0]), funcDataSize)
pclnOff += funcDataSize
}
module.pclntable = module.pclntable[:pclnOff]
if len(module.ftab) >= 2 {
module.ftab[0] = module.ftab[1]
}
modulesLock.Lock()
addModule(&codeModule, &module, runtime.Version())
modulesLock.Unlock()
copy(codeByte, code.Code)
copy(codeByte[len(code.Code):], code.Data)
codeModule.CodeByte = codeByte
for i := range codeModule.itabSyms {
it := &codeModule.itabSyms[i]
if it.inter == -1 || it._type == -1 {
continue
}
it.ptr = getitab(it.inter, it._type, false)
}
for _, it := range codeModule.itabs {
symAddr := codeModule.itabSyms[it.symOff].ptr
if symAddr == 0 {
continue
}
switch it.locType {
case R_PCREL:
pc := base + it.locOff + it.size
offset := symAddr - pc + it.add
if offset > 2147483647 || offset < -2147483647 {
offset = (base + jmpOff) - pc + it.add
binary.LittleEndian.PutUint32(codeByte[it.locOff:], uint32(offset))
codeByte[it.locOff-2:][0] = movcode
*(*uintptr)(unsafe.Pointer(&(codeByte[jmpOff:][0]))) = uintptr(symAddr)
jmpOff += PtrSize
continue
}
binary.LittleEndian.PutUint32(codeByte[it.locOff:], uint32(offset))
case R_ADDRARM64:
relocADRP(codeByte[it.locOff:], base+it.locOff, symAddr, "unknown")
}
}
if errBuf.Len() > 0 {
return &codeModule, errors.New(errBuf.String())
}
return &codeModule, nil
}
func relocADRP(mCode []byte, pc int, symAddr int, symName string) {
pcPage := pc - pc&0xfff
lowOff := symAddr & 0xfff
symPage := symAddr - lowOff
pageOff := symPage - pcPage
if pageOff > 1<<31 || pageOff < -1<<31 {
// fmt.Println("adrp overflow!", symName, symAddr, symAddr < (1<<31))
movlow := binary.LittleEndian.Uint32(mov32bit[:4])
movhigh := binary.LittleEndian.Uint32(mov32bit[4:])
adrp := binary.LittleEndian.Uint32(mCode)
symAddrUint32 := uint32(symAddr)
movlow = (((adrp & 0x1f) | movlow) | ((symAddrUint32 & 0xffff) << 5))
movhigh = (((adrp & 0x1f) | movhigh) | ((symAddrUint32 & 0xffff0000) >> 16 << 5))
// fmt.Println(adrp, movlow, movhigh)
binary.LittleEndian.PutUint32(mCode, movlow)
binary.LittleEndian.PutUint32(mCode[4:], movhigh)
return
}
fmt.Println("pageOff<0:", pageOff < 0)
// 2bit + 19bit + low(12bit) = 33bit
pageAnd := (uint32((pageOff>>12)&3) << 29) | (uint32((pageOff>>15)&0x7ffff) << 5)
adrp := binary.LittleEndian.Uint32(mCode)
adrp = adrp | pageAnd
binary.LittleEndian.PutUint32(mCode, adrp)
lowOff = lowOff << 10
adrpAdd := binary.LittleEndian.Uint32(mCode[4:])
adrpAdd = adrpAdd | uint32(lowOff)
binary.LittleEndian.PutUint32(mCode[4:], adrpAdd)
}
func copy2Slice(dst []byte, src unsafe.Pointer, size int) {
var s = sliceHeader{
Data: (uintptr)(src),
Len: size,
Cap: size,
}
copy(dst, *(*[]byte)(unsafe.Pointer(&s)))
}
func (cm *CodeModule) Unload() {
runtime.GC()
modulesLock.Lock()
removeModule(cm.Module, runtime.Version())
modulesLock.Unlock()
Munmap(cm.CodeByte)
}