japaric / cargo-call-stack

Whole program static stack analysis
Apache License 2.0
579 stars 52 forks source link

Any chance of supporting AVR? #92

Open xphoniex opened 1 year ago

xphoniex commented 1 year ago

I'm compiling rust code for AVR using avr-hal which uses avr-gcc for final compilation and bypasses LLVM. If I try:

$ cargo call-stack...

error: failed to parse application's LLVM IR from `/target/avr-atmega328p.json/release/deps/arduino_lib-3273400ccbbcd796.ll`: BUG: failed to parse LLVM IR; please submit a cargo-call-stack bug report and attach the `.ll` file: Failure("Eof in line 46")

file rename and attached: arduino_lib-3273400ccbbcd796.txt

matteocarnelos commented 1 year ago

I had the same issue. The problem is that avr-hal uses custom targets, and therefore this and other tools won't work.

As a temporary solution, you can use the built-in avr-unknown-gnu-atmega328 target together with the default-linker-libraries flag set in your .cargo/config.toml file:

[target.'cfg(target_arch = "avr")']
runner = "ravedude nano -cb 57600"
rustflags = [
    "-C", "default-linker-libraries",
]

[build]
target = "avr-unknown-gnu-atmega328"

[unstable]
build-std = ["core"]

I opened a discussion on avr-hal to discuss switching to built-in targets to fix these issues, but I haven't received any reply so far.

xphoniex commented 1 year ago

Still not working for me, here's a sample: avr-bug.zip

after changing:

# .cargo/config.toml

rustflags = ["-Z emit-stack-sizes", "-C", "default-linker-libraries"] 

# ...

[build]
target = "avr-unknown-gnu-atmega328"

now getting a lot of:

          /.cargo/git/checkouts/noble-secp256k1-rs-de6fa1b71b2c997d/cc4b0d9/src/point.rs:88: undefined reference to `memcpy'
          /.cargo/git/checkouts/noble-secp256k1-rs-de6fa1b71b2c997d/cc4b0d9/src/point.rs:91: undefined reference to `memcpy'
          /avr-bug/target/avr-unknown-gnu-atmega328/release/deps/avr_bug-c860dd4dcd188a17.avr_bug.61b298e20baa14c7-cgu.0.rcgu.o: In function `awint_core::data::inlawi::InlAwi$LT$_$C$_$GT$::unstable_from_u8_slice::h6374a892fad81781':
          /.cargo/registry/src/index.crates.io-6f17d22bba15001f/awint_core-0.7.0/src/data/inlawi.rs:169: undefined reference to `memset'
          /.cargo/registry/src/index.crates.io-6f17d22bba15001f/awint_core-0.7.0/src/data/inlawi.rs:169: undefined reference to `memset'
          /.cargo/registry/src/index.crates.io-6f17d22bba15001f/awint_core-0.7.0/src/data/inlawi.rs:169: undefined reference to `memset'
          /avr-bug/target/avr-unknown-gnu-atmega328/release/deps/avr_bug-c860dd4dcd188a17.avr_bug.61b298e20baa14c7-cgu.0.rcgu.o: In function `core::intrinsics::copy_nonoverlapping::h0c9c42d02499e18c':
          /.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/intrinsics.rs:2663: undefined reference to `memcpy'
          /.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/intrinsics.rs:2663: undefined reference to `memcpy'
          /.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/intrinsics.rs:2663: undefined reference to `memcpy'
          /.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/intrinsics.rs:2663: undefined reference to `memcpy'
          /.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/intrinsics.rs:2663: undefined reference to `memcpy'
          /avr-bug/target/avr-unknown-gnu-atmega328/release/deps/avr_bug-c860dd4dcd188a17.avr_bug.61b298e20baa14c7-cgu.0.rcgu.o:/.cargo/git/checkouts/noble-secp256k1-rs-de6fa1b71b2c997d/cc4b0d9/src/point.rs:519: more undefined references to `memcpy' follow
          /avr-bug/target/avr-unknown-gnu-atmega328/release/deps/avr_bug-c860dd4dcd188a17.avr_bug.61b298e20baa14c7-cgu.0.rcgu.o: In function `noble_secp256k1::point::Point::double_assign_mod::h5cae01c52035a5db':
          avr_bug.61b298e20baa14c7-cgu.0:(.text._ZN15noble_secp256k15point5Point17double_assign_mod17h5cae01c52035a5dbE+0x818): undefined reference to `abort'
          collect2: error: ld returned 1 exit status

  = note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

warning: `avr-bug` (bin "avr-bug") generated 3 warnings
error: could not compile `avr-bug` (bin "avr-bug") due to previous error; 3 warnings emitted
matteocarnelos commented 1 year ago

Some notes about the code you attached:

  1. The rustflags option must be placed under the [target] attribute.
  2. "-Z emit-stack-sizes" is not a valid flag, you need to split it in two strings: "-Z", "emit-stack-sizes".
  3. If you change the build target, you have to modify the [target] attribute accordingly.

Therefore, this is the correct .cargo/config.toml:

[target.avr-unknown-gnu-atmega328]
runner = "qemu-system-avr -M uno -nographic -serial tcp::5678,server=on -bios"
rustflags = ["-Z", "emit-stack-sizes", "-C", "default-linker-libraries"]

[build]
target = "avr-unknown-gnu-atmega328"

[unstable]
build-std = ["core"]
xphoniex commented 1 year ago

Still no luck, now I'm getting:

error: couldn't open ELF file `/avr-bug/target/avr-unknown-gnu-atmega328/release/avr-bug`: No such file or directory (os error 2)
matteocarnelos commented 1 year ago

This error seems to be unrelated to the compilation target, so I cannot really help.