/
main.go
446 lines (376 loc) · 10.8 KB
/
main.go
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package main
import (
"bytes"
"errors"
"flag"
"fmt"
"go/token"
"io"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"sort"
"strings"
)
const helpText = `Usage: %[1]s [options] <ident> <source file> [-- <C flags>]
ident is used as the stem of all generated Go types and functions, and
must be a valid Go identifier.
source is a single C file that is compiled using the specified compiler
(usually some version of clang).
You can pass options to the compiler by appending them after a '--' argument
or by supplying -cflags. Flags passed as arguments take precedence
over flags passed via -cflags. Additionally, the program expands quotation
marks in -cflags. This means that -cflags 'foo "bar baz"' is passed to the
compiler as two arguments "foo" and "bar baz".
The program expects GOPACKAGE to be set in the environment, and should be invoked
via go generate. The generated files are written to the current directory.
Options:
`
// Targets understood by bpf2go.
//
// Targets without a Linux string can't be used directly and are only included
// for the generic bpf, bpfel, bpfeb targets.
var targetByGoArch = map[string]target{
"386": {"bpfel", "x86"},
"amd64": {"bpfel", "x86"},
"amd64p32": {"bpfel", ""},
"arm": {"bpfel", "arm"},
"arm64": {"bpfel", "arm64"},
"mipsle": {"bpfel", ""},
"mips64le": {"bpfel", ""},
"mips64p32le": {"bpfel", ""},
"ppc64le": {"bpfel", "powerpc"},
"riscv64": {"bpfel", ""},
"armbe": {"bpfeb", "arm"},
"arm64be": {"bpfeb", "arm64"},
"mips": {"bpfeb", ""},
"mips64": {"bpfeb", ""},
"mips64p32": {"bpfeb", ""},
"ppc64": {"bpfeb", "powerpc"},
"s390": {"bpfeb", "s390"},
"s390x": {"bpfeb", "s390"},
"sparc": {"bpfeb", "sparc"},
"sparc64": {"bpfeb", "sparc"},
}
func run(stdout io.Writer, pkg, outputDir string, args []string) (err error) {
b2g := bpf2go{
stdout: stdout,
pkg: pkg,
outputDir: outputDir,
}
fs := flag.NewFlagSet("bpf2go", flag.ContinueOnError)
fs.StringVar(&b2g.cc, "cc", "clang", "`binary` used to compile C to BPF")
fs.StringVar(&b2g.strip, "strip", "", "`binary` used to strip DWARF from compiled BPF (default \"llvm-strip\")")
fs.BoolVar(&b2g.disableStripping, "no-strip", false, "disable stripping of DWARF")
flagCFlags := fs.String("cflags", "", "flags passed to the compiler, may contain quoted arguments")
fs.StringVar(&b2g.tags, "tags", "", "list of Go build tags to include in generated files")
flagTarget := fs.String("target", "bpfel,bpfeb", "clang target to compile for")
fs.StringVar(&b2g.makeBase, "makebase", "", "write make compatible depinfo files relative to `directory`")
fs.Var(&b2g.cTypes, "type", "`Name` of a type to generate a Go declaration for, may be repeated")
fs.BoolVar(&b2g.skipGlobalTypes, "no-global-types", false, "Skip generating types for map keys and values, etc.")
fs.SetOutput(stdout)
fs.Usage = func() {
fmt.Fprintf(fs.Output(), helpText, fs.Name())
fs.PrintDefaults()
fmt.Fprintln(fs.Output())
printTargets(fs.Output())
}
if err := fs.Parse(args); errors.Is(err, flag.ErrHelp) {
return nil
} else if err != nil {
return err
}
if b2g.pkg == "" {
return errors.New("missing package, are you running via go generate?")
}
if b2g.cc == "" {
return errors.New("no compiler specified")
}
args, cFlags := splitCFlagsFromArgs(fs.Args())
if *flagCFlags != "" {
splitCFlags, err := splitArguments(*flagCFlags)
if err != nil {
return err
}
// Command line arguments take precedence over C flags
// from the flag.
cFlags = append(splitCFlags, cFlags...)
}
for _, cFlag := range cFlags {
if strings.HasPrefix(cFlag, "-M") {
return fmt.Errorf("use -makebase instead of %q", cFlag)
}
}
b2g.cFlags = cFlags[:len(cFlags):len(cFlags)]
if len(args) < 2 {
return errors.New("expected at least two arguments")
}
b2g.ident = args[0]
if !token.IsIdentifier(b2g.ident) {
return fmt.Errorf("%q is not a valid identifier", b2g.ident)
}
input := args[1]
if _, err := os.Stat(input); os.IsNotExist(err) {
return fmt.Errorf("file %s doesn't exist", input)
} else if err != nil {
return fmt.Errorf("state %s: %s", input, err)
}
b2g.sourceFile, err = filepath.Abs(input)
if err != nil {
return err
}
if b2g.makeBase != "" {
b2g.makeBase, err = filepath.Abs(b2g.makeBase)
if err != nil {
return err
}
}
if strings.ContainsRune(b2g.tags, '\n') {
return fmt.Errorf("-tags mustn't contain new line characters")
}
targetArches := strings.Split(*flagTarget, ",")
if len(targetArches) == 0 {
return fmt.Errorf("no targets specified")
}
targets, err := collectTargets(targetArches)
if errors.Is(err, errInvalidTarget) {
printTargets(stdout)
fmt.Fprintln(stdout)
return err
}
if err != nil {
return err
}
if !b2g.disableStripping {
// Try to find a suitable llvm-strip, possibly with a version suffix derived
// from the clang binary.
if b2g.strip == "" {
b2g.strip = "llvm-strip"
if strings.HasPrefix(b2g.cc, "clang") {
b2g.strip += strings.TrimPrefix(b2g.cc, "clang")
}
}
b2g.strip, err = exec.LookPath(b2g.strip)
if err != nil {
return err
}
}
for target, arches := range targets {
if err := b2g.convert(target, arches); err != nil {
return err
}
}
return nil
}
// cTypes collects the C type names a user wants to generate Go types for.
//
// Names are guaranteed to be unique, and only a subset of names is accepted so
// that we may extend the flag syntax in the future.
type cTypes []string
var _ flag.Value = (*cTypes)(nil)
func (ct *cTypes) String() string {
if ct == nil {
return "[]"
}
return fmt.Sprint(*ct)
}
const validCTypeChars = `[a-z0-9_]`
var reValidCType = regexp.MustCompile(`(?i)^` + validCTypeChars + `+$`)
func (ct *cTypes) Set(value string) error {
if !reValidCType.MatchString(value) {
return fmt.Errorf("%q contains characters outside of %s", value, validCTypeChars)
}
i := sort.SearchStrings(*ct, value)
if i >= len(*ct) {
*ct = append(*ct, value)
return nil
}
if (*ct)[i] == value {
return fmt.Errorf("duplicate type %q", value)
}
*ct = append((*ct)[:i], append([]string{value}, (*ct)[i:]...)...)
return nil
}
type bpf2go struct {
stdout io.Writer
// Absolute path to a .c file.
sourceFile string
// Absolute path to a directory where .go are written
outputDir string
// Valid go package name.
pkg string
// Valid go identifier.
ident string
// C compiler.
cc string
// Command used to strip DWARF.
strip string
disableStripping bool
// C flags passed to the compiler.
cFlags []string
skipGlobalTypes bool
// C types to include in the generatd output.
cTypes cTypes
// Go tags included in the .go
tags string
// Base directory of the Makefile. Enables outputting make-style dependencies
// in .d files.
makeBase string
}
func (b2g *bpf2go) convert(tgt target, arches []string) (err error) {
removeOnError := func(f *os.File) {
if err != nil {
os.Remove(f.Name())
}
f.Close()
}
stem := fmt.Sprintf("%s_%s", strings.ToLower(b2g.ident), tgt.clang)
if tgt.linux != "" {
stem = fmt.Sprintf("%s_%s_%s", strings.ToLower(b2g.ident), tgt.clang, tgt.linux)
}
objFileName := filepath.Join(b2g.outputDir, stem+".o")
cwd, err := os.Getwd()
if err != nil {
return err
}
var tags []string
if len(arches) > 0 {
tags = append(tags, strings.Join(arches, " "))
}
if b2g.tags != "" {
tags = append(tags, b2g.tags)
}
cFlags := make([]string, len(b2g.cFlags))
copy(cFlags, b2g.cFlags)
if tgt.linux != "" {
cFlags = append(cFlags, "-D__TARGET_ARCH_"+tgt.linux)
}
var dep bytes.Buffer
err = compile(compileArgs{
cc: b2g.cc,
cFlags: cFlags,
target: tgt.clang,
dir: cwd,
source: b2g.sourceFile,
dest: objFileName,
dep: &dep,
})
if err != nil {
return err
}
fmt.Fprintln(b2g.stdout, "Compiled", objFileName)
if !b2g.disableStripping {
if err := strip(b2g.strip, objFileName); err != nil {
return err
}
fmt.Fprintln(b2g.stdout, "Stripped", objFileName)
}
// Write out generated go
goFileName := filepath.Join(b2g.outputDir, stem+".go")
goFile, err := os.Create(goFileName)
if err != nil {
return err
}
defer removeOnError(goFile)
err = output(outputArgs{
pkg: b2g.pkg,
ident: b2g.ident,
cTypes: b2g.cTypes,
skipGlobalTypes: b2g.skipGlobalTypes,
tags: tags,
obj: objFileName,
out: goFile,
})
if err != nil {
return fmt.Errorf("can't write %s: %s", goFileName, err)
}
fmt.Fprintln(b2g.stdout, "Wrote", goFileName)
if b2g.makeBase == "" {
return
}
deps, err := parseDependencies(cwd, &dep)
if err != nil {
return fmt.Errorf("can't read dependency information: %s", err)
}
// There is always at least a dependency for the main file.
deps[0].file = goFileName
depFile, err := adjustDependencies(b2g.makeBase, deps)
if err != nil {
return fmt.Errorf("can't adjust dependency information: %s", err)
}
depFileName := goFileName + ".d"
if err := os.WriteFile(depFileName, depFile, 0666); err != nil {
return fmt.Errorf("can't write dependency file: %s", err)
}
fmt.Fprintln(b2g.stdout, "Wrote", depFileName)
return nil
}
type target struct {
clang string
linux string
}
func printTargets(w io.Writer) {
var arches []string
for arch, archTarget := range targetByGoArch {
if archTarget.linux == "" {
continue
}
arches = append(arches, arch)
}
sort.Strings(arches)
fmt.Fprint(w, "Supported targets:\n")
fmt.Fprint(w, "\tbpf\n\tbpfel\n\tbpfeb\n")
for _, arch := range arches {
fmt.Fprintf(w, "\t%s\n", arch)
}
}
var errInvalidTarget = errors.New("unsupported target")
func collectTargets(targets []string) (map[target][]string, error) {
result := make(map[target][]string)
for _, tgt := range targets {
switch tgt {
case "bpf", "bpfel", "bpfeb":
var goarches []string
for arch, archTarget := range targetByGoArch {
if archTarget.clang == tgt {
// Include tags for all goarches that have the same endianness.
goarches = append(goarches, arch)
}
}
sort.Strings(goarches)
result[target{tgt, ""}] = goarches
case "native":
tgt = runtime.GOARCH
fallthrough
default:
archTarget, ok := targetByGoArch[tgt]
if !ok || archTarget.linux == "" {
return nil, fmt.Errorf("%q: %w", tgt, errInvalidTarget)
}
var goarches []string
for goarch, lt := range targetByGoArch {
if lt == archTarget {
// Include tags for all goarches that have the same
// target.
goarches = append(goarches, goarch)
}
}
sort.Strings(goarches)
result[archTarget] = goarches
}
}
return result, nil
}
func main() {
outputDir, err := os.Getwd()
if err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(1)
}
if err := run(os.Stdout, os.Getenv("GOPACKAGE"), outputDir, os.Args[1:]); err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(1)
}
}