Makefile 7.2 KB

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