...
1
2
3
4
5
6
21
22 package loader
23
24 import (
25 `syscall`
26 `unsafe`
27 )
28
29 const (
30 MEM_COMMIT = 0x00001000
31 MEM_RESERVE = 0x00002000
32 )
33
34 var (
35 libKernel32 = syscall.NewLazyDLL("KERNEL32.DLL")
36 libKernel32_VirtualAlloc = libKernel32.NewProc("VirtualAlloc")
37 libKernel32_VirtualProtect = libKernel32.NewProc("VirtualProtect")
38 )
39
40 func mmap(nb int) uintptr {
41 addr, err := winapi_VirtualAlloc(0, nb, MEM_COMMIT|MEM_RESERVE, syscall.PAGE_READWRITE)
42 if err != nil {
43 panic(err)
44 }
45 return addr
46 }
47
48 func mprotect(p uintptr, nb int) (oldProtect int) {
49 err := winapi_VirtualProtect(p, nb, syscall.PAGE_EXECUTE_READ, &oldProtect)
50 if err != nil {
51 panic(err)
52 }
53 return
54 }
55
56
57
58 func winapi_VirtualAlloc(lpAddr uintptr, dwSize int, flAllocationType int, flProtect int) (uintptr, error) {
59 r1, _, err := libKernel32_VirtualAlloc.Call(
60 lpAddr,
61 uintptr(dwSize),
62 uintptr(flAllocationType),
63 uintptr(flProtect),
64 )
65 if r1 == 0 {
66 return 0, err
67 }
68 return r1, nil
69 }
70
71
72
73 func winapi_VirtualProtect(lpAddr uintptr, dwSize int, flNewProtect int, lpflOldProtect *int) error {
74 r1, _, err := libKernel32_VirtualProtect.Call(
75 lpAddr,
76 uintptr(dwSize),
77 uintptr(flNewProtect),
78 uintptr(unsafe.Pointer(lpflOldProtect)),
79 )
80 if r1 == 0 {
81 return err
82 }
83 return nil
84 }
85
View as plain text