Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cross compile for no_std environment build is failing at linking #598

Closed
isaac-asdf opened this issue Apr 18, 2023 · 13 comments
Closed

Cross compile for no_std environment build is failing at linking #598

isaac-asdf opened this issue Apr 18, 2023 · 13 comments

Comments

@isaac-asdf
Copy link
Contributor

isaac-asdf commented Apr 18, 2023

I posted this in the hal crate for the microcontroller I'm working with ESP32, but will cross post here in case it's something I'm just doing wrong with the initial set up. Am I supposed to build the C library manually, or use the crate secp256k1-sys to build the the library for use? I'm getting errors for the functions I'm calling that look like : undefined reference to 'rustsecp256k1_v0_8_1_schnorrsig_sign'

Source code for project

@apoelstra
Copy link
Member

It's difficult to see what's happening since even cargo tree fails with error: invalid toolchain name: 'esp'. When I try moving/deleting rust-toolchain.toml I get process didn't exit successfully:rustc - --crate-name ___ --print=file-names -C link-arg=-Tlinkall.x -C link-arg=-nostartfiles -C link-arg=-Tesp32_rom_functions.x -L libsecp256k1/libsecp256k1.a -l secp256k1 --target xtensa-esp32-none-elf --crate-type bin --crate-type rlib --crate-type dylib --crate-type cdylib --crate-type staticlib --crate-type proc-macro --print=sysroot --print=cfg (exit status: 1).

But the short answer is that yes, you should be compiling libsecp via secp256k1-sys. If you don't want to do this, you can add RUSTFLAGS=--cfg=rust_secp_no_symbol_renaming which will forgo compiling the included version of libsecp (and avoid doing the rustsecp256k1_v0_8_1 prefixing of all the symbols); in that case you are responsible for compiling and linking a compatible version of libsecp into your project.

But having said this, your existing setup should Just Work, using secp256k1-sys (which is a dep of secp256k1), whose build.rs should build libsecp256k1 for you.

I checked my local copy of secp256k1-sys 0.8.1 in my cargo cache and it does appear to define and export that symbol..

@Kixunil
Copy link
Collaborator

Kixunil commented Apr 18, 2023

@apoelstra IIRC you need to use a forked compiler for ESP (for now, until they upstream it).

@isaac-asdf
Copy link
Contributor Author

@Kixunil is correct, ESP uses needs a forked compiler. Running cargo tree gave me:

├── secp256k1 v0.27.0
│   ├── rand v0.8.5
│   │   └── rand_core v0.6.4
│   ├── secp256k1-sys v0.8.1
│   │   [build-dependencies]
│   │   └── cc v1.0.79
│   └── serde v1.0.160 (*)

I'll try the step @apoelstra mentioned of compiling with RUSTFLAGS=--cfg=rust_secp_no_symbol_renaming and let y'all know if that worked

@apoelstra
Copy link
Member

My guess is that if you compile with that flag, you'll get link errors of the form undefined reference to secp256k1_schnorrsig_sign (note no weird rust version prefixing) and the solution will be to link your own copy of libsecp in.

My guess about the original issue is that the ESP compiler is somehow incompatible with the link symbol shenanigans we do in this library ... though I have no idea who is at fault.

@Kixunil
Copy link
Collaborator

Kixunil commented Apr 18, 2023

I'd guess this should be job for cc.

@isaac-asdf
Copy link
Contributor Author

isaac-asdf commented Apr 19, 2023

Yeah, the RUSTFLAGS=--cfg=rust_secp_no_symbol_renaming just gave me different errors. @Kixunil do you have any recommendations or resources I could look into for troubleshooting this on the cc side? I'm also wondering if it would just be easier to try to cut out this crate and try to right the FFI bindings for the few functions I'm actually using (basically just Schnorr signing)

@apoelstra
Copy link
Member

@isaac-asdf if you want to do that, you could try depending on secp256k1-sys directly

@Kixunil
Copy link
Collaborator

Kixunil commented Apr 20, 2023

@isaac-asdf maybe you could first run their tests on ESP and see if anything fails, if it does report it to them, if it doesn't then it may be actually our problem.

@isaac-asdf
Copy link
Contributor Author

The code I have compiles and runs as long as the signature portion is commented out. I'm testing if I can get it to compile and run with just including a more basic C function to see if it's just an xtensa linking issue with cc. If that doesn't run then I think I can just close this out as it would be a linking issue

@isaac-asdf
Copy link
Contributor Author

The issue appears to be coming from the combo of cc and the Rust fork that the xtensa chip is using. I ran the code from this repo and got the same error message of undefined reference to my_c_function. Thanks for the help @Kixunil and @apoelstra!

@0xApprentice
Copy link

@isaac-asdf
image
hi,Did you resolve your issue in the end?
I'm encountering compatibility issues with secp256k1 while linking a Rust static library for ESP32. and I found the following message: 'c928380082b86401-secp256k1.o: file format not recognized'.

@isaac-asdf
Copy link
Contributor Author

isaac-asdf commented Aug 29, 2024 via email

@0xApprentice
Copy link

Thank you for your help, I will go and give it a try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants