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

Linking fails for AVR: undefined reference to '__gtdf2' / '__gedf2' #139144

Closed
karip opened this issue Mar 30, 2025 · 7 comments
Closed

Linking fails for AVR: undefined reference to '__gtdf2' / '__gedf2' #139144

karip opened this issue Mar 30, 2025 · 7 comments
Labels
A-floating-point Area: Floating point numbers and arithmetic A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. O-AVR Target: AVR processors (ATtiny, ATmega, etc.) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@karip
Copy link

karip commented Mar 30, 2025

I'm trying to build a Rust project for Arduino Mega 2560. The project was generated with avr-hal and is based on mega2560-usart.

Here's a zip package with a complete Rust project: avr-f64-fail.zip

I tried this code with f64 floating point numbers. Linking fails when cargo build is run:

    let mem = &[ 1.0f64, 2.0f64 ];
    let mut total = 0.0f64;
    for val in mem {
        total += val;
    }

I expected to see this happen: the program is linked successfully.
Instead, this happened: a linking error is given:

   Compiling avr-f64-fail v0.1.0 (/home/user.linux/rust/avr-f64-fail)
error: linking with `avr-gcc` failed: exit status: 1
  |
  = note: LC_ALL="C" PATH="/home/user.linux/.rustup/toolchains/nightly-2024-03-22-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/bin:/home/user.linux/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/sbin:/sbin:/usr/sbin:/sbin" VSLANG="1033" "avr-gcc" "-mmcu=atmega2560" "-Wl,--as-needed,--print-memory-usage" "/tmp/rustc5qP9bC/symbols.o" "/home/user.linux/rust/avr-f64-fail/target/avr-atmega2560/debug/deps/avr_f64_fail-15b0a58a54a0cc6f.panic_halt-b1c0cfac532dc279.panic_halt.fa57cffaf2a67a43-cgu.0.rcgu.o.rcgu.o" "-Wl,--as-needed" "-L" "/home/user.linux/rust/avr-f64-fail/target/avr-atmega2560/debug/deps" "-L" "/home/user.linux/rust/avr-f64-fail/target/debug/deps" "-L" "/home/user.linux/.rustup/toolchains/nightly-2024-03-22-aarch64-unknown-linux-gnu/lib/rustlib/avr-atmega2560/lib" "-Wl,-Bstatic" "/home/user.linux/rust/avr-f64-fail/target/avr-atmega2560/debug/deps/libcompiler_builtins-89c1a6b65a32f973.rlib" "-Wl,-Bdynamic" "-lgcc" "-Wl,-z,noexecstack" "-L" "/home/user.linux/.rustup/toolchains/nightly-2024-03-22-aarch64-unknown-linux-gnu/lib/rustlib/avr-atmega2560/lib" "-o" "/home/user.linux/rust/avr-f64-fail/target/avr-atmega2560/debug/deps/avr_f64_fail-15b0a58a54a0cc6f.elf" "-Wl,--gc-sections" "-no-pie"
  = note: /home/user.linux/rust/avr-f64-fail/target/avr-atmega2560/debug/deps/avr_f64_fail-15b0a58a54a0cc6f.panic_halt-b1c0cfac532dc279.panic_halt.fa57cffaf2a67a43-cgu.0.rcgu.o.rcgu.o: In function `avr_f64_fail::__avr_device_rt_main':
          /home/user.linux/rust/avr-f64-fail/src/main.rs:22: undefined reference to `__gtdf2'
          /home/user.linux/rust/avr-f64-fail/src/main.rs:22: undefined reference to `__gedf2'
          /home/user.linux/rust/avr-f64-fail/target/avr-atmega2560/debug/deps/libcompiler_builtins-89c1a6b65a32f973.rlib(compiler_builtins-89c1a6b65a32f973.compiler_builtins.59f32f9b712396b1-cgu.0.rcgu.o): In function `<u64 as core::ops::arith::AddAssign>::add_assign':
          /home/user.linux/.rustup/toolchains/nightly-2024-03-22-aarch64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/arith.rs:761: undefined reference to `core::panicking::panic'
          /home/user.linux/.rustup/toolchains/nightly-2024-03-22-aarch64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/arith.rs:761: undefined reference to `core::panicking::panic'
          /home/user.linux/rust/avr-f64-fail/target/avr-atmega2560/debug/deps/libcompiler_builtins-89c1a6b65a32f973.rlib(compiler_builtins-89c1a6b65a32f973.compiler_builtins.59f32f9b712396b1-cgu.0.rcgu.o): In function `<u64 as core::ops::bit::Shl<u32>>::shl':
          /home/user.linux/.rustup/toolchains/nightly-2024-03-22-aarch64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/bit.rs:470: undefined reference to `core::panicking::panic'
          /home/user.linux/rust/avr-f64-fail/target/avr-atmega2560/debug/deps/libcompiler_builtins-89c1a6b65a32f973.rlib(compiler_builtins-89c1a6b65a32f973.compiler_builtins.59f32f9b712396b1-cgu.0.rcgu.o): In function `compiler_builtins::float::add::add':
          /home/user.linux/.cargo/registry/src/index.crates.io-6f17d22bba15001f/compiler_builtins-0.1.108/src/float/add.rs:149: undefined reference to `core::panicking::panic'
          /home/user.linux/.cargo/registry/src/index.crates.io-6f17d22bba15001f/compiler_builtins-0.1.108/src/float/add.rs:161: undefined reference to `core::panicking::panic'
          /home/user.linux/rust/avr-f64-fail/target/avr-atmega2560/debug/deps/libcompiler_builtins-89c1a6b65a32f973.rlib(compiler_builtins-89c1a6b65a32f973.compiler_builtins.59f32f9b712396b1-cgu.0.rcgu.o):/home/user.linux/.rustup/toolchains/nightly-2024-03-22-aarch64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/bit.rs:588: more undefined references to `core::panicking::panic' follow
          collect2: error: ld returned 1 exit status
          Memory region         Used Size  Region Size  %age Used
                      text:        6816 B       256 KB      2.60%
                      data:         809 B         8 KB      9.88%
                    eeprom:          0 GB         4 KB      0.00%
                      fuse:          0 GB          3 B      0.00%
                      lock:          0 GB         1 KB      0.00%
                 signature:          0 GB         1 KB      0.00%
           user_signatures:          0 GB         1 KB      0.00%
          
  = 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#rustc-link-lib)

error: could not compile `avr-f64-fail` (bin "avr-f64-fail") due to 1 previous error

Meta

rustc --version --verbose:

rustc 1.79.0-nightly (0ad927c0c 2024-03-21)
binary: rustc
commit-hash: 0ad927c0c07b65fc0dae37105e09c877c87c296a
commit-date: 2024-03-21
host: aarch64-unknown-linux-gnu
release: 1.79.0-nightly
LLVM version: 18.1.2
@karip karip added the C-bug Category: This is a bug. label Mar 30, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 30, 2025
@karip karip changed the title Linking fails for AVR: undefined reference to __gtdf2' / __gedf2' Linking fails for AVR: undefined reference to '__gtdf2' / '__gedf2' Mar 30, 2025
@moxian
Copy link
Contributor

moxian commented Mar 31, 2025

@rustbot label: +O-AVR +A-floating-point +A-linkage +T-compiler

@rustbot rustbot added A-floating-point Area: Floating point numbers and arithmetic A-linkage Area: linking into static, shared libraries and binaries O-AVR Target: AVR processors (ATtiny, ATmega, etc.) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 31, 2025
@jfrimmel
Copy link
Contributor

jfrimmel commented Apr 1, 2025

According to the libgcc-docs, those are used to compare two f64-values (that are not NaN) with >= and >. Those functions thus should be provided by libgcc, which is linked by default. The argument is indeed part of the command line in the report, so this should not be the problem. Libgcc should contain an implementation for this function for at least 10 years. So this is likely not the problem as well...

Which version of avr-gcc are you using?

@karip
Copy link
Author

karip commented Apr 1, 2025

I'm using avr-gcc (GCC) 7.3.0 on Ubuntu 24.04.2 LTS.

My project doesn't use avr-none, because the AVR template didn't use it. It uses avr-specs/avr-atmega2560.json. I tried removing rust-toolchain.toml and replacing .cargo/config.toml with this:

[build]
#target = "avr-specs/avr-atmega2560.json"
target = "avr-none"
rustflags = ["-C", "target-cpu=atmega2560"]

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

Now the project should use avr-none, but I'm still getting the same linking error.

Here's the long output of avr-gcc -v:

Using built-in specs.
Reading specs from /usr/lib/gcc/avr/7.3.0/device-specs/specs-avr2
COLLECT_GCC=avr-gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/avr/7.3.0/lto-wrapper
Target: avr
Configured with: ../gcc/configure -v --enable-languages=c,c++ --prefix=/usr/lib --infodir=/usr/share/info --mandir=/usr/share/man --bindir=/usr/bin --libexecdir=/usr/lib --libdir=/usr/lib --enable-shared --with-system-zlib --enable-long-long --enable-nls --without-included-gettext --disable-libssp --disable-libcc1 --build=aarch64-linux-gnu --host=aarch64-linux-gnu --target=avr --enable-host-pie ASFLAGS= ASFLAGS_FOR_BUILD= CFLAGS='-g -O2 -ffile-prefix-map=/build/gcc-avr-SuyGv2/gcc-avr-7.3.0+Atmel3.7.0=. -flto=auto -ffat-lto-objects -fstack-protector-strong -fstack-clash-protection -Wformat  -mbranch-protection=standard -fdebug-prefix-map=/build/gcc-avr-SuyGv2/gcc-avr-7.3.0+Atmel3.7.0=/usr/src/gcc-avr-1:7.3.0+Atmel3.7.0-1' 'CFLAGS_FOR_BUILD=-g -O2' CPPFLAGS='-Wdate-time -D_FORTIFY_SOURCE=2' CPPFLAGS_FOR_BUILD= CXXFLAGS='-g -O2 -ffile-prefix-map=/build/gcc-avr-SuyGv2/gcc-avr-7.3.0+Atmel3.7.0=. -flto=auto -ffat-lto-objects -fstack-protector-strong -fstack-clash-protection -Wformat  -mbranch-protection=standard -fdebug-prefix-map=/build/gcc-avr-SuyGv2/gcc-avr-7.3.0+Atmel3.7.0=/usr/src/gcc-avr-1:7.3.0+Atmel3.7.0-1' 'CXXFLAGS_FOR_BUILD=-g -O2' DFLAGS=-frelease DFLAGS_FOR_BUILD=-frelease FCFLAGS='-g -O2 -ffile-prefix-map=/build/gcc-avr-SuyGv2/gcc-avr-7.3.0+Atmel3.7.0=. -flto=auto -ffat-lto-objects -fstack-protector-strong -fstack-clash-protection -mbranch-protection=standard -fdebug-prefix-map=/build/gcc-avr-SuyGv2/gcc-avr-7.3.0+Atmel3.7.0=/usr/src/gcc-avr-1:7.3.0+Atmel3.7.0-1' 'FCFLAGS_FOR_BUILD=-g -O2' FFLAGS='-g -O2 -ffile-prefix-map=/build/gcc-avr-SuyGv2/gcc-avr-7.3.0+Atmel3.7.0=. -flto=auto -ffat-lto-objects -fstack-protector-strong -fstack-clash-protection -mbranch-protection=standard -fdebug-prefix-map=/build/gcc-avr-SuyGv2/gcc-avr-7.3.0+Atmel3.7.0=/usr/src/gcc-avr-1:7.3.0+Atmel3.7.0-1' 'FFLAGS_FOR_BUILD=-g -O2' GCJFLAGS=-fdebug-prefix-map=/build/gcc-avr-SuyGv2/gcc-avr-7.3.0+Atmel3.7.0=/usr/src/gcc-avr-1:7.3.0+Atmel3.7.0-1 LDFLAGS='-Wl,-Bsymbolic-functions -flto=auto -ffat-lto-objects -Wl,-z,relro' LDFLAGS_FOR_BUILD= OBJCFLAGS='-g -O2 -ffile-prefix-map=/build/gcc-avr-SuyGv2/gcc-avr-7.3.0+Atmel3.7.0=. -flto=auto -ffat-lto-objects -fstack-protector-strong -fstack-clash-protection -Wformat  -mbranch-protection=standard -fdebug-prefix-map=/build/gcc-avr-SuyGv2/gcc-avr-7.3.0+Atmel3.7.0=/usr/src/gcc-avr-1:7.3.0+Atmel3.7.0-1' 'OBJCFLAGS_FOR_BUILD=-g -O2' OBJCXXFLAGS='-g -O2 -ffile-prefix-map=/build/gcc-avr-SuyGv2/gcc-avr-7.3.0+Atmel3.7.0=. -flto=auto -ffat-lto-objects -fstack-protector-strong -fstack-clash-protection -Wformat  -mbranch-protection=standard -fdebug-prefix-map=/build/gcc-avr-SuyGv2/gcc-avr-7.3.0+Atmel3.7.0=/usr/src/gcc-avr-1:7.3.0+Atmel3.7.0-1' 'OBJCXXFLAGS_FOR_BUILD=-g -O2'
Thread model: single
gcc version 7.3.0 (GCC) 

@jfrimmel
Copy link
Contributor

jfrimmel commented Apr 1, 2025

I've reproduced your issue locally with an avr-gcc version 14.2.0, so I can confirm the issue on the nightly you use even with different avr-gcc-versions.


I've tried it with a more recent nightly (nightly-2025-03-11 to be precise) and the issue seems to be resolved there:

$ cd /tmp/
$ curl -L https://github.com/user-attachments/files/19526406/avr-f64-fail.zip -o avr-f64-fail.zip
$ unzip -q avr-f64-fail.zip
$ cd avr-f64-fail/
$ sed -i 's/"nightly-.*"/"nightly-2025-03-11"/' rust-toolchain.toml
$ RUSTFLAGS='-Ctarget-cpu=atmega2560' cargo build -q
$ avr-size target/avr-atmega2560/debug/avr-f64-fail.elf
   text    data     bss     dec     hex filename
   4170       0       1    4171    104b target/avr-atmega2560/debug/avr-f64-fail.elf

I've run a little bisection (just compilation without running of the code, as I don't have access to an ATmega2560 at the moment) and came to the conclusion, that a compiler after 2024-07-30 should work (which contains #125016, which updates compiler-builtins).

(manual) bisection logs
  • 2024-03-22 bad (original error)
  • 2024-03-23 good
  • 2024-06-22 bad (multiple definition of __addsf3 due to compiler-builtins)
  • 2024-07-07 bad (multiple definition of __addsf3 due to compiler-builtins)
  • 2024-07-21 bad (multiple definition of __addsf3 due to compiler-builtins)
  • 2024-07-25 bad (multiple definition of __addsf3 due to compiler-builtins)
  • 2024-07-28 bad (multiple definition of __addsf3 due to compiler-builtins)
  • 2024-07-29 bad (multiple definition of __addsf3 due to compiler-builtins)
  • 2024-07-30 good
  • 2024-08-01 good
  • 2024-09-22 good
  • 2025-03-11 good
  • 2025-04-01 good (today)

Is there a reason for using this old compiler?

@karip
Copy link
Author

karip commented Apr 1, 2025

There's no reason for using 2024-03-21. It was part of the template, so I ended up using it.

I tried your commands on two different Ubuntus, but I'm still getting the same linking error.

Here's what rustc --version --verbose prints out after running your commands:

rustc 1.87.0-nightly (9fb94b32d 2025-03-10)
binary: rustc
commit-hash: 9fb94b32df38073bf63d009df77ed10cb1c989d0
commit-date: 2025-03-10
host: aarch64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.0

The other Ubuntu is running on x86_64 so it prints out the host line as host: x86_64-unknown-linux-gnu.

I also tried "nightly-2025-04-01", but it gives the same linking error.

Maybe I am missing some system libraries? I have run sudo apt install avr-libc gcc-avr pkg-config avrdude libudev-dev build-essential as suggested by avr-hal.

@karip
Copy link
Author

karip commented Apr 1, 2025

Actually, the linking error isn't exactly the same. It is also complaining arith.rs:762: undefined reference to '__adddf3' when building with nightly-2025-03-10 or nightly-2025-04-01:

$ RUSTFLAGS='-Ctarget-cpu=atmega2560' cargo build -q
info: syncing channel updates for 'nightly-2025-03-11-aarch64-unknown-linux-gnu'
info: latest update on 2025-03-11, rust version 1.87.0-nightly (9fb94b32d 2025-03-10)
info: downloading component 'cargo'
info: downloading component 'rust-src'
info: downloading component 'rust-std'
info: downloading component 'rustc'
info: installing component 'cargo'
info: installing component 'rust-src'
info: installing component 'rust-std'
info: installing component 'rustc'
error: linking with `avr-gcc` failed: exit status: 1
  |
  = note:  "avr-gcc" "-mmcu=atmega2560" "-Wl,--as-needed,--print-memory-usage" "/tmp/rustcGdU0rf/symbols.o" "<1 object files omitted>" "-Wl,--as-needed" "-Wl,-Bstatic" "/home/user.linux/rust/newdd/avr-f64-fail/target/avr-atmega2560/debug/deps/{libcompiler_builtins-0b4e8ec1b022a6c8.rlib}.rlib" "-L" "/tmp/rustcGdU0rf/raw-dylibs" "-Wl,-Bdynamic" "-lgcc" "-Wl,-z,noexecstack" "-o" "/home/user.linux/rust/newdd/avr-f64-fail/target/avr-atmega2560/debug/deps/avr_f64_fail-e4d8ff0aa406eb03.elf" "-Wl,--gc-sections" "-no-pie" "-mmcu=atmega2560"
  = note: some arguments are omitted. use `--verbose` to show all linker arguments
  = note: /home/user.linux/rust/newdd/avr-f64-fail/target/avr-atmega2560/debug/deps/avr_f64_fail-e4d8ff0aa406eb03.panic_halt-f014238b31be2ac3.panic_halt.ace25d9be84e6998-cgu.0.rcgu.o.rcgu.o: In function `<f64 as core::ops::arith::AddAssign>::add_assign':
          /home/user.linux/.rustup/toolchains/nightly-2025-03-11-aarch64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/arith.rs:762: undefined reference to `__adddf3'
          /home/user.linux/rust/newdd/avr-f64-fail/target/avr-atmega2560/debug/deps/avr_f64_fail-e4d8ff0aa406eb03.panic_halt-f014238b31be2ac3.panic_halt.ace25d9be84e6998-cgu.0.rcgu.o.rcgu.o: In function `avr_f64_fail::__avr_device_rt_main':
          /home/user.linux/rust/newdd/avr-f64-fail/src/main.rs:22: undefined reference to `__gtdf2'
          /home/user.linux/rust/newdd/avr-f64-fail/src/main.rs:22: undefined reference to `__gedf2'
          collect2: error: ld returned 1 exit status
          Memory region         Used Size  Region Size  %age Used
                      text:        2356 B       256 KB      0.90%
                      data:          75 B         8 KB      0.92%
                    eeprom:          0 GB         4 KB      0.00%
                      fuse:          0 GB          3 B      0.00%
                      lock:          0 GB         1 KB      0.00%
                 signature:          0 GB         1 KB      0.00%
           user_signatures:          0 GB         1 KB      0.00%
          
  = 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#rustc-link-lib)

error: could not compile `avr-f64-fail` (bin "avr-f64-fail") due to 1 previous error

@karip
Copy link
Author

karip commented Apr 4, 2025

Thanks for pointing out that I was using old compilers!

I was able to get the code linking by using avr-gcc version 14.2.0. The older avr-gcc 7.3.0 wasn't good enough. I had assumed that Ubuntu 24.04 would include a fairly recent avr-gcc, but it still ships with avr-gcc 7.3.0. I installed Ubuntu 25.04, which includes avr-gcc 14.2.0 and then everything started linking. So, if anyone is having similar AVR linking issues, try with avr-gcc 14.2.0 or later and Rust 1.87.0 or later.

I'll close this issue, because it is now resolved for me.

@karip karip closed this as completed Apr 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-floating-point Area: Floating point numbers and arithmetic A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. O-AVR Target: AVR processors (ATtiny, ATmega, etc.) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants