apache / incubator-teaclave-trustzone-sdk

Teaclave TrustZone SDK enables safe, functional, and ergonomic development of trustlets.
https://teaclave.apache.org
Apache License 2.0
203 stars 58 forks source link

hack to 'fix' Issue #135 for acipher-rs TA #138

Open msgilligan opened 1 month ago

msgilligan commented 1 month ago

This is a "hack" which demonstrates changes that I made that eliminate the error seen in Issue #135. I don't understand the compile/link process well enough to submit a real fix and I haven't even tested that the build of this TA works, but I am submitting this patch as a demonstration of how to make the error message seen in Issue #135 go away.

b49020 commented 1 month ago

@msgilligan Since it's a buildroot toolchain compiled from source in case of native aarch64 builds. I would like to see the linker path provided. Can you try following diff instead?

diff --git a/examples/acipher-rs/ta/Makefile b/examples/acipher-rs/ta/Makefile
index ad35909..498b22e 100644
--- a/examples/acipher-rs/ta/Makefile
+++ b/examples/acipher-rs/ta/Makefile
@@ -20,7 +20,7 @@ UUID ?= $(shell cat "../uuid.txt")
 TARGET ?= aarch64-unknown-linux-gnu
 CROSS_COMPILE ?= aarch64-linux-gnu-
 OBJCOPY := $(CROSS_COMPILE)objcopy
-LINKER_CFG := target.$(TARGET).linker=\"$(CROSS_COMPILE)ld.bfd\"
+LINKER_CFG := target.$(TARGET).linker=\"$(CROSS_COMPILE)ld\"

 TA_SIGN_KEY ?= $(TA_DEV_KIT_DIR)/keys/default_ta.pem
 SIGN := $(TA_DEV_KIT_DIR)/scripts/sign_encrypt.py
@@ -29,6 +29,7 @@ OUT_DIR := $(CURDIR)/target/$(TARGET)/release
 all: ta strip sign

 ta:
+       @echo ${LINKER_CFG}
        @cargo build --target $(TARGET) --release --config $(LINKER_CFG)

 strip:
msgilligan commented 1 month ago

Since it's a buildroot toolchain compiled from source in case of native aarch64 builds.

After seeing the following line in setup.sh in this repository, I have been experimenting with using the standard apt toolchain on an aarch64 host:

apt update && apt -y install gcc-aarch64-linux-gnu gcc-arm-linux-gnueabihf

When I tried to apt install gcc-aarch64-linux-gnu I got the following message:

Note, selecting 'gcc' instead of 'gcc-aarch64-linux-gnu'
gcc is already the newest version (4:12.2.0-3).

So this lead me to create PR #137.

Can you try following diff instead?

It's force-pushed and I will try it out in my aarch64 Debian VM.

Please note the two commented-out lines in examples/acipher-rs/ta/build.rs. They set the following two linker options, which I suspect are necessary for proper TA operation (but have not tested yet):

--sort-section=alignment
--dynamic-list=dyn_list

This PR is meant to document my work-in-progress and to stimulate discussion, it is a "very drafty" DRAFT.

Note that I am new to OP-TEE, relatively new to Rust, have not used Rust in embedded environments before and am not knowledgeable about these linker options, so I'm experimenting and learning as I go along...

msgilligan commented 1 month ago

Can you try following diff instead?

It's force-pushed and I will try it out in my aarch64 Debian VM.

If I use this PR after the force-push of your diff, I get the following error message:

ld: cannot find -lgcc_s: No such file or directory

This is the same as with ld.bfd.

If I remove the --config $(LINKER_CFG) from the end of the @cargo line, the build successfully completes. So it seems the --config $(LINKER_CFG) must be doing more than just switching the linker command.

Is there a way to find out what else it is doing?

b49020 commented 1 month ago

Can you share the output of following echo command?

+       @echo ${LINKER_CFG}
b49020 commented 1 month ago

If I remove the --config $(LINKER_CFG) from the end of the @cargo line, the build successfully completes. So it seems the --config $(LINKER_CFG) must be doing more than just switching the linker command.

Is there a way to find out what else it is doing?

It is there to setup linker for the aarch64 target when Rust examples are being cross-compiled. However, here you are trying a native aarch64 Rust examples build which isn't tested before. Let me see if I can test it on a aarch64 machine too.

msgilligan commented 1 month ago

Can you share the output of following echo command?

+       @echo ${LINKER_CFG}
target.aarch64-unknown-linux-gnu.linker="ld"
msgilligan commented 1 month ago

@b49020 See https://github.com/apache/incubator-teaclave-trustzone-sdk/issues/135#issuecomment-2150795417