Skip to content

Commit 4d4692e

Browse files
florianlgopherbot
authored andcommitted
unix: add Auxv
Fixes golang/go#67839 Change-Id: I3af38d21159f7cac3786b49ac17657d314fbc178 Reviewed-on: https://go-review.googlesource.com/c/sys/+/644295 Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Carlos Amedee <carlos@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com>
1 parent b215a1c commit 4d4692e

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

unix/auxv.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2025 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 go1.21 && (aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || zos)
6+
7+
package unix
8+
9+
import (
10+
"syscall"
11+
"unsafe"
12+
)
13+
14+
//go:linkname runtime_getAuxv runtime.getAuxv
15+
func runtime_getAuxv() []uintptr
16+
17+
// Auxv returns the ELF auxiliary vector as a sequence of key/value pairs.
18+
// The returned slice is always a fresh copy, owned by the caller.
19+
// It returns an error on non-ELF platforms, or if the auxiliary vector cannot be accessed,
20+
// which happens in some locked-down environments and build modes.
21+
func Auxv() ([][2]uintptr, error) {
22+
vec := runtime_getAuxv()
23+
vecLen := len(vec)
24+
25+
if vecLen == 0 {
26+
return nil, syscall.ENOENT
27+
}
28+
29+
if vecLen%2 != 0 {
30+
return nil, syscall.EINVAL
31+
}
32+
33+
result := make([]uintptr, vecLen)
34+
copy(result, vec)
35+
return unsafe.Slice((*[2]uintptr)(unsafe.Pointer(&result[0])), vecLen/2), nil
36+
}

unix/auxv_linux_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2025 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 go1.21 && linux
6+
7+
package unix_test
8+
9+
import (
10+
"testing"
11+
12+
"golang.org/x/sys/unix"
13+
)
14+
15+
func TestAuxv(t *testing.T) {
16+
vec, err := unix.Auxv()
17+
if err != nil {
18+
t.Fatalf("unexpected error: %v", err)
19+
}
20+
if len(vec) == 0 {
21+
t.Errorf("got zero auxv entries on linux, expected > 0")
22+
}
23+
}

unix/auxv_unsupported.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2025 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 !go1.21 && (aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || zos)
6+
7+
package unix
8+
9+
import "syscall"
10+
11+
func Auxv() ([][2]uintptr, error) {
12+
return nil, syscall.ENOTSUP
13+
}

0 commit comments

Comments
 (0)