1link-out-dir = $(out-dir)/core
2
3link-script-dummy = $(arch-dir)/kernel/link_dummy.ld
4link-script = $(if $(wildcard $(platform-dir)/kern.ld.S), \
5		$(platform-dir)/kern.ld.S, \
6		$(arch-dir)/kernel/kern.ld.S)
7link-script-pp = $(link-out-dir)/kern.ld
8link-script-dep = $(link-out-dir)/.kern.ld.d
9
10AWK	 = awk
11
12link-ldflags  = $(LDFLAGS)
13ifeq ($(CFG_CORE_ASLR),y)
14link-ldflags += -pie -Bsymbolic -z notext -z norelro $(ldflag-apply-dynamic-relocs)
15endif
16ifeq ($(CFG_CORE_BTI),y)
17# force-bti tells the linker to warn if some object files lack the .note.gnu.property
18# section with the BTI flag, and to turn on the BTI flag in the output anyway. The
19# resulting executable would likely fail at runtime so we use this flag along
20# with the --fatal-warnings below to check and prevent this situation (with useful
21# diagnostics).
22link-ldflags += $(call ld-option,-z force-bti) --fatal-warnings
23endif
24link-ldflags += -T $(link-script-pp) -Map=$(link-out-dir)/tee.map
25link-ldflags += --sort-section=alignment
26link-ldflags += --fatal-warnings
27link-ldflags += --gc-sections
28
29link-ldadd  = $(LDADD)
30link-ldadd += $(ldflags-external)
31link-ldadd += $(libdeps)
32link-objs := $(filter-out \
33	       $(out-dir)/$(platform-dir)/link_dummies_paged.o \
34	       $(out-dir)/$(platform-dir)/link_dummies_init.o \
35	       $(out-dir)/$(arch-dir)/kernel/link_dummies_paged.o \
36	       $(out-dir)/$(arch-dir)/kernel/link_dummies_init.o, \
37	       $(objs))
38link-objs-init := $(filter-out \
39		    $(out-dir)/$(platform-dir)/link_dummies_init.o \
40		    $(out-dir)/$(arch-dir)/kernel/link_dummies_init.o, \
41		    $(objs))
42ldargs-tee.elf := $(link-ldflags) $(link-objs) $(link-out-dir)/version.o \
43		  $(link-ldadd) $(libgcccore)
44
45link-script-cppflags := \
46	$(filter-out $(CPPFLAGS_REMOVE) $(cppflags-remove), \
47		$(nostdinccore) $(CPPFLAGS) \
48		$(addprefix -I,$(incdirscore) $(link-out-dir)) \
49		$(cppflagscore))
50
51ldargs-all_objs := -T $(link-script-dummy) --no-check-sections \
52		   $(link-objs) $(link-ldadd) $(libgcccore)
53cleanfiles += $(link-out-dir)/all_objs.o
54$(link-out-dir)/all_objs.o: $(objs) $(libdeps) $(MAKEFILE_LIST)
55	@$(cmd-echo-silent) '  LD      $@'
56	$(q)$(LDcore) $(ldargs-all_objs) -o $@
57
58cleanfiles += $(link-out-dir)/unpaged_entries.txt
59$(link-out-dir)/unpaged_entries.txt: $(link-out-dir)/all_objs.o
60	@$(cmd-echo-silent) '  GEN     $@'
61	$(q)$(NMcore) $< | \
62		$(AWK) '/ ____keep_pager/ { printf "-u%s ", $$3 }' > $@
63
64unpaged-ldargs = -T $(link-script-dummy) --no-check-sections --gc-sections
65unpaged-ldadd := $(objs) $(link-ldadd) $(libgcccore)
66cleanfiles += $(link-out-dir)/unpaged.o
67$(link-out-dir)/unpaged.o: $(link-out-dir)/unpaged_entries.txt
68	@$(cmd-echo-silent) '  LD      $@'
69	$(q)$(LDcore) $(unpaged-ldargs) \
70		`cat $(link-out-dir)/unpaged_entries.txt` \
71		$(unpaged-ldadd) -o $@
72
73cleanfiles += $(link-out-dir)/text_unpaged.ld.S
74$(link-out-dir)/text_unpaged.ld.S: $(link-out-dir)/unpaged.o
75	@$(cmd-echo-silent) '  GEN     $@'
76	$(q)$(READELFcore) -S -W $< | \
77		$(PYTHON3) ./scripts/gen_ld_sects.py .text. > $@
78
79cleanfiles += $(link-out-dir)/rodata_unpaged.ld.S
80$(link-out-dir)/rodata_unpaged.ld.S: $(link-out-dir)/unpaged.o
81	@$(cmd-echo-silent) '  GEN     $@'
82	$(q)$(READELFcore) -S -W $< | \
83		$(PYTHON3) ./scripts/gen_ld_sects.py .rodata. > $@
84
85
86cleanfiles += $(link-out-dir)/init_entries.txt
87$(link-out-dir)/init_entries.txt: $(link-out-dir)/all_objs.o
88	@$(cmd-echo-silent) '  GEN     $@'
89	$(q)$(NMcore) $< | \
90		$(AWK) '/ ____keep_init/ { printf "-u%s ", $$3 }' > $@
91
92init-ldargs := -T $(link-script-dummy) --no-check-sections --gc-sections
93init-ldadd := $(link-objs-init) $(link-out-dir)/version.o  $(link-ldadd) \
94	      $(libgcccore)
95cleanfiles += $(link-out-dir)/init.o
96$(link-out-dir)/init.o: $(link-out-dir)/init_entries.txt
97	$(call gen-version-o)
98	@$(cmd-echo-silent) '  LD      $@'
99	$(q)$(LDcore) $(init-ldargs) \
100		`cat $(link-out-dir)/init_entries.txt` \
101		$(init-ldadd) -o $@
102
103cleanfiles += $(link-out-dir)/text_init.ld.S
104$(link-out-dir)/text_init.ld.S: $(link-out-dir)/init.o
105	@$(cmd-echo-silent) '  GEN     $@'
106	$(q)$(READELFcore) -S -W $< | \
107		$(PYTHON3) ./scripts/gen_ld_sects.py .text. > $@
108
109cleanfiles += $(link-out-dir)/rodata_init.ld.S
110$(link-out-dir)/rodata_init.ld.S: $(link-out-dir)/init.o
111	@$(cmd-echo-silent) '  GEN     $@'
112	$(q)$(READELFcore) -S -W $< | $(PYTHON3) ./scripts/gen_ld_sects.py .rodata. > $@
113
114-include $(link-script-dep)
115
116link-script-extra-deps += $(link-out-dir)/text_unpaged.ld.S
117link-script-extra-deps += $(link-out-dir)/rodata_unpaged.ld.S
118link-script-extra-deps += $(link-out-dir)/text_init.ld.S
119link-script-extra-deps += $(link-out-dir)/rodata_init.ld.S
120link-script-extra-deps += $(conf-file)
121cleanfiles += $(link-script-pp) $(link-script-dep)
122$(link-script-pp): $(link-script) $(link-script-extra-deps)
123	@$(cmd-echo-silent) '  CPP     $@'
124	@mkdir -p $(dir $@)
125	$(q)$(CPPcore) -P -MT $@ -MD -MF $(link-script-dep) \
126		$(link-script-cppflags) $< -o $@
127
128define update-buildcount
129	@$(cmd-echo-silent) '  UPD     $(1)'
130	$(q)if [ ! -f $(1) ]; then \
131		mkdir -p $(dir $(1)); \
132		echo 1 >$(1); \
133	else \
134		expr 0`cat $(1)` + 1 >$(1); \
135	fi
136endef
137
138# filter-out to workaround objdump warning
139version-o-cflags = $(filter-out -g3,$(core-platform-cflags) \
140			$(platform-cflags) $(cflagscore))
141# SOURCE_DATE_EPOCH defined for reproducible builds
142ifneq ($(SOURCE_DATE_EPOCH),)
143date-opts = -d @$(SOURCE_DATE_EPOCH)
144endif
145DATE_STR = `LANG=C date -u $(date-opts)`
146BUILD_COUNT_STR = `cat $(link-out-dir)/.buildcount`
147CORE_CC_VERSION = `$(CCcore) -v 2>&1 | grep "version " | sed 's/ *$$//'`
148define gen-version-o
149	$(call update-buildcount,$(link-out-dir)/.buildcount)
150	@$(cmd-echo-silent) '  GEN     $(link-out-dir)/version.o'
151	$(q)echo -e "const char core_v_str[] =" \
152		"\"$(TEE_IMPL_VERSION) \"" \
153		"\"($(CORE_CC_VERSION)) \"" \
154		"\"#$(BUILD_COUNT_STR) \"" \
155		"\"$(DATE_STR) \"" \
156		"\"$(CFG_KERN_LINKER_ARCH)\";\n" \
157		| $(CCcore) $(version-o-cflags) \
158			-xc - -c -o $(link-out-dir)/version.o
159endef
160$(link-out-dir)/version.o:
161	$(call gen-version-o)
162
163-include $(link-out-dir)/.tee.elf.cmd
164define check-link-objs
165$(if $(strip $(filter-out $(link-objs), $(old-link-objs))
166	     $(filter-out $(old-link-objs), $(link-objs))), FORCE_LINK := FORCE)
167endef
168$(eval $(call check-link-objs))
169
170all: $(link-out-dir)/tee.elf
171cleanfiles += $(link-out-dir)/tee.elf $(link-out-dir)/tee.map
172cleanfiles += $(link-out-dir)/version.o
173cleanfiles += $(link-out-dir)/.buildcount
174cleanfiles += $(link-out-dir)/.tee.elf.cmd
175$(link-out-dir)/tee.elf: $(link-objs) $(libdeps) $(link-script-pp) $(FORCE_LINK)
176	@echo "old-link-objs := $(link-objs)" >$(link-out-dir)/.tee.elf.cmd
177	@$(cmd-echo-silent) '  LD      $@'
178	$(q)$(LDcore) $(ldargs-tee.elf) -o $@
179
180all: $(link-out-dir)/tee.dmp
181cleanfiles += $(link-out-dir)/tee.dmp
182$(link-out-dir)/tee.dmp: $(link-out-dir)/tee.elf
183	@$(cmd-echo-silent) '  OBJDUMP $@'
184	$(q)$(OBJDUMPcore) -l -x -d $< > $@
185
186cleanfiles += $(link-out-dir)/tee-pager.bin
187$(link-out-dir)/tee-pager.bin: $(link-out-dir)/tee.elf scripts/gen_tee_bin.py
188	@echo Warning: $@ is deprecated
189	@$(cmd-echo-silent) '  GEN     $@'
190	$(q)$(PYTHON3) scripts/gen_tee_bin.py --input $< --out_tee_pager_bin $@
191
192cleanfiles += $(link-out-dir)/tee-pageable.bin
193$(link-out-dir)/tee-pageable.bin: $(link-out-dir)/tee.elf scripts/gen_tee_bin.py
194	@echo Warning: $@ is deprecated
195	@$(cmd-echo-silent) '  GEN     $@'
196	$(q)$(PYTHON3) scripts/gen_tee_bin.py --input $< --out_tee_pageable_bin $@
197
198all: $(link-out-dir)/tee.bin
199cleanfiles += $(link-out-dir)/tee.bin
200$(link-out-dir)/tee.bin: $(link-out-dir)/tee.elf scripts/gen_tee_bin.py
201	@$(cmd-echo-silent) '  GEN     $@'
202	$(q)$(PYTHON3) scripts/gen_tee_bin.py --input $< --out_tee_bin $@
203
204all: $(link-out-dir)/tee-header_v2.bin
205cleanfiles += $(link-out-dir)/tee-header_v2.bin
206$(link-out-dir)/tee-header_v2.bin: $(link-out-dir)/tee.elf \
207				   scripts/gen_tee_bin.py
208	@$(cmd-echo-silent) '  GEN     $@'
209	$(q)$(PYTHON3) scripts/gen_tee_bin.py --input $< --out_header_v2 $@
210
211all: $(link-out-dir)/tee-pager_v2.bin
212cleanfiles += $(link-out-dir)/tee-pager_v2.bin
213$(link-out-dir)/tee-pager_v2.bin: $(link-out-dir)/tee.elf scripts/gen_tee_bin.py
214	@$(cmd-echo-silent) '  GEN     $@'
215	$(q)$(PYTHON3) scripts/gen_tee_bin.py --input $< --out_pager_v2 $@
216
217all: $(link-out-dir)/tee-pageable_v2.bin
218cleanfiles += $(link-out-dir)/tee-pageable_v2.bin
219$(link-out-dir)/tee-pageable_v2.bin: $(link-out-dir)/tee.elf \
220				     scripts/gen_tee_bin.py
221	@$(cmd-echo-silent) '  GEN     $@'
222	$(q)$(PYTHON3) scripts/gen_tee_bin.py --input $< --out_pageable_v2 $@
223
224all: $(link-out-dir)/tee.symb_sizes
225cleanfiles += $(link-out-dir)/tee.symb_sizes
226$(link-out-dir)/tee.symb_sizes: $(link-out-dir)/tee.elf
227	@$(cmd-echo-silent) '  GEN     $@'
228	$(q)$(NMcore) --print-size --reverse-sort --size-sort $< > $@
229
230cleanfiles += $(link-out-dir)/tee.mem_usage
231ifneq ($(filter mem_usage,$(MAKECMDGOALS)),)
232mem_usage: $(link-out-dir)/tee.mem_usage
233
234$(link-out-dir)/tee.mem_usage: $(link-out-dir)/tee.elf
235	@$(cmd-echo-silent) '  GEN     $@'
236	$(q)$(PYTHON3) ./scripts/mem_usage.py $< > $@
237endif
238
239cleanfiles += $(link-out-dir)/tee-raw.bin
240$(link-out-dir)/tee-raw.bin: $(link-out-dir)/tee.elf scripts/gen_tee_bin.py
241	@$(cmd-echo-silent) '  GEN     $@'
242	$(q)scripts/gen_tee_bin.py --input $< --out_tee_raw_bin $@
243
244cleanfiles += $(link-out-dir)/tee.srec
245$(link-out-dir)/tee.srec: $(link-out-dir)/tee-raw.bin
246	@$(cmd-echo-silent) '  SREC    $@'
247	$(q)$(OBJCOPYcore) -I binary -O srec $(SRECFLAGS) $< $@
248