#
# linux/arch/arm/boot/compressed/Makefile
#
# create a compressed vmlinuz image from the original vmlinux
#

OBJS		=
KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_SCS), $(KBUILD_CFLAGS))
ifeq ($(CONFIG_RTOS_PAC_CFI_KERNEL),y)
KBUILD_CFLAGS := $(filter-out -mllvm -FECFI			\
							-mllvm -FGBECFI			\
							-fpass-plugin=% -mllvm -PAC_OPT=% \
							-mllvm -PAC_COMBINED_INST=%, \
							$(KBUILD_CFLAGS))
endif

SP_SYSREG_CFLAGS = -mstack-protector-guard=sysreg		  \
				-mstack-protector-guard-reg=sp_el0	  \
				-mstack-protector-guard-offset=$(shell	  \
			awk '{if ($$2 == "TSK_STACK_CANARY") print $$3;}' \
					include/generated/asm-offsets.h)
KBUILD_CFLAGS := $(filter-out $(SP_SYSREG_CFLAGS), $(KBUILD_CFLAGS))

HEAD	= head.o
OBJS	+= misc.o decompress.o base.o
ifeq ($(CONFIG_RTOS_DROL),y)
OBJS	+= drol.o
endif

ifeq ($(CONFIG_HISI_WATCHDOG),y)
OBJS	+= hwdog.o
endif

ifeq ($(CONFIG_DEBUG_UNCOMPRESS),y)
OBJS	+= debug.o
endif
FONTC	= $(srctree)/lib/fonts/font_acorn_8x8.c

# string library code (-Os is enforced to keep it much smaller)
OBJS		+= string.o
CFLAGS_string.o	:= -Os

OBJS		+= hyp-stub.o

GCOV_PROFILE		:= n
KASAN_SANITIZE		:= n
UBSAN_SANITIZE		:= n
KCOV_INSTRUMENT		:= n

ifeq ($(CONFIG_RTOS_HAL_SELFDECOMPRESS_ACPI),y)
extra-y		:= $(obj)/efi-entry.o  $(objtree)/drivers/firmware/efi/libstub/lib.a
OBJS 		+= efi.o
OBJCOPYFLAGS 	:= --prefix-symbols=__efistub_
$(obj)/efi-entry.stub.o: $(obj)/efi-entry.o $(objtree)/drivers/firmware/efi/libstub/lib.a FORCE
	$(call if_changed,objcopy)

HEAD    	+= $(objtree)/arch/arm64/boot/compressed/efi-entry.stub.o $(objtree)/drivers/firmware/efi/libstub/lib.a
endif
#
# Architecture dependencies
#
ifeq ($(CONFIG_ARCH_ACORN),y)
OBJS		+= ll_char_wr.o font.o
endif

ifeq ($(CONFIG_ARCH_SA1100),y)
OBJS		+= head-sa1100.o
endif

ifeq ($(CONFIG_CPU_XSCALE),y)
OBJS		+= head-xscale.o
endif

ifeq ($(CONFIG_PXA_SHARPSL_DETECT_MACH_ID),y)
OBJS		+= head-sharpsl.o
endif

ifeq ($(CONFIG_CPU_ENDIAN_BE32),y)
ifeq ($(CONFIG_CPU_CP15),y)
OBJS		+= big-endian.o
else
# The endian should be set by h/w design.
endif
endif

ifeq ($(CONFIG_ARCH_SHMOBILE_LEGACY),y)
OBJS		+= head-shmobile.o
endif

#
# We now have a PIC decompressor implementation.  Decompressors running
# from RAM should not define ZTEXTADDR.  Decompressors running directly
# from ROM or Flash must define ZTEXTADDR (preferably via the config)
# FIXME: Previous assignment to ztextaddr-y is lost here. See SHARK
ifeq ($(CONFIG_ZBOOT_ROM),y)
ZTEXTADDR	:= $(CONFIG_ZBOOT_ROM_TEXT)
ZBSSADDR	:= $(CONFIG_ZBOOT_ROM_BSS)
else
ZTEXTADDR	:= 0
ZBSSADDR	:= ALIGN(8)
endif

CPPFLAGS_vmlinux.lds := -DTEXT_START="$(ZTEXTADDR)" -DBSS_START="$(ZBSSADDR)"

suffix_$(CONFIG_KERNEL_GZIP) = gzip
suffix_$(CONFIG_KERNEL_LZO)  = lzo
suffix_$(CONFIG_KERNEL_LZMA) = lzma
suffix_$(CONFIG_KERNEL_XZ)   = xzkern
suffix_$(CONFIG_KERNEL_LZ4)  = lz4
# Borrowed libfdt files for the ATAG compatibility mode

libfdt		:= fdt_rw.c fdt_ro.c fdt_wip.c fdt.c
libfdt_hdrs	:= fdt.h libfdt.h libfdt_internal.h
libfdt_env_h	:= libfdt_env.h

libfdt_objs	:= $(addsuffix .o, $(basename $(libfdt)))

$(addprefix $(obj)/,$(libfdt) $(libfdt_hdrs)): $(obj)/%: $(srctree)/scripts/dtc/libfdt/%
	$(call cmd,shipped)
$(addprefix $(obj)/,$(libfdt_env_h)): $(obj)/%: $(srctree)/include/linux/%
	$(call cmd,shipped)

$(addprefix $(obj)/,$(libfdt_objs) base.o misc.o): \
	$(addprefix $(obj)/,$(libfdt_hdrs) $(libfdt_env_h))

OBJS	+= $(libfdt_objs) base.o
$(addprefix $(obj)/, $(OBJS)): $(addprefix $(obj)/,$(libfdt) $(libfdt_hdrs) $(libfdt_env_h))

targets       := vmlinux vmlinux.lds \
		 piggy.$(suffix_y) piggy.$(suffix_y).o \
		 head.o misc.o $(OBJS)

# Make sure files are removed during clean
extra-y       += piggy.gzip piggy.lzo piggy.lzma piggy.xzkern piggy.lz4 \
		 $(libfdt) $(libfdt_hdrs) $(libfdt_env_h)\
		 hyp-stub.S

ifeq ($(CONFIG_FUNCTION_TRACER),y)
ORIG_CFLAGS := $(KBUILD_CFLAGS)
KBUILD_CFLAGS = $(subst -pg, , $(ORIG_CFLAGS))
endif

ccflags-y := -fpic  -fno-builtin -I$(obj)
asflags-y := -DZIMAGE

# Supply kernel BSS size to the decompressor via a linker symbol.
KBSS_SZ = $(shell $(CROSS_COMPILE)size $(obj)/../../../../vmlinux | \
		awk 'END{print $$3}')
# Supply ZRELADDR to the decompressor via a linker symbol.
LDFLAGS_vmlinux = --defsym _kernel_bss_size=$(KBSS_SZ)
ifneq ($(CONFIG_AUTO_ZRELADDR),y)
LDFLAGS_vmlinux += --defsym zreladdr=0x80000
endif
ifeq ($(CONFIG_CPU_ENDIAN_BE8),y)
LDFLAGS_vmlinux += --be8
endif
# ?
ifeq ($(CONFIG_CC_IS_GCC), y)
LDFLAGS_vmlinux += -p
endif
# Report unresolved symbol references
LDFLAGS_vmlinux += --no-undefined
# Delete all temporary local symbols
LDFLAGS_vmlinux += -X
# Next argument is a linker script
LDFLAGS_vmlinux += -T

# For __aeabi_uidivmod


# For __aeabi_llsl


# For __bswapsi2, __bswapdi2

$(obj)/bswapsdi2.S: $(srctree)/arch/$(SRCARCH)/lib/bswapsdi2.S

# We need to prevent any GOTOFF relocs being used with references
# to symbols in the .bss section since we cannot relocate them
# independently from the rest at run time.  This can be achieved by
# ensuring that no private .bss symbols exist, as global symbols
# always have a GOT entry which is what we need.
# The .data section is already discarded by the linker script so no need
# to bother about it here.
check_for_bad_syms = \
bad_syms=$$($(CROSS_COMPILE)nm $@ | sed -n 's/^.\{8\} [bc] \(.*\)/\1/p') && \
[ -z "$$bad_syms" ] || \
  ( echo "following symbols must have non local/private scope:" >&2; \
    echo "$$bad_syms" >&2; rm -f $@; false )

check_for_multiple_zreladdr = \
if [ $(words $(ZRELADDR)) -gt 1 -a "$(CONFIG_AUTO_ZRELADDR)" = "" ]; then \
	echo 'multiple zreladdrs: $(ZRELADDR)'; \
	echo 'This needs CONFIG_AUTO_ZRELADDR to be set'; \
	false; \
fi
$(obj)/vmlinux: $(obj)/vmlinux.lds $(obj)/$(HEAD) $(obj)/piggy.$(suffix_y).o \
		$(addprefix $(obj)/, $(OBJS)) $(lib1funcs) $(ashldi3) \
		$(bswapsdi2) FORCE
	@$(check_for_multiple_zreladdr)
	$(call if_changed,ld)
	@$(check_for_bad_syms)

$(obj)/piggy.$(suffix_y): $(obj)/../Image FORCE
	$(call if_changed,$(suffix_y))

$(obj)/piggy.$(suffix_y).o:  $(obj)/piggy.$(suffix_y) FORCE

CFLAGS_font.o := -Dstatic=

$(obj)/font.c: $(FONTC)
	$(call cmd,shipped)
