tst-mman-consts.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/python3
  2. # Test that glibc's sys/mman.h constants match the kernel's.
  3. # Copyright (C) 2018-2019 Free Software Foundation, Inc.
  4. # This file is part of the GNU C Library.
  5. #
  6. # The GNU C Library is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU Lesser General Public
  8. # License as published by the Free Software Foundation; either
  9. # version 2.1 of the License, or (at your option) any later version.
  10. #
  11. # The GNU C Library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. # Lesser General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Lesser General Public
  17. # License along with the GNU C Library; if not, see
  18. # <http://www.gnu.org/licenses/>.
  19. import argparse
  20. import sys
  21. import glibcextract
  22. def linux_kernel_version(cc):
  23. """Return the (major, minor) version of the Linux kernel headers."""
  24. sym_data = ['#include <linux/version.h>', 'START',
  25. ('LINUX_VERSION_CODE', 'LINUX_VERSION_CODE')]
  26. val = glibcextract.compute_c_consts(sym_data, cc)['LINUX_VERSION_CODE']
  27. val = int(val)
  28. return ((val & 0xff0000) >> 16, (val & 0xff00) >> 8)
  29. def main():
  30. """The main entry point."""
  31. parser = argparse.ArgumentParser(
  32. description="Test that glibc's sys/mman.h constants "
  33. "match the kernel's.")
  34. parser.add_argument('--cc', metavar='CC',
  35. help='C compiler (including options) to use')
  36. args = parser.parse_args()
  37. linux_version_headers = linux_kernel_version(args.cc)
  38. linux_version_glibc = (4, 20)
  39. sys.exit(glibcextract.compare_macro_consts(
  40. '#define _GNU_SOURCE 1\n'
  41. '#include <sys/mman.h>\n',
  42. '#define _GNU_SOURCE 1\n'
  43. '#include <linux/mman.h>\n',
  44. args.cc,
  45. 'MAP_.*',
  46. # A series of MAP_HUGE_<size> macros are defined by the kernel
  47. # but not by glibc. MAP_UNINITIALIZED is kernel-only.
  48. # MAP_FAILED is not a MAP_* flag and is glibc-only, as is the
  49. # MAP_ANON alias for MAP_ANONYMOUS. MAP_RENAME, MAP_AUTOGROW,
  50. # MAP_LOCAL and MAP_AUTORSRV are in the kernel header for
  51. # MIPS, marked as "not used by linux"; SPARC has MAP_INHERIT
  52. # in the kernel header, but does not use it.
  53. 'MAP_HUGE_[0-9].*|MAP_UNINITIALIZED|MAP_FAILED|MAP_ANON'
  54. '|MAP_RENAME|MAP_AUTOGROW|MAP_LOCAL|MAP_AUTORSRV|MAP_INHERIT',
  55. linux_version_glibc > linux_version_headers,
  56. linux_version_headers > linux_version_glibc))
  57. if __name__ == '__main__':
  58. main()