Skip to content

Commit

Permalink
[LoongArch] Implement a workaround for R_LARCH_PCALA_LO12
Browse files Browse the repository at this point in the history
It seems R_LARCH_PCALA_LO12 is sometimes used against JIRL even though
such use case is incorrect as per the psABI.

At least, `/usr/lib/loongarch64-linux-gnu/libc_nonshared.a(atexit.oS)`
on `cfarm400.cfarm.net` contains such relocation.

We need this patch to self-host mold on that cfarm machine.
  • Loading branch information
rui314 committed Oct 31, 2023
1 parent 3229b5b commit d3188e3
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion elf/arch-loongarch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,17 @@ void InputSection<E>::apply_reloc_alloc(Context<E> &ctx, u8 *base) {
write_k12(loc, (S + A) >> 52);
break;
case R_LARCH_PCALA_LO12:
write_k12(loc, S + A);
// It looks like R_LARCH_PCALA_LO12 is sometimes used for JIRL even
// though the instruction takes a 16 bit immediate rather than 12 bits.
// It is contrary to the psABI document, but GNU ld has special
// code to handle it. We accept it with a warning message.
if ((*(ul32 *)loc & 0xfc00'0000) == 0x4c00'0000) {
Warn(ctx) << *this << ": invalid use of a R_LARCH_PCALA_LO12 relocation"
<< " against a JIRL instruction";
write_k16(loc, sign_extend(S + A, 11) >> 2);
} else {
write_k12(loc, S + A);
}
break;
case R_LARCH_PCALA_HI20:
write_j20(loc, hi20(S + A, P));
Expand Down

0 comments on commit d3188e3

Please sign in to comment.