1# SPDX-License-Identifier: GPL-2.0 2# 3# Author: Huacai Chen <chenhuacai@loongson.cn> 4# Copyright (C) 2020-2022 Loongson Technology Corporation Limited 5 6boot := arch/loongarch/boot 7 8ifeq ($(shell uname -m),loongarch32) 9KBUILD_DEFCONFIG := loongson32_defconfig 10else 11KBUILD_DEFCONFIG := loongson64_defconfig 12endif 13 14KBUILD_DTBS := dtbs 15 16image-name-y := vmlinux 17image-name-$(CONFIG_EFI_ZBOOT) := vmlinuz 18 19ifndef CONFIG_EFI_STUB 20KBUILD_IMAGE := $(boot)/vmlinux.elf 21else 22KBUILD_IMAGE := $(boot)/$(image-name-y).efi 23endif 24 25# 26# Select the object file format to substitute into the linker script. 27# 2864bit-tool-archpref = loongarch64 2932bit-bfd = elf32-loongarch 3064bit-bfd = elf64-loongarch 3132bit-emul = elf32loongarch 3264bit-emul = elf64loongarch 33 34CC_FLAGS_FPU := -mfpu=64 35CC_FLAGS_NO_FPU := -msoft-float 36 37ifdef CONFIG_UNWINDER_ORC 38orc_hash_h := arch/$(SRCARCH)/include/generated/asm/orc_hash.h 39orc_hash_sh := $(srctree)/scripts/orc_hash.sh 40targets += $(orc_hash_h) 41quiet_cmd_orc_hash = GEN $@ 42 cmd_orc_hash = mkdir -p $(dir $@); \ 43 $(CONFIG_SHELL) $(orc_hash_sh) < $< > $@ 44$(orc_hash_h): $(srctree)/arch/loongarch/include/asm/orc_types.h $(orc_hash_sh) FORCE 45 $(call if_changed,orc_hash) 46archprepare: $(orc_hash_h) 47endif 48 49ifdef CONFIG_DYNAMIC_FTRACE 50KBUILD_CPPFLAGS += -DCC_USING_PATCHABLE_FUNCTION_ENTRY 51CC_FLAGS_FTRACE := -fpatchable-function-entry=2 52endif 53 54ifdef CONFIG_64BIT 55tool-archpref = $(64bit-tool-archpref) 56UTS_MACHINE := loongarch64 57endif 58 59ifneq ($(SUBARCH),$(ARCH)) 60 ifeq ($(CROSS_COMPILE),) 61 CROSS_COMPILE := $(call cc-cross-prefix, $(tool-archpref)-linux- $(tool-archpref)-linux-gnu- $(tool-archpref)-unknown-linux-gnu-) 62 endif 63endif 64 65ifdef CONFIG_64BIT 66ld-emul = $(64bit-emul) 67cflags-y += -mabi=lp64s -mcmodel=normal 68endif 69 70cflags-y += -pipe $(CC_FLAGS_NO_FPU) 71LDFLAGS_vmlinux += -static -n -nostdlib 72 73# When the assembler supports explicit relocation hint, we must use it. 74# GCC may have -mexplicit-relocs off by default if it was built with an old 75# assembler, so we force it via an option. 76# 77# When the assembler does not supports explicit relocation hint, we can't use 78# it. Disable it if the compiler supports it. 79# 80# The combination of a "new" assembler and "old" GCC is not supported, given 81# the rarity of this combo and the extra complexity needed to make it work. 82# Either upgrade the compiler or downgrade the assembler; the build will error 83# out if it is the case (by probing for the model attribute; all supported 84# compilers in this case would have support). 85# 86# Also, -mdirect-extern-access is useful in case of building with explicit 87# relocs, for avoiding unnecessary GOT accesses. It is harmless to not have 88# support though. 89ifdef CONFIG_AS_HAS_EXPLICIT_RELOCS 90cflags-y += $(call cc-option,-mexplicit-relocs) 91KBUILD_CFLAGS_KERNEL += $(call cc-option,-mdirect-extern-access) 92KBUILD_CFLAGS_KERNEL += $(call cc-option,-fdirect-access-external-data) 93KBUILD_AFLAGS_MODULE += $(call cc-option,-fno-direct-access-external-data) 94KBUILD_CFLAGS_MODULE += $(call cc-option,-fno-direct-access-external-data) 95else 96cflags-y += $(call cc-option,-mno-explicit-relocs) 97KBUILD_AFLAGS_KERNEL += -Wa,-mla-global-with-pcrel 98KBUILD_CFLAGS_KERNEL += -Wa,-mla-global-with-pcrel 99KBUILD_AFLAGS_MODULE += -Wa,-mla-global-with-abs 100KBUILD_CFLAGS_MODULE += -fplt -Wa,-mla-global-with-abs,-mla-local-with-abs 101endif 102 103KBUILD_AFLAGS += $(call cc-option,-mno-relax) $(call cc-option,-Wa$(comma)-mno-relax) 104KBUILD_CFLAGS += $(call cc-option,-mno-relax) $(call cc-option,-Wa$(comma)-mno-relax) 105KBUILD_AFLAGS += $(call cc-option,-mthin-add-sub) $(call cc-option,-Wa$(comma)-mthin-add-sub) 106KBUILD_CFLAGS += $(call cc-option,-mthin-add-sub) $(call cc-option,-Wa$(comma)-mthin-add-sub) 107 108ifdef CONFIG_OBJTOOL 109ifdef CONFIG_CC_HAS_ANNOTATE_TABLEJUMP 110KBUILD_CFLAGS += -mannotate-tablejump 111else 112KBUILD_CFLAGS += -fno-jump-tables # keep compatibility with older compilers 113endif 114ifdef CONFIG_RUSTC_HAS_ANNOTATE_TABLEJUMP 115KBUILD_RUSTFLAGS += -Cllvm-args=--loongarch-annotate-tablejump 116else 117KBUILD_RUSTFLAGS += $(if $(call rustc-min-version,109300),-Cjump-tables=n,-Zno-jump-tables) # keep compatibility with older compilers 118endif 119ifdef CONFIG_LTO_CLANG 120# The annotate-tablejump option can not be passed to LLVM backend when LTO is enabled. 121# Ensure it is aware of linker with LTO, '--loongarch-annotate-tablejump' also needs to 122# be passed via '-mllvm' to ld.lld. 123KBUILD_LDFLAGS += $(call ld-option,-mllvm --loongarch-annotate-tablejump) 124endif 125endif 126 127KBUILD_RUSTFLAGS += --target=loongarch64-unknown-none-softfloat -Ccode-model=small 128KBUILD_RUSTFLAGS_KERNEL += -Zdirect-access-external-data=yes 129KBUILD_RUSTFLAGS_MODULE += -Zdirect-access-external-data=no 130 131ifeq ($(CONFIG_RELOCATABLE),y) 132KBUILD_CFLAGS_KERNEL += -fPIE 133KBUILD_RUSTFLAGS_KERNEL += -Crelocation-model=pie 134LDFLAGS_vmlinux += -static -pie --no-dynamic-linker -z notext $(call ld-option, --apply-dynamic-relocs) 135endif 136 137cflags-y += $(call cc-option, -mno-check-zero-division -fno-isolate-erroneous-paths-dereference) 138 139ifndef CONFIG_KASAN 140cflags-y += -fno-builtin-memcpy -fno-builtin-memmove -fno-builtin-memset 141endif 142 143load-y = 0x9000000000200000 144bootvars-y = VMLINUX_LOAD_ADDRESS=$(load-y) 145 146drivers-$(CONFIG_PCI) += arch/loongarch/pci/ 147 148KBUILD_AFLAGS += $(cflags-y) 149KBUILD_CFLAGS += $(cflags-y) 150KBUILD_CPPFLAGS += -DVMLINUX_LOAD_ADDRESS=$(load-y) 151 152# This is required to get dwarf unwinding tables into .debug_frame 153# instead of .eh_frame so we don't discard them. 154KBUILD_CFLAGS += -fno-asynchronous-unwind-tables 155 156ifdef CONFIG_ARCH_STRICT_ALIGN 157# Don't emit unaligned accesses. 158# Not all LoongArch cores support unaligned access, and as kernel we can't 159# rely on others to provide emulation for these accesses. 160KBUILD_CFLAGS += $(call cc-option,-mstrict-align) 161else 162# Optimise for performance on hardware supports unaligned access. 163KBUILD_CFLAGS += $(call cc-option,-mno-strict-align) 164endif 165 166KBUILD_CFLAGS += -isystem $(shell $(CC) -print-file-name=include) 167 168KBUILD_LDFLAGS += -m $(ld-emul) 169 170ifdef need-compiler 171CHECKFLAGS += $(shell $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) -dM -E -x c /dev/null | \ 172 grep -E -vw '__GNUC_(MINOR_|PATCHLEVEL_)?_' | \ 173 sed -e "s/^\#define /-D'/" -e "s/ /'='/" -e "s/$$/'/" -e 's/\$$/&&/g') 174endif 175 176libs-y += arch/loongarch/lib/ 177libs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a 178 179drivers-y += arch/loongarch/crypto/ 180 181# suspend and hibernation support 182drivers-$(CONFIG_PM) += arch/loongarch/power/ 183 184ifeq ($(KBUILD_EXTMOD),) 185prepare: vdso_prepare 186vdso_prepare: prepare0 187 $(Q)$(MAKE) $(build)=arch/loongarch/vdso include/generated/vdso-offsets.h 188endif 189 190vdso-install-y += arch/loongarch/vdso/vdso.so.dbg 191 192all: $(notdir $(KBUILD_IMAGE)) $(KBUILD_DTBS) 193 194vmlinuz.efi: vmlinux.efi 195 196vmlinux.elf vmlinux.efi vmlinuz.efi: vmlinux 197 $(Q)$(MAKE) $(build)=$(boot) $(bootvars-y) $(boot)/$@ 198 199install: 200 $(call cmd,install) 201 202define archhelp 203 echo ' vmlinux.elf - Uncompressed ELF kernel image (arch/loongarch/boot/vmlinux.elf)' 204 echo ' vmlinux.efi - Uncompressed EFI kernel image (arch/loongarch/boot/vmlinux.efi)' 205 echo ' vmlinuz.efi - GZIP/ZSTD-compressed EFI kernel image (arch/loongarch/boot/vmlinuz.efi)' 206 echo ' Default when CONFIG_EFI_ZBOOT=y' 207 echo ' install - Install kernel using (your) ~/bin/$(INSTALLKERNEL) or' 208 echo ' (distribution) /sbin/$(INSTALLKERNEL) or install.sh to $$(INSTALL_PATH)' 209 echo 210endef 211