xref: /linux/arch/x86/entry/vdso/Makefile (revision 27879e8cb6b0fdb5cdcd76685f290729309711c6)
1# SPDX-License-Identifier: GPL-2.0
2#
3# Building vDSO images for x86.
4#
5
6# Include the generic Makefile to check the built vDSO:
7include $(srctree)/lib/vdso/Makefile
8
9# Files to link into the vDSO:
10vobjs-y := vdso-note.o vclock_gettime.o vgetcpu.o vgetrandom.o vgetrandom-chacha.o
11vobjs32-y := vdso32/note.o vdso32/system_call.o vdso32/sigreturn.o
12vobjs32-y += vdso32/vclock_gettime.o vdso32/vgetcpu.o
13vobjs-$(CONFIG_X86_SGX)	+= vsgx.o
14
15# Files to link into the kernel:
16obj-y						+= vma.o extable.o
17
18# vDSO images to build:
19obj-$(CONFIG_X86_64)				+= vdso-image-64.o
20obj-$(CONFIG_X86_X32_ABI)			+= vdso-image-x32.o
21obj-$(CONFIG_COMPAT_32)				+= vdso-image-32.o vdso32-setup.o
22
23vobjs := $(addprefix $(obj)/, $(vobjs-y))
24vobjs32 := $(addprefix $(obj)/, $(vobjs32-y))
25
26$(obj)/vdso.o: $(obj)/vdso.so
27
28targets += vdso.lds $(vobjs-y)
29targets += vdso32/vdso32.lds $(vobjs32-y)
30
31targets += $(foreach x, 64 x32 32, vdso-image-$(x).c vdso$(x).so vdso$(x).so.dbg)
32
33CPPFLAGS_vdso.lds += -P -C
34
35VDSO_LDFLAGS_vdso.lds = -m elf_x86_64 -soname linux-vdso.so.1 --no-undefined \
36			-z max-page-size=4096
37
38$(obj)/vdso64.so.dbg: $(obj)/vdso.lds $(vobjs) FORCE
39	$(call if_changed,vdso_and_check)
40
41HOST_EXTRACFLAGS += -I$(srctree)/tools/include -I$(srctree)/include/uapi -I$(srctree)/arch/$(SUBARCH)/include/uapi
42hostprogs += vdso2c
43
44quiet_cmd_vdso2c = VDSO2C  $@
45      cmd_vdso2c = $(obj)/vdso2c $< $(<:%.dbg=%) $@
46
47$(obj)/vdso-image-%.c: $(obj)/vdso%.so.dbg $(obj)/vdso%.so $(obj)/vdso2c FORCE
48	$(call if_changed,vdso2c)
49
50#
51# Don't omit frame pointers for ease of userspace debugging, but do
52# optimize sibling calls.
53#
54CFL := $(PROFILING) -mcmodel=small -fPIC -O2 -fasynchronous-unwind-tables -m64 \
55       $(filter -g%,$(KBUILD_CFLAGS)) -fno-stack-protector \
56       -fno-omit-frame-pointer -foptimize-sibling-calls \
57       -DDISABLE_BRANCH_PROFILING -DBUILD_VDSO
58
59ifdef CONFIG_MITIGATION_RETPOLINE
60ifneq ($(RETPOLINE_VDSO_CFLAGS),)
61  CFL += $(RETPOLINE_VDSO_CFLAGS)
62endif
63endif
64
65$(vobjs): KBUILD_CFLAGS := $(filter-out $(PADDING_CFLAGS) $(CC_FLAGS_LTO) $(CC_FLAGS_CFI) $(RANDSTRUCT_CFLAGS) $(GCC_PLUGINS_CFLAGS) $(RETPOLINE_CFLAGS),$(KBUILD_CFLAGS)) $(CFL)
66$(vobjs): KBUILD_AFLAGS += -DBUILD_VDSO
67
68#
69# vDSO code runs in userspace and -pg doesn't help with profiling anyway.
70#
71CFLAGS_REMOVE_vclock_gettime.o = -pg
72CFLAGS_REMOVE_vdso32/vclock_gettime.o = -pg
73CFLAGS_REMOVE_vgetcpu.o = -pg
74CFLAGS_REMOVE_vdso32/vgetcpu.o = -pg
75CFLAGS_REMOVE_vsgx.o = -pg
76CFLAGS_REMOVE_vgetrandom.o = -pg
77
78#
79# X32 processes use x32 vDSO to access 64bit kernel data.
80#
81# Build x32 vDSO image:
82# 1. Compile x32 vDSO as 64bit.
83# 2. Convert object files to x32.
84# 3. Build x32 VDSO image with x32 objects, which contains 64bit codes
85# so that it can reach 64bit address space with 64bit pointers.
86#
87
88CPPFLAGS_vdsox32.lds = $(CPPFLAGS_vdso.lds)
89VDSO_LDFLAGS_vdsox32.lds = -m elf32_x86_64 -soname linux-vdso.so.1 \
90			   -z max-page-size=4096
91
92# x32-rebranded versions
93vobjx32s-y := $(vobjs-y:.o=-x32.o)
94
95# same thing, but in the output directory
96vobjx32s := $(addprefix $(obj)/, $(vobjx32s-y))
97
98# Convert 64bit object file to x32 for x32 vDSO.
99quiet_cmd_x32 = X32     $@
100      cmd_x32 = $(OBJCOPY) -O elf32-x86-64 $< $@
101
102$(obj)/%-x32.o: $(obj)/%.o FORCE
103	$(call if_changed,x32)
104
105targets += vdsox32.lds $(vobjx32s-y)
106
107$(obj)/%.so: OBJCOPYFLAGS := -S --remove-section __ex_table
108$(obj)/%.so: $(obj)/%.so.dbg FORCE
109	$(call if_changed,objcopy)
110
111$(obj)/vdsox32.so.dbg: $(obj)/vdsox32.lds $(vobjx32s) FORCE
112	$(call if_changed,vdso_and_check)
113
114CPPFLAGS_vdso32/vdso32.lds = $(CPPFLAGS_vdso.lds)
115VDSO_LDFLAGS_vdso32.lds = -m elf_i386 -soname linux-gate.so.1
116
117KBUILD_AFLAGS_32 := $(filter-out -m64,$(KBUILD_AFLAGS)) -DBUILD_VDSO
118$(obj)/vdso32.so.dbg: KBUILD_AFLAGS = $(KBUILD_AFLAGS_32)
119$(obj)/vdso32.so.dbg: asflags-$(CONFIG_X86_64) += -m32
120
121KBUILD_CFLAGS_32 := $(filter-out -m64,$(KBUILD_CFLAGS))
122KBUILD_CFLAGS_32 := $(filter-out -mcmodel=kernel,$(KBUILD_CFLAGS_32))
123KBUILD_CFLAGS_32 := $(filter-out -fno-pic,$(KBUILD_CFLAGS_32))
124KBUILD_CFLAGS_32 := $(filter-out -mfentry,$(KBUILD_CFLAGS_32))
125KBUILD_CFLAGS_32 := $(filter-out $(RANDSTRUCT_CFLAGS),$(KBUILD_CFLAGS_32))
126KBUILD_CFLAGS_32 := $(filter-out $(GCC_PLUGINS_CFLAGS),$(KBUILD_CFLAGS_32))
127KBUILD_CFLAGS_32 := $(filter-out $(RETPOLINE_CFLAGS),$(KBUILD_CFLAGS_32))
128KBUILD_CFLAGS_32 := $(filter-out $(CC_FLAGS_LTO),$(KBUILD_CFLAGS_32))
129KBUILD_CFLAGS_32 := $(filter-out $(CC_FLAGS_CFI),$(KBUILD_CFLAGS_32))
130KBUILD_CFLAGS_32 := $(filter-out $(PADDING_CFLAGS),$(KBUILD_CFLAGS_32))
131KBUILD_CFLAGS_32 += -m32 -msoft-float -mregparm=0 -fpic
132KBUILD_CFLAGS_32 += -fno-stack-protector
133KBUILD_CFLAGS_32 += $(call cc-option, -foptimize-sibling-calls)
134KBUILD_CFLAGS_32 += -fno-omit-frame-pointer
135KBUILD_CFLAGS_32 += -DDISABLE_BRANCH_PROFILING
136
137ifdef CONFIG_MITIGATION_RETPOLINE
138ifneq ($(RETPOLINE_VDSO_CFLAGS),)
139  KBUILD_CFLAGS_32 += $(RETPOLINE_VDSO_CFLAGS)
140endif
141endif
142
143$(obj)/vdso32.so.dbg: KBUILD_CFLAGS = $(KBUILD_CFLAGS_32)
144
145$(obj)/vdso32.so.dbg: $(obj)/vdso32/vdso32.lds $(vobjs32) FORCE
146	$(call if_changed,vdso_and_check)
147
148#
149# The DSO images are built using a special linker script.
150#
151quiet_cmd_vdso = VDSO    $@
152      cmd_vdso = $(LD) -o $@ \
153		       $(VDSO_LDFLAGS) $(VDSO_LDFLAGS_$(filter %.lds,$(^F))) \
154		       -T $(filter %.lds,$^) $(filter %.o,$^) && \
155		 sh $(src)/checkundef.sh '$(NM)' '$@'
156
157VDSO_LDFLAGS = -shared --hash-style=both --build-id=sha1 \
158	$(call ld-option, --eh-frame-hdr) -Bsymbolic -z noexecstack
159
160quiet_cmd_vdso_and_check = VDSO    $@
161      cmd_vdso_and_check = $(cmd_vdso); $(cmd_vdso_check)
162