162306a36Sopenharmony_ci========================= 262306a36Sopenharmony_ciGCC plugin infrastructure 362306a36Sopenharmony_ci========================= 462306a36Sopenharmony_ci 562306a36Sopenharmony_ci 662306a36Sopenharmony_ciIntroduction 762306a36Sopenharmony_ci============ 862306a36Sopenharmony_ci 962306a36Sopenharmony_ciGCC plugins are loadable modules that provide extra features to the 1062306a36Sopenharmony_cicompiler [1]_. They are useful for runtime instrumentation and static analysis. 1162306a36Sopenharmony_ciWe can analyse, change and add further code during compilation via 1262306a36Sopenharmony_cicallbacks [2]_, GIMPLE [3]_, IPA [4]_ and RTL passes [5]_. 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_ciThe GCC plugin infrastructure of the kernel supports building out-of-tree 1562306a36Sopenharmony_cimodules, cross-compilation and building in a separate directory. 1662306a36Sopenharmony_ciPlugin source files have to be compilable by a C++ compiler. 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_ciCurrently the GCC plugin infrastructure supports only some architectures. 1962306a36Sopenharmony_ciGrep "select HAVE_GCC_PLUGINS" to find out which architectures support 2062306a36Sopenharmony_ciGCC plugins. 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ciThis infrastructure was ported from grsecurity [6]_ and PaX [7]_. 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_ci-- 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_ci.. [1] https://gcc.gnu.org/onlinedocs/gccint/Plugins.html 2762306a36Sopenharmony_ci.. [2] https://gcc.gnu.org/onlinedocs/gccint/Plugin-API.html#Plugin-API 2862306a36Sopenharmony_ci.. [3] https://gcc.gnu.org/onlinedocs/gccint/GIMPLE.html 2962306a36Sopenharmony_ci.. [4] https://gcc.gnu.org/onlinedocs/gccint/IPA.html 3062306a36Sopenharmony_ci.. [5] https://gcc.gnu.org/onlinedocs/gccint/RTL.html 3162306a36Sopenharmony_ci.. [6] https://grsecurity.net/ 3262306a36Sopenharmony_ci.. [7] https://pax.grsecurity.net/ 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ciPurpose 3662306a36Sopenharmony_ci======= 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ciGCC plugins are designed to provide a place to experiment with potential 3962306a36Sopenharmony_cicompiler features that are neither in GCC nor Clang upstream. Once 4062306a36Sopenharmony_citheir utility is proven, the goal is to upstream the feature into GCC 4162306a36Sopenharmony_ci(and Clang), and then to finally remove them from the kernel once the 4262306a36Sopenharmony_cifeature is available in all supported versions of GCC. 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ciSpecifically, new plugins should implement only features that have no 4562306a36Sopenharmony_ciupstream compiler support (in either GCC or Clang). 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ciWhen a feature exists in Clang but not GCC, effort should be made to 4862306a36Sopenharmony_cibring the feature to upstream GCC (rather than just as a kernel-specific 4962306a36Sopenharmony_ciGCC plugin), so the entire ecosystem can benefit from it. 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_ciSimilarly, even if a feature provided by a GCC plugin does *not* exist 5262306a36Sopenharmony_ciin Clang, but the feature is proven to be useful, effort should be spent 5362306a36Sopenharmony_cito upstream the feature to GCC (and Clang). 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ciAfter a feature is available in upstream GCC, the plugin will be made 5662306a36Sopenharmony_ciunbuildable for the corresponding GCC version (and later). Once all 5762306a36Sopenharmony_cikernel-supported versions of GCC provide the feature, the plugin will 5862306a36Sopenharmony_cibe removed from the kernel. 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_ciFiles 6262306a36Sopenharmony_ci===== 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ci**$(src)/scripts/gcc-plugins** 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ci This is the directory of the GCC plugins. 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci**$(src)/scripts/gcc-plugins/gcc-common.h** 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ci This is a compatibility header for GCC plugins. 7162306a36Sopenharmony_ci It should be always included instead of individual gcc headers. 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_ci**$(src)/scripts/gcc-plugins/gcc-generate-gimple-pass.h, 7462306a36Sopenharmony_ci$(src)/scripts/gcc-plugins/gcc-generate-ipa-pass.h, 7562306a36Sopenharmony_ci$(src)/scripts/gcc-plugins/gcc-generate-simple_ipa-pass.h, 7662306a36Sopenharmony_ci$(src)/scripts/gcc-plugins/gcc-generate-rtl-pass.h** 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_ci These headers automatically generate the registration structures for 7962306a36Sopenharmony_ci GIMPLE, SIMPLE_IPA, IPA and RTL passes. 8062306a36Sopenharmony_ci They should be preferred to creating the structures by hand. 8162306a36Sopenharmony_ci 8262306a36Sopenharmony_ci 8362306a36Sopenharmony_ciUsage 8462306a36Sopenharmony_ci===== 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_ciYou must install the gcc plugin headers for your gcc version, 8762306a36Sopenharmony_cie.g., on Ubuntu for gcc-10:: 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_ci apt-get install gcc-10-plugin-dev 9062306a36Sopenharmony_ci 9162306a36Sopenharmony_ciOr on Fedora:: 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ci dnf install gcc-plugin-devel libmpc-devel 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_ciOr on Fedora when using cross-compilers that include plugins:: 9662306a36Sopenharmony_ci 9762306a36Sopenharmony_ci dnf install libmpc-devel 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ciEnable the GCC plugin infrastructure and some plugin(s) you want to use 10062306a36Sopenharmony_ciin the kernel config:: 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_ci CONFIG_GCC_PLUGINS=y 10362306a36Sopenharmony_ci CONFIG_GCC_PLUGIN_LATENT_ENTROPY=y 10462306a36Sopenharmony_ci ... 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_ciRun gcc (native or cross-compiler) to ensure plugin headers are detected:: 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_ci gcc -print-file-name=plugin 10962306a36Sopenharmony_ci CROSS_COMPILE=arm-linux-gnu- ${CROSS_COMPILE}gcc -print-file-name=plugin 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ciThe word "plugin" means they are not detected:: 11262306a36Sopenharmony_ci 11362306a36Sopenharmony_ci plugin 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ciA full path means they are detected:: 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_ci /usr/lib/gcc/x86_64-redhat-linux/12/plugin 11862306a36Sopenharmony_ci 11962306a36Sopenharmony_ciTo compile the minimum tool set including the plugin(s):: 12062306a36Sopenharmony_ci 12162306a36Sopenharmony_ci make scripts 12262306a36Sopenharmony_ci 12362306a36Sopenharmony_cior just run the kernel make and compile the whole kernel with 12462306a36Sopenharmony_cithe cyclomatic complexity GCC plugin. 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_ci 12762306a36Sopenharmony_ci4. How to add a new GCC plugin 12862306a36Sopenharmony_ci============================== 12962306a36Sopenharmony_ci 13062306a36Sopenharmony_ciThe GCC plugins are in scripts/gcc-plugins/. You need to put plugin source files 13162306a36Sopenharmony_ciright under scripts/gcc-plugins/. Creating subdirectories is not supported. 13262306a36Sopenharmony_ciIt must be added to scripts/gcc-plugins/Makefile, scripts/Makefile.gcc-plugins 13362306a36Sopenharmony_ciand a relevant Kconfig file. 134