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
|
// Copyright 2017 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/google/syzkaller/pkg/compiler"
)
type netbsd struct{}
func (*netbsd) prepare(sourcedir string, build bool, arches []*Arch) error {
if !build {
return fmt.Errorf("netbsd requires -build flag")
}
return nil
}
func (*netbsd) prepareArch(arch *Arch) error {
links := [][2]string{
{"amd64", "machine"},
{"amd64", "amd64"},
{"x86", "x86"},
}
for _, v := range links {
if err := machineLink(arch, v[0], v[1]); err != nil {
return err
}
}
return nil
}
func machineLink(arch *Arch, machine, dest string) error {
if err := os.Symlink(machineInclude(arch, machine),
filepath.Join(arch.buildDir, dest)); err != nil {
return fmt.Errorf("failed to create link: %w", err)
}
return nil
}
func machineInclude(arch *Arch, machine string) string {
return filepath.Join(arch.sourceDir, "sys", "arch", machine, "include")
}
func (*netbsd) processFile(arch *Arch, info *compiler.ConstInfo) (map[string]uint64, map[string]bool, error) {
args := []string{
"-fmessage-length=0",
"-nostdinc",
"-D_KERNEL",
"-D__BSD_VISIBLE=1",
"-I", filepath.Join(arch.sourceDir, "sys"),
"-I", filepath.Join(arch.sourceDir, "sys", "sys"),
"-I", filepath.Join(arch.sourceDir, "sys", "arch", "amd64"),
"-I", filepath.Join(arch.sourceDir, "common", "include"),
"-I", filepath.Join(arch.sourceDir, "sys", "compat", "linux", "common"),
"-I", filepath.Join(arch.sourceDir, "include"),
"-I", arch.buildDir,
}
for _, incdir := range info.Incdirs {
args = append(args, "-I"+filepath.Join(arch.sourceDir, incdir))
}
if arch.includeDirs != "" {
for _, dir := range strings.Split(arch.includeDirs, ",") {
args = append(args, "-I"+dir)
}
}
// Syscall consts on netbsd have weird prefixes sometimes,
// try to extract consts with these prefixes as well.
compatNames := make(map[string][]string)
for _, def := range info.Consts {
const SYS = "SYS_"
if strings.HasPrefix(def.Name, SYS) {
for _, prefix := range []string{"_", "__", "___"} {
for _, suffix := range []string{"30", "50"} {
compat := SYS + prefix + def.Name[len(SYS):] + suffix
compatNames[def.Name] = append(compatNames[def.Name], compat)
info.Consts = append(info.Consts, &compiler.Const{Name: compat})
}
}
} else {
compat := "LINUX_" + def.Name
compatNames[def.Name] = append(compatNames[def.Name], compat)
info.Consts = append(info.Consts, &compiler.Const{Name: compat})
}
}
params := &extractParams{
AddSource: "#include <sys/syscall.h>",
TargetEndian: arch.target.HostEndian,
}
res, undeclared, err := extract(info, "gcc", args, params)
for orig, compats := range compatNames {
for _, compat := range compats {
if undeclared[orig] && !undeclared[compat] {
res[orig] = res[compat]
delete(res, compat)
delete(undeclared, orig)
}
delete(undeclared, compat)
}
}
return res, undeclared, err
}
|