auxv.go 1020 B

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