ldc-developers / ldc2.snap

Snap package definition for LDC, the LLVM-based D compiler
11 stars 4 forks source link

Please add ARM compiler RT library #97

Open denizzzka opened 4 years ago

denizzzka commented 4 years ago

Please add ARM compiler RT library to prevent linking errors while cross-compile. Errors like:

ld.lld: error: undefined symbol: __aeabi_uldivmod
denizzzka commented 4 years ago

Strictly I could not verify it at 100% because built-in linker is not working for cross compiling ARM

void main()
{
    long a = 1;
    long b = 1;

    long c = a / b;
}
ldc2 --static --linker=lld -march=thumb -mcpu=cortex-m3 app.d
ld.lld: error: unknown argument: --no-warn-search-mismatch
collect2: error: ld returned 1 exit status
denizzzka commented 4 years ago

All the same, anyway, an external linker has to take this library from somewhere.

WebDrake commented 4 years ago

Thanks for reporting this. Which version are you using? Can you give me the result of snap list ldc2 just to confirm?

denizzzka commented 4 years ago
Name  Version  Rev  Tracking  Publisher  Notes
ldc2  1.18.0   115  stable    ldc        classic
denizzzka commented 4 years ago

Looks like it is need to add:

        -DCOMPILER_RT_BUILD_BUILTINS=ON \
        -DCOMPILER_RT_BAREMETAL_BUILD=ON \

into llvm section of snapcraft.yaml

it would also be cool to add libunwind because it is sort of runtime library for dlang without betterC

        -DLLVM_ENABLE_PROJECTS='compiler-rt;lld;libunwind' \
WebDrake commented 4 years ago

OK, so let's try and break this down following @kinke's feedback on https://github.com/ldc-developers/ldc2.snap/pull/101.

Some of the reported errors appear to be unrelated to ARM and are instead about using LLD. I can reproduce with the following steps:

This results in the error:

/usr/bin/ld.lld: error: unknown argument: --no-warn-search-mismatch
collect2: error: ld returned 1 exit status
Error: /usr/bin/gcc failed with status: 1

If I do not have lld installed on my host system, instead I get:

collect2: fatal error: cannot find 'ld'
compilation terminated.
Error: /usr/bin/gcc failed with status: 1

If instead I try to build with

ldc2 --static --link-internally hello.d

I get the errors:

lld: error: unknown argument '--no-warn-search-mismatch'
lld: error: unable to find library -lrt
lld: error: unable to find library -ldl
lld: error: unable to find library -lpthread
lld: error: unable to find library -lm
Error: linking with LLD failed

... which suggests that lld can be used per se, but the system libraries it requires/expects are missing from the snap package.

The --no-warn-search-mismatch flag is coming from ldc2.conf, and should not be used with LLD as it does not support it, see: https://github.com/ldc-developers/ldc/issues/2717#issuecomment-392882094

The flag is used with the snap package, but not with the standard LDC binaries, to avoid linker errors when searching for paths that do not exist. Essentially it's a workaround for including all of lib, lib32 and lib64 in the search path. So we probably ought to address this separately.

That aside, it's clear we have (at least) 2 separate problems here:

A related question is how much the snap package should aim to fulfil that goal. For example: what is the current state of support provided by LDC binary packages?

@denizzzka are you able/willing to try using one of the LDC binary packages downloadable from GitHub (https://github.com/ldc-developers/ldc/releases/tag/v1.18.0 should give you a comparable setup) and compare how it works when trying to cross-compile, with and without using lld as the linker?

denizzzka commented 4 years ago

Some of the reported errors appear to be unrelated to ARM and are instead about using LLD.

What kind of erros?

WebDrake commented 4 years ago

The unknown argument error reported in https://github.com/ldc-developers/ldc2.snap/issues/97#issuecomment-591975815.

kinke commented 4 years ago

The lld arg has been resolved with v1.20...

WebDrake commented 4 years ago

On the more general problem: multi-arch cross-compiling support for snap packages is non-trivial in general. To see why, consider the following difference between the linking of 64-bit and 32-bit libs in the snap package:

64-bit:

$ ldd /snap/ldc2/current/lib64/libphobos2-ldc-shared.so
    linux-vdso.so.1 (0x00007ffd555da000)
    libgtk3-nocsd.so.0 => /usr/lib/x86_64-linux-gnu/libgtk3-nocsd.so.0 (0x00007f4a2b3e1000)
    libdruntime-ldc-shared.so.88 => /snap/ldc2/current/lib64/libdruntime-ldc-shared.so.88 (0x00007f4a2b078000)
    libm.so.6 => /snap/core/current/lib/x86_64-linux-gnu/libm.so.6 (0x00007f4a2ad6f000)
    libpthread.so.0 => /snap/core/current/lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f4a2ab52000)
    librt.so.1 => /snap/core/current/lib/x86_64-linux-gnu/librt.so.1 (0x00007f4a2a94a000)
    libdl.so.2 => /snap/core/current/lib/x86_64-linux-gnu/libdl.so.2 (0x00007f4a2a746000)
    libgcc_s.so.1 => /snap/core/current/lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f4a2a530000)
    libc.so.6 => /snap/core/current/lib/x86_64-linux-gnu/libc.so.6 (0x00007f4a2a166000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f4a2bd94000)

32-bit:

$ ldd /snap/ldc2/current/lib32/libphobos2-ldc-shared.so
ERROR: ld.so: object 'libgtk3-nocsd.so.0' from LD_PRELOAD cannot be preloaded (cannot open shared object file): ignored.
    linux-gate.so.1 (0xf7f0c000)
    libdruntime-ldc-shared.so.88 => /snap/ldc2/current/lib32/libdruntime-ldc-shared.so.88 (0xf7834000)
    libm.so.6 => /lib/i386-linux-gnu/libm.so.6 (0xf7732000)
    libpthread.so.0 => /lib/i386-linux-gnu/libpthread.so.0 (0xf7713000)
    librt.so.1 => /lib/i386-linux-gnu/librt.so.1 (0xf7709000)
    libdl.so.2 => /lib/i386-linux-gnu/libdl.so.2 (0xf7704000)
    libgcc_s.so.1 => /lib/i386-linux-gnu/libgcc_s.so.1 (0xf76e6000)
    libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xf750a000)
    /lib/ld-linux.so.2 (0xf7f0d000)

This is almost certainly why the snap package (as currently written) reports some missing stage-packages during the build. This can in principle be addressed by the following diff:

diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml
index 0171f08..15b55cc 100644
--- a/snap/snapcraft.yaml
+++ b/snap/snapcraft.yaml
@@ -58,6 +58,13 @@ parts:
     - g++-multilib
     - ninja-build
     - zlib1g-dev
+    stage-packages:
+    - on amd64:
+      - lib32gcc1
+      - libc6-i386
+    - on i386:
+      - lib64gcc1
+      - libc6-amd64
     after:
     - ctest-setup
     - ldc-bootstrap

... after which the 32-bit libs link up against those staged packages instead:

$ ldd /snap/ldc2/current/lib32/libphobos2-ldc-shared.so
ERROR: ld.so: object 'libgtk3-nocsd.so.0' from LD_PRELOAD cannot be preloaded (cannot open shared object file): ignored.
    linux-gate.so.1 (0xf7fc3000)
    libdruntime-ldc-shared.so.88 => /snap/ldc2/current/lib32/libdruntime-ldc-shared.so.88 (0xf78eb000)
    libm.so.6 => /snap/ldc2/current/lib32/libm.so.6 (0xf7896000)
    libpthread.so.0 => /snap/ldc2/current/lib32/libpthread.so.0 (0xf77ab000)
    librt.so.1 => /snap/ldc2/current/lib32/librt.so.1 (0xf77a2000)
    libdl.so.2 => /snap/ldc2/current/lib32/libdl.so.2 (0xf779d000)
    libgcc_s.so.1 => /lib/i386-linux-gnu/libgcc_s.so.1 (0xf777f000)
    libc.so.6 => /snap/ldc2/current/lib32/libc.so.6 (0xf75ca000)
    /lib/ld-linux.so.2 (0xf7fc4000)
WebDrake commented 4 years ago

The lld arg has been resolved with v1.20...

I've created a separate issue for this: https://github.com/ldc-developers/ldc2.snap/issues/102.