Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MULADDC_X2_CORE ARM crashes the linker (ld) (Android build) #6472

Closed
0x416c69 opened this issue Oct 22, 2022 · 3 comments
Closed

MULADDC_X2_CORE ARM crashes the linker (ld) (Android build) #6472

0x416c69 opened this issue Oct 22, 2022 · 3 comments

Comments

@0x416c69
Copy link

Summary

Linker crashes only when compiling with optimization and LTO (I haven't tested if LTO is related or not, but the debug builds, i.e. no optimization compiles which I suspect is because MULADDC_CANNOT_USE_R7 is defined when compiling in debug mode which doesn't use the inline assembly at all) (As a work around I'm undefining MBEDTLS_HAVE_ASM)

By the way the build is in THUMB-v2.

 invalid instruction, any one of the following would fix this:
        ldr.w    r0, [r6,  #-4]
        ^
<inline asm>:4:25: note: too few operands for instruction
        ldr.w    r0, [r6,  #-4]
                               ^
<inline asm>:4:15: note: invalid operand for instruction
        ldr.w    r0, [r6,  #-4]
                     ^
 invalid instruction, any one of the following would fix this:
        ldr.w    r5, [r1, #-4]
        ^
<inline asm>:5:24: note: too few operands for instruction
        ldr.w    r5, [r1, #-4]
                              ^
<inline asm>:5:15: note: invalid operand for instruction
        ldr.w    r5, [r1, #-4]
                     ^
 invalid instruction, any one of the following would fix this:
        str.w    r3, [r1, #-8]
        ^
<inline asm>:8:24: note: too few operands for instruction
        str.w    r3, [r1, #-8]
                              ^
<inline asm>:8:15: note: invalid operand for instruction
        str.w    r3, [r1, #-8]
                     ^
 invalid instruction, any one of the following would fix this:
        str.w    r5, [r1, #-4]
        ^
<inline asm>:9:24: note: too few operands for instruction
        str.w    r5, [r1, #-4]
                              ^
<inline asm>:9:15: note: invalid operand for instruction
        str.w    r5, [r1, #-4]
                     ^
 invalid instruction, any one of the following would fix this:
        ldr.w    r0, [r6,  #-4]
        ^
<inline asm>:13:25: note: too few operands for instruction
        ldr.w    r0, [r6,  #-4]
                               ^
<inline asm>:13:15: note: invalid operand for instruction
        ldr.w    r0, [r6,  #-4]
                     ^
 invalid instruction, any one of the following would fix this:
        ldr.w    r5, [r1, #-4]
        ^
<inline asm>:14:24: note: too few operands for instruction
        ldr.w    r5, [r1, #-4]
                              ^
<inline asm>:14:15: note: invalid operand for instruction
        ldr.w    r5, [r1, #-4]
                     ^
 invalid instruction, any one of the following would fix this:
        str.w    r3, [r1, #-8]
        ^
<inline asm>:17:24: note: too few operands for instruction
        str.w    r3, [r1, #-8]
                              ^
<inline asm>:17:15: note: invalid operand for instruction
        str.w    r3, [r1, #-8]
                     ^
 invalid instruction, any one of the following would fix this:
        str.w    r5, [r1, #-4]
        ^
<inline asm>:18:24: note: too few operands for instruction
        str.w    r5, [r1, #-4]
                              ^
<inline asm>:18:15: note: invalid operand for instruction
        str.w    r5, [r1, #-4]
                     ^
 invalid instruction, any one of the following would fix this:
        ldr.w    r0, [r6,  #-4]
        ^
<inline asm>:22:25: note: too few operands for instruction
        ldr.w    r0, [r6,  #-4]
                               ^
<inline asm>:22:15: note: invalid operand for instruction
        ldr.w    r0, [r6,  #-4]
                     ^
 invalid instruction, any one of the following would fix this:
        ldr.w    r5, [r1, #-4]
        ^
<inline asm>:23:24: note: too few operands for instruction
        ldr.w    r5, [r1, #-4]
                              ^
<inline asm>:23:15: note: invalid operand for instruction
        ldr.w    r5, [r1, #-4]
                     ^
 invalid instruction, any one of the following would fix this:
        str.w    r3, [r1, #-8]
        ^
<inline asm>:26:24: note: too few operands for instruction
        str.w    r3, [r1, #-8]
                              ^
<inline asm>:26:15: note: invalid operand for instruction
        str.w    r3, [r1, #-8]
                     ^
 invalid instruction, any one of the following would fix this:
        str.w    r5, [r1, #-4]
        ^
<inline asm>:27:24: note: too few operands for instruction
        str.w    r5, [r1, #-4]
                              ^
<inline asm>:27:15: note: invalid operand for instruction
        str.w    r5, [r1, #-4]
                     ^
 invalid instruction, any one of the following would fix this:
        ldr.w    r0, [r6,  #-4]
        ^
<inline asm>:31:25: note: too few operands for instruction
        ldr.w    r0, [r6,  #-4]
                               ^
<inline asm>:31:15: note: invalid operand for instruction
        ldr.w    r0, [r6,  #-4]
                     ^
 invalid instruction, any one of the following would fix this:
        ldr.w    r5, [r1, #-4]
        ^
<inline asm>:32:24: note: too few operands for instruction
        ldr.w    r5, [r1, #-4]
                              ^
<inline asm>:32:15: note: invalid operand for instruction
        ldr.w    r5, [r1, #-4]
                     ^
 invalid instruction, any one of the following would fix this:
        str.w    r3, [r1, #-8]
        ^
<inline asm>:35:24: note: too few operands for instruction
        str.w    r3, [r1, #-8]
                              ^
<inline asm>:35:15: note: invalid operand for instruction
        str.w    r3, [r1, #-8]
                     ^
 invalid instruction, any one of the following would fix this:
        str.w    r5, [r1, #-4]
        ^
<inline asm>:36:24: note: too few operands for instruction
        str.w    r5, [r1, #-4]
                              ^
<inline asm>:36:15: note: invalid operand for instruction
        str.w    r5, [r1, #-4]
                     ^
LLVM ERROR: Error parsing inline asm
PLEASE submit a bug report to https://github.com/android-ndk/ndk/issues and include the crash backtrace.
Stack dump:
0.	Program arguments: D:\\android-ndk-r23\\toolchains\\llvm\\prebuilt\\windows-x86_64\\bin\\ld --sysroot=D:\\android-ndk-r23\\toolchains\\llvm\\prebuilt\\windows-x86_64\\sysroot -z noexecstack -EL --warn-shared-textrel -z now -z relro -z max-page-size=4096 -X --hash-style=both --enable-new-dtags --eh-frame-hdr -m armelf_linux_eabi -shared -o obj/ARM/Beta/UnitTestLib\\libUnitTestLib.so D:\\android-ndk-r23\\toolchains\\llvm\\prebuilt\\windows-x86_64\\sysroot/usr/lib/arm-linux-androideabi/16\\crtbegin_so.o -LD:\\android-ndk-r23\\toolchains\\llvm\\prebuilt\\windows-x86_64\\lib64\\clang\\12.0.9\\lib\\linux\\arm -LD:\\android-ndk-r23\\toolchains\\llvm\\prebuilt\\windows-x86_64\\sysroot/usr/lib/arm-linux-androideabi/16 -LD:\\android-ndk-r23\\toolchains\\llvm\\prebuilt\\windows-x86_64\\sysroot/usr/lib/arm-linux-androideabi -LD:\\android-ndk-r23\\toolchains\\llvm\\prebuilt\\windows-x86_64\\sysroot/usr/lib/../lib -LD:\\android-ndk-r23\\toolchains\\llvm\\prebuilt\\windows-x86_64\\sysroot/usr/lib -plugin D:\\android-ndk-r23\\toolchains\\llvm\\prebuilt\\windows-x86_64\\bin\\..\\lib64\\LLVMgold.dll -plugin-opt=mcpu=generic -soname=libUnitTestLib.so -rpath-link=D:\\android-ndk-r23\\toolchains\\llvm\\prebuilt\\windows-x86_64\\sysroot\\usr\\lib\\arm-linux-androideabi -rpath-link=D:\\android-ndk-r23\\toolchains\\llvm\\prebuilt\\windows-x86_64\\sysroot\\usr\\lib\\arm-linux-androideabi\\16 -LD:\\android-ndk-r23\\toolchains\\llvm\\prebuilt\\windows-x86_64\\sysroot\\usr\\lib\\arm-linux-androideabi -LD:\\android-ndk-r23\\toolchains\\llvm\\prebuilt\\windows-x86_64\\sysroot\\usr\\lib\\arm-linux-androideabi\\16 -LD:\\android-ndk-r23\\toolchains\\arm-linux-androideabi-4.9\\prebuilt\\windows-x86_64\\lib\\gcc\\arm-linux-androideabi\\4.9.x\\armv7-a -LD:\\android-ndk-r23\\sources\\cxx-stl\\llvm-libc++\\libs\\armeabi-v7a --no-undefined -z relro -z now -z noexecstack obj/ARM/Beta/UnitTestLib\\tests.o obj/ARM/Beta/UnitTestLib\\Exports.o D:\\[Redacted]\\ARM\\Beta\\libsdk.a D:\\[Redacted]\\ARM\\Beta\\libmbedtls.a D:\\[Redacted]\\ARM\\Beta\\libcurl.a D:\\[Redacted]\\ARM\\Beta\\libzlib.a -lc++_shared -lc++abi -llog -landroid -lm -llog D:\\android-ndk-r23\\toolchains\\llvm\\prebuilt\\windows-x86_64\\lib64\\clang\\12.0.9\\lib\\linux\\libclang_rt.builtins-arm-android.a -l:libunwind.a -ldl -lc D:\\android-ndk-r23\\toolchains\\llvm\\prebuilt\\windows-x86_64\\lib64\\clang\\12.0.9\\lib\\linux\\libclang_rt.builtins-arm-android.a -l:libunwind.a -ldl D:\\android-ndk-r23\\toolchains\\llvm\\prebuilt\\windows-x86_64\\sysroot/usr/lib/arm-linux-androideabi/16\\crtend_so.o
1.	Running pass 'Function Pass Manager' on module 'ld-temp.o'.
2.	Running pass 'ARM Assembly Printer' on function '@mbedtls_mpi_mul_int'
 #0 0x00007ff6ed584e76 (D:\android-ndk-r23\toolchains\llvm\prebuilt\windows-x86_64\bin\ld.exe+0xc4e76)
 #1 0x00007ffc10401881 (C:\WINDOWS\System32\ucrtbase.dll+0x71881)
 #2 0x00007ffc10402851 (C:\WINDOWS\System32\ucrtbase.dll+0x72851)
 #3 0x00007ff6ed582e1e (D:\android-ndk-r23\toolchains\llvm\prebuilt\windows-x86_64\bin\ld.exe+0xc2e1e)
 #4 0x00007ff6ed582c55 (D:\android-ndk-r23\toolchains\llvm\prebuilt\windows-x86_64\bin\ld.exe+0xc2c55)
 #5 0x00007ff6edef5ee7 (D:\android-ndk-r23\toolchains\llvm\prebuilt\windows-x86_64\bin\ld.exe+0xa35ee7)
 #6 0x00007ff6edef76c8 (D:\android-ndk-r23\toolchains\llvm\prebuilt\windows-x86_64\bin\ld.exe+0xa376c8)
 #7 0x00007ff6edeceb7f (D:\android-ndk-r23\toolchains\llvm\prebuilt\windows-x86_64\bin\ld.exe+0xa0eb7f)
 #8 0x00007ff6ed6c8f84 (D:\android-ndk-r23\toolchains\llvm\prebuilt\windows-x86_64\bin\ld.exe+0x208f84)
 #9 0x00007ff6edb6e0b3 (D:\android-ndk-r23\toolchains\llvm\prebuilt\windows-x86_64\bin\ld.exe+0x6ae0b3)
#10 0x00007ff6edac9388 (D:\android-ndk-r23\toolchains\llvm\prebuilt\windows-x86_64\bin\ld.exe+0x609388)
#11 0x00007ff6edacfb93 (D:\android-ndk-r23\toolchains\llvm\prebuilt\windows-x86_64\bin\ld.exe+0x60fb93)
#12 0x00007ff6edac9bb2 (D:\android-ndk-r23\toolchains\llvm\prebuilt\windows-x86_64\bin\ld.exe+0x609bb2)
#13 0x00007ff6ee006ca6 (D:\android-ndk-r23\toolchains\llvm\prebuilt\windows-x86_64\bin\ld.exe+0xb46ca6)
#14 0x00007ff6ee00600b (D:\android-ndk-r23\toolchains\llvm\prebuilt\windows-x86_64\bin\ld.exe+0xb4600b)
#15 0x00007ff6ed6fd803 (D:\android-ndk-r23\toolchains\llvm\prebuilt\windows-x86_64\bin\ld.exe+0x23d803)
#16 0x00007ff6ed6fcbf5 (D:\android-ndk-r23\toolchains\llvm\prebuilt\windows-x86_64\bin\ld.exe+0x23cbf5)
#17 0x00007ff6ed8de091 (D:\android-ndk-r23\toolchains\llvm\prebuilt\windows-x86_64\bin\ld.exe+0x41e091)
#18 0x00007ff6ed552f93 (D:\android-ndk-r23\toolchains\llvm\prebuilt\windows-x86_64\bin\ld.exe+0x92f93)
#19 0x00007ff6ed54bb1e (D:\android-ndk-r23\toolchains\llvm\prebuilt\windows-x86_64\bin\ld.exe+0x8bb1e)
#20 0x00007ff6ed539f54 (D:\android-ndk-r23\toolchains\llvm\prebuilt\windows-x86_64\bin\ld.exe+0x79f54)
#21 0x00007ff6ed5387c2 (D:\android-ndk-r23\toolchains\llvm\prebuilt\windows-x86_64\bin\ld.exe+0x787c2)
#22 0x00007ff6ed4c1f7c (D:\android-ndk-r23\toolchains\llvm\prebuilt\windows-x86_64\bin\ld.exe+0x1f7c)
#23 0x00007ff6ed4c16cb (D:\android-ndk-r23\toolchains\llvm\prebuilt\windows-x86_64\bin\ld.exe+0x16cb)
#24 0x00007ff6ed4c1395 (D:\android-ndk-r23\toolchains\llvm\prebuilt\windows-x86_64\bin\ld.exe+0x1395)
#25 0x00007ff6ed4c14c8 (D:\android-ndk-r23\toolchains\llvm\prebuilt\windows-x86_64\bin\ld.exe+0x14c8)
#26 0x00007ffc11d47034 (C:\WINDOWS\System32\KERNEL32.DLL+0x17034)
#27 0x00007ffc12ca2651 (C:\WINDOWS\SYSTEM32\ntdll.dll+0x52651)
clang: error: linker command failed due to signal (use -v to see invocation)

System information

Mbed TLS version (number or commit id): 0x03020100
Operating system and version: Windows 10, cross compiling for android
Configuration (if not default, please attach mbedtls_config.h): I believe it is the default
Compiler and options (if you used a pre-built binary, please indicate how you obtained it): Visible in the summary
Additional environment information:

Expected behavior

To not crash.

Actual behavior

It crashes.

Steps to reproduce

Compile and link for ARMv7-A THUMB-2 with optimization and -flto

Additional information

Nah

@gilles-peskine-arm
Copy link
Contributor

This looks like the same issue as #6089,
which will be fixed in the next release. Can you please try either the development branch, or just the patch from #6090 ?

If not, can you please share which compiler options are used, in particular the exact target? I can see the compiler version in the log, but I don't know exactly how the Android NDK settings map to compiler options.

@0x416c69
Copy link
Author

According to https://developer.arm.com/documentation/ddi0406/latest
Head over to A8.8.63, if you have a look at Encoding T3, you shall see that ldr.w does not support negative indexing.

I believe you're using LDR.W because of this comment:

Make sure loop is 4-byte aligned to avoid stalls upon repeated non-word aligned instructions in some microarchitectures.

But how does this make any sense on thumb2 environment? What is this argument even based on?

@0x416c69
Copy link
Author

0x416c69 commented Oct 22, 2022

@gilles-peskine-arm

Can you please try either the development branch, or just the patch from #6090 ?

Yeah, before you replied I wasn't aware of the PR, in fact I've fixed it by getting rid of the .w suffix and I was writing the comment for it and I was going to make a PR but that PR looks to be right. Thanks!

I hope the next release comes soon otherwise some other poor soul is gonna take some time to look into this again 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants