<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Consolas;
panose-1:2 11 6 9 2 2 4 3 2 4;}
@font-face
{font-family:inherit;
panose-1:0 0 0 0 0 0 0 0 0 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
code
{mso-style-priority:99;
font-family:"Courier New";}
pre
{mso-style-priority:99;
mso-style-link:"HTML Preformatted Char";
margin:0in;
font-size:10.0pt;
font-family:"Courier New";}
span.HTMLPreformattedChar
{mso-style-name:"HTML Preformatted Char";
mso-style-priority:99;
mso-style-link:"HTML Preformatted";
font-family:Consolas;}
span.gmail-hljs-string
{mso-style-name:gmail-hljs-string;}
span.gmail-hljs-comment
{mso-style-name:gmail-hljs-comment;}
span.gmail-hljs-keyword
{mso-style-name:gmail-hljs-keyword;}
span.gmail-hljs-builtin
{mso-style-name:gmail-hljs-built_in;}
span.EmailStyle27
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
/* List Definitions */
@list l0
{mso-list-id:979723525;
mso-list-type:hybrid;
mso-list-template-ids:885936620 67698703 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;}
@list l0:level1
{mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;}
@list l0:level2
{mso-level-number-format:alpha-lower;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;}
@list l0:level3
{mso-level-number-format:roman-lower;
mso-level-tab-stop:none;
mso-level-number-position:right;
text-indent:-9.0pt;}
@list l0:level4
{mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;}
@list l0:level5
{mso-level-number-format:alpha-lower;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;}
@list l0:level6
{mso-level-number-format:roman-lower;
mso-level-tab-stop:none;
mso-level-number-position:right;
text-indent:-9.0pt;}
@list l0:level7
{mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;}
@list l0:level8
{mso-level-number-format:alpha-lower;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;}
@list l0:level9
{mso-level-number-format:roman-lower;
mso-level-tab-stop:none;
mso-level-number-position:right;
text-indent:-9.0pt;}
ol
{margin-bottom:0in;}
ul
{margin-bottom:0in;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal">Some small suggestions:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">LTO makes everything more complicated; make sure things work without -flto before trying LTO.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">A “.got” section shows up when an object file compiled with -fPIC/-fPIE accesses an external global variable or constant. You can check if an object file contains a relocation like this with objdump -r (something like R_ARM_GOT_PREL).
For a static link, the linker should fix it to point to the right address. If the linker isn’t computing the correct address, my best guess is that something is wrong with the linker script. Are you setting the address of flash/RAM correctly? Is the difference
between the address in the GOT and the actual address related to the size of some section? (The consequences of getting this wrong might not be immediately obvious if the code is mostly using pc-relative addressing.)<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">If you have a working build with gcc, you might want to compare to see what, exactly, is different. Check the link line with “-###”; are you using the same linker? The same libraries?<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">-Eli<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt">
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b>From:</b> llvm-dev <llvm-dev-bounces@lists.llvm.org> <b>On Behalf Of
</b>Kacper Kowalski via llvm-dev<br>
<b>Sent:</b> Saturday, November 20, 2021 6:22 AM<br>
<b>To:</b> llvm-dev <llvm-dev@lists.llvm.org><br>
<b>Subject:</b> [llvm-dev] Exceptions not working when cross compiling for ARM Cortex M4 with clang and precompiled libraries from ARM GNU GCC Toolchain<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p align="center" style="text-align:center"><strong><span style="font-size:10.5pt;font-family:"Arial",sans-serif;color:black;background:yellow">WARNING:</span></strong><span style="font-size:10.5pt;font-family:"Arial",sans-serif;color:black;background:yellow">
This email originated from outside of Qualcomm. Please be wary of any links or attachments, and do not enable macros.</span><o:p></o:p></p>
<div>
<div>
<p class="MsoNormal">Hello!<o:p></o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">I have been struggling with enabling exceptions for the Clang build, when cross compiling using precompiled libraries from ARM GNU GCC Toolchain.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt">LLVM version: 13.0.0<br>
CMake version: 3.21.3<br>
targeted CPU: STM32L432KC, ARM Cortex M4<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">The C++ flags:<o:p></o:p></p>
</div>
<div>
<pre style="vertical-align:baseline;font-variant-numeric:inherit;font-variant-east-asian:inherit;font-stretch:inherit;box-sizing:inherit;max-height:600px;border-radius:5px;overflow:auto"><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">-mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16<o:p></o:p></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">-nodefaultlibs<o:p></o:p></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">--sysroot=${ARM_GNU_TOOLCHAIN_PATH}/arm-none-eabi<o:p></o:p></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">-flto<o:p></o:p></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">-fdata-sections -ffunction-sections<o:p></o:p></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in"># For <iostream>, <string>, ...<o:p></o:p></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">-isystem </span></code><span class="gmail-hljs-string"><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">"${ARM_GNU_TOOLCHAIN_PATH}/arm-none-eabi/include/c++/${ARM_GNU_TOOLCHAIN_GCC_VERSION}/"</span></span><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in"> <o:p></o:p></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in"># For <bits</span></code><span class="gmail-hljs-comment"><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">/*>, ...<o:p></o:p></span></span></pre>
<pre style="vertical-align:baseline"><span class="gmail-hljs-comment"><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">-isystem "${ARM_GNU_TOOLCHAIN_PATH}/arm-none-eabi/include/c++/${ARM_GNU_TOOLCHAIN_GCC_VERSION}/arm-none-eabi/thumb/v7e-m+fp/hard/"<o:p></o:p></span></span></pre>
<pre style="vertical-align:baseline"><span class="gmail-hljs-comment"><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">-fexceptions<o:p></o:p></span></span></pre>
<pre style="vertical-align:baseline"><span class="gmail-hljs-comment"><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">-g</span></span><o:p></o:p></pre>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">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">https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads</a><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">The linker flags:<br>
<br>
<o:p></o:p></p>
<pre style="vertical-align:baseline"><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">-mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16<o:p></o:p></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">-nodefaultlibs<o:p></o:p></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">--sysroot=${ARM_GNU_TOOLCHAIN_PATH}/arm-none-eabi<o:p></o:p></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">-flto<o:p></o:p></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">-fdata-sections -ffunction-sections<o:p></o:p></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">-Wl,--gc-sections<o:p></o:p></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">-g<o:p></o:p></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">-flto<o:p></o:p></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">-fexceptions<o:p></o:p></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in"># Path to standard libraries: libc, libm, ...<o:p></o:p></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">-</span></code><span class="gmail-hljs-string"><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">L"${ARM_GNU_TOOLCHAIN_PATH}/arm-none-eabi/lib/thumb/v7e-m+fp/hard/"</span></span><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in"><o:p></o:p></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in"># Path to libgcc<o:p></o:p></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">-</span></code><span class="gmail-hljs-string"><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">L"${ARM_GNU_TOOLCHAIN_PATH}/lib/gcc/arm-none-eabi/${ARM_GNU_TOOLCHAIN_GCC_VERSION}/thumb/v7e-m+fp/hard/"</span></span><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in"><o:p></o:p></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">-lc -lm -lnosys -lstdc++ -lgcc</span></code><span class="gmail-hljs-string"><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">")<o:p></o:p></span></span></pre>
<p class="MsoNormal"><br>
I use custom linker script and custom startup file to compile the firmware properly. I attach them both.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">This code:<o:p></o:p></p>
</div>
<div>
<pre style="vertical-align:baseline;font-variant-numeric:inherit;font-variant-east-asian:inherit;font-stretch:inherit;box-sizing:inherit;max-height:600px;border-radius:5px;overflow:auto"><span class="gmail-hljs-keyword"><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">try</span></span><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in"><o:p></o:p></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">{<o:p></o:p></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in"> </span></code><span class="gmail-hljs-keyword"><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">throw</span></span><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in"> std::runtime_error{</span></code><span class="gmail-hljs-string"><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">"Some error!"</span></span><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">};<o:p></o:p></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">} </span></code><span class="gmail-hljs-builtin"><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">catch</span></span><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in"> (</span></code><span class="gmail-hljs-keyword"><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">const</span></span><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in"> std::exception&e)<o:p></o:p></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">{<o:p></o:p></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in"> </span></code><span class="gmail-hljs-builtin"><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">printf</span></span><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">(</span></code><span class="gmail-hljs-string"><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">"Error: %s\r\n"</span></span><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">, e.</span></code><span class="gmail-hljs-builtin"><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">what</span></span><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">());<o:p></o:p></span></code></pre>
<pre style="vertical-align:baseline"><code><span style="font-family:"inherit",serif;border:none windowtext 1.0pt;padding:0in">}</span></code><o:p></o:p></pre>
</div>
<div>
<p class="MsoNormal">Will not work for the Clang build. It will crash. It works normally with the GCC build.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">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.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">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:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">vtable for __cxxabiv1::__class_type_info + 8 in section .rodata<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">I know that, because I have scrapped it from the GCC build (performed with the downloaded toolchain), for which the exceptions work properly.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">The code crashes in the __cxa_type_match:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">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<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">According to the comments in the assembly:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt">r4 contains the address to the got section, which is 0x20000000. Under 0x20000000 lies the address to the random ASCII rodata. It means that:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> ldr r6, [r3, #16]<o:p></o:p></p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal">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.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">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:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">[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<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">I attach section dump and disassembly of the program as well.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Am I missing some compiler or linker flag?<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">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.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Any help would be appreciated.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Kind regards,<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">Kacper Kowalski<o:p></o:p></p>
</div>
</div>
</div>
</div>
</div>
</body>
</html>