]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: common auxv parser
authorAustin Clements <austin@google.com>
Thu, 14 Apr 2016 16:12:45 +0000 (12:12 -0400)
committerAustin Clements <austin@google.com>
Sat, 16 Apr 2016 21:42:27 +0000 (21:42 +0000)
Currently several different Linux architectures have separate copies
of the auxv parser. Bring these all together into a single copy of the
parser that calls out to a per-arch handler for each tag/value pair.
This is in preparation for handling common auxv tags in one place.

For #9993.

Change-Id: Iceebc3afad6b4133b70fca7003561ae370445c10
Reviewed-on: https://go-review.googlesource.com/22061
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Hudson-Doyle <michael.hudson@canonical.com>
src/runtime/os_linux.go
src/runtime/os_linux_386.go
src/runtime/os_linux_arm.go
src/runtime/os_linux_arm64.go
src/runtime/os_linux_noauxv.go [new file with mode: 0644]
src/runtime/vdso_linux_amd64.go
src/runtime/vdso_none.go

index 7d8cc7e5c4c39b2c92038b9511986aac1e73ce0d..eeb30c7dd93c51728e1be6f938797bfa298af9eb 100644 (file)
@@ -176,6 +176,29 @@ func newosproc0(stacksize uintptr, fn unsafe.Pointer) {
 var failallocatestack = []byte("runtime: failed to allocate stack for the new OS thread\n")
 var failthreadcreate = []byte("runtime: failed to create new OS thread\n")
 
+const (
+       _AT_NULL = 0 // End of vector
+)
+
+func sysargs(argc int32, argv **byte) {
+       n := argc + 1
+
+       // skip over argv, envp to get to auxv
+       for argv_index(argv, n) != nil {
+               n++
+       }
+
+       // skip NULL separator
+       n++
+
+       // now argv+n is auxv
+       auxv := (*[1 << 28]uintptr)(add(unsafe.Pointer(argv), uintptr(n)*sys.PtrSize))
+       for i := 0; auxv[i] != _AT_NULL; i += 2 {
+               tag, val := auxv[i], auxv[i+1]
+               archauxv(tag, val)
+       }
+}
+
 func osinit() {
        ncpu = getproccount()
 }
index 0f39cade3b864f348651380188ba8772bc6d7b52..2383d962b2971c943bf233ecc5b80e810b5ab671 100644 (file)
@@ -4,30 +4,16 @@
 
 package runtime
 
-import (
-       "runtime/internal/sys"
-       "unsafe"
-)
+import "unsafe"
 
 const (
-       _AT_NULL    = 0
        _AT_RANDOM  = 25
        _AT_SYSINFO = 32
 )
 
-func sysargs(argc int32, argv **byte) {
-       // skip over argv, envv to get to auxv
-       n := argc + 1
-       for argv_index(argv, n) != nil {
-               n++
-       }
-       n++
-       auxv := (*[1 << 28]uint32)(add(unsafe.Pointer(argv), uintptr(n)*sys.PtrSize))
-
-       for i := 0; auxv[i] != _AT_NULL; i += 2 {
-               switch auxv[i] {
-               case _AT_RANDOM:
-                       startupRandomData = (*[16]byte)(unsafe.Pointer(uintptr(auxv[i+1])))[:]
-               }
+func archauxv(tag, val uintptr) {
+       switch tag {
+       case _AT_RANDOM:
+               startupRandomData = (*[16]byte)(unsafe.Pointer(val))[:]
        }
 }
index 8fdfb585ba6e03be084c8db432d484791712098f..a61be916b6ded9a3327d44f6cde32908228ff613 100644 (file)
@@ -4,13 +4,9 @@
 
 package runtime
 
-import (
-       "runtime/internal/sys"
-       "unsafe"
-)
+import "unsafe"
 
 const (
-       _AT_NULL     = 0
        _AT_PLATFORM = 15 //  introduced in at least 2.6.11
        _AT_HWCAP    = 16 // introduced in at least 2.6.11
        _AT_RANDOM   = 25 // introduced in 2.6.29
@@ -36,33 +32,23 @@ func checkgoarm() {
        }
 }
 
-func sysargs(argc int32, argv **byte) {
-       // skip over argv, envv to get to auxv
-       n := argc + 1
-       for argv_index(argv, n) != nil {
-               n++
-       }
-       n++
-       auxv := (*[1 << 28]uint32)(add(unsafe.Pointer(argv), uintptr(n)*sys.PtrSize))
-
-       for i := 0; auxv[i] != _AT_NULL; i += 2 {
-               switch auxv[i] {
-               case _AT_RANDOM: // kernel provides a pointer to 16-bytes worth of random data
-                       startupRandomData = (*[16]byte)(unsafe.Pointer(uintptr(auxv[i+1])))[:]
-                       // the pointer provided may not be word aligned, so we must treat it
-                       // as a byte array.
-                       randomNumber = uint32(startupRandomData[4]) | uint32(startupRandomData[5])<<8 |
-                               uint32(startupRandomData[6])<<16 | uint32(startupRandomData[7])<<24
-
-               case _AT_PLATFORM: // v5l, v6l, v7l
-                       t := *(*uint8)(unsafe.Pointer(uintptr(auxv[i+1] + 1)))
-                       if '5' <= t && t <= '7' {
-                               armArch = t - '0'
-                       }
-
-               case _AT_HWCAP: // CPU capability bit flags
-                       hwcap = auxv[i+1]
+func archauxv(tag, val uintptr) {
+       switch tag {
+       case _AT_RANDOM: // kernel provides a pointer to 16-bytes worth of random data
+               startupRandomData = (*[16]byte)(unsafe.Pointer(val))[:]
+               // the pointer provided may not be word aligned, so we must treat it
+               // as a byte array.
+               randomNumber = uint32(startupRandomData[4]) | uint32(startupRandomData[5])<<8 |
+                       uint32(startupRandomData[6])<<16 | uint32(startupRandomData[7])<<24
+
+       case _AT_PLATFORM: // v5l, v6l, v7l
+               t := *(*uint8)(unsafe.Pointer(val + 1))
+               if '5' <= t && t <= '7' {
+                       armArch = t - '0'
                }
+
+       case _AT_HWCAP: // CPU capability bit flags
+               hwcap = uint32(val)
        }
 }
 
index 57184b0d3a442fa18a3a1d4f9539c0e5cd756f12..aa9d4d9885b13aa129cc2c2207672c577afe72e5 100644 (file)
@@ -4,36 +4,22 @@
 
 package runtime
 
-import (
-       "runtime/internal/sys"
-       "unsafe"
-)
+import "unsafe"
 
 const (
-       _AT_NULL   = 0
        _AT_RANDOM = 25 // introduced in 2.6.29
 )
 
 var randomNumber uint32
 
-func sysargs(argc int32, argv **byte) {
-       // skip over argv, envv to get to auxv
-       n := argc + 1
-       for argv_index(argv, n) != nil {
-               n++
-       }
-       n++
-       auxv := (*[1 << 29]uint64)(add(unsafe.Pointer(argv), uintptr(n)*sys.PtrSize))
-
-       for i := 0; auxv[i] != _AT_NULL; i += 2 {
-               switch auxv[i] {
-               case _AT_RANDOM: // kernel provides a pointer to 16-bytes worth of random data
-                       startupRandomData = (*[16]byte)(unsafe.Pointer(uintptr(auxv[i+1])))[:]
-                       // the pointer provided may not be word aligned, so we must treat it
-                       // as a byte array.
-                       randomNumber = uint32(startupRandomData[4]) | uint32(startupRandomData[5])<<8 |
-                               uint32(startupRandomData[6])<<16 | uint32(startupRandomData[7])<<24
-               }
+func archauxv(tag, val uintptr) {
+       switch tag {
+       case _AT_RANDOM: // kernel provides a pointer to 16-bytes worth of random data
+               startupRandomData = (*[16]byte)(unsafe.Pointer(val))[:]
+               // the pointer provided may not be word aligned, so we must treat it
+               // as a byte array.
+               randomNumber = uint32(startupRandomData[4]) | uint32(startupRandomData[5])<<8 |
+                       uint32(startupRandomData[6])<<16 | uint32(startupRandomData[7])<<24
        }
 }
 
diff --git a/src/runtime/os_linux_noauxv.go b/src/runtime/os_linux_noauxv.go
new file mode 100644 (file)
index 0000000..d26c85b
--- /dev/null
@@ -0,0 +1,10 @@
+// Copyright 2014 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build !386,!amd64,!arm,!arm64
+
+package runtime
+
+func archauxv(tag, val uintptr) {
+}
index 42571e063cd3f86e2c98d4bc5e6830ba4b265c16..1aae9b65703f4fe7756b10e07374da5a8838fb76 100644 (file)
@@ -4,10 +4,7 @@
 
 package runtime
 
-import (
-       "runtime/internal/sys"
-       "unsafe"
-)
+import "unsafe"
 
 // Look up symbols in the Linux vDSO.
 
@@ -23,7 +20,6 @@ import (
 const (
        _AT_RANDOM       = 25
        _AT_SYSINFO_EHDR = 33
-       _AT_NULL         = 0 /* End of vector */
 
        _PT_LOAD    = 1 /* Loadable program segment */
        _PT_DYNAMIC = 2 /* Dynamic linking information */
@@ -294,37 +290,21 @@ func vdso_parse_symbols(info *vdso_info, version int32) {
        }
 }
 
-func sysargs(argc int32, argv **byte) {
-       n := argc + 1
-
-       // skip envp to get to ELF auxiliary vector.
-       for argv_index(argv, n) != nil {
-               n++
-       }
-
-       // skip NULL separator
-       n++
-
-       // now argv+n is auxv
-       auxv := (*[1 << 32]elf64Auxv)(add(unsafe.Pointer(argv), uintptr(n)*sys.PtrSize))
-
-       for i := 0; auxv[i].a_type != _AT_NULL; i++ {
-               av := &auxv[i]
-               switch av.a_type {
-               case _AT_SYSINFO_EHDR:
-                       if av.a_val == 0 {
-                               // Something went wrong
-                               continue
-                       }
-                       var info vdso_info
-                       // TODO(rsc): I don't understand why the compiler thinks info escapes
-                       // when passed to the three functions below.
-                       info1 := (*vdso_info)(noescape(unsafe.Pointer(&info)))
-                       vdso_init_from_sysinfo_ehdr(info1, (*elf64Ehdr)(unsafe.Pointer(uintptr(av.a_val))))
-                       vdso_parse_symbols(info1, vdso_find_version(info1, &linux26))
-
-               case _AT_RANDOM:
-                       startupRandomData = (*[16]byte)(unsafe.Pointer(uintptr(av.a_val)))[:]
+func archauxv(tag, val uintptr) {
+       switch tag {
+       case _AT_SYSINFO_EHDR:
+               if val == 0 {
+                       // Something went wrong
+                       return
                }
+               var info vdso_info
+               // TODO(rsc): I don't understand why the compiler thinks info escapes
+               // when passed to the three functions below.
+               info1 := (*vdso_info)(noescape(unsafe.Pointer(&info)))
+               vdso_init_from_sysinfo_ehdr(info1, (*elf64Ehdr)(unsafe.Pointer(val)))
+               vdso_parse_symbols(info1, vdso_find_version(info1, &linux26))
+
+       case _AT_RANDOM:
+               startupRandomData = (*[16]byte)(unsafe.Pointer(val))[:]
        }
 }
index e14e1a470791e641303b598223fe902a91c8af57..efae23f6eeb907821425f54dc78f49d1b9c97cb5 100644 (file)
@@ -2,10 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build !linux !amd64
-// +build !linux !386
-// +build !linux !arm
-// +build !linux !arm64
+// +build !linux
 
 package runtime