<div dir="ltr">Hello!<br><div><br></div><div>I have been struggling with enabling exceptions for the Clang build, when cross compiling using precompiled libraries from ARM GNU GCC Toolchain.</div><div><br></div><div>LLVM version: 13.0.0<br>CMake version: 3.21.3<br>targeted CPU: STM32L432KC, ARM Cortex M4<br><br></div><div>The C++ flags:</div><div><pre style="white-space:pre-wrap;margin-top:0px;padding:12px;border:0px;font-variant-numeric:inherit;font-variant-east-asian:inherit;font-stretch:inherit;line-height:1.30769;font-size:13px;vertical-align:baseline;box-sizing:inherit;width:auto;max-height:600px;overflow:auto;border-radius:5px"><code style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit;background-color:transparent;white-space:inherit">-mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16
-nodefaultlibs
--sysroot=${ARM_GNU_TOOLCHAIN_PATH}/arm-none-eabi
-flto
-fdata-sections -ffunction-sections
# For <iostream>, <string>, ...
-isystem <span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit">"${ARM_GNU_TOOLCHAIN_PATH}/arm-none-eabi/include/c++/${ARM_GNU_TOOLCHAIN_GCC_VERSION}/"</span>
# For <bits<span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit">/*>, ...
-isystem "${ARM_GNU_TOOLCHAIN_PATH}/arm-none-eabi/include/c++/${ARM_GNU_TOOLCHAIN_GCC_VERSION}/arm-none-eabi/thumb/v7e-m+fp/hard/"
-fexceptions
-g</span></code></pre><br></div><div>ARM_GNU_TOOLCHAIN_PATH is the root path to the mentioned ARM GNU GCC Toolchain. ARM_GNU_TOOLCHAIN_GCC_VERSION is equal to 10.3.1. One can download it here: <a href="https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads" target="_blank">https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads</a></div><div><br></div><div>The linker flags:<br><pre style="white-space:pre-wrap;margin-top:0px;padding:12px;border:0px;font-variant-numeric:inherit;font-variant-east-asian:inherit;font-stretch:inherit;line-height:1.30769;font-size:13px;vertical-align:baseline;box-sizing:inherit;width:auto;max-height:600px;overflow:auto;border-radius:5px"><code style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit;background-color:transparent;white-space:inherit">-mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16
-nodefaultlibs
--sysroot=${ARM_GNU_TOOLCHAIN_PATH}/arm-none-eabi
-flto
-fdata-sections -ffunction-sections
-Wl,--gc-sections
-g
-flto
-fexceptions
# Path to standard libraries: libc, libm, ...
-<span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit">L"${ARM_GNU_TOOLCHAIN_PATH}/arm-none-eabi/lib/thumb/v7e-m+fp/hard/"</span>
# Path to libgcc
-<span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit">L"${ARM_GNU_TOOLCHAIN_PATH}/lib/gcc/arm-none-eabi/${ARM_GNU_TOOLCHAIN_GCC_VERSION}/thumb/v7e-m+fp/hard/"</span>
-lc -lm -lnosys -lstdc++ -lgcc<span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit">")
</span></code></pre><br>I use custom linker script and custom startup file to compile the firmware properly. I attach them both.</div><div><br></div><div>This code:</div><div><pre style="white-space:pre-wrap;margin-top:0px;padding:12px;border:0px;font-variant-numeric:inherit;font-variant-east-asian:inherit;font-stretch:inherit;line-height:1.30769;font-size:13px;vertical-align:baseline;box-sizing:inherit;width:auto;max-height:600px;overflow:auto;border-radius:5px"><code style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit;background-color:transparent;white-space:inherit"><span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit">try</span>
{
<span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit">throw</span> std::runtime_error{<span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit">"Some error!"</span>};
} <span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit">catch</span> (<span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit">const</span> std::exception&e)
{
<span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit">printf</span>(<span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit">"Error: %s\r\n"</span>, e.<span style="margin:0px;padding:0px;border:0px;font-style:inherit;font-variant:inherit;font-weight:inherit;font-stretch:inherit;line-height:inherit;font-family:inherit;vertical-align:baseline;box-sizing:inherit">what</span>());
}</code></pre></div><div>Will not work for the Clang build. It will crash. It works normally with the GCC build.</div><div><br></div><div>The problem is that clang generates ".got" section, which it inputs automatically, without being "said", to RAM. I have included it in the ".data" section, because otherwise it breaks the global variable initialization within the startup code. GOT section is normally related to dynamic symbol resolution. Since I link everything statically, I don't understand why this section shows up there.</div><div><br></div><div>The mentioned ".got" section contains one variable, which is an address pointing to some place in "rodata". Unfortunately, this address is wrong. It points to some random rodata, where some ASCII strings are held. It shall point to the symbol:</div><div>vtable for __cxxabiv1::__class_type_info + 8 in section .rodata</div><div>I know that, because I have scrapped it from the GCC build (performed with the downloaded toolchain), for which the exceptions work properly.</div><div><br></div><div>The code crashes in the __cxa_type_match:</div><div>0x080096d6 in __cxa_type_match ()<br>─── Assembly ─────────────<br> 0x080096d6 ? ldr r3, [r4, #0] # r4 holds the address of "got"<br> 0x080096d8 ? mov r1, r6<br> 0x080096da ? mov r0, r4<br> 0x080096dc ? ldr r6, [r3, #16] # Loaded from the address pointed by (r3 + 16)<br> 0x080096de ? add r2, sp, #4<br> 0x080096e0 ? movs r3, #1<br> 0x080096e2 ? blx r6 # This line crashes<br></div><div><br></div><div>According to the comments in the assembly:</div><div>r4 contains the address to the got section, which is 0x20000000. Under 0x20000000 lies the address to the random ASCII rodata. It means that:<br><br></div><div> ldr r6, [r3, #16]<br></div><br><div>Loads the ASCII characters to r6. Then we perform the blx jump with contents of r6, which is wrong, because ASCII data is no address.</div><div><br></div><div>Using gdb I am able to write data to RAM by hand. I have found the address of the symbol vtable for __cxxabiv1::__class_type_info + 8 in section .rodata, which is 0x8019004. Then I wrote this address under 0x20000000 (the "got" section). Unfortunately, the program didn't end up as expected - within the "catch" block, but went to "_exit", through "std::terminate", what means that no suitable handler has been found during stack unwinding. The stack trace from that attempt:</div><div><br></div><div>[0] from 0x08008df0 in _exit<br>[1] from 0x08008d42 in abort<br>[2] from 0x08009d08 in __gnu_cxx::__verbose_terminate_handler()<br>[3] from 0x08008cc2 in __cxxabiv1::__terminate(void (*)())<br>[4] from 0x08008cf2 in std::terminate()<br>[5] from 0x08009c08 in __cxa_throw<br>[6] from 0x0800fedc in test_throws_uncaught_exception()+52 at /home/kacper/Workspace/Aura/tests/device/test_serial_logger/test_logs_uncaught_exception.cpp:11<br>[7] from 0x08014ff0 in run_test(void (*)(), char const*, unsigned int)+92 at test_logs_uncaught_exception_runner.cpp:62<br>[8] from 0x08014f8e in test_main()+12 at test_logs_uncaught_exception_runner.cpp:79<br>[9] from 0x08014440 in app_main::$_0::operator()() const+16 at /home/kacper/Workspace/Aura/tests/device/setup/test_runner.cpp:24<br></div><div><br></div><div>I attach section dump and disassembly of the program as well.</div><div><br></div><div>Am I missing some compiler or linker flag?</div><div><br></div><div>I am fighting with it for a week at least. I was hoping that I will be able to push my project with clang build as the main build, but I guess I will have to fall back to GCC instead.</div><div><br></div><div>Any help would be appreciated.</div><div><br></div><div>P.S. The startup code and the linker script are attached. The rest can be downloaded under the following links due to size limit.</div><div>The dissasembly: <a href="https://drive.google.com/file/d/1DYfwBK4lq9g8bIfTnwZrixUnXwn_asJg/view?usp=sharing">https://drive.google.com/file/d/1DYfwBK4lq9g8bIfTnwZrixUnXwn_asJg/view?usp=sharing</a></div><div>The section dump: <a href="https://drive.google.com/file/d/1pGABAexwqAsBUnmFH1uDaPkaspwoVT4u/view?usp=sharing">https://drive.google.com/file/d/1pGABAexwqAsBUnmFH1uDaPkaspwoVT4u/view?usp=sharing</a></div><div><br></div><div>Kind regards,</div><div>Kacper Kowalski</div></div>