Problems with code model large and relocations

Hi,

we use mcjit with code model large and have recently started looking into AARCH64. Currently, we face a sporadic issue with relocations. The overflow check fails here [1] (we are still on LLVM 13).

The relocation command R_AARCH64_ADR_PREL_PG_HI21, which fails, is not visible in the object file when inspecting with objdump or readelf. Instead, they show R_AARCH64_ADR_GOT_PAGE at the same offset.

Is this some kind of linker optimization?

Output of objdump -d -r:

18: adrp x10, 0 <_symbol>
18: R_AARCH64_ADR_GOT_PAGE _symbol
1c: ldr x10, [x10]
1c: R_AARCH64_LD64_GOT_LO12_NC _symbol

[1] llvm-project/RuntimeDyldELF.cpp at llvmorg-13.0.1 · llvm/llvm-project · GitHub

Best regards,
Frank

The relocation command R_AARCH64_ADR_PREL_PG_HI21, which fails, is not visible in the object file when inspecting with objdump or readelf. Instead, they show R_AARCH64_ADR_GOT_PAGE at the same offset.

I suspect you’re generating a different object file, and assuming it should be the same for some reason.

The choice of relocation is affected by -fPIC/-fPIE or equivalent.

I agree with efriedma-quic about the likely reasons for the relocation difference. Another possible reason could be the JIT knowing the destination is in the same program (can rule out the possibility of it being imported from a shared library) so an indirection via the GOT isn’t needed.

As an aside, there are some linker optimizations described by the ABI (abi-aa/aaelf64.rst at main · ARM-software/abi-aa · GitHub) but I think these are only implemented in LLD, and they are subject to range checks.

The large code model for AArch64 is designed for a large amount of static data and not sparse code. The documentation we have abi-aa/sysvabi64.rst at main · ARM-software/abi-aa · GitHub still cites 2 GiB as the maximum text segment size due to the use of relative relocations in exeception tables, although without these it can go up to 4 GiB

We had/have some similar issues to this in julia
See Switch aarch64-darwin codegen to JITLink (ObjectLinkingLayer) and small code model by dnadlinger · Pull Request #43664 · JuliaLang/julia · GitHub for more details.
But basically the large code model doesn’t really work for JITs so the actual fix is using JITLink + the small code model

There is some progress towards resolving this in: [llvm-rtdyld] AArch64 ABI Relocation Restrictions