make_syscall_h.sh 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/bin/sh
  2. # syscall reporting example for seccomp
  3. #
  4. # Copyright (c) 2012 The Chromium OS Authors <chromium-os-dev@chromium.org>
  5. # Authors:
  6. # Kees Cook <keescook@chromium.org>
  7. #
  8. # Use of this source code is governed by a BSD-style license that can be
  9. # found in the LICENSE file.
  10. CC=$1
  11. [ -n "$TARGET_CC_NOCACHE" ] && CC=$TARGET_CC_NOCACHE
  12. echo "#include <asm/unistd.h>"
  13. echo "static const char *__syscall_names[] = {"
  14. echo "#include <sys/syscall.h>" | ${CC} -E -dM - | grep '^#define __NR_[a-z0-9_]\+[ \t].*[0-9].*$' | \
  15. LC_ALL=C sed -r -n -e 's/^\#define[ \t]+__NR_([a-z0-9_]+)[ \t]+([ ()+0-9a-zNR_LSYCABE]+)(.*)/ [\2] = "\1",/p'
  16. echo "};"
  17. extra_syscalls="$(echo "#include <sys/syscall.h>" | ${CC} -E -dM - | sed -r -n -e 's/^#define __ARM_NR_([a-z0-9_]+)/\1/p')"
  18. cat <<EOF
  19. static inline const char *syscall_name(unsigned i) {
  20. if (i < ARRAY_SIZE(__syscall_names))
  21. return __syscall_names[i];
  22. switch (i) {
  23. EOF
  24. echo "$extra_syscalls" | \
  25. LC_ALL=C sed -r -n -e 's/^([a-z0-9_]+)[ \t]+([ ()+0-9a-zNR_LAMBSE]+)(.*)/ case \2: return "\1";/p'
  26. cat <<EOF
  27. default: return (void*)0;
  28. }
  29. }
  30. EOF
  31. cat <<EOF
  32. static inline int syscall_index(unsigned i) {
  33. if (i < ARRAY_SIZE(__syscall_names))
  34. return i;
  35. switch (i) {
  36. EOF
  37. echo "$extra_syscalls" | \
  38. LC_ALL=C perl -ne 'print " case $2: return ARRAY_SIZE(__syscall_names) + ", $. - 1, ";\n" if /^([a-z0-9_]+)[ \t]+([ ()+0-9a-zNR_LAMBSE]+)(.*)/;'
  39. cat <<EOF
  40. default: return -1;
  41. }
  42. }
  43. EOF
  44. cat <<EOF
  45. static inline int syscall_index_to_number(unsigned i) {
  46. if (i < ARRAY_SIZE(__syscall_names))
  47. return i;
  48. switch (i) {
  49. EOF
  50. echo "$extra_syscalls" | \
  51. LC_ALL=C perl -ne 'print " case ARRAY_SIZE(__syscall_names) + ", $. - 1, ": return $2;\n" if /^([a-z0-9_]+)[ \t]+([ ()+0-9a-zNR_LAMBSE]+)(.*)/;'
  52. cat <<EOF
  53. default: return -1;
  54. }
  55. }
  56. EOF
  57. echo "#define SYSCALL_COUNT (ARRAY_SIZE(__syscall_names) + $({ test -n "$extra_syscalls" && echo "$extra_syscalls"; } | wc -l))"