forked from paolostivanin/syscalls-table-64bit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_syscalls.py
executable file
·119 lines (101 loc) · 4.97 KB
/
gen_syscalls.py
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
#!/usr/bin/env python
# SCARY CODE UPHEAD. PROCEED AT YOUR OWN RISK.
import ctags
import re
import simplejson
from ctags import CTags, TagEntry
def gen_syscalls(sct_file, x32=False):
"""
Generate an Array of syscalls
sct_file: File handle of the name table
x32: the name table is 32 bit syscalls?
"""
tags = CTags('tags')
entry = TagEntry()
sys_calls = []
i = 0
for line in sct_file:
if line.startswith("#"):
continue
parts = line.split()
if(len(parts) > 3 and parts[0] >= '0'):
name = parts[3].encode("utf-8")
if tags.find(entry, name, ctags.TAG_FULLMATCH | ctags.TAG_OBSERVECASE):
found_sym = False
while(not found_sym):
if(entry["kind"].decode("utf-8") == "prototype"):
found_sym = True
details = {"number": {"int": i, "hex": ("%0#4x" % (i))}, "name": name,
"parameters": entry[b"signature"]}
if(entry[b"signature"] != "(void)"):
sig = entry[b"signature"].decode(
"utf-8").strip('()').split(',')
else:
sig = []
regs = {}
details["parameters_details"] = []
if(len(sig) < (7 if x32 else 8)):
for param in sig:
par = param.strip()
par_def = ""
if(param.find("struct") != -1):
type_match = re.search(
"struct (\w+)", param)
if(type_match):
par_entry = TagEntry()
if(tags.find(par_entry, type_match.group(1).encode("utf-8"), ctags.TAG_FULLMATCH | ctags.TAG_OBSERVECASE)):
if(par_entry[b'kind'] == "struct"):
par_def = {'file': par_entry['file'], 'line': int(
par_entry['lineNumber'])}
details["parameters_details"].append(
{'type': par, 'def': par_def})
else:
details["parameters_details"].append(
{"type": "param addr*", "def": par_def})
remaining = (5 if x32 else 6) - \
len(details["parameters_details"])
for x in range(0, remaining):
details["parameters_details"].append("")
pattern = "SYSCALL_DEFINE%d(%s" % (
len(sig), name.decode("utf-8").replace("sys_", ""))
search = "SYSCALL_DEFINE%d" % (len(sig))
if tags.find(entry, search.encode("utf-8"), ctags.TAG_FULLMATCH | ctags.TAG_OBSERVECASE):
found = False
while(not found):
if(entry["pattern"].decode("utf-8").find(pattern) >= 2):
# details['found'] = entry['pattern']
details["definition"] = {
"file": entry["file"], "lineno": int(entry['lineNumber'])}
found = True
break
if(not tags.findNext(entry)):
details["definition"] = {
"file": "", "lineno": ""}
break
else:
details["definition"] = {
"file": "", "lineno": ""}
sys_calls.append(details)
else:
if(not tags.findNext(entry)):
sys_calls.append([i].append(
[""] * (7 if x32 else 8)))
break
i += 1
else:
details = {"number": {"int": i, "hex": ("%0#4x" % (i))}, "name": "not implemented", "parameters": "", "parameters_details": {
}, "definition": {"file": "", "lineno": ""}}
sys_calls.append(details)
i += 1
return sys_calls
def main():
# file generated by ctags --fields=afmikKlnsStz --c-kinds=+pc -R
with open("syscall_64.tbl", "r") as file_64, open("syscall_32.tbl", "r") as file_32:
syscalls_32 = gen_syscalls(file_32, True)
syscalls_64 = gen_syscalls(file_64)
print(simplejson.dumps(
{"x86": syscalls_32, "x86_64": syscalls_64}, indent=" "))
file_32.close()
file_64.close()
if __name__ == "__main__":
main()