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