Note about building Position Independent Code (PIC) on AArch64

Issue

Failure can occur at the linking stage when building Position-Independent Code (PIC) on AArch64 using the lower-case -fpic compiler flag with GCC compilers (gfortran, gcc, g++), in preference to using the upper-case -fPIC flag.  

Notes:

  • This issue does not occur when using the -fpic flag with Arm compilers for HPC (armflang/armclang/armclang++), and it also does not occur on x86_64 because -fpic operates the same as -fPIC.

  • PIC is code which is suitable for shared libraries.

Cause

Using the -fpic compiler flag with GCC compilers on AArch64 causes the compiler to generate one less instruction per address computation in the code, and can provide code size and performance benefits. However, it also sets a limit of 32k for the Global Offset Table (GOT), and the build can fail at the executable linking stage because the GOT overflows.

Note: When building PIC with Arm Compilers for HPC on AArch64, or building PIC on x86_64, -fpic does not set a limit for the GOT, and this issue does not occur.

Solution

Consider using the -fPIC compiler flag with GCC compilers on AArch64, because it ensures that the size of the GOT for a dynamically linked executable will be large enough to allow the entries to be resolved by the dynamic loader.