-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy patharch.go
59 lines (51 loc) · 1.19 KB
/
arch.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
package hinako
import (
"fmt"
"runtime"
)
const (
_ASM_OP_NEAR_JMP = 0xE9 // jmp rel32
_ASM_OP_FAR_JMP = 0x25FF // jmp dword ptr[addr32]
_ASM_OP_PUSH = 0x68 // push
_ASM_OP_MOV_RSP4 = 0x042444C7 // mov DWORD PTR [rsp+0x4], ...
_ASM_OP_RET = 0xC3 // ret
)
type Arch interface {
DisassembleMode() int
NearJumpSize() uint
FarJumpSize() uint
NewNearJumpAsm(from, to uintptr) []byte
NewFarJumpAsm(from, to uintptr) []byte
}
func maxTrampolineSize(arch Arch) uint {
return 40
}
func isFarJump(from, to uintptr) bool {
if to >= from {
return (to - from) > uintptr(0x7fff0000)
} else {
return (from - to) > uintptr(0x7fff0000)
}
}
func jumpSize(arch Arch, from, to uintptr) uint {
if isFarJump(from, to) {
return arch.FarJumpSize()
}
return arch.NearJumpSize()
}
func newJumpAsm(arch Arch, from, to uintptr) []byte {
if isFarJump(from, to) {
return arch.NewFarJumpAsm(from, to)
}
return arch.NewNearJumpAsm(from, to)
}
//NewRuntimeArch func
func NewRuntimeArch() (Arch, error) {
switch runtime.GOARCH {
case "386":
return &Arch386{}, nil
case "amd64":
return &ArchAMD64{}, nil
}
return nil, fmt.Errorf("unsupported Arch: %s", runtime.GOARCH)
}