File: auxv.go

package info (click to toggle)
golang-github-cilium-ebpf 0.17.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,684 kB
  • sloc: ansic: 1,259; makefile: 127; python: 113; awk: 29; sh: 24
file content (67 lines) | stat: -rw-r--r-- 1,374 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package linux

import (
	"errors"
	"fmt"
	"io"
	_ "unsafe"

	"github.com/cilium/ebpf/internal"
)

type auxvPairReader interface {
	Close() error
	ReadAuxvPair() (uint64, uint64, error)
}

// See https://elixir.bootlin.com/linux/v6.5.5/source/include/uapi/linux/auxvec.h
const (
	_AT_NULL         = 0  // End of vector
	_AT_SYSINFO_EHDR = 33 // Offset to vDSO blob in process image
)

//go:linkname runtime_getAuxv runtime.getAuxv
func runtime_getAuxv() []uintptr

type auxvRuntimeReader struct {
	data  []uintptr
	index int
}

func (r *auxvRuntimeReader) Close() error {
	return nil
}

func (r *auxvRuntimeReader) ReadAuxvPair() (uint64, uint64, error) {
	if r.index >= len(r.data)+2 {
		return 0, 0, io.EOF
	}

	// we manually add the (_AT_NULL, _AT_NULL) pair at the end
	// that is not provided by the go runtime
	var tag, value uintptr
	if r.index+1 < len(r.data) {
		tag, value = r.data[r.index], r.data[r.index+1]
	} else {
		tag, value = _AT_NULL, _AT_NULL
	}
	r.index += 2
	return uint64(tag), uint64(value), nil
}

func newAuxvRuntimeReader() (auxvPairReader, error) {
	if !internal.OnLinux {
		return nil, fmt.Errorf("read auxv from runtime: %w", internal.ErrNotSupportedOnOS)
	}

	data := runtime_getAuxv()

	if len(data)%2 != 0 {
		return nil, errors.New("malformed auxv passed from runtime")
	}

	return &auxvRuntimeReader{
		data:  data,
		index: 0,
	}, nil
}