Is cross-compiling for ARM on x86 with llvm/Clang possible?

Hello list,

I wonder if llvm/Clang can compile C or C++ for ARM from on x86.

http://comments.gmane.org/gmane.comp.compilers.clang.devel/8896

The talk above answered ‘NO’ to my question, which means Clang is not yet able to cross compile for ARM on X86.

Is the answer still correct for my question?

I saw somewhere that Clang supports ARM on Darwin only. Then is the cross compiling possible only on Darwin not on linux or FreeBSD?

If the cross compiling is supported, is there any documentation on how to do it?

Thank you very much in advance.

Best regards
Journeyer

I wonder if llvm/Clang can compile C or C++ for ARM from on x86.

Yes. I use
  clang -emit-llvm -ccc-host-triple arm-unknown-linux-gnu -I /..arm../include/
to generate LLVM bitcode files for ARM. llc then automagically knows to
generate ARM assembly, and ARM binutils take it from there.

If the cross compiling is supported, is there any documentation on how to
do it?

Dunno. Last time I looked, the documentation of clang's command line flags
disagreed with reality, and the -ccc-host-triple flag wasn't documented
anywhere. This might have changed in the meantime.

Hi, Gergö

Cross compiling with Clang/llvm is much easier than with llvm-gcc or gcc!

Thank you very much!!
Though I am so much new to llvm that I can’t prove your answer right way, my friend verified your answer.

Thank you very much.

Sincerely
Journeyer

(PS. Sorry I am resending same email because I sent only to you. I want to say thank you on the mail list.)

2012/6/18 Gergö Barany <gergo@complang.tuwien.ac.at>

The short version is: assuming you have a cross-binutils installation
using e.g. x86_64--netbsd-as and x86_64--netbsd-ld, you add a symlink
called x86_64--netbsd-clang to clang and just call that with an
appropiate --sysroot to make it find your target include headers and
libraries. Substitute x86_64--netbsd with the triple of your choice.
Alternative, call clang -target x86_64--netbsd ...

Joerg

Hello Gergö, Joerg and people on our list

With your kind answer, I tried to build a hello world program for ARM(arm-none-linux-gnueabi) on my x86-64 PC.

Thank you we verified the generated bitcode. The only thing remained is linking.

Let me brief what I did so far.

  1. Built Clang/llvm in a way explained in http://clang.llvm.org/get_started.html on Ubuntu 11.10 x86-64 PC
  2. Downloaded gcc-4.0 toolchain binaries for x86-64 from http://www.gnuarm.com/
  3. Wrote a hello.c as shown below.

#include <stdio.h>

int main(void)
{
printf(“\nHello World!\n\n”);

return 0;
}

  1. Tested as shown below.

./clang -v -emit-llvm -ccc-host-triple arm-none-linux-gnueabi -I/home/hum/Documents/Projects/llvm_clang/gnuarm-4.0.2/arm-elf/include -L/home/hum/Documents/Projects/llvm_clang/gnuarm-4.0.2/arm-elf/bin hello.c

  1. Received an output and an error message shown below.

clang version 3.2 (trunk 158657)
Target: arm-none-linux-gnueabi
Thread model: posix
“/home/hum/Documents/Projects/llvm_clang/build/Debug+Asserts/bin/clang” -cc1 -triple armv4t-none-linux-gnueabi -emit-llvm-bc -disable-free -main-file-name hello.c -mrelocation-model static -mdisable-fp-elim -fmath-errno -mconstructor-aliases -target-abi aapcs-linux -target-cpu arm7tdmi -mfloat-abi soft -target-feature +soft-float-abi -target-linker-version 2.21.53.20110810 -momit-leaf-frame-pointer -v -resource-dir /home/hum/Documents/Projects/llvm_clang/build/Debug+Asserts/bin/…/lib/clang/3.2 -I /home/hum/Documents/Projects/llvm_clang/gnuarm-4.0.2/arm-elf/include -fmodule-cache-path /var/tmp/clang-module-cache -internal-isystem /usr/local/include -internal-isystem /home/hum/Documents/Projects/llvm_clang/build/Debug+Asserts/bin/…/lib/clang/3.2/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -fno-dwarf-directory-asm -fdebug-compilation-dir /home/hum/Documents/Projects/llvm_clang/build/Debug+Asserts/bin -ferror-limit 19 -fmessage-length 212 -mstackrealign -fno-signed-char -fgnu-runtime -fobjc-runtime-has-arc -fobjc-runtime-has-weak -fobjc-fragile-abi -fdiagnostics-show-option -fcolor-diagnostics -o /tmp/hello-T8xLAt.o -x c hello.c
clang -cc1 version 3.2 based upon LLVM 3.2svn default target x86_64-unknown-linux-gnu
ignoring nonexistent directory “/include”
#include “…” search starts here:
#include <…> search starts here:
/home/hum/Documents/Projects/llvm_clang/gnuarm-4.0.2/arm-elf/include
/usr/local/include
/home/hum/Documents/Projects/llvm_clang/build/Debug+Asserts/bin/…/lib/clang/3.2/include
/usr/include
End of search list.
“/usr/bin/ld” -z relro -X --hash-style=gnu --build-id --eh-frame-hdr -m armelf_linux_eabi -dynamic-linker /lib/ld-linux.so.3 -o a.out crt1.o crti.o crtbegin.o -L/home/hum/Documents/Projects/llvm_clang/gnuarm-4.0.2/arm-elf/bin -L/usr/lib/…/lib32 -L/lib -L/usr/lib -plugin /home/hum/Documents/Projects/llvm_clang/build/Debug+Asserts/bin/…/lib/LLVMgold.so /tmp/hello-T8xLAt.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed crtend.o crtn.o
/usr/bin/ld: unrecognised emulation mode: armelf_linux_eabi
Supported emulations: elf_x86_64 elf32_x86_64 elf_i386 i386linux elf_l1om elf_k1om
clang: error: linker command failed with exit code 1 (use -v to see invocation)

  1. The problem occurred when clang starts to link with ld. Clang tried to use the ld in a wrong place - “/usr/bin/ld”. So I tried it myself like below.

/home/hum/Documents/Projects/llvm_clang/gnuarm-4.0.2/arm-elf/bin/ld -z relro -X --hash-style=gnu --build-id --eh-frame-hdr -m armelf_linux_eabi -o a.out crt1.o crti.o crtbegin.o -L/home/hum/Documents/Projects/llvm_clang/gnuarm-4.0.2/arm-elf/bin -plugin /home/hum/Documents/Projects/llvm_clang/build/Debug+Asserts/bin/…/lib/LLVMgold.so /tmp/hello-Fe7D3Y.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed crtend.o crtn.o

  1. And received an error message below.

/home/hum/Documents/Projects/llvm_clang/gnuarm-4.0.2/arm-elf/bin/ld: unrecognised emulation mode: armelf_linux_eabi
Supported emulations: armelf

  1. I found a talk about this problem below.

http://sourceware.org/bugzilla/show_bug.cgi?id=12643

  1. But I don’t know where ld/configure.tgt exists.

Let me list up things I want to know.

A. For the #6 above, how can I make clang use ld in the toolchain not int the system?
B. For the #7 above, how can I trouble-shoot this, I couldn’t find .tgt file introduced in the #9 above.
C. For all the steps above, am I doing all right, I doubt if www.gnuarm.com is trustful.

For Joerg, I tried using -isysroot as a clang option, but I couldn’t find any difference it brings.

Thank you very much.

Sincerely

Journeyer J. Joh

2012/6/18 Joerg Sonnenberger <joerg@britannica.bec.de>

Hello

./clang -v -emit-llvm -ccc-host-triple arm-none-linux-gnueabi
-I/home/hum/Documents/Projects/llvm_clang/gnuarm-4.0.2/arm-elf/include
-L/home/hum/Documents/Projects/llvm_clang/gnuarm-4.0.2/arm-elf/bin hello.c

You forgot about sysroot here.

/home/hum/Documents/Projects/llvm_clang/gnuarm-4.0.2/arm-elf/bin/ld:
unrecognised emulation mode: armelf_linux_eabi
Supported emulations: armelf

1. Your ld is too old
2. Your ld was built for arm-elf, not arm-linux-eabi

Hello,

Thank you for your kind attention to my issue and your help.

I changed the tool chain and tried again. And there is a little progress but still have some problem.

Using --sysroot doesn’t make clang use linker(ld) in the cross tool.
Most important question is how I can make clang use cross tool linker.

Let me show you my experiment and questions below.
There are two questions.

[Run]
./clang -v --save-temps -emit-llvm -ccc-host-triple arm-none-linux-gnueabi --sysroot=/home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/libc hello.c

[Question]

  1. How am I supposed to use --sysroot? Though I used “–sysroot” above, Clang uses native linker, “/usr/bin/ld” not the one in the tool chain, “/home/hum/Documents/Projects/arm_toolchain/arm-2010.09/bin/arm-none-linux-gnueabi-ld”.

[Output]
clang version 3.2 (trunk 158784)
Target: arm-none-linux-gnueabi
Thread model: posix

“/home/hum/Documents/Projects/clang_llvm/build/Debug+Asserts/bin/clang”
-cc1
-triple armv4t-none-linux-gnueabi
-E
-disable-free
-main-file-name hello.c
-mrelocation-model static
-mdisable-fp-elim
-fmath-errno
-mconstructor-aliases
-target-abi aapcs-linux
-target-cpu arm7tdmi
-mfloat-abi soft
-target-feature +soft-float-abi
-target-linker-version 2.22
-momit-leaf-frame-pointer
-v
-resource-dir /home/hum/Documents/Projects/clang_llvm/build/Debug+Asserts/bin/…/lib/clang/3.2
-isysroot /home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/libc
-fmodule-cache-path /var/tmp/clang-module-cache
-internal-isystem /home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/libc/usr/local/include
-internal-isystem /home/hum/Documents/Projects/clang_llvm/build/Debug+Asserts/bin/…/lib/clang/3.2/include
-internal-externc-isystem /home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/libc/include
-internal-externc-isystem /home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/libc/usr/include
-fno-dwarf-directory-asm
-fdebug-compilation-dir /home/hum/Documents/Projects/clang_llvm/build/Debug+Asserts/bin
-ferror-limit 19
-fmessage-length 205
-mstackrealign
-fno-signed-char
-fgnu-runtime
-fobjc-runtime-has-arc
-fobjc-runtime-has-weak
-fobjc-fragile-abi
-fdiagnostics-show-option
-fcolor-diagnostics
-o hello.i
-x c
hello.c

clang -cc1 version 3.2 based upon LLVM 3.2svn default target i386-pc-linux-gnu
ignoring nonexistent directory “/home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/libc/usr/local/include”
ignoring nonexistent directory “/home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/libc/include”
#include “…” search starts here:
#include <…> search starts here:
/home/hum/Documents/Projects/clang_llvm/build/Debug+Asserts/bin/…/lib/clang/3.2/include
/home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/libc/usr/include
End of search list.

“/home/hum/Documents/Projects/clang_llvm/build/Debug+Asserts/bin/clang”
-cc1
-triple armv4t-none-linux-gnueabi
-emit-llvm-bc
-disable-free
-main-file-name hello.c
-mrelocation-model static
-mdisable-fp-elim
-fmath-errno
-mconstructor-aliases
-target-abi aapcs-linux
-target-cpu arm7tdmi
-mfloat-abi soft
-target-feature +soft-float-abi
-target-linker-version 2.22
-momit-leaf-frame-pointer
-v
-resource-dir /home/hum/Documents/Projects/clang_llvm/build/Debug+Asserts/bin/…/lib/clang/3.2
-fno-dwarf-directory-asm
-fdebug-compilation
-dir /home/hum/Documents/Projects/clang_llvm/build/Debug+Asserts/bin
-ferror-limit 19
-fmessage-length 205
-mstackrealign
-fno-signed-char
-fgnu-runtime
-fobjc-runtime-has-arc
-fobjc-runtime-has-weak
-fobjc-fragile-abi
-fdiagnostics-show-option
-fcolor-diagnostics
-o hello.o
-x cpp-output
hello.i

clang -cc1 version 3.2 based upon LLVM 3.2svn default target i386-pc-linux-gnu
#include “…” search starts here:
End of search list.

“/usr/bin/ld” <<<<<<<<<< Problem #1 : wrong linker! ld in cross toolchain is needed!
–sysroot=/home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/libc
-z relro
-X
–hash-style=gnu
–build-id
–eh-frame-hdr
-m armelf_linux_eabi
-dynamic-linker /lib/ld-linux.so.3 <<<<<<<<<< Problem #2 : All libraries have to be of the cross toolchain!
-o a.out
/home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/libc/usr/lib/crt1.o
/home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/libc/usr/lib/crti.o
crtbegin.o <<<<<<<<<< Problem #3 : All libraries have to be of the cross toolchain! (/home/hum/Documents/Projects/arm_toolchain/arm-2010.09/lib/gcc/arm-none-linux-gnueabi/4.5.1/armv4t/crtbegin.o needs to be used!!!)
-L/home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/libc/lib
-L/home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/libc/usr/lib
-plugin /home/hum/Documents/Projects/clang_llvm/build/Debug+Asserts/bin/…/lib/LLVMgold.so <<<<<<<<<< Problem #4 : There is no LLVMgold.so in llvm/clang bin/
hello.o
-lgcc
–as-needed
-lgcc_s
–no-as-needed
-lc
-lgcc
–as-needed
-lgcc_s
–no-as-needed
crtend.o <<<<<<<<<< Problem #5 : All libraries have to be of the cross toolchain! (/home/hum/Documents/Projects/arm_toolchain/arm-2010.09/lib/gcc/arm-none-linux-gnueabi/4.5.1/armv4t/crtend.o needs to be used!!!)
/home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/libc/usr/lib/crtn.o

/usr/bin/ld: unrecognised emulation mode: armelf_linux_eabi
Supported emulations: elf_i386 i386linux elf32_x86_64 elf_x86_64 elf_l1om elf_k1om
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Problem #1 : wrong linker! ld in cross toolchain is needed!
Problem #2 : All libraries have to be of the cross toolchain!
Problem #3 : All libraries have to be of the cross toolchain! (/home/hum/Documents/Projects/arm_toolchain/arm-2010.09/lib/gcc/arm-none-linux-gnueabi/4.5.1/armv4t/crtbegin.o needs to be used!!!)
Problem #4 : There is no LLVMgold.so in llvm/clang bin/
Problem #5 : All libraries have to be of the cross toolchain! (/home/hum/Documents/Projects/arm_toolchain/arm-2010.09/lib/gcc/arm-none-linux-gnueabi/4.5.1/armv4t/crtend.o needs to be used!!!)

Workaround #1 : tried to run /home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/bin/ld manually.
Workaround #2 : Ignored this library in this manual run.
Workaround #3 : /home/hum/Documents/Projects/arm_toolchain/arm-2010.09/lib/gcc/arm-none-linux-gnueabi/4.5.1/armv4t/crtbegin.o used instead.
Workaround #4 : Ignored this library in this manual run.
Workaround #5 : /home/hum/Documents/Projects/arm_toolchain/arm-2010.09/lib/gcc/arm-none-linux-gnueabi/4.5.1/armv4t/crtend.o used instead.

[Workaround - Manual Run]
/home/hum/Documents/Projects/arm_toolchain/arm-2010.09/bin/arm-none-linux-gnueabi-ld --sysroot=/home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/libc -z relro -X --hash-style=gnu --build-id --eh-frame-hdr -m armelf_linux_eabi -o a.out /home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/libc/usr/lib/crt1.o /home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/libc/usr/lib/crti.o /home/hum/Documents/Projects/arm_toolchain/arm-2010.09/lib/gcc/arm-none-linux-gnueabi/4.5.1/armv4t/crtbegin.o -L/home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/libc/lib -L/home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/libc/usr/lib hello.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /home/hum/Documents/Projects/arm_toolchain/arm-2010.09/lib/gcc/arm-none-linux-gnueabi/4.5.1/armv4t/crtend.o /home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/libc/usr/lib/crtn.o

[Output]
hello.o: file not recognized: File format not recognized

[Test]
“file hello.o” prints out :
hello.o: LLVM bitcode

[Question]
2. Am I using a wrong cross toolchain? Doesn’t feeding LLVM bitcode to the ARM cross linker make sense?

Thank you very much.

Sincerely
Journeyer J. Joh

2012/6/19 Anton Korobeynikov <anton@korobeynikov.info>

Hello,

Please ignore my previous email.
I received the final binary with command below.
I had to get rid of the option, -emit-llvm.

./clang -v --save-temps -ccc-host-triple arm-none-linux-gnueabi --sysroot=/home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/libc hello.c

file hello.i
hello.i: ASCII C program text

file hello.s
hello.s: ASCII assembler program text

/home/hum/Documents/Projects/arm_toolchain/arm-2010.09/bin/arm-none-linux-gnueabi-as -mfloat-abi=softfp -o hello.o hello.s

file hello.o
hello.o: ELF 32-bit LSB relocatable, ARM, version 1 (SYSV), not stripped

/home/hum/Documents/Projects/arm_toolchain/arm-2010.09/bin/arm-none-linux-gnueabi-ld --sysroot=/home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/libc -z relro -X --hash-style=gnu --build-id --eh-frame-hdr -m armelf_linux_eabi -o a.out /home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/libc/usr/lib/crt1.o /home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/libc/usr/lib/crti.o /home/hum/Documents/Projects/arm_toolchain/arm-2010.09/lib/gcc/arm-none-linux-gnueabi/4.5.1/armv4t/crtbegin.o -L/home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/libc/lib -L/home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/libc/usr/lib -L/home/hum/Documents/Projects/arm_toolchain/arm-2010.09/lib/gcc/arm-none-linux-gnueabi/4.5.1/armv4t hello.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /home/hum/Documents/Projects/arm_toolchain/arm-2010.09/lib/gcc/arm-none-linux-gnueabi/4.5.1/armv4t/crtend.o /home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/libc/usr/lib/crtn.o

file a.out
a.out: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.16, BuildID[sha1]=0x7825ca07341b2f337c4f10b5711055c997820a9c, not stripped

The only thing remained is to specify the cross tool chain location to the clang so that clang uses cross-as and ld automatically.

If someone provide these information, it would be welcomed!

Thank you very much.

Journeyer J. Joh

2012/6/20 Journeyer J. Joh <oosaprogrammer@gmail.com>

Hi Joerg,

Thank you very much!

I finally found a working command string to cross-compile for ARM on x86.

./clang -v --save-temps -ccc-host-triple arm-none-linux-gnueabi --sysroot=/home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/libc -gcc-toolchain /home/hum/Documents/Projects/arm_toolchain/arm-2010.09 hello.c -o hello

Above works fine! Important options are

-ccc-host-triple arm-none-linux-gnueabi
–sysroot=/home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/libc
-gcc-toolchain /home/hum/Documents/Projects/arm_toolchain/arm-2010.09

And the environment variable PATH doesn’t need to be changed.

Thank you everyone who answered for my questions.

Regards
Journeyer J. Joh

2012/6/20 Joerg Sonnenberger <joerg@britannica.bec.de>

Hello,

With your kind concern and help, I now can make a binary for ARM target.

./clang -v --save-temps -ccc-host-triple arm-none-linux-gnueabi --sysroot=/home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/libc -gcc-toolchain /home/hum/Documents/Projects/arm_toolchain/arm-2010.09 hello.c -o hello

The build command is shown above.

After that, I prepared an ARM laptop, AC100 - TOSHIBA.
I installed Ubuntu 12.04 in the way guided from the link below.

https://wiki.ubuntu.com/ARM/TEGRA/AC100

So I moved the final binary from host PC to AC100 and executed.
But the binary DIDN’T RUN.

$./hello

returns an error message below.

bash: ./hello: No such file hello

I need to understand why it doesn’t run on ARM laptop.

I compiled another sample program hi.c on the ARM laptop and compared both - the one cross compiled “hello” and the other self host compiled “hi”.

Output message of utility “file” is almost the same except Linux version, one is 2.6.16 and the other is 2.6.31. But the output of “ldd” shows something meaningful.

ldd hi
libc.so.6 => /lib/arm-linux-gnueabihf/libc.so.6 (0xb6ecf000)
/lib/ld-linux-armhf.so.3 (0xb6fc3000)

ldd hello
not a dynamic executable

I guess I have to use a cross toolchain for my ARM target - AC 100. It has been equipped with Ubuntu 12.04.

Could you provide me any hint to analize this problem?

Thank you in advance.

Journeyer J. Joh

2012/6/21 Journeyer J. Joh <oosaprogrammer@gmail.com>

Hello,

With your kind concern and help, I now can make a binary for ARM target.

./clang -v --save-temps -ccc-host-triple arm-none-linux-gnueabi
--sysroot=/home/hum/Documents/Projects/arm_toolchain/arm-2010.09/arm-none-linux-gnueabi/libc
-gcc-toolchain /home/hum/Documents/Projects/arm_toolchain/arm-2010.09
hello.c -o hello

The build command is shown above.

After that, I prepared an ARM laptop, AC100 - TOSHIBA.
I installed Ubuntu 12.04 in the way guided from the link below.

ARM/TEGRA/AC100 - Ubuntu Wiki

So I moved the final binary from host PC to AC100 and executed.
But the binary DIDN'T RUN.

$./hello

returns an error message below.

bash: ./hello: No such file hello

You could try readelf -l hello and check the INTERP header. When I've seen this message usually PT_INTERP points to something that the kernel can't find.

I need to understand why it doesn't run on ARM laptop.

I compiled another sample program hi.c on the ARM laptop and compared
both - the one cross compiled "hello" and the other self host compiled "hi".

Output message of utility "file" is almost the same except Linux
version, one is 2.6.16 and the other is 2.6.31. But the output of "ldd"
shows something meaningful.

This is curious based on the output from ldd below, does file report each as, "dynamically linked".

Hello list

I think I really finally found a way to crossbuild a binary for ARM using Clang/llvm.

This message would be a documentation for someone who may concern for the same issue with me.

  • Target : TOSHIBA AC100 / Ubuntu 12.04 (https://wiki.ubuntu.com/ARM/TEGRA/AC100)
  • Host : i386 Desktop PC / Ubuntu 12.04
  • Toolchain on host : sudo apt-get install gcc-arm-linux-gnueabi
  • Clang/llvm compile : http://clang.llvm.org/get_started.html
  • Command string for crossbuild : ./clang -v --save-temps -ccc-host-triple arm-linux-gnueabi --sysroot=/usr/arm-linux-gnueabi -gcc-toolchain /usr/ -Wl,-dynamic-linker,/lib/ld-linux-armhf.so.3 hello.c -o hello

During this testing I found something like a bug in Clang or llvm.
Please note the Command string for crossbuild above.
Problem occurs if “-ccc-host-triple” gets “arm-linux-gnueabihf” instead of “arm-linux-gnueabi”.
Problem is that ld is called with a incomplete or no path for libgcc, crtbegin.o and crtend.o “-gcc-toolchain” options doesn’t seem to work.
Workaroud for this problem is using a command string like below.

./clang -v --save-temps -ccc-host-triple arm-linux-gnueabihf -mfloat-abi=hard -mfpu=vfpv3-d16 --sysroot=/usr/arm-linux-gnueabihf -gcc-toolchain /usr/ -Wl,-dynamic-linker,/lib/ld-linux-armhf.so.3 -L/usr/lib/gcc/arm-linux-gnueabihf/4.6 -L/usr/lib/gcc/arm-linux-gnueabihf/4.6/…/…/…/…/arm-linux-gnueabihf/lib/…/lib -L/usr/lib/gcc/arm-linux-gnueabihf/4.6/…/…/…/…/arm-linux-gnueabihf/lib -L/lib/arm-linux-gnueabihf -L/usr/lib/arm-linux-gnueabihf -L/usr/lib -L/lib hello.c -o hello

Note all library path have to be specified. And more importantly crtend.o and crtbegin.o have to be symbolic-linked!!
I tested with a toolchain for arm-linux-gnueabihf for this test.(sudo apt-get install gcc-arm-linux-gnueabihf)

Thank you everyone who answered for my questions.

Journeyer J. Joh

(ps. For Sid, The final binary “hello” prints “not a dynamic executable” when asked with “ldd”. But it runs very well. ^^)

2012/6/27 Sid Manning <sidneym@codeaurora.org>

Hello Sid

I checked the binary with “readelf -l” and you are right. Originally, it pointed the one of the host system. It has to be the one of the target system. This is why the option Wl,-dynamic-linker,path_to_target_dynamic_loader is needed.

I understood this issue now.
Thank you very much!

Regards

Journeyer J. Joh

2012/6/27 Sid Manning <sidneym@codeaurora.org>

Hi Journeyer

First, thank you so much for your updates on your experiments.
I am currently following your steps but have found myself stuck with the
following error:

/usr/lib/gcc/arm-linux-gnueabi/4.6/../../../../arm-linux-gnueabi/bin/ld:
this linker was not configured to use sysroots
clang: error: linker command failed with exit code 1 (use -v to see
invocation)

I used the command string you wrote :
./clang -v --save-temps -ccc-host-triple arm-linux-gnueabi
--sysroot=/usr/arm-linux-gnueabi -gcc-toolchain /usr/
-Wl,-dynamic-linker,/lib/ld-linux-armhf.so.3 hello.c -o hello

Can you help me identify the problem?
I'm also really interested in cross compiling ARM and x86.

For the complete logs of the above command, please refer below :

clang version 3.1 (branches/release_31)
Target: arm--linux-gnueabi
Thread model: posix
"/home/jedd10-04/TOOC/LLVM-3.1/llvm-3.1.src/build/Debug+Asserts/bin/clang"
-cc1 -triple armv4t--linux-gnueabi -E -disable-free -main-file-name hello.c
-mrelocation-model static -mdisable-fp-elim -mconstructor-aliases
-target-abi aapcs-linux -target-cpu arm7tdmi -mfloat-abi soft
-target-feature +soft-float-abi -target-linker-version 2.20.1
-momit-leaf-frame-pointer -v -resource-dir
/home/jedd10-04/TOOC/LLVM-3.1/llvm-3.1.src/build/Debug+Asserts/bin/../lib/clang/3.1
-isysroot /usr/arm-linux-gnueabi -fmodule-cache-path
/var/tmp/clang-module-cache -internal-isystem
/usr/arm-linux-gnueabi/usr/local/include -internal-isystem
/home/jedd10-04/TOOC/LLVM-3.1/llvm-3.1.src/build/Debug+Asserts/bin/../lib/clang/3.1/include
-internal-externc-isystem /usr/arm-linux-gnueabi/include
-internal-externc-isystem /usr/arm-linux-gnueabi/usr/include
-fno-dwarf-directory-asm -fdebug-compilation-dir
/home/jedd10-04/TOOC/clang-samples -ferror-limit 19 -fmessage-length 132
-mstackrealign -fno-signed-char -fgnu-runtime -fobjc-runtime-has-arc
-fobjc-runtime-has-weak -fobjc-fragile-abi -fdiagnostics-show-option
-fcolor-diagnostics -o hello.i -x c hello.c
clang -cc1 version 3.1 based upon LLVM 3.1 default target
x86_64-unknown-linux-gnu
ignoring nonexistent directory "/usr/arm-linux-gnueabi/usr/local/include"
ignoring nonexistent directory "/usr/arm-linux-gnueabi/usr/include"
#include "..." search starts here:
#include <...> search starts here:

/home/jedd10-04/TOOC/LLVM-3.1/llvm-3.1.src/build/Debug+Asserts/bin/../lib/clang/3.1/include
/usr/arm-linux-gnueabi/include
End of search list.
"/home/jedd10-04/TOOC/LLVM-3.1/llvm-3.1.src/build/Debug+Asserts/bin/clang"
-cc1 -triple armv4t--linux-gnueabi -S -disable-free -main-file-name hello.c
-mrelocation-model static -mdisable-fp-elim -mconstructor-aliases
-target-abi aapcs-linux -target-cpu arm7tdmi -mfloat-abi soft
-target-feature +soft-float-abi -target-linker-version 2.20.1
-momit-leaf-frame-pointer -v -resource-dir
/home/jedd10-04/TOOC/LLVM-3.1/llvm-3.1.src/build/Debug+Asserts/bin/../lib/clang/3.1
-fno-dwarf-directory-asm -fdebug-compilation-dir
/home/jedd10-04/TOOC/clang-samples -ferror-limit 19 -fmessage-length 132
-mstackrealign -fno-signed-char -fgnu-runtime -fobjc-runtime-has-arc
-fobjc-runtime-has-weak -fobjc-fragile-abi -fdiagnostics-show-option
-fcolor-diagnostics -o hello.s -x cpp-output hello.i
clang -cc1 version 3.1 based upon LLVM 3.1 default target
x86_64-unknown-linux-gnu
#include "..." search starts here:
End of search list.
"/usr/lib/gcc/arm-linux-gnueabi/4.6/../../../../arm-linux-gnueabi/bin/as"
-o hello.o hello.s
"/usr/lib/gcc/arm-linux-gnueabi/4.6/../../../../arm-linux-gnueabi/bin/ld"
--sysroot=/usr/arm-linux-gnueabi -z relro -X --hash-style=both --build-id
--eh-frame-hdr -m armelf_linux_eabi -dynamic-linker /lib/ld-linux.so.3 -o
hello /usr/arm-linux-gnueabi/lib/crt1.o /usr/arm-linux-gnueabi/lib/crti.o
/usr/lib/gcc/arm-linux-gnueabi/4.6/crtbegin.o
-L/usr/lib/gcc/arm-linux-gnueabi/4.6 -L/usr/arm-linux-gnueabi/lib
-dynamic-linker /lib/ld-linux-armhf.so.3 hello.o -lgcc --as-needed -lgcc_s
--no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed
/usr/lib/gcc/arm-linux-gnueabi/4.6/crtend.o
/usr/arm-linux-gnueabi/lib/crtn.o
/usr/lib/gcc/arm-linux-gnueabi/4.6/../../../../arm-linux-gnueabi/bin/ld:
this linker was not configured to use sysroots
clang: error: linker command failed with exit code 1 (use -v to see
invocation)

Hello Tsukishiro

First of all, I am in a learning stage of LLVM and Clang. This means I
am not an expert! ^^;

The final trial I succeeded was under the environment below.

- Target : TOSHIBA AC100 / Ubuntu 12.04 (ARM - Ubuntu Wiki**
TEGRA/AC100 <https://wiki.ubuntu.com/ARM/TEGRA/AC100&gt;\)
- Host : i386 Desktop PC / Ubuntu 12.04
- Toolchain on host : sudo apt-get install gcc-arm-linux-gnueabi
- Clang/llvm compile : http://clang.llvm.org/get_started.html

I wonder your environment.

- Target :
- Host :
- Toolchain on host : How did you get your toolchain?
- Clang/llvm compile : How did you compile Clang/llvm?

I tested successfully with a command below.

- Command string for crossbuild : ./clang -v --save-temps -ccc-host-triple
arm-linux-gnueabi --sysroot=/usr/arm-linux-gnueabi -gcc-toolchain /usr/
-Wl,-dynamic-linker,/lib/ld-linux-armhf.so.3 hello.c -o hello

Important options are shown below.

-ccc-host-triple : arm-linux-gnueabi
--sysroot : /usr/arm-linux-gnueabi
-gcc-toolchain : /usr/
-Wl,-dynamic-linker,/lib/ld-linux-armhf.so.3

I want to know your command string not mine. Could you write it to me?

I am afraid I could fail to help you.... I am not an expert.. Please
consider this.

Best regards
Journeyer

/usr/lib/gcc/arm-linux-gnueabi/4.6/../../../../arm-linux-gnueabi/bin/ld:
this linker was not configured to use sysroots
clang: error: linker command failed with exit code 1 (use -v to see
invocation)

Well, the error is pretty clear - your linker does not support sysroot.