ccompilationlinker-errorsautotoolsfpic

Recompile with -fPIC on NDK CPython compilation


I want to compile CPython with NDK and I get fPIC error no matter I put -fPIC in CFLAGS.

ld: error: relocation R_ARM_REL32 cannot be used against symbol PyBool_Type; recompile with -fPIC

I have Python source with Autoconf configure script and NDK.

My own mini "conf" script to prepare and run autoconf script

#!/bin/bash

export NDK_HOME=/home/msz/ndk/android-ndk-r23c
export TOOLCHAIN=$NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64
export READELF=$TOOLCHAIN/bin/llvm-readelf
export LLVM_PROFDATA=$TOOLCHAIN/bin/llvm-profdata
export LD=$TOOLCHAIN/bin/ld.ldd
export AR=$TOOLCHAIN/bin/llvm-ar

./configure \
CC=$TOOLCHAIN/bin/armv7a-linux-androideabi28-clang \
CFLAGS=-fPIC \
--enable-optimizations \
--enable-ipv6 \
--enable-shared \
--build=x86_64-linux-gnu \
--host=aarch64-linux-android \
--target=aarch64-linux-android28 \
ac_cv_file__dev_ptmx=no \
ac_cv_file__dev_ptc=no

make

OUTPUT:

ld: error: relocation R_ARM_REL32 cannot be used against symbol PyBool_Type; recompile with -fPIC
>>> defined in Objects/boolobject.o
>>> referenced by boolobject.c:0 (Objects/boolobject.c:0)
>>>               Objects/boolobject.o:(bool_and)

ld: error: relocation R_ARM_REL32 cannot be used against symbol PyBool_Type; recompile with -fPIC
>>> defined in Objects/boolobject.o
>>> referenced by boolobject.c:0 (Objects/boolobject.c:0)
>>>               Objects/boolobject.o:(bool_xor)

ld: error: relocation R_ARM_REL32 cannot be used against symbol PyBool_Type; recompile with -fPIC
>>> defined in Objects/boolobject.o
>>> referenced by boolobject.c:0 (Objects/boolobject.c:0)
>>>               Objects/boolobject.o:(bool_or)

ld: error: relocation R_ARM_REL32 cannot be used against symbol PyByteArray_Type; recompile with -fPIC
>>> defined in Objects/bytearrayobject.o
>>> referenced by bytearrayobject.c:0 (Objects/bytearrayobject.c:0)
>>>               Objects/bytearrayobject.o:(PyByteArray_FromObject)

ld: error: relocation R_ARM_REL32 cannot be used against symbol PyByteArray_Type; recompile with -fPIC
>>> defined in Objects/bytearrayobject.o
>>> referenced by bytearrayobject.c:0 (Objects/bytearrayobject.c:0)
>>>               Objects/bytearrayobject.o:(PyByteArray_FromObject)

ld: error: relocation R_ARM_REL32 cannot be used against symbol PyByteArray_Type; recompile with -fPIC
>>> defined in Objects/bytearrayobject.o
>>> referenced by bytearrayobject.c:0 (Objects/bytearrayobject.c:0)
>>>               Objects/bytearrayobject.o:(PyByteArray_FromObject)

ld: error: relocation R_ARM_REL32 cannot be used against symbol PyByteArray_Type; recompile with -fPIC
>>> defined in Objects/bytearrayobject.o
>>> referenced by bytearrayobject.c:0 (Objects/bytearrayobject.c:0)
>>>               Objects/bytearrayobject.o:(PyByteArray_FromStringAndSize)

ld: error: relocation R_ARM_REL32 cannot be used against symbol _PyByteArray_empty_string; recompile with -fPIC
>>> defined in Objects/bytearrayobject.o
>>> referenced by bytearrayobject.c:0 (Objects/bytearrayobject.c:0)
>>>               Objects/bytearrayobject.o:(PyByteArray_AsString)

ld: error: relocation R_ARM_REL32 cannot be used against symbol _PyByteArray_empty_string; recompile with -fPIC
>>> defined in Objects/bytearrayobject.o
>>> referenced by bytearrayobject.c:0 (Objects/bytearrayobject.c:0)
>>>               Objects/bytearrayobject.o:(PyByteArray_Resize)

ld: error: relocation R_ARM_REL32 cannot be used against symbol _PyByteArray_empty_string; recompile with -fPIC
>>> defined in Objects/bytearrayobject.o
>>> referenced by bytearrayobject.c:0 (Objects/bytearrayobject.c:0)
>>>               Objects/bytearrayobject.o:(PyByteArray_Resize)

ld: error: relocation R_ARM_REL32 cannot be used against symbol _PyByteArray_empty_string; recompile with -fPIC
>>> defined in Objects/bytearrayobject.o
>>> referenced by bytearrayobject.c:0 (Objects/bytearrayobject.c:0)
>>>               Objects/bytearrayobject.o:(bytearray_repr)

ld: error: relocation R_ARM_REL32 cannot be used against symbol _PyByteArray_empty_string; recompile with -fPIC
>>> defined in Objects/bytearrayobject.o
>>> referenced by bytearrayobject.c:0 (Objects/bytearrayobject.c:0)
>>>               Objects/bytearrayobject.o:(bytearray_repr)

ld: error: relocation R_ARM_REL32 cannot be used against symbol PyByteArray_Type; recompile with -fPIC
>>> defined in Objects/bytearrayobject.o
>>> referenced by bytearrayobject.c:0 (Objects/bytearrayobject.c:0)
>>>               Objects/bytearrayobject.o:(bytearray_iter)

ld: error: relocation R_ARM_REL32 cannot be used against symbol PyByteArray_Type; recompile with -fPIC
>>> defined in Objects/bytearrayobject.o
>>> referenced by bytearrayobject.c:0 (Objects/bytearrayobject.c:0)
>>>               Objects/bytearrayobject.o:(bytearray_iter)

ld: error: relocation R_ARM_REL32 cannot be used against symbol PyByteArrayIter_Type; recompile with -fPIC
>>> defined in Objects/bytearrayobject.o
>>> referenced by bytearrayobject.c:0 (Objects/bytearrayobject.c:0)
>>>               Objects/bytearrayobject.o:(bytearray_iter)

ld: error: relocation R_ARM_REL32 cannot be used against symbol _PyByteArray_empty_string; recompile with -fPIC
>>> defined in Objects/bytearrayobject.o
>>> referenced by bytearrayobject.c:0 (Objects/bytearrayobject.c:0)
>>>               Objects/bytearrayobject.o:(bytearray___init__)

ld: error: relocation R_ARM_REL32 cannot be used against symbol _PyByteArray_empty_string; recompile with -fPIC
>>> defined in Objects/bytearrayobject.o
>>> referenced by bytearrayobject.c:0 (Objects/bytearrayobject.c:0)
>>>               Objects/bytearrayobject.o:(bytearray___init__)

ld: error: relocation R_ARM_REL32 cannot be used against symbol _PyByteArray_empty_string; recompile with -fPIC
>>> defined in Objects/bytearrayobject.o
>>> referenced by bytearrayobject.c:0 (Objects/bytearrayobject.c:0)
>>>               Objects/bytearrayobject.o:(bytearray___init__)

ld: error: relocation R_ARM_REL32 cannot be used against symbol _PyByteArray_empty_string; recompile with -fPIC
>>> defined in Objects/bytearrayobject.o
>>> referenced by bytearrayobject.c:0 (Objects/bytearrayobject.c:0)
>>>               Objects/bytearrayobject.o:(bytearray___init__)

ld: error: relocation R_ARM_REL32 cannot be used against symbol _PyByteArray_empty_string; recompile with -fPIC
>>> defined in Objects/bytearrayobject.o
>>> referenced by bytearrayobject.c:0 (Objects/bytearrayobject.c:0)
>>>               Objects/bytearrayobject.o:(bytearrayiter_next)

ld: error: too many errors emitted, stopping now (use -error-limit=0 to see all errors)
clang-12: error: linker command failed with exit code 1 (use -v to see invocation)
ln: failed to access 'libpython3.10.so.1.0': No such file or directory
make[3]: *** [Makefile:655: libpython3.10.so] Error 1
make[3]: Leaving directory '/home/msz/Desktop/Python-3.10.5'
make[2]: *** [Makefile:531: build_all_generate_profile] Error 2
make[2]: Leaving directory '/home/msz/Desktop/Python-3.10.5'
make[1]: *** [Makefile:507: profile-gen-stamp] Error 2
make[1]: Leaving directory '/home/msz/Desktop/Python-3.10.5'
make: *** [Makefile:519: profile-run-stamp] Error 2

Configure seems to properly use given -fPIC flag, here is a config.log fragment:

configure:17792: /home/msz/ndk/android-ndk-r23c/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi28-clang -o conftest -fPIC    -march=armv7-a -Wl,--fix-cortex-a8  conftest.c  -ldl  -lm >&5

So, why do I still get this error?

If I wrote something stupid know that I'm just learning Autotools.


Solution

  • Problem was solved by 273K, make clean helped.

    #!/bin/bash
    
    export NDK_HOME=/home/msz/ndk/android-ndk-r23c
    export TOOLCHAIN=$NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64
    export READELF=$TOOLCHAIN/bin/llvm-readelf
    export LLVM_PROFDATA=$TOOLCHAIN/bin/llvm-profdata
    export LD=$TOOLCHAIN/bin/ld.ldd
    export AR=$TOOLCHAIN/bin/llvm-ar
    
    ./configure \
    CC=$TOOLCHAIN/bin/armv7a-linux-androideabi28-clang \
    CFLAGS=-fPIC \
    --enable-optimizations \
    --enable-ipv6 \
    --enable-shared \
    --build=x86_64-linux-gnu \
    --host=aarch64-linux-android \
    --target=aarch64-linux-android28 \
    ac_cv_file__dev_ptmx=no \
    ac_cv_file__dev_ptc=no
    
    make clean
    make