Re: Clang cross-compiling to Cortex-M4 (ARM)

From: Brooks Davis <brooks_at_freebsd.org>
Date: Mon, 1 Oct 2018 15:52:51 +0000
On Sun, Sep 30, 2018 at 02:20:26PM +0200, Goran Meki?? wrote:
> Hello,
> 
> With Clang 4.0 cross-compiling works, but any later versions (I tried
> 5.0, 6.0 and 7.0 from packages) fails. This is the simple test program:
> 
> #include <stdio.h>
> 
> int main() {
>   return 0;
> }
> 
> 
> These are the results:
> 
> clang -fno-builtin -nostdlib -ffreestanding -target arm-none-eabi -march=armv7-m -mcpu=cortex-m4 -o test test.c
> test.c:1:10: fatal error: 'stdio.h' file not found
> #include <stdio.h>
>          ^~~~~~~~~
> 1 error generated.
> 
> clang40 -fno-builtin -nostdlib -ffreestanding -target arm-none-eabi -march=armv7-m -mcpu=cortex-m4 -o test test.c
> /usr/local/bin/arm-none-eabi-ld: warning: cannot find entry symbol _start; defaulting to 0000000000008000
> 
> 
> System clang is 6.0.1, but it's the same with the one from packages (to
> be precise, clang60 command). I suppose this is more suitable for LLVM
> mailing list, but I wanted to check if there's something specific on
> FreeBSD I should know about before I write there? Also, this does
> compile:
> 
> clang -fno-builtin -nostdlib -ffreestanding -target arm-none-eabi -march=armv7-m -mcpu=cortex-m4 -isystem /usr/include -o test test.c
> /usr/bin/ld.lld: warning: cannot find entry symbol _start; defaulting to 0x11000

It appears that post-4.0 clang dropped /usr/include from the include
path in this case.  You can see this with the --verbose flag:

$ clang40 -fno-builtin -nostdlib -ffreestanding -target arm-none-eabi
-march=armv7-m -mcpu=cortex-m4 -c -o test test.c --verbose     
clang version 4.0.1 (tags/RELEASE_401/final)
Target: arm-none--eabi
Thread model: posix
InstalledDir: /usr/local/llvm40/bin
 "/usr/local/llvm40/bin/clang-4.0" -cc1 -triple thumbv7em-none--eabi
-emit-obj -mrelax-all -disable-free -main-file-name test.c
-mrelocation-model static -mthread-model posix -mdisable-fp-elim
-fmath-errno -masm-verbose -mconstructor-aliases -target-cpu cortex-m4
-target-feature +soft-float-abi -target-feature +strict-align
-target-abi aapcs -mfloat-abi soft -v -dwarf-column-info
-debugger-tuning=gdb -coverage-notes-file
/home/bed22/git/freebsd/test.gcno -resource-dir
/usr/local/llvm40/bin/../lib/clang/4.0.1 -fdebug-compilation-dir
/home/bed22/git/freebsd -ferror-limit 19 -fmessage-length 80
-ffreestanding -fallow-half-arguments-and-returns -fno-builtin
-fno-signed-char -fobjc-runtime=gcc -fdiagnostics-show-option
-fcolor-diagnostics -o test -x c test.c
clang -cc1 version 4.0.1 based upon LLVM 4.0.1 default target
x86_64-portbld-freebsd11.1
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/local/llvm40/bin/../lib/clang/4.0.1/include
 /usr/include
End of search list.


$ clang60 -fno-builtin -nostdlib -ffreestanding -target arm-none-eabi
-march=armv7-m -mcpu=cortex-m4 -c -o test test.c --verbose     
clang version 6.0.1 (tags/RELEASE_601/final)
Target: arm-none--eabi
Thread model: posix
InstalledDir: /usr/local/llvm60/bin
 "/usr/local/llvm60/bin/clang-6.0" -cc1 -triple thumbv7em-none--eabi -emit-obj -mrelax-all -disable-free -main-file-name test.c -mrelocation-model static -mthread-model posix -mdisable-fp-elim -fmath-errno -no-integrated-as -mconstructor-aliases -ffreestanding -nostdsysteminc -target-cpu cortex-m4 -target-feature +soft-float-abi -target-feature -crc -target-feature +dsp -target-feature -ras -target-feature -dotprod -target-feature -hwdiv-arm -target-feature +hwdiv -target-feature +strict-align -target-abi aapcs -mfloat-abi soft -fallow-half-arguments-and-returns -dwarf-column-info -debugger-tuning=gdb -v -coverage-notes-file /home/bed22/git/freebsd/test.gcno -resource-dir /usr/local/llvm60/lib/clang/6.0.1 -internal-isystem /usr/local/llvm60/lib/clang/6.0.1/include -internal-isystem include -fdebug-compilation-dir /home/bed22/git/freebsd -ferror-limit 19 -fmessage-length 80 -fno-builtin -fno-signed-char -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -o test -x c test.c
clang -cc1 version 6.0.1 based upon LLVM 6.0.1 default target
x86_64-portbld-freebsd11.1
ignoring duplicate directory
"/usr/local/llvm60/lib/clang/6.0.1/include"
#include "..." search starts here:
#include <...> search starts here:
  /usr/local/llvm60/lib/clang/6.0.1/include
  include
End of search list.
In file included from test.c:1:
include/stdio.h:41:10: fatal error: 'sys/cdefs.h' file not found
#include <sys/cdefs.h>
          ^~~~~~~~~~~~~
1 error generated.


I think this change is correct (the system headers aren't generally the
headers you're looking for when targeting bare metal).  You can restore
the previous behavior (more or less) with "-isystem /usr/include".

-- Brooks

Received on Mon Oct 01 2018 - 13:52:58 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:41:18 UTC