Makefile 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. # SPDX-License-Identifier: GPL-2.0
  2. #
  3. # Building vDSO images for x86.
  4. #
  5. KASAN_SANITIZE := n
  6. UBSAN_SANITIZE := n
  7. OBJECT_FILES_NON_STANDARD := y
  8. # Prevents link failures: __sanitizer_cov_trace_pc() is not linked in.
  9. KCOV_INSTRUMENT := n
  10. VDSO64-$(CONFIG_X86_64) := y
  11. VDSOX32-$(CONFIG_X86_X32_ABI) := y
  12. VDSO32-$(CONFIG_X86_32) := y
  13. VDSO32-$(CONFIG_IA32_EMULATION) := y
  14. # files to link into the vdso
  15. vobjs-y := vdso-note.o vclock_gettime.o vgetcpu.o
  16. # files to link into kernel
  17. obj-y += vma.o
  18. OBJECT_FILES_NON_STANDARD_vma.o := n
  19. # vDSO images to build
  20. vdso_img-$(VDSO64-y) += 64
  21. vdso_img-$(VDSOX32-y) += x32
  22. vdso_img-$(VDSO32-y) += 32
  23. obj-$(VDSO32-y) += vdso32-setup.o
  24. vobjs := $(foreach F,$(vobjs-y),$(obj)/$F)
  25. $(obj)/vdso.o: $(obj)/vdso.so
  26. targets += vdso.lds $(vobjs-y)
  27. # Build the vDSO image C files and link them in.
  28. vdso_img_objs := $(vdso_img-y:%=vdso-image-%.o)
  29. vdso_img_cfiles := $(vdso_img-y:%=vdso-image-%.c)
  30. vdso_img_sodbg := $(vdso_img-y:%=vdso%.so.dbg)
  31. obj-y += $(vdso_img_objs)
  32. targets += $(vdso_img_cfiles)
  33. targets += $(vdso_img_sodbg)
  34. .SECONDARY: $(vdso_img-y:%=$(obj)/vdso-image-%.c) \
  35. $(vdso_img-y:%=$(obj)/vdso%.so)
  36. export CPPFLAGS_vdso.lds += -P -C
  37. VDSO_LDFLAGS_vdso.lds = -m elf_x86_64 -soname linux-vdso.so.1 --no-undefined \
  38. -z max-page-size=4096
  39. $(obj)/vdso64.so.dbg: $(src)/vdso.lds $(vobjs) FORCE
  40. $(call if_changed,vdso)
  41. HOST_EXTRACFLAGS += -I$(srctree)/tools/include -I$(srctree)/include/uapi -I$(srctree)/arch/$(SUBARCH)/include/uapi
  42. hostprogs-y += vdso2c
  43. quiet_cmd_vdso2c = VDSO2C $@
  44. define cmd_vdso2c
  45. $(obj)/vdso2c $< $(<:%.dbg=%) $@
  46. endef
  47. $(obj)/vdso-image-%.c: $(obj)/vdso%.so.dbg $(obj)/vdso%.so $(obj)/vdso2c FORCE
  48. $(call if_changed,vdso2c)
  49. #
  50. # Don't omit frame pointers for ease of userspace debugging, but do
  51. # optimize sibling calls.
  52. #
  53. CFL := $(PROFILING) -mcmodel=small -fPIC -O2 -fasynchronous-unwind-tables -m64 \
  54. $(filter -g%,$(KBUILD_CFLAGS)) $(call cc-option, -fno-stack-protector) \
  55. -fno-omit-frame-pointer -foptimize-sibling-calls \
  56. -DDISABLE_BRANCH_PROFILING -DBUILD_VDSO $(DISABLE_LTO)
  57. ifdef CONFIG_RETPOLINE
  58. ifneq ($(RETPOLINE_VDSO_CFLAGS),)
  59. CFL += $(RETPOLINE_VDSO_CFLAGS)
  60. endif
  61. endif
  62. $(vobjs): KBUILD_CFLAGS := $(filter-out $(GCC_PLUGINS_CFLAGS) $(RETPOLINE_CFLAGS),$(KBUILD_CFLAGS)) $(CFL)
  63. #
  64. # vDSO code runs in userspace and -pg doesn't help with profiling anyway.
  65. #
  66. CFLAGS_REMOVE_vdso-note.o = -pg
  67. CFLAGS_REMOVE_vclock_gettime.o = -pg
  68. CFLAGS_REMOVE_vgetcpu.o = -pg
  69. CFLAGS_REMOVE_vvar.o = -pg
  70. #
  71. # X32 processes use x32 vDSO to access 64bit kernel data.
  72. #
  73. # Build x32 vDSO image:
  74. # 1. Compile x32 vDSO as 64bit.
  75. # 2. Convert object files to x32.
  76. # 3. Build x32 VDSO image with x32 objects, which contains 64bit codes
  77. # so that it can reach 64bit address space with 64bit pointers.
  78. #
  79. CPPFLAGS_vdsox32.lds = $(CPPFLAGS_vdso.lds)
  80. VDSO_LDFLAGS_vdsox32.lds = -m elf32_x86_64 -soname linux-vdso.so.1 \
  81. -z max-page-size=4096
  82. # 64-bit objects to re-brand as x32
  83. vobjs64-for-x32 := $(filter-out $(vobjs-nox32),$(vobjs-y))
  84. # x32-rebranded versions
  85. vobjx32s-y := $(vobjs64-for-x32:.o=-x32.o)
  86. # same thing, but in the output directory
  87. vobjx32s := $(foreach F,$(vobjx32s-y),$(obj)/$F)
  88. # Convert 64bit object file to x32 for x32 vDSO.
  89. quiet_cmd_x32 = X32 $@
  90. cmd_x32 = $(OBJCOPY) -O elf32-x86-64 $< $@
  91. $(obj)/%-x32.o: $(obj)/%.o FORCE
  92. $(call if_changed,x32)
  93. targets += vdsox32.lds $(vobjx32s-y)
  94. $(obj)/%.so: OBJCOPYFLAGS := -S
  95. $(obj)/%.so: $(obj)/%.so.dbg
  96. $(call if_changed,objcopy)
  97. $(obj)/vdsox32.so.dbg: $(src)/vdsox32.lds $(vobjx32s) FORCE
  98. $(call if_changed,vdso)
  99. CPPFLAGS_vdso32.lds = $(CPPFLAGS_vdso.lds)
  100. VDSO_LDFLAGS_vdso32.lds = -m elf_i386 -soname linux-gate.so.1
  101. # This makes sure the $(obj) subdirectory exists even though vdso32/
  102. # is not a kbuild sub-make subdirectory.
  103. override obj-dirs = $(dir $(obj)) $(obj)/vdso32/
  104. targets += vdso32/vdso32.lds
  105. targets += vdso32/note.o vdso32/system_call.o vdso32/sigreturn.o
  106. targets += vdso32/vclock_gettime.o
  107. KBUILD_AFLAGS_32 := $(filter-out -m64,$(KBUILD_AFLAGS)) -DBUILD_VDSO
  108. $(obj)/vdso32.so.dbg: KBUILD_AFLAGS = $(KBUILD_AFLAGS_32)
  109. $(obj)/vdso32.so.dbg: asflags-$(CONFIG_X86_64) += -m32
  110. KBUILD_CFLAGS_32 := $(filter-out -m64,$(KBUILD_CFLAGS))
  111. KBUILD_CFLAGS_32 := $(filter-out -mcmodel=kernel,$(KBUILD_CFLAGS_32))
  112. KBUILD_CFLAGS_32 := $(filter-out -fno-pic,$(KBUILD_CFLAGS_32))
  113. KBUILD_CFLAGS_32 := $(filter-out -mfentry,$(KBUILD_CFLAGS_32))
  114. KBUILD_CFLAGS_32 := $(filter-out $(GCC_PLUGINS_CFLAGS),$(KBUILD_CFLAGS_32))
  115. KBUILD_CFLAGS_32 := $(filter-out $(RETPOLINE_CFLAGS),$(KBUILD_CFLAGS_32))
  116. KBUILD_CFLAGS_32 := $(filter-out $(LTO_CFLAGS),$(KBUILD_CFLAGS_32))
  117. KBUILD_CFLAGS_32 := $(filter-out $(CFI_CFLAGS),$(KBUILD_CFLAGS_32))
  118. KBUILD_CFLAGS_32 += -m32 -msoft-float -mregparm=0 -fpic
  119. KBUILD_CFLAGS_32 += $(call cc-option, -fno-stack-protector)
  120. KBUILD_CFLAGS_32 += $(call cc-option, -foptimize-sibling-calls)
  121. KBUILD_CFLAGS_32 += -fno-omit-frame-pointer
  122. KBUILD_CFLAGS_32 += -DDISABLE_BRANCH_PROFILING
  123. ifdef CONFIG_RETPOLINE
  124. ifneq ($(RETPOLINE_VDSO_CFLAGS),)
  125. KBUILD_CFLAGS_32 += $(RETPOLINE_VDSO_CFLAGS)
  126. endif
  127. endif
  128. $(obj)/vdso32.so.dbg: KBUILD_CFLAGS = $(KBUILD_CFLAGS_32)
  129. $(obj)/vdso32.so.dbg: FORCE \
  130. $(obj)/vdso32/vdso32.lds \
  131. $(obj)/vdso32/vclock_gettime.o \
  132. $(obj)/vdso32/note.o \
  133. $(obj)/vdso32/system_call.o \
  134. $(obj)/vdso32/sigreturn.o
  135. $(call if_changed,vdso)
  136. #
  137. # The DSO images are built using a special linker script.
  138. #
  139. quiet_cmd_vdso = VDSO $@
  140. cmd_vdso = $(LD) -nostdlib -o $@ \
  141. $(VDSO_LDFLAGS) $(VDSO_LDFLAGS_$(filter %.lds,$(^F))) \
  142. -T $(filter %.lds,$^) $(filter %.o,$^) && \
  143. sh $(srctree)/$(src)/checkundef.sh '$(NM)' '$@'
  144. VDSO_LDFLAGS = -shared $(call ld-option, --hash-style=both) \
  145. $(call ld-option, --build-id) $(call ld-option, --eh-frame-hdr) \
  146. -Bsymbolic
  147. GCOV_PROFILE := n
  148. #
  149. # Install the unstripped copies of vdso*.so. If our toolchain supports
  150. # build-id, install .build-id links as well.
  151. #
  152. quiet_cmd_vdso_install = INSTALL $(@:install_%=%)
  153. define cmd_vdso_install
  154. cp $< "$(MODLIB)/vdso/$(@:install_%=%)"; \
  155. if readelf -n $< |grep -q 'Build ID'; then \
  156. buildid=`readelf -n $< |grep 'Build ID' |sed -e 's/^.*Build ID: \(.*\)$$/\1/'`; \
  157. first=`echo $$buildid | cut -b-2`; \
  158. last=`echo $$buildid | cut -b3-`; \
  159. mkdir -p "$(MODLIB)/vdso/.build-id/$$first"; \
  160. ln -sf "../../$(@:install_%=%)" "$(MODLIB)/vdso/.build-id/$$first/$$last.debug"; \
  161. fi
  162. endef
  163. vdso_img_insttargets := $(vdso_img_sodbg:%.dbg=install_%)
  164. $(MODLIB)/vdso: FORCE
  165. @mkdir -p $(MODLIB)/vdso
  166. $(vdso_img_insttargets): install_%: $(obj)/%.dbg $(MODLIB)/vdso
  167. $(call cmd,vdso_install)
  168. PHONY += vdso_install $(vdso_img_insttargets)
  169. vdso_install: $(vdso_img_insttargets)
  170. clean-files := vdso32.so vdso32.so.dbg vdso64* vdso-image-*.c vdsox32.so*