-
Notifications
You must be signed in to change notification settings - Fork 328
/
main.go
129 lines (113 loc) · 2.63 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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon
package main
import (
"bufio"
"debug/elf"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"os"
"strings"
)
type SyscallArg struct {
Name string
Type string
}
func dumpSyscalls(fname string) {
f, err := elf.Open(fname)
if err != nil {
panic(err)
}
defer f.Close()
elfSyms, err := f.Symbols()
if err != nil {
panic(err)
}
typs := make(map[string]*elf.Symbol)
args := make(map[string]*elf.Symbol)
secDataIdx := -1
secRodataIdx := -1
var secData, secRodata *elf.Section
for idx, s := range f.Sections {
if s.Name == ".data" {
secDataIdx = idx
secData = s
} else if s.Name == ".rodata" {
secRodataIdx = idx
secRodata = s
}
}
if secDataIdx == -1 || secRodataIdx == -1 {
panic("no .data")
}
// fmt.Printf(".data section %+v\n", secData)
secDataReader := secData.Open()
// fmt.Printf(".rodata section %+v\n", secData)
secRodataReader := secRodata.Open()
for i := range elfSyms {
s := &elfSyms[i]
if strings.HasPrefix(s.Name, "types__") {
syscall := strings.TrimPrefix(s.Name, "types__")
typs[syscall] = s
} else if strings.HasPrefix(s.Name, "args__") {
syscall := strings.TrimPrefix(s.Name, "args__")
args[syscall] = s
}
}
ReadString := func(dataAddr uint64) string {
_, err := secDataReader.Seek(int64(dataAddr-secData.Addr), io.SeekStart)
if err != nil {
panic(err)
}
var v uint64
err = binary.Read(secDataReader, f.FileHeader.ByteOrder, &v)
if err != nil {
panic("read failed")
}
_, err = secRodataReader.Seek(int64(v-secRodata.Addr), io.SeekStart)
if err != nil {
panic("seek to value failed")
}
brd := bufio.NewReader(secRodataReader)
data, _ := brd.ReadBytes(0)
return string(data[:len(data)-1])
}
info := make(map[string][]SyscallArg, 0)
for name := range typs {
typSym := typs[name]
argSym := args[name]
if int(typSym.Section) != secDataIdx || int(argSym.Section) != secDataIdx {
panic("symbols not in .data")
}
if (typSym.Size != argSym.Size) || (typSym.Size%8 != 0) {
panic("invalid symbol size")
}
arr := make([]SyscallArg, 0)
n := typSym.Size / 8
for i := uint64(0); i < n; i++ {
typPtr := typSym.Value + (i * 8)
typStr := ReadString(typPtr)
argPtr := argSym.Value + (i * 8)
argStr := ReadString(argPtr)
arr = append(arr, SyscallArg{
Name: argStr,
Type: typStr,
})
}
info[name] = arr
}
b, err := json.MarshalIndent(info, "", " ")
if err != nil {
panic(err)
}
os.Stdout.Write(b)
}
func main() {
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "Usage: %s <kernel_image>\n", os.Args[0])
os.Exit(1)
}
dumpSyscalls(os.Args[1])
}