www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - LDC with ARM backend

reply Claude <no no.no> writes:
Hello,

I would like to cross-compile a D program from a x86 machine to 
an ARM target.

I work on GNU/Linux Ubuntu 64-bit.
I have an ARM gcc toolchain, which I can use to make programs on 
an ARM Cortex-A9 architecture running a Linux kernel 3.4.11+.

I managed to build and install LLVM 3.8.1 with LDC 1.1-alpha1, 
which works fine to build and run native programs.

I read some documentation here:
http://wiki.dlang.org/Minimal_semihosted_ARM_Cortex-M_%22Hello_World%22

... but it seems to target bare-metal programming, whereas I 
already have a GNU/Linux running on my ARM target and want to use 
it. It does noty tell how to have an LDC with ARM backend.

So I'm a bit confused of what the current state of LDC+ARM is. 
For example, is the run-time fully ported on ARM/Linux?

What would be the steps to have an LDC cross-compiling to ARM?

Thanks
Jul 15 2016
next sibling parent reply Radu <radu void.null> writes:
On Friday, 15 July 2016 at 14:09:40 UTC, Claude wrote:
 Hello,

 I would like to cross-compile a D program from a x86 machine to 
 an ARM target.

 I work on GNU/Linux Ubuntu 64-bit.
 I have an ARM gcc toolchain, which I can use to make programs 
 on an ARM Cortex-A9 architecture running a Linux kernel 3.4.11+.

 I managed to build and install LLVM 3.8.1 with LDC 1.1-alpha1, 
 which works fine to build and run native programs.

 I read some documentation here:
 http://wiki.dlang.org/Minimal_semihosted_ARM_Cortex-M_%22Hello_World%22

 ... but it seems to target bare-metal programming, whereas I 
 already have a GNU/Linux running on my ARM target and want to 
 use it. It does noty tell how to have an LDC with ARM backend.

 So I'm a bit confused of what the current state of LDC+ARM is. 
 For example, is the run-time fully ported on ARM/Linux?

 What would be the steps to have an LDC cross-compiling to ARM?

 Thanks
Hi, LDC on Linux ARM is fairly complete. I think it is a fully supported platform (all tests are passing). Check in https://wiki.dlang.org/Compilers the LDC column. This is the close for a tutorial for cross-compiling https://wiki.dlang.org/Build_LDC_for_Android builds.
Jul 15 2016
parent Claude <no no.no> writes:
On Friday, 15 July 2016 at 15:02:15 UTC, Radu wrote:
 Hi,
 LDC on Linux ARM is fairly complete. I think it is a fully 
 supported platform (all tests are passing). Check in 
 https://wiki.dlang.org/Compilers the LDC column.

 This is the close for a tutorial for cross-compiling 
 https://wiki.dlang.org/Build_LDC_for_Android builds.
Great, I didn't see it. However I don't use Android on my ARM target, I have a arm-none-linux-gnueabi toolchain. I think I have to change the Android patch, keep the "80-bit float" stuff, and modify the build scripts somehow to use GNU version.
Jul 15 2016
prev sibling parent reply Kai Nacke <kai redstar.de> writes:
Hi Claude!

On Friday, 15 July 2016 at 14:09:40 UTC, Claude wrote:
 Hello,

 I would like to cross-compile a D program from a x86 machine to 
 an ARM target.
 [...]
 So I'm a bit confused of what the current state of LDC+ARM is. 
 For example, is the run-time fully ported on ARM/Linux?
LDC is fully ported to Linux/ARM. The current release also includes LDC pre-compiled for ARMv7 with hard floats (e.g. matches recent Raspberry hardware). On such a platform you can simply unpack the binary packages and LDC should run out of the box.
 What would be the steps to have an LDC cross-compiling to ARM?
That is a somewhat different story. First, you need to built LLVM with support for ARM and then compile LDC against this version of LLVM. You can run ldc2 -version to see if the ARM target is supported. As Radu already mentioned, most of the required steps are described in the wiki at https://wiki.dlang.org/Build_LDC_for_Android. From a different perspective there is an old news post about cross-compiling to AArch64, too: http://forum.dlang.org/post/fhwvxatxezkafnalwhqr forum.dlang.org. There is a reason why we do not distribute a binary version of LDC with all LLVM targets enabled. LDC still uses the real format of the host. This is different on ARM (80bit on Linux/x86 vs. 64bit on Linux/ARM). Do not expect that applications using real type work correctly. (The Windows version of LDC uses 64bit reals. The binary build has the ARM target enabled.) Regards, Kai
Jul 15 2016
parent reply Claude <no no.no> writes:
On Friday, 15 July 2016 at 15:24:36 UTC, Kai Nacke wrote:
 There is a reason why we do not distribute a binary version of 
 LDC with all LLVM targets enabled. LDC still uses the real 
 format of the host. This is different on ARM (80bit on 
 Linux/x86 vs. 64bit on Linux/ARM). Do not expect that 
 applications using real type work correctly.
 (The Windows version of LDC uses 64bit reals. The binary build 
 has the ARM target enabled.)

 Regards,
 Kai
Hello Kai, Thanks for your answer. From the link https://wiki.dlang.org/Build_LDC_for_Android , I did exactly the same steps described in section "Compile LLVM" (patch applied). At section "Build ldc for Android/ARM", I did it quite the same. I applied the patch ldc_1.0.0_android_arm, but changed runtime/CMakeList.txt, instead of using Android specific stuff, I did: Line 15: set(D_FLAGS -w;-mtriple=arm-none-linux-gnueabi CACHE STRING "Runtime build flags, separated by ;") Line 505: set(RT_CFLAGS "-g") set(CMAKE_SYSTEM_NAME Linux) set(CMAKE_C_COMPILER /opt/arm-2009q1/bin/arm-none-linux-gnueabi-gcc) set(CMAKE_CXX_COMPILER /opt/arm-2009q1/bin/arm-none-linux-gnueabi-c++) On the command line, I aliased DMD to /usr/bin/dmd and runt cmake as described... Afterwards, I ran make for ldc2, phobos2-ldc an druntime-ldc, but I did not apply the patches on phobos and runtime. It looked like the path introduced some static compilation towards Android, so I thought it would not apply to my needs. So here' what I get if I do a "ldc2 -version": LDC - the LLVM D compiler (1.0.0): based on DMD v2.070.2 and LLVM 3.8.1 built with DMD64 D Compiler v2.071.1 Default target: x86_64-unknown-linux-gnu Host CPU: westmere http://dlang.org - http://wiki.dlang.org/LDC Registered Targets: arm - ARM armeb - ARM (big endian) thumb - Thumb thumbeb - Thumb (big endian) I can strictly compile a "hello world" program: ./bin/ldc2 -mtriple=arm-none-linux-gnueabi test.d I get the expected "test.o" But I don't know how to link it. I don't have "clang". I tried to link it with the gcc from the gnu ARM toolchain with libdruntime-ldc.a, libldc.a and libphobos2-ldc.a, but it fails miserably: many undefined symbols (pthread, and some other os related stuff).
Jul 19 2016
parent reply Claude <no no.no> writes:
I think my cross-compile LDC is fine.

I tried to build this D program:

/// loire.d
int main()
{
     return 42;
}



However, the run-time is not (neither is phobos), most of the 
linker issues come from the druntime. So...

I wrote my own druntime. Here's the code:

/// dummyruntime.d
// from rt/sections_elf_shared.d, probably don't need it right 
now...
extern(C) void _d_dso_registry(void* data)
{
}

// from rt/dmain2.d, just call my D main(), ignore args...
private alias extern(C) int function(char[][] args) MainFunc;

extern (C) int _d_run_main(int argc, char **argv, MainFunc 
mainFunc)
{
     return mainFunc(null);
}



I built everything:


./bin/ldc2 -mtriple=arm-none-linux-gnueabi -c loire.d
./bin/ldc2 -mtriple=arm-none-linux-gnueabi -c dummyruntime.d

/opt/arm-2009q1/bin/arm-none-linux-gnueabi-gcc loire.o 
dummyruntime.o -o loire


And I ran it successfully on my ARM target:

$> loire
$> echo $?
42



So now I know I have a proper LDC cross-compiler! :)

I'm jut missing a proper druntime and phobos for GNU/Linux ARM.
Jul 20 2016
parent reply Claude <no no.no> writes:
So I'm trying to build druntime correctly, I corrected some 
problems here and there, but I still cannot link with 
libdruntime-ldc.a:


/opt/arm-2009q1/bin/arm-none-linux-gnueabi-gcc loire.o 
lib/libdruntime-ldc.a -o loire


I get many errors like:

/opt/arm-2009q1/bin/../lib/gcc/arm-none-linux-gnueabi/4.3.3/../../../../arm-none-l
nux-gnueabi/bin/ld: lib/libdruntime-ldc.a(libunwind.o)(.text._D3ldc2eh6common61__T21eh_personality_commonTS3ldc2eh9libunwind13NativeContextZ21eh_personality_commonUKS3ldc2eh9libunwind13NativeContextZ3acbMFNbNcNiNfZPS3ldc2eh6common18ActiveCleanupBlock[_D3ldc2eh6common61__T21eh_personality_commonTS3ldc2eh9libunwind13NativeContextZ21eh_personality_commonUKS3ldc2eh9libunwind13NativeContextZ3acbMFNbNcNiNfZPS3ldc2eh6common18Active
leanupBlock]+0x38): R_ARM_TLS_IE32 used with non-TLS symbol
_D3ldc2eh6common21innermostCleanupBlockPS3ldc2eh6common18ActiveCleanupBlock




R_ARM_TLS_IE32 used with non-TLS symbol ??
Jul 20 2016
parent reply Claude <no no.no> writes:
On Wednesday, 20 July 2016 at 16:10:48 UTC, Claude wrote:
 R_ARM_TLS_IE32 used with non-TLS symbol ??
Oh, that was actually quite obvious... If I revert the first android patch on LLVM sources, and build it back it works! I can build a "Hello world" program on ARM GNU/Linux, with druntime and phobos. I'll write a doc page about that.
Jul 21 2016
parent reply Andrea Fontana <nospam example.com> writes:
On Thursday, 21 July 2016 at 09:59:53 UTC, Claude wrote:
 I can build a "Hello world" program on ARM GNU/Linux, with 
 druntime and phobos.
 I'll write a doc page about that.
It's a good idea :)
Jul 21 2016
parent reply Claude <no no.no> writes:
On Thursday, 21 July 2016 at 10:30:55 UTC, Andrea Fontana wrote:
 On Thursday, 21 July 2016 at 09:59:53 UTC, Claude wrote:
 I can build a "Hello world" program on ARM GNU/Linux, with 
 druntime and phobos.
 I'll write a doc page about that.
It's a good idea :)
Done: https://wiki.dlang.org/LDC_cross-compilation_for_ARM_GNU/Linux I based it totally on Kai's previous page for LDC on Android. It lacks the build for druntime/phobos unit-tests.
Jul 21 2016
next sibling parent Kai Nacke <kai redstar.de> writes:
On Thursday, 21 July 2016 at 13:13:39 UTC, Claude wrote:
 On Thursday, 21 July 2016 at 10:30:55 UTC, Andrea Fontana wrote:
 On Thursday, 21 July 2016 at 09:59:53 UTC, Claude wrote:
 I can build a "Hello world" program on ARM GNU/Linux, with 
 druntime and phobos.
 I'll write a doc page about that.
It's a good idea :)
Done: https://wiki.dlang.org/LDC_cross-compilation_for_ARM_GNU/Linux I based it totally on Kai's previous page for LDC on Android. It lacks the build for druntime/phobos unit-tests.
Thanks! That's really awesome! Did you manage to build more complex applications? EABI is a bit different from the hardfloat ABI and there may be still bugs lurking in LDC... Regards, Kai
Jul 31 2016
prev sibling parent reply Joakim <dlang joakim.fea.st> writes:
On Thursday, 21 July 2016 at 13:13:39 UTC, Claude wrote:
 On Thursday, 21 July 2016 at 10:30:55 UTC, Andrea Fontana wrote:
 On Thursday, 21 July 2016 at 09:59:53 UTC, Claude wrote:
 I can build a "Hello world" program on ARM GNU/Linux, with 
 druntime and phobos.
 I'll write a doc page about that.
It's a good idea :)
Done: https://wiki.dlang.org/LDC_cross-compilation_for_ARM_GNU/Linux I based it totally on Kai's previous page for LDC on Android. It lacks the build for druntime/phobos unit-tests.
Sorry, I didn't see this thread till now, or I could have saved you some time by telling you not to apply the llvm patch on non-Android linux. Note that you don't have to compile llvm yourself at all, as long as the system llvm has the ARM backend built in, as it often does.
Aug 01 2016
parent Claude <no no.no> writes:
On Monday, 1 August 2016 at 06:21:48 UTC, Kai Nacke wrote:
 Thanks! That's really awesome!

 Did you manage to build more complex applications? EABI is a 
 bit different from the hardfloat ABI and there may be still 
 bugs lurking in LDC...
Unfortunately no, I didn't have the time. I was interested in building audio applications in D, but I do not use much float arithmetic on embedded systems (I prefer integer/fixed-point over it). Anyway I have some pieces of DSP algorithms I could try out in float (FFT, biquads, FIR etc). I could also try to run the phobos test suite on the board I use, if there is an easy way to do it (I'm pretty new to all this). On Tuesday, 2 August 2016 at 04:19:15 UTC, Joakim wrote:
 Sorry, I didn't see this thread till now, or I could have saved 
 you some time by telling you not to apply the llvm patch on 
 non-Android linux.  Note that you don't have to compile llvm 
 yourself at all, as long as the system llvm has the ARM backend 
 built in, as it often does.
Ah ok. I am totally new to llvm. I did it the hard way. :)
Aug 09 2016