forked from googleapis/google-cloud-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arch.go
186 lines (164 loc) · 4.44 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
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
// Copyright 2018 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package arch contains architecture-specific definitions.
package arch
import (
"encoding/binary"
"math"
)
const MaxBreakpointSize = 4 // TODO
// Architecture defines the architecture-specific details for a given machine.
type Architecture struct {
// BreakpointSize is the size of a breakpoint instruction, in bytes.
BreakpointSize int
// IntSize is the size of the int type, in bytes.
IntSize int
// PointerSize is the size of a pointer, in bytes.
PointerSize int
// ByteOrder is the byte order for ints and pointers.
ByteOrder binary.ByteOrder
// FloatByteOrder is the byte order for floats.
FloatByteOrder binary.ByteOrder
BreakpointInstr [MaxBreakpointSize]byte
}
func (a *Architecture) Int(buf []byte) int64 {
return int64(a.Uint(buf))
}
func (a *Architecture) Uint(buf []byte) uint64 {
if len(buf) != a.IntSize {
panic("bad IntSize")
}
switch a.IntSize {
case 4:
return uint64(a.ByteOrder.Uint32(buf[:4]))
case 8:
return a.ByteOrder.Uint64(buf[:8])
}
panic("no IntSize")
}
func (a *Architecture) Int16(buf []byte) int16 {
return int16(a.Uint16(buf))
}
func (a *Architecture) Int32(buf []byte) int32 {
return int32(a.Uint32(buf))
}
func (a *Architecture) Int64(buf []byte) int64 {
return int64(a.Uint64(buf))
}
func (a *Architecture) Uint16(buf []byte) uint16 {
return a.ByteOrder.Uint16(buf)
}
func (a *Architecture) Uint32(buf []byte) uint32 {
return a.ByteOrder.Uint32(buf)
}
func (a *Architecture) Uint64(buf []byte) uint64 {
return a.ByteOrder.Uint64(buf)
}
func (a *Architecture) IntN(buf []byte) int64 {
if len(buf) == 0 {
return 0
}
x := int64(0)
if a.ByteOrder == binary.LittleEndian {
i := len(buf) - 1
x = int64(int8(buf[i])) // sign-extended
for i--; i >= 0; i-- {
x <<= 8
x |= int64(buf[i]) // not sign-extended
}
} else {
x = int64(int8(buf[0])) // sign-extended
for i := 1; i < len(buf); i++ {
x <<= 8
x |= int64(buf[i]) // not sign-extended
}
}
return x
}
func (a *Architecture) UintN(buf []byte) uint64 {
u := uint64(0)
if a.ByteOrder == binary.LittleEndian {
shift := uint(0)
for _, c := range buf {
u |= uint64(c) << shift
shift += 8
}
} else {
for _, c := range buf {
u <<= 8
u |= uint64(c)
}
}
return u
}
func (a *Architecture) Uintptr(buf []byte) uint64 {
if len(buf) != a.PointerSize {
panic("bad PointerSize")
}
switch a.PointerSize {
case 4:
return uint64(a.ByteOrder.Uint32(buf[:4]))
case 8:
return a.ByteOrder.Uint64(buf[:8])
}
panic("no PointerSize")
}
func (a *Architecture) Float32(buf []byte) float32 {
if len(buf) != 4 {
panic("bad float32 size")
}
return math.Float32frombits(a.FloatByteOrder.Uint32(buf))
}
func (a *Architecture) Float64(buf []byte) float64 {
if len(buf) != 8 {
panic("bad float64 size")
}
return math.Float64frombits(a.FloatByteOrder.Uint64(buf))
}
func (a *Architecture) Complex64(buf []byte) complex64 {
if len(buf) != 8 {
panic("bad complex64 size")
}
return complex(a.Float32(buf[0:4]), a.Float32(buf[4:8]))
}
func (a *Architecture) Complex128(buf []byte) complex128 {
if len(buf) != 16 {
panic("bad complex128 size")
}
return complex(a.Float64(buf[0:8]), a.Float64(buf[8:16]))
}
var AMD64 = Architecture{
BreakpointSize: 1,
IntSize: 8,
PointerSize: 8,
ByteOrder: binary.LittleEndian,
FloatByteOrder: binary.LittleEndian,
BreakpointInstr: [MaxBreakpointSize]byte{0xCC}, // INT 3
}
var X86 = Architecture{
BreakpointSize: 1,
IntSize: 4,
PointerSize: 4,
ByteOrder: binary.LittleEndian,
FloatByteOrder: binary.LittleEndian,
BreakpointInstr: [MaxBreakpointSize]byte{0xCC}, // INT 3
}
var ARM = Architecture{
BreakpointSize: 4, // TODO
IntSize: 4,
PointerSize: 4,
ByteOrder: binary.LittleEndian,
FloatByteOrder: binary.LittleEndian, // TODO
BreakpointInstr: [MaxBreakpointSize]byte{0x00, 0x00, 0x00, 0x00}, // TODO
}