Introduce GCC plugin infrastructure
Subsystems (8)
v5 cover Introduce GCC plugin infrastructure
This patch set introduce the GCC plugin infrastructure with examples for testing and documentation.
GCC plugins are loadable modules that provide extra features to the compiler. They are useful for runtime instrumentation and static analysis.
The infrastructure supports all gcc versions from 4.5 to 6.0, building out-of-tree modules and building in a separate directory. Cross-compilation is supported too but currently only the x86 architecture enables plugins.
This infrastructure was ported from grsecurity/PaX. It is a CII project supported by the Linux Foundation.
Emese Revfy (5):
Shared library support
GCC plugin infrastructure
Add Cyclomatic complexity plugin
Documentations of the GCC plugin infrastructre
Add sancov plugin
Changes from v4:
* Moved shared library support from the GCC plugin infrastructure patch into a different patch
* Update gcc-*.h from PaX
* Fixed gcc-common.h for gcc 6
* Added pass cloning support to the gcc pass generators
* Disable all plugins in vdso because it is userland code
* Add sancov gcc plugin: another use case for gcc plugin support in the kernel is when there is a feature in the latest gcc version and we would like to use it with older gcc versions as well (e.g., distros).
Changes from v3:
* Fix some indentation related warnings
(Suggested by checkpatch.pl)
* Add maintainer entries
* Don't run gcc_plugin.sh when the GCC_PLUGINS option is disabled or unsupported (Reported-by: Fengguang Wu <[email protected]>)
Changes from v2:
* Fixed incorrectly encoded characters
* Generate the GIMPLE, IPA, SIMPLE_IPA and RTL pass structures (Suggested-by: Rasmus Villemoes <[email protected]>)
* Write plugin related warning messages to stderr instead of stdout (Suggested-by: Kees Cook <[email protected]>)
* Mention the installation of the gcc plugin headers (Documentation)
Changes from v1:
* Move the gcc-plugins make target into a separate Makefile because there may be a lot of plugins (Suggested-by: Rasmus Villemoes)
* Simplify the dependencies of the plugin related config option (Suggested-by: Kees Cook <[email protected]>)
* Removed the unnecessary example plugin
--- Documentation/dontdiff | 1 + Documentation/gcc-plugins.txt | 82 ++++++++++++++++++ MAINTAINERS | 8 ++ Makefile | 41 ++++++--- arch/Kconfig | 36 ++++++++ arch/x86/Kconfig | 1 + arch/x86/entry/vdso/Makefile | 2 +- init/Makefile | 3 + scripts/Makefile.gcc-plugins | 35 ++++++++ scripts/gcc-plugin.sh | 51 +++++++++++ scripts/link-vmlinux.sh | 2 +- scripts/package/builddeb | 1 + tools/gcc/Makefile | 21 +++++ tools/gcc/cyc_complexity_plugin.c | 73 ++++++++++++++++ tools/gcc/gcc-common.h | 830 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tools/gcc/gcc-generate-gimple-pass.h | 175 ++++++++++++++++++++++++++++++++++++++ tools/gcc/gcc-generate-ipa-pass.h | 289 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tools/gcc/gcc-generate-rtl-pass.h | 175 ++++++++++++++++++++++++++++++++++++++ tools/gcc/gcc-generate-simple_ipa-pass.h | 175 ++++++++++++++++++++++++++++++++++++++ tools/gcc/sancov_plugin.c | 133 +++++++++++++++++++++++++++++ 20 files changed, 2121 insertions(+), 13 deletions(-)
PATCH v5 1/5 Shared library support
Infrastructure for building independent shared library targets. This effectively also reverts commit 62e2210798ed38928ab24841e8b4878a (Masahiro Yamada, kbuild: drop shared library support from Makefile.host).
--- scripts/Makefile.build | 2 +- scripts/Makefile.clean | 3 ++- scripts/Makefile.host | 69 +++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 71 insertions(+), 3 deletions(-)
scripts/Makefile.build +1 -1
@@ -60,7 +60,7 @@ endif endif # Do not include host rules unless needed -ifneq ($(hostprogs-y)$(hostprogs-m),) +ifneq ($(hostprogs-y)$(hostprogs-m)$(hostlibs-y)$(hostlibs-m)$(hostcxxlibs-y)$(hostcxxlibs-m),) include scripts/Makefile.host endif
scripts/Makefile.clean +2 -1
@@ -38,7 +38,8 @@ subdir-ymn := $(addprefix $(obj)/,$(subdir-ymn)) __clean-files := $(extra-y) $(extra-m) $(extra-) \ $(always) $(targets) $(clean-files) \ $(host-progs) \ - $(hostprogs-y) $(hostprogs-m) $(hostprogs-) + $(hostprogs-y) $(hostprogs-m) $(hostprogs-) \ + $(hostlibs-y) $(hostlibs-m) $(hostlibs-) __clean-files := $(filter-out $(no-clean-files), $(__clean-files))
scripts/Makefile.host +68 -1
92 lines not on this page · raw message
Signature · 1 line
2.4.1
PATCH v5 2/5 GCC plugin infrastructure
In reply to Emese RevfyThis patch allows to build the whole kernel with GCC plugins. It was ported from grsecurity/PaX. The infrastructure supports building out-of-tree modules and building in a separate directory. Cross-compilation is supported too but currently only the x86 architecture enables plugins.
The directory of the gcc plugins is tools/gcc. You can use a file or a directory there. The plugins compile with these options:
* -fno-rtti: gcc is compiled with this option so the plugins must use it too
* -fno-exceptions: this is inherited from gcc too
* -fasynchronous-unwind-tables: this is inherited from gcc too
* -ggdb: it is useful for debugging a plugin (better backtrace on internal errors)
* -Wno-narrowing: to suppress warnings from gcc headers (ipa-utils.h)
* -Wno-unused-variable: to suppress warnings from gcc headers (gcc_version variable, plugin-version.h)
The infrastructure introduces a new Makefile target called gcc-plugins. It supports all gcc versions from 4.5 to 6.0. The scripts/gcc-plugin.sh script chooses the proper host compiler (gcc-4.7 can be built by either gcc or g++). This script also checks the availability of the included headers in tools/gcc/gcc-common.h.
The gcc-common.h header contains frequently included headers for GCC plugins and it has a compatibility layer for the supported gcc versions.
The gcc-generate-*-pass.h headers automatically generate the registration structures for GIMPLE, SIMPLE_IPA, IPA and RTL passes.
Note that 'make clean' keeps the *.so files (only the distclean or mrproper targets clean all) because they are needed for out-of-tree modules.
--- Documentation/dontdiff | 1 + MAINTAINERS | 8 + Makefile | 41 +- arch/Kconfig | 13 + arch/x86/Kconfig | 1 + arch/x86/entry/vdso/Makefile | 2 +- init/Makefile | 3 + scripts/Makefile.gcc-plugins | 28 ++ scripts/gcc-plugin.sh | 51 ++ scripts/link-vmlinux.sh | 2 +- scripts/package/builddeb | 1 + tools/gcc/Makefile | 15 + tools/gcc/gcc-common.h | 830 +++++++++++++++++++++++++++++++ tools/gcc/gcc-generate-gimple-pass.h | 175 +++++++ tools/gcc/gcc-generate-ipa-pass.h | 289 +++++++++++ tools/gcc/gcc-generate-rtl-pass.h | 175 +++++++ tools/gcc/gcc-generate-simple_ipa-pass.h | 175 +++++++ 17 files changed, 1797 insertions(+), 13 deletions(-) create mode 100644 scripts/Makefile.gcc-plugins create mode 100644 scripts/gcc-plugin.sh create mode 100644 tools/gcc/Makefile create mode 100644 tools/gcc/gcc-common.h create mode 100644 tools/gcc/gcc-generate-gimple-pass.h create mode 100644 tools/gcc/gcc-generate-ipa-pass.h create mode 100644 tools/gcc/gcc-generate-rtl-pass.h create mode 100644 tools/gcc/gcc-generate-simple_ipa-pass.h
Documentation/dontdiff +1 -0
@@ -3,6 +3,7 @@ *.bc *.bin *.bz2 +*.c.[012]*.* *.cis *.cpio *.csp
MAINTAINERS +8 -0
@@ -4760,6 +4760,14 @@ L: [email protected] S: Odd Fixes (e.g., new signatures) F: drivers/scsi/fdomain.* +GCC PLUGINS +M: Kees Cook <[email protected]> +R: Emese Revfy <[email protected]> +L: [email protected] +S: Maintained +F: tools/gcc +F: Documentation/gcc-plugins.txt + GCOV BASED KERNEL PROFILING M: Peter Oberparleiter <[email protected]> S: Maintained
Makefile +30 -11
121 lines not on this page · raw message
arch/Kconfig +13 -0
@@ -357,6 +357,19 @@ config SECCOMP_FILTER See Documentation/prctl/seccomp_filter.txt for details. +config HAVE_GCC_PLUGINS + bool + help + An arch should select this symbol if it supports building with + GCC plugins. + +menuconfig GCC_PLUGINS + bool "GCC plugins" + depends on HAVE_GCC_PLUGINS + help + GCC plugins are loadable modules that provide extra features to the + compiler. They are useful for runtime instrumentation and static analysis. + config HAVE_CC_STACKPROTECTOR bool help
arch/x86/Kconfig +1 -0
@@ -160,6 +160,7 @@ config X86 select ARCH_USES_HIGH_VMA_FLAGS if X86_INTEL_MEMORY_PROTECTION_KEYS select ARCH_HAS_PKEYS if X86_INTEL_MEMORY_PROTECTION_KEYS select HAVE_STACK_VALIDATION if X86_64 + select HAVE_GCC_PLUGINS config INSTRUCTION_DECODER def_bool y
arch/x86/entry/vdso/Makefile +1 -1
@@ -74,7 +74,7 @@ CFL := $(PROFILING) -mcmodel=small -fPIC -O2 -fasynchronous-unwind-tables -m64 \ -fno-omit-frame-pointer -foptimize-sibling-calls \ -DDISABLE_BRANCH_PROFILING -DBUILD_VDSO -$(vobjs): KBUILD_CFLAGS += $(CFL) +$(vobjs): KBUILD_CFLAGS := $(filter-out $(GCC_PLUGINS_CFLAGS),$(KBUILD_CFLAGS)) $(CFL) # # vDSO code runs in userspace and -pg doesn't help with profiling anyway.
init/Makefile +3 -0
@@ -2,6 +2,9 @@ # Makefile for the linux kernel. # +ccflags-y := $(GCC_PLUGINS_CFLAGS) +asflags-y := $(GCC_PLUGINS_AFLAGS) + obj-y := main.o version.o mounts.o ifneq ($(CONFIG_BLK_DEV_INITRD),y) obj-y += noinitramfs.o
scripts/Makefile.gcc-plugins new file +28 -0
@@ -0,0 +1,28 @@ +ifdef CONFIG_GCC_PLUGINS +ifeq ($(call cc-ifversion, -ge, 0408, y), y) +PLUGINCC := $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-plugin.sh "$(HOSTCXX)" "$(HOSTCXX)" "$(CC)") +else +PLUGINCC := $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-plugin.sh "$(HOSTCC)" "$(HOSTCXX)" "$(CC)") +endif +ifneq ($(PLUGINCC),) +export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGINS_AFLAGS +ifeq ($(KBUILD_EXTMOD),) +gcc-plugins: + $(Q)$(MAKE) $(build)=tools/gcc +else +gcc-plugins: ; +endif +else +gcc-plugins: +ifeq ($(call cc-ifversion, -ge, 0405, y), y) + $(warning warning, your gcc installation does not support plugins, perhaps the necessary headers are missing?) +ifeq ($(call cc-ifversion, -ge, 0408, y), y) + $(CONFIG_SHELL) -x $(srctree)/scripts/gcc-plugin.sh "$(HOSTCXX)" "$(HOSTCXX)" "$(CC)" +else + $(CONFIG_SHELL) -x $(srctree)/scripts/gcc-plugin.sh "$(HOSTCC)" "$(HOSTCXX)" "$(CC)" +endif +else + $(warning warning, your gcc version does not support plugins, you should upgrade it to gcc 4.5 at least) +endif +endif +endif
scripts/gcc-plugin.sh new file +51 -0
51 lines not on this page · raw message
scripts/link-vmlinux.sh +1 -1
@@ -184,7 +184,7 @@ else fi; # final build of init/ -${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init +${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init GCC_PLUGINS_CFLAGS="${GCC_PLUGINS_CFLAGS}" GCC_PLUGINS_AFLAGS="${GCC_PLUGINS_AFLAGS}" kallsymso="" kallsyms_vmlinux=""
scripts/package/builddeb +1 -0
@@ -326,6 +326,7 @@ fi (cd $srctree; find arch/$SRCARCH -name module.lds -o -name Kbuild.platforms -o -name Platform) >> "$objtree/debian/hdrsrcfiles" (cd $srctree; find $(find arch/$SRCARCH -name include -o -name scripts -type d) -type f) >> "$objtree/debian/hdrsrcfiles" (cd $objtree; find arch/$SRCARCH/include Module.symvers include scripts -type f) >> "$objtree/debian/hdrobjfiles" +(cd $objtree; find tools/gcc -name \*.so -o -name gcc-common.h) >> "$objtree/debian/hdrobjfiles" destdir=$kernel_headers_dir/usr/src/linux-headers-$version mkdir -p "$destdir" (cd $srctree; tar -c -f - -T -) < "$objtree/debian/hdrsrcfiles" | (cd $destdir; tar -xf -)
tools/gcc/Makefile new file +15 -0
@@ -0,0 +1,15 @@ +GCC_PLUGINS_DIR := $(shell $(CC) -print-file-name=plugin) + +ifeq ($(PLUGINCC),$(HOSTCC)) +HOSTLIBS := hostlibs +HOST_EXTRACFLAGS += -I$(GCC_PLUGINS_DIR)/include -I$(src) -std=gnu99 -ggdb +export HOST_EXTRACFLAGS +else +HOSTLIBS := hostcxxlibs +HOST_EXTRACXXFLAGS += -I$(GCC_PLUGINS_DIR)/include -I$(src) -std=gnu++98 -fno-rtti -fno-exceptions -fasynchronous-unwind-tables -ggdb -Wno-narrowing -Wno-unused-variable +export HOST_EXTRACXXFLAGS +endif + +export GCCPLUGINS_DIR HOSTLIBS + +always := $($(HOSTLIBS)-y)
tools/gcc/gcc-common.h new file +830 -0
830 lines not on this page · raw message
tools/gcc/gcc-generate-gimple-pass.h new file +175 -0
175 lines not on this page · raw message
tools/gcc/gcc-generate-ipa-pass.h new file +289 -0
289 lines not on this page · raw message
tools/gcc/gcc-generate-rtl-pass.h new file +175 -0
175 lines not on this page · raw message
tools/gcc/gcc-generate-simple_ipa-pass.h new file +175 -0
175 lines not on this page · raw message
Signature · 1 line
2.4.1
PATCH v5 3/5 Add Cyclomatic complexity GCC plugin
In reply to Emese RevfyAdd a very simple plugin to demonstrate the GCC plugin infrastructure. This GCC plugin computes the cyclomatic complexity of each function.
The complexity M of a function's control flow graph is defined as:
M = E - N + 2P
where
E = the number of edges
N = the number of nodes
P = the number of connected components (exit nodes).
--- arch/Kconfig | 12 +++++++ scripts/Makefile.gcc-plugins | 6 +++- tools/gcc/Makefile | 4 +++ tools/gcc/cyc_complexity_plugin.c | 73 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 tools/gcc/cyc_complexity_plugin.c
arch/Kconfig +12 -0
@@ -370,6 +370,18 @@ menuconfig GCC_PLUGINS GCC plugins are loadable modules that provide extra features to the compiler. They are useful for runtime instrumentation and static analysis. +config GCC_PLUGIN_CYC_COMPLEXITY + bool "Compute the cyclomatic complexity of a function" + depends on GCC_PLUGINS + help + The complexity M of a function's control flow graph is defined as: + M = E - N + 2P + where + + E = the number of edges + N = the number of nodes + P = the number of connected components (exit nodes). + config HAVE_CC_STACKPROTECTOR bool help
scripts/Makefile.gcc-plugins +5 -1
@@ -5,7 +5,11 @@ else PLUGINCC := $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-plugin.sh "$(HOSTCC)" "$(HOSTCXX)" "$(CC)") endif ifneq ($(PLUGINCC),) -export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGINS_AFLAGS +ifdef CONFIG_GCC_PLUGIN_CYC_COMPLEXITY +GCC_PLUGIN_CYC_COMPLEXITY_CFLAGS := -fplugin=$(objtree)/tools/gcc/cyc_complexity_plugin.so +endif +GCC_PLUGINS_CFLAGS := $(GCC_PLUGIN_CYC_COMPLEXITY_CFLAGS) +export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGINS_AFLAGS GCC_PLUGIN_CYC_COMPLEXITY ifeq ($(KBUILD_EXTMOD),) gcc-plugins: $(Q)$(MAKE) $(build)=tools/gcc
tools/gcc/Makefile +4 -0
@@ -12,4 +12,8 @@ endif export GCCPLUGINS_DIR HOSTLIBS +$(HOSTLIBS)-$(CONFIG_GCC_PLUGIN_CYC_COMPLEXITY) := cyc_complexity_plugin.so + always := $($(HOSTLIBS)-y) + +cyc_complexity_plugin-objs := cyc_complexity_plugin.o
tools/gcc/cyc_complexity_plugin.c new file +73 -0
73 lines not on this page · raw message
Signature · 1 line
2.4.1
PATCH v5 4/5 Documentation for the GCC plugin infrastructure
In reply to Emese RevfyThis is the GCC infrastructure documentation about its operation, how to add and use a new plugin with an example.
--- Documentation/gcc-plugins.txt | 82 +++++++++++++++++++++++++++++++++++++++++++ arch/Kconfig | 2 ++ 2 files changed, 84 insertions(+) create mode 100644 Documentation/gcc-plugins.txt
Documentation/gcc-plugins.txt new file +82 -0
@@ -0,0 +1,82 @@ +GCC plugin infrastructure +========================= + + +1. Introduction +=============== + +GCC plugins are loadable modules that provide extra features to the +compiler [1]. They are useful for runtime instrumentation and static analysis. +We can analyse, change and add further code during compilation via +callbacks [2], GIMPLE [3], IPA [4] and RTL passes [5]. + +The GCC plugin infrastructure of the kernel supports all gcc versions from +4.5 to 6.0, building out-of-tree modules, cross-compilation and building in a +separate directory. + +Currently the GCC plugin infrastructure supports only the x86 architecture. + +This infrastructure was ported from grsecurity [6] and PaX [7]. + +-- +[1] https://gcc.gnu.org/onlinedocs/gccint/Plugins.html +[2] https://gcc.gnu.org/onlinedocs/gccint/Plugin-API.html#Plugin-API +[3] https://gcc.gnu.org/onlinedocs/gccint/GIMPLE.html +[4] https://gcc.gnu.org/onlinedocs/gccint/IPA.html +[5] https://gcc.gnu.org/onlinedocs/gccint/RTL.html +[6] https://grsecurity.net/ +[7] https://pax.grsecurity.net/ + + +2. Files +======== + +$(src)/tools/gcc + This is the directory of the GCC plugins. + +$(src)/tools/gcc/gcc-common.h + This is a compatibility header for GCC plugins. + It should be always included instead of individual gcc headers. + +$(src)/scripts/gcc-plugin.sh + This script checks the availability of the included headers in + gcc-common.h and chooses the proper host compiler to build the plugins + (gcc-4.7 can be built by either gcc or g++). + +$(src)/tools/gcc/gcc-generate-gimple-pass.h +$(src)/tools/gcc/gcc-generate-ipa-pass.h +$(src)/tools/gcc/gcc-generate-simple_ipa-pass.h +$(src)/tools/gcc/gcc-generate-rtl-pass.h + These headers automatically generate the registration structures for + GIMPLE, SIMPLE_IPA, IPA and RTL passes. They support all gcc versions + from 4.5 to 6.0. + They should be preferred to creating the structures by hand. + + +3. Usage +======== + +You must install the gcc plugin headers for your gcc version, +e.g., on Ubuntu for gcc-4.9: + + apt-get install gcc-4.9-plugin-dev + +Enable a GCC plugin based feature in the kernel config: + + CONFIG_GCC_PLUGIN_CYC_COMPLEXITY = y + +To compile only the plugin(s): + + make gcc-plugins + +or just run the kernel make and compile the whole kernel with +the cyclomatic complexity GCC plugin. + + +4. How to add a new GCC plugin +============================== + +The GCC plugins are in $(src)/tools/gcc/. You can use a file or a directory +here. It must be added to $(src)/tools/gcc/Makefile, +$(src)/scripts/Makefile.gcc-plugins and $(src)/arch/Kconfig. +See the cyc_complexity_plugin.c (CONFIG_GCC_PLUGIN_CYC_COMPLEXITY) GCC plugin.
arch/Kconfig +2 -0
@@ -370,6 +370,8 @@ menuconfig GCC_PLUGINS GCC plugins are loadable modules that provide extra features to the compiler. They are useful for runtime instrumentation and static analysis. + See Documentation/gcc-plugins.txt for details. + config GCC_PLUGIN_CYC_COMPLEXITY bool "Compute the cyclomatic complexity of a function" depends on GCC_PLUGINS
Signature · 1 line
2.4.1
PATCH v5 5/5 Add sancov plugin
In reply to Emese RevfyThe sancov gcc plugin inserts a __sanitizer_cov_trace_pc() call at the start of basic blocks.
This plugin is a helper plugin for the kcov feature. It supports all gcc versions with plugin support (from gcc-4.5 on).
It is based on the gcc commit "Add fuzzing coverage support" by Dmitry Vyukov
(https://gcc.gnu.org/viewcvs/gcc?limit_changes=0&view=revision&revision=231296).
--- arch/Kconfig | 9 +++ scripts/Makefile.gcc-plugins | 7 ++- tools/gcc/Makefile | 2 + tools/gcc/sancov_plugin.c | 133 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 tools/gcc/sancov_plugin.c
arch/Kconfig +9 -0
@@ -384,6 +384,15 @@ config GCC_PLUGIN_CYC_COMPLEXITY N = the number of nodes P = the number of connected components (exit nodes). +config GCC_PLUGIN_SANCOV + bool "Helper plugin for the kcov feature" + depends on GCC_PLUGINS + help + This plugin inserts a __sanitizer_cov_trace_pc() call at the start of + basic blocks. It supports all gcc versions with plugin support (from + gcc-4.5 on). It is based on the commit "Add fuzzing coverage support" + by Dmitry Vyukov <[email protected]>. + config HAVE_CC_STACKPROTECTOR bool help
scripts/Makefile.gcc-plugins +5 -2
@@ -8,8 +8,11 @@ ifneq ($(PLUGINCC),) ifdef CONFIG_GCC_PLUGIN_CYC_COMPLEXITY GCC_PLUGIN_CYC_COMPLEXITY_CFLAGS := -fplugin=$(objtree)/tools/gcc/cyc_complexity_plugin.so endif -GCC_PLUGINS_CFLAGS := $(GCC_PLUGIN_CYC_COMPLEXITY_CFLAGS) -export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGINS_AFLAGS GCC_PLUGIN_CYC_COMPLEXITY +ifdef CONFIG_GCC_PLUGIN_SANCOV +GCC_PLUGIN_SANCOV_CFLAGS := -fplugin=$(objtree)/tools/gcc/sancov_plugin.so +endif +GCC_PLUGINS_CFLAGS := $(GCC_PLUGIN_CYC_COMPLEXITY_CFLAGS) $(GCC_PLUGIN_SANCOV_CFLAGS) +export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGINS_AFLAGS GCC_PLUGIN_CYC_COMPLEXITY GCC_PLUGIN_SANCOV ifeq ($(KBUILD_EXTMOD),) gcc-plugins: $(Q)$(MAKE) $(build)=tools/gcc
tools/gcc/Makefile +2 -0
@@ -13,7 +13,9 @@ endif export GCCPLUGINS_DIR HOSTLIBS $(HOSTLIBS)-$(CONFIG_GCC_PLUGIN_CYC_COMPLEXITY) := cyc_complexity_plugin.so +$(HOSTLIBS)-$(CONFIG_GCC_PLUGIN_SANCOV) += sancov_plugin.so always := $($(HOSTLIBS)-y) cyc_complexity_plugin-objs := cyc_complexity_plugin.o +sancov_plugin-objs := sancov_plugin.o
tools/gcc/sancov_plugin.c new file +133 -0
133 lines not on this page · raw message
Signature · 1 line
2.4.1
Re: [PATCH v5 1/5] Shared library support
In reply to Emese RevfyQuoted 3 lines · Emese Revfy
Infrastructure for building independent shared library targets. This effectively also reverts commit
62e2210798ed38928ab24841e8b4878a(Masahiro Yamada, kbuild: drop shared library support from Makefile.host).
Should this literally be a revert? If so, we'd gain the Documentation changes back too.
-Kees
Quoted 7 lines
Signed-off-by Emese Revfy <[email protected]>--- scripts/Makefile.build | 2 +- scripts/Makefile.clean | 3 ++- scripts/Makefile.host | 69 +++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 71 insertions(+), 3 deletions(-)
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Re: [PATCH v5 5/5] Add sancov plugin
In reply to Emese RevfyQuoted 7 lines · Emese Revfy
The sancov gcc plugin inserts a
__sanitizer_cov_trace_pc()call at the start of basic blocks.This plugin is a helper plugin for the kcov feature. It supports all gcc versions with plugin support (from gcc-4.5 on).
It is based on the gcc commit "Add fuzzing coverage support" by Dmitry Vyukov
(https://gcc.gnu.org/viewcvs/gcc?limit_changes=0&view=revision&revision=231296).
Very cool! Dmitry, is this something you could put to good use?
Quoted 16 lines
[...]
tools/gcc/sancov_plugin.c new file +9
-0@@ -0,0 +1,133 @@ +/* +* Copyright 2011-2016 by Emese Revfy <[email protected]> +* Licensed under the GPL v2, or (at your option) v3 +* +* Homepage: +* https://github.com/ephox-gcc-plugins/sancov +* +* This plugin inserts a __sanitizer_cov_trace_pc() call at the start of basic blocks. +* It supports all gcc versions with plugin support (from gcc-4.5 on).
tools/gcc/sancov_plugin.c line 19
Quoted 10 lines
tools/gcc/sancov_plugin.c new file +10
-0+* It is based on the commit "Add fuzzing coverage support" by Dmitry Vyukov <[email protected]>. +* +* You can read about it more here: +* https://gcc.gnu.org/viewcvs/gcc?limit_changes=0&view=revision&revision=231296 +* http://lwn.net/Articles/674854/ +* https://github.com/google/syzkaller +* https://lwn.net/Articles/677764/ +* +* Usage: +* make run
Is this accurate? Wouldn't it just be selected from CONFIGs during kernel build?
-Kees
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Re: [PATCH v5 5/5] Add sancov plugin
Quoted 19 lines · On Mon, 7 Mar 2016 13:07:32 -0800 Kees Cook <[email protected]> wrote:
Quoted 19 lines
tools/gcc/sancov_plugin.c new file +13
-0@@ -0,0 +1,133 @@ +/* +* Copyright 2011-2016 by Emese Revfy <[email protected]> +* Licensed under the GPL v2, or (at your option) v3 +* +* Homepage: +* https://github.com/ephox-gcc-plugins/sancov +* +* This plugin inserts a __sanitizer_cov_trace_pc() call at the start of basic blocks. +* It supports all gcc versions with plugin support (from gcc-4.5 on). +* It is based on the commit "Add fuzzing coverage support" by Dmitry Vyukov <[email protected]>. +* +* You can read about it more here: +* https://gcc.gnu.org/viewcvs/gcc?limit_changes=0&view=revision&revision=231296
Quoted 8 lines
tools/gcc/sancov_plugin.c line 19
Quoted 6 lines
tools/gcc/sancov_plugin.c new file +6
-0+* http://lwn.net/Articles/674854/ +* https://github.com/google/syzkaller +* https://lwn.net/Articles/677764/ +* +* Usage: +* make run Is this accurate? Wouldn't it just be selected from CONFIGs during kernel build?
This is just a usage example when someone clones the plugin from github. Every plugin written by me contains this. :)
For now it can be enabled from menuconfig for testing because I'm waiting for the reactions. Later I think the best way would be when kcov is enabled that it also enables the plugin automatically if the target gcc version doesn't support the -fsanitize-coverage=trace-pc option but it supports plugins.
Signature · 1 line
Emese
Re: [PATCH v5 1/5] Shared library support
In reply to Kees CookQuoted 7 lines · On Mon, 7 Mar 2016 13:05:16 -0800 Kees Cook <[email protected]> wrote:
Quoted 3 lines · Emese Revfy
Infrastructure for building independent shared library targets. This effectively also reverts commit
62e2210798ed38928ab24841e8b4878a(Masahiro Yamada, kbuild: drop shared library support from Makefile.host).Should this literally be a revert? If so, we'd gain the Documentation changes back too.
Sure, I'll do it in the next patch set.
Signature · 1 line
Emese
Re: [PATCH v5 5/5] Add sancov plugin
In reply to Kees CookQuoted 10 lines · Kees Cook
Quoted 7 lines · Emese Revfy
The sancov gcc plugin inserts a
__sanitizer_cov_trace_pc()call at the start of basic blocks.This plugin is a helper plugin for the kcov feature. It supports all gcc versions with plugin support (from gcc-4.5 on).
It is based on the gcc commit "Add fuzzing coverage support" by Dmitry Vyukov
(https://gcc.gnu.org/viewcvs/gcc?limit_changes=0&view=revision&revision=231296).Very cool! Dmitry, is this something you could put to good use?
I am using gcc with sancov as a regular pass, so I don't a good use for this right now. Maybe it will help with deployment for Android. I don't know. Though, it will require backporting this plugin support.
Re: [kernel-hardening] [PATCH v5 2/5] GCC plugin infrastructure
In reply to Emese RevfyQuoted 4 lines · Emese Revfy
This patch allows to build the whole kernel with GCC plugins. It was ported from grsecurity/PaX. The infrastructure supports building out-of-tree modules and building in a separate directory. Cross-compilation is supported too but currently only the x86 architecture enables plugins.
I've tested this with both ARM and ARM64. There are some missing headers in the arm64 gcc, reported here:
https://bugs.linaro.org/show_bug.cgi?id=2123 (also upstream), but this should work once that is fixed.
Feel free to fold these into your patch, or, if you prefer, I can send out separate patches for them.
arch/arm/Kconfig +0 -0
@@ -54,6 +54,7 @@ config ARM
select HAVE_FUNCTION_GRAPH_TRACER if (!THUMB2_KERNEL) select HAVE_FUNCTION_TRACER if (!XIP_KERNEL) select HAVE_GENERIC_DMA_COHERENT
+ select HAVE_GCC_PLUGINS
select HAVE_HW_BREAKPOINT if (PERF_EVENTS && (CPU_V6 || CPU_V6K || CPU_V7)) select HAVE_IDE if PCI || ISA || PCMCIA select HAVE_IRQ_TIME_ACCOUNTING
arch/arm64/Kconfig +0 -0
@@ -70,6 +70,7 @@ config ARM64
select HAVE_FTRACE_MCOUNT_RECORD
select HAVE_FUNCTION_TRACER
select HAVE_FUNCTION_GRAPH_TRACER
+ select HAVE_GCC_PLUGINS
select HAVE_GENERIC_DMA_COHERENT
select HAVE_HW_BREAKPOINT if PERF_EVENTS
select HAVE_IRQ_TIME_ACCOUNTING
Re: [kernel-hardening] [PATCH v5 2/5] GCC plugin infrastructure
Quoted 10 lines · David Brown
Quoted 6 lines · Emese Revfy
This patch allows to build the whole kernel with GCC plugins. It was ported from
grsecurity/PaX. The infrastructure supports building out-of-tree modules and
building in a separate directory. Cross-compilation is supported too but currently only the x86 architecture enables plugins.
Quoted 7 lines
I've tested this with both ARM and ARM64. There are some missing headers in the arm64 gcc, reported here:
https://bugs.linaro.org/show_bug.cgi?id=2123 (also upstream), but this should work once that is fixed.Feel free to fold these into your patch, or, if you prefer, I can send out separate patches for them.
Ah-ha, great! Thanks for testing!
Emese, if you're not interested in carrying this, I can add it to my tree.
-Kees
Quoted 29 lines
Signed-off-by David Brown <[email protected]>arch/arm/Kconfig +1
-0@@ -54,6 +54,7 @@ config ARM select HAVE_FUNCTION_GRAPH_TRACER if (!THUMB2_KERNEL) select HAVE_FUNCTION_TRACER if (!XIP_KERNEL) select HAVE_GENERIC_DMA_COHERENT + select HAVE_GCC_PLUGINS select HAVE_HW_BREAKPOINT if (PERF_EVENTS && (CPU_V6 || CPU_V6K || CPU_V7)) select HAVE_IDE if PCI || ISA || PCMCIAselect
HAVE_IRQ_TIME_ACCOUNTINGarch/arm64/Kconfig +1
-1@@ -70,6 +70,7 @@ config ARM64 select HAVE_FTRACE_MCOUNT_RECORD select HAVE_FUNCTION_TRACER select HAVE_FUNCTION_GRAPH_TRACER + select HAVE_GCC_PLUGINS select HAVE_GENERIC_DMA_COHERENT select HAVE_HW_BREAKPOINT if PERF_EVENTS select HAVE_IRQ_TIME_ACCOUNTING --
Signature · 2 lines
Kees Cook Chrome OS & Brillo Security
Re: [kernel-hardening] [PATCH v5 2/5] GCC plugin infrastructure
In reply to David BrownQuoted 2 lines · On Wed, 9 Mar 2016 02:01:15 -0700 David Brown <[email protected]> wrote:
Feel free to fold these into your patch, or, if you prefer, I can send out separate patches for them.
Thanks, I'll take it.
Signature · 1 line
Emese
Re: [kernel-hardening] [PATCH v5 2/5] GCC plugin infrastructure
In reply to Kees CookQuoted 6 lines · On Wed, 9 Mar 2016 12:50:26 -0800 Kees Cook <[email protected]> wrote:
Quoted 2 lines
Feel free to fold these into your patch, or, if you prefer, I can send out separate patches for them.
Ah-ha, great! Thanks for testing!
Emese, if you're not interested in carrying this, I can add it to my tree.
I think it is easier if it is in my patch set.
Signature · 1 line
Emese
Re: [PATCH v5 1/5] Shared library support
In reply to Emese RevfyHi Emese,
2016-03-07 8:03 GMT+09:00 Emese Revfy <[email protected]>:
Quoted 45 lines
Infrastructure for building independent shared library targets. This effectively also reverts commit
62e2210798ed38928ab24841e8b4878a(Masahiro Yamada, kbuild: drop shared library support from Makefile.host).Signed-off-by Emese Revfy <[email protected]>--- scripts/Makefile.build | 2 +- scripts/Makefile.clean | 3 ++- scripts/Makefile.host | 69 +++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 71 insertions(+), 3 deletions(-)scripts/Makefile.build +1
-1@@ -60,7 +60,7 @@ endif endif # Do not include host rules unless needed -ifneq ($(hostprogs-y)$(hostprogs-m),) +ifneq ($(hostprogs-y)$(hostprogs-m)$(hostlibs-y)$(hostlibs-m)$(hostcxxlibs-y)$(hostcxxlibs-m),) include scripts/Makefile.host endifscripts/Makefile.clean +2
-1@@ -38,7 +38,8 @@ subdir-ymn := $(addprefix $(obj)/,$(subdir-ymn)) __clean-files := $(extra-y) $(extra-m) $(extra-) \ $(always) $(targets) $(clean-files) \ $(host-progs) \ - $(hostprogs-y) $(hostprogs-m) $(hostprogs-) + $(hostprogs-y) $(hostprogs-m) $(hostprogs-) \ + $(hostlibs-y) $(hostlibs-m) $(hostlibs-) __clean-files := $(filter-out $(no-clean-files), $(__clean-files))scripts/Makefile.host +0
-0@@ -20,7 +20,25 @@ # Will compile qconf as a C++ program, and menu as a C program. # They are linked as C++ code to the executable qconf
scripts/Makefile.host line 31
Quoted 10 lines
scripts/Makefile.host +9
-0+# hostprogs-y := conf +# conf-objs := conf.o libkconfig.so +# libkconfig-objs := expr.o type.o +# Will create a shared library named libkconfig.so that consists of +# expr.o and type.o (they are both compiled as C code and the object files +# are made as position independent code). +# conf.c is compiled as a C program, and conf.o is linked together with +# libkconfig.so as the executable conf. +# Note: Shared libraries consisting of C++ files are not supported
I do not see any use-case for the above.
This series adds cases for "foo.so" as a final target.
As an alternative, you can add needed build rules into tools/gcc/Makefile, not scripts/Makefile.host
I guess these rule won't be used in other places.
Quoted 13 lines
scripts/Makefile.host +8
-0+# hostcc-option +# Usage: cflags-y += $(call hostcc-option,-march=winchip-c6,-march=i586) + +hostcc-option = $(call try-run,\ + $(HOSTCC) $(HOSTCFLAGS) $(HOST_EXTRACFLAGS) $(1) -c -x c /dev/null -o "$$TMP",$(1),$(2)) + __hostprogs := $(sort $(hostprogs-y) $(hostprogs-m)) +__hostlibs := $(sort $(hostlibs-y) $(hostlibs-m)) +__hostcxxlibs := $(sort $(hostcxxlibs-y) $(hostcxxlibs-m)) # C code # Executables compiled from a single .c file
scripts/Makefile.host host-cxxmulti := $(foreach m,$(__hostprogs),$(if $($(m)-cxxobjs),$(m))) · line 65
Quoted 6 lines
scripts/Makefile.host +3
-0@@ -42,6 +60,19 @@ host-cxxmulti := $(foreach m,$(__hostprogs),$(if $($(m)-cxxobjs),$(m))) # C++ Object (.o) files compiled from .cc files host-cxxobjs := $(sort $(foreach m,$(host-cxxmulti),$($(m)-cxxobjs))) +# Shared libaries (only .c supported) +# Shared libraries (.so) - all .so files referenced in "xxx-objs" +host-cshlib := $(sort $(filter %.so, $(host-cobjs)))
useless.
Quoted 40 lines
scripts/Makefile.host +26
-0+host-cshlib += $(sort $(filter %.so, $(__hostlibs))) +host-cxxshlib := $(sort $(filter %.so, $(__hostcxxlibs))) +# Remove .so files from "xxx-objs" +host-cobjs := $(filter-out %.so,$(host-cobjs)) +host-cxxobjs := $(filter-out %.so,$(host-cxxobjs)) + +# Object (.o) files used by the shared libaries +host-cshobjs := $(sort $(foreach m,$(host-cshlib),$($(m:.so=-objs)))) +host-cxxshobjs := $(sort $(foreach m,$(host-cxxshlib),$($(m:.so=-objs)))) + # output directory for programs/.o files # hostprogs-y := tools/build may have been specified. # Retrieve also directory of .o files from prog-objs or prog-cxxobjs notation@@ -56,6 +87,10 @@ host-cmulti := $(addprefix $(obj)/,$(host-cmulti)) host-cobjs := $(addprefix $(obj)/,$(host-cobjs)) host-cxxmulti := $(addprefix $(obj)/,$(host-cxxmulti)) host-cxxobjs := $(addprefix $(obj)/,$(host-cxxobjs)) +host-cshlib := $(addprefix $(obj)/,$(host-cshlib)) +host-cxxshlib := $(addprefix $(obj)/,$(host-cxxshlib)) +host-cshobjs := $(addprefix $(obj)/,$(host-cshobjs)) +host-cxxshobjs := $(addprefix $(obj)/,$(host-cxxshobjs)) host-objdirs := $(addprefix $(obj)/,$(host-objdirs)) obj-dirs += $(host-objdirs)@@ -124,5 +159,37 @@ quiet_cmd_host-cxxobjs = HOSTCXX $@ $(host-cxxobjs): $(obj)/%.o: $(src)/%.cc FORCE $(call if_changed_dep,host-cxxobjs) +# Compile .c file, create position independent .o file +# host-cshobjs -> .o +quiet_cmd_host-cshobjs = HOSTCC -fPIC $@ + cmd_host-cshobjs = $(HOSTCC) $(hostc_flags) -fPIC -c -o $@ $< +$(host-cshobjs): $(obj)/%.o: $(src)/%.c FORCE + $(call if_changed_dep,host-cshobjs) + +# Compile .c file, create position independent .o file +# host-cxxshobjs -> .o +quiet_cmd_host-cxxshobjs = HOSTCXX -fPIC $@ + cmd_host-cxxshobjs = $(HOSTCXX) $(hostcxx_flags) -fPIC -c -o $@ $< +$(host-cxxshobjs): $(obj)/%.o: $(src)/%.c FORCE
scripts/Makefile.host quiet_cmd_host-cxxobjs = HOSTCXX $@ · line 183
Quoted 10 lines
scripts/Makefile.host +10
-0+ $(call if_changed_dep,host-cxxshobjs) + +# Link a shared library, based on position independent .o files +# *.o -> .so shared library (host-cshlib) +quiet_cmd_host-cshlib = HOSTLLD -shared $@ + cmd_host-cshlib = $(HOSTCC) $(HOSTLDFLAGS) -shared -o $@ \ + $(addprefix $(obj)/,$($(@F:.so=-objs))) \ + $(HOST_LOADLIBES) $(HOSTLOADLIBES_$(@F)) +$(host-cshlib): $(obj)/%: $(host-cshobjs) FORCE + $(call if_changed,host-cshlib)
Could you use $(call multi-depend, ...)
if you need to handle multi-object please?
Please refer to commit c8589d1e9e01 and commit 97e3226e6e984c8.
But, I still do not see any gcc-plugin that is large enough to be linked from multiple objects.
Signature · 2 lines
Best Regards Masahiro Yamada
Re: [PATCH v5 2/5] GCC plugin infrastructure
In reply to Emese RevfyHi Emese,
I am not familiar with GCC plugins.
Comments from the view of Kbuild.
2016-03-07 8:04 GMT+09:00 Emese Revfy <[email protected]>:
Quoted 6 lines
This patch allows to build the whole kernel with GCC plugins. It was ported from grsecurity/PaX. The infrastructure supports building out-of-tree modules and building in a separate directory. Cross-compilation is supported too but currently only the x86 architecture enables plugins.
The directory of the gcc plugins is
tools/gcc. You can use a file or a directory
Maybe scripts/gcc-plugins/ is better than tools/gcc ?
In the directory "scripts/", we have several tools used during building the kernel image.
We have some optional programs in the directory "tools/", which are not used for building the kernel image itself.
Please correct me if I am wrong.
You sprinkle "gcc-plugins" target in the top Makefile, which I do not like.
Can you descend into scripts/gcc-plugins from scripts/Makefile?
subdir-$(CONFIG_MODVERSIONS) += genksyms
subdir-y += mod
subdir-$(CONFIG_SECURITY_SELINUX) += selinux
subdir-$(CONFIG_DTC) += dtc
subdir-$(CONFIG_GDB_SCRIPTS) += gdb
+subdir-$(CONFIG_GCC_PLUGINS) += gcc-plugins
This is how other host tools do, I think.
Quoted 13 lines
init/Makefile +3
-0+ccflags-y := $(GCC_PLUGINS_CFLAGS) +asflags-y := $(GCC_PLUGINS_AFLAGS) + obj-y := main.o version.o mounts.o ifneq ($(CONFIG_BLK_DEV_INITRD),y) obj-y += noinitramfs.oscripts/Makefile.gcc-plugins new file +0
-0
scripts/Makefile.gcc-plugins line 6
Quoted 6 lines
scripts/Makefile.gcc-plugins new file +6
-0@@ -0,0 +1,28 @@ +ifdef CONFIG_GCC_PLUGINS +ifeq ($(call cc-ifversion, -ge, 0408, y), y) +PLUGINCC := $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-plugin.sh "$(HOSTCXX)" "$(HOSTCXX)" "$(CC)") +else +PLUGINCC := $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-plugin.sh "$(HOSTCC)" "$(HOSTCXX)" "$(CC)") +endif
The difference is only the first argument.
Can you make it as follows?
__HOSTCC := $(call cc-ifversion, -ge, 0408, $(HOSTCXX), $(HOSTCC))
PLUGINCC := $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-plugin.sh "$(__HOSTCC)" "$(HOSTCXX)" "$(CC)")
I did not come up with a good name for __HOSTCC.
Feel free to replace it with a better one.
Quoted 12 lines
scripts/Makefile.gcc-plugins new file +12
-0+ifneq ($(PLUGINCC),) +export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGINS_AFLAGS +ifeq ($(KBUILD_EXTMOD),) +gcc-plugins: + $(Q)$(MAKE) $(build)=tools/gcc +else +gcc-plugins: ; +endif +else +gcc-plugins: +ifeq ($(call cc-ifversion, -ge, 0405, y), y) + $(warning warning, your gcc installation does not support plugins, perhaps the necessary headers are missing?)
scripts/Makefile.gcc-plugins line 28
Quoted 10 lines
scripts/Makefile.gcc-plugins new file +10
-0+ifeq ($(call cc-ifversion, -ge, 0408, y), y) + $(CONFIG_SHELL) -x $(srctree)/scripts/gcc-plugin.sh "$(HOSTCXX)" "$(HOSTCXX)" "$(CC)" +else + $(CONFIG_SHELL) -x $(srctree)/scripts/gcc-plugin.sh "$(HOSTCC)" "$(HOSTCXX)" "$(CC)" +endif +else + $(warning warning, your gcc version does not support plugins, you should upgrade it to gcc 4.5 at least) +endif +endif +endif
These ifdef's are really unreadable.
I wondered if it could be a bit simpler.
At least, the deepest one can be resolved with the "__HOSTCC" set above.
scripts/gcc-plugin.sh line 5
Quoted 11 lines
scripts/gcc-plugin.sh new file +5
-0@@ -0,0 +1,51 @@ +#!/bin/sh +srctree=$(dirname "$0") +gccplugins_dir=$($3 -print-file-name=plugin) +plugincc=$($1 -E -x c++ - -o /dev/null -I"${srctree}"/../tools/gcc -I"${gccplugins_dir}"/include 2>&1 <<EOF +#include "gcc-common.h"
Maybe <gcc-common.h> because it is not located at the same directory?
Quoted 44 lines
tools/gcc/gcc-common.h new file +38
-0@@ -0,0 +1,830 @@ +#ifndef GCC_COMMON_H_INCLUDED +#define GCC_COMMON_H_INCLUDED + +#include "bversion.h" +#if BUILDING_GCC_VERSION >=6000 +#include "gcc-plugin.h" +#else +#include "plugin.h" +#endif +#include "plugin-version.h" +#include "config.h" +#include "system.h" +#include "coretypes.h" +#include "tm.h" +#include "line-map.h" +#include "input.h" +#include "tree.h" + +#include "tree-inline.h" +#include "version.h" +#include "rtl.h" +#include "tm_p.h" +#include "flags.h" +#include "hard-reg-set.h" +#include "output.h" +#include "except.h" +#include "function.h" +#include "toplev.h" +#include "basic-block.h" +#include "intl.h" +#include "ggc.h" +#include "timevar.h" + +#include "params.h" + +#if BUILDING_GCC_VERSION <=4009 +#include "pointer-set.h" +#else
tools/gcc/gcc-common.h line 48
Quoted 10 lines
tools/gcc/gcc-common.h new file +10
-0+#include "hash-map.h" +#endif + +#include "emit-rtl.h" +#include "debug.h" +#include "target.h" +#include "langhooks.h" +#include "cfgloop.h" +#include "cgraph.h" +#include "opts.h"
All of these are included by "...", not <...>.
As mentioned above, I want you to use "..." style when you need to use relative path from the source.
I do not see most of them in tools/gcc/.
Signature · 2 lines
Best Regards Masahiro Yamada
Re: [PATCH v5 3/5] Add Cyclomatic complexity GCC plugin
In reply to Emese RevfyHi Emese,
2016-03-07 8:05 GMT+09:00 Emese Revfy <[email protected]>:
Quoted 47 lines
Add a very simple plugin to demonstrate the GCC plugin infrastructure. This GCC plugin computes the cyclomatic complexity of each function.
The complexity M of a function's control flow graph is defined as:
M = E - N + 2P
where
E = the number of edges
N = the number of nodes
P = the number of connected components (exit nodes).Signed-off-by Emese Revfy <[email protected]>--- arch/Kconfig | 12 +++++++ scripts/Makefile.gcc-plugins | 6 +++- tools/gcc/Makefile | 4 +++ tools/gcc/cyc_complexity_plugin.c | 73 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 tools/gcc/cyc_complexity_plugin.carch/Kconfig +12
-0@@ -370,6 +370,18 @@ menuconfig GCC_PLUGINS GCC plugins are loadable modules that provide extra features to the compiler. They are useful for runtime instrumentation and static analysis. +config GCC_PLUGIN_CYC_COMPLEXITY + bool "Compute the cyclomatic complexity of a function" + depends on GCC_PLUGINS + help + The complexity M of a function's control flow graph is defined as: + M = E - N + 2P + where + + E = the number of edges + N = the number of nodes + P = the number of connected components (exit nodes). + config HAVE_CC_STACKPROTECTOR bool helpscripts/Makefile.gcc-plugins +0
-0
scripts/Makefile.gcc-plugins else · line 12
Quoted 9 lines
scripts/Makefile.gcc-plugins +5
-1@@ -5,7 +5,11 @@ else PLUGINCC := $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-plugin.sh "$(HOSTCC)" "$(HOSTCXX)" "$(CC)") endif ifneq ($(PLUGINCC),) -export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGINS_AFLAGS +ifdef CONFIG_GCC_PLUGIN_CYC_COMPLEXITY +GCC_PLUGIN_CYC_COMPLEXITY_CFLAGS := -fplugin=$(objtree)/tools/gcc/cyc_complexity_plugin.so +endif +GCC_PLUGINS_CFLAGS := $(GCC_PLUGIN_CYC_COMPLEXITY_CFLAGS) +export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGINS_AFLAGS GCC_PLUGIN_CYC_COMPLEXITY
Do you need to export "GCC_PLUGIN_CYC_COMPLEXITY"? I do not see any reference to it.
If we expect more and more plugins in the future,
is it better to do like this?
gcc-plugin-$(CONFIG_GCC_PLUGIN_CYC_COMPLEXITY) += cyc_complexity_plugin.so gcc-plugin-$(CONFIG_GCC_PLUGIN_SANCOV) += sancov_plugin.so gcc-plugin-$(CONFIG_GCC_PLUGIN_FOO) += foo_plugin.so
GCC_PLUGINS_CFLAGS := $(addprefix -fplugin=$(objtree)/tools/gcc/, $(gcc-plugin-y))
Signature · 2 lines
Best Regards Masahiro Yamada
Re: [PATCH v5 1/5] Shared library support
In reply to Masahiro YamadaQuoted 4 lines · On Fri, 11 Mar 2016 15:19:33 +0900 Masahiro Yamada…
As an alternative, you can add needed build rules into
tools/gcc/Makefile, notscripts/Makefile.hostI guess these rule won't be used in other places.
I think it is better if the rules stay under scripts/ because I expect that there will also be clang and llvm plugins in the future (e.g., clang plugins can access the frontend that gcc plugins can't do). In this case these rules would either have to be duplicated or moved back under scripts/ (which makes it difficult to backport).
Quoted 16 lines
Quoted 16 lines
scripts/Makefile.host +8
-0+# hostcc-option +# Usage: cflags-y += $(call hostcc-option,-march=winchip-c6,-march=i586) + +hostcc-option = $(call try-run,\ + $(HOSTCC) $(HOSTCFLAGS) $(HOST_EXTRACFLAGS) $(1) -c -x c /dev/null -o "$$TMP",$(1),$(2)) + __hostprogs := $(sort $(hostprogs-y) $(hostprogs-m)) +__hostlibs := $(sort $(hostlibs-y) $(hostlibs-m)) +__hostcxxlibs := $(sort $(hostcxxlibs-y) $(hostcxxlibs-m)) # C code # Executables compiled from a single .c file@@ -42,6 +60,19 @@ host-cxxmulti := $(foreach m,$(__hostprogs),$(if $($(m)-cxxobjs),$(m))) # C++ Object (.o) files compiled from .cc files host-cxxobjs := $(sort $(foreach m,$(host-cxxmulti),$($(m)-cxxobjs)))
Quoted 5 lines
scripts/Makefile.host host-cxxmulti := $(foreach m,$(__hostprogs),$(if $($(m)-cxxobjs),$(m))) · line 65
Quoted 3 lines
scripts/Makefile.host +3
-0+# Shared libaries (only .c supported) +# Shared libraries (.so) - all .so files referenced in "xxx-objs" +host-cshlib := $(sort $(filter %.so, $(host-cobjs)))useless.
Which part do you think is useless and why?
Quoted 55 lines
Quoted 40 lines
scripts/Makefile.host +26
-0+host-cshlib += $(sort $(filter %.so, $(__hostlibs))) +host-cxxshlib := $(sort $(filter %.so, $(__hostcxxlibs))) +# Remove .so files from "xxx-objs" +host-cobjs := $(filter-out %.so,$(host-cobjs)) +host-cxxobjs := $(filter-out %.so,$(host-cxxobjs)) + +# Object (.o) files used by the shared libaries +host-cshobjs := $(sort $(foreach m,$(host-cshlib),$($(m:.so=-objs)))) +host-cxxshobjs := $(sort $(foreach m,$(host-cxxshlib),$($(m:.so=-objs)))) + # output directory for programs/.o files # hostprogs-y := tools/build may have been specified. # Retrieve also directory of .o files from prog-objs or prog-cxxobjs notation@@ -56,6 +87,10 @@ host-cmulti := $(addprefix $(obj)/,$(host-cmulti)) host-cobjs := $(addprefix $(obj)/,$(host-cobjs)) host-cxxmulti := $(addprefix $(obj)/,$(host-cxxmulti)) host-cxxobjs := $(addprefix $(obj)/,$(host-cxxobjs)) +host-cshlib := $(addprefix $(obj)/,$(host-cshlib)) +host-cxxshlib := $(addprefix $(obj)/,$(host-cxxshlib)) +host-cshobjs := $(addprefix $(obj)/,$(host-cshobjs)) +host-cxxshobjs := $(addprefix $(obj)/,$(host-cxxshobjs)) host-objdirs := $(addprefix $(obj)/,$(host-objdirs)) obj-dirs += $(host-objdirs)@@ -124,5 +159,37 @@ quiet_cmd_host-cxxobjs = HOSTCXX $@ $(host-cxxobjs): $(obj)/%.o: $(src)/%.cc FORCE $(call if_changed_dep,host-cxxobjs) +# Compile .c file, create position independent .o file +# host-cshobjs -> .o +quiet_cmd_host-cshobjs = HOSTCC -fPIC $@ + cmd_host-cshobjs = $(HOSTCC) $(hostc_flags) -fPIC -c -o $@ $< +$(host-cshobjs): $(obj)/%.o: $(src)/%.c FORCE + $(call if_changed_dep,host-cshobjs) + +# Compile .c file, create position independent .o file +# host-cxxshobjs -> .o +quiet_cmd_host-cxxshobjs = HOSTCXX -fPIC $@ + cmd_host-cxxshobjs = $(HOSTCXX) $(hostcxx_flags) -fPIC -c -o $@ $< +$(host-cxxshobjs): $(obj)/%.o: $(src)/%.c FORCEscripts/Makefile.host quiet_cmd_host-cxxobjs = HOSTCXX $@ · line 183
Quoted 10 lines
scripts/Makefile.host +10
-0+ $(call if_changed_dep,host-cxxshobjs) + +# Link a shared library, based on position independent .o files +# *.o -> .so shared library (host-cshlib) +quiet_cmd_host-cshlib = HOSTLLD -shared $@ + cmd_host-cshlib = $(HOSTCC) $(HOSTLDFLAGS) -shared -o $@ \ + $(addprefix $(obj)/,$($(@F:.so=-objs))) \ + $(HOST_LOADLIBES) $(HOSTLOADLIBES_$(@F)) +$(host-cshlib): $(obj)/%: $(host-cshobjs) FORCE + $(call if_changed,host-cshlib)Could you use $(call multi-depend, ...)
if you need to handle multi-object please?
Quoted 1 line
Please refer to commit
c8589d1e9e01and commit97e3226e6e984c8.
Ok, I will check it out.
Quoted 2 lines
But, I still do not see any gcc-plugin that is large enough to be linked from multiple objects.
There will be large plugins later (typically if the plugin has more passes e.g., my size_overflow plugin).
Signature · 1 line
Emese
Re: [PATCH v5 2/5] GCC plugin infrastructure
In reply to Masahiro YamadaQuoted 8 lines · On Fri, 11 Mar 2016 15:25:19 +0900 Masahiro Yamada…
Maybe
scripts/gcc-plugins/is better thantools/gcc?In the directory "
scripts/", we have several tools used during building the kernel image.
We have some optional programs in the directory "tools/", which are not used for building the kernel image itself.Please correct me if I am wrong.
I think scripts isn't the right place because there are tools there which don't get used
during the building of vmlinux (e.g., coccinelle, checkpatch).
The scripts name also doesn't describe gcc plugins well. Plugins take part in the image building process in a different way than these tools and scripts do.
Since there doesn't seen to be a good place for compiler plugins, maybe we should create a new toplevel directory called "build". Compiler plugins and other existing build tools could live there. What do you think?
Quoted 23 lines
Quoted 12 lines
init/Makefile +3
-0+ccflags-y := $(GCC_PLUGINS_CFLAGS) +asflags-y := $(GCC_PLUGINS_AFLAGS) + obj-y := main.o version.o mounts.o ifneq ($(CONFIG_BLK_DEV_INITRD),y) obj-y += noinitramfs.oscripts/Makefile.gcc-plugins new file +0
-0scripts/Makefile.gcc-plugins line 6
Quoted 6 lines
scripts/Makefile.gcc-plugins new file +6
-0@@ -0,0 +1,28 @@ +ifdef CONFIG_GCC_PLUGINS +ifeq ($(call cc-ifversion, -ge, 0408, y), y) +PLUGINCC := $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-plugin.sh "$(HOSTCXX)" "$(HOSTCXX)" "$(CC)") +else +PLUGINCC := $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-plugin.sh "$(HOSTCC)" "$(HOSTCXX)" "$(CC)") +endifThe difference is only the first argument.
Can you make it as follows?
Quoted 8 lines
__HOSTCC:= $(call cc-ifversion, -ge, 0408, $(HOSTCXX), $(HOSTCC))PLUGINCC := $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-plugin.sh "$(__HOSTCC)" "$(HOSTCXX)" "$(CC)")
I did not come up with a good name for
__HOSTCC.
Feel free to replace it with a better one.
Sure, I will do it.
Signature · 1 line
Emese
Re: [PATCH v5 3/5] Add Cyclomatic complexity GCC plugin
In reply to Masahiro YamadaQuoted 10 lines · On Fri, 11 Mar 2016 15:26:39 +0900 Masahiro Yamada…
Quoted 10 lines
scripts/Makefile.gcc-plugins +1
-1@@ -5,7 +5,11 @@ else PLUGINCC := $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-plugin.sh "$(HOSTCC)" "$(HOSTCXX)" "$(CC)") endif ifneq ($(PLUGINCC),) -export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGINS_AFLAGS +ifdef CONFIG_GCC_PLUGIN_CYC_COMPLEXITY
Quoted 8 lines
scripts/Makefile.gcc-plugins else · line 12
Quoted 4 lines
scripts/Makefile.gcc-plugins +4
-0+GCC_PLUGIN_CYC_COMPLEXITY_CFLAGS := -fplugin=$(objtree)/tools/gcc/cyc_complexity_plugin.so +endif +GCC_PLUGINS_CFLAGS := $(GCC_PLUGIN_CYC_COMPLEXITY_CFLAGS) +export PLUGINCC GCC_PLUGINS_CFLAGS GCC_PLUGINS_AFLAGS GCC_PLUGIN_CYC_COMPLEXITYDo you need to export "
GCC_PLUGIN_CYC_COMPLEXITY"? I do not see any reference to it.
This is a demo plugin. I just want to demonstrate how to use this infrastructure but I can remove it. In my project there would be other plugins which use the export.
Quoted 10 lines
If we expect more and more plugins in the future,
is it better to do like this?gcc-plugin-$(CONFIG_GCC_PLUGIN_CYC_COMPLEXITY) += cyc_complexity_plugin.so gcc-plugin-$(CONFIG_GCC_PLUGIN_SANCOV) += sancov_plugin.so gcc-plugin-$(CONFIG_GCC_PLUGIN_FOO) += foo_plugin.so
GCC_PLUGINS_CFLAGS:= $(addprefix -fplugin=$(objtree)/tools/gcc/, $(gcc-plugin-y))
Yes, it is better, I will do it.
Signature · 1 line
Emese
Re: [PATCH v5 2/5] GCC plugin infrastructure
In reply to Masahiro YamadaQuoted 14 lines · Masahiro Yamada
scripts/gcc-plugin.sh line 5
Quoted 11 lines
scripts/gcc-plugin.sh new file +5
-0@@ -0,0 +1,51 @@ +#!/bin/sh +srctree=$(dirname "$0") +gccplugins_dir=$($3 -print-file-name=plugin) +plugincc=$($1 -E -x c++ - -o /dev/null -I"${srctree}"/../tools/gcc -I"${gccplugins_dir}"/include 2>&1 <<EOF +#include "gcc-common.h"Maybe <
gcc-common.h> because it is not located at the same directory?
[snip]
Quoted 11 lines
scripts/gcc-plugin.sh
Quoted 7 lines
scripts/gcc-plugin.sh new file +7
-0+#include "emit-rtl.h" +#include "debug.h" +#include "target.h" +#include "langhooks.h" +#include "cfgloop.h" +#include "cgraph.h" +#include "opts.h"All of these are included by "...", not <...>.
Quoted 4 lines
As mentioned above, I want you to use "..." style when you need to use relative path from the source.
I do not see most of them in
tools/gcc/.
no, that'd be incorrect for several reasons. first, the rule to use <...> vs. "..." include directives is not about the header being in the same directory but whether the header is a system header or one provided by the given program. roughly speaking, system headers are those that are available through the compiler's default include paths (gcc's own headers, those of glibc and other libraries under /usr/include, etc). gcc plugin headers are *not* available by default, one has to query the compiler about their path (see -print-file-name=plugin above) and explicitly add it to the compiler's include search path.
second, regardless of whether plugin headers are available by default or not, we still couldn't use them during cross-compilation as the plugin headers we want are those of the target compiler (that will load the plugin eventually), not that of the host compiler (which merely compiles the plugin and in theory doesn't even have to be gcc or a plugin capable gcc).
for these reasons the correct include directive is "..." and not <...>. if it helps to understand the situation better, consider that gcc plugins are to gcc as kernel modules are to vmlinux and all kernel headers are included via "..." as well, regardless of whether they're in the same directory or not.
cheers,
PaX Team
Re: [PATCH v5 2/5] GCC plugin infrastructure
Hi PaX,
2016-03-15 6:25 GMT+09:00 PaX Team <[email protected]>:
Quoted 34 lines · Masahiro Yamada
Quoted 15 lines
scripts/gcc-plugin.sh line 5
Quoted 12 lines
scripts/gcc-plugin.sh new file +5
-0@@ -0,0 +1,51 @@ +#!/bin/sh +srctree=$(dirname "$0") +gccplugins_dir=$($3 -print-file-name=plugin) +plugincc=$($1 -E -x c++ - -o /dev/null -I"${srctree}"/../tools/gcc -I"${gccplugins_dir}"/include 2>&1 <<EOF +#include "gcc-common.h"Maybe <
gcc-common.h> because it is not located at the same directory?[snip]
Quoted 11 lines
scripts/gcc-plugin.sh
Quoted 7 lines
scripts/gcc-plugin.sh new file +7
-0+#include "emit-rtl.h" +#include "debug.h" +#include "target.h" +#include "langhooks.h" +#include "cfgloop.h" +#include "cgraph.h" +#include "opts.h"All of these are included by "...", not <...>.
Quoted 4 lines
As mentioned above, I want you to use "..." style when you need to use relative path from the source.
I do not see most of them in
tools/gcc/.
Quoted 8 lines
no, that'd be incorrect for several reasons. first, the rule to use <...> vs. "..." include directives is not about the header being in the same directory but whether the header is a system header or one provided by the given program. roughly speaking, system headers are those that are available through the compiler's default include paths (gcc's own headers, those of glibc and other libraries under
/usr/include, etc). gcc plugin headers are *not* available by default, one has to query the compiler about their path (see -print-file-name=plugin above) and explicitly add it to the compiler's include search path.
Are you sure?
I'd recommend you to read the following:
https://gcc.gnu.org/onlinedocs/cpp/Search-Path.html
scripts/gcc-plugin.sh adds
- ${srctree}"/../tools/gcc
- ${gccplugins_dir}/include
to the header search path with "-I" option.
GCC looks for headers requested with #include <file>
in directories specified with "-I" as well as in the default ones.
Quoted 5 lines
second, regardless of whether plugin headers are available by default or not, we still couldn't use them during cross-compilation as the plugin headers we want are those of the target compiler (that will load the plugin eventually), not that of the host compiler (which merely compiles the plugin and in theory doesn't even have to be gcc or a plugin capable gcc).
So, how could this be the reason why we should avoid #include <file>?
Quoted 4 lines
for these reasons the correct include directive is "..." and not <...>. if it helps to understand the situation better, consider that gcc plugins are to gcc as kernel modules are to vmlinux and all kernel headers are included via "..." as well, regardless of whether they're in the same directory or not.
Of course, you could use #include "..." as well to include kernel headers, but you should not do that.
You should do so only when you need to include headers that are local to your directory.
Signature · 2 lines
Best Regards Masahiro Yamada
Re: [PATCH v5 2/5] GCC plugin infrastructure
In reply to Emese RevfyHi Emese,
2016-03-15 5:52 GMT+09:00 Emese Revfy <[email protected]>:
Quoted 10 lines
Quoted 7 lines · On Fri, 11 Mar 2016 15:25:19 +0900 Masahiro Yamada…
Maybe
scripts/gcc-plugins/is better thantools/gcc?In the directory "
scripts/", we have several tools used during building the kernel image.
We have some optional programs in the directory "tools/", which are not used for building the kernel image itself.
Quoted 4 lines
Quoted 1 line
Please correct me if I am wrong.
I think scripts isn't the right place because there are tools there which don't get used during the building of vmlinux (e.g., coccinelle, checkpatch).
No, this is not what I meant.
What I want to say is:
All the host tools used for building vmlinux should be located in the directory "scripts/",
but the directory may also other host tools that are not used for building vmlinux (such as checkpatch).
Quoted 1 line
The scripts name also doesn't describe gcc plugins well.
With my suggestion "scripts/gcc-plugins/",
the sub-directory name describes it very well.
Quoted 4 lines
Plugins take part in the image building process
in a different way than these tools and scripts do.
Since there doesn't seen to be a good place for compiler plugins, maybe we should create a new toplevel directory called "build". Compiler plugins and other existing build tools could live there. What do you think?
In my understanding, they are plug-in'ed into the cross-compiler that compiles vmlinux.
If so, GCC plugins should be happy in "scripts/"
because Kbuild descends into "scripts/" before building any objects for vmlinux.
Signature · 2 lines
Best Regards Masahiro Yamada
Re: [PATCH v5 1/5] Shared library support
In reply to Emese RevfyHi Emese,
2016-03-15 5:14 GMT+09:00 Emese Revfy <[email protected]>:
Quoted 28 lines
Quoted 4 lines · On Fri, 11 Mar 2016 15:19:33 +0900 Masahiro Yamada…
As an alternative, you can add needed build rules into
tools/gcc/Makefile, notscripts/Makefile.hostI guess these rule won't be used in other places.
I think it is better if the rules stay under
scripts/because I expect that there will also be clang and llvm plugins in the future (e.g., clang plugins can access the frontend that gcc plugins can't do). In this case these rules would either have to be duplicated or moved back underscripts/(which makes it difficult to backport).Quoted 16 lines
Quoted 16 lines
scripts/Makefile.host +8
-0+# hostcc-option +# Usage: cflags-y += $(call hostcc-option,-march=winchip-c6,-march=i586) + +hostcc-option = $(call try-run,\ + $(HOSTCC) $(HOSTCFLAGS) $(HOST_EXTRACFLAGS) $(1) -c -x c /dev/null -o "$$TMP",$(1),$(2)) + __hostprogs := $(sort $(hostprogs-y) $(hostprogs-m)) +__hostlibs := $(sort $(hostlibs-y) $(hostlibs-m)) +__hostcxxlibs := $(sort $(hostcxxlibs-y) $(hostcxxlibs-m)) # C code # Executables compiled from a single .c file@@ -42,6 +60,19 @@ host-cxxmulti := $(foreach m,$(__hostprogs),$(if $($(m)-cxxobjs),$(m))) # C++ Object (.o) files compiled from .cc files host-cxxobjs := $(sort $(foreach m,$(host-cxxmulti),$($(m)-cxxobjs)))
Quoted 7 lines
Quoted 5 lines
scripts/Makefile.host host-cxxmulti := $(foreach m,$(__hostprogs),$(if $($(m)-cxxobjs),$(m))) · line 65
Quoted 3 lines
scripts/Makefile.host +3
-0+# Shared libaries (only .c supported) +# Shared libraries (.so) - all .so files referenced in "xxx-objs" +host-cshlib := $(sort $(filter %.so, $(host-cobjs)))useless.
Which part do you think is useless and why?
The line
host-cshlib := $(sort $(filter %.so, $(host-cobjs)))
is useless because there is no host program that consists of .so files.
Your intention is to generate shared objects as final targets, not host tools that call shared objects.
Signature · 2 lines
Best Regards Masahiro Yamada
Re: [PATCH v5 2/5] GCC plugin infrastructure
In reply to Masahiro YamadaOn 16 Mar 2016 at 16:34, Masahiro Yamada wrote:
Hi,
Quoted 14 lines
Quoted 5 lines
Quoted 4 lines
As mentioned above, I want you to use "..." style when you need to use relative path from the source.
I do not see most of them in
tools/gcc/.Quoted 8 lines
no, that'd be incorrect for several reasons. first, the rule to use <...> vs. "..." include directives is not about the header being in the same directory but whether the header is a system header or one provided by the given program. roughly speaking, system headers are those that are available through the compiler's default include paths (gcc's own headers, those of glibc and other libraries under
/usr/include, etc). gcc plugin headers are *not* available by default, one has to query the compiler about their path (see -print-file-name=plugin above) and explicitly add it to the compiler's include search path.
Quoted 1 line
Are you sure?
sure about which part? ;) the use of <...> vs "..." is a matter of convention (to differentiate between 'system' headers from the program's own) and for gcc the plugin headers don't count as 'system' headers, gcc uses "..." throughout its codebase itself (so does clang/llvm). it'd look weird and inconsistent if compiler plugins used <...>.
at the end of the day this comes down to whether gcc plugins are considered kernel code and thus the kernel style applies to them (the kernel itself uses <...> because it's a freestanding program so its 'system' headers are mostly what it provides itself, not those of the compiler let alone userland libs) or gcc extensions and thus the gcc include style applies to them. as you can guess, i'm arguing for the latter, because gcc plugins are meant for the compiler, not the kernel per se, and in fact we have several plugins already that can be used in userland as well (e.g., Emese's size overflow plugin).
the one thing for plugins that i borrowed from the kernel instead of gcc is the code formatting style as the GNU one is as bad as Linus argued decades ago ;).
Quoted 7 lines
Quoted 5 lines
second, regardless of whether plugin headers are available by default or not, we still couldn't use them during cross-compilation as the plugin headers we want are those of the target compiler (that will load the plugin eventually), not that of the host compiler (which merely compiles the plugin and in theory doesn't even have to be gcc or a plugin capable gcc).
So, how could this be the reason why we should avoid #include <file>?
because <...> is meant for headers that are on the compiler's default search path and even if gcc plugin headers were there (they aren't) they'd be the wrong ones to use, you'd have to specifically override the include path with that of the target compiler which is a sign that you're no longer including 'system' headers (again, this convention holds for hosted programs, freestanding ones don't have anything but 'system' headers usually).
Quoted 3 lines
Quoted 3 lines
for these reasons the correct include directive is "..." and not <...>. if it helps to understand the situation better, consider that gcc plugins are to gcc as kernel modules are to vmlinux and all kernel headers are included via "..." as well, regardless
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
sorry, i take that back, the kernel uses <...> but for a reason that in my opinion doesn't apply to compiler plugins (see above).
Quoted 7 lines
Quoted 1 line
of whether they're in the same directory or not.
Of course, you could use #include "..." as well to include kernel headers, but you should not do that.
You should do so only when you need to include headers that are local to your directory.
sure, a header in the same directory is a sign that it's not a 'system' header but i'm also saying that there can be other non-system headers in a program in other directories as well. FWIW, the kernel itself has several "..." directives that reference headers outside of the same directory, e.g., try this in a kernel tree:
grep "#include.*\"\." -rn
cheers,
PaX Team
Re: [PATCH v5 2/5] GCC plugin infrastructure
In reply to Masahiro YamadaQuoted 11 lines · On Wed, 16 Mar 2016 16:41:36 +0900 Masahiro Yamada…
Quoted 1 line
The scripts name also doesn't describe gcc plugins well.
With my suggestion "
scripts/gcc-plugins/",
the sub-directory name describes it very well.Quoted 4 lines
Plugins take part in the image building process
in a different way than these tools and scripts do.
Since there doesn't seen to be a good place for compiler plugins, maybe we should create a new toplevel directory called "build". Compiler plugins and other existing build tools could live there. What do you think?
Quoted 5 lines
In my understanding, they are plug-in'ed into the cross-compiler that compiles vmlinux.
If so, GCC plugins should be happy in "
scripts/"
becauseKbuilddescends into "scripts/" before building any objects for vmlinux.
Ok, I'll move them under scripts/ in the next patch set.
Signature · 1 line
Emese
Re: [PATCH v5 2/5] GCC plugin infrastructure
In reply to PaX Team2016-03-16 21:49 GMT+09:00 PaX Team <[email protected]>:
Quoted 18 lines · Masahiro Yamada
Hi,
Quoted 14 lines
Quoted 13 lines
Quoted 4 lines
As mentioned above, I want you to use "..." style when you need to use relative path from the source.
I do not see most of them in
tools/gcc/.no, that'd be incorrect for several reasons. first, the rule to use <...> vs. "..." include directives is not about the header being in the same directory but whether the header is a system header or one provided by the given program. roughly speaking, system headers are those that are available through the compiler's default include paths (gcc's own headers, those of glibc and other libraries under
/usr/include, etc). gcc plugin headers are *not* available by default, one has to query the compiler about their path (see -print-file-name=plugin above) and explicitly add it to the compiler's include search path.
Quoted 3 lines
Quoted 1 line
Are you sure?
sure about which part? ;)
You said "that'd be incorrect", but
there is no "correct" or "incorrect" in either way as long as it works.
This decision varies from project to project.
At least in Linux, it generally uses #include <file> style except ones in local directories.
I guess it is debatable if this rule also applies to host tools or not.
Quoted 47 lines
the use of <...> vs "..." is a matter of convention (to
differentiate between 'system' headers from the program's own) and for gcc the plugin headers don't count as 'system' headers, gcc uses "..." throughout its codebase itself (so does clang/llvm). it'd look weird and inconsistent if compiler plugins used <...>.at the end of the day this comes down to whether gcc plugins are considered kernel code and thus the kernel style applies to them (the kernel itself uses <...> because it's a freestanding program so its 'system' headers are mostly what it provides itself, not those of the compiler let alone userland libs) or gcc extensions and thus the gcc include style applies to them. as you can guess, i'm arguing for the latter, because gcc plugins are meant for the compiler, not the kernel per se, and in fact we have several plugins already that can be used in userland as well (e.g., Emese's size overflow plugin).
the one thing for plugins that i borrowed from the kernel instead of gcc is the code formatting style as the GNU one is as bad as Linus argued decades ago ;).
Quoted 7 lines
Quoted 5 lines
second, regardless of whether plugin headers are available by default or not, we still couldn't use them during cross-compilation as the plugin headers we want are those of the target compiler (that will load the plugin eventually), not that of the host compiler (which merely compiles the plugin and in theory doesn't even have to be gcc or a plugin capable gcc).
So, how could this be the reason why we should avoid #include <file>?
because <...> is meant for headers that are on the compiler's default search path and even if gcc plugin headers were there (they aren't) they'd be the wrong ones to use, you'd have to specifically override the include path with that of the target compiler which is a sign that you're no longer including 'system' headers (again, this convention holds for hosted programs, freestanding ones don't have anything but 'system' headers usually).
Quoted 3 lines
Quoted 3 lines
for these reasons the correct include directive is "..." and not <...>. if it helps to understand the situation better, consider that gcc plugins are to gcc as kernel modules are to vmlinux and all kernel headers are included via "..." as well, regardless
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^sorry, i take that back, the kernel uses <...> but for a reason that in my opinion doesn't apply to compiler plugins (see above).
Quoted 7 lines
Quoted 1 line
of whether they're in the same directory or not.
Of course, you could use #include "..." as well to include kernel headers, but you should not do that.
You should do so only when you need to include headers that are local to your directory.
Quoted 7 lines
sure, a header in the same directory is a sign that it's not a 'system' header but i'm also saying that there can be other non-system headers in a program in other directories as well. FWIW, the kernel itself has several "..." directives that reference headers outside of the same directory, e.g., try this in a kernel tree:
grep "#include.*\"\." -rn
To sum up, your suggestion for GCC plugins is
- Use #include <...> to include header files provided by the host compiler
- Use #include "..." to include header files provided by the cross
compiler (plugin directory)
- Use #include "..." to include header files in the local project
Correct?
I leave the final decision to others.
Signature · 2 lines
Best Regards Masahiro Yamada
Re: [PATCH v5 2/5] GCC plugin infrastructure
In reply to Masahiro YamadaQuoted 13 lines · On Fri, 11 Mar 2016 15:25:19 +0900 Masahiro Yamada…
You sprinkle "gcc-plugins" target in the top
Makefile, which I do not like.Can you descend into
scripts/gcc-pluginsfromscripts/Makefile?subdir-$(CONFIG_MODVERSIONS) += genksyms
subdir-y += mod
subdir-$(CONFIG_SECURITY_SELINUX) += selinux
subdir-$(CONFIG_DTC) += dtc
subdir-$(CONFIG_GDB_SCRIPTS) += gdb
+subdir-$(CONFIG_GCC_PLUGINS) += gcc-plugins
Quoted 1 line
This is how other host tools do, I think.
Hi,
I tried to remove the gcc-plugins target but sadly in this case the plugins aren't complied. I don't know if I understand how it should work. I pushed a commit:
https://github.com/ephox-gcc-plugins/gcc-plugins_linux-next/commit/0b005b886bde6c4b9134d01f830d88d464911a65
Could you please help me with what I did wrong?
Thanks
Signature · 1 line
Emese
Re: [PATCH v5 2/5] GCC plugin infrastructure
Hi Emese,
2016-03-24 9:07 GMT+09:00 Emese Revfy <[email protected]>:
Quoted 16 lines
Quoted 13 lines · On Fri, 11 Mar 2016 15:25:19 +0900 Masahiro Yamada…
You sprinkle "gcc-plugins" target in the top
Makefile, which I do not like.Can you descend into
scripts/gcc-pluginsfromscripts/Makefile?subdir-$(CONFIG_MODVERSIONS) += genksyms
subdir-y += mod
subdir-$(CONFIG_SECURITY_SELINUX) += selinux
subdir-$(CONFIG_DTC) += dtc
subdir-$(CONFIG_GDB_SCRIPTS) += gdb
+subdir-$(CONFIG_GCC_PLUGINS) += gcc-plugins
Quoted 9 lines
Quoted 1 line
This is how other host tools do, I think.
Hi,
I tried to remove the gcc-plugins target but sadly in this case the plugins aren't complied. I don't know if I understand how it should work. I pushed a commit:
https://github.com/ephox-gcc-plugins/gcc-plugins_linux-next/commit/0b005b886bde6c4b9134d01f830d88d464911a65
Could you please help me with what I did wrong?
Sorry, I missed this problem.
GCC-plugins must be compiled before descending into ./Kbuild.
Please apply the following on top of commit 0b005b88 and check if it works.
Makefile +0 -0
@@ -1009,6 +1009,9 @@ archprepare: archheaders archscripts prepare1
scripts_basic
prepare0: KBUILD_CFLAGS += $(GCC_PLUGINS_CFLAGS)
prepare0: KBUILD_AFLAGS += $(GCC_PLUGINS_AFLAGS)
prepare0: archprepare FORCE
+ifdef CONFIG_GCC_PLUGINS
+ $(Q)$(MAKE) $(build)=scripts/gcc-plugins
+endif
$(Q)$(MAKE) $(build)=.
# All the preparing..
scripts/Makefile +1 -2
@@ -45,7 +45,6 @@ subdir-y += mod subdir-$(CONFIG_SECURITY_SELINUX) += selinux subdir-$(CONFIG_DTC) += dtc subdir-$(CONFIG_GDB_SCRIPTS) += gdb -subdir-$(CONFIG_GCC_PLUGINS) += gcc-plugins # Let clean descend into subdirs -subdir- += basic kconfig package +subdir- += basic kconfig package gcc-plugins
Signature · 2 lines
Best Regards Masahiro Yamada
Re: [PATCH v5 2/5] GCC plugin infrastructure
Quoted 33 lines · On Sat, 26 Mar 2016 11:39:32 +0900 Masahiro Yamada…
Quoted 5 lines
I tried to remove the gcc-plugins target but sadly in this case the plugins aren't complied. I don't know if I understand how it should work. I pushed a commit:
https://github.com/ephox-gcc-plugins/gcc-plugins_linux-next/commit/0b005b886bde6c4b9134d01f830d88d464911a65
Could you please help me with what I did wrong?Sorry, I missed this problem.
GCC-plugins must be compiled before descending into ./Kbuild.
Please apply the following on top of commit
0b005b88and check if it works.Makefile +0
-0@@ -1009,6 +1009,9 @@ archprepare: archheaders archscripts prepare1
scripts_basic
prepare0:KBUILD_CFLAGS+= $(GCC_PLUGINS_CFLAGS)
prepare0:KBUILD_AFLAGS+= $(GCC_PLUGINS_AFLAGS)
prepare0: archprepare FORCEMakefile +3
-0+ifdef CONFIG_GCC_PLUGINS + $(Q)$(MAKE) $(build)=scripts/gcc-plugins +endif $(Q)$(MAKE) $(build)=. # All the preparing..scripts/Makefile +0
-0
scripts/Makefile subdir-y += mod · line 50
Quoted 8 lines
scripts/Makefile +1
-2@@ -45,7 +45,6 @@ subdir-y += mod subdir-$(CONFIG_SECURITY_SELINUX) += selinux subdir-$(CONFIG_DTC) += dtc subdir-$(CONFIG_GDB_SCRIPTS) += gdb -subdir-$(CONFIG_GCC_PLUGINS) += gcc-plugins # Let clean descend into subdirs -subdir- += basic kconfig package +subdir- += basic kconfig package gcc-plugins
Thanks for the patch. I tested it and there is a problem when I run a parallel make.
The plugins are compiled later than needed (e.g., scripts/mod/empty.o). There is a lot of important code under scripts/mod which may need the plugins. If the plugins were compiled when the "scripts_basic" target runs it would be good but unfortunately the config symbol CONFIG_GCC_PLUGINS doesn't seem to be defined yet. Could you please help me solve this problem?
Signature · 1 line
Emese
Re: [PATCH v5 2/5] GCC plugin infrastructure
Hi Emese,
Sorry for my late reply.
Quoted 3 lines
Thanks for the patch. I tested it and there is a problem when I run a parallel make. The plugins are compiled later than needed (e.g.,
scripts/mod/empty.o). There is a lot of important code underscripts/modwhich may need the plugins.
Ah, I missed that.
Quoted 3 lines
If the plugins were compiled when the "
scripts_basic"
target runs it would be good but unfortunately the config symbolCONFIG_GCC_PLUGINSdoesn't seem to be defined yet. Could you please help me solve this problem?
How about this?
It should apply on commit 0b005b886bde6c4b9134d01f830d88d464911a65 of your tree.
Makefile +8 -1
@@ -550,7 +550,7 @@ ifeq ($(KBUILD_EXTMOD),) # in parallel PHONY += scripts scripts: scripts_basic include/config/auto.conf include/config/tristate.conf \ - asm-generic + asm-generic gcc-plugins $(Q)$(MAKE) $(build)=$(@) # Objects we will link into vmlinux / subdirs we need to visit
@@ -625,6 +625,13 @@ endif # Tell gcc to never replace conditional load with a non-conditional one KBUILD_CFLAGS += $(call cc-option,--param=allow-store-data-races=0) +PHONY += gcc-plugins +gcc-plugins: scripts_basic +ifdef CONFIG_GCC_PLUGINS + $(Q)$(MAKE) $(build)=scripts/gcc-plugins +endif + @: + include scripts/Makefile.gcc-plugins ifdef CONFIG_READABLE_ASM
@@ -1008,7 +1015,7 @@ archprepare: archheaders archscripts prepare1
scripts_basic
prepare0: KBUILD_CFLAGS += $(GCC_PLUGINS_CFLAGS)
prepare0: KBUILD_AFLAGS += $(GCC_PLUGINS_AFLAGS)
-prepare0: archprepare FORCE
+prepare0: archprepare gcc-plugins FORCE
$(Q)$(MAKE) $(build)=.
# All the preparing..
scripts/Makefile +1 -2
@@ -45,7 +45,6 @@ subdir-y += mod subdir-$(CONFIG_SECURITY_SELINUX) += selinux subdir-$(CONFIG_DTC) += dtc subdir-$(CONFIG_GDB_SCRIPTS) += gdb -subdir-$(CONFIG_GCC_PLUGINS) += gcc-plugins # Let clean descend into subdirs -subdir- += basic kconfig package +subdir- += basic kconfig package gcc-plugins
Signature · 2 lines
Best Regards Masahiro Yamada