clang provides arguments to avr-gcc in a wrong order. This results in linking errors undefined reference to .... This patch fixes this.
Bug on GitHub: https://github.com/llvm/llvm-project/issues/56409
Differential D127501
[AVR] Fixed broken linking using `avr-gcc`. KOLANICH on Jun 10 2022, 8:08 AM. Authored by
Details
clang provides arguments to avr-gcc in a wrong order. This results in linking errors undefined reference to .... This patch fixes this. Bug on GitHub: https://github.com/llvm/llvm-project/issues/56409
Diff Detail Event TimelineComment Actions Could you please give more details about the error you got ?
Comment Actions
Kubuntu 21.10
7.3.0-atmel3.6.1-arduino7, it is the latest one that Arduino IDE has updated. Its version number matches the one coming from Atmel Microchip website, but it has some patches by Arduino applied.
Not installed, just unpacked, it is portable.
The ones I have created. I'm not ready to share them they are in pretty dirty state now, but the issue is fundamental and should be reproducible on any project structured as a typical Arduino project and using its standard library and runtime.
I have modified the well-known set of CMake toolchain files, here is my fork: https://github.com/KOLANICH/Arduino-CMake-Toolchain/tree/clang The failed command was bash /usr/lib/llvm-15/bin/clang++ --target=avr --sysroot=./arduino_ide/1.8.19/hardware/tools/avr -fno-rtti -mmcu=atmega2560 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -O3 -DNDEBUG -mmcu=atmega2560 -lm -fuse-linker-plugin -v --ld-path="/usr/lib/llvm-15/bin/ld.lld" CMakeFiles/stepper_control.dir/src/main.cpp.obj -o stepper_control.elf lib_arduino_lib_core.a lib_arduino_lib_Stepper.a lib_arduino_lib_core.a (the paths have been edited, the prefix was replaced to not disclose projects location in the fs) It results in clang: warning: argument unused during compilation: '--ld-path=/usr/lib/llvm-15/bin/ld.lld' [-Wunused-command-line-argument] It seems that lld cannot link for AVR target and it is an another big issue. bash "./arduino_ide/1.8.19/hardware/tools/avr/lib/gcc/avr/7.3.0/../../../../bin/avr-ld" -lm CMakeFiles/stepper_control.dir/src/main.cpp.obj lib_arduino_lib_core.a lib_arduino_lib_Stepper.a lib_arduino_lib_core.a -o stepper_control.elf --gc-sections -L./arduino_ide/1.8.19/hardware/tools/avr/lib/gcc/avr/7.3.0/../../../../avr/lib/avr6 -L./arduino_ide/1.8.19/hardware/tools/avr/lib/gcc/avr/7.3.0/avr6 -Tdata=0x800200 --start-group -l:crtatmega2560.o -lgcc -lm -lc -latmega2560 --end-group -mavr6 The actual command - it always calls avr-ld and I have no means to override it. If we put our object files to the group in the end, it links succesfully (if -flto is not enabled. Offtop: if it is enabled, the linker fails to link llvm bitcode, I have tried to link all the bitcode + native code into a joint object file, then compile it using llc, then link using avr-ld and got other undefined reference issues, it seems the optimizer eats some symbols, and using -O0 makes it run out of registers). To be honest, if I had means to override it I'd have just written a shell script instead of taking the burden of compiling clang from source. ./arduino_ide/1.8.19/hardware/tools/avr/lib/gcc/avr/7.3.0/../../../../avr/lib/avr6/crtatmega2560.o:(.init9+0x0): undefined reference to `main' main is defined within lib_arduino_lib_core.a, which is compiled by the CMake scripts for each target, and it uses setup and loop that are defined in CMakeFiles/stepper_control.dir/src/main.cpp.obj, which is compiled from my code. A typical Arduino project is structured like this.
It should have been already tested, we just changed the place where the user's files are placed into the command line.
I think it is more error-prone than having a lambda. I expect the optimizer to be smart enough to inline it. Comment Actions
Comment Actions Here is the test project. https://github.com/KOLANICH/barebones_arduino_mega_2560_clang_test |
I think directly using AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA); twice is fine, we need not define a lamnda function.