Source file
src/runtime/vdso_linux.go
1
2
3
4
5
6
7 package runtime
8
9 import "unsafe"
10
11
12
13
14
15
16
17
18
19
20
21
22 const (
23 _AT_SYSINFO_EHDR = 33
24
25 _PT_LOAD = 1
26 _PT_DYNAMIC = 2
27
28 _DT_NULL = 0
29 _DT_HASH = 4
30 _DT_STRTAB = 5
31 _DT_SYMTAB = 6
32 _DT_GNU_HASH = 0x6ffffef5
33 _DT_VERSYM = 0x6ffffff0
34 _DT_VERDEF = 0x6ffffffc
35
36 _VER_FLG_BASE = 0x1
37
38 _SHN_UNDEF = 0
39
40 _SHT_DYNSYM = 11
41
42 _STT_FUNC = 2
43
44 _STT_NOTYPE = 0
45
46 _STB_GLOBAL = 1
47 _STB_WEAK = 2
48
49 _EI_NIDENT = 16
50
51
52
53 vdsoSymTabSize = vdsoArrayMax / unsafe.Sizeof(elfSym{})
54 vdsoDynSize = vdsoArrayMax / unsafe.Sizeof(elfDyn{})
55 vdsoSymStringsSize = vdsoArrayMax
56 vdsoVerSymSize = vdsoArrayMax / 2
57 vdsoHashSize = vdsoArrayMax / 4
58
59
60
61 vdsoBloomSizeScale = unsafe.Sizeof(uintptr(0)) / 4
62 )
63
64
65 func _ELF_ST_BIND(val byte) byte { return val >> 4 }
66 func _ELF_ST_TYPE(val byte) byte { return val & 0xf }
67
68 type vdsoSymbolKey struct {
69 name string
70 symHash uint32
71 gnuHash uint32
72 ptr *uintptr
73 }
74
75 type vdsoVersionKey struct {
76 version string
77 verHash uint32
78 }
79
80 type vdsoInfo struct {
81 valid bool
82
83
84 loadAddr uintptr
85 loadOffset uintptr
86
87
88 symtab *[vdsoSymTabSize]elfSym
89 symstrings *[vdsoSymStringsSize]byte
90 chain []uint32
91 bucket []uint32
92 symOff uint32
93 isGNUHash bool
94
95
96 versym *[vdsoVerSymSize]uint16
97 verdef *elfVerdef
98 }
99
100 var vdsoLoadStart, vdsoLoadEnd uintptr
101
102
103
104 func vdsoInitFromSysinfoEhdr(info *vdsoInfo, hdr *elfEhdr) {
105 info.valid = false
106 info.loadAddr = uintptr(unsafe.Pointer(hdr))
107
108 pt := unsafe.Pointer(info.loadAddr + uintptr(hdr.e_phoff))
109
110
111
112 var foundVaddr bool
113 var dyn *[vdsoDynSize]elfDyn
114 for i := uint16(0); i < hdr.e_phnum; i++ {
115 pt := (*elfPhdr)(add(pt, uintptr(i)*unsafe.Sizeof(elfPhdr{})))
116 switch pt.p_type {
117 case _PT_LOAD:
118 if !foundVaddr {
119 foundVaddr = true
120 info.loadOffset = info.loadAddr + uintptr(pt.p_offset-pt.p_vaddr)
121 vdsoLoadStart = info.loadOffset
122 vdsoLoadEnd = info.loadOffset + uintptr(pt.p_memsz)
123 }
124
125 case _PT_DYNAMIC:
126 dyn = (*[vdsoDynSize]elfDyn)(unsafe.Pointer(info.loadAddr + uintptr(pt.p_offset)))
127 }
128 }
129
130 if !foundVaddr || dyn == nil {
131 return
132 }
133
134
135
136 var hash, gnuhash *[vdsoHashSize]uint32
137 info.symstrings = nil
138 info.symtab = nil
139 info.versym = nil
140 info.verdef = nil
141 for i := 0; dyn[i].d_tag != _DT_NULL; i++ {
142 dt := &dyn[i]
143 p := info.loadOffset + uintptr(dt.d_val)
144 switch dt.d_tag {
145 case _DT_STRTAB:
146 info.symstrings = (*[vdsoSymStringsSize]byte)(unsafe.Pointer(p))
147 case _DT_SYMTAB:
148 info.symtab = (*[vdsoSymTabSize]elfSym)(unsafe.Pointer(p))
149 case _DT_HASH:
150 hash = (*[vdsoHashSize]uint32)(unsafe.Pointer(p))
151 case _DT_GNU_HASH:
152 gnuhash = (*[vdsoHashSize]uint32)(unsafe.Pointer(p))
153 case _DT_VERSYM:
154 info.versym = (*[vdsoVerSymSize]uint16)(unsafe.Pointer(p))
155 case _DT_VERDEF:
156 info.verdef = (*elfVerdef)(unsafe.Pointer(p))
157 }
158 }
159
160 if info.symstrings == nil || info.symtab == nil || (hash == nil && gnuhash == nil) {
161 return
162 }
163
164 if info.verdef == nil {
165 info.versym = nil
166 }
167
168 if gnuhash != nil {
169
170 nbucket := gnuhash[0]
171 info.symOff = gnuhash[1]
172 bloomSize := gnuhash[2]
173 info.bucket = gnuhash[4+bloomSize*uint32(vdsoBloomSizeScale):][:nbucket]
174 info.chain = gnuhash[4+bloomSize*uint32(vdsoBloomSizeScale)+nbucket:]
175 info.isGNUHash = true
176 } else {
177
178 nbucket := hash[0]
179 nchain := hash[1]
180 info.bucket = hash[2 : 2+nbucket]
181 info.chain = hash[2+nbucket : 2+nbucket+nchain]
182 }
183
184
185 info.valid = true
186 }
187
188 func vdsoFindVersion(info *vdsoInfo, ver *vdsoVersionKey) int32 {
189 if !info.valid {
190 return 0
191 }
192
193 def := info.verdef
194 for {
195 if def.vd_flags&_VER_FLG_BASE == 0 {
196 aux := (*elfVerdaux)(add(unsafe.Pointer(def), uintptr(def.vd_aux)))
197 if def.vd_hash == ver.verHash && ver.version == gostringnocopy(&info.symstrings[aux.vda_name]) {
198 return int32(def.vd_ndx & 0x7fff)
199 }
200 }
201
202 if def.vd_next == 0 {
203 break
204 }
205 def = (*elfVerdef)(add(unsafe.Pointer(def), uintptr(def.vd_next)))
206 }
207
208 return -1
209 }
210
211 func vdsoParseSymbols(info *vdsoInfo, version int32) {
212 if !info.valid {
213 return
214 }
215
216 apply := func(symIndex uint32, k vdsoSymbolKey) bool {
217 sym := &info.symtab[symIndex]
218 typ := _ELF_ST_TYPE(sym.st_info)
219 bind := _ELF_ST_BIND(sym.st_info)
220
221 if typ != _STT_FUNC && typ != _STT_NOTYPE || bind != _STB_GLOBAL && bind != _STB_WEAK || sym.st_shndx == _SHN_UNDEF {
222 return false
223 }
224 if k.name != gostringnocopy(&info.symstrings[sym.st_name]) {
225 return false
226 }
227
228 if info.versym != nil && version != 0 && int32(info.versym[symIndex]&0x7fff) != version {
229 return false
230 }
231
232 *k.ptr = info.loadOffset + uintptr(sym.st_value)
233 return true
234 }
235
236 if !info.isGNUHash {
237
238 for _, k := range vdsoSymbolKeys {
239 if len(info.bucket) > 0 {
240 for chain := info.bucket[k.symHash%uint32(len(info.bucket))]; chain != 0; chain = info.chain[chain] {
241 if apply(chain, k) {
242 break
243 }
244 }
245 }
246 }
247 return
248 }
249
250
251 for _, k := range vdsoSymbolKeys {
252 symIndex := info.bucket[k.gnuHash%uint32(len(info.bucket))]
253 if symIndex < info.symOff {
254 continue
255 }
256 for ; ; symIndex++ {
257 hash := info.chain[symIndex-info.symOff]
258 if hash|1 == k.gnuHash|1 {
259
260 if apply(symIndex, k) {
261 break
262 }
263 }
264 if hash&1 != 0 {
265
266 break
267 }
268 }
269 }
270 }
271
272 func vdsoauxv(tag, val uintptr) {
273 switch tag {
274 case _AT_SYSINFO_EHDR:
275 if val == 0 {
276
277 return
278 }
279 var info vdsoInfo
280
281
282 info1 := (*vdsoInfo)(noescape(unsafe.Pointer(&info)))
283 vdsoInitFromSysinfoEhdr(info1, (*elfEhdr)(unsafe.Pointer(val)))
284 vdsoParseSymbols(info1, vdsoFindVersion(info1, &vdsoLinuxVersion))
285 }
286 }
287
288
289
290
291 func inVDSOPage(pc uintptr) bool {
292 return pc >= vdsoLoadStart && pc < vdsoLoadEnd
293 }
294
View as plain text