forked from goretk/gore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
moduledata.go
361 lines (310 loc) · 8.79 KB
/
moduledata.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
// This file is part of GoRE.
//
// Copyright (C) 2019-2024 GoRE Authors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package gore
//go:generate go run ./gen moduledata
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"strconv"
"github.com/ZxillyFork/gore/extern"
"github.com/ZxillyFork/gore/extern/gover"
)
// Moduledata holds information about the layout of the executable image in memory.
type Moduledata interface {
// Text returns the text secion.
Text() ModuleDataSection
// NoPtrData returns the noptrdata section.
NoPtrData() ModuleDataSection
// Data returns the data section.
Data() ModuleDataSection
// Bss returns the bss section.
Bss() ModuleDataSection
// NoPtrBss returns the noptrbss section.
NoPtrBss() ModuleDataSection
// Types returns the types section.
Types() ModuleDataSection
// PCLNTab returns the pclntab section.
PCLNTab() ModuleDataSection
// FuncTab returns the functab section.
FuncTab() ModuleDataSection
// ITabLinks returns the itablinks section.
ITabLinks() ModuleDataSection
// TypeLink returns the typelink section.
TypeLink() ModuleDataSection
// TypeLinkData returns the typelink section data.
TypeLinkData() ([]int32, error)
// GoFuncValue returns the value of the 'go:func.*' symbol.
GoFuncValue() uint64
}
type moduledata struct {
TextAddr, TextLen uint64
NoPtrDataAddr, NoPtrDataLen uint64
DataAddr, DataLen uint64
BssAddr, BssLen uint64
NoPtrBssAddr, NoPtrBssLen uint64
TypesAddr, TypesLen uint64
TypelinkAddr, TypelinkLen uint64
ITabLinkAddr, ITabLinkLen uint64
FuncTabAddr, FuncTabLen uint64
PCLNTabAddr, PCLNTabLen uint64
GoFuncVal uint64
fh fileHandler
}
// Text returns the text section.
func (m moduledata) Text() ModuleDataSection {
return ModuleDataSection{
Address: m.TextAddr,
Length: m.TextLen,
fh: m.fh,
}
}
// NoPtrData returns the noptrdata section.
func (m moduledata) NoPtrData() ModuleDataSection {
return ModuleDataSection{
Address: m.NoPtrDataAddr,
Length: m.NoPtrDataLen,
fh: m.fh,
}
}
// Data returns the data section.
func (m moduledata) Data() ModuleDataSection {
return ModuleDataSection{
Address: m.DataAddr,
Length: m.DataLen,
fh: m.fh,
}
}
// Bss returns the bss section.
func (m moduledata) Bss() ModuleDataSection {
return ModuleDataSection{
Address: m.BssAddr,
Length: m.BssLen,
fh: m.fh,
}
}
// NoPtrBss returns the noptrbss section.
func (m moduledata) NoPtrBss() ModuleDataSection {
return ModuleDataSection{
Address: m.NoPtrBssAddr,
Length: m.NoPtrBssLen,
fh: m.fh,
}
}
// Types returns the types section.
func (m moduledata) Types() ModuleDataSection {
return ModuleDataSection{
Address: m.TypesAddr,
Length: m.TypesLen,
fh: m.fh,
}
}
// PCLNTab returns the pclntab section.
func (m moduledata) PCLNTab() ModuleDataSection {
return ModuleDataSection{
Address: m.PCLNTabAddr,
Length: m.PCLNTabLen,
fh: m.fh,
}
}
// FuncTab returns the functab section.
func (m moduledata) FuncTab() ModuleDataSection {
return ModuleDataSection{
Address: m.FuncTabAddr,
Length: m.FuncTabLen,
fh: m.fh,
}
}
// ITabLinks returns the itablinks section.
func (m moduledata) ITabLinks() ModuleDataSection {
return ModuleDataSection{
Address: m.ITabLinkAddr,
Length: m.ITabLinkLen,
fh: m.fh,
}
}
// TypeLink returns the typelink section.
func (m moduledata) TypeLink() ModuleDataSection {
return ModuleDataSection{
Address: m.TypelinkAddr,
Length: m.TypelinkLen,
fh: m.fh,
}
}
// TypeLinkData returns the typelink section.
func (m moduledata) TypeLinkData() ([]int32, error) {
base, data, err := m.fh.getSectionDataFromAddress(m.TypelinkAddr)
if err != nil {
return nil, fmt.Errorf("failed to get the typelink data section: %w", err)
}
r := bytes.NewReader(data[m.TypelinkAddr-base:])
a := make([]int32, 0, m.TypelinkLen)
bo := m.fh.getFileInfo().ByteOrder
for i := uint64(0); i < m.TypelinkLen; i++ {
// Type offsets are always int32
var off int32
err = binary.Read(r, bo, &off)
if err != nil {
return nil, fmt.Errorf("failed to read typelink item %d: %w", i, err)
}
a = append(a, off)
}
return a, nil
}
// GoFuncValue returns the value of the "go:func.*" symbol.
func (m moduledata) GoFuncValue() uint64 {
return m.GoFuncVal
}
// ModuleDataSection is a section defined in the Moduledata structure.
type ModuleDataSection struct {
// Address is the virtual address where the section starts.
Address uint64
// Length is the byte length for the data in this section.
Length uint64
fh fileHandler
}
// Data returns the data in the section.
func (m ModuleDataSection) Data() ([]byte, error) {
// If we don't have any data, return an empty slice.
if m.Length == 0 {
return []byte{}, nil
}
base, data, err := m.fh.getSectionDataFromAddress(m.Address)
if err != nil {
return nil, fmt.Errorf("getting module data section failed: %w", err)
}
start := m.Address - base
if uint64(len(data)) < start+m.Length {
return nil, fmt.Errorf("the length of module data section is to big: address 0x%x, base 0x%x, length 0x%x", m.Address, base, m.Length)
}
buf := make([]byte, m.Length)
copy(buf, data[start:start+m.Length])
return buf, nil
}
func buildPclnTabAddrBinary(wordSize int, order binary.ByteOrder, addr uint64) []byte {
buf := make([]byte, wordSize)
if wordSize == intSize32 {
order.PutUint32(buf, uint32(addr))
} else {
order.PutUint64(buf, addr)
}
return buf
}
func pickVersionedModuleData(info *FileInfo) (modulable, error) {
var bits int
if info.WordSize == intSize32 {
bits = 32
} else {
bits = 64
}
if info.goversion == nil {
return nil, ErrNoGoVersionFound
}
ver := gover.Parse(extern.StripGo(info.goversion.Name))
zero := gover.Version{}
if ver == zero {
return nil, errors.New("could not parse the go version " + info.goversion.Name)
}
verBit, err := strconv.Atoi(ver.Minor)
if err != nil {
return nil, err
}
buf, err := selectModuleData(verBit, bits)
if err != nil {
return nil, fmt.Errorf("error when selecting the module data: %w", err)
}
return buf, nil
}
func extractModuledata(f *GoFile) (moduledata, error) {
vmd, err := pickVersionedModuleData(f.FileInfo)
if err != nil {
return moduledata{}, err
}
vmdSize := binary.Size(vmd)
// pre define these variables to follow the goto requirements
var off int
var magic []byte
var tabAddr uint64
secAddr, secData, err := f.fh.getSectionData(f.fh.moduledataSection())
if err != nil {
return moduledata{}, err
}
// if we can get the moduledata addr from the symbol, we have no need to search
sym, err := f.fh.getSymbol("runtime.firstmoduledata")
if err == nil {
off = int(sym.Value - secAddr)
goto load
}
err = f.initPclntab()
if err != nil {
return moduledata{}, err
}
tabAddr = f.pclntabAddr
magic = buildPclnTabAddrBinary(f.FileInfo.WordSize, f.FileInfo.ByteOrder, tabAddr)
search:
off = bytes.Index(secData, magic)
load:
if off == -1 {
return moduledata{}, errors.New("could not find moduledata")
}
if len(secData) < off+vmdSize {
return moduledata{}, fmt.Errorf("offset %d is out of bounds %d", off, len(secData))
}
data := secData[off : off+vmdSize]
// Read the module struct from the file.
r := bytes.NewReader(data)
err = binary.Read(r, f.FileInfo.ByteOrder, vmd)
if err != nil {
return moduledata{}, fmt.Errorf("error when reading module data from file: %w", err)
}
// Convert the read struct to the type we return to the caller.
md := vmd.toModuledata()
// Take a simple validation step to ensure that the moduledata is valid.
text := md.TextAddr
etext := md.TextAddr + md.TextLen
textSectAddr, textSect, err := f.fh.getCodeSection()
if err != nil {
return moduledata{}, err
}
if text > etext {
goto invalidMD
}
if !(textSectAddr <= text && text < textSectAddr+uint64(len(textSect))) {
goto invalidMD
}
// Add the file handler.
md.fh = f.fh
return md, nil
invalidMD:
secData = secData[off+1:]
goto search
}
func readUIntTo64(r io.Reader, byteOrder binary.ByteOrder, is32bit bool) (addr uint64, err error) {
if is32bit {
var addr32 uint32
err = binary.Read(r, byteOrder, &addr32)
addr = uint64(addr32)
} else {
err = binary.Read(r, byteOrder, &addr)
}
return
}
type modulable interface {
toModuledata() moduledata
}