Large code model for specific variables?

Hello Clang-Experts,

I have a very simple code like this:

extern int far_value;

void Initialize() {
    far_value = 12345;
}

Which we will then compile to a static library ahead of time for our platform, not providing an implementation for far_value. At runtime our system will be aware of the address for far_value and relocate the symbol with the address. That works fine so far.
However, sometimes we run into the issue, that far_value is to far away from our code to be referenced by a relative address encoded into the assembly instruction.

mov	dword ptr [rip + "?far_value@@3HA"], 12345

With the -mcmodel=large setting we were able to work around this issue creating code like this:

movabs	rax, offset "?far_value@@3HA"
mov	dword ptr [rax], 12345

Which is okay for the access to far_value - but it will effect all other referenced variables and functions as well!
Can we somehow tell Clang to only assume the large code model for certain symbols or code sections? For MIPS targets I have seen the far attribute which however is only platform specific. Would it help to first compile to the LLVM IR to modify the code accordingly? Sadly the output in the end has to be a static library so we can currently not uset he OrcJIT.

Any help and guidance is welcome!

It looks like there is __attribute((model("large"))) for this, but for some reason it is currently only supported on loongarch ([clang] Add per-global code model attribute by heiher · Pull Request #72078 · llvm/llvm-project · GitHub).

As someone involved in the creation of the model attribute for LoongArch: I think the general idea can be applied to other targets, just that someone has to step up and adapt their target’s codegen. One can look at the LoongArch adaptation to get an idea how this is done. It should be relatively easy to enable the attribute for the target at Clang side then.