xref: /linux/arch/riscv/Makefile (revision a8d70602b186f3c347e62c59a418be802b71886d)
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
9OBJCOPYFLAGS    := -O binary
10LDFLAGS_vmlinux := -z norelro
11ifeq ($(CONFIG_RELOCATABLE),y)
12	LDFLAGS_vmlinux += -shared -Bsymbolic -z notext --emit-relocs
13	KBUILD_CFLAGS += -fPIE
14endif
15ifeq ($(CONFIG_DYNAMIC_FTRACE),y)
16	LDFLAGS_vmlinux += --no-relax
17	KBUILD_CPPFLAGS += -DCC_USING_PATCHABLE_FUNCTION_ENTRY
18ifeq ($(CONFIG_RISCV_ISA_C),y)
19	CC_FLAGS_FTRACE := -fpatchable-function-entry=4
20else
21	CC_FLAGS_FTRACE := -fpatchable-function-entry=2
22endif
23endif
24
25ifeq ($(CONFIG_CMODEL_MEDLOW),y)
26KBUILD_CFLAGS_MODULE += -mcmodel=medany
27endif
28
29export BITS
30ifeq ($(CONFIG_ARCH_RV64I),y)
31	BITS := 64
32	UTS_MACHINE := riscv64
33
34	KBUILD_CFLAGS += -mabi=lp64
35	KBUILD_AFLAGS += -mabi=lp64
36
37	KBUILD_LDFLAGS += -melf64lriscv
38else
39	BITS := 32
40	UTS_MACHINE := riscv32
41
42	KBUILD_CFLAGS += -mabi=ilp32
43	KBUILD_AFLAGS += -mabi=ilp32
44	KBUILD_LDFLAGS += -melf32lriscv
45endif
46
47ifeq ($(CONFIG_LD_IS_LLD),y)
48ifeq ($(call test-lt, $(CONFIG_LLD_VERSION), 150000),y)
49	KBUILD_CFLAGS += -mno-relax
50	KBUILD_AFLAGS += -mno-relax
51ifndef CONFIG_AS_IS_LLVM
52	KBUILD_CFLAGS += -Wa,-mno-relax
53	KBUILD_AFLAGS += -Wa,-mno-relax
54endif
55endif
56endif
57
58# ISA string setting
59riscv-march-$(CONFIG_ARCH_RV32I)	:= rv32ima
60riscv-march-$(CONFIG_ARCH_RV64I)	:= rv64ima
61riscv-march-$(CONFIG_FPU)		:= $(riscv-march-y)fd
62riscv-march-$(CONFIG_RISCV_ISA_C)	:= $(riscv-march-y)c
63riscv-march-$(CONFIG_RISCV_ISA_V)	:= $(riscv-march-y)v
64
65ifdef CONFIG_TOOLCHAIN_NEEDS_OLD_ISA_SPEC
66KBUILD_CFLAGS += -Wa,-misa-spec=2.2
67KBUILD_AFLAGS += -Wa,-misa-spec=2.2
68else
69riscv-march-$(CONFIG_TOOLCHAIN_NEEDS_EXPLICIT_ZICSR_ZIFENCEI) := $(riscv-march-y)_zicsr_zifencei
70endif
71
72# Check if the toolchain supports Zihintpause extension
73riscv-march-$(CONFIG_TOOLCHAIN_HAS_ZIHINTPAUSE) := $(riscv-march-y)_zihintpause
74
75# Remove F,D,V from isa string for all. Keep extensions between "fd" and "v" by
76# matching non-v and non-multi-letter extensions out with the filter ([^v_]*)
77KBUILD_CFLAGS += -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima|rv64ima)fd([^v_]*)v?/\1\2/')
78
79KBUILD_AFLAGS += -march=$(riscv-march-y)
80
81KBUILD_CFLAGS += -mno-save-restore
82KBUILD_CFLAGS += -DCONFIG_PAGE_OFFSET=$(CONFIG_PAGE_OFFSET)
83
84ifeq ($(CONFIG_CMODEL_MEDLOW),y)
85	KBUILD_CFLAGS += -mcmodel=medlow
86endif
87ifeq ($(CONFIG_CMODEL_MEDANY),y)
88	KBUILD_CFLAGS += -mcmodel=medany
89endif
90ifeq ($(CONFIG_PERF_EVENTS),y)
91        KBUILD_CFLAGS += -fno-omit-frame-pointer
92endif
93
94# Avoid generating .eh_frame sections.
95KBUILD_CFLAGS += -fno-asynchronous-unwind-tables -fno-unwind-tables
96
97# The RISC-V attributes frequently cause compatibility issues and provide no
98# information, so just turn them off.
99KBUILD_CFLAGS += $(call cc-option,-mno-riscv-attribute)
100KBUILD_AFLAGS += $(call cc-option,-mno-riscv-attribute)
101KBUILD_CFLAGS += $(call as-option,-Wa$(comma)-mno-arch-attr)
102KBUILD_AFLAGS += $(call as-option,-Wa$(comma)-mno-arch-attr)
103
104KBUILD_CFLAGS_MODULE += $(call cc-option,-mno-relax)
105KBUILD_AFLAGS_MODULE += $(call as-option,-Wa$(comma)-mno-relax)
106
107# GCC versions that support the "-mstrict-align" option default to allowing
108# unaligned accesses.  While unaligned accesses are explicitly allowed in the
109# RISC-V ISA, they're emulated by machine mode traps on all extant
110# architectures.  It's faster to have GCC emit only aligned accesses.
111KBUILD_CFLAGS += $(call cc-option,-mstrict-align)
112
113ifeq ($(CONFIG_STACKPROTECTOR_PER_TASK),y)
114prepare: stack_protector_prepare
115stack_protector_prepare: prepare0
116	$(eval KBUILD_CFLAGS += -mstack-protector-guard=tls		  \
117				-mstack-protector-guard-reg=tp		  \
118				-mstack-protector-guard-offset=$(shell	  \
119			awk '{if ($$2 == "TSK_STACK_CANARY") print $$3;}' \
120					include/generated/asm-offsets.h))
121endif
122
123# arch specific predefines for sparse
124CHECKFLAGS += -D__riscv -D__riscv_xlen=$(BITS)
125
126# Default target when executing plain make
127boot		:= arch/riscv/boot
128ifeq ($(CONFIG_XIP_KERNEL),y)
129KBUILD_IMAGE := $(boot)/xipImage
130else
131KBUILD_IMAGE	:= $(boot)/Image.gz
132endif
133
134libs-y += arch/riscv/lib/
135libs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a
136
137PHONY += vdso_install
138vdso_install:
139	$(Q)$(MAKE) $(build)=arch/riscv/kernel/vdso $@
140	$(if $(CONFIG_COMPAT),$(Q)$(MAKE) \
141		$(build)=arch/riscv/kernel/compat_vdso compat_$@)
142
143ifeq ($(KBUILD_EXTMOD),)
144ifeq ($(CONFIG_MMU),y)
145prepare: vdso_prepare
146vdso_prepare: prepare0
147	$(Q)$(MAKE) $(build)=arch/riscv/kernel/vdso include/generated/vdso-offsets.h
148	$(if $(CONFIG_COMPAT),$(Q)$(MAKE) \
149		$(build)=arch/riscv/kernel/compat_vdso include/generated/compat_vdso-offsets.h)
150
151endif
152endif
153
154ifneq ($(CONFIG_XIP_KERNEL),y)
155ifeq ($(CONFIG_RISCV_M_MODE)$(CONFIG_ARCH_CANAAN),yy)
156KBUILD_IMAGE := $(boot)/loader.bin
157else
158ifeq ($(CONFIG_EFI_ZBOOT),)
159KBUILD_IMAGE := $(boot)/Image.gz
160else
161KBUILD_IMAGE := $(boot)/vmlinuz.efi
162endif
163endif
164endif
165BOOT_TARGETS := Image Image.gz loader loader.bin xipImage vmlinuz.efi
166
167all:	$(notdir $(KBUILD_IMAGE))
168
169$(BOOT_TARGETS): vmlinux
170	$(Q)$(MAKE) $(build)=$(boot) $(boot)/$@
171	@$(kecho) '  Kernel: $(boot)/$@ is ready'
172
173Image.%: Image
174	$(Q)$(MAKE) $(build)=$(boot) $(boot)/$@
175
176install: KBUILD_IMAGE := $(boot)/Image
177zinstall: KBUILD_IMAGE := $(boot)/Image.gz
178install zinstall:
179	$(call cmd,install)
180
181PHONY += rv32_randconfig
182rv32_randconfig:
183	$(Q)$(MAKE) KCONFIG_ALLCONFIG=$(srctree)/arch/riscv/configs/32-bit.config \
184		-f $(srctree)/Makefile randconfig
185
186PHONY += rv64_randconfig
187rv64_randconfig:
188	$(Q)$(MAKE) KCONFIG_ALLCONFIG=$(srctree)/arch/riscv/configs/64-bit.config \
189		-f $(srctree)/Makefile randconfig
190
191PHONY += rv32_defconfig
192rv32_defconfig:
193	$(Q)$(MAKE) -f $(srctree)/Makefile defconfig 32-bit.config
194
195PHONY += rv32_nommu_virt_defconfig
196rv32_nommu_virt_defconfig:
197	$(Q)$(MAKE) -f $(srctree)/Makefile nommu_virt_defconfig 32-bit.config
198