| 1 |
// Copyright 2012 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
//go:build linux && (386 || amd64 || arm || arm64 || loong64 || mips64 || mips64le || ppc64 || ppc64le || riscv64 || s390x) |
| 6 |
|
| 7 |
package runtime |
| 8 |
|
| 9 |
import "unsafe" |
| 10 |
|
| 11 |
// Look up symbols in the Linux vDSO. |
| 12 |
|
| 13 |
// This code was originally based on the sample Linux vDSO parser at |
| 14 |
// https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/tools/testing/selftests/vDSO/parse_vdso.c |
| 15 |
|
| 16 |
// This implements the ELF dynamic linking spec at |
| 17 |
// http://sco.com/developers/gabi/latest/ch5.dynamic.html |
| 18 |
|
| 19 |
// The version section is documented at |
| 20 |
// https://refspecs.linuxfoundation.org/LSB_3.2.0/LSB-Core-generic/LSB-Core-generic/symversion.html |
| 21 |
|
| 22 |
const ( |
| 23 |
_AT_SYSINFO_EHDR = 33 |
| 24 |
|
| 25 |
_PT_LOAD = 1 /* Loadable program segment */ |
| 26 |
_PT_DYNAMIC = 2 /* Dynamic linking information */ |
| 27 |
|
| 28 |
_DT_NULL = 0 /* Marks end of dynamic section */ |
| 29 |
_DT_HASH = 4 /* Dynamic symbol hash table */ |
| 30 |
_DT_STRTAB = 5 /* Address of string table */ |
| 31 |
_DT_SYMTAB = 6 /* Address of symbol table */ |
| 32 |
_DT_GNU_HASH = 0x6ffffef5 /* GNU-style dynamic symbol hash table */ |
| 33 |
_DT_VERSYM = 0x6ffffff0 |
| 34 |
_DT_VERDEF = 0x6ffffffc |
| 35 |
|
| 36 |
_VER_FLG_BASE = 0x1 /* Version definition of file itself */ |
| 37 |
|
| 38 |
_SHN_UNDEF = 0 /* Undefined section */ |
| 39 |
|
| 40 |
_SHT_DYNSYM = 11 /* Dynamic linker symbol table */ |
| 41 |
|
| 42 |
_STT_FUNC = 2 /* Symbol is a code object */ |
| 43 |
|
| 44 |
_STT_NOTYPE = 0 /* Symbol type is not specified */ |
| 45 |
|
| 46 |
_STB_GLOBAL = 1 /* Global symbol */ |
| 47 |
_STB_WEAK = 2 /* Weak symbol */ |
| 48 |
|
| 49 |
_EI_NIDENT = 16 |
| 50 |
|
| 51 |
// Maximum indices for the array types used when traversing the vDSO ELF structures. |
| 52 |
// Computed from architecture-specific max provided by vdso_linux_*.go |
| 53 |
vdsoSymTabSize = vdsoArrayMax / unsafe.Sizeof(elfSym{}) |
| 54 |
vdsoDynSize = vdsoArrayMax / unsafe.Sizeof(elfDyn{}) |
| 55 |
vdsoSymStringsSize = vdsoArrayMax // byte |
| 56 |
vdsoVerSymSize = vdsoArrayMax / 2 // uint16 |
| 57 |
vdsoHashSize = vdsoArrayMax / 4 // uint32 |
| 58 |
|
| 59 |
// vdsoBloomSizeScale is a scaling factor for gnuhash tables which are uint32 indexed, |
| 60 |
// but contain uintptrs |
| 61 |
vdsoBloomSizeScale = unsafe.Sizeof(uintptr(0)) / 4 // uint32 |
| 62 |
) |
| 63 |
|
| 64 |
/* How to extract and insert information held in the st_info field. */ |
| 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 |
/* Load information */ |
| 84 |
loadAddr uintptr |
| 85 |
loadOffset uintptr /* loadAddr - recorded vaddr */ |
| 86 |
|
| 87 |
/* Symbol table */ |
| 88 |
symtab *[vdsoSymTabSize]elfSym |
| 89 |
symstrings *[vdsoSymStringsSize]byte |
| 90 |
chain []uint32 |
| 91 |
bucket []uint32 |
| 92 |
symOff uint32 |
| 93 |
isGNUHash bool |
| 94 |
|
| 95 |
/* Version table */ |
| 96 |
versym *[vdsoVerSymSize]uint16 |
| 97 |
verdef *elfVerdef |
| 98 |
} |
| 99 |
|
| 100 |
var vdsoLoadStart, vdsoLoadEnd uintptr |
| 101 |
|
| 102 |
// see vdso_linux_*.go for vdsoSymbolKeys[] and vdso*Sym vars |
| 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 |
// We need two things from the segment table: the load offset |
| 111 |
// and the dynamic table. |
| 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 // Failed |
| 132 |
} |
| 133 |
|
| 134 |
// Fish out the useful bits of the dynamic table. |
| 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 // Failed |
| 162 |
} |
| 163 |
|
| 164 |
if info.verdef == nil { |
| 165 |
info.versym = nil |
| 166 |
} |
| 167 |
|
| 168 |
if gnuhash != nil { |
| 169 |
// Parse the GNU hash table header. |
| 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 |
// Parse the hash table header. |
| 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 |
// That's all we need. |
| 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 // cannot match any version |
| 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 |
// On ppc64x, VDSO functions are of type _STT_NOTYPE. |
| 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 |
// Check symbol version. |
| 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 |
// Old-style DT_HASH table. |
| 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 |
// New-style DT_GNU_HASH table. |
| 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 |
// Found a hash match. |
| 260 |
if apply(symIndex, k) { |
| 261 |
break |
| 262 |
} |
| 263 |
} |
| 264 |
if hash&1 != 0 { |
| 265 |
// End of chain. |
| 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 |
// Something went wrong |
| 277 |
return |
| 278 |
} |
| 279 |
var info vdsoInfo |
| 280 |
// TODO(rsc): I don't understand why the compiler thinks info escapes |
| 281 |
// when passed to the three functions below. |
| 282 |
info1 := (*vdsoInfo)(noescape(unsafe.Pointer(&info))) |
| 283 |
vdsoInitFromSysinfoEhdr(info1, (*elfEhdr)(unsafe.Pointer(val))) |
| 284 |
vdsoParseSymbols(info1, vdsoFindVersion(info1, &vdsoLinuxVersion)) |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
// vdsoMarker reports whether PC is on the VDSO page. |
| 289 |
// |
| 290 |
//go:nosplit |
| 291 |
func inVDSOPage(pc uintptr) bool { |
| 292 |
return pc >= vdsoLoadStart && pc < vdsoLoadEnd |
| 293 |
} |