From 5950ffbe09b01ee8000766483e831ab2d229fe1d Mon Sep 17 00:00:00 2001 From: Jens Wiklander Date: Wed, 31 May 2023 10:49:16 +0200 Subject: [PATCH 1/5] Move mv-if-changed() into mk/macros.mk Moves the make macro mv-if-changed() into mk/macros.mk to allow use of it in the TA devkit. Reviewed-by: Jerome Forissier Signed-off-by: Jens Wiklander --- Makefile | 1 + mk/checkconf.mk | 10 ---------- mk/macros.mk | 9 +++++++++ ta/mk/ta_dev_kit.mk | 2 ++ ta/ta.mk | 2 +- 5 files changed, 13 insertions(+), 11 deletions(-) create mode 100644 mk/macros.mk diff --git a/Makefile b/Makefile index 516ee610ce3..8db66780f52 100644 --- a/Makefile +++ b/Makefile @@ -18,6 +18,7 @@ unexport MAKEFILE_LIST # nonzero status). Useful since a few recipes use shell redirection. .DELETE_ON_ERROR: +include mk/macros.mk include mk/checkconf.mk .PHONY: all diff --git a/mk/checkconf.mk b/mk/checkconf.mk index 77b3748ee1e..449b1c2b89c 100644 --- a/mk/checkconf.mk +++ b/mk/checkconf.mk @@ -55,16 +55,6 @@ define check-conf-mk $(call mv-if-changed,$@.tmp,$@) endef -# Rename $1 to $2 only if file content differs. Otherwise just delete $1. -define mv-if-changed - if cmp -s $2 $1; then \ - rm -f $1; \ - else \ - $(cmd-echo-silent) ' UPD $2'; \ - mv $1 $2; \ - fi -endef - define cfg-vars-by-prefix $(strip $(if $(1),$(call _cfg-vars-by-prefix,$(1)), $(call _cfg-vars-by-prefix,CFG_ _CFG_ PLATFORM_))) diff --git a/mk/macros.mk b/mk/macros.mk new file mode 100644 index 00000000000..277ecf2212c --- /dev/null +++ b/mk/macros.mk @@ -0,0 +1,9 @@ +# Rename $1 to $2 only if file content differs. Otherwise just delete $1. +define mv-if-changed + if cmp -s $2 $1; then \ + rm -f $1; \ + else \ + $(cmd-echo-silent) ' UPD $2'; \ + mv $1 $2; \ + fi +endef diff --git a/ta/mk/ta_dev_kit.mk b/ta/mk/ta_dev_kit.mk index b93a751b22f..6d544a7a4ef 100644 --- a/ta/mk/ta_dev_kit.mk +++ b/ta/mk/ta_dev_kit.mk @@ -7,6 +7,8 @@ all: include $(ta-dev-kit-dir)/mk/conf.mk ta-dev-kit-dir$(sm) := $(ta-dev-kit-dir) +include $(ta-dev-kit-dir$(sm))/mk/macros.mk + ifneq (1, $(words $(BINARY) $(LIBNAME) $(SHLIBNAME))) $(error You must specify exactly one of BINARY, LIBNAME or SHLIBNAME) endif diff --git a/ta/ta.mk b/ta/ta.mk index fb7335a4450..09c20bfa16f 100644 --- a/ta/ta.mk +++ b/ta/ta.mk @@ -148,7 +148,7 @@ $(foreach f, $(libfiles), \ # Copy .mk files ta-mkfiles = mk/compile.mk mk/subdir.mk mk/gcc.mk mk/clang.mk mk/cleandirs.mk \ - mk/cc-option.mk \ + mk/cc-option.mk mk/macros.mk \ ta/link.mk ta/link_shlib.mk \ ta/mk/ta_dev_kit.mk From bdab5181e94c9780314191d2b65b355067ee7b05 Mon Sep 17 00:00:00 2001 From: Jens Wiklander Date: Wed, 31 May 2023 09:56:23 +0200 Subject: [PATCH 2/5] ta/link.mk: always generate TA dyn_list Always generate the file holding the list of dynamic symbols a TA should provide. This is needed if CFG_FTRACE_SUPPORT should be changed between two compilations. Use the make macro mv-if-changed() to only update the used file if it will be changed. Reviewed-by: Jerome Forissier Signed-off-by: Jens Wiklander --- ta/link.mk | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/ta/link.mk b/ta/link.mk index 40bdc9fb909..ae6219a392c 100644 --- a/ta/link.mk +++ b/ta/link.mk @@ -37,17 +37,18 @@ endif link-ldflags += --as-needed # Do not add dependency on unused shlib link-ldflags += $(link-ldflags$(sm)) -$(link-out-dir$(sm))/dyn_list: +$(link-out-dir$(sm))/dyn_list: FORCE @$(cmd-echo-silent) ' GEN $@' $(q)mkdir -p $(dir $@) - $(q)echo "{" >$@ - $(q)echo "__elf_phdr_info;" >>$@ + $(q)echo "{" >$@.tmp + $(q)echo "__elf_phdr_info;" >>$@.tmp ifeq ($(CFG_FTRACE_SUPPORT),y) - $(q)echo "__ftrace_info;" >>$@ + $(q)echo "__ftrace_info;" >>$@.tmp endif - $(q)echo "trace_ext_prefix;" >>$@ - $(q)echo "trace_level;" >>$@ - $(q)echo "};" >>$@ + $(q)echo "trace_ext_prefix;" >>$@.tmp + $(q)echo "trace_level;" >>$@.tmp + $(q)echo "};" >>$@.tmp + $(q)$(call mv-if-changed,$@.tmp,$@) link-ldflags += --dynamic-list $(link-out-dir$(sm))/dyn_list dynlistdep = $(link-out-dir$(sm))/dyn_list cleanfiles += $(link-out-dir$(sm))/dyn_list From 4f3e80681b00d6c49ca689264750df0b907a0b58 Mon Sep 17 00:00:00 2001 From: Jens Wiklander Date: Tue, 30 May 2023 19:40:36 +0200 Subject: [PATCH 3/5] scripts/ts_bin_to_c.py: look for ta_head symbol Legacy TAs have their TA header in a .ta_head section of the TA binary. However, in commits to follow the TA header will instead be located in the symbol ta_head located somewhere inside the ELF binary. So update the ts_bin_to_c.py script to support the updated format. Acked-by: Jerome Forissier Signed-off-by: Jens Wiklander --- scripts/ts_bin_to_c.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/scripts/ts_bin_to_c.py b/scripts/ts_bin_to_c.py index f150207a30c..2bbd74e3056 100755 --- a/scripts/ts_bin_to_c.py +++ b/scripts/ts_bin_to_c.py @@ -8,6 +8,7 @@ import argparse import array from elftools.elf.elffile import ELFFile, ELFError +from elftools.elf.sections import SymbolTableSection import os import re import struct @@ -65,6 +66,21 @@ def ta_get_flags(ta_f): with open(ta_f, 'rb') as f: elffile = ELFFile(f) + for s in elffile.iter_sections(): + if isinstance(s, SymbolTableSection): + for symbol in s.iter_symbols(): + if symbol.name == 'ta_head': + # Get the section containing the symbol + s2 = elffile.get_section(symbol.entry['st_shndx']) + offs = s2.header['sh_offset'] - s2.header['sh_addr'] + # ta_head offset into ELF binary + offs = offs + symbol.entry['st_value'] + offs = offs + 20 # Flags offset in ta_head + f.seek(offs) + flags = struct.unpack(' Date: Wed, 31 May 2023 10:07:07 +0200 Subject: [PATCH 4/5] ldelf: look for ta_head symbol Legacy TAs has their TA header as at the load address of the TA binary. However, in commits to follow the TA header will instead be located in the symbol ta_head located somewhere inside the ELF binary. So update the ldelf to support the updated format while still supporting legacy TAs. Reviewed-by: Jerome Forissier Signed-off-by: Jens Wiklander --- ldelf/ta_elf.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ldelf/ta_elf.c b/ldelf/ta_elf.c index d7d975683c9..cf802ac8acf 100644 --- a/ldelf/ta_elf.c +++ b/ldelf/ta_elf.c @@ -1195,6 +1195,8 @@ static void set_tls_offset(struct ta_elf *elf __unused) {} static void load_main(struct ta_elf *elf) { + vaddr_t va = 0; + init_elf(elf); map_segments(elf); populate_segments(elf); @@ -1207,7 +1209,10 @@ static void load_main(struct ta_elf *elf) if (elf->bti_enabled) ta_elf_add_bti(elf); - elf->head = (struct ta_head *)elf->load_addr; + if (!ta_elf_resolve_sym("ta_head", &va, NULL, elf)) + elf->head = (struct ta_head *)va; + else + elf->head = (struct ta_head *)elf->load_addr; if (elf->head->depr_entry != UINT64_MAX) { /* * Legacy TAs sets their entry point in ta_head. For From 6878231b941d1ba15fa3424dd31905aa6a071272 Mon Sep 17 00:00:00 2001 From: Jens Wiklander Date: Wed, 31 May 2023 10:08:45 +0200 Subject: [PATCH 5/5] ta: relax location of ta_head TAs where required to have the ta_head as at the load address of the TA prior to this patch. This makes the linker script slightly more complicated and also confuses GDB so that an offset must be applied to the load address of the TA when using GDB for debugging. So allow that ta_head symbol to reside anywhere in the ELF binary and also add ta_head to the .dynsym section to make sure that tools and ldelf can find the symbol. This change requires prior updates to tools and ldelf. Reviewed-by: Jerome Forissier Signed-off-by: Jens Wiklander --- ta/arch/arm/ta.ld.S | 1 - ta/link.mk | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/ta/arch/arm/ta.ld.S b/ta/arch/arm/ta.ld.S index 1ec760a34e4..de0fa61fab5 100644 --- a/ta/arch/arm/ta.ld.S +++ b/ta/arch/arm/ta.ld.S @@ -14,7 +14,6 @@ OUTPUT_ARCH(aarch64) #endif SECTIONS { - .ta_head : {*(.ta_head)} .text : { __text_start = .; *(.text .text.*) diff --git a/ta/link.mk b/ta/link.mk index ae6219a392c..b00f22055bf 100644 --- a/ta/link.mk +++ b/ta/link.mk @@ -47,6 +47,7 @@ ifeq ($(CFG_FTRACE_SUPPORT),y) endif $(q)echo "trace_ext_prefix;" >>$@.tmp $(q)echo "trace_level;" >>$@.tmp + $(q)echo "ta_head;" >>$@.tmp $(q)echo "};" >>$@.tmp $(q)$(call mv-if-changed,$@.tmp,$@) link-ldflags += --dynamic-list $(link-out-dir$(sm))/dyn_list