Skip to content

Commit f716a85

Browse files
committed
Merge branch 'kbuild' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuild
Pull kbuild updates from Michal Marek: - GCC plugin support by Emese Revfy from grsecurity, with a fixup from Kees Cook. The plugins are meant to be used for static analysis of the kernel code. Two plugins are provided already. - reduction of the gcc commandline by Arnd Bergmann. - IS_ENABLED / IS_REACHABLE macro enhancements by Masahiro Yamada - bin2c fix by Michael Tautschnig - setlocalversion fix by Wolfram Sang * 'kbuild' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuild: gcc-plugins: disable under COMPILE_TEST kbuild: Abort build on bad stack protector flag scripts: Fix size mismatch of kexec_purgatory_size kbuild: make samples depend on headers_install Kbuild: don't add obj tree in additional includes Kbuild: arch: look for generated headers in obtree Kbuild: always prefix objtree in LINUXINCLUDE Kbuild: avoid duplicate include path Kbuild: don't add ../../ to include path vmlinux.lds.h: replace config_enabled() with IS_ENABLED() kconfig.h: allow to use IS_{ENABLE,REACHABLE} in macro expansion kconfig.h: use already defined macros for IS_REACHABLE() define export.h: use __is_defined() to check if __KSYM_* is defined kconfig.h: use __is_defined() to check if MODULE is defined kbuild: setlocalversion: print error to STDERR Add sancov plugin Add Cyclomatic complexity GCC plugin GCC plugin infrastructure Shared library support
2 parents 221bb8a + a519167 commit f716a85

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2252
-54
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ modules.builtin
3737
Module.symvers
3838
*.dwo
3939
*.su
40+
*.c.[012]*.*
4041

4142
#
4243
# Top-level generic files

Documentation/dontdiff

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*.bc
44
*.bin
55
*.bz2
6+
*.c.[012]*.*
67
*.cis
78
*.cpio
89
*.csp

Documentation/gcc-plugins.txt

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
GCC plugin infrastructure
2+
=========================
3+
4+
5+
1. Introduction
6+
===============
7+
8+
GCC plugins are loadable modules that provide extra features to the
9+
compiler [1]. They are useful for runtime instrumentation and static analysis.
10+
We can analyse, change and add further code during compilation via
11+
callbacks [2], GIMPLE [3], IPA [4] and RTL passes [5].
12+
13+
The GCC plugin infrastructure of the kernel supports all gcc versions from
14+
4.5 to 6.0, building out-of-tree modules, cross-compilation and building in a
15+
separate directory.
16+
Plugin source files have to be compilable by both a C and a C++ compiler as well
17+
because gcc versions 4.5 and 4.6 are compiled by a C compiler,
18+
gcc-4.7 can be compiled by a C or a C++ compiler,
19+
and versions 4.8+ can only be compiled by a C++ compiler.
20+
21+
Currently the GCC plugin infrastructure supports only the x86, arm and arm64
22+
architectures.
23+
24+
This infrastructure was ported from grsecurity [6] and PaX [7].
25+
26+
--
27+
[1] https://gcc.gnu.org/onlinedocs/gccint/Plugins.html
28+
[2] https://gcc.gnu.org/onlinedocs/gccint/Plugin-API.html#Plugin-API
29+
[3] https://gcc.gnu.org/onlinedocs/gccint/GIMPLE.html
30+
[4] https://gcc.gnu.org/onlinedocs/gccint/IPA.html
31+
[5] https://gcc.gnu.org/onlinedocs/gccint/RTL.html
32+
[6] https://grsecurity.net/
33+
[7] https://pax.grsecurity.net/
34+
35+
36+
2. Files
37+
========
38+
39+
$(src)/scripts/gcc-plugins
40+
This is the directory of the GCC plugins.
41+
42+
$(src)/scripts/gcc-plugins/gcc-common.h
43+
This is a compatibility header for GCC plugins.
44+
It should be always included instead of individual gcc headers.
45+
46+
$(src)/scripts/gcc-plugin.sh
47+
This script checks the availability of the included headers in
48+
gcc-common.h and chooses the proper host compiler to build the plugins
49+
(gcc-4.7 can be built by either gcc or g++).
50+
51+
$(src)/scripts/gcc-plugins/gcc-generate-gimple-pass.h
52+
$(src)/scripts/gcc-plugins/gcc-generate-ipa-pass.h
53+
$(src)/scripts/gcc-plugins/gcc-generate-simple_ipa-pass.h
54+
$(src)/scripts/gcc-plugins/gcc-generate-rtl-pass.h
55+
These headers automatically generate the registration structures for
56+
GIMPLE, SIMPLE_IPA, IPA and RTL passes. They support all gcc versions
57+
from 4.5 to 6.0.
58+
They should be preferred to creating the structures by hand.
59+
60+
61+
3. Usage
62+
========
63+
64+
You must install the gcc plugin headers for your gcc version,
65+
e.g., on Ubuntu for gcc-4.9:
66+
67+
apt-get install gcc-4.9-plugin-dev
68+
69+
Enable a GCC plugin based feature in the kernel config:
70+
71+
CONFIG_GCC_PLUGIN_CYC_COMPLEXITY = y
72+
73+
To compile only the plugin(s):
74+
75+
make gcc-plugins
76+
77+
or just run the kernel make and compile the whole kernel with
78+
the cyclomatic complexity GCC plugin.
79+
80+
81+
4. How to add a new GCC plugin
82+
==============================
83+
84+
The GCC plugins are in $(src)/scripts/gcc-plugins/. You can use a file or a directory
85+
here. It must be added to $(src)/scripts/gcc-plugins/Makefile,
86+
$(src)/scripts/Makefile.gcc-plugins and $(src)/arch/Kconfig.
87+
See the cyc_complexity_plugin.c (CONFIG_GCC_PLUGIN_CYC_COMPLEXITY) GCC plugin.

MAINTAINERS

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5094,6 +5094,15 @@ L: linux-scsi@vger.kernel.org
50945094
S: Odd Fixes (e.g., new signatures)
50955095
F: drivers/scsi/fdomain.*
50965096

5097+
GCC PLUGINS
5098+
M: Kees Cook <keescook@chromium.org>
5099+
R: Emese Revfy <re.emese@gmail.com>
5100+
L: kernel-hardening@lists.openwall.com
5101+
S: Maintained
5102+
F: scripts/gcc-plugins/
5103+
F: scripts/gcc-plugin.sh
5104+
F: Documentation/gcc-plugins.txt
5105+
50975106
GCOV BASED KERNEL PROFILING
50985107
M: Peter Oberparleiter <oberpar@linux.vnet.ibm.com>
50995108
S: Maintained

Makefile

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -371,26 +371,27 @@ CFLAGS_KERNEL =
371371
AFLAGS_KERNEL =
372372
LDFLAGS_vmlinux =
373373
CFLAGS_GCOV = -fprofile-arcs -ftest-coverage -fno-tree-loop-im
374-
CFLAGS_KCOV = -fsanitize-coverage=trace-pc
374+
CFLAGS_KCOV := $(call cc-option,-fsanitize-coverage=trace-pc,)
375375

376376

377377
# Use USERINCLUDE when you must reference the UAPI directories only.
378378
USERINCLUDE := \
379379
-I$(srctree)/arch/$(hdr-arch)/include/uapi \
380-
-Iarch/$(hdr-arch)/include/generated/uapi \
380+
-I$(objtree)/arch/$(hdr-arch)/include/generated/uapi \
381381
-I$(srctree)/include/uapi \
382-
-Iinclude/generated/uapi \
382+
-I$(objtree)/include/generated/uapi \
383383
-include $(srctree)/include/linux/kconfig.h
384384

385385
# Use LINUXINCLUDE when you must reference the include/ directory.
386386
# Needed to be compatible with the O= option
387387
LINUXINCLUDE := \
388388
-I$(srctree)/arch/$(hdr-arch)/include \
389-
-Iarch/$(hdr-arch)/include/generated/uapi \
390-
-Iarch/$(hdr-arch)/include/generated \
389+
-I$(objtree)/arch/$(hdr-arch)/include/generated/uapi \
390+
-I$(objtree)/arch/$(hdr-arch)/include/generated \
391391
$(if $(KBUILD_SRC), -I$(srctree)/include) \
392-
-Iinclude \
393-
$(USERINCLUDE)
392+
-I$(objtree)/include
393+
394+
LINUXINCLUDE += $(filter-out $(LINUXINCLUDE),$(USERINCLUDE))
394395

395396
KBUILD_CPPFLAGS := -D__KERNEL__
396397

@@ -554,7 +555,7 @@ ifeq ($(KBUILD_EXTMOD),)
554555
# in parallel
555556
PHONY += scripts
556557
scripts: scripts_basic include/config/auto.conf include/config/tristate.conf \
557-
asm-generic
558+
asm-generic gcc-plugins
558559
$(Q)$(MAKE) $(build)=$(@)
559560

560561
# Objects we will link into vmlinux / subdirs we need to visit
@@ -635,6 +636,15 @@ endif
635636
# Tell gcc to never replace conditional load with a non-conditional one
636637
KBUILD_CFLAGS += $(call cc-option,--param=allow-store-data-races=0)
637638

639+
PHONY += gcc-plugins
640+
gcc-plugins: scripts_basic
641+
ifdef CONFIG_GCC_PLUGINS
642+
$(Q)$(MAKE) $(build)=scripts/gcc-plugins
643+
endif
644+
@:
645+
646+
include scripts/Makefile.gcc-plugins
647+
638648
ifdef CONFIG_READABLE_ASM
639649
# Disable optimizations that make assembler listings hard to read.
640650
# reorder blocks reorders the control in the function
@@ -666,21 +676,11 @@ endif
666676
endif
667677
# Find arch-specific stack protector compiler sanity-checking script.
668678
ifdef CONFIG_CC_STACKPROTECTOR
669-
stackp-path := $(srctree)/scripts/gcc-$(ARCH)_$(BITS)-has-stack-protector.sh
670-
ifneq ($(wildcard $(stackp-path)),)
671-
stackp-check := $(stackp-path)
672-
endif
679+
stackp-path := $(srctree)/scripts/gcc-$(SRCARCH)_$(BITS)-has-stack-protector.sh
680+
stackp-check := $(wildcard $(stackp-path))
673681
endif
674682
KBUILD_CFLAGS += $(stackp-flag)
675683

676-
ifdef CONFIG_KCOV
677-
ifeq ($(call cc-option, $(CFLAGS_KCOV)),)
678-
$(warning Cannot use CONFIG_KCOV: \
679-
-fsanitize-coverage=trace-pc is not supported by compiler)
680-
CFLAGS_KCOV =
681-
endif
682-
endif
683-
684684
ifeq ($(cc-name),clang)
685685
KBUILD_CPPFLAGS += $(call cc-option,-Qunused-arguments,)
686686
KBUILD_CPPFLAGS += $(call cc-option,-Wno-unknown-warning-option,)
@@ -1019,7 +1019,7 @@ prepare1: prepare2 $(version_h) include/generated/utsrelease.h \
10191019

10201020
archprepare: archheaders archscripts prepare1 scripts_basic
10211021

1022-
prepare0: archprepare
1022+
prepare0: archprepare gcc-plugins
10231023
$(Q)$(MAKE) $(build)=.
10241024

10251025
# All the preparing..
@@ -1531,6 +1531,7 @@ clean: $(clean-dirs)
15311531
-o -name '.*.d' -o -name '.*.tmp' -o -name '*.mod.c' \
15321532
-o -name '*.symtypes' -o -name 'modules.order' \
15331533
-o -name modules.builtin -o -name '.tmp_*.o.*' \
1534+
-o -name '*.c.[012]*.*' \
15341535
-o -name '*.gcno' \) -type f -print | xargs rm -f
15351536

15361537
# Generate tags for editors
@@ -1641,7 +1642,7 @@ endif
16411642
$(Q)$(MAKE) KBUILD_MODULES=$(if $(CONFIG_MODULES),1) \
16421643
$(build)=$(build-dir)
16431644
# Make sure the latest headers are built for Documentation
1644-
Documentation/: headers_install
1645+
Documentation/ samples/: headers_install
16451646
%/: prepare scripts FORCE
16461647
$(cmd_crmodverdir)
16471648
$(Q)$(MAKE) KBUILD_MODULES=$(if $(CONFIG_MODULES),1) \

arch/Kconfig

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,43 @@ config SECCOMP_FILTER
357357

358358
See Documentation/prctl/seccomp_filter.txt for details.
359359

360+
config HAVE_GCC_PLUGINS
361+
bool
362+
help
363+
An arch should select this symbol if it supports building with
364+
GCC plugins.
365+
366+
menuconfig GCC_PLUGINS
367+
bool "GCC plugins"
368+
depends on HAVE_GCC_PLUGINS
369+
depends on !COMPILE_TEST
370+
help
371+
GCC plugins are loadable modules that provide extra features to the
372+
compiler. They are useful for runtime instrumentation and static analysis.
373+
374+
See Documentation/gcc-plugins.txt for details.
375+
376+
config GCC_PLUGIN_CYC_COMPLEXITY
377+
bool "Compute the cyclomatic complexity of a function"
378+
depends on GCC_PLUGINS
379+
help
380+
The complexity M of a function's control flow graph is defined as:
381+
M = E - N + 2P
382+
where
383+
384+
E = the number of edges
385+
N = the number of nodes
386+
P = the number of connected components (exit nodes).
387+
388+
config GCC_PLUGIN_SANCOV
389+
bool
390+
depends on GCC_PLUGINS
391+
help
392+
This plugin inserts a __sanitizer_cov_trace_pc() call at the start of
393+
basic blocks. It supports all gcc versions with plugin support (from
394+
gcc-4.5 on). It is based on the commit "Add fuzzing coverage support"
395+
by Dmitry Vyukov <dvyukov@google.com>.
396+
360397
config HAVE_CC_STACKPROTECTOR
361398
bool
362399
help

arch/alpha/boot/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ targets := vmlinux.gz vmlinux \
1515
OBJSTRIP := $(obj)/tools/objstrip
1616

1717
HOSTCFLAGS := -Wall -I$(objtree)/usr/include
18-
BOOTCFLAGS += -I$(obj) -I$(srctree)/$(obj)
18+
BOOTCFLAGS += -I$(objtree)/$(obj) -I$(srctree)/$(obj)
1919

2020
# SRM bootable image. Copy to offset 512 of a partition.
2121
$(obj)/bootimage: $(addprefix $(obj)/tools/,mkbb lxboot bootlx) $(obj)/vmlinux.nh

arch/arm/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ config ARM
5454
select HAVE_FTRACE_MCOUNT_RECORD if (!XIP_KERNEL)
5555
select HAVE_FUNCTION_GRAPH_TRACER if (!THUMB2_KERNEL)
5656
select HAVE_FUNCTION_TRACER if (!XIP_KERNEL)
57+
select HAVE_GCC_PLUGINS
5758
select HAVE_GENERIC_DMA_COHERENT
5859
select HAVE_HW_BREAKPOINT if (PERF_EVENTS && (CPU_V6 || CPU_V6K || CPU_V7))
5960
select HAVE_IDE if PCI || ISA || PCMCIA

arch/arm64/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ config ARM64
7878
select HAVE_FTRACE_MCOUNT_RECORD
7979
select HAVE_FUNCTION_TRACER
8080
select HAVE_FUNCTION_GRAPH_TRACER
81+
select HAVE_GCC_PLUGINS
8182
select HAVE_GENERIC_DMA_COHERENT
8283
select HAVE_HW_BREAKPOINT if PERF_EVENTS
8384
select HAVE_IRQ_TIME_ACCOUNTING

arch/powerpc/boot/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ ifeq ($(call cc-option-yn, -fstack-protector),y)
4343
BOOTCFLAGS += -fno-stack-protector
4444
endif
4545

46-
BOOTCFLAGS += -I$(obj) -I$(srctree)/$(obj)
46+
BOOTCFLAGS += -I$(objtree)/$(obj) -I$(srctree)/$(obj)
4747

4848
DTC_FLAGS ?= -p 1024
4949

arch/powerpc/kvm/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ common-objs-y += powerpc.o emulate.o emulate_loadstore.o
2020
obj-$(CONFIG_KVM_EXIT_TIMING) += timing.o
2121
obj-$(CONFIG_KVM_BOOK3S_HANDLER) += book3s_exports.o
2222

23-
AFLAGS_booke_interrupts.o := -I$(obj)
23+
AFLAGS_booke_interrupts.o := -I$(objtree)/$(obj)
2424

2525
kvm-e500-objs := \
2626
$(common-objs-y) \

arch/s390/boot/compressed/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ quiet_cmd_sizes = GEN $@
3333
$(obj)/sizes.h: vmlinux
3434
$(call if_changed,sizes)
3535

36-
AFLAGS_head.o += -I$(obj)
36+
AFLAGS_head.o += -I$(objtree)/$(obj)
3737
$(obj)/head.o: $(obj)/sizes.h
3838

39-
CFLAGS_misc.o += -I$(obj)
39+
CFLAGS_misc.o += -I$(objtree)/$(obj)
4040
$(obj)/misc.o: $(obj)/sizes.h
4141

4242
OBJCOPYFLAGS_vmlinux.bin := -R .comment -S

arch/um/Kconfig.common

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ config UML
99
select GENERIC_CPU_DEVICES
1010
select GENERIC_IO
1111
select GENERIC_CLOCKEVENTS
12+
select HAVE_GCC_PLUGINS
1213
select TTY # Needed for line.c
1314

1415
config MMU

arch/um/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ include $(ARCH_DIR)/Makefile-os-$(OS)
7878

7979
KBUILD_CPPFLAGS += -I$(srctree)/$(HOST_DIR)/include \
8080
-I$(srctree)/$(HOST_DIR)/include/uapi \
81-
-I$(HOST_DIR)/include/generated \
82-
-I$(HOST_DIR)/include/generated/uapi
81+
-I$(objtree)/$(HOST_DIR)/include/generated \
82+
-I$(objtree)/$(HOST_DIR)/include/generated/uapi
8383

8484
# -Derrno=kernel_errno - This turns all kernel references to errno into
8585
# kernel_errno to separate them from the libc errno. This allows -fno-common

arch/x86/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ config X86
111111
select HAVE_FUNCTION_GRAPH_FP_TEST
112112
select HAVE_FUNCTION_GRAPH_TRACER
113113
select HAVE_FUNCTION_TRACER
114+
select HAVE_GCC_PLUGINS
114115
select HAVE_GENERIC_DMA_COHERENT if X86_32
115116
select HAVE_HW_BREAKPOINT
116117
select HAVE_IDE

arch/x86/boot/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ $(obj)/zoffset.h: $(obj)/compressed/vmlinux FORCE
9696
$(call if_changed,zoffset)
9797

9898

99-
AFLAGS_header.o += -I$(obj)
99+
AFLAGS_header.o += -I$(objtree)/$(obj)
100100
$(obj)/header.o: $(obj)/zoffset.h
101101

102102
LDFLAGS_setup.elf := -T

arch/x86/entry/vdso/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ CFL := $(PROFILING) -mcmodel=small -fPIC -O2 -fasynchronous-unwind-tables -m64 \
7575
-fno-omit-frame-pointer -foptimize-sibling-calls \
7676
-DDISABLE_BRANCH_PROFILING -DBUILD_VDSO
7777

78-
$(vobjs): KBUILD_CFLAGS += $(CFL)
78+
$(vobjs): KBUILD_CFLAGS := $(filter-out $(GCC_PLUGINS_CFLAGS),$(KBUILD_CFLAGS)) $(CFL)
7979

8080
#
8181
# vDSO code runs in userspace and -pg doesn't help with profiling anyway.
@@ -145,6 +145,7 @@ KBUILD_CFLAGS_32 := $(filter-out -m64,$(KBUILD_CFLAGS))
145145
KBUILD_CFLAGS_32 := $(filter-out -mcmodel=kernel,$(KBUILD_CFLAGS_32))
146146
KBUILD_CFLAGS_32 := $(filter-out -fno-pic,$(KBUILD_CFLAGS_32))
147147
KBUILD_CFLAGS_32 := $(filter-out -mfentry,$(KBUILD_CFLAGS_32))
148+
KBUILD_CFLAGS_32 := $(filter-out $(GCC_PLUGINS_CFLAGS),$(KBUILD_CFLAGS_32))
148149
KBUILD_CFLAGS_32 += -m32 -msoft-float -mregparm=0 -fpic
149150
KBUILD_CFLAGS_32 += $(call cc-option, -fno-stack-protector)
150151
KBUILD_CFLAGS_32 += $(call cc-option, -foptimize-sibling-calls)

arch/x86/purgatory/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ PURGATORY_OBJS = $(addprefix $(obj)/,$(purgatory-y))
88
LDFLAGS_purgatory.ro := -e purgatory_start -r --no-undefined -nostdlib -z nodefaultlib
99
targets += purgatory.ro
1010

11+
KCOV_INSTRUMENT := n
12+
1113
# Default KBUILD_CFLAGS can have -pg option set when FTRACE is enabled. That
1214
# in turn leaves some undefined symbols like __fentry__ in purgatory and not
1315
# sure how to relocate those. Like kexec-tools, use custom flags.

0 commit comments

Comments
 (0)