-
Notifications
You must be signed in to change notification settings - Fork 8
/
vdso_linux_amd64.go
414 lines (352 loc) · 10.9 KB
/
vdso_linux_amd64.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
package numa
import (
"os"
"unsafe"
)
const (
_AT_NULL = 0 // End of vector
_AT_SYSINFO_EHDR = 33
_PT_LOAD = 1 /* Loadable program segment */
_PT_DYNAMIC = 2 /* Dynamic linking information */
_DT_NULL = 0 /* Marks end of dynamic section */
_DT_HASH = 4 /* Dynamic symbol hash table */
_DT_STRTAB = 5 /* Address of string table */
_DT_SYMTAB = 6 /* Address of symbol table */
_DT_GNU_HASH = 0x6ffffef5 /* GNU-style dynamic symbol hash table */
_DT_VERSYM = 0x6ffffff0
_DT_VERDEF = 0x6ffffffc
_VER_FLG_BASE = 0x1 /* Version definition of file itself */
_SHN_UNDEF = 0 /* Undefined section */
_SHT_DYNSYM = 11 /* Dynamic linker symbol table */
_STT_FUNC = 2 /* Symbol is a code object */
_STT_NOTYPE = 0 /* Symbol type is not specified */
_STB_GLOBAL = 1 /* Global symbol */
_STB_WEAK = 2 /* Weak symbol */
_EI_NIDENT = 16
// vdsoArrayMax is the byte-size of a maximally sized array on this architecture.
// See cmd/compile/internal/amd64/galign.go arch.MAXWIDTH initialization.
vdsoArrayMax = 1<<50 - 1
// Maximum indices for the array types used when traversing the vDSO ELF structures.
// Computed from architecture-specific max provided by vdso_linux_*.go
vdsoSymTabSize = vdsoArrayMax / unsafe.Sizeof(elfSym{})
vdsoDynSize = vdsoArrayMax / unsafe.Sizeof(elfDyn{})
vdsoSymStringsSize = vdsoArrayMax // byte
vdsoVerSymSize = vdsoArrayMax / 2 // uint16
vdsoHashSize = vdsoArrayMax / 4 // uint32
// vdsoBloomSizeScale is a scaling factor for gnuhash tables which are uint32 indexed,
// but contain uintptrs
vdsoBloomSizeScale = unsafe.Sizeof(uintptr(0)) / 4 // uint32
)
type vdsoVersionKey struct {
version string
verHash uint32
}
// ELF64 structure definitions for use by the vDSO loader
type elfSym struct {
st_name uint32
st_info byte
st_other byte
st_shndx uint16
st_value uint64
st_size uint64
}
type elfVerdef struct {
vd_version uint16 /* Version revision */
vd_flags uint16 /* Version information */
vd_ndx uint16 /* Version Index */
vd_cnt uint16 /* Number of associated aux entries */
vd_hash uint32 /* Version name hash value */
vd_aux uint32 /* Offset in bytes to verdaux array */
vd_next uint32 /* Offset in bytes to next verdef entry */
}
type elfEhdr struct {
e_ident [_EI_NIDENT]byte /* Magic number and other info */
e_type uint16 /* Object file type */
e_machine uint16 /* Architecture */
e_version uint32 /* Object file version */
e_entry uint64 /* Entry point virtual address */
e_phoff uint64 /* Program header table file offset */
e_shoff uint64 /* Section header table file offset */
e_flags uint32 /* Processor-specific flags */
e_ehsize uint16 /* ELF header size in bytes */
e_phentsize uint16 /* Program header table entry size */
e_phnum uint16 /* Program header table entry count */
e_shentsize uint16 /* Section header table entry size */
e_shnum uint16 /* Section header table entry count */
e_shstrndx uint16 /* Section header string table index */
}
type elfPhdr struct {
p_type uint32 /* Segment type */
p_flags uint32 /* Segment flags */
p_offset uint64 /* Segment file offset */
p_vaddr uint64 /* Segment virtual address */
p_paddr uint64 /* Segment physical address */
p_filesz uint64 /* Segment size in file */
p_memsz uint64 /* Segment size in memory */
p_align uint64 /* Segment alignment */
}
type elfShdr struct {
sh_name uint32 /* Section name (string tbl index) */
sh_type uint32 /* Section type */
sh_flags uint64 /* Section flags */
sh_addr uint64 /* Section virtual addr at execution */
sh_offset uint64 /* Section file offset */
sh_size uint64 /* Section size in bytes */
sh_link uint32 /* Link to another section */
sh_info uint32 /* Additional section information */
sh_addralign uint64 /* Section alignment */
sh_entsize uint64 /* Entry size if section holds table */
}
type elfDyn struct {
d_tag int64 /* Dynamic entry type */
d_val uint64 /* Integer value */
}
type elfVerdaux struct {
vda_name uint32 /* Version or dependency names */
vda_next uint32 /* Offset in bytes to next verdaux entry */
}
type vdsoInfo struct {
valid bool
/* Load information */
loadAddr unsafe.Pointer
loadOffset unsafe.Pointer /* loadAddr - recorded vaddr */
/* Symbol table */
symtab *[vdsoSymTabSize]elfSym
symstrings *[vdsoSymStringsSize]byte
chain []uint32
bucket []uint32
symOff uint32
isGNUHash bool
/* Version table */
versym *[vdsoVerSymSize]uint16
verdef *elfVerdef
}
var (
vdsoLinuxVersion = vdsoVersionKey{"LINUX_2.6", 0x3ae75f6}
vdsoinfo vdsoInfo
vdsoVersion int32
vdsoGetCPU uintptr
)
/* How to extract and insert information held in the st_info field. */
func _ELF_ST_BIND(val byte) byte { return val >> 4 }
func _ELF_ST_TYPE(val byte) byte { return val & 0xf }
//go:nosplit
func add(p unsafe.Pointer, x uintptr) unsafe.Pointer {
return unsafe.Pointer(uintptr(p) + x)
}
type stringStruct struct {
str unsafe.Pointer
len int
}
const maxAlloc = 1024
func findnull(s *byte) int {
if s == nil {
return 0
}
p := (*[maxAlloc/2/2 - 1]byte)(unsafe.Pointer(s))
l := 0
for p[l] != 0 {
l++
}
return l
}
// Substitute for runtime.gostringnocopy.
func gostringnocopy(str *byte) string {
ss := stringStruct{str: unsafe.Pointer(str), len: findnull(str)}
s := *(*string)(unsafe.Pointer(&ss))
return s
}
func vdsoInitFromSysinfoEhdr(info *vdsoInfo, hdr *elfEhdr) {
info.valid = false
info.loadAddr = unsafe.Pointer(hdr)
pt := unsafe.Pointer(uintptr(info.loadAddr) + uintptr(hdr.e_phoff))
// We need two things from the segment table: the load offset
// and the dynamic table.
var foundVaddr bool
var dyn *[vdsoDynSize]elfDyn
for i := uint16(0); i < hdr.e_phnum; i++ {
pt := (*elfPhdr)(add(pt, uintptr(i)*unsafe.Sizeof(elfPhdr{})))
switch pt.p_type {
case _PT_LOAD:
if !foundVaddr {
foundVaddr = true
info.loadOffset = unsafe.Pointer(uintptr(info.loadAddr) + uintptr(pt.p_offset-pt.p_vaddr))
}
case _PT_DYNAMIC:
dyn = (*[vdsoDynSize]elfDyn)(unsafe.Pointer(uintptr(info.loadAddr) + uintptr(pt.p_offset)))
}
}
if !foundVaddr || dyn == nil {
return // Failed
}
// Fish out the useful bits of the dynamic table.
var hash, gnuhash *[vdsoHashSize]uint32
info.symstrings = nil
info.symtab = nil
info.versym = nil
info.verdef = nil
for i := 0; dyn[i].d_tag != _DT_NULL; i++ {
dt := &dyn[i]
p := unsafe.Pointer(uintptr(info.loadOffset) + uintptr(dt.d_val))
switch dt.d_tag {
case _DT_STRTAB:
info.symstrings = (*[vdsoSymStringsSize]byte)(unsafe.Pointer(p))
case _DT_SYMTAB:
info.symtab = (*[vdsoSymTabSize]elfSym)(unsafe.Pointer(p))
case _DT_HASH:
hash = (*[vdsoHashSize]uint32)(unsafe.Pointer(p))
case _DT_GNU_HASH:
gnuhash = (*[vdsoHashSize]uint32)(unsafe.Pointer(p))
case _DT_VERSYM:
info.versym = (*[vdsoVerSymSize]uint16)(unsafe.Pointer(p))
case _DT_VERDEF:
info.verdef = (*elfVerdef)(unsafe.Pointer(p))
}
}
if info.symstrings == nil || info.symtab == nil || (hash == nil && gnuhash == nil) {
return // Failed
}
if info.verdef == nil {
info.versym = nil
}
if gnuhash != nil {
// Parse the GNU hash table header.
nbucket := gnuhash[0]
info.symOff = gnuhash[1]
bloomSize := gnuhash[2]
info.bucket = gnuhash[4+bloomSize*uint32(vdsoBloomSizeScale):][:nbucket]
info.chain = gnuhash[4+bloomSize*uint32(vdsoBloomSizeScale)+nbucket:]
info.isGNUHash = true
} else {
// Parse the hash table header.
nbucket := hash[0]
nchain := hash[1]
info.bucket = hash[2 : 2+nbucket]
info.chain = hash[2+nbucket : 2+nbucket+nchain]
}
// That's all we need.
info.valid = true
}
func vdsoFindVersion(info *vdsoInfo, ver *vdsoVersionKey) int32 {
if !info.valid {
return 0
}
def := info.verdef
for {
if def.vd_flags&_VER_FLG_BASE == 0 {
aux := (*elfVerdaux)(add(unsafe.Pointer(def), uintptr(def.vd_aux)))
if def.vd_hash == ver.verHash && ver.version == gostringnocopy(&info.symstrings[aux.vda_name]) {
return int32(def.vd_ndx & 0x7fff)
}
}
if def.vd_next == 0 {
break
}
def = (*elfVerdef)(add(unsafe.Pointer(def), uintptr(def.vd_next)))
}
return -1 // cannot match any version
}
func vdsoParseSymbols(name string, info *vdsoInfo, version int32) uintptr {
if !info.valid {
return 0
}
load := func(symIndex uint32, name string) uintptr {
sym := &info.symtab[symIndex]
typ := _ELF_ST_TYPE(sym.st_info)
bind := _ELF_ST_BIND(sym.st_info)
// On ppc64x, VDSO functions are of type _STT_NOTYPE.
if typ != _STT_FUNC && typ != _STT_NOTYPE || bind != _STB_GLOBAL && bind != _STB_WEAK || sym.st_shndx == _SHN_UNDEF {
return 0
}
if name != gostringnocopy(&info.symstrings[sym.st_name]) {
return 0
}
// Check symbol version.
if info.versym != nil && version != 0 && int32(info.versym[symIndex]&0x7fff) != version {
return 0
}
return uintptr(info.loadOffset) + uintptr(sym.st_value)
}
if !info.isGNUHash {
// Old-style DT_HASH table.
hash := elfHash(name)
for chain := info.bucket[hash%uint32(len(info.bucket))]; chain != 0; chain = info.chain[chain] {
if p := load(chain, name); p != 0 {
return p
}
}
return 0
}
// New-style DT_GNU_HASH table.
gnuhash := elfGNUHash(name)
symIndex := info.bucket[gnuhash%uint32(len(info.bucket))]
if symIndex < info.symOff {
return 0
}
for ; ; symIndex++ {
hash := info.chain[symIndex-info.symOff]
if hash|1 == gnuhash|1 {
// Found a hash match.
if p := load(symIndex, name); p != 0 {
return p
}
}
if hash&1 != 0 {
// End of chain.
break
}
}
return 0
}
func elfHash(name string) (h uint32) {
for i := 0; i < len(name); i++ {
h = h<<4 + uint32(name[i])
g := h & 0xf0000000
if g != 0 {
h ^= g >> 24
}
h &= ^g
}
return
}
func elfGNUHash(name string) (h uint32) {
h = 5381
for i := 0; i < len(name); i++ {
h = h*33 + uint32(name[i])
}
return
}
func init() {
fd, err := os.Open("/proc/self/auxv")
if err != nil {
panic(err)
}
var auxv [128]uintptr
n, err := fd.Read((*(*[128 * unsafe.Sizeof(auxv[0])]byte)(unsafe.Pointer(&auxv)))[:])
if err != nil || n <= 0 {
panic(err)
}
var base unsafe.Pointer
auxv[len(auxv)-2] = _AT_NULL // Make sure auxv is terminated, even if we didn't read the whole file.
for i := 0; auxv[i] != _AT_NULL; i += 2 {
tag, val := auxv[i], auxv[i+1]
if tag != _AT_SYSINFO_EHDR || val == 0 {
continue
}
vdsoInitFromSysinfoEhdr(&vdsoinfo, (*elfEhdr)(unsafe.Pointer(uintptr(base)+val)))
}
vdsoVersion = vdsoFindVersion(&vdsoinfo, &vdsoLinuxVersion)
initVDSOAll()
}
func vdsoSym(name string) uintptr {
return vdsoParseSymbols(name, &vdsoinfo, vdsoVersion)
}
func initVDSODefault(name string, def uintptr) uintptr {
if p := vdsoSym(name); p != 0 {
def = p
}
return def
}
func initVDSOAll() {
vdsoGetCPU = initVDSODefault("__vdso_getcpu", 0xffffffffff600800)
}