github.com/filosottile/go@v0.0.0-20170906193555-dbed9972d994/src/runtime/vdso_linux_amd64.go (about)

     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  package runtime
     6  
     7  import "unsafe"
     8  
     9  // Look up symbols in the Linux vDSO.
    10  
    11  // This code was originally based on the sample Linux vDSO parser at
    12  // https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/vDSO/parse_vdso.c
    13  
    14  // This implements the ELF dynamic linking spec at
    15  // http://sco.com/developers/gabi/latest/ch5.dynamic.html
    16  
    17  // The version section is documented at
    18  // http://refspecs.linuxfoundation.org/LSB_3.2.0/LSB-Core-generic/LSB-Core-generic/symversion.html
    19  
    20  const (
    21  	_AT_SYSINFO_EHDR = 33
    22  
    23  	_PT_LOAD    = 1 /* Loadable program segment */
    24  	_PT_DYNAMIC = 2 /* Dynamic linking information */
    25  
    26  	_DT_NULL     = 0          /* Marks end of dynamic section */
    27  	_DT_HASH     = 4          /* Dynamic symbol hash table */
    28  	_DT_STRTAB   = 5          /* Address of string table */
    29  	_DT_SYMTAB   = 6          /* Address of symbol table */
    30  	_DT_GNU_HASH = 0x6ffffef5 /* GNU-style dynamic symbol hash table */
    31  	_DT_VERSYM   = 0x6ffffff0
    32  	_DT_VERDEF   = 0x6ffffffc
    33  
    34  	_VER_FLG_BASE = 0x1 /* Version definition of file itself */
    35  
    36  	_SHN_UNDEF = 0 /* Undefined section */
    37  
    38  	_SHT_DYNSYM = 11 /* Dynamic linker symbol table */
    39  
    40  	_STT_FUNC = 2 /* Symbol is a code object */
    41  
    42  	_STB_GLOBAL = 1 /* Global symbol */
    43  	_STB_WEAK   = 2 /* Weak symbol */
    44  
    45  	_EI_NIDENT = 16
    46  )
    47  
    48  /* How to extract and insert information held in the st_info field.  */
    49  func _ELF64_ST_BIND(val byte) byte { return val >> 4 }
    50  func _ELF64_ST_TYPE(val byte) byte { return val & 0xf }
    51  
    52  type elf64Sym struct {
    53  	st_name  uint32
    54  	st_info  byte
    55  	st_other byte
    56  	st_shndx uint16
    57  	st_value uint64
    58  	st_size  uint64
    59  }
    60  
    61  type elf64Verdef struct {
    62  	vd_version uint16 /* Version revision */
    63  	vd_flags   uint16 /* Version information */
    64  	vd_ndx     uint16 /* Version Index */
    65  	vd_cnt     uint16 /* Number of associated aux entries */
    66  	vd_hash    uint32 /* Version name hash value */
    67  	vd_aux     uint32 /* Offset in bytes to verdaux array */
    68  	vd_next    uint32 /* Offset in bytes to next verdef entry */
    69  }
    70  
    71  type elf64Ehdr struct {
    72  	e_ident     [_EI_NIDENT]byte /* Magic number and other info */
    73  	e_type      uint16           /* Object file type */
    74  	e_machine   uint16           /* Architecture */
    75  	e_version   uint32           /* Object file version */
    76  	e_entry     uint64           /* Entry point virtual address */
    77  	e_phoff     uint64           /* Program header table file offset */
    78  	e_shoff     uint64           /* Section header table file offset */
    79  	e_flags     uint32           /* Processor-specific flags */
    80  	e_ehsize    uint16           /* ELF header size in bytes */
    81  	e_phentsize uint16           /* Program header table entry size */
    82  	e_phnum     uint16           /* Program header table entry count */
    83  	e_shentsize uint16           /* Section header table entry size */
    84  	e_shnum     uint16           /* Section header table entry count */
    85  	e_shstrndx  uint16           /* Section header string table index */
    86  }
    87  
    88  type elf64Phdr struct {
    89  	p_type   uint32 /* Segment type */
    90  	p_flags  uint32 /* Segment flags */
    91  	p_offset uint64 /* Segment file offset */
    92  	p_vaddr  uint64 /* Segment virtual address */
    93  	p_paddr  uint64 /* Segment physical address */
    94  	p_filesz uint64 /* Segment size in file */
    95  	p_memsz  uint64 /* Segment size in memory */
    96  	p_align  uint64 /* Segment alignment */
    97  }
    98  
    99  type elf64Shdr struct {
   100  	sh_name      uint32 /* Section name (string tbl index) */
   101  	sh_type      uint32 /* Section type */
   102  	sh_flags     uint64 /* Section flags */
   103  	sh_addr      uint64 /* Section virtual addr at execution */
   104  	sh_offset    uint64 /* Section file offset */
   105  	sh_size      uint64 /* Section size in bytes */
   106  	sh_link      uint32 /* Link to another section */
   107  	sh_info      uint32 /* Additional section information */
   108  	sh_addralign uint64 /* Section alignment */
   109  	sh_entsize   uint64 /* Entry size if section holds table */
   110  }
   111  
   112  type elf64Dyn struct {
   113  	d_tag int64  /* Dynamic entry type */
   114  	d_val uint64 /* Integer value */
   115  }
   116  
   117  type elf64Verdaux struct {
   118  	vda_name uint32 /* Version or dependency names */
   119  	vda_next uint32 /* Offset in bytes to next verdaux entry */
   120  }
   121  
   122  type elf64Auxv struct {
   123  	a_type uint64 /* Entry type */
   124  	a_val  uint64 /* Integer value */
   125  }
   126  
   127  type symbol_key struct {
   128  	name     string
   129  	sym_hash uint32
   130  	gnu_hash uint32
   131  	ptr      *uintptr
   132  }
   133  
   134  type version_key struct {
   135  	version  string
   136  	ver_hash uint32
   137  }
   138  
   139  type vdso_info struct {
   140  	valid bool
   141  
   142  	/* Load information */
   143  	load_addr   uintptr
   144  	load_offset uintptr /* load_addr - recorded vaddr */
   145  
   146  	/* Symbol table */
   147  	symtab     *[1 << 32]elf64Sym
   148  	symstrings *[1 << 32]byte
   149  	chain      []uint32
   150  	bucket     []uint32
   151  	symOff     uint32
   152  	isGNUHash  bool
   153  
   154  	/* Version table */
   155  	versym *[1 << 32]uint16
   156  	verdef *elf64Verdef
   157  }
   158  
   159  var linux26 = version_key{"LINUX_2.6", 0x3ae75f6}
   160  
   161  var sym_keys = []symbol_key{
   162  	{"__vdso_time", 0xa33c485, 0x821e8e0d, &__vdso_time_sym},
   163  	{"__vdso_gettimeofday", 0x315ca59, 0xb01bca00, &__vdso_gettimeofday_sym},
   164  	{"__vdso_clock_gettime", 0xd35ec75, 0x6e43a318, &__vdso_clock_gettime_sym},
   165  }
   166  
   167  // initialize with vsyscall fallbacks
   168  var (
   169  	__vdso_time_sym          uintptr = 0xffffffffff600400
   170  	__vdso_gettimeofday_sym  uintptr = 0xffffffffff600000
   171  	__vdso_clock_gettime_sym uintptr = 0
   172  )
   173  
   174  func vdso_init_from_sysinfo_ehdr(info *vdso_info, hdr *elf64Ehdr) {
   175  	info.valid = false
   176  	info.load_addr = uintptr(unsafe.Pointer(hdr))
   177  
   178  	pt := unsafe.Pointer(info.load_addr + uintptr(hdr.e_phoff))
   179  
   180  	// We need two things from the segment table: the load offset
   181  	// and the dynamic table.
   182  	var found_vaddr bool
   183  	var dyn *[1 << 20]elf64Dyn
   184  	for i := uint16(0); i < hdr.e_phnum; i++ {
   185  		pt := (*elf64Phdr)(add(pt, uintptr(i)*unsafe.Sizeof(elf64Phdr{})))
   186  		switch pt.p_type {
   187  		case _PT_LOAD:
   188  			if !found_vaddr {
   189  				found_vaddr = true
   190  				info.load_offset = info.load_addr + uintptr(pt.p_offset-pt.p_vaddr)
   191  			}
   192  
   193  		case _PT_DYNAMIC:
   194  			dyn = (*[1 << 20]elf64Dyn)(unsafe.Pointer(info.load_addr + uintptr(pt.p_offset)))
   195  		}
   196  	}
   197  
   198  	if !found_vaddr || dyn == nil {
   199  		return // Failed
   200  	}
   201  
   202  	// Fish out the useful bits of the dynamic table.
   203  
   204  	var hash, gnuhash *[1 << 30]uint32
   205  	info.symstrings = nil
   206  	info.symtab = nil
   207  	info.versym = nil
   208  	info.verdef = nil
   209  	for i := 0; dyn[i].d_tag != _DT_NULL; i++ {
   210  		dt := &dyn[i]
   211  		p := info.load_offset + uintptr(dt.d_val)
   212  		switch dt.d_tag {
   213  		case _DT_STRTAB:
   214  			info.symstrings = (*[1 << 32]byte)(unsafe.Pointer(p))
   215  		case _DT_SYMTAB:
   216  			info.symtab = (*[1 << 32]elf64Sym)(unsafe.Pointer(p))
   217  		case _DT_HASH:
   218  			hash = (*[1 << 30]uint32)(unsafe.Pointer(p))
   219  		case _DT_GNU_HASH:
   220  			gnuhash = (*[1 << 30]uint32)(unsafe.Pointer(p))
   221  		case _DT_VERSYM:
   222  			info.versym = (*[1 << 32]uint16)(unsafe.Pointer(p))
   223  		case _DT_VERDEF:
   224  			info.verdef = (*elf64Verdef)(unsafe.Pointer(p))
   225  		}
   226  	}
   227  
   228  	if info.symstrings == nil || info.symtab == nil || (hash == nil && gnuhash == nil) {
   229  		return // Failed
   230  	}
   231  
   232  	if info.verdef == nil {
   233  		info.versym = nil
   234  	}
   235  
   236  	if gnuhash != nil {
   237  		// Parse the GNU hash table header.
   238  		nbucket := gnuhash[0]
   239  		info.symOff = gnuhash[1]
   240  		bloomSize := gnuhash[2]
   241  		info.bucket = gnuhash[4+bloomSize*2:][:nbucket]
   242  		info.chain = gnuhash[4+bloomSize*2+nbucket:]
   243  		info.isGNUHash = true
   244  	} else {
   245  		// Parse the hash table header.
   246  		nbucket := hash[0]
   247  		nchain := hash[1]
   248  		info.bucket = hash[2 : 2+nbucket]
   249  		info.chain = hash[2+nbucket : 2+nbucket+nchain]
   250  	}
   251  
   252  	// That's all we need.
   253  	info.valid = true
   254  }
   255  
   256  func vdso_find_version(info *vdso_info, ver *version_key) int32 {
   257  	if !info.valid {
   258  		return 0
   259  	}
   260  
   261  	def := info.verdef
   262  	for {
   263  		if def.vd_flags&_VER_FLG_BASE == 0 {
   264  			aux := (*elf64Verdaux)(add(unsafe.Pointer(def), uintptr(def.vd_aux)))
   265  			if def.vd_hash == ver.ver_hash && ver.version == gostringnocopy(&info.symstrings[aux.vda_name]) {
   266  				return int32(def.vd_ndx & 0x7fff)
   267  			}
   268  		}
   269  
   270  		if def.vd_next == 0 {
   271  			break
   272  		}
   273  		def = (*elf64Verdef)(add(unsafe.Pointer(def), uintptr(def.vd_next)))
   274  	}
   275  
   276  	return -1 // cannot match any version
   277  }
   278  
   279  func vdso_parse_symbols(info *vdso_info, version int32) {
   280  	if !info.valid {
   281  		return
   282  	}
   283  
   284  	apply := func(symIndex uint32, k symbol_key) bool {
   285  		sym := &info.symtab[symIndex]
   286  		typ := _ELF64_ST_TYPE(sym.st_info)
   287  		bind := _ELF64_ST_BIND(sym.st_info)
   288  		if typ != _STT_FUNC || bind != _STB_GLOBAL && bind != _STB_WEAK || sym.st_shndx == _SHN_UNDEF {
   289  			return false
   290  		}
   291  		if k.name != gostringnocopy(&info.symstrings[sym.st_name]) {
   292  			return false
   293  		}
   294  
   295  		// Check symbol version.
   296  		if info.versym != nil && version != 0 && int32(info.versym[symIndex]&0x7fff) != version {
   297  			return false
   298  		}
   299  
   300  		*k.ptr = info.load_offset + uintptr(sym.st_value)
   301  		return true
   302  	}
   303  
   304  	if !info.isGNUHash {
   305  		// Old-style DT_HASH table.
   306  		for _, k := range sym_keys {
   307  			for chain := info.bucket[k.sym_hash%uint32(len(info.bucket))]; chain != 0; chain = info.chain[chain] {
   308  				if apply(chain, k) {
   309  					break
   310  				}
   311  			}
   312  		}
   313  		return
   314  	}
   315  
   316  	// New-style DT_GNU_HASH table.
   317  	for _, k := range sym_keys {
   318  		symIndex := info.bucket[k.gnu_hash%uint32(len(info.bucket))]
   319  		if symIndex < info.symOff {
   320  			continue
   321  		}
   322  		for ; ; symIndex++ {
   323  			hash := info.chain[symIndex-info.symOff]
   324  			if hash|1 == k.gnu_hash|1 {
   325  				// Found a hash match.
   326  				if apply(symIndex, k) {
   327  					break
   328  				}
   329  			}
   330  			if hash&1 != 0 {
   331  				// End of chain.
   332  				break
   333  			}
   334  		}
   335  	}
   336  }
   337  
   338  func archauxv(tag, val uintptr) {
   339  	switch tag {
   340  	case _AT_SYSINFO_EHDR:
   341  		if val == 0 {
   342  			// Something went wrong
   343  			return
   344  		}
   345  		var info vdso_info
   346  		// TODO(rsc): I don't understand why the compiler thinks info escapes
   347  		// when passed to the three functions below.
   348  		info1 := (*vdso_info)(noescape(unsafe.Pointer(&info)))
   349  		vdso_init_from_sysinfo_ehdr(info1, (*elf64Ehdr)(unsafe.Pointer(val)))
   350  		vdso_parse_symbols(info1, vdso_find_version(info1, &linux26))
   351  	}
   352  }