Closed Javier-varez closed 3 years ago
It looks like this is happening because somehow clang-12
links against the wrong library. below is a snippet from the map file of the elf file generated using clang-12:
8000000 8000000 b4 4 .root_section
8000000 8000000 40 4 _deps/cortex-m_startup-build/libcortex-m_startup.a(startup.cpp.o):(.exception_table)
8000000 8000000 40 1 g_exception_table
8000040 8000040 10 4 /home/javier/Documents/devtools/gcc-arm-none-eabi-10-2020-q4-major/bin/../arm-none-eabi/lib/libc_nano.a(lib_a-aeabi_memcpy.o):(.text.__aeabi_memcpy)
8000040 8000040 0 1 $a
8000040 8000040 10 1 __aeabi_memcpy
8000040 8000040 10 1 __aeabi_memcpy8
8000040 8000040 10 1 __aeabi_memcpy4
Compared to an elf file built with clang-11
it is quite clear that the linked library is not correct:
8000000 8000000 80 4 .root_section
8000000 8000000 40 4 _deps/cortex-m_startup-build/libcortex-m_startup.a(startup.cpp.o):(.exception_table)
8000000 8000000 40 1 g_exception_table
8000040 8000040 4 2 /home/javier/Documents/devtools/gcc-arm-none-eabi-10-2020-q4-major/bin/../arm-none-eabi/lib/thumb/v7-m/nofp/libc_nano.a(lib_a-aeabi_memcpy.o):(.text.__aeabi_memcpy)
8000040 8000040 0 1 $t
8000041 8000041 4 1 __aeabi_memcpy
8000041 8000041 4 1 __aeabi_memcpy8
8000041 8000041 4 1 __aeabi_memcpy4
It looks like clang-12
does not properly use the multilib settings to search for libs in lib/thumb/v7-m/nofp
and instead links directly against the libc_nano library located in lib
.
It turns out that clang-12
adds the lib
path to the library search paths based on the --sysroot
option. The invocation of lld
is:
"/usr/bin/ld.lld-12" "--gc-sections" "--gc-sections" "CMakeFiles/test.dir/test.cpp.o" "/lib/thumb/v7-m/nofp:/home/javier/Documents/devtools/gcc-arm-none-eabi-10-2020-q4-major/bin/../lib/gcc/arm-none-eabi/10.2.1/thumb/v7-m/nofp" "-lnosys" "-lc_nano" "-lstdc++_nano" "-lgcc" "-lnosys" "-lc_nano" "-lstdc++_nano" "-lgcc" "-Bstatic" "-L/usr/lib/llvm-12/lib/clang/12.0.1/lib/baremetal" "-L/home/javier/Documents/devtools/gcc-arm-none-eabi-10-2020-q4-major/bin/../arm-none-eabi/lib" "-L/home/javier/Documents/devtools/gcc-arm-none-eabi-10-2020-q4-major/bin/../arm-none-eabi/lib/thumb/v7-m/nofp" "-L/home/javier/Documents/devtools/gcc-arm-none-eabi-10-2020-q4-major/bin/../lib/gcc/arm-none-eabi/10.2.1/thumb/v7-m/nofp" "-o" "test"
When building with clang-12 the compiler actually links against an ARM std library instead of a Thumb standard library. Therefore, when running on the Cortex-M3, the processor immediately triggers a hardfault when it hits a blx instruction transitioning to the ARM execution mode.
This seems to only happen with clang-12, but it is not reproducible with clang-10 or clang-11.
The following snippet shows the blx call to ARM code:
Instead, this should look like this: