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

clang with -flto argument result in File format not recognized while linking #242

Closed
kuldeepdhaka opened this issue Nov 21, 2016 · 1 comment

Comments

@kuldeepdhaka
Copy link

kuldeepdhaka commented Nov 21, 2016

[kuldeep@madresistor ndk-test]$ ndk-build 
[arm64-v8a] Compile        : testme <= test.c
[arm64-v8a] SharedLibrary  : libtestme.so
./obj/local/arm64-v8a/objs/testme/__/test.o: file not recognized: File format not recognized
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [/home/kuldeep/software/android-ndk-r13b/build/core/build-binary.mk:703: obj/local/arm64-v8a/libtestme.so] Error 1

ndk-test.zip

Tested: NDK r13b

modify the Android.mk and remove the LOCAL_CFLAGS += -flto to make it compile.

without -flto argument:

[kuldeep@madresistor ndk-test]$ ndk-build 
[arm64-v8a] Compile        : testme <= test.c
[arm64-v8a] SharedLibrary  : libtestme.so
[arm64-v8a] Install        : libtestme.so => libs/arm64-v8a/libtestme.so
[x86_64] Compile        : testme <= test.c
[x86_64] SharedLibrary  : libtestme.so
[x86_64] Install        : libtestme.so => libs/x86_64/libtestme.so
[mips64] Compile        : testme <= test.c
[mips64] SharedLibrary  : libtestme.so
[mips64] Install        : libtestme.so => libs/mips64/libtestme.so
[armeabi-v7a] Compile thumb  : testme <= test.c
[armeabi-v7a] SharedLibrary  : libtestme.so
[armeabi-v7a] Install        : libtestme.so => libs/armeabi-v7a/libtestme.so
[armeabi] Compile thumb  : testme <= test.c
[armeabi] SharedLibrary  : libtestme.so
[armeabi] Install        : libtestme.so => libs/armeabi/libtestme.so
[x86] Compile        : testme <= test.c
[x86] SharedLibrary  : libtestme.so
[x86] Install        : libtestme.so => libs/x86/libtestme.so
[mips] Compile        : testme <= test.c
[mips] SharedLibrary  : libtestme.so
[mips] Install        : libtestme.so => libs/mips/libtestme.so
@DanAlbert
Copy link
Member

Aha, figured this one out.

Clang only supports LTO when using ld.gold, but arm64 still defaults to ld.bfd (not really a good reason for this any more I don't think, I should change that).

To fix your Android.mk:

# Gold does not support mips or mips64, but gold is needed for LTO with Clang.
ifeq (,$(filter $(TARGET_ARCH_ABI),mips mips64)
    LOCAL_CFLAGS += -flto
    LOCAL_LDFLAGS := -flto -fuse-ld=gold
endif

username223 added a commit to OpenVR-Advanced-Settings/OpenVR-AdvancedSettings that referenced this issue Apr 9, 2020
Using `-flto` on clang without using the gold linker produces the error:
```main.o: file not recognized: File format not recognized```
android/ndk#242

`gold` seems to be faster than `ld`, and decently tested even in 2010:
https://stackoverflow.com/questions/3476093/replacing-ld-with-gold-any-experience

Qmake will still append `-Wl,-O1` even though higher optimization levels
are used. Removing this in order to keep the compile command as short as
possible, and to reduce weird issues down the line. Ref topic:
https://stackoverflow.com/questions/53384988/why-qmake-passes-wl-o1-to-gcc-when-linking-and-will-it-harm-lto
username223 added a commit to OpenVR-Advanced-Settings/OpenVR-AdvancedSettings that referenced this issue Apr 9, 2020
Using `-flto` on clang without using the gold linker produces the error:
```main.o: file not recognized: File format not recognized```
android/ndk#242

`gold` seems to be faster than `ld`, and decently tested even in 2010:
https://stackoverflow.com/questions/3476093/replacing-ld-with-gold-any-experience

Qmake will still append `-Wl,-O1` even though higher optimization levels
are used. Removing this in order to keep the compile command as short as
possible, and to reduce weird issues down the line. Ref topic:
https://stackoverflow.com/questions/53384988/why-qmake-passes-wl-o1-to-gcc-when-linking-and-will-it-harm-lto
username223 added a commit to OpenVR-Advanced-Settings/OpenVR-AdvancedSettings that referenced this issue Apr 11, 2020
Using `-flto` on clang without using the gold linker produces the error:
```main.o: file not recognized: File format not recognized```
android/ndk#242

`gold` seems to be faster than `ld`, and decently tested even in 2010:
https://stackoverflow.com/questions/3476093/replacing-ld-with-gold-any-experience

Qmake will still append `-Wl,-O1` even though higher optimization levels
are used. Removing this in order to keep the compile command as short as
possible, and to reduce weird issues down the line. Ref topic:
https://stackoverflow.com/questions/53384988/why-qmake-passes-wl-o1-to-gcc-when-linking-and-will-it-harm-lto
username223 added a commit to OpenVR-Advanced-Settings/OpenVR-AdvancedSettings that referenced this issue Apr 11, 2020
Using `-flto` on clang without using the gold linker produces the error:
```main.o: file not recognized: File format not recognized```
android/ndk#242

`gold` seems to be faster than `ld`, and decently tested even in 2010:
https://stackoverflow.com/questions/3476093/replacing-ld-with-gold-any-experience

Qmake will still append `-Wl,-O1` even though higher optimization levels
are used. Removing this in order to keep the compile command as short as
possible, and to reduce weird issues down the line. Ref topic:
https://stackoverflow.com/questions/53384988/why-qmake-passes-wl-o1-to-gcc-when-linking-and-will-it-harm-lto
username223 added a commit to OpenVR-Advanced-Settings/OpenVR-AdvancedSettings that referenced this issue Apr 11, 2020
Using `-flto` on clang without using the gold linker produces the error:
```main.o: file not recognized: File format not recognized```
android/ndk#242

`gold` seems to be faster than `ld`, and decently tested even in 2010:
https://stackoverflow.com/questions/3476093/replacing-ld-with-gold-any-experience

Qmake will still append `-Wl,-O1` even though higher optimization levels
are used. Removing this in order to keep the compile command as short as
possible, and to reduce weird issues down the line. Ref topic:
https://stackoverflow.com/questions/53384988/why-qmake-passes-wl-o1-to-gcc-when-linking-and-will-it-harm-lto
username223 added a commit to OpenVR-Advanced-Settings/OpenVR-AdvancedSettings that referenced this issue Apr 11, 2020
Using `-flto` on clang without using the gold linker produces the error:
```main.o: file not recognized: File format not recognized```
android/ndk#242

`gold` seems to be faster than `ld`, and decently tested even in 2010:
https://stackoverflow.com/questions/3476093/replacing-ld-with-gold-any-experience

Qmake will still append `-Wl,-O1` even though higher optimization levels
are used. Removing this in order to keep the compile command as short as
possible, and to reduce weird issues down the line. Ref topic:
https://stackoverflow.com/questions/53384988/why-qmake-passes-wl-o1-to-gcc-when-linking-and-will-it-harm-lto
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