1link-script$(sm) = $(ta-dev-kit-dir$(sm))/src/ta.ld.S
2link-script-pp$(sm) = $(link-out-dir$(sm))/ta.lds
3link-script-dep$(sm) = $(link-out-dir$(sm))/.ta.ld.d
4
5SIGN_ENC ?= $(PYTHON3) $(ta-dev-kit-dir$(sm))/scripts/sign_encrypt.py
6TA_SIGN_KEY ?= $(ta-dev-kit-dir$(sm))/keys/default_ta.pem
7
8ifeq ($(CFG_ENCRYPT_TA),y)
9# Default TA encryption key is a dummy key derived from default
10# hardware unique key (an array of 16 zero bytes) to demonstrate
11# usage of REE-FS TAs encryption feature.
12#
13# Note that a user of this TA encryption feature needs to provide
14# encryption key and its handling corresponding to their security
15# requirements.
16TA_ENC_KEY ?= 'b64d239b1f3c7d3b06506229cd8ff7c8af2bb4db2168621ac62c84948468c4f4'
17endif
18
19all: $(link-out-dir$(sm))/$(user-ta-uuid).dmp \
20	$(link-out-dir$(sm))/$(user-ta-uuid).stripped.elf \
21	$(link-out-dir$(sm))/$(user-ta-uuid).ta
22cleanfiles += $(link-out-dir$(sm))/$(user-ta-uuid).elf
23cleanfiles += $(link-out-dir$(sm))/$(user-ta-uuid).dmp
24cleanfiles += $(link-out-dir$(sm))/$(user-ta-uuid).map
25cleanfiles += $(link-out-dir$(sm))/$(user-ta-uuid).stripped.elf
26cleanfiles += $(link-out-dir$(sm))/$(user-ta-uuid).ta
27cleanfiles += $(link-script-pp$(sm)) $(link-script-dep$(sm))
28
29link-ldflags  = -e__ta_entry -pie
30link-ldflags += -T $(link-script-pp$(sm))
31link-ldflags += -Map=$(link-out-dir$(sm))/$(user-ta-uuid).map
32link-ldflags += --sort-section=alignment
33link-ldflags += -z max-page-size=4096 # OP-TEE always uses 4K alignment
34ifeq ($(sm)-$(CFG_TA_BTI),ta_arm64-y)
35link-ldflags += $(call ld-option,-z force-bti) --fatal-warnings
36endif
37link-ldflags += --as-needed # Do not add dependency on unused shlib
38link-ldflags += $(link-ldflags$(sm))
39
40$(link-out-dir$(sm))/dyn_list:
41	@$(cmd-echo-silent) '  GEN     $@'
42	$(q)mkdir -p $(dir $@)
43	$(q)echo "{" >$@
44	$(q)echo "__elf_phdr_info;" >>$@
45ifeq ($(CFG_FTRACE_SUPPORT),y)
46	$(q)echo "__ftrace_info;" >>$@
47endif
48	$(q)echo "trace_ext_prefix;" >>$@
49	$(q)echo "trace_level;" >>$@
50	$(q)echo "};" >>$@
51link-ldflags += --dynamic-list $(link-out-dir$(sm))/dyn_list
52dynlistdep = $(link-out-dir$(sm))/dyn_list
53cleanfiles += $(link-out-dir$(sm))/dyn_list
54
55link-ldadd  = $(user-ta-ldadd) $(addprefix -L,$(libdirs))
56link-ldadd += --start-group
57link-ldadd += $(addprefix -l,$(libnames))
58ifneq (,$(filter %.cpp,$(srcs)))
59link-ldflags += --eh-frame-hdr
60link-ldadd += $(libstdc++$(sm)) $(libgcc_eh$(sm))
61endif
62link-ldadd += --end-group
63
64link-ldadd-after-libgcc += $(addprefix -l,$(libnames-after-libgcc))
65
66ldargs-$(user-ta-uuid).elf := $(link-ldflags) $(objs) $(link-ldadd) \
67				$(libgcc$(sm)) $(link-ldadd-after-libgcc)
68
69link-script-cppflags-$(sm) := \
70	$(filter-out $(CPPFLAGS_REMOVE) $(cppflags-remove), \
71		$(nostdinc$(sm)) $(CPPFLAGS) \
72		$(addprefix -I,$(incdirs$(sm)) $(link-out-dir$(sm))) \
73		$(cppflags$(sm)))
74
75-include $(link-script-dep$(sm))
76
77link-script-pp-makefiles$(sm) = $(filter-out %.d %.cmd,$(MAKEFILE_LIST))
78
79define gen-link-t
80$(link-script-pp$(sm)): $(link-script$(sm)) $(conf-file) $(link-script-pp-makefiles$(sm))
81	@$(cmd-echo-silent) '  CPP     $$@'
82	$(q)mkdir -p $$(dir $$@)
83	$(q)$(CPP$(sm)) -P -MT $$@ -MD -MF $(link-script-dep$(sm)) \
84		$(link-script-cppflags-$(sm)) $$< -o $$@
85
86$(link-out-dir$(sm))/$(user-ta-uuid).elf: $(objs) $(libdeps) \
87					  $(libdeps-after-libgcc) \
88					  $(link-script-pp$(sm)) \
89					  $(dynlistdep) \
90					  $(additional-link-deps)
91	@$(cmd-echo-silent) '  LD      $$@'
92	$(q)$(LD$(sm)) $(ldargs-$(user-ta-uuid).elf) -o $$@
93
94$(link-out-dir$(sm))/$(user-ta-uuid).dmp: \
95			$(link-out-dir$(sm))/$(user-ta-uuid).elf
96	@$(cmd-echo-silent) '  OBJDUMP $$@'
97	$(q)$(OBJDUMP$(sm)) -l -x -d $$< > $$@
98
99$(link-out-dir$(sm))/$(user-ta-uuid).stripped.elf: \
100			$(link-out-dir$(sm))/$(user-ta-uuid).elf
101	@$(cmd-echo-silent) '  OBJCOPY $$@'
102	$(q)$(OBJCOPY$(sm)) --strip-unneeded $$< $$@
103
104cmd-echo$(user-ta-uuid) := SIGN   #
105ifeq ($(CFG_ENCRYPT_TA),y)
106crypt-args$(user-ta-uuid) := --enc-key $(TA_ENC_KEY)
107cmd-echo$(user-ta-uuid) := SIGNENC
108endif
109$(link-out-dir$(sm))/$(user-ta-uuid).ta: \
110			$(link-out-dir$(sm))/$(user-ta-uuid).stripped.elf \
111			$(TA_SIGN_KEY) $(TA_SUBKEY_DEPS) \
112			$(lastword $(SIGN_ENC))
113	@$(cmd-echo-silent) '  $$(cmd-echo$(user-ta-uuid)) $$@'
114	$(q)$(SIGN_ENC) --key $(TA_SIGN_KEY) $(TA_SUBKEY_ARGS) \
115		$$(crypt-args$(user-ta-uuid)) \
116		--uuid $(user-ta-uuid) --ta-version $(user-ta-version) \
117		--in $$< --out $$@
118endef
119
120$(eval $(call gen-link-t))
121
122additional-link-deps :=
123