Mauro Carvalho Chehab | ccd8d55 | 2017-05-14 13:45:26 -0300 | [diff] [blame] | 1 | ========================= |
Emese Revfy | 6b90bd4 | 2016-05-24 00:09:38 +0200 | [diff] [blame] | 2 | GCC plugin infrastructure |
| 3 | ========================= |
| 4 | |
| 5 | |
Mauro Carvalho Chehab | ccd8d55 | 2017-05-14 13:45:26 -0300 | [diff] [blame] | 6 | Introduction |
| 7 | ============ |
Emese Revfy | 6b90bd4 | 2016-05-24 00:09:38 +0200 | [diff] [blame] | 8 | |
| 9 | GCC plugins are loadable modules that provide extra features to the |
Mauro Carvalho Chehab | ccd8d55 | 2017-05-14 13:45:26 -0300 | [diff] [blame] | 10 | compiler [1]_. They are useful for runtime instrumentation and static analysis. |
Emese Revfy | 6b90bd4 | 2016-05-24 00:09:38 +0200 | [diff] [blame] | 11 | We can analyse, change and add further code during compilation via |
Mauro Carvalho Chehab | ccd8d55 | 2017-05-14 13:45:26 -0300 | [diff] [blame] | 12 | callbacks [2]_, GIMPLE [3]_, IPA [4]_ and RTL passes [5]_. |
Emese Revfy | 6b90bd4 | 2016-05-24 00:09:38 +0200 | [diff] [blame] | 13 | |
Masahiro Yamada | 9b61643 | 2021-01-23 22:33:33 +0900 | [diff] [blame] | 14 | The GCC plugin infrastructure of the kernel supports building out-of-tree |
| 15 | modules, cross-compilation and building in a separate directory. |
| 16 | Plugin source files have to be compilable by a C++ compiler. |
Emese Revfy | 6b90bd4 | 2016-05-24 00:09:38 +0200 | [diff] [blame] | 17 | |
Masahiro Yamada | 9b61643 | 2021-01-23 22:33:33 +0900 | [diff] [blame] | 18 | Currently the GCC plugin infrastructure supports only some architectures. |
| 19 | Grep "select HAVE_GCC_PLUGINS" to find out which architectures support |
| 20 | GCC plugins. |
Emese Revfy | 6b90bd4 | 2016-05-24 00:09:38 +0200 | [diff] [blame] | 21 | |
Mauro Carvalho Chehab | ccd8d55 | 2017-05-14 13:45:26 -0300 | [diff] [blame] | 22 | This infrastructure was ported from grsecurity [6]_ and PaX [7]_. |
Emese Revfy | 6b90bd4 | 2016-05-24 00:09:38 +0200 | [diff] [blame] | 23 | |
| 24 | -- |
Mauro Carvalho Chehab | ccd8d55 | 2017-05-14 13:45:26 -0300 | [diff] [blame] | 25 | |
| 26 | .. [1] https://gcc.gnu.org/onlinedocs/gccint/Plugins.html |
| 27 | .. [2] https://gcc.gnu.org/onlinedocs/gccint/Plugin-API.html#Plugin-API |
| 28 | .. [3] https://gcc.gnu.org/onlinedocs/gccint/GIMPLE.html |
| 29 | .. [4] https://gcc.gnu.org/onlinedocs/gccint/IPA.html |
| 30 | .. [5] https://gcc.gnu.org/onlinedocs/gccint/RTL.html |
| 31 | .. [6] https://grsecurity.net/ |
| 32 | .. [7] https://pax.grsecurity.net/ |
Emese Revfy | 6b90bd4 | 2016-05-24 00:09:38 +0200 | [diff] [blame] | 33 | |
| 34 | |
Kees Cook | 8bd51a2 | 2021-10-20 10:35:53 -0700 | [diff] [blame] | 35 | Purpose |
| 36 | ======= |
| 37 | |
| 38 | GCC plugins are designed to provide a place to experiment with potential |
| 39 | compiler features that are neither in GCC nor Clang upstream. Once |
| 40 | their utility is proven, the goal is to upstream the feature into GCC |
| 41 | (and Clang), and then to finally remove them from the kernel once the |
| 42 | feature is available in all supported versions of GCC. |
| 43 | |
| 44 | Specifically, new plugins should implement only features that have no |
| 45 | upstream compiler support (in either GCC or Clang). |
| 46 | |
| 47 | When a feature exists in Clang but not GCC, effort should be made to |
| 48 | bring the feature to upstream GCC (rather than just as a kernel-specific |
| 49 | GCC plugin), so the entire ecosystem can benefit from it. |
| 50 | |
| 51 | Similarly, even if a feature provided by a GCC plugin does *not* exist |
| 52 | in Clang, but the feature is proven to be useful, effort should be spent |
| 53 | to upstream the feature to GCC (and Clang). |
| 54 | |
| 55 | After a feature is available in upstream GCC, the plugin will be made |
| 56 | unbuildable for the corresponding GCC version (and later). Once all |
| 57 | kernel-supported versions of GCC provide the feature, the plugin will |
| 58 | be removed from the kernel. |
| 59 | |
| 60 | |
Mauro Carvalho Chehab | ccd8d55 | 2017-05-14 13:45:26 -0300 | [diff] [blame] | 61 | Files |
| 62 | ===== |
Emese Revfy | 6b90bd4 | 2016-05-24 00:09:38 +0200 | [diff] [blame] | 63 | |
Mauro Carvalho Chehab | ccd8d55 | 2017-05-14 13:45:26 -0300 | [diff] [blame] | 64 | **$(src)/scripts/gcc-plugins** |
| 65 | |
Emese Revfy | 6b90bd4 | 2016-05-24 00:09:38 +0200 | [diff] [blame] | 66 | This is the directory of the GCC plugins. |
| 67 | |
Mauro Carvalho Chehab | ccd8d55 | 2017-05-14 13:45:26 -0300 | [diff] [blame] | 68 | **$(src)/scripts/gcc-plugins/gcc-common.h** |
| 69 | |
Emese Revfy | 6b90bd4 | 2016-05-24 00:09:38 +0200 | [diff] [blame] | 70 | This is a compatibility header for GCC plugins. |
| 71 | It should be always included instead of individual gcc headers. |
| 72 | |
Mauro Carvalho Chehab | ccd8d55 | 2017-05-14 13:45:26 -0300 | [diff] [blame] | 73 | **$(src)/scripts/gcc-plugins/gcc-generate-gimple-pass.h, |
| 74 | $(src)/scripts/gcc-plugins/gcc-generate-ipa-pass.h, |
| 75 | $(src)/scripts/gcc-plugins/gcc-generate-simple_ipa-pass.h, |
| 76 | $(src)/scripts/gcc-plugins/gcc-generate-rtl-pass.h** |
| 77 | |
Emese Revfy | 6b90bd4 | 2016-05-24 00:09:38 +0200 | [diff] [blame] | 78 | These headers automatically generate the registration structures for |
Masahiro Yamada | 9b61643 | 2021-01-23 22:33:33 +0900 | [diff] [blame] | 79 | GIMPLE, SIMPLE_IPA, IPA and RTL passes. |
Emese Revfy | 6b90bd4 | 2016-05-24 00:09:38 +0200 | [diff] [blame] | 80 | They should be preferred to creating the structures by hand. |
| 81 | |
| 82 | |
Mauro Carvalho Chehab | ccd8d55 | 2017-05-14 13:45:26 -0300 | [diff] [blame] | 83 | Usage |
| 84 | ===== |
Emese Revfy | 6b90bd4 | 2016-05-24 00:09:38 +0200 | [diff] [blame] | 85 | |
| 86 | You must install the gcc plugin headers for your gcc version, |
Masahiro Yamada | 9b61643 | 2021-01-23 22:33:33 +0900 | [diff] [blame] | 87 | e.g., on Ubuntu for gcc-10:: |
Emese Revfy | 6b90bd4 | 2016-05-24 00:09:38 +0200 | [diff] [blame] | 88 | |
Masahiro Yamada | 9b61643 | 2021-01-23 22:33:33 +0900 | [diff] [blame] | 89 | apt-get install gcc-10-plugin-dev |
Emese Revfy | 6b90bd4 | 2016-05-24 00:09:38 +0200 | [diff] [blame] | 90 | |
Michael Ellerman | 43e96ef | 2020-02-21 11:48:43 +1100 | [diff] [blame] | 91 | Or on Fedora:: |
| 92 | |
| 93 | dnf install gcc-plugin-devel |
| 94 | |
Masahiro Yamada | 9b61643 | 2021-01-23 22:33:33 +0900 | [diff] [blame] | 95 | Enable the GCC plugin infrastructure and some plugin(s) you want to use |
| 96 | in the kernel config:: |
Emese Revfy | 6b90bd4 | 2016-05-24 00:09:38 +0200 | [diff] [blame] | 97 | |
Masahiro Yamada | 9b61643 | 2021-01-23 22:33:33 +0900 | [diff] [blame] | 98 | CONFIG_GCC_PLUGINS=y |
Masahiro Yamada | 9b61643 | 2021-01-23 22:33:33 +0900 | [diff] [blame] | 99 | CONFIG_GCC_PLUGIN_LATENT_ENTROPY=y |
| 100 | ... |
Emese Revfy | 6b90bd4 | 2016-05-24 00:09:38 +0200 | [diff] [blame] | 101 | |
Masahiro Yamada | 9b61643 | 2021-01-23 22:33:33 +0900 | [diff] [blame] | 102 | To compile the minimum tool set including the plugin(s):: |
Emese Revfy | 6b90bd4 | 2016-05-24 00:09:38 +0200 | [diff] [blame] | 103 | |
Masahiro Yamada | 9b61643 | 2021-01-23 22:33:33 +0900 | [diff] [blame] | 104 | make scripts |
Emese Revfy | 6b90bd4 | 2016-05-24 00:09:38 +0200 | [diff] [blame] | 105 | |
| 106 | or just run the kernel make and compile the whole kernel with |
| 107 | the cyclomatic complexity GCC plugin. |
| 108 | |
| 109 | |
| 110 | 4. How to add a new GCC plugin |
| 111 | ============================== |
| 112 | |
Masahiro Yamada | 9b61643 | 2021-01-23 22:33:33 +0900 | [diff] [blame] | 113 | The GCC plugins are in scripts/gcc-plugins/. You need to put plugin source files |
| 114 | right under scripts/gcc-plugins/. Creating subdirectories is not supported. |
| 115 | It must be added to scripts/gcc-plugins/Makefile, scripts/Makefile.gcc-plugins |
| 116 | and a relevant Kconfig file. |