blob: fa488447a5eb12019cc8f447da51414e17d7eb6b [file] [log] [blame]
Elliott Hughes6b586e72021-04-15 13:39:08 -07001#!/usr/bin/env python3
Martijn Coenen0c6de752019-01-02 12:52:51 +01002
3import argparse
Martijn Coenen0c6de752019-01-02 12:52:51 +01004import logging
5import os
6import re
Martijn Coenen0c6de752019-01-02 12:52:51 +01007
Elliott Hughesae03b122019-09-17 16:37:05 -07008from gensyscalls import SupportedArchitectures, SysCallsTxtParser
Martijn Coenen0c6de752019-01-02 12:52:51 +01009from genseccomp import parse_syscall_NRs
10
Martijn Coenen0c6de752019-01-02 12:52:51 +010011
Dan Albert1dffb862021-02-03 16:32:10 -080012def load_syscall_names_from_file(file_path, architecture):
13 parser = SysCallsTxtParser()
14 parser.parse_open_file(open(file_path))
15 arch_map = {}
16 for syscall in parser.syscalls:
17 if syscall.get(architecture):
18 arch_map[syscall["func"]] = syscall["name"]
19
20 return arch_map
21
Martijn Coenen0c6de752019-01-02 12:52:51 +010022
23def gen_syscall_nrs(out_file, base_syscall_file, syscall_NRs):
Dan Albert1dffb862021-02-03 16:32:10 -080024 for arch in SupportedArchitectures:
25 base_names = load_syscall_names_from_file(base_syscall_file, arch)
Martijn Coenen0c6de752019-01-02 12:52:51 +010026
Dan Albert1dffb862021-02-03 16:32:10 -080027 for func, syscall in base_names.items():
28 out_file.write("#define __" + arch + "_" + func + " " +
29 str(syscall_NRs[arch][syscall]) + ";\n")
30
Martijn Coenen0c6de752019-01-02 12:52:51 +010031
32def main():
Dan Albert1dffb862021-02-03 16:32:10 -080033 parser = argparse.ArgumentParser(
34 description=
35 "Generates a mapping of bionic functions to system call numbers per architecture."
36 )
37 parser.add_argument("--verbose", "-v", help="Enables verbose logging.")
38 parser.add_argument("--out-dir",
39 help="The output directory for the output files")
40 parser.add_argument(
41 "base_file",
42 metavar="base-file",
43 type=str,
44 help="The path of the base syscall list (SYSCALLS.TXT).")
45 parser.add_argument(
46 "files",
47 metavar="FILE",
48 type=str,
49 nargs="+",
50 help=("A syscall name-number mapping file for an architecture.\n"))
51 args = parser.parse_args()
Martijn Coenen0c6de752019-01-02 12:52:51 +010052
Dan Albert1dffb862021-02-03 16:32:10 -080053 if args.verbose:
54 logging.basicConfig(level=logging.DEBUG)
55 else:
56 logging.basicConfig(level=logging.INFO)
Martijn Coenen0c6de752019-01-02 12:52:51 +010057
Dan Albert1dffb862021-02-03 16:32:10 -080058 syscall_NRs = {}
59 for filename in args.files:
60 m = re.search(r"libseccomp_gen_syscall_nrs_([^/]+)", filename)
61 syscall_NRs[m.group(1)] = parse_syscall_NRs(filename)
Martijn Coenen0c6de752019-01-02 12:52:51 +010062
Dan Albert1dffb862021-02-03 16:32:10 -080063 output_path = os.path.join(args.out_dir, "func_to_syscall_nrs.h")
64 with open(output_path, "w") as output_file:
65 gen_syscall_nrs(out_file=output_file,
66 syscall_NRs=syscall_NRs,
67 base_syscall_file=args.base_file)
68
Martijn Coenen0c6de752019-01-02 12:52:51 +010069
70if __name__ == "__main__":
Dan Albert1dffb862021-02-03 16:32:10 -080071 main()