^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) =========================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) GCC plugin infrastructure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) =========================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) Introduction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) ============
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) GCC plugins are loadable modules that provide extra features to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) compiler [1]_. They are useful for runtime instrumentation and static analysis.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) We can analyse, change and add further code during compilation via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) callbacks [2]_, GIMPLE [3]_, IPA [4]_ and RTL passes [5]_.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) The GCC plugin infrastructure of the kernel supports building out-of-tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) modules, cross-compilation and building in a separate directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) Plugin source files have to be compilable by a C++ compiler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) Currently the GCC plugin infrastructure supports only some architectures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) Grep "select HAVE_GCC_PLUGINS" to find out which architectures support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) GCC plugins.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) This infrastructure was ported from grsecurity [6]_ and PaX [7]_.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) --
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) .. [1] https://gcc.gnu.org/onlinedocs/gccint/Plugins.html
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) .. [2] https://gcc.gnu.org/onlinedocs/gccint/Plugin-API.html#Plugin-API
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) .. [3] https://gcc.gnu.org/onlinedocs/gccint/GIMPLE.html
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) .. [4] https://gcc.gnu.org/onlinedocs/gccint/IPA.html
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) .. [5] https://gcc.gnu.org/onlinedocs/gccint/RTL.html
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) .. [6] https://grsecurity.net/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) .. [7] https://pax.grsecurity.net/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) Files
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) =====
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) **$(src)/scripts/gcc-plugins**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) This is the directory of the GCC plugins.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) **$(src)/scripts/gcc-plugins/gcc-common.h**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) This is a compatibility header for GCC plugins.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) It should be always included instead of individual gcc headers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) **$(src)/scripts/gcc-plugins/gcc-generate-gimple-pass.h,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) $(src)/scripts/gcc-plugins/gcc-generate-ipa-pass.h,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) $(src)/scripts/gcc-plugins/gcc-generate-simple_ipa-pass.h,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) $(src)/scripts/gcc-plugins/gcc-generate-rtl-pass.h**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) These headers automatically generate the registration structures for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) GIMPLE, SIMPLE_IPA, IPA and RTL passes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) They should be preferred to creating the structures by hand.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) Usage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) =====
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) You must install the gcc plugin headers for your gcc version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) e.g., on Ubuntu for gcc-10::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) apt-get install gcc-10-plugin-dev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) Or on Fedora::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) dnf install gcc-plugin-devel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) Enable the GCC plugin infrastructure and some plugin(s) you want to use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) in the kernel config::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) CONFIG_GCC_PLUGINS=y
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) CONFIG_GCC_PLUGIN_CYC_COMPLEXITY=y
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) CONFIG_GCC_PLUGIN_LATENT_ENTROPY=y
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) To compile the minimum tool set including the plugin(s)::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) make scripts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) or just run the kernel make and compile the whole kernel with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) the cyclomatic complexity GCC plugin.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) 4. How to add a new GCC plugin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) ==============================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) The GCC plugins are in scripts/gcc-plugins/. You need to put plugin source files
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) right under scripts/gcc-plugins/. Creating subdirectories is not supported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) It must be added to scripts/gcc-plugins/Makefile, scripts/Makefile.gcc-plugins
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) and a relevant Kconfig file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) See the cyc_complexity_plugin.c (CONFIG_GCC_PLUGIN_CYC_COMPLEXITY) GCC plugin.