xref: /linux/arch/x86/entry/vdso/Makefile (revision c61fea676bcb5f14adcd882a7f7d9c5b082fe922)
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
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
76
77#
78# X32 processes use x32 vDSO to access 64bit kernel data.
79#
80# Build x32 vDSO image:
81# 1. Compile x32 vDSO as 64bit.
82# 2. Convert object files to x32.
83# 3. Build x32 VDSO image with x32 objects, which contains 64bit codes
84# so that it can reach 64bit address space with 64bit pointers.
85#
86
87CPPFLAGS_vdsox32.lds = $(CPPFLAGS_vdso.lds)
88VDSO_LDFLAGS_vdsox32.lds = -m elf32_x86_64 -soname linux-vdso.so.1 \
89			   -z max-page-size=4096
90
91# x32-rebranded versions
92vobjx32s-y := $(vobjs-y:.o=-x32.o)
93
94# same thing, but in the output directory
95vobjx32s := $(addprefix $(obj)/, $(vobjx32s-y))
96
97# Convert 64bit object file to x32 for x32 vDSO.
98quiet_cmd_x32 = X32     $@
99      cmd_x32 = $(OBJCOPY) -O elf32-x86-64 $< $@
100
101$(obj)/%-x32.o: $(obj)/%.o FORCE
102	$(call if_changed,x32)
103
104targets += vdsox32.lds $(vobjx32s-y)
105
106$(obj)/%.so: OBJCOPYFLAGS := -S --remove-section __ex_table
107$(obj)/%.so: $(obj)/%.so.dbg FORCE
108	$(call if_changed,objcopy)
109
110$(obj)/vdsox32.so.dbg: $(obj)/vdsox32.lds $(vobjx32s) FORCE
111	$(call if_changed,vdso_and_check)
112
113CPPFLAGS_vdso32/vdso32.lds = $(CPPFLAGS_vdso.lds)
114VDSO_LDFLAGS_vdso32.lds = -m elf_i386 -soname linux-gate.so.1
115
116KBUILD_AFLAGS_32 := $(filter-out -m64,$(KBUILD_AFLAGS)) -DBUILD_VDSO
117$(obj)/vdso32.so.dbg: KBUILD_AFLAGS = $(KBUILD_AFLAGS_32)
118$(obj)/vdso32.so.dbg: asflags-$(CONFIG_X86_64) += -m32
119
120KBUILD_CFLAGS_32 := $(filter-out -m64,$(KBUILD_CFLAGS))
121KBUILD_CFLAGS_32 := $(filter-out -mcmodel=kernel,$(KBUILD_CFLAGS_32))
122KBUILD_CFLAGS_32 := $(filter-out -fno-pic,$(KBUILD_CFLAGS_32))
123KBUILD_CFLAGS_32 := $(filter-out -mfentry,$(KBUILD_CFLAGS_32))
124KBUILD_CFLAGS_32 := $(filter-out $(RANDSTRUCT_CFLAGS),$(KBUILD_CFLAGS_32))
125KBUILD_CFLAGS_32 := $(filter-out $(GCC_PLUGINS_CFLAGS),$(KBUILD_CFLAGS_32))
126KBUILD_CFLAGS_32 := $(filter-out $(RETPOLINE_CFLAGS),$(KBUILD_CFLAGS_32))
127KBUILD_CFLAGS_32 := $(filter-out $(CC_FLAGS_LTO),$(KBUILD_CFLAGS_32))
128KBUILD_CFLAGS_32 := $(filter-out $(CC_FLAGS_CFI),$(KBUILD_CFLAGS_32))
129KBUILD_CFLAGS_32 := $(filter-out $(PADDING_CFLAGS),$(KBUILD_CFLAGS_32))
130KBUILD_CFLAGS_32 += -m32 -msoft-float -mregparm=0 -fpic
131KBUILD_CFLAGS_32 += -fno-stack-protector
132KBUILD_CFLAGS_32 += $(call cc-option, -foptimize-sibling-calls)
133KBUILD_CFLAGS_32 += -fno-omit-frame-pointer
134KBUILD_CFLAGS_32 += -DDISABLE_BRANCH_PROFILING
135
136ifdef CONFIG_MITIGATION_RETPOLINE
137ifneq ($(RETPOLINE_VDSO_CFLAGS),)
138  KBUILD_CFLAGS_32 += $(RETPOLINE_VDSO_CFLAGS)
139endif
140endif
141
142$(obj)/vdso32.so.dbg: KBUILD_CFLAGS = $(KBUILD_CFLAGS_32)
143
144$(obj)/vdso32.so.dbg: $(obj)/vdso32/vdso32.lds $(vobjs32) FORCE
145	$(call if_changed,vdso_and_check)
146
147#
148# The DSO images are built using a special linker script.
149#
150quiet_cmd_vdso = VDSO    $@
151      cmd_vdso = $(LD) -o $@ \
152		       $(VDSO_LDFLAGS) $(VDSO_LDFLAGS_$(filter %.lds,$(^F))) \
153		       -T $(filter %.lds,$^) $(filter %.o,$^) && \
154		 sh $(src)/checkundef.sh '$(NM)' '$@'
155
156VDSO_LDFLAGS = -shared --hash-style=both --build-id=sha1 \
157	$(call ld-option, --eh-frame-hdr) -Bsymbolic -z noexecstack
158
159quiet_cmd_vdso_and_check = VDSO    $@
160      cmd_vdso_and_check = $(cmd_vdso); $(cmd_vdso_check)
161