...
Run Format

Source file src/runtime/vdso_linux.go

Documentation: runtime

     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  // +build linux
     6  // +build 386 amd64 arm arm64
     7  
     8  package runtime
     9  
    10  import "unsafe"
    11  
    12  // Look up symbols in the Linux vDSO.
    13  
    14  // This code was originally based on the sample Linux vDSO parser at
    15  // https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/tools/testing/selftests/vDSO/parse_vdso.c
    16  
    17  // This implements the ELF dynamic linking spec at
    18  // http://sco.com/developers/gabi/latest/ch5.dynamic.html
    19  
    20  // The version section is documented at
    21  // https://refspecs.linuxfoundation.org/LSB_3.2.0/LSB-Core-generic/LSB-Core-generic/symversion.html
    22  
    23  const (
    24  	_AT_SYSINFO_EHDR = 33
    25  
    26  	_PT_LOAD    = 1 /* Loadable program segment */
    27  	_PT_DYNAMIC = 2 /* Dynamic linking information */
    28  
    29  	_DT_NULL     = 0          /* Marks end of dynamic section */
    30  	_DT_HASH     = 4          /* Dynamic symbol hash table */
    31  	_DT_STRTAB   = 5          /* Address of string table */
    32  	_DT_SYMTAB   = 6          /* Address of symbol table */
    33  	_DT_GNU_HASH = 0x6ffffef5 /* GNU-style dynamic symbol hash table */
    34  	_DT_VERSYM   = 0x6ffffff0
    35  	_DT_VERDEF   = 0x6ffffffc
    36  
    37  	_VER_FLG_BASE = 0x1 /* Version definition of file itself */
    38  
    39  	_SHN_UNDEF = 0 /* Undefined section */
    40  
    41  	_SHT_DYNSYM = 11 /* Dynamic linker symbol table */
    42  
    43  	_STT_FUNC = 2 /* Symbol is a code object */
    44  
    45  	_STB_GLOBAL = 1 /* Global symbol */
    46  	_STB_WEAK   = 2 /* Weak symbol */
    47  
    48  	_EI_NIDENT = 16
    49  
    50  	// Maximum indices for the array types used when traversing the vDSO ELF structures.
    51  	// Computed from architecture-specific max provided by vdso_linux_*.go
    52  	vdsoSymTabSize     = vdsoArrayMax / unsafe.Sizeof(elfSym{})
    53  	vdsoDynSize        = vdsoArrayMax / unsafe.Sizeof(elfDyn{})
    54  	vdsoSymStringsSize = vdsoArrayMax     // byte
    55  	vdsoVerSymSize     = vdsoArrayMax / 2 // uint16
    56  	vdsoHashSize       = vdsoArrayMax / 4 // uint32
    57  
    58  	// vdsoBloomSizeScale is a scaling factor for gnuhash tables which are uint32 indexed,
    59  	// but contain uintptrs
    60  	vdsoBloomSizeScale = unsafe.Sizeof(uintptr(0)) / 4 // uint32
    61  )
    62  
    63  /* How to extract and insert information held in the st_info field.  */
    64  func _ELF_ST_BIND(val byte) byte { return val >> 4 }
    65  func _ELF_ST_TYPE(val byte) byte { return val & 0xf }
    66  
    67  type vdsoSymbolKey struct {
    68  	name    string
    69  	symHash uint32
    70  	gnuHash uint32
    71  	ptr     *uintptr
    72  }
    73  
    74  type vdsoVersionKey struct {
    75  	version string
    76  	verHash uint32
    77  }
    78  
    79  type vdsoInfo struct {
    80  	valid bool
    81  
    82  	/* Load information */
    83  	loadAddr   uintptr
    84  	loadOffset uintptr /* loadAddr - recorded vaddr */
    85  
    86  	/* Symbol table */
    87  	symtab     *[vdsoSymTabSize]elfSym
    88  	symstrings *[vdsoSymStringsSize]byte
    89  	chain      []uint32
    90  	bucket     []uint32
    91  	symOff     uint32
    92  	isGNUHash  bool
    93  
    94  	/* Version table */
    95  	versym *[vdsoVerSymSize]uint16
    96  	verdef *elfVerdef
    97  }
    98  
    99  // see vdso_linux_*.go for vdsoSymbolKeys[] and vdso*Sym vars
   100  
   101  func vdsoInitFromSysinfoEhdr(info *vdsoInfo, hdr *elfEhdr) {
   102  	info.valid = false
   103  	info.loadAddr = uintptr(unsafe.Pointer(hdr))
   104  
   105  	pt := unsafe.Pointer(info.loadAddr + uintptr(hdr.e_phoff))
   106  
   107  	// We need two things from the segment table: the load offset
   108  	// and the dynamic table.
   109  	var foundVaddr bool
   110  	var dyn *[vdsoDynSize]elfDyn
   111  	for i := uint16(0); i < hdr.e_phnum; i++ {
   112  		pt := (*elfPhdr)(add(pt, uintptr(i)*unsafe.Sizeof(elfPhdr{})))
   113  		switch pt.p_type {
   114  		case _PT_LOAD:
   115  			if !foundVaddr {
   116  				foundVaddr = true
   117  				info.loadOffset = info.loadAddr + uintptr(pt.p_offset-pt.p_vaddr)
   118  			}
   119  
   120  		case _PT_DYNAMIC:
   121  			dyn = (*[vdsoDynSize]elfDyn)(unsafe.Pointer(info.loadAddr + uintptr(pt.p_offset)))
   122  		}
   123  	}
   124  
   125  	if !foundVaddr || dyn == nil {
   126  		return // Failed
   127  	}
   128  
   129  	// Fish out the useful bits of the dynamic table.
   130  
   131  	var hash, gnuhash *[vdsoHashSize]uint32
   132  	info.symstrings = nil
   133  	info.symtab = nil
   134  	info.versym = nil
   135  	info.verdef = nil
   136  	for i := 0; dyn[i].d_tag != _DT_NULL; i++ {
   137  		dt := &dyn[i]
   138  		p := info.loadOffset + uintptr(dt.d_val)
   139  		switch dt.d_tag {
   140  		case _DT_STRTAB:
   141  			info.symstrings = (*[vdsoSymStringsSize]byte)(unsafe.Pointer(p))
   142  		case _DT_SYMTAB:
   143  			info.symtab = (*[vdsoSymTabSize]elfSym)(unsafe.Pointer(p))
   144  		case _DT_HASH:
   145  			hash = (*[vdsoHashSize]uint32)(unsafe.Pointer(p))
   146  		case _DT_GNU_HASH:
   147  			gnuhash = (*[vdsoHashSize]uint32)(unsafe.Pointer(p))
   148  		case _DT_VERSYM:
   149  			info.versym = (*[vdsoVerSymSize]uint16)(unsafe.Pointer(p))
   150  		case _DT_VERDEF:
   151  			info.verdef = (*elfVerdef)(unsafe.Pointer(p))
   152  		}
   153  	}
   154  
   155  	if info.symstrings == nil || info.symtab == nil || (hash == nil && gnuhash == nil) {
   156  		return // Failed
   157  	}
   158  
   159  	if info.verdef == nil {
   160  		info.versym = nil
   161  	}
   162  
   163  	if gnuhash != nil {
   164  		// Parse the GNU hash table header.
   165  		nbucket := gnuhash[0]
   166  		info.symOff = gnuhash[1]
   167  		bloomSize := gnuhash[2]
   168  		info.bucket = gnuhash[4+bloomSize*uint32(vdsoBloomSizeScale):][:nbucket]
   169  		info.chain = gnuhash[4+bloomSize*uint32(vdsoBloomSizeScale)+nbucket:]
   170  		info.isGNUHash = true
   171  	} else {
   172  		// Parse the hash table header.
   173  		nbucket := hash[0]
   174  		nchain := hash[1]
   175  		info.bucket = hash[2 : 2+nbucket]
   176  		info.chain = hash[2+nbucket : 2+nbucket+nchain]
   177  	}
   178  
   179  	// That's all we need.
   180  	info.valid = true
   181  }
   182  
   183  func vdsoFindVersion(info *vdsoInfo, ver *vdsoVersionKey) int32 {
   184  	if !info.valid {
   185  		return 0
   186  	}
   187  
   188  	def := info.verdef
   189  	for {
   190  		if def.vd_flags&_VER_FLG_BASE == 0 {
   191  			aux := (*elfVerdaux)(add(unsafe.Pointer(def), uintptr(def.vd_aux)))
   192  			if def.vd_hash == ver.verHash && ver.version == gostringnocopy(&info.symstrings[aux.vda_name]) {
   193  				return int32(def.vd_ndx & 0x7fff)
   194  			}
   195  		}
   196  
   197  		if def.vd_next == 0 {
   198  			break
   199  		}
   200  		def = (*elfVerdef)(add(unsafe.Pointer(def), uintptr(def.vd_next)))
   201  	}
   202  
   203  	return -1 // cannot match any version
   204  }
   205  
   206  func vdsoParseSymbols(info *vdsoInfo, version int32) {
   207  	if !info.valid {
   208  		return
   209  	}
   210  
   211  	apply := func(symIndex uint32, k vdsoSymbolKey) bool {
   212  		sym := &info.symtab[symIndex]
   213  		typ := _ELF_ST_TYPE(sym.st_info)
   214  		bind := _ELF_ST_BIND(sym.st_info)
   215  		if typ != _STT_FUNC || bind != _STB_GLOBAL && bind != _STB_WEAK || sym.st_shndx == _SHN_UNDEF {
   216  			return false
   217  		}
   218  		if k.name != gostringnocopy(&info.symstrings[sym.st_name]) {
   219  			return false
   220  		}
   221  		// Check symbol version.
   222  		if info.versym != nil && version != 0 && int32(info.versym[symIndex]&0x7fff) != version {
   223  			return false
   224  		}
   225  
   226  		*k.ptr = info.loadOffset + uintptr(sym.st_value)
   227  		return true
   228  	}
   229  
   230  	if !info.isGNUHash {
   231  		// Old-style DT_HASH table.
   232  		for _, k := range vdsoSymbolKeys {
   233  			for chain := info.bucket[k.symHash%uint32(len(info.bucket))]; chain != 0; chain = info.chain[chain] {
   234  				if apply(chain, k) {
   235  					break
   236  				}
   237  			}
   238  		}
   239  		return
   240  	}
   241  
   242  	// New-style DT_GNU_HASH table.
   243  	for _, k := range vdsoSymbolKeys {
   244  		symIndex := info.bucket[k.gnuHash%uint32(len(info.bucket))]
   245  		if symIndex < info.symOff {
   246  			continue
   247  		}
   248  		for ; ; symIndex++ {
   249  			hash := info.chain[symIndex-info.symOff]
   250  			if hash|1 == k.gnuHash|1 {
   251  				// Found a hash match.
   252  				if apply(symIndex, k) {
   253  					break
   254  				}
   255  			}
   256  			if hash&1 != 0 {
   257  				// End of chain.
   258  				break
   259  			}
   260  		}
   261  	}
   262  }
   263  
   264  func vdsoauxv(tag, val uintptr) {
   265  	switch tag {
   266  	case _AT_SYSINFO_EHDR:
   267  		if val == 0 {
   268  			// Something went wrong
   269  			return
   270  		}
   271  		var info vdsoInfo
   272  		// TODO(rsc): I don't understand why the compiler thinks info escapes
   273  		// when passed to the three functions below.
   274  		info1 := (*vdsoInfo)(noescape(unsafe.Pointer(&info)))
   275  		vdsoInitFromSysinfoEhdr(info1, (*elfEhdr)(unsafe.Pointer(val)))
   276  		vdsoParseSymbols(info1, vdsoFindVersion(info1, &vdsoLinuxVersion))
   277  	}
   278  }
   279  
   280  // vdsoMarker returns whether PC is on the VDSO page.
   281  func inVDSOPage(pc uintptr) bool {
   282  	for _, k := range vdsoSymbolKeys {
   283  		if *k.ptr != 0 {
   284  			page := *k.ptr &^ (physPageSize - 1)
   285  			return pc >= page && pc < page+physPageSize
   286  		}
   287  	}
   288  	return false
   289  }
   290  

View as plain text