| 1 | //go:build !windows |
| 2 | |
| 3 | package genscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/ |
| 4 | |
| 5 | import ( |
| 6 | "errorsscip-go gomod github.com/golang/go/src go1.24.0 errors/" |
| 7 | "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/" |
| 8 | "go/build/constraintscip-go gomod github.com/golang/go/src go1.24.0 `go/build/constraint`/" |
| 9 | "mapsscip-go gomod github.com/golang/go/src go1.24.0 maps/" |
| 10 | "runtimescip-go gomod github.com/golang/go/src go1.24.0 runtime/" |
| 11 | "slicesscip-go gomod github.com/golang/go/src go1.24.0 slices/" |
| 12 | ) |
| 13 | |
| 14 | var ErrInvalidTargetscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/ErrInvalidTarget. = errorsscip-go gomod github.com/golang/go/src go1.24.0 errors/.Newscip-go gomod github.com/golang/go/src go1.24.0 errors/New().("unsupported target") |
| 15 | |
| 16 | var targetsByGoArchscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/targetsByGoArch. = map[GoArchscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/GoArch#]Targetscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/Target#{ |
| 17 | "386": {"bpfel", "x86", ""}, |
| 18 | "amd64": {"bpfel", "x86", ""}, |
| 19 | "arm": {"bpfel", "arm", ""}, |
| 20 | "arm64": {"bpfel", "arm64", ""}, |
| 21 | "loong64": {"bpfel", "loongarch", ""}, |
| 22 | "mips": {"bpfeb", "mips", ""}, |
| 23 | "mipsle": {"bpfel", "", ""}, |
| 24 | "mips64": {"bpfeb", "", ""}, |
| 25 | "mips64le": {"bpfel", "", ""}, |
| 26 | "ppc64": {"bpfeb", "powerpc", ""}, |
| 27 | "ppc64le": {"bpfel", "powerpc", ""}, |
| 28 | "riscv64": {"bpfel", "riscv", ""}, |
| 29 | "s390x": {"bpfeb", "s390", ""}, |
| 30 | "wasm": {"bpfel", "", "js"}, |
| 31 | } |
| 32 | |
| 33 | type Targetscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/Target# struct { |
| 34 | // Clang arch string, used to define the clang -target flag, as per |
| 35 | // "clang -print-targets". |
| 36 | clangscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/Target#clang. string |
| 37 | // Linux arch string, used to define __TARGET_ARCH_xzy macros used by |
| 38 | // https://github.com/libbpf/libbpf/blob/master/src/bpf_tracing.h |
| 39 | linuxscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/Target#linux. string |
| 40 | // GOOS override for use during tests. |
| 41 | goosscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/Target#goos. string |
| 42 | } |
| 43 | |
| 44 | // TargetsByGoArch returns all supported targets. |
| 45 | func TargetsByGoArchscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/TargetsByGoArch().() map[GoArchscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/GoArch#]Targetscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/Target# { |
| 46 | return mapsscip-go gomod github.com/golang/go/src go1.24.0 maps/.Clonescip-go gomod github.com/golang/go/src go1.24.0 maps/Clone().(targetsByGoArchscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/targetsByGoArch.) |
| 47 | } |
| 48 | |
| 49 | // IsGeneric returns true if the target will compile to generic BPF. |
| 50 | func (tgt *Targetscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/Target#) IsGenericscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/Target#IsGeneric().() bool { |
| 51 | return tgt.linuxscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/Target#linux. == "" |
| 52 | } |
| 53 | |
| 54 | // Suffix returns a a string suitable for appending to a file name to |
| 55 | // identify the target. |
| 56 | func (tgt *Targetscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/Target#) Suffixscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/Target#Suffix().() string { |
| 57 | // The output filename must not match any of the following patterns: |
| 58 | // |
| 59 | // *_GOOS |
| 60 | // *_GOARCH |
| 61 | // *_GOOS_GOARCH |
| 62 | // |
| 63 | // Otherwise it is interpreted as a build constraint by the Go toolchain. |
| 64 | stem := tgt.clangscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/Target#clang. |
| 65 | if tgt.linuxscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/Target#linux. != "" { |
| 66 | stem = fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprintf().("%s_%s", tgt.linuxscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/Target#linux., tgt.clangscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/Target#clang.) |
| 67 | } |
| 68 | return stem |
| 69 | } |
| 70 | |
| 71 | // ObsoleteSuffix returns an obsolete suffix for a subset of targets. |
| 72 | // |
| 73 | // It's used to work around an old bug and should not be used in new code. |
| 74 | func (tgt *Targetscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/Target#) ObsoleteSuffixscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/Target#ObsoleteSuffix().() string { |
| 75 | if tgt.linuxscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/Target#linux. == "" { |
| 76 | return "" |
| 77 | } |
| 78 | |
| 79 | return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprintf().("%s_%s", tgt.clangscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/Target#clang., tgt.linuxscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/Target#linux.) |
| 80 | } |
| 81 | |
| 82 | // GoArch is a Go arch string. |
| 83 | // |
| 84 | // See https://go.dev/doc/install/source#environment for valid GOARCHes when |
| 85 | // GOOS=linux. |
| 86 | type GoArchscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/GoArch# string |
| 87 | |
| 88 | type GoArchesscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/GoArches# []GoArchscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/GoArch# |
| 89 | |
| 90 | // Constraints is satisfied when GOARCH is any of the arches. |
| 91 | func (arches GoArchesscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/GoArches#) Constraintscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/GoArches#Constraint().() constraintscip-go gomod github.com/golang/go/src go1.24.0 `go/build/constraint`/.Exprscip-go gomod github.com/golang/go/src go1.24.0 `go/build/constraint`/Expr# { |
| 92 | var archConstraint constraintscip-go gomod github.com/golang/go/src go1.24.0 `go/build/constraint`/.Exprscip-go gomod github.com/golang/go/src go1.24.0 `go/build/constraint`/Expr# |
| 93 | for _, goarch := range arches { |
| 94 | tag := &constraintscip-go gomod github.com/golang/go/src go1.24.0 `go/build/constraint`/.TagExprscip-go gomod github.com/golang/go/src go1.24.0 `go/build/constraint`/TagExpr#{Tagscip-go gomod github.com/golang/go/src go1.24.0 `go/build/constraint`/TagExpr#Tag.: string(goarch)} |
| 95 | archConstraint = orConstraintsscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/orConstraints().(archConstraint, tag) |
| 96 | } |
| 97 | return archConstraint |
| 98 | } |
| 99 | |
| 100 | // FindTarget turns a list of identifiers into targets and their respective |
| 101 | // GoArches. |
| 102 | // |
| 103 | // The following are valid identifiers: |
| 104 | // |
| 105 | // - bpf: compile generic BPF for host endianness |
| 106 | // - bpfel: compile generic BPF for little endian |
| 107 | // - bpfeb: compile generic BPF for big endian |
| 108 | // - native: compile BPF for host target |
| 109 | // - $GOARCH: compile BPF for $GOARCH target |
| 110 | // |
| 111 | // Generic BPF can run on any target goarch with the correct endianness, |
| 112 | // but doesn't have access to some arch specific tracing functionality. |
| 113 | func FindTargetscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/FindTarget().(id string) (Targetscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/Target#, GoArchesscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/GoArches#, error) { |
| 114 | switch id { |
| 115 | case "bpf", "bpfel", "bpfeb": |
| 116 | var goarches []GoArchscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/GoArch# |
| 117 | for arch, archTarget := range targetsByGoArchscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/targetsByGoArch. { |
| 118 | if archTarget.clangscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/Target#clang. == id { |
| 119 | // Include tags for all goarches that have the same endianness. |
| 120 | goarches = append(goarches, arch) |
| 121 | } |
| 122 | } |
| 123 | slicesscip-go gomod github.com/golang/go/src go1.24.0 slices/.Sortscip-go gomod github.com/golang/go/src go1.24.0 slices/Sort().(goarches) |
| 124 | return Targetscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/Target#{id, "", ""}, goarches, nil |
| 125 | |
| 126 | case "native": |
| 127 | id = runtimescip-go gomod github.com/golang/go/src go1.24.0 runtime/.GOARCHscip-go gomod github.com/golang/go/src go1.24.0 runtime/GOARCH. |
| 128 | fallthrough |
| 129 | |
| 130 | default: |
| 131 | archTarget, ok := targetsByGoArchscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/targetsByGoArch.[GoArchscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/GoArch#(id)] |
| 132 | if !ok || archTarget.linuxscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/Target#linux. == "" { |
| 133 | return Targetscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/Target#{}, nil, fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("%q: %w", id, ErrInvalidTargetscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/ErrInvalidTarget.) |
| 134 | } |
| 135 | |
| 136 | var goarches []GoArchscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/GoArch# |
| 137 | for goarch, lt := range targetsByGoArchscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/targetsByGoArch. { |
| 138 | if lt == archTarget { |
| 139 | // Include tags for all goarches that have the same |
| 140 | // target. |
| 141 | goarches = append(goarches, goarch) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | slicesscip-go gomod github.com/golang/go/src go1.24.0 slices/.Sortscip-go gomod github.com/golang/go/src go1.24.0 slices/Sort().(goarches) |
| 146 | return archTarget, goarches, nil |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | func orConstraintsscip-go gomod github.com/cilium/ebpf . `github.com/cilium/ebpf/cmd/bpf2go/gen`/orConstraints().(x, y constraintscip-go gomod github.com/golang/go/src go1.24.0 `go/build/constraint`/.Exprscip-go gomod github.com/golang/go/src go1.24.0 `go/build/constraint`/Expr#) constraintscip-go gomod github.com/golang/go/src go1.24.0 `go/build/constraint`/.Exprscip-go gomod github.com/golang/go/src go1.24.0 `go/build/constraint`/Expr# { |
| 151 | if x == nil { |
| 152 | return y |
| 153 | } |
| 154 | |
| 155 | if y == nil { |
| 156 | return x |
| 157 | } |
| 158 | |
| 159 | return &constraintscip-go gomod github.com/golang/go/src go1.24.0 `go/build/constraint`/.OrExprscip-go gomod github.com/golang/go/src go1.24.0 `go/build/constraint`/OrExpr#{Xscip-go gomod github.com/golang/go/src go1.24.0 `go/build/constraint`/OrExpr#X.: x, Yscip-go gomod github.com/golang/go/src go1.24.0 `go/build/constraint`/OrExpr#Y.: y} |
| 160 | } |