Elliott Hughes | 6b586e7 | 2021-04-15 13:39:08 -0700 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
Martijn Coenen | 0c6de75 | 2019-01-02 12:52:51 +0100 | [diff] [blame] | 2 | |
| 3 | import argparse |
Martijn Coenen | 0c6de75 | 2019-01-02 12:52:51 +0100 | [diff] [blame] | 4 | import logging |
| 5 | import os |
| 6 | import re |
Martijn Coenen | 0c6de75 | 2019-01-02 12:52:51 +0100 | [diff] [blame] | 7 | |
Elliott Hughes | ae03b12 | 2019-09-17 16:37:05 -0700 | [diff] [blame] | 8 | from gensyscalls import SupportedArchitectures, SysCallsTxtParser |
Martijn Coenen | 0c6de75 | 2019-01-02 12:52:51 +0100 | [diff] [blame] | 9 | from genseccomp import parse_syscall_NRs |
| 10 | |
Martijn Coenen | 0c6de75 | 2019-01-02 12:52:51 +0100 | [diff] [blame] | 11 | |
Dan Albert | 1dffb86 | 2021-02-03 16:32:10 -0800 | [diff] [blame] | 12 | def 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 Coenen | 0c6de75 | 2019-01-02 12:52:51 +0100 | [diff] [blame] | 22 | |
| 23 | def gen_syscall_nrs(out_file, base_syscall_file, syscall_NRs): |
Dan Albert | 1dffb86 | 2021-02-03 16:32:10 -0800 | [diff] [blame] | 24 | for arch in SupportedArchitectures: |
| 25 | base_names = load_syscall_names_from_file(base_syscall_file, arch) |
Martijn Coenen | 0c6de75 | 2019-01-02 12:52:51 +0100 | [diff] [blame] | 26 | |
Dan Albert | 1dffb86 | 2021-02-03 16:32:10 -0800 | [diff] [blame] | 27 | for func, syscall in base_names.items(): |
| 28 | out_file.write("#define __" + arch + "_" + func + " " + |
| 29 | str(syscall_NRs[arch][syscall]) + ";\n") |
| 30 | |
Martijn Coenen | 0c6de75 | 2019-01-02 12:52:51 +0100 | [diff] [blame] | 31 | |
| 32 | def main(): |
Dan Albert | 1dffb86 | 2021-02-03 16:32:10 -0800 | [diff] [blame] | 33 | 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 Coenen | 0c6de75 | 2019-01-02 12:52:51 +0100 | [diff] [blame] | 52 | |
Dan Albert | 1dffb86 | 2021-02-03 16:32:10 -0800 | [diff] [blame] | 53 | if args.verbose: |
| 54 | logging.basicConfig(level=logging.DEBUG) |
| 55 | else: |
| 56 | logging.basicConfig(level=logging.INFO) |
Martijn Coenen | 0c6de75 | 2019-01-02 12:52:51 +0100 | [diff] [blame] | 57 | |
Dan Albert | 1dffb86 | 2021-02-03 16:32:10 -0800 | [diff] [blame] | 58 | 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 Coenen | 0c6de75 | 2019-01-02 12:52:51 +0100 | [diff] [blame] | 62 | |
Dan Albert | 1dffb86 | 2021-02-03 16:32:10 -0800 | [diff] [blame] | 63 | 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 Coenen | 0c6de75 | 2019-01-02 12:52:51 +0100 | [diff] [blame] | 69 | |
| 70 | if __name__ == "__main__": |
Dan Albert | 1dffb86 | 2021-02-03 16:32:10 -0800 | [diff] [blame] | 71 | main() |