<div dir="ltr">Hi all,<div><br></div><div>I am trying to cross-compile my application for ARM Cortex-M3. Here's how I'm doing it:</div><div><br></div><div>1, Get a pre-built GNU toolchain for ARM Cortex-M processors from <a href="https://developer.arm.com/open-source/gnu-toolchain/gnu-rm/downloads" target="_blank">https://developer.arm.<wbr>com/open-source/gnu-toolchain/<wbr>gnu-rm/downloads</a></div><div><br></div><div>2, Create an object file for ARM:</div><div>clang -c -target arm-none-eabi -mcpu=cortex-m3 -mthumb -O3 -g --sysroot=/home/gcc-arm-none-<wbr>eabi-6-2017-q2-update/arm-<wbr>none-eabi test.c -o test.o<br></div><div><br></div><div>3, Then link it with ld:</div><div>arm-none-eabi-gcc -o test test.o</div><div><br></div><div>Everything seems to work until now (actually I get this warning "<b>arm-none-eabi/bin/ld: warning: test.o uses 32-bit enums yet the output is to use variable-size enums; use of enum values across objects may fail</b>", but I guess it is not important): I get an ELF executable for ARM Cortex-M3 as expected.</div><div><br></div><div>However, things get tricky if we look closer at the assembly file (obtained by <b>arm-none-eabi-objdump</b>): the user code (written by me) is in Thumb mode, for example:</div><div><br><div>1420 00008fd2 <<b>main</b>>:</div><div>1421 8fd2: b580 push {r7, lr}</div><div>1422 8fd4: 466f mov r7, sp</div><div><b>1423 8fd6: f7ff fd83 bl 91d8 <srand></b></div><div>1424 8fda: 2000 movs r0, #0</div><div>1425 8fdc: bd80 pop {r7, pc}</div></div><div><br></div><div>But libc code is in ARM mode:</div><div><br></div><div><div>1563 000091d8 <<b>srand</b>>:</div><div>1564 91d8: e3a02000 mov r2, #0</div><div>1565 91dc: e59f300c ldr r3, [pc, #12] ; 91f0 <srand+0x18></div><div>1566 91e0: e5933000 ldr r3, [r3]</div><div>1567 91e4: e58300a8 str r0, [r3, #168] ; 0xa8</div><div>1568 91e8: e58320ac str r2, [r3, #172] ; 0xac</div><div>1569 91ec: e12fff1e bx lr</div></div><div><br></div><div>As you can see, the call to <b>srand</b> is just a <b>bl (Branch with Link)</b>, not a <b>blx (Branch with Link and Exchange instruction set)</b>, so I think something is going wrong here. Indeed, when using a binary analysis tool to simulate this code, it cannot executes correctly <b>srand</b> because it decodes instruction in Thumb mode. Surprisingly, compiling this code with <b>arm-none-eabi-gcc</b> with these same options yields a better result: the user code doesn't change, but libc code is now in Thumb mode, and the binary analysis tool can simulate the application just fine:</div><div><br></div><div><div>1743 00009198 <<b>srand</b>>:</div><div>1744 9198: 2200 movs r2, #0</div><div>1745 919a: 4b03 ldr r3, [pc, #12] ; (91a8 <srand+0x10>)</div><div>1746 919c: 681b ldr r3, [r3, #0]</div><div>1747 919e: f8c3 00a8 str.w r0, [r3, #168] ; 0xa8</div><div>1748 91a2: f8c3 20ac str.w r2, [r3, #172] ; 0xac</div><div>1749 91a6: 4770 bx lr</div></div><div><br></div><div>Can anyone kindly give me some pointers on how to debug this? Or at least tell me whether this is a bug? I am a bit lost now, I've tried to look up for more information on this but cannot find out why Clang doesn't generate Thumb code for libc functions.</div><div><br></div><div>Thank you very much for your help,</div><div><br></div><div><div><div class="m_7812857754752616518gmail_signature"><div dir="ltr">Son Tuan Vu</div></div></div>
</div></div>