Debian Codegraph @

cmd/bpf2go/gen/compile.go

raw symbols SCIP
1 //go:build !windows
2
3 package gen
4
5 import (
6 "fmt"
7 "os"
8 "os/exec"
9 "path/filepath"
10 "strings"
11 )
12
13 type CompileArgs struct {
14 // Which compiler to use.
15 CC string
16 // Command used to strip DWARF from the ELF.
17 Strip string
18 // Flags to pass to the compiler. This may contain positional arguments as well.
19 Flags []string
20 // Absolute working directory
21 Workdir string
22 // Absolute input file name
23 Source string
24 // Absolute output file name
25 Dest string
26 // Target to compile for, defaults to compiling generic BPF in host endianness.
27 Target Target
28 DisableStripping bool
29 }
30
31 func insertDefaultFlags(flags []string) []string {
32 // Default cflags that can be overridden by the user.
33 overrideFlags := []string{
34 // Code needs to be optimized, otherwise the verifier will often fail
35 // to understand it.
36 "-O2",
37 // Clang defaults to mcpu=probe which checks the kernel that we are
38 // compiling on. This isn't appropriate for ahead of time
39 // compiled code so force the most compatible version.
40 "-mcpu=v1",
41 }
42
43 insert := 0
44
45 // Find the first non-positional argument to support CC commands with
46 // multiple components. E.g.: BPF2GO_CC="ccache clang" ...
47 for ; insert < len(flags); insert++ {
48 if strings.HasPrefix(flags[insert], "-") {
49 break
50 }
51 }
52
53 result := append([]string(nil), flags[:insert]...)
54 result = append(result, overrideFlags...)
55 result = append(result, flags[insert:]...)
56
57 return result
58 }
59
60 // Compile C to a BPF ELF file.
61 func Compile(args CompileArgs) error {
62 cmd := exec.Command(args.CC, insertDefaultFlags(args.Flags)...)
63 cmd.Stderr = os.Stderr
64
65 inputDir := filepath.Dir(args.Source)
66 relInputDir, err := filepath.Rel(args.Workdir, inputDir)
67 if err != nil {
68 return err
69 }
70
71 target := args.Target
72 if target == (Target{}) {
73 target.clang = "bpf"
74 }
75
76 // C flags that can't be overridden.
77 if linux := target.linux; linux != "" {
78 cmd.Args = append(cmd.Args, "-D__TARGET_ARCH_"+linux)
79 }
80
81 cmd.Args = append(cmd.Args,
82 "-Wunused-command-line-argument",
83 "-target", target.clang,
84 "-c", args.Source,
85 "-o", args.Dest,
86 // Don't include clang version
87 "-fno-ident",
88 // Don't output inputDir into debug info
89 "-fdebug-prefix-map="+inputDir+"="+relInputDir,
90 "-fdebug-compilation-dir", ".",
91 // We always want BTF to be generated, so enforce debug symbols
92 "-g",
93 fmt.Sprintf("-D__BPF_TARGET_MISSING=%q", "GCC error \"The eBPF is using target specific macros, please provide -target that is not bpf, bpfel or bpfeb\""),
94 )
95 cmd.Dir = args.Workdir
96
97 if err := cmd.Run(); err != nil {
98 return err
99 }
100
101 if args.DisableStripping {
102 return nil
103 }
104
105 cmd = exec.Command(args.Strip, "-g", args.Dest)
106 cmd.Stderr = os.Stderr
107 if err := cmd.Run(); err != nil {
108 return fmt.Errorf("strip %s: %w", args.Dest, err)
109 }
110
111 return nil
112 }