[llvm] [AArch64,ELF] Restrict MOVZ/MOVK to non-PIC large code model (PR #70178)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 25 12:26:07 PDT 2023


MaskRay wrote:

> Doesn't this effectively make -mcmodel=large equivalent to -mcmodel=small when -fpie is used? I'm a bit nervous about this as it might lead people to believe that -mcmodel=large is working until their program breaks.

Yes. The change is primarily about clarification: the large code model support in `lib/Target/AArch64` is no-pic only. The `isPositionIndependent` conditions serve as a document that the PIC large code model needs to do something different.

If there are JIT-style workloads using large code model, this change will force them to use the correct static relocation model instead of relying on a possibly wrong PIC relocation model.

> Personally I'd prefer an error message like GNU for now as it is marked as unsupported in the ABI [ARM-software/abi-aa@`main`/sysvabi64/sysvabi64.rst#implementation-of-code-models](https://github.com/ARM-software/abi-aa/blob/main/sysvabi64/sysvabi64.rst?rgh-link-date=2023-10-25T14%3A38%3A00Z#implementation-of-code-models)

I was uncertain whether Clang Driver should reject it and the description currently says
"The PIC large code model is largely undefined in toolchains, but Clang doesn't reject -fpic -mcmodel=large as GCC does.".

Thanks for the opinion. I will sent a patch to reject `-fpic -mcmodel=large` for AArch64 on top of #53402

> One thought I had about code-models and PIC/PIE was to force all global data accesses through the GOT, this would mean that although the GOT would have to be within 4 GiB of the code, global data accessed via the GOT could be anywhere within the address space. Whether that should be called large-code model or something else I don't know. I'm not sure that making a PC-relative offset from a sequence of MOVW using (R__MOVW_PREL_G0) to the GOT really helps for most programs.

Using GOT is Power's choice: https://maskray.me/blog/2023-05-14-relocation-overflow-and-code-models

Personally I think reusing small code model should be fine for an extended period of time as

> For data references from code, x86-64 uses R_X86_64_REX_GOTPCRELX/R_X86_64_PC32 relocations, which have a smaller range [-2**31,2**31). In contrast, AArch64 employs R_AARCH64_ADR_PREL_PG_HI21 relocations, which has a doubled range of [-2**32,2**32). This larger range makes it unlikely for AArch64 to encounter relocation overflow issues before the binary becomes excessively oversized for x86-64.

https://github.com/llvm/llvm-project/pull/70178


More information about the llvm-commits mailing list