1#!/usr/bin/env python 2 3import argparse 4import collections 5import logging 6import os 7import re 8import subprocess 9import textwrap 10 11from gensyscalls import SupportedArchitectures, SysCallsTxtParser 12from genseccomp import parse_syscall_NRs 13 14def 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 24def gen_syscall_nrs(out_file, base_syscall_file, syscall_NRs): 25 for arch in SupportedArchitectures: 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 31def 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 59if __name__ == "__main__": 60 main() 61