diff options
Diffstat (limited to 'libc/tools/gensyscalls.py')
-rwxr-xr-x | libc/tools/gensyscalls.py | 218 |
1 files changed, 169 insertions, 49 deletions
diff --git a/libc/tools/gensyscalls.py b/libc/tools/gensyscalls.py index e8ec636..4d0afe2 100755 --- a/libc/tools/gensyscalls.py +++ b/libc/tools/gensyscalls.py @@ -4,21 +4,29 @@ # the header files listing all available system calls, and the # makefiles used to build all the stubs. +import atexit import commands import filecmp import glob +import logging import os.path import re import shutil import stat +import string import sys +import tempfile -from bionic_utils import * -bionic_libc_root = os.environ["ANDROID_BUILD_TOP"] + "/bionic/libc/" +all_arches = [ "arm", "arm64", "mips", "mips64", "x86", "x86_64" ] + # temp directory where we store all intermediate files -bionic_temp = "/tmp/bionic_gensyscalls/" +bionic_temp = tempfile.mkdtemp(prefix="bionic_gensyscalls"); +# Make sure the directory is deleted when the script exits. +atexit.register(shutil.rmtree, bionic_temp) + +bionic_libc_root = os.path.join(os.environ["ANDROID_BUILD_TOP"], "bionic/libc") warning = "Generated by gensyscalls.py. Do not edit." @@ -34,9 +42,10 @@ def make_dir(path): def create_file(relpath): - dir = os.path.dirname(bionic_temp + relpath) + full_path = os.path.join(bionic_temp, relpath) + dir = os.path.dirname(full_path) make_dir(dir) - return open(bionic_temp + relpath, "w") + return open(full_path, "w") syscall_stub_header = "/* " + warning + " */\n" + \ @@ -47,12 +56,6 @@ ENTRY(%(func)s) """ -function_alias = """ - .globl %(alias)s - .equ %(alias)s, %(func)s -""" - - # # ARM assembler templates for each syscall stub # @@ -265,7 +268,7 @@ def count_generic_param_registers64(params): # This lets us support regular system calls like __NR_write and also weird # ones like __ARM_NR_cacheflush, where the NR doesn't come at the start. def make__NR_name(name): - if name.startswith("__"): + if name.startswith("__ARM_NR_"): return name else: return "__NR_%s" % (name) @@ -275,10 +278,11 @@ def add_footer(pointer_length, stub, syscall): # Add any aliases for this syscall. aliases = syscall["aliases"] for alias in aliases: - stub += function_alias % { "func" : syscall["func"], "alias" : alias } + stub += "\nALIAS_SYMBOL(%s, %s)\n" % (alias, syscall["func"]) - # Use hidden visibility for any functions beginning with underscores. - if pointer_length == 64 and syscall["func"].startswith("__"): + # Use hidden visibility on LP64 for any functions beginning with underscores. + # Force hidden visibility for any functions which begin with 3 underscores + if (pointer_length == 64 and syscall["func"].startswith("__")) or syscall["func"].startswith("___"): stub += '.hidden ' + syscall["func"] + '\n' return stub @@ -380,6 +384,120 @@ def x86_64_genstub(syscall): return result +class SysCallsTxtParser: + def __init__(self): + self.syscalls = [] + self.lineno = 0 + + def E(self, msg): + print "%d: %s" % (self.lineno, msg) + + def parse_line(self, line): + """ parse a syscall spec line. + + line processing, format is + return type func_name[|alias_list][:syscall_name[:socketcall_id]] ( [paramlist] ) architecture_list + """ + pos_lparen = line.find('(') + E = self.E + if pos_lparen < 0: + E("missing left parenthesis in '%s'" % line) + return + + pos_rparen = line.rfind(')') + if pos_rparen < 0 or pos_rparen <= pos_lparen: + E("missing or misplaced right parenthesis in '%s'" % line) + return + + return_type = line[:pos_lparen].strip().split() + if len(return_type) < 2: + E("missing return type in '%s'" % line) + return + + syscall_func = return_type[-1] + return_type = string.join(return_type[:-1],' ') + socketcall_id = -1 + + pos_colon = syscall_func.find(':') + if pos_colon < 0: + syscall_name = syscall_func + else: + if pos_colon == 0 or pos_colon+1 >= len(syscall_func): + E("misplaced colon in '%s'" % line) + return + + # now find if there is a socketcall_id for a dispatch-type syscall + # after the optional 2nd colon + pos_colon2 = syscall_func.find(':', pos_colon + 1) + if pos_colon2 < 0: + syscall_name = syscall_func[pos_colon+1:] + syscall_func = syscall_func[:pos_colon] + else: + if pos_colon2+1 >= len(syscall_func): + E("misplaced colon2 in '%s'" % line) + return + syscall_name = syscall_func[(pos_colon+1):pos_colon2] + socketcall_id = int(syscall_func[pos_colon2+1:]) + syscall_func = syscall_func[:pos_colon] + + alias_delim = syscall_func.find('|') + if alias_delim > 0: + alias_list = syscall_func[alias_delim+1:].strip() + syscall_func = syscall_func[:alias_delim] + alias_delim = syscall_name.find('|') + if alias_delim > 0: + syscall_name = syscall_name[:alias_delim] + syscall_aliases = string.split(alias_list, ',') + else: + syscall_aliases = [] + + if pos_rparen > pos_lparen+1: + syscall_params = line[pos_lparen+1:pos_rparen].split(',') + params = string.join(syscall_params,',') + else: + syscall_params = [] + params = "void" + + t = { + "name" : syscall_name, + "func" : syscall_func, + "aliases" : syscall_aliases, + "params" : syscall_params, + "decl" : "%-15s %s (%s);" % (return_type, syscall_func, params), + "socketcall_id" : socketcall_id + } + + # Parse the architecture list. + arch_list = line[pos_rparen+1:].strip() + if arch_list == "all": + for arch in all_arches: + t[arch] = True + else: + for arch in string.split(arch_list, ','): + if arch in all_arches: + t[arch] = True + else: + E("invalid syscall architecture '%s' in '%s'" % (arch, line)) + return + + self.syscalls.append(t) + + logging.debug(t) + + + def parse_file(self, file_path): + logging.debug("parse_file: %s" % file_path) + fp = open(file_path) + for line in fp.xreadlines(): + self.lineno += 1 + line = line.strip() + if not line: continue + if line[0] == '#': continue + self.parse_line(line) + + fp.close() + + class State: def __init__(self): self.old_stubs = [] @@ -437,22 +555,22 @@ class State: def gen_glibc_syscalls_h(self): # TODO: generate a separate file for each architecture, like glibc's bits/syscall.h. glibc_syscalls_h_path = "include/sys/glibc-syscalls.h" - D("generating " + glibc_syscalls_h_path) + logging.info("generating " + glibc_syscalls_h_path) glibc_fp = create_file(glibc_syscalls_h_path) glibc_fp.write("/* %s */\n" % warning) glibc_fp.write("#ifndef _BIONIC_GLIBC_SYSCALLS_H_\n") glibc_fp.write("#define _BIONIC_GLIBC_SYSCALLS_H_\n") glibc_fp.write("#if defined(__aarch64__)\n") - self.scan_linux_unistd_h(glibc_fp, bionic_libc_root + "/kernel/uapi/asm-generic/unistd.h") + self.scan_linux_unistd_h(glibc_fp, os.path.join(bionic_libc_root, "kernel/uapi/asm-generic/unistd.h")) glibc_fp.write("#elif defined(__arm__)\n") - self.scan_linux_unistd_h(glibc_fp, bionic_libc_root + "/kernel/uapi/asm-arm/asm/unistd.h") + self.scan_linux_unistd_h(glibc_fp, os.path.join(bionic_libc_root, "kernel/uapi/asm-arm/asm/unistd.h")) glibc_fp.write("#elif defined(__mips__)\n") - self.scan_linux_unistd_h(glibc_fp, bionic_libc_root + "/kernel/uapi/asm-mips/asm/unistd.h") + self.scan_linux_unistd_h(glibc_fp, os.path.join(bionic_libc_root, "kernel/uapi/asm-mips/asm/unistd.h")) glibc_fp.write("#elif defined(__i386__)\n") - self.scan_linux_unistd_h(glibc_fp, bionic_libc_root + "/kernel/uapi/asm-x86/asm/unistd_32.h") + self.scan_linux_unistd_h(glibc_fp, os.path.join(bionic_libc_root, "kernel/uapi/asm-x86/asm/unistd_32.h")) glibc_fp.write("#elif defined(__x86_64__)\n") - self.scan_linux_unistd_h(glibc_fp, bionic_libc_root + "/kernel/uapi/asm-x86/asm/unistd_64.h") + self.scan_linux_unistd_h(glibc_fp, os.path.join(bionic_libc_root, "kernel/uapi/asm-x86/asm/unistd_64.h")) glibc_fp.write("#endif\n") glibc_fp.write("#endif /* _BIONIC_GLIBC_SYSCALLS_H_ */\n") @@ -466,7 +584,7 @@ class State: for arch in all_arches: if syscall.has_key("asm-%s" % arch): filename = "arch-%s/syscalls/%s.S" % (arch, syscall["func"]) - D2(">>> generating " + filename) + logging.info(">>> generating " + filename) fp = create_file(filename) fp.write(syscall["asm-%s" % arch]) fp.close() @@ -474,48 +592,49 @@ class State: def regenerate(self): - D("scanning for existing architecture-specific stub files...") - - bionic_libc_root_len = len(bionic_libc_root) + logging.info("scanning for existing architecture-specific stub files...") for arch in all_arches: - arch_path = bionic_libc_root + "arch-" + arch - D("scanning " + arch_path) - files = glob.glob(arch_path + "/syscalls/*.S") - for f in files: - self.old_stubs.append(f[bionic_libc_root_len:]) + arch_dir = "arch-" + arch + logging.info("scanning " + os.path.join(bionic_libc_root, arch_dir)) + rel_path = os.path.join(arch_dir, "syscalls") + for file in os.listdir(os.path.join(bionic_libc_root, rel_path)): + if file.endswith(".S"): + self.old_stubs.append(os.path.join(rel_path, file)) - D("found %d stub files" % len(self.old_stubs)) + logging.info("found %d stub files" % len(self.old_stubs)) if not os.path.exists(bionic_temp): - D("creating %s..." % bionic_temp) + logging.info("creating %s..." % bionic_temp) make_dir(bionic_temp) - D("re-generating stubs and support files...") + logging.info("re-generating stubs and support files...") self.gen_glibc_syscalls_h() self.gen_syscall_stubs() - D("comparing files...") + logging.info("comparing files...") adds = [] edits = [] for stub in self.new_stubs + self.other_files: - if not os.path.exists(bionic_libc_root + stub): + tmp_file = os.path.join(bionic_temp, stub) + libc_file = os.path.join(bionic_libc_root, stub) + if not os.path.exists(libc_file): # new file, git add it - D("new file: " + stub) - adds.append(bionic_libc_root + stub) - shutil.copyfile(bionic_temp + stub, bionic_libc_root + stub) + logging.info("new file: " + stub) + adds.append(libc_file) + shutil.copyfile(tmp_file, libc_file) - elif not filecmp.cmp(bionic_temp + stub, bionic_libc_root + stub): - D("changed file: " + stub) + elif not filecmp.cmp(tmp_file, libc_file): + logging.info("changed file: " + stub) edits.append(stub) deletes = [] for stub in self.old_stubs: if not stub in self.new_stubs: - D("deleted file: " + stub) - deletes.append(bionic_libc_root + stub) + logging.info("deleted file: " + stub) + deletes.append(os.path.join(bionic_libc_root, stub)) if not DRY_RUN: if adds: @@ -524,18 +643,19 @@ class State: commands.getoutput("git rm " + " ".join(deletes)) if edits: for file in edits: - shutil.copyfile(bionic_temp + file, bionic_libc_root + file) - commands.getoutput("git add " + " ".join((bionic_libc_root + file) for file in edits)) + shutil.copyfile(os.path.join(bionic_temp, file), + os.path.join(bionic_libc_root, file)) + commands.getoutput("git add " + " ".join((os.path.join(bionic_libc_root, file)) for file in edits)) - commands.getoutput("git add %s%s" % (bionic_libc_root,"SYSCALLS.TXT")) + commands.getoutput("git add %s" % (os.path.join(bionic_libc_root, "SYSCALLS.TXT"))) if (not adds) and (not deletes) and (not edits): - D("no changes detected!") + logging.info("no changes detected!") else: - D("ready to go!!") + logging.info("ready to go!!") -D_setlevel(1) +logging.basicConfig(level=logging.INFO) state = State() -state.process_file(bionic_libc_root+"SYSCALLS.TXT") +state.process_file(os.path.join(bionic_libc_root, "SYSCALLS.TXT")) state.regenerate() |