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