gcc-plugins.rst 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. =========================
  2. GCC plugin infrastructure
  3. =========================
  4. Introduction
  5. ============
  6. GCC plugins are loadable modules that provide extra features to the
  7. compiler [1]_. They are useful for runtime instrumentation and static analysis.
  8. We can analyse, change and add further code during compilation via
  9. callbacks [2]_, GIMPLE [3]_, IPA [4]_ and RTL passes [5]_.
  10. The GCC plugin infrastructure of the kernel supports building out-of-tree
  11. modules, cross-compilation and building in a separate directory.
  12. Plugin source files have to be compilable by a C++ compiler.
  13. Currently the GCC plugin infrastructure supports only some architectures.
  14. Grep "select HAVE_GCC_PLUGINS" to find out which architectures support
  15. GCC plugins.
  16. This infrastructure was ported from grsecurity [6]_ and PaX [7]_.
  17. --
  18. .. [1] https://gcc.gnu.org/onlinedocs/gccint/Plugins.html
  19. .. [2] https://gcc.gnu.org/onlinedocs/gccint/Plugin-API.html#Plugin-API
  20. .. [3] https://gcc.gnu.org/onlinedocs/gccint/GIMPLE.html
  21. .. [4] https://gcc.gnu.org/onlinedocs/gccint/IPA.html
  22. .. [5] https://gcc.gnu.org/onlinedocs/gccint/RTL.html
  23. .. [6] https://grsecurity.net/
  24. .. [7] https://pax.grsecurity.net/
  25. Purpose
  26. =======
  27. GCC plugins are designed to provide a place to experiment with potential
  28. compiler features that are neither in GCC nor Clang upstream. Once
  29. their utility is proven, the goal is to upstream the feature into GCC
  30. (and Clang), and then to finally remove them from the kernel once the
  31. feature is available in all supported versions of GCC.
  32. Specifically, new plugins should implement only features that have no
  33. upstream compiler support (in either GCC or Clang).
  34. When a feature exists in Clang but not GCC, effort should be made to
  35. bring the feature to upstream GCC (rather than just as a kernel-specific
  36. GCC plugin), so the entire ecosystem can benefit from it.
  37. Similarly, even if a feature provided by a GCC plugin does *not* exist
  38. in Clang, but the feature is proven to be useful, effort should be spent
  39. to upstream the feature to GCC (and Clang).
  40. After a feature is available in upstream GCC, the plugin will be made
  41. unbuildable for the corresponding GCC version (and later). Once all
  42. kernel-supported versions of GCC provide the feature, the plugin will
  43. be removed from the kernel.
  44. Files
  45. =====
  46. **$(src)/scripts/gcc-plugins**
  47. This is the directory of the GCC plugins.
  48. **$(src)/scripts/gcc-plugins/gcc-common.h**
  49. This is a compatibility header for GCC plugins.
  50. It should be always included instead of individual gcc headers.
  51. **$(src)/scripts/gcc-plugins/gcc-generate-gimple-pass.h,
  52. $(src)/scripts/gcc-plugins/gcc-generate-ipa-pass.h,
  53. $(src)/scripts/gcc-plugins/gcc-generate-simple_ipa-pass.h,
  54. $(src)/scripts/gcc-plugins/gcc-generate-rtl-pass.h**
  55. These headers automatically generate the registration structures for
  56. GIMPLE, SIMPLE_IPA, IPA and RTL passes.
  57. They should be preferred to creating the structures by hand.
  58. Usage
  59. =====
  60. You must install the gcc plugin headers for your gcc version,
  61. e.g., on Ubuntu for gcc-10::
  62. apt-get install gcc-10-plugin-dev
  63. Or on Fedora::
  64. dnf install gcc-plugin-devel libmpc-devel
  65. Or on Fedora when using cross-compilers that include plugins::
  66. dnf install libmpc-devel
  67. Enable the GCC plugin infrastructure and some plugin(s) you want to use
  68. in the kernel config::
  69. CONFIG_GCC_PLUGINS=y
  70. CONFIG_GCC_PLUGIN_LATENT_ENTROPY=y
  71. ...
  72. Run gcc (native or cross-compiler) to ensure plugin headers are detected::
  73. gcc -print-file-name=plugin
  74. CROSS_COMPILE=arm-linux-gnu- ${CROSS_COMPILE}gcc -print-file-name=plugin
  75. The word "plugin" means they are not detected::
  76. plugin
  77. A full path means they are detected::
  78. /usr/lib/gcc/x86_64-redhat-linux/12/plugin
  79. To compile the minimum tool set including the plugin(s)::
  80. make scripts
  81. or just run the kernel make and compile the whole kernel with
  82. the cyclomatic complexity GCC plugin.
  83. 4. How to add a new GCC plugin
  84. ==============================
  85. The GCC plugins are in scripts/gcc-plugins/. You need to put plugin source files
  86. right under scripts/gcc-plugins/. Creating subdirectories is not supported.
  87. It must be added to scripts/gcc-plugins/Makefile, scripts/Makefile.gcc-plugins
  88. and a relevant Kconfig file.