1link-out-dir = $(out-dir)/core
2
3link-script = $(if $(wildcard $(platform-dir)/kern.ld.S), \
4		$(platform-dir)/kern.ld.S, \
5		$(arch-dir)/kernel/kern.ld.S)
6link-script-pp = $(link-out-dir)/kern.ld
7link-script-dep = $(link-out-dir)/.kern.ld.d
8
9link-ldflags-common += $(call ld-option,--no-warn-rwx-segments)
10
11link-ldflags  = $(LDFLAGS)
12ifeq ($(CFG_CORE_ASLR),y)
13link-ldflags += -pie -Bsymbolic -z norelro $(ldflag-apply-dynamic-relocs)
14endif
15
16link-ldflags += -T $(link-script-pp) -Map=$(link-out-dir)/tee.map
17link-ldflags += --sort-section=alignment
18link-ldflags += --fatal-warnings
19link-ldflags += --gc-sections
20link-ldflags += $(link-ldflags-common)
21
22link-ldadd  = $(LDADD)
23link-ldadd += $(ldflags-external)
24link-ldadd += $(libdeps)
25link-objs := $(objs)
26
27ldargs-tee.elf := $(link-ldflags) $(link-objs) $(link-out-dir)/version.o \
28		  $(link-ldadd) $(libgcccore)
29
30link-script-cppflags := \
31	$(filter-out $(CPPFLAGS_REMOVE) $(cppflags-remove), \
32		$(nostdinccore) $(CPPFLAGS) \
33		$(addprefix -I,$(incdirscore) $(link-out-dir)) \
34		$(cppflagscore))
35
36ldargs-all_objs := -T $(link-script) --no-check-sections \
37		   $(link-ldflags-common) \
38		   $(link-objs) $(link-ldadd) $(libgcccore)
39cleanfiles += $(link-out-dir)/all_objs.o
40$(link-out-dir)/all_objs.o: $(objs) $(libdeps) $(MAKEFILE_LIST)
41	@$(cmd-echo-silent) '  LD      $@'
42	$(q)$(LDcore) $(ldargs-all_objs) -o $@
43
44-include $(link-script-dep)
45
46link-script-extra-deps += $(conf-file)
47cleanfiles += $(link-script-pp) $(link-script-dep)
48$(link-script-pp): $(link-script) $(link-script-extra-deps)
49	@$(cmd-echo-silent) '  CPP     $@'
50	@mkdir -p $(dir $@)
51	$(q)$(CPPcore) -P -MT $@ -MD -MF $(link-script-dep) \
52		$(link-script-cppflags) $< -o $@
53
54define update-buildcount
55	@$(cmd-echo-silent) '  UPD     $(1)'
56	$(q)if [ ! -f $(1) ]; then \
57		mkdir -p $(dir $(1)); \
58		echo 1 >$(1); \
59	else \
60		expr 0`cat $(1)` + 1 >$(1); \
61	fi
62endef
63
64# filter-out to workaround objdump warning
65version-o-cflags = $(filter-out -g3,$(core-platform-cflags) \
66			$(platform-cflags) $(cflagscore))
67# SOURCE_DATE_EPOCH defined for reproducible builds
68ifneq ($(SOURCE_DATE_EPOCH),)
69date-opts = -d @$(SOURCE_DATE_EPOCH)
70endif
71DATE_STR = `LC_ALL=C date -u $(date-opts)`
72BUILD_COUNT_STR = `cat $(link-out-dir)/.buildcount`
73CORE_CC_VERSION = `$(CCcore) -v 2>&1 | grep "version " | sed 's/ *$$//'`
74define gen-version-o
75	$(call update-buildcount,$(link-out-dir)/.buildcount)
76	@$(cmd-echo-silent) '  GEN     $(link-out-dir)/version.o'
77	$(q)echo -e "const char core_v_str[] =" \
78		"\"$(TEE_IMPL_VERSION) \"" \
79		"\"($(CORE_CC_VERSION)) \"" \
80		"\"#$(BUILD_COUNT_STR) \"" \
81		"\"$(DATE_STR) \"" \
82		"\"$(CFG_KERN_LINKER_ARCH)\";\n" \
83		| $(CCcore) $(version-o-cflags) \
84			-xc - -c -o $(link-out-dir)/version.o
85endef
86
87$(link-out-dir)/version.o:
88	$(call gen-version-o)
89
90-include $(link-out-dir)/.tee.elf.cmd
91define check-link-objs
92$(if $(strip $(filter-out $(link-objs), $(old-link-objs))
93	     $(filter-out $(old-link-objs), $(link-objs))), FORCE_LINK := FORCE)
94endef
95#$(eval $(call check-link-objs))
96
97all: $(link-out-dir)/tee.elf
98cleanfiles += $(link-out-dir)/tee.elf $(link-out-dir)/tee.map
99cleanfiles += $(link-out-dir)/version.o
100cleanfiles += $(link-out-dir)/.buildcount
101cleanfiles += $(link-out-dir)/.tee.elf.cmd
102$(link-out-dir)/tee.elf: $(link-objs) $(libdeps) $(link-script-pp) $(FORCE_LINK)
103	$(call gen-version-o)
104	@echo "old-link-objs := $(link-objs)" >$(link-out-dir)/.tee.elf.cmd
105	@$(cmd-echo-silent) '  LD      $@'
106	$(q)$(LDcore) $(ldargs-tee.elf) -o $@
107
108all: $(link-out-dir)/tee.dmp
109cleanfiles += $(link-out-dir)/tee.dmp
110$(link-out-dir)/tee.dmp: $(link-out-dir)/tee.elf
111	@$(cmd-echo-silent) '  OBJDUMP $@'
112	$(q)$(OBJDUMPcore) -l -x -d $< > $@
113
114all: $(link-out-dir)/tee.bin
115cleanfiles += $(link-out-dir)/tee.bin
116$(link-out-dir)/tee.bin: $(link-out-dir)/tee.elf
117	@$(cmd-echo-silent) '  GEN     $@'
118	$(q)$(OBJCOPYcore) -O binary $< $@
119
120all: $(link-out-dir)/tee.symb_sizes
121cleanfiles += $(link-out-dir)/tee.symb_sizes
122$(link-out-dir)/tee.symb_sizes: $(link-out-dir)/tee.elf
123	@$(cmd-echo-silent) '  GEN     $@'
124	$(q)$(NMcore) --print-size --reverse-sort --size-sort $< > $@
125
126cleanfiles += $(link-out-dir)/tee.mem_usage
127ifneq ($(filter mem_usage,$(MAKECMDGOALS)),)
128mem_usage: $(link-out-dir)/tee.mem_usage
129
130$(link-out-dir)/tee.mem_usage: $(link-out-dir)/tee.elf
131	@$(cmd-echo-silent) '  GEN     $@'
132	$(q)$(PYTHON3) ./scripts/mem_usage.py $< > $@
133endif
134
135cleanfiles += $(link-out-dir)/tee-raw.bin
136$(link-out-dir)/tee-raw.bin: $(link-out-dir)/tee.elf
137	@$(cmd-echo-silent) '  GEN     $@'
138	$(q)$(OBJCOPYcore) -O binary $< $@
139
140cleanfiles += $(link-out-dir)/tee.srec
141$(link-out-dir)/tee.srec: $(link-out-dir)/tee-raw.bin
142	@$(cmd-echo-silent) '  SREC    $@'
143	$(q)$(OBJCOPYcore) -I binary -O srec $(SRECFLAGS) $< $@
144