ndk icon indicating copy to clipboard operation
ndk copied to clipboard

investigate strange lld behavior reported on SO

Open DanAlbert opened this issue 7 years ago • 20 comments

https://stackoverflow.com/q/55014879/632035

Whatever is going on in this bug is strange. Looks like the user was using lld and having issues with --gc-sections. Should see if we can figure out what's causing that...

DanAlbert avatar Mar 08 '19 08:03 DanAlbert

This seems like a fairly common error in lld. Do a google search for "in global part of symbol table" (and also in internal bug tracker. b/77634281 for when we saw it in the Android platform).

The ideal resolution is to link everything with LLD. Since that's not always feasible, other workarounds are to use --no-fatal-warnings or remove --gc-sections. I am not entirely sure why the latter matters for this error.

pirama-arumuga-nainar avatar Mar 08 '19 22:03 pirama-arumuga-nainar

Is there a third workaround? Or maybe a fix? Android really needs --fatal-warnings, --gc-sections is also very important, and app developers don't have a say in how their middleware is linked.

DanAlbert avatar Mar 08 '19 23:03 DanAlbert

Here is a simple way to reproduce the problem.

hello-jni/jni/Android.mk
    LOCAL_PATH := $(call my-dir)
    include $(CLEAR_VARS)
    LOCAL_MODULE    := hello-jni
    LOCAL_SRC_FILES := hello-jni.c
    include $(BUILD_SHARED_LIBRARY)

hello-jni/jni/Application.mk
    APP_CFLAGS := -fstack-protector-all -fvisibility=hidden -ffunction-sections -fdata-sections
    APP_LDFLAGS := -Wl,--gc-sections,-fvisibility=hidden,--strip-debug

hello-jni/jni/hello-jni.c
    #include <string.h>
    #include <jni.h>
    JNIEXPORT jstring JNICALL
    Java_com_example_hellojni_HelloJni_stringFromJNI(JNIEnv* env,
                                                     jobject thiz) {
        return (*env)->NewStringUTF(env, "Hello from JNI!");
    }

To build it, cd hello-jni and enter NDK_PROJECT_PATH=. ~/Android/Sdk/ndk-bundle/ndk-build APP_PLATFORM=android-23 NDK_DEBUG=0 V=1 -B. The output file will be in libs/{arm64-v8a,armeabi-v7a,x86,x86_64}/libhello-jni.so.

In short, __bss_start, _edata, and _end are in LOCAL for arm64-v8a, but they are all GLOBAL in other build variants.

$ ~/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android-readelf -s libs/arm64-v8a/libhello-jni.so

Symbol table '.dynsym' contains 15 entries:
Num:    Value          Size Type    Bind   Vis      Ndx Name
    0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
    1: 0000000000000620     0 SECTION LOCAL  DEFAULT   10
    2: 0000000000011000     0 SECTION LOCAL  DEFAULT   18
    3: 0000000000011008     0 NOTYPE  LOCAL  DEFAULT  ABS __bss_start    <-- ?
    4: 0000000000011008     0 NOTYPE  LOCAL  DEFAULT  ABS _edata
    5: 0000000000011008     0 NOTYPE  LOCAL  DEFAULT  ABS _end
    6: 0000000000011008     0 NOTYPE  GLOBAL DEFAULT  ABS _bss_end__
    7: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __cxa_finalize@LIBC (2)
    8: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __register_atfork@LIBC (2)
    9: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __stack_chk_fail@LIBC (2)
    10: 0000000000011008     0 NOTYPE  GLOBAL DEFAULT  ABS __end__
    11: 0000000000011008     0 NOTYPE  GLOBAL DEFAULT  ABS __bss_start__
    12: 0000000000011008     0 NOTYPE  GLOBAL DEFAULT  ABS __bss_end__
    13: 0000000000000668    84 FUNC    GLOBAL DEFAULT   10 Java_com_example_hellojni
    14: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __cxa_atexit@LIBC (2)

Workaround is to remove --gc-sections, which yields a bigger library size if there are large sections of unused code OR --no-fatal-warnings, which might not be an option, depending on how the build is done.

solamour avatar Mar 09 '19 06:03 solamour

I just checked ndk-19 -fuse-ld=lld with android toolchain.cmake

readelf -s libgc_sections.so | grep bss 6: 0000000000000000 0 NOTYPE GLOBAL DEFAULT ABS __bss_start 32: 0000000000000000 0 NOTYPE GLOBAL DEFAULT ABS __bss_start

CMakeLists.txt cmake_minimum_required(VERSION 3.6) project(gc-sections LANGUAGES CXX) set(CMAKE_CXX_FLAGS "-fstack-protector-all -fvisibility=hidden -ffunction-sections -fdata-sections") set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=lld -Wl,--gc-sections -fvisibility=hidden -Wl,--strip-debug" ) add_library( gc_sections SHARED jni.cc )

.cc #include <string.h> #include <jni.h> JNIEXPORT jstring JNICALL Java_com_example_hellojni_HelloJni_stringFromJNI(JNIEnv* env, jobject thiz) { return (*env).NewStringUTF( "Hello from JNI!"); }`

export ANDROID_ABI=arm64-v8a export ANDROID_NDK=/opt/android-ndk-r19

Build /opt/android-sdk/cmake/3.10.2.4988404/bin/cmake -GNinja -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \ -DANDROID_PLATFORM=android-21 \ -DANDROID_ABI=$ANDROID_ABI \ -DANDROID_NDK=$ANDROID_NDK \ -DANDROID_TOOLCHAIN=clang \ -DCMAKE_BUILD_TYPE=Release \ ..

ninja -v [1/2] /opt/android-ndk-r19/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=aarch64-none-linux-android21 --gcc-toolchain=/opt/android-ndk-r19/toolchains/llvm/prebuilt/linux-x86_64 -Dgc_sections_EXPORTS -fstack-protector-all -fvisibility=hidden -ffunction-sections -fdata-sections -O2 -DNDEBUG -fPIC -MD -MT CMakeFiles/gc_sections.dir/jni.cc.o -MF CMakeFiles/gc_sections.dir/jni.cc.o.d -o CMakeFiles/gc_sections.dir/jni.cc.o -c ../jni.cc [2/2] : && /opt/android-ndk-r19/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=aarch64-none-linux-android21 --gcc-toolchain=/opt/android-ndk-r19/toolchains/llvm/prebuilt/linux-x86_64 -fPIC -fstack-protector-all -fvisibility=hidden -ffunction-sections -fdata-sections -O2 -DNDEBUG -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -fuse-ld=lld -Wl,--gc-sections -fvisibility=hidden -Wl,--strip-debug -shared -Wl,-soname,libgc_sections.so -o libgc_sections.so CMakeFiles/gc_sections.dir/jni.cc.o -latomic -lm && :

arturbac avatar Mar 10 '19 16:03 arturbac

-fuse-ld=lld was the missing ingredient. I must have used it incorrectly when I tested it. Anyhow, the following made it work.

APP_LDFLAGS := -Wl,--gc-sections,--strip-debug -fvisibility=hidden -fuse-ld=lld

Thanks everyone for helping me out. Feel free to close the issue.

solamour avatar Mar 11 '19 19:03 solamour

AFIR binutils ld is the default linker still, but im not sure if bfd was the proper default for aarch64, afir there at some point was gold set as default for aarch64. I use from ndk 17 as default linker lld for my project.

-fuse-ld=gold readelf -s libgc_sections.so | grep bss 6: 0000000000020008 0 NOTYPE GLOBAL DEFAULT ABS __bss_start 32: 0000000000020008 0 NOTYPE GLOBAL DEFAULT ABS __bss_start

-fuse-ld=bfd readelf -s libgc_sections.so | grep bss readelf: Warning: local symbol 3 found at index >= .dynsym's sh_info value of 3 readelf: Warning: local symbol 4 found at index >= .dynsym's sh_info value of 3 readelf: Warning: local symbol 5 found at index >= .dynsym's sh_info value of 3 3: 0000000000011008 0 NOTYPE LOCAL DEFAULT ABS _bss_start 6: 0000000000011008 0 NOTYPE GLOBAL DEFAULT ABS bss_end 11: 0000000000011008 0 NOTYPE GLOBAL DEFAULT ABS bss_start 12: 0000000000011008 0 NOTYPE GLOBAL DEFAULT ABS bss_end 46: 0000000000011008 0 NOTYPE LOCAL DEFAULT ABS _bss_start 53: 0000000000011008 0 NOTYPE GLOBAL DEFAULT ABS bss_end 58: 0000000000011008 0 NOTYPE GLOBAL DEFAULT ABS bss_start 59: 0000000000011008 0 NOTYPE GLOBAL DEFAULT ABS bss_end

arturbac avatar Mar 11 '19 21:03 arturbac

And if you build on windows with lld dont forget to disable threads

if(CMAKE_HOST_WIN32 ) #LLD hangs on windows with multiple threads during linking stage set(CMAKE_SHARED_LINKER_FLAGS " ${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-threads") endif()

arturbac avatar Mar 11 '19 22:03 arturbac

If you use cmake to build the library, you can use following command to fix the problem: cmake -DCMAKE_SHARED_LINKER_FLAGS=-fuse-ld=lld ..

xiaxinkai avatar Dec 25 '19 11:12 xiaxinkai

We are transitioning to LLD, and it sounds like there isn't much else to be done about this, since it's a bfd bug and LLD is just warning about it. (I'd love to be able to selectively enable and disable warnings/werror for the linker like I can for the compiler, but that's well outside the scope of this bug and out of our control.)

DanAlbert avatar Apr 14 '20 00:04 DanAlbert

I have similar problem with ndk22/23(beta) ld.lld, but I need to link my JNI so against (prebuilt) 3rdparty SO that contains these symbols. I tried all above recommendations (APP_LDFLAGS := -Wl,--gc-sections,--strip-debug -fvisibility=hidden -fuse-ld=lld) but they do not work. A return to "-fuse=gold" worked for ndk22, but it is not an option for ndk23 ...

timofey-retailnext avatar Mar 04 '21 23:03 timofey-retailnext

From https://github.com/android/ndk/issues/927#issuecomment-471103365, it sounds like those dependencies need to be rebuilt (with LLD). Unfortunately there isn't anything we can do to fix bfd codegen bugs that have already shipped. LLD is correct in emitting the warning because it's handling a suspicious input. You can disable linker warnings in your project (--no-fatal-warnings mentioned further up the thread), but understand that many linker warnings are highlighting serious bugs, so you'll want to make sure you're reading your warnings if you can't enforce them in your project.

DanAlbert avatar Mar 04 '21 23:03 DanAlbert

Thanks, unfortunately experiment with "no-fatal-warnings" failed: I've added "APP_LDFLAGS += -Wl,--no-fatal-warnings" to my Application.mk However, seems that Android build system adds "-Wl,--fatal-warnings" unconditionally. Here is my verbose ndk-build linker cmd line:

[arm64-v8a] SharedLibrary : libsomething-jni.so path-to/clang++ -Wl,-soname,libsomething-jni.so -shared

... list of object files and static libs ...

-lgcc -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_real.a -latomic -Wl,--exclude-libs,libatomic.a

... list of shared libs ...

-target aarch64-none-linux-androidNN -no-canonical-prefixes

-Wl,--build-id=sha1 -Wl,--no-rosegment -Wl,--no-fatal-warnings -nostdlib++ -Wl,--no-undefined -Wl,--fatal-warnings

... list of -lXXX ...

-o path-to/libsomething-jni.so

What is the best way to remove/suppress built-it "-Wl,--fatal-warnings" ?

timofey-retailnext avatar Mar 20 '21 18:03 timofey-retailnext

fixed by these lines:

LOCAL_DISABLE_FATAL_LINKER_WARNINGS := true LOCAL_LDFLAGS := -Wl,--no-fatal-warnings

in my JNI lib's Android.mk. 3rdparty lib's linker warnings are still there, but they do not cause linker errors anymore.

timofey-retailnext avatar Mar 20 '21 19:03 timofey-retailnext

ndk-r21e (latest LTS version) doesn't seem to have this bug.

ndk-21b(latest stable version) also doesn't have this bug, but its maximum supported native api level is 29, if you need to work with api 30, go with ndk-21e.

ndk-22b, 23 beta both have this bug.

--- Above results were tested on ubuntu18, not sure if same results apply on windows or other OS.

ZhaoqunZhong avatar May 26 '21 04:05 ZhaoqunZhong

If it is in fact the same bug then the answer hasn't changed: https://github.com/android/ndk/issues/927#issuecomment-791019667

If it's a new bug please file a new bug with repro instructions.

DanAlbert avatar Jun 01 '21 19:06 DanAlbert

Bump its been 2 months and this bug is still on ive been trying to build an arm64-v8a libfmod.so and still get the in global part of symbol table, have tried every latest NDK and CMake and every argument option there is but to no avail :(

DelinWorks avatar Aug 02 '21 22:08 DelinWorks

Same answer as the comment before yours.

DanAlbert avatar Aug 02 '21 22:08 DanAlbert

@DanAlbert Hello, can you please help me with the following error.

invalid local symbol '' in global part of symbol table

I am getting this error while I am trying to link an external .so file in my project. I have tried using different versions of ndk but still getting the same error.

Note: .so file is stripped

ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, BuildID[sha1]=fe065c05622fc60c311ff1ba7846162537b202f7, stripped

any help will be appreciated.

harsh-im avatar Oct 03 '23 07:10 harsh-im

@everyone any updates?

harsh-im avatar Oct 03 '23 13:10 harsh-im

You need to file a new bug with the appropriate context.

@everyone doesn't do what you think it does. You just CC'd the github org named "everyone", which is a name I'm sure they regret choosing. In any case, subscribers are automatically notified, so that's not needed.

DanAlbert avatar Oct 03 '23 19:10 DanAlbert