xref: /linux/arch/riscv/Makefile (revision ec2e0fb07d789976c601bec19ecced7a501c3705)
1# This file is included by the global makefile so that you can add your own
2# architecture-specific flags and dependencies.
3#
4# This file is subject to the terms and conditions of the GNU General Public
5# License.  See the file "COPYING" in the main directory of this archive
6# for more details.
7#
8
9LDFLAGS_vmlinux := -z norelro
10ifeq ($(CONFIG_RELOCATABLE),y)
11	LDFLAGS_vmlinux += -shared -Bsymbolic -z notext
12	KBUILD_CFLAGS += -fPIE
13endif
14ifeq ($(CONFIG_DYNAMIC_FTRACE),y)
15	LDFLAGS_vmlinux += --no-relax
16	KBUILD_CPPFLAGS += -DCC_USING_PATCHABLE_FUNCTION_ENTRY
17ifeq ($(CONFIG_RISCV_ISA_C),y)
18	CC_FLAGS_FTRACE := -fpatchable-function-entry=8,4
19else
20	CC_FLAGS_FTRACE := -fpatchable-function-entry=4,2
21endif
22endif
23
24ifeq ($(CONFIG_CMODEL_MEDLOW),y)
25KBUILD_CFLAGS_MODULE += -mcmodel=medany
26endif
27
28export BITS
29ifeq ($(CONFIG_ARCH_RV64I),y)
30	BITS := 64
31	UTS_MACHINE := riscv64
32
33	KBUILD_CFLAGS += -mabi=lp64
34	KBUILD_AFLAGS += -mabi=lp64
35
36	KBUILD_LDFLAGS += -melf64lriscv
37
38	KBUILD_RUSTFLAGS += -Ctarget-cpu=generic-rv64 --target=riscv64imac-unknown-none-elf \
39			    -Cno-redzone
40else
41	BITS := 32
42	UTS_MACHINE := riscv32
43
44	KBUILD_CFLAGS += -mabi=ilp32
45	KBUILD_AFLAGS += -mabi=ilp32
46	KBUILD_LDFLAGS += -melf32lriscv
47endif
48
49# LLVM has an issue with target-features and LTO: https://github.com/llvm/llvm-project/issues/59350
50# Ensure it is aware of linker relaxation with LTO, otherwise relocations may
51# be incorrect: https://github.com/llvm/llvm-project/issues/65090
52ifeq ($(CONFIG_LTO_CLANG),y)
53	KBUILD_LDFLAGS += -mllvm -mattr=+c -mllvm -mattr=+relax
54endif
55
56ifeq ($(CONFIG_SHADOW_CALL_STACK),y)
57	KBUILD_LDFLAGS += --no-relax-gp
58endif
59
60# ISA string setting
61riscv-march-$(CONFIG_ARCH_RV32I)	:= rv32ima
62riscv-march-$(CONFIG_ARCH_RV64I)	:= rv64ima
63riscv-march-$(CONFIG_FPU)		:= $(riscv-march-y)fd
64riscv-march-$(CONFIG_RISCV_ISA_C)	:= $(riscv-march-y)c
65riscv-march-$(CONFIG_RISCV_ISA_V)	:= $(riscv-march-y)v
66
67ifneq ($(CONFIG_RISCV_ISA_C),y)
68	KBUILD_RUSTFLAGS += -Ctarget-feature=-c
69endif
70
71ifdef CONFIG_TOOLCHAIN_NEEDS_OLD_ISA_SPEC
72KBUILD_CFLAGS += -Wa,-misa-spec=2.2
73KBUILD_AFLAGS += -Wa,-misa-spec=2.2
74else
75riscv-march-$(CONFIG_TOOLCHAIN_NEEDS_EXPLICIT_ZICSR_ZIFENCEI) := $(riscv-march-y)_zicsr_zifencei
76endif
77
78# Check if the toolchain supports Zacas
79riscv-march-$(CONFIG_TOOLCHAIN_HAS_ZACAS) := $(riscv-march-y)_zacas
80
81# Check if the toolchain supports Zabha
82riscv-march-$(CONFIG_TOOLCHAIN_HAS_ZABHA) := $(riscv-march-y)_zabha
83
84# Remove F,D,V from isa string for all. Keep extensions between "fd" and "v" by
85# matching non-v and non-multi-letter extensions out with the filter ([^v_]*)
86KBUILD_CFLAGS += -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima|rv64ima)fd([^v_]*)v?/\1\2/')
87
88KBUILD_AFLAGS += -march=$(riscv-march-y)
89
90# For C code built with floating-point support, exclude V but keep F and D.
91CC_FLAGS_FPU  := -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima|rv64ima)([^v_]*)v?/\1\2/')
92
93KBUILD_CFLAGS += -mno-save-restore
94
95ifeq ($(CONFIG_CMODEL_MEDLOW),y)
96	KBUILD_CFLAGS += -mcmodel=medlow
97endif
98ifeq ($(CONFIG_CMODEL_MEDANY),y)
99	KBUILD_CFLAGS += -mcmodel=medany
100endif
101
102# Avoid generating .eh_frame sections.
103KBUILD_CFLAGS += -fno-asynchronous-unwind-tables -fno-unwind-tables
104
105# The RISC-V attributes frequently cause compatibility issues and provide no
106# information, so just turn them off.
107KBUILD_CFLAGS += $(call cc-option,-mno-riscv-attribute)
108KBUILD_AFLAGS += $(call cc-option,-mno-riscv-attribute)
109KBUILD_CFLAGS += $(call as-option,-Wa$(comma)-mno-arch-attr)
110KBUILD_AFLAGS += $(call as-option,-Wa$(comma)-mno-arch-attr)
111
112KBUILD_CFLAGS_MODULE += $(call cc-option,-mno-relax)
113KBUILD_AFLAGS_MODULE += $(call as-option,-Wa$(comma)-mno-relax)
114
115# GCC versions that support the "-mstrict-align" option default to allowing
116# unaligned accesses.  While unaligned accesses are explicitly allowed in the
117# RISC-V ISA, they're emulated by machine mode traps on all extant
118# architectures.  It's faster to have GCC emit only aligned accesses.
119ifneq ($(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS),y)
120KBUILD_CFLAGS += $(call cc-option,-mstrict-align)
121endif
122
123ifeq ($(CONFIG_STACKPROTECTOR_PER_TASK),y)
124prepare: stack_protector_prepare
125stack_protector_prepare: prepare0
126	$(eval KBUILD_CFLAGS += -mstack-protector-guard=tls		  \
127				-mstack-protector-guard-reg=tp		  \
128				-mstack-protector-guard-offset=$(shell	  \
129			awk '{if ($$2 == "TSK_STACK_CANARY") print $$3;}' \
130				$(objtree)/include/generated/asm-offsets.h))
131endif
132
133# arch specific predefines for sparse
134CHECKFLAGS += -D__riscv -D__riscv_xlen=$(BITS)
135
136# Default target when executing plain make
137boot		:= arch/riscv/boot
138ifeq ($(CONFIG_XIP_KERNEL),y)
139KBUILD_IMAGE := $(boot)/xipImage
140else
141ifeq ($(CONFIG_RISCV_M_MODE)$(CONFIG_SOC_CANAAN_K210),yy)
142KBUILD_IMAGE := $(boot)/loader.bin
143else
144ifeq ($(CONFIG_EFI_ZBOOT),)
145KBUILD_IMAGE	:= $(boot)/Image.gz
146else
147KBUILD_IMAGE := $(boot)/vmlinuz.efi
148endif
149endif
150endif
151
152boot					:= arch/riscv/boot
153boot-image-y				:= Image
154boot-image-$(CONFIG_KERNEL_BZIP2)	:= Image.bz2
155boot-image-$(CONFIG_KERNEL_GZIP)	:= Image.gz
156boot-image-$(CONFIG_KERNEL_LZ4)		:= Image.lz4
157boot-image-$(CONFIG_KERNEL_LZMA)	:= Image.lzma
158boot-image-$(CONFIG_KERNEL_LZO)		:= Image.lzo
159boot-image-$(CONFIG_KERNEL_ZSTD)	:= Image.zst
160boot-image-$(CONFIG_KERNEL_XZ)		:= Image.xz
161ifdef CONFIG_RISCV_M_MODE
162boot-image-$(CONFIG_ARCH_CANAAN)	:= loader.bin
163endif
164boot-image-$(CONFIG_EFI_ZBOOT)		:= vmlinuz.efi
165boot-image-$(CONFIG_XIP_KERNEL)		:= xipImage
166KBUILD_IMAGE				:= $(boot)/$(boot-image-y)
167
168libs-y += arch/riscv/lib/
169libs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a
170
171ifeq ($(KBUILD_EXTMOD),)
172ifeq ($(CONFIG_MMU),y)
173prepare: vdso_prepare
174vdso_prepare: prepare0
175	$(Q)$(MAKE) $(build)=arch/riscv/kernel/vdso include/generated/vdso-offsets.h
176	$(if $(CONFIG_COMPAT),$(Q)$(MAKE) \
177		$(build)=arch/riscv/kernel/compat_vdso include/generated/compat_vdso-offsets.h)
178
179endif
180endif
181
182vdso-install-y			+= arch/riscv/kernel/vdso/vdso.so.dbg
183vdso-install-$(CONFIG_COMPAT)	+= arch/riscv/kernel/compat_vdso/compat_vdso.so.dbg
184
185BOOT_TARGETS := Image Image.gz Image.bz2 Image.lz4 Image.lzma Image.lzo Image.zst Image.xz loader loader.bin xipImage vmlinuz.efi
186
187all:	$(notdir $(KBUILD_IMAGE))
188
189loader.bin: loader
190Image.gz Image.bz2 Image.lz4 Image.lzma Image.lzo Image.zst Image.xz loader xipImage vmlinuz.efi: Image
191
192$(BOOT_TARGETS): vmlinux
193	$(Q)$(MAKE) $(build)=$(boot) $(boot)/$@
194	@$(kecho) '  Kernel: $(boot)/$@ is ready'
195
196# the install target always installs KBUILD_IMAGE (which may be compressed)
197# but keep the zinstall target for compatibility with older releases
198install zinstall:
199	$(call cmd,install)
200
201PHONY += rv32_randconfig
202rv32_randconfig:
203	$(Q)$(MAKE) KCONFIG_ALLCONFIG=$(srctree)/arch/riscv/configs/32-bit.config \
204		-f $(srctree)/Makefile randconfig
205
206PHONY += rv64_randconfig
207rv64_randconfig:
208	$(Q)$(MAKE) KCONFIG_ALLCONFIG=$(srctree)/arch/riscv/configs/64-bit.config \
209		-f $(srctree)/Makefile randconfig
210
211PHONY += rv32_defconfig
212rv32_defconfig:
213	$(Q)$(MAKE) -f $(srctree)/Makefile defconfig 32-bit.config
214
215PHONY += rv32_nommu_virt_defconfig
216rv32_nommu_virt_defconfig:
217	$(Q)$(MAKE) -f $(srctree)/Makefile nommu_virt_defconfig 32-bit.config
218
219define archhelp
220  echo  '  Image		- Uncompressed kernel image (arch/riscv/boot/Image)'
221  echo  '  Image.gz	- Compressed kernel image (arch/riscv/boot/Image.gz)'
222  echo  '  Image.bz2	- Compressed kernel image (arch/riscv/boot/Image.bz2)'
223  echo  '  Image.lz4	- Compressed kernel image (arch/riscv/boot/Image.lz4)'
224  echo  '  Image.lzma	- Compressed kernel image (arch/riscv/boot/Image.lzma)'
225  echo  '  Image.lzo	- Compressed kernel image (arch/riscv/boot/Image.lzo)'
226  echo  '  Image.zst	- Compressed kernel image (arch/riscv/boot/Image.zst)'
227  echo  '  Image.xz	- Compressed kernel image (arch/riscv/boot/Image.xz)'
228  echo  '  vmlinuz.efi	- Compressed EFI kernel image (arch/riscv/boot/vmlinuz.efi)'
229  echo  '		  Default when CONFIG_EFI_ZBOOT=y'
230  echo  '  xipImage	- Execute-in-place kernel image (arch/riscv/boot/xipImage)'
231  echo  '		  Default when CONFIG_XIP_KERNEL=y'
232  echo  '  install	- Install kernel using (your) ~/bin/$(INSTALLKERNEL) or'
233  echo  '		  (distribution) /sbin/$(INSTALLKERNEL) or install to '
234  echo  '		  $$(INSTALL_PATH)'
235endef
236