<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:Wingdings;
panose-1:5 0 0 0 0 0 0 0 0 0;}
@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;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
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:0cm;
font-size:10.0pt;
font-family:"Courier New";}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
{mso-style-priority:34;
margin-top:0cm;
margin-right:0cm;
margin-bottom:0cm;
margin-left:36.0pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
span.HTMLPreformattedChar
{mso-style-name:"HTML Preformatted Char";
mso-style-priority:99;
mso-style-link:"HTML Preformatted";
font-family:Consolas;
mso-fareast-language:EN-GB;}
span.EmailStyle23
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri",sans-serif;
mso-fareast-language:EN-US;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
{page:WordSection1;}
/* List Definitions */
@list l0
{mso-list-id:250507066;
mso-list-type:hybrid;
mso-list-template-ids:673765338 -915625820 134807555 134807557 134807553 134807555 134807557 134807553 134807555 134807557;}
@list l0:level1
{mso-level-number-format:bullet;
mso-level-text:;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-18.0pt;
font-family:Symbol;
mso-fareast-font-family:Calibri;
mso-bidi-font-family:"Times New Roman";}
@list l0:level2
{mso-level-number-format:bullet;
mso-level-text:o;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-18.0pt;
font-family:"Courier New";}
@list l0:level3
{mso-level-number-format:bullet;
mso-level-text:;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-18.0pt;
font-family:Wingdings;}
@list l0:level4
{mso-level-number-format:bullet;
mso-level-text:;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-18.0pt;
font-family:Symbol;}
@list l0:level5
{mso-level-number-format:bullet;
mso-level-text:o;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-18.0pt;
font-family:"Courier New";}
@list l0:level6
{mso-level-number-format:bullet;
mso-level-text:;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-18.0pt;
font-family:Wingdings;}
@list l0:level7
{mso-level-number-format:bullet;
mso-level-text:;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-18.0pt;
font-family:Symbol;}
@list l0:level8
{mso-level-number-format:bullet;
mso-level-text:o;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-18.0pt;
font-family:"Courier New";}
@list l0:level9
{mso-level-number-format:bullet;
mso-level-text:;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-18.0pt;
font-family:Wingdings;}
@list l1
{mso-list-id:1744065175;
mso-list-type:hybrid;
mso-list-template-ids:-1209861404 1980810974 134807577 134807579 134807567 134807577 134807579 134807567 134807577 134807579;}
@list l1:level1
{mso-level-text:"%1\.\)";
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-18.0pt;}
@list l1:level2
{mso-level-number-format:alpha-lower;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-18.0pt;}
@list l1:level3
{mso-level-number-format:roman-lower;
mso-level-tab-stop:none;
mso-level-number-position:right;
text-indent:-9.0pt;}
@list l1:level4
{mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-18.0pt;}
@list l1:level5
{mso-level-number-format:alpha-lower;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-18.0pt;}
@list l1:level6
{mso-level-number-format:roman-lower;
mso-level-tab-stop:none;
mso-level-number-position:right;
text-indent:-9.0pt;}
@list l1:level7
{mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-18.0pt;}
@list l1:level8
{mso-level-number-format:alpha-lower;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-18.0pt;}
@list l1:level9
{mso-level-number-format:roman-lower;
mso-level-tab-stop:none;
mso-level-number-position:right;
text-indent:-9.0pt;}
@list l2
{mso-list-id:1855877405;
mso-list-type:hybrid;
mso-list-template-ids:-1736384394 73566854 134807555 134807557 134807553 134807555 134807557 134807553 134807555 134807557;}
@list l2:level1
{mso-level-start-at:0;
mso-level-number-format:bullet;
mso-level-text:;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-18.0pt;
font-family:Symbol;
mso-fareast-font-family:Calibri;
mso-bidi-font-family:"Times New Roman";}
@list l2:level2
{mso-level-number-format:bullet;
mso-level-text:o;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-18.0pt;
font-family:"Courier New";}
@list l2:level3
{mso-level-number-format:bullet;
mso-level-text:;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-18.0pt;
font-family:Wingdings;}
@list l2:level4
{mso-level-number-format:bullet;
mso-level-text:;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-18.0pt;
font-family:Symbol;}
@list l2:level5
{mso-level-number-format:bullet;
mso-level-text:o;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-18.0pt;
font-family:"Courier New";}
@list l2:level6
{mso-level-number-format:bullet;
mso-level-text:;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-18.0pt;
font-family:Wingdings;}
@list l2:level7
{mso-level-number-format:bullet;
mso-level-text:;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-18.0pt;
font-family:Symbol;}
@list l2:level8
{mso-level-number-format:bullet;
mso-level-text:o;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-18.0pt;
font-family:"Courier New";}
@list l2:level9
{mso-level-number-format:bullet;
mso-level-text:;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-18.0pt;
font-family:Wingdings;}
ol
{margin-bottom:0cm;}
ul
{margin-bottom:0cm;}
--></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-GB" link="blue" vlink="purple" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">IIUC then LLD with libc++, libc++abi, and libunwind works, but no other combination of c++ libraries has worked?<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">Although I don’t have a source for it, my understanding is that it is not recommended to mix runtime support libraries. For example libc++abi needs to be matched with llvm libunwind and possibly
compiler-rt. The same goes for the GNU equivalents.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">Splitting things up the problem is likely to be one or more of:<o:p></o:p></span></p>
<ul style="margin-top:0cm" type="disc">
<li class="MsoListParagraph" style="margin-left:0cm;mso-list:l0 level1 lfo3"><span style="mso-fareast-language:EN-US">Code generation of exceptions in clang not being understood by GNU exception handling code<o:p></o:p></span></li><li class="MsoListParagraph" style="margin-left:0cm;mso-list:l0 level1 lfo3"><span style="mso-fareast-language:EN-US">LLD is unable to link GNU exception handling code correctly<o:p></o:p></span></li><li class="MsoListParagraph" style="margin-left:0cm;mso-list:l0 level1 lfo3"><span style="mso-fareast-language:EN-US">The combination of runtime libraries that you’ve chosen isn’t working together<o:p></o:p></span></li></ul>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">One possible experiment to see if it is LLD is to try compiling with clang but linking with the GNU linker. If that starts to work, then can you look to see what the differences are? LLD has a –reproduce
option that you can upload to bug-report, although you’ll have to wait for the Bugzilla migration to finish before doing that. If that doesn’t work then it is either some problem/incompatibility with the GNU runtime libraries or an incompatible set of libraries.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">There are some things that can be done to reduce the size of libc++ and libc++abi. The demangler in libc++abi is large and only used in error messages so it can be omitted with a few tweaks. Anything
that avoids bringing in locale, which is often not needed in embedded systems will also help.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">I’m still puzzled at why you are seeing so many GOT entries being created. As Eli mentioned the linker will create a GOT entry in response to specific relocation types like R_ARM_GOT_PREL. The compiler
shouldn’t be generating these for a non-PIC and non-PIE build. One possibility is that some later flag on the command line is overriding the -fno-pic, it could be the use of LTO as the code-generator may change the PIC setting from what has been used to make
the bitcode objects.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">That’s probably the best I can do without seeing the objects.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US">Peter<o:p></o:p></span></p>
<p class="MsoNormal"><span style="mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt">
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal"><b><span lang="EN-US">From:</span></b><span lang="EN-US"> Kacper Kowalski <kacper.s.kowalski@gmail.com>
<br>
<b>Sent:</b> 25 November 2021 09:41<br>
<b>To:</b> Peter Smith <Peter.Smith@arm.com><br>
<b>Cc:</b> Eli Friedman <efriedma@quicinc.com>; llvm-dev@lists.llvm.org<br>
<b>Subject:</b> Re: [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></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal">Hello Eli, Peter!<o:p></o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Thank you for the tips.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Peter,<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">1. I guess that the default option for orphan handling is "place", because the "got" section, which was initially not in the linker script, was put by the linker to the "data" section. When I have figured it out, I added such section to
the linker script:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm">
<p class="MsoNormal"> .got :<br>
{<br>
. = ALIGN(8);<br>
*(.got)<br>
*(.got*)<br>
. = ALIGN(8);<br>
} >FLASH<o:p></o:p></p>
</blockquote>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">As you can see, I put it in ROM, so there is no way for the value to be invalid, because those values are loaded by the programmer/flasher device. No "data" section is involved here, so it is not initialized by the startup code.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">When building in Debug mode, then "orphan handling" equal to "warn" warns me only about some "debug*" and "comment" sections. I guess it's irrelevant?<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Eli,<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">2. The default option for the compiler was to compile it with "-fno-pic". When I added that flag explicitly to the compiler invocation, no difference in the executable layout, nor in the object files or static libraries, has been observed.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">3. I have checked the object files and static libraries for the entries in the "got" section (using "objdump -r ...") and I couldn't find one. It seems that the linker is inputting the "got" section on its own.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">4. FLASH and RAM address are for sure OK. See the linker script: <a href="https://drive.google.com/file/d/1G2oBmN574LTOE9chbBgtkYZRdjcfjluR/view?usp=sharing">https://drive.google.com/file/d/1G2oBmN574LTOE9chbBgtkYZRdjcfjluR/view?usp=sharing</a><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">5. I have compared the value which is in the "got" section with the value it should have and couldn't find any pattern, unfortunately. It might be there, but it would be hard to calculate it.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">6. "-flto" affects the size of the binary only a little, but the error recurs the same way, when the flag is not used.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">7. One more thing: I have compiled libcxx, libunwind, libcxxabi and compiler-rt with the very same flags like specified in the first post and using clang. I linked it with my binary. It worked! The exceptions have been caught properly.
I had to disable some functions from libcxx and libcxxabi, and provide some stubs, which have no implementation (e.g. aligned (de)allocation). I guess this may break other functionality in the app. The binary size increases too much, so I don't think that
using libcxx and libcxxabi is a long-run solution. The problem recurs for the setup: libstdc++ with libunwind and compiler-rt (when libgcc is replaced with libunwind + compiler-rt). It seems that using lld along with libstdc++ breaks the build. One more thing:
lld generates a much larger "got" section, with multiple values, when libcxx is involved.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">8. I didn't compare the linker flags yet for the gcc vs clang build. I will do it soon, and I will get back with the results.<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<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">pon., 22 lis 2021 o 22:26 Peter Smith <<a href="mailto:Peter.Smith@arm.com" target="_blank">Peter.Smith@arm.com</a>> napisał(a):<o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm">
<div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">One more small suggestion. Eli’s suggestions are well worth trying first.<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">If you are static linking the linker should have written the relocated value into the .got section leaving no dynamic relocation. Can you use the linker map file to find that section
in the ELF file, presumably it will be in FLASH and check that the resulting value in the flash is correct (assuming the .got will be in the correct place) after copying to RAM. If it is correct in the FLASH but not after copying to RAM then it could possibly
be something to do with copying the GOT to the correct address in RAM, i.e. if that has gone wrong then the GOT and various other bits of the program could also be wrong.<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">I’d also look at the map file to see if other orphan sections (those that haven’t been explicitly placed by your script) have missed out being copied into RAM. To be sure your script
hasn’t missed anything I recommend setting --orphan-handling-mode=warn or =error
<a href="https://sourceware.org/binutils/docs/ld/Options.html#index-_002d_002dorphan_002dhandling_003dMODE" target="_blank">
https://sourceware.org/binutils/docs/ld/Options.html#index-_002d_002dorphan_002dhandling_003dMODE</a><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto">Peter<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt">
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><b><span lang="EN-US">From:</span></b><span lang="EN-US"> llvm-dev <<a href="mailto:llvm-dev-bounces@lists.llvm.org" target="_blank">llvm-dev-bounces@lists.llvm.org</a>>
<b>On Behalf Of </b>Eli Friedman via llvm-dev<br>
<b>Sent:</b> 22 November 2021 19:28<br>
<b>To:</b> Kacper Kowalski <<a href="mailto:kacper.s.kowalski@gmail.com" target="_blank">kacper.s.kowalski@gmail.com</a>>; llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>><br>
<b>Subject:</b> Re: [llvm-dev] Exceptions not working when cross compiling for ARM Cortex M4 with clang and precompiled libraries from ARM GNU GCC Toolchain</span><o:p></o:p></p>
</div>
</div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"> <o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">Some small suggestions:</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US"> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">LTO makes everything more complicated; make sure things work without -flto before trying LTO.</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US"> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">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.)</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US"> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">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?</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US"> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">-Eli</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US"> </span><o:p></o:p></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt">
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><b><span lang="EN-US">From:</span></b><span lang="EN-US"> llvm-dev <<a href="mailto:llvm-dev-bounces@lists.llvm.org" target="_blank">llvm-dev-bounces@lists.llvm.org</a>>
<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 <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>><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</span><o:p></o:p></p>
</div>
</div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US"> </span><o:p></o:p></p>
<p align="center" style="text-align:center"><strong><span lang="EN-US" style="font-size:10.5pt;font-family:"Arial",sans-serif;color:black;background:yellow">WARNING:</span></strong><span lang="EN-US" 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" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">Hello!</span><o:p></o:p></p>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US"> </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">I have been struggling with enabling exceptions for the Clang build, when cross compiling using precompiled libraries from ARM GNU GCC Toolchain.</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US"> </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;margin-bottom:12.0pt"><span lang="EN-US">LLVM version: 13.0.0<br>
CMake version: 3.21.3<br>
targeted CPU: STM32L432KC, ARM Cortex M4</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">The C++ flags:</span><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 lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">-mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16</span></code><o:p></o:p></pre>
<pre style="vertical-align:baseline"><code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">-nodefaultlibs</span></code><o:p></o:p></pre>
<pre style="vertical-align:baseline"><code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">--sysroot=${ARM_GNU_TOOLCHAIN_PATH}/arm-none-eabi</span></code><o:p></o:p></pre>
<pre style="vertical-align:baseline"><code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">-flto</span></code><o:p></o:p></pre>
<pre style="vertical-align:baseline"><code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">-fdata-sections -ffunction-sections</span></code><o:p></o:p></pre>
<pre style="vertical-align:baseline"><code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm"># For <iostream>, <string>, ...</span></code><o:p></o:p></pre>
<pre style="vertical-align:baseline"><code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">-isystem </span></code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">"${ARM_GNU_TOOLCHAIN_PATH}/arm-none-eabi/include/c++/${ARM_GNU_TOOLCHAIN_GCC_VERSION}/"<code><span style="font-family:inherit"> </span></code></span><o:p></o:p></pre>
<pre style="vertical-align:baseline"><code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm"># For <bits</span></code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">/*>, ...</span><o:p></o:p></pre>
<pre style="vertical-align:baseline"><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">-isystem "${ARM_GNU_TOOLCHAIN_PATH}/arm-none-eabi/include/c++/${ARM_GNU_TOOLCHAIN_GCC_VERSION}/arm-none-eabi/thumb/v7e-m+fp/hard/"</span><o:p></o:p></pre>
<pre style="vertical-align:baseline"><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">-fexceptions</span><o:p></o:p></pre>
<pre style="vertical-align:baseline"><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">-g</span><o:p></o:p></pre>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US"> </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">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></span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US"> </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;margin-bottom:12.0pt"><span lang="EN-US">The linker flags:</span><o:p></o:p></p>
<pre style="vertical-align:baseline"><code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">-mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16</span></code><o:p></o:p></pre>
<pre style="vertical-align:baseline"><code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">-nodefaultlibs</span></code><o:p></o:p></pre>
<pre style="vertical-align:baseline"><code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">--sysroot=${ARM_GNU_TOOLCHAIN_PATH}/arm-none-eabi</span></code><o:p></o:p></pre>
<pre style="vertical-align:baseline"><code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">-flto</span></code><o:p></o:p></pre>
<pre style="vertical-align:baseline"><code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">-fdata-sections -ffunction-sections</span></code><o:p></o:p></pre>
<pre style="vertical-align:baseline"><code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">-Wl,--gc-sections</span></code><o:p></o:p></pre>
<pre style="vertical-align:baseline"><code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">-g</span></code><o:p></o:p></pre>
<pre style="vertical-align:baseline"><code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">-flto</span></code><o:p></o:p></pre>
<pre style="vertical-align:baseline"><code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">-fexceptions</span></code><o:p></o:p></pre>
<pre style="vertical-align:baseline"><code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm"># Path to standard libraries: libc, libm, ...</span></code><o:p></o:p></pre>
<pre style="vertical-align:baseline"><code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">-</span></code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">L"${ARM_GNU_TOOLCHAIN_PATH}/arm-none-eabi/lib/thumb/v7e-m+fp/hard/"</span><o:p></o:p></pre>
<pre style="vertical-align:baseline"><code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm"># Path to libgcc</span></code><o:p></o:p></pre>
<pre style="vertical-align:baseline"><code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">-</span></code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">L"${ARM_GNU_TOOLCHAIN_PATH}/lib/gcc/arm-none-eabi/${ARM_GNU_TOOLCHAIN_GCC_VERSION}/thumb/v7e-m+fp/hard/"</span><o:p></o:p></pre>
<pre style="vertical-align:baseline"><code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">-lc -lm -lnosys -lstdc++ -lgcc</span></code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">")</span><o:p></o:p></pre>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US"><br>
I use custom linker script and custom startup file to compile the firmware properly. I attach them both.</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US"> </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">This code:</span><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 lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">try</span><o:p></o:p></pre>
<pre style="vertical-align:baseline"><code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">{</span></code><o:p></o:p></pre>
<pre style="vertical-align:baseline"><code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm"> </span></code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">throw<code><span style="font-family:inherit"> std::runtime_error{</span></code>"Some error!"<code><span style="font-family:inherit">};</span></code></span><o:p></o:p></pre>
<pre style="vertical-align:baseline"><code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">} </span></code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">catch<code><span style="font-family:inherit"> (</span></code>const<code><span style="font-family:inherit"> std::exception&e)</span></code></span><o:p></o:p></pre>
<pre style="vertical-align:baseline"><code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">{</span></code><o:p></o:p></pre>
<pre style="vertical-align:baseline"><code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm"> </span></code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">printf<code><span style="font-family:inherit">(</span></code>"Error: %s\r\n"<code><span style="font-family:inherit">, e.</span></code>what<code><span style="font-family:inherit">());</span></code></span><o:p></o:p></pre>
<pre style="vertical-align:baseline"><code><span lang="EN-US" style="font-family:inherit;border:none windowtext 1.0pt;padding:0cm">}</span></code><o:p></o:p></pre>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">Will not work for the Clang build. It will crash. It works normally with the GCC build.</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US"> </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">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.</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US"> </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">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:</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">vtable for __cxxabiv1::__class_type_info + 8 in section .rodata</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">I know that, because I have scrapped it from the GCC build (performed with the downloaded toolchain), for which the exceptions work properly.</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US"> </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">The code crashes in the __cxa_type_match:</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">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</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US"> </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">According to the comments in the assembly:</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;margin-bottom:12.0pt"><span lang="EN-US">r4 contains the address to the got section, which is 0x20000000. Under 0x20000000 lies the address to the random ASCII rodata. It means that:</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US"> ldr r6, [r3, #16]</span><o:p></o:p></p>
</div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US"> </span><o:p></o:p></p>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">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.</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US"> </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">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:</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US"> </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">[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</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US"> </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">I attach section dump and disassembly of the program as well.</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US"> </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">Am I missing some compiler or linker flag?</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US"> </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">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.</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US"> </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">Any help would be appreciated.</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US"> </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">Kind regards,</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">Kacper Kowalski</span><o:p></o:p></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</body>
</html>