Repository: pytorch
Issue: pytorch_android-2.1.0.aar/jni/arm64-v8a/libpytorch_jni.so: invalid local symbol '__bss_end__' in global part of symbol table
Labels: oncall: mobile

### 🐛 Describe the bug

I have a running libtorch inference code which I want to re-use in an android app. I built pytorch for android following this
[guide.](https://github.com/pytorch/pytorch/blob/main/android/README.md#linking-to-prebuilt-libtorch-library-from-gradle-dependency)

Dependencies added in build.gradle:
```
extractForNativeBuild('org.pytorch:pytorch_android:2.1.0')
implementation 'com.facebook.soloader:nativeloader:0.11.0'
implementation 'com.facebook.fbjni:fbjni-java-only:0.6.0'
```
NDK version used for the build:  26.1.10909125

After the build, I have pytorch_android-2.1.0.aar inside /build folder.

My CMakeLists.txt file contains:

```
# Sets the minimum CMake version required for this project.
cmake_minimum_required(VERSION 3.22.1)

# Declares the project name.
project("test")

# Function to set paths based on ABI
function(set_icu_paths ABI)
    set(ICU_ROOT "/home/ik/Downloads/icu4c-main/prebuilt/libs/android/${ABI}")
    include_directories("/home/ik/Downloads/icu4c-main/prebuilt/include")
    add_library(icui18n STATIC IMPORTED)
    set_target_properties(icui18n PROPERTIES IMPORTED_LOCATION ${ICU_ROOT}/libicui18n_floris.a)
    add_library(icuuc STATIC IMPORTED)
    set_target_properties(icuuc PROPERTIES IMPORTED_LOCATION ${ICU_ROOT}/libicuuc_floris.a)
    add_library(icudata STATIC IMPORTED)
    set_target_properties(icudata PROPERTIES IMPORTED_LOCATION ${ICU_ROOT}/libicudata_floris.a)
endfunction()

# Set paths and include directories based on ABI
if(${ANDROID_ABI} STREQUAL "arm64-v8a")
    set_icu_paths("arm64-v8a")
elseif(${ANDROID_ABI} STREQUAL "armeabi-v7a")
    set_icu_paths("armeabi-v7a")
elseif(${ANDROID_ABI} STREQUAL "x86")
    set_icu_paths("x86")
elseif(${ANDROID_ABI} STREQUAL "x86_64")
    set_icu_paths("x86_64")
else()
    message(FATAL_ERROR "Unsupported ABI: ${ANDROID_ABI}")
endif()


# code from pytorch github doc
# Relative path of gradle build directory to CMakeLists.txt
set(build_DIR "${CMAKE_SOURCE_DIR}/build")

file(GLOB PYTORCH_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/build/pytorch_android-2.1.0.aar/headers")
file(GLOB PYTORCH_LINK_DIRS "${CMAKE_SOURCE_DIR}/build/pytorch_android-2.1.0.aar/jni/${ANDROID_ABI}")

# Ensure the directories exist before attempting to use them
if (NOT EXISTS "${PYTORCH_INCLUDE_DIRS}")
    message(FATAL_ERROR "Pytorch include directories not found: ${PYTORCH_INCLUDE_DIRS}")
endif()
if (NOT EXISTS "${PYTORCH_LINK_DIRS}")
    message(FATAL_ERROR "Pytorch link directories not found: ${PYTORCH_LINK_DIRS}")
endif()

# Specify the target include directories
include_directories(${PYTORCH_INCLUDE_DIRS})

# Define the shared library target
add_library(${PROJECT_NAME} SHARED
        src/main/cpp/test.cpp
)

# Find the Pytorch and FBJNI libraries
find_library(PYTORCH_LIBRARY pytorch_jni
        PATHS ${PYTORCH_LINK_DIRS}
        NO_CMAKE_FIND_ROOT_PATH)

find_library(FBJNI_LIBRARY fbjni
        PATHS ${PYTORCH_LINK_DIRS}
        NO_CMAKE_FIND_ROOT_PATH)

# Check if the libraries were found
if (NOT PYTORCH_LIBRARY)
    message(FATAL_ERROR "Pytorch library not found")
endif()
if (NOT FBJNI_LIBRARY)
    message(FATAL_ERROR "FBJNI library not found")
endif()

# Link the libraries to the target
target_link_libraries(${PROJECT_NAME}
        ${PYTORCH_LIBRARY}
        ${FBJNI_LIBRARY}
        icui18n icudata icuuc
        android
        log
)

```

When I run the app, I get the following errors:

```
> Task :app:buildCMakeDebug[arm64-v8a] FAILED
C/C++: ninja: Entering directory `/home/ik/AndroidStudioProjects/test-app/app/.cxx/Debug/6zi25n2z/arm64-v8a'
C/C++: : && /home/ik/Android/Sdk/ndk/26.1.10909125/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=aarch64-none-linux-android24 --sysroot=/home/ik/Android/Sdk/ndk/26.1.10909125/toolchains/llvm/prebuilt/linux-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security   -fno-limit-debug-info  -Wl,--build-id=sha1 -Wl,--no-rosegment -Wl,--no-undefined-version -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -shared -Wl,-soname,libaakhor.so -o ../../../../build/intermediates/cxx/Debug/6zi25n2z/obj/arm64-v8a/libaakhor.so CMakeFiles/test-app.dir/src/main/cpp/test.cpp.o CMakeFiles/test-app.dir/src/main/cpp/asm_tokenizer.cpp.o CMakeFiles/test-app.dir/src/main/cpp/eng_tokenizer.cpp.o  ../../../../build/pytorch_android-2.1.0.aar/jni/arm64-v8a/libpytorch_jni.so  ../../../../build/pytorch_android-2.1.0.aar/jni/arm64-v8a/libfbjni.so  /home/ik/Downloads/icu4c-main/prebuilt/libs/android/arm64-v8a/libicui18n_floris.a  /home/ik/Downloads/icu4c-main/prebuilt/libs/android/arm64-v8a/libicudata_floris.a  /home/ik/Downloads/icu4c-main/prebuilt/libs/android/arm64-v8a/libicuuc_floris.a  -landroid  -llog  -latomic -lm && :
C/C++: ld.lld: error: ../../../../build/pytorch_android-2.1.0.aar/jni/arm64-v8a/libpytorch_jni.so: invalid local symbol '__bss_end__' in global part of symbol table
C/C++: ld.lld: error: ../../../../build/pytorch_android-2.1.0.aar/jni/arm64-v8a/libpytorch_jni.so: invalid local symbol '__bss_start' in global part of symbol table
C/C++: ld.lld: error: ../../../../build/pytorch_android-2.1.0.aar/jni/arm64-v8a/libpytorch_jni.so: invalid local symbol '_end' in global part of symbol table
C/C++: ld.lld: error: ../../../../build/pytorch_android-2.1.0.aar/jni/arm64-v8a/libpytorch_jni.so: invalid local symbol '_edata' in global part of symbol table
C/C++: ld.lld: error: ../../../../build/pytorch_android-2.1.0.aar/jni/arm64-v8a/libpytorch_jni.so: invalid local symbol '__bss_start__' in global part of symbol table
C/C++: ld.lld: error: ../../../../build/pytorch_android-2.1.0.aar/jni/arm64-v8a/libpytorch_jni.so: invalid local symbol '_bss_end__' in global part of symbol table
C/C++: ld.lld: error: ../../../../build/pytorch_android-2.1.0.aar/jni/arm64-v8a/libpytorch_jni.so: invalid local symbol '__end__' in global part of symbol table
C/C++: clang++: error: linker command failed with exit code 1 (use -v to see invocation)
C/C++: ninja: build stopped: subcommand failed.

```

I am assuming this could be a version incompatibility issue. Requesting help to resolve this.

### Versions

NDK: 26.1.10909125
Gradle: 8.7
Java: 17.0.11
gcc: gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
CMake: 3.22.1