From 3ae644457e67f0abe50f15726bf4b316fd3cca45 Mon Sep 17 00:00:00 2001 From: Andrew Boie Date: Tue, 28 Nov 2017 13:59:15 -0800 Subject: [PATCH] gen_syscall_header: create dummy handler refs The core kernel is built with the --no-whole-archive linker option. For all the individual .o files which make up the kernel, if there are no external references to symbols within these object files, everything in the object file is dropped. This has a subtle interaction with system call handlers. If an object file has system call handlers inside it, and nothing else in the object file is referenced, then the linker will prefer the weak version of the handler in the generated syscall_dispatch.c. The user will get an "unimplemented system call" error if the associated system call for that handler is made. Fix this by making a fake reference to the handler function at the system call site. The address gets stored inside a special section "hndlr_ref". This is enough to prevent the handlers from being dropped, and the hndlr_ref section is itself dropped from the binary from gc-sections; these references will not consume space. Handlers for system calls that are never invoked anywhere will still be dropped if nothing else in their containing C files is used, which is a good thing. A future enhancement could be to split out all handlers into individual object files, such that we can guarantee that any system call that is not made somewhere in the application will have its handler dropped. This will need to be extended to driver subsystems as well. This won't be pretty but will ensure the tightest binary size. Fixes #5184. Signed-off-by: Andrew Boie --- scripts/gen_syscall_header.py | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/scripts/gen_syscall_header.py b/scripts/gen_syscall_header.py index b5ae1243d332..627b8a10d73e 100755 --- a/scripts/gen_syscall_header.py +++ b/scripts/gen_syscall_header.py @@ -40,7 +40,32 @@ def gen_fn(ret, argc, name, extern=False): sys.stdout.write(", ") sys.stdout.write(")") -def gen_make_syscall(ret, argc): +def tabs(count): + sys.stdout.write("\t" * count); + +def gen_make_syscall(ret, argc, tabcount): + tabs(tabcount) + + # The core kernel is built with the --no-whole-archive linker option. + # For all the individual .o files which make up the kernel, if there + # are no external references to symbols within these object files, + # everything in the object file is dropped. + # + # This has a subtle interaction with system call handlers. If an object + # file has system call handler inside it, and nothing else in the + # object file is referenced, then the linker will prefer the weak + # version of the handler in the generated syscall_dispatch.c. The + # user will get an "unimplemented system call" error if the associated + # system call for that handler is made. + # + # Fix this by making a fake reference to the handler function at the + # system call site. The address gets stored inside a special section + # "hndlr_ref". This is enough to prevent the handlers from being + # dropped, and the hndlr_ref section is itself dropped from the binary + # from gc-sections; these references will not consume space. + + sys.stdout.write("static _GENERIC_SECTION(hndlr_ref) __used void *href = (void *)&_handler_##name; \\\n") + tabs(tabcount) if (ret != Retval.VOID): sys.stdout.write("return (ret)") if (argc <= 6 and ret != Retval.U64): @@ -83,14 +108,12 @@ def gen_defines_inner(ret, argc, kernel_only=False, user_only=False): sys.stdout.write("\t\t") gen_call_impl(ret, argc) elif user_only: - sys.stdout.write("\t\t") - gen_make_syscall(ret, argc) + gen_make_syscall(ret, argc, 2) else: sys.stdout.write("\t\tif (_is_user_context()) {") newline() - sys.stdout.write("\t\t\t") - gen_make_syscall(ret, argc) + gen_make_syscall(ret, argc, 3) sys.stdout.write("\t\t} else {") newline()