Martijn Coenen | 0c6de75 | 2019-01-02 12:52:51 +0100 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import argparse |
| 4 | import collections |
| 5 | import logging |
| 6 | import os |
| 7 | import re |
| 8 | import subprocess |
| 9 | import textwrap |
| 10 | |
| 11 | from gensyscalls import SysCallsTxtParser |
| 12 | from genseccomp import parse_syscall_NRs |
| 13 | |
| 14 | def load_syscall_names_from_file(file_path, architecture): |
| 15 | parser = SysCallsTxtParser() |
| 16 | parser.parse_open_file(open(file_path)) |
| 17 | arch_map = {} |
| 18 | for syscall in parser.syscalls: |
| 19 | if syscall.get(architecture): |
| 20 | arch_map[syscall["func"]] = syscall["name"]; |
| 21 | |
| 22 | return arch_map |
| 23 | |
| 24 | def gen_syscall_nrs(out_file, base_syscall_file, syscall_NRs): |
| 25 | for arch in ('arm', 'arm64', 'mips', 'mips64', 'x86', 'x86_64'): |
| 26 | base_names = load_syscall_names_from_file(base_syscall_file, arch) |
| 27 | |
| 28 | for func,syscall in base_names.iteritems(): |
| 29 | out_file.write("#define __" + arch + "_" + func + " " + str(syscall_NRs[arch][syscall]) + ";\n") |
| 30 | |
| 31 | def main(): |
| 32 | parser = argparse.ArgumentParser( |
| 33 | description="Generates a mapping of bionic functions to system call numbers per architecture.") |
| 34 | parser.add_argument("--verbose", "-v", help="Enables verbose logging.") |
| 35 | parser.add_argument("--out-dir", |
| 36 | help="The output directory for the output files") |
| 37 | parser.add_argument("base_file", metavar="base-file", type=str, |
| 38 | help="The path of the base syscall list (SYSCALLS.TXT).") |
| 39 | parser.add_argument("files", metavar="FILE", type=str, nargs="+", |
| 40 | help=("A syscall name-number mapping file for an architecture.\n")) |
| 41 | args = parser.parse_args() |
| 42 | |
| 43 | if args.verbose: |
| 44 | logging.basicConfig(level=logging.DEBUG) |
| 45 | else: |
| 46 | logging.basicConfig(level=logging.INFO) |
| 47 | |
| 48 | syscall_files = [] |
| 49 | syscall_NRs = {} |
| 50 | for filename in args.files: |
| 51 | m = re.search(r"libseccomp_gen_syscall_nrs_([^/]+)", filename) |
| 52 | syscall_NRs[m.group(1)] = parse_syscall_NRs(filename) |
| 53 | |
| 54 | output_path = os.path.join(args.out_dir, "func_to_syscall_nrs.h") |
| 55 | with open(output_path, "w") as output_file: |
| 56 | gen_syscall_nrs(out_file=output_file, |
| 57 | syscall_NRs=syscall_NRs, base_syscall_file=args.base_file) |
| 58 | |
| 59 | if __name__ == "__main__": |
| 60 | main() |