drogue-iot / embedded-tls

An Rust TLS 1.3 implementation for embedded devices.
Apache License 2.0
172 stars 22 forks source link

Feature: MIPS Support #4

Open sajattack opened 3 years ago

sajattack commented 3 years ago

Hi, I'm working on getting this to compile for MIPS, specifically, mipsel-sony-psp :smile: I've encountered a couple issues, the first of which was

.cflag("--specs=nosys.specs")

in the build.rs of drogue-tls-sys, which I commented out to get a bit further, resulting in the following linker error:

error: linking with `rust-lld` failed: exit code: 1
  |
  = note: "rust-lld" "-flavor" "gnu" "--emit-relocs" "--script" "/tmp/rustceQaBEg/rustc-mipsel-sony-psp-linkfile.ld" "--eh-frame-hdr" "-L" "/home/paul/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/mipsel-sony-psp/lib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/tls_test-fe407b252425d246.tls_test.bh5ffmnh-cgu.0.rcgu.o" "/tmp/cargo/target/mipsel-sony-psp/release/deps/tls_test-fe407b252425d246.tls_test.bh5ffmnh-cgu.1.rcgu.o" "/tmp/cargo/target/mipsel-sony-psp/release/deps/tls_test-fe407b252425d246.tls_test.bh5ffmnh-cgu.10.rcgu.o" "/tmp/cargo/target/mipsel-sony-psp/release/deps/tls_test-fe407b252425d246.tls_test.bh5ffmnh-cgu.11.rcgu.o" "/tmp/cargo/target/mipsel-sony-psp/release/deps/tls_test-fe407b252425d246.tls_test.bh5ffmnh-cgu.12.rcgu.o" "/tmp/cargo/target/mipsel-sony-psp/release/deps/tls_test-fe407b252425d246.tls_test.bh5ffmnh-cgu.13.rcgu.o" "/tmp/cargo/target/mipsel-sony-psp/release/deps/tls_test-fe407b252425d246.tls_test.bh5ffmnh-cgu.14.rcgu.o" "/tmp/cargo/target/mipsel-sony-psp/release/deps/tls_test-fe407b252425d246.tls_test.bh5ffmnh-cgu.15.rcgu.o" "/tmp/cargo/target/mipsel-sony-psp/release/deps/tls_test-fe407b252425d246.tls_test.bh5ffmnh-cgu.2.rcgu.o" "/tmp/cargo/target/mipsel-sony-psp/release/deps/tls_test-fe407b252425d246.tls_test.bh5ffmnh-cgu.3.rcgu.o" "/tmp/cargo/target/mipsel-sony-psp/release/deps/tls_test-fe407b252425d246.tls_test.bh5ffmnh-cgu.4.rcgu.o" "/tmp/cargo/target/mipsel-sony-psp/release/deps/tls_test-fe407b252425d246.tls_test.bh5ffmnh-cgu.5.rcgu.o" "/tmp/cargo/target/mipsel-sony-psp/release/deps/tls_test-fe407b252425d246.tls_test.bh5ffmnh-cgu.6.rcgu.o" "/tmp/cargo/target/mipsel-sony-psp/release/deps/tls_test-fe407b252425d246.tls_test.bh5ffmnh-cgu.7.rcgu.o" "/tmp/cargo/target/mipsel-sony-psp/release/deps/tls_test-fe407b252425d246.tls_test.bh5ffmnh-cgu.8.rcgu.o" "/tmp/cargo/target/mipsel-sony-psp/release/deps/tls_test-fe407b252425d246.tls_test.bh5ffmnh-cgu.9.rcgu.o" "-o" "/tmp/cargo/target/mipsel-sony-psp/release/deps/tls_test-fe407b252425d246" "/tmp/cargo/target/mipsel-sony-psp/release/deps/tls_test-fe407b252425d246.2l1r74g8j7bd858c.rcgu.o" "-O1" "-L" "/tmp/cargo/target/mipsel-sony-psp/release/deps" "-L" "/tmp/cargo/target/release/deps" "-L" "/tmp/cargo/target/mipsel-sony-psp/release/build/drogue-ffi-compat-814b53b555495bf6/out" "-L" "/tmp/cargo/target/mipsel-sony-psp/release/build/drogue-tls-sys-d0da709cd8350c71/out/build/library" "-L" "/tmp/cargo/target/mipsel-sony-psp/release/build/psp-eaba5d74c30458e4/out" "-L" "/home/paul/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/mipsel-sony-psp/lib" "--start-group" "-Bstatic" "/tmp/cargo/target/mipsel-sony-psp/release/deps/libpsp-880272dbea34a26f.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/libbitflags-1f778bbfceb482be.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/libnum_enum-ea1108a89ed97b11.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/libpanic_unwind-9d5a773e48ad5301.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/libunwind-34749114f5c2ae2c.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/liblibc-304ab79f6e16861a.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/libcfg_if-6eba1e6d19653fc1.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/libpaste-5cdf41bac73a8b8a.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/libdrogue_tls-78fcc7b9b95d3a05.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/liblog-bd5a3f0baf5c9591.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/libcfg_if-52575463aa6737fe.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/libdrogue_network-eca1c74b036417ac.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/libnb-a10826c77cebfb0f.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/libnb-3111f54c991fa057.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/libno_std_net-c9ef1072820a82cb.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/libcortex_m-7373f80f2a2f6a64.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/libvolatile_register-336bf940b6c1efbb.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/libvcell-53db5e0da633532a.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/libbare_metal-48a07fdcb8182655.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/libaligned-8a0578a4267a59c1.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/libdrogue_tls_sys-8a0a586684123d43.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/libdrogue_ffi_compat-c4119ac008e03123.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/libheapless-f50791a0a73c95eb.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/libas_slice-c0e127ac585661bc.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/libstable_deref_trait-73d7f4ad268a08a9.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/libgeneric_array-8e55ccf01a9512c0.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/libgeneric_array-2783b5efc350047e.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/libhash32-ac5d74c170e48ab8.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/libbyteorder-4d922be9382afeb1.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/libgeneric_array-d6bd62b464817469.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/libtypenum-e9ea79367608db85.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/liballoc-d8c8fb192fcc01cb.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/librustc_std_workspace_core-f5d2557c5bcf4e39.rlib" "/tmp/cargo/target/mipsel-sony-psp/release/deps/libcore-4b4dc1cf417634a3.rlib" "--end-group" "/tmp/cargo/target/mipsel-sony-psp/release/deps/libcompiler_builtins-8fd0e71070cc2276.rlib" "-Bdynamic"
  = note: rust-lld: error: /tmp/cargo/target/mipsel-sony-psp/release/deps/libdrogue_tls_sys-8a0a586684123d43.rlib(ctr_drbg.c.o) is incompatible with /tmp/cargo/target/mipsel-sony-psp/release/deps/tls_test-fe407b252425d246.tls_test.bh5ffmnh-cgu.0.rcgu.o
          rust-lld: error: /tmp/cargo/target/mipsel-sony-psp/release/deps/libdrogue_tls_sys-8a0a586684123d43.rlib(entropy.c.o) is incompatible with /tmp/cargo/target/mipsel-sony-psp/release/deps/tls_test-fe407b252425d246.tls_test.bh5ffmnh-cgu.0.rcgu.o
          rust-lld: error: /tmp/cargo/target/mipsel-sony-psp/release/deps/libdrogue_tls_sys-8a0a586684123d43.rlib(platform.c.o) is incompatible with /tmp/cargo/target/mipsel-sony-psp/release/deps/tls_test-fe407b252425d246.tls_test.bh5ffmnh-cgu.0.rcgu.o
          rust-lld: error: /tmp/cargo/target/mipsel-sony-psp/release/deps/libdrogue_tls_sys-8a0a586684123d43.rlib(debug.c.o) is incompatible with /tmp/cargo/target/mipsel-sony-psp/release/deps/tls_test-fe407b252425d246.tls_test.bh5ffmnh-cgu.0.rcgu.o
          rust-lld: error: /tmp/cargo/target/mipsel-sony-psp/release/deps/libdrogue_tls_sys-8a0a586684123d43.rlib(ssl_msg.c.o) is incompatible with /tmp/cargo/target/mipsel-sony-psp/release/deps/tls_test-fe407b252425d246.tls_test.bh5ffmnh-cgu.0.rcgu.o
          rust-lld: error: /tmp/cargo/target/mipsel-sony-psp/release/deps/libdrogue_tls_sys-8a0a586684123d43.rlib(ssl_tls.c.o) is incompatible with /tmp/cargo/target/mipsel-sony-psp/release/deps/tls_test-fe407b252425d246.tls_test.bh5ffmnh-cgu.0.rcgu.o
          rust-lld: error: /tmp/cargo/target/mipsel-sony-psp/release/deps/libdrogue_ffi_compat-c4119ac008e03123.rlib(printf.o) is incompatible with /tmp/cargo/target/mipsel-sony-psp/release/deps/tls_test-fe407b252425d246.tls_test.bh5ffmnh-cgu.0.rcgu.o

error: aborting due to previous error; 6 warnings emitted

error: could not compile `tls-test`

I did a bit more digging, namely, running ar x on the rlibs it mentioned, and then file on the resulting .o, which let me know it was compiled for x86_64.

ctr_drbg.c.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped

Do I need to supply a MIPS-specific specs file or are the two issues unrelated? How can I tell it to compile for MIPS-2?

sajattack commented 3 years ago

I would also recommend making the cortex-m dependency an optional dependency behind a feature flag.

sajattack commented 3 years ago

So, I tried setting TARGET_CC to clang, and TARGET_CFLAGS to specify a clang target that's compatible, but it looks like TARGET_CFLAGS also get passed to gcc for linking. I wonder what others do here.

sajattack commented 3 years ago

After lots of mucking about with cmake and c flags, I got all the mbedtls .a files built for mips. Now just a few functions missing when I try to link:

 = note: rust-lld: error: undefined symbol: strncmp
          >>> referenced by x509_create.c
          >>>               x509_create.c.obj:(mbedtls_x509_string_to_names) in archive ./library/libmbedx509.a
          >>> referenced by x509_create.c
          >>>               x509_create.c.obj:(mbedtls_x509_string_to_names) in archive ./library/libmbedx509.a
          >>> referenced by x509_create.c
          >>>               x509_create.c.obj:(mbedtls_x509_string_to_names) in archive ./library/libmbedx509.a
          >>> referenced 21 more times

          rust-lld: error: undefined symbol: strstr
          >>> referenced by x509_crt.c
          >>>               x509_crt.c.obj:(mbedtls_x509_crt_parse) in archive ./library/libmbedx509.a
          >>> referenced by pem.c
          >>>               pem.c.obj:(mbedtls_pem_read_buffer) in archive ./library/libmbedcrypto.a
          >>> referenced by pem.c
          >>>               pem.c.obj:(mbedtls_pem_read_buffer) in archive ./library/libmbedcrypto.a

          rust-lld: error: undefined symbol: strncpy
          >>> referenced by x509write_crt.c
          >>>               x509write_crt.c.obj:(mbedtls_x509write_crt_set_validity) in archive ./library/libmbedx509.a
          >>> referenced by x509write_crt.c
          >>>               x509write_crt.c.obj:(mbedtls_x509write_crt_set_validity) in archive ./library/libmbedx509.a

          rust-lld: error: undefined symbol: strcmp
          >>> referenced by cipher.c
          >>>               cipher.c.obj:(mbedtls_cipher_info_from_string) in archive ./library/libmbedcrypto.a
          >>> referenced by ecp.c
          >>>               ecp.c.obj:(mbedtls_ecp_curve_info_from_name) in archive ./library/libmbedcrypto.a
          >>> referenced by ecp.c
          >>>               ecp.c.obj:(mbedtls_ecp_curve_info_from_name) in archive ./library/libmbedcrypto.a
          >>> referenced 20 more times

          rust-lld: error: undefined symbol: rand
          >>> referenced by rsa.c
          >>>               rsa.c.obj:(mbedtls_rsa_self_test) in archive ./library/libmbedcrypto.a
          >>> referenced by rsa.c
          >>>               rsa.c.obj:(myrand) in archive ./library/libmbedcrypto.a

Could these be good candidates for inclusion in drogue-ffi-compat?