How to resolve "ld: error: .* :relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 20145225728 is not in [-4294967296, 4294967295]"

  android, android-ndk, c++, clang

I am building an Android executable using the cmake and ninja:

cmake -H../ -DCMAKE_CXX_FLAGS=-std=c++11 -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=$NDK/build/cmake/android.toolchain.cmake -DANDROID_ABI=$ABI -DANDROID_NDK=$NDK -DANDROID_PLATFORM=android-$MIN_SDK_VERSION -DCMAKE_ANDROID_ARCH_ABI=$ABI -DCMAKE_ANDROID_NDK=$NDK -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=$ABI -DCMAKE_MAKE_PROGRAM=$SDK_PATH/cmake/$CMAKE_VERSION/bin/ninja.exe -DCMAKE_SYSTEM_NAME=Android -DCMAKE_SYSTEM_VERSION=$MIN_SDK_VERSION -DANDROID_STL=c++_static -B$BUILD_DIR -GNinja

ninja

The project has some very large variables declared in a global space of a file:

BCCH_BCH_Message bcch_bch_Message;
BCCH_DL_SCH_Message bcch_dl_sch_Message;
DL_CCCH_Message dl_ccch_Message;
DL_DCCH_Message dl_dcch_Message;
PCCH_Message pcch_Message;
...

When I run the project then I will start getting the build errors like following:

-- The C compiler identification is Clang 11.0.5
-- The CXX compiler identification is Clang 11.0.5
-- Check for working C compiler: D:/Sdk/ndk/22.0.7026061/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe
-- Check for working C compiler: D:/Sdk/ndk/22.0.7026061/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe -- works      
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: D:/Sdk/ndk/22.0.7026061/toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe
-- Check for working CXX compiler: D:/Sdk/ndk/22.0.7026061/toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: D:/Development/CPP/ASNWrapper/build
[4/4] Linking CXX executable asn_wrap
FAILED: asn_wrap
cmd.exe /C "cd . && D:Sdkndk.0.7026061toolchainsllvmprebuiltwindows-x86_64binclang++.exe --target=aarch64-none-linux-android29 --gcc-toolchain=D:/Sdk/ndk/22.0.7026061/toolchains/llvm/prebuilt/windows-x86_64 --sysroot=D:/Sdk/ndk/22.0.7026061/toolchains/llvm/prebuilt/windows-x86_64/sysroot  -fPIC -O2 -DNDEBUG  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id=sha1 -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -Wl,--gc-sections CMakeFiles/asn_wrap.dir/src/test_dl_dcch.cpp.o CMakeFiles/asn_wrap.dir/src/nas_asn_wrap.cpp.o CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o  -o asn_wrap -LD:/Development/CPP/ASNWrapper/lib/arm64-v8a -lasn1per -lasn1rt -lasn1rt3gpp -losys3gpp ../lib/arm64-v8a/libasn_rrc.a -llog -latomic -lm && cd ."
ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0xDF0): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 10986287104 is not in [-4294967296, 4294967295]; references pcch_Message
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0xE90): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 10986291200 is not in [-4294967296, 4294967295]; references ul_ccch_Message
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0xF30): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 10986291200 is not in [-4294967296, 4294967295]; references ul_ccch1_Message
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0xFD0): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 10986287104 is not in [-4294967296, 4294967295]; references ul_dcch_Message
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0x1070): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 14935199744 is not in [-4294967296, 4294967295]; references 
rrcReconfig
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0x1110): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 20145197056 is not in [-4294967296, 4294967295]; references 
rrcReconfigComp
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0x11B0): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 20145225728 is not in [-4294967296, 4294967295]; references 
sib1
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0x1250): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 20145434624 is not in [-4294967296, 4294967295]; references 
sysInfo
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0x12F0): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 20145381376 is not in [-4294967296, 4294967295]; references 
sib2
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0x1390): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 20145381376 is not in [-4294967296, 4294967295]; references 
sib3
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0x1430): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 20145385472 is not in [-4294967296, 4294967295]; references 
sib4
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0x14D0): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 20145426432 is not in [-4294967296, 4294967295]; references 
sib5
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0x1570): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 20145434624 is not in [-4294967296, 4294967295]; references 
sib6
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0x1610): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 20145434624 is not in [-4294967296, 4294967295]; references 
sib7
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0x16B0): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 20145434624 is not in [-4294967296, 4294967295]; references 
sib8
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0x1750): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 20145434624 is not in [-4294967296, 4294967295]; references 
sib9
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: /buildbot/src/android/ndk-release-r22toolchain/llvm-project/libcxx/../../../toolchain/llvm-project/libcxxabi/../libcxx/include/__threading_support:367:(.text.__cxa_guard_abort+0x10): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 20144017408 is not in [-4294967296, 4294967295]
ld: error: /buildbot/src/android/ndk-release-r22toolchain/llvm-project/libcxx/../../../toolchain/llvm-project/libcxxabi/../libcxx/include/__threading_support:367:(.text._ZN12_GLOBAL__N_115fallback_mallocEm+0x10): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 20143935488 is not in [-4294967296, 4294967295]
ld: error: /buildbot/src/android/ndk-release-r22toolchain/llvm-project/libcxx/../../../toolchain/llvm-project/libcxxabi/../libcxx/include/__threading_support:367:(.text.__cxa_guard_acquire+0x2C): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 20144021504 is not in [-4294967296, 4294967295]
ld: error: rrc_asn_wrap.cpp:(function rrc_asn_wrap::decode_nr(rrc_asn_wrap::l3_type, unsigned char const*, unsigned long): .text+0x17F0): relocation R_AARCH64_ADR_PREL_PG_HI21 out of range: 11474681856 is not in [-4294967296, 4294967295]; references 
cellGroupConfig
>>> defined in CMakeFiles/asn_wrap.dir/src/rrc_asn_wrap.cpp.o

ld: error: too many errors emitted, stopping now (use -error-limit=0 to see all errors)
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

I don’t get errors if I comment the global variable that seems very large like:

// DL_CCCH_Message dl_ccch_Message;
// DL_DCCH_Message dl_dcch_Message;

But I do need these variables.
I have search a lot on internet in past three days. Some says to add "-fPIC" and I did but still these errors not resolved.

Here is the declaration of one such variable in the header file:

/**************************************************************/
/*                                                            */
/*  DL_CCCH_Message                                           */
/*                                                            */
/**************************************************************/
/*
DL-CCCH-Message ::= SEQUENCE {
   message [0] EXPLICIT DL-CCCH-MessageType
}
*/
typedef struct EXTERN DL_CCCH_Message {
   DL_CCCH_MessageType message;
} DL_CCCH_Message;

The definition of this is in .so file and the code is not given to me. So, I can just use it as it is.

Please suggest something to resolve these errors.

Source: Windows Questions C++

LEAVE A COMMENT