Skip to content

Commit 42640b1

Browse files
committed
kbuild: move host .so build rules to scripts/gcc-plugins/Makefile
The host shared library rules are currently implemented in scripts/Makefile.host, but actually GCC-plugin is the only user of them. (The VDSO .so files are built for the target by different build rules) Hence, they do not need to be treewide available. Move all the relevant build rules to scripts/gcc-plugins/Makefile. I also optimized the build steps so *.so is directly built from .c because every upstream plugin is compiled from a single source file. I am still keeping the multi-file plugin support, which Kees Cook mentioned might be needed by out-of-tree plugins. (https://lkml.org/lkml/2019/1/11/1107) If the plugin, foo.so, is compiled from two files foo.c and foo2.c, then you can do like follows: foo-objs := foo.o foo2.o Single-file plugins do not need the *-objs notation. Signed-off-by: Masahiro Yamada <[email protected]> Acked-by: Kees Cook <[email protected]>
1 parent 16a122c commit 42640b1

File tree

4 files changed

+55
-43
lines changed

4 files changed

+55
-43
lines changed

scripts/Makefile.build

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ include $(kbuild-file)
4545

4646
include scripts/Makefile.lib
4747

48-
# Do not include host rules unless needed
49-
ifneq ($(hostprogs)$(hostcxxlibs-y)$(hostcxxlibs-m),)
48+
# Do not include hostprogs rules unless needed
49+
ifneq ($(hostprogs),)
5050
include scripts/Makefile.host
5151
endif
5252

scripts/Makefile.clean

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ subdir-ymn := $(addprefix $(obj)/,$(subdir-ymn))
2929

3030
__clean-files := $(extra-y) $(extra-m) $(extra-) \
3131
$(always) $(always-y) $(always-m) $(always-) $(targets) $(clean-files) \
32-
$(hostprogs) $(hostprogs-y) $(hostprogs-m) $(hostprogs-) $(userprogs) \
33-
$(hostcxxlibs-y) $(hostcxxlibs-m)
32+
$(hostprogs) $(hostprogs-y) $(hostprogs-m) $(hostprogs-) $(userprogs)
3433

3534
__clean-files := $(filter-out $(no-clean-files), $(__clean-files))
3635

scripts/Makefile.host

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ $(obj)/%.tab.c $(obj)/%.tab.h: $(src)/%.y FORCE
3939
# They are linked as C++ code to the executable qconf
4040

4141
__hostprogs := $(sort $(hostprogs))
42-
host-cxxshlib := $(sort $(hostcxxlibs-y) $(hostcxxlibs-m))
4342

4443
# C code
4544
# Executables compiled from a single .c file
@@ -61,16 +60,11 @@ host-cxxmulti := $(foreach m,$(__hostprogs),$(if $($(m)-cxxobjs),$(m)))
6160
# C++ Object (.o) files compiled from .cc files
6261
host-cxxobjs := $(sort $(foreach m,$(host-cxxmulti),$($(m)-cxxobjs)))
6362

64-
# Object (.o) files used by the shared libaries
65-
host-cxxshobjs := $(sort $(foreach m,$(host-cxxshlib),$($(m:.so=-objs))))
66-
6763
host-csingle := $(addprefix $(obj)/,$(host-csingle))
6864
host-cmulti := $(addprefix $(obj)/,$(host-cmulti))
6965
host-cobjs := $(addprefix $(obj)/,$(host-cobjs))
7066
host-cxxmulti := $(addprefix $(obj)/,$(host-cxxmulti))
7167
host-cxxobjs := $(addprefix $(obj)/,$(host-cxxobjs))
72-
host-cxxshlib := $(addprefix $(obj)/,$(host-cxxshlib))
73-
host-cxxshobjs := $(addprefix $(obj)/,$(host-cxxshobjs))
7468

7569
#####
7670
# Handle options to gcc. Support building with separate output directory
@@ -136,25 +130,5 @@ quiet_cmd_host-cxxobjs = HOSTCXX $@
136130
$(host-cxxobjs): $(obj)/%.o: $(src)/%.cc FORCE
137131
$(call if_changed_dep,host-cxxobjs)
138132

139-
# Compile .c file, create position independent .o file
140-
# Note that plugin capable gcc versions can be either C or C++ based
141-
# therefore plugin source files have to be compilable in both C and C++ mode.
142-
# This is why a C++ compiler is invoked on a .c file.
143-
# host-cxxshobjs -> .o
144-
quiet_cmd_host-cxxshobjs = HOSTCXX -fPIC $@
145-
cmd_host-cxxshobjs = $(HOSTCXX) $(hostcxx_flags) -fPIC -c -o $@ $<
146-
$(host-cxxshobjs): $(obj)/%.o: $(src)/%.c FORCE
147-
$(call if_changed_dep,host-cxxshobjs)
148-
149-
# Link a shared library, based on position independent .o files
150-
# *.o -> .so shared library (host-cxxshlib)
151-
quiet_cmd_host-cxxshlib = HOSTLLD -shared $@
152-
cmd_host-cxxshlib = $(HOSTCXX) $(KBUILD_HOSTLDFLAGS) -shared -o $@ \
153-
$(addprefix $(obj)/, $($(target-stem)-objs)) \
154-
$(KBUILD_HOSTLDLIBS) $(HOSTLDLIBS_$(target-stem).so)
155-
$(host-cxxshlib): FORCE
156-
$(call if_changed,host-cxxshlib)
157-
$(call multi_depend, $(host-cxxshlib), .so, -objs)
158-
159-
targets += $(host-csingle) $(host-cmulti) $(host-cobjs)\
160-
$(host-cxxmulti) $(host-cxxobjs) $(host-cxxshlib) $(host-cxxshobjs)
133+
targets += $(host-csingle) $(host-cmulti) $(host-cobjs) \
134+
$(host-cxxmulti) $(host-cxxobjs)

scripts/gcc-plugins/Makefile

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,61 @@
11
# SPDX-License-Identifier: GPL-2.0
2-
GCC_PLUGINS_DIR := $(shell $(CC) -print-file-name=plugin)
32

4-
HOST_EXTRACXXFLAGS += -I$(GCC_PLUGINS_DIR)/include -I$(src) -std=gnu++98 -fno-rtti
5-
HOST_EXTRACXXFLAGS += -fno-exceptions -fasynchronous-unwind-tables -ggdb
6-
HOST_EXTRACXXFLAGS += -Wno-narrowing -Wno-unused-variable -Wno-c++11-compat
7-
HOST_EXTRACXXFLAGS += -Wno-format-diag
8-
9-
$(obj)/randomize_layout_plugin.o: $(objtree)/$(obj)/randomize_layout_seed.h
3+
$(obj)/randomize_layout_plugin.so: $(objtree)/$(obj)/randomize_layout_seed.h
104
quiet_cmd_create_randomize_layout_seed = GENSEED $@
115
cmd_create_randomize_layout_seed = \
126
$(CONFIG_SHELL) $(srctree)/$(src)/gen-random-seed.sh $@ $(objtree)/include/generated/randomize_layout_hash.h
137
$(objtree)/$(obj)/randomize_layout_seed.h: FORCE
148
$(call if_changed,create_randomize_layout_seed)
15-
targets = randomize_layout_seed.h randomize_layout_hash.h
9+
targets += randomize_layout_seed.h randomize_layout_hash.h
10+
11+
# Build rules for plugins
12+
#
13+
# No extra code is needed for single-file plugins.
14+
# For multi-file plugins, use *-objs syntax to list the objects.
15+
#
16+
# If the plugin foo.so is compiled from foo.c and foo2.c, you can do:
17+
#
18+
# foo-objs := foo.o foo2.o
19+
20+
always-y += $(GCC_PLUGIN)
1621

17-
hostcxxlibs-y := $(GCC_PLUGIN)
18-
always-y := $(hostcxxlibs-y)
22+
GCC_PLUGINS_DIR = $(shell $(CC) -print-file-name=plugin)
1923

20-
$(foreach p,$(hostcxxlibs-y:%.so=%),$(eval $(p)-objs := $(p).o))
24+
plugin_cxxflags = -Wp,-MMD,$(depfile) $(KBUILD_HOSTCXXFLAGS) -fPIC \
25+
-I $(GCC_PLUGINS_DIR)/include -I $(obj) -std=gnu++98 \
26+
-fno-rtti -fno-exceptions -fasynchronous-unwind-tables \
27+
-ggdb -Wno-narrowing -Wno-unused-variable -Wno-c++11-compat \
28+
-Wno-format-diag
2129

30+
plugin_ldflags = -shared
31+
32+
plugin-single := $(foreach m, $(GCC_PLUGIN), $(if $($(m:%.so=%-objs)),,$(m)))
33+
plugin-multi := $(filter-out $(plugin-single), $(GCC_PLUGIN))
34+
plugin-objs := $(sort $(foreach m, $(plugin-multi), $($(m:%.so=%-objs))))
35+
36+
targets += $(plugin-single) $(plugin-multi) $(plugin-objs)
2237
clean-files += *.so
38+
39+
plugin-single := $(addprefix $(obj)/, $(plugin-single))
40+
plugin-multi := $(addprefix $(obj)/, $(plugin-multi))
41+
plugin-objs := $(addprefix $(obj)/, $(plugin-objs))
42+
43+
quiet_cmd_plugin_cxx_so_c = HOSTCXX $@
44+
cmd_plugin_cxx_so_c = $(HOSTCXX) $(plugin_cxxflags) $(plugin_ldflags) -o $@ $<
45+
46+
$(plugin-single): $(obj)/%.so: $(src)/%.c FORCE
47+
$(call if_changed_dep,plugin_cxx_so_c)
48+
49+
quiet_cmd_plugin_ld_so_o = HOSTLD $@
50+
cmd_plugin_ld_so_o = $(HOSTCXX) $(plugin_ldflags) -o $@ \
51+
$(addprefix $(obj)/, $($(target-stem)-objs))
52+
53+
$(plugin-multi): FORCE
54+
$(call if_changed,plugin_ld_so_o)
55+
$(foreach m, $(notdir $(plugin-multi)), $(eval $(obj)/$m: $(addprefix $(obj)/, $($(m:%.so=%-objs)))))
56+
57+
quiet_cmd_plugin_cxx_o_c = HOSTCXX $@
58+
cmd_plugin_cxx_o_c = $(HOSTCXX) $(plugin_cxxflags) -c -o $@ $<
59+
60+
$(plugin-objs): $(obj)/%.o: $(src)/%.c FORCE
61+
$(call if_changed_dep,plugin_cxx_o_c)

0 commit comments

Comments
 (0)