[Bug target/119894] -fno-PIC -Os on loongarch creates code incompatible with plts

2025-04-22 Thread phcoder at gmail dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119894

--- Comment #3 from Vladimir 'phcoder' Serbinenko  ---
What is a long stub? Stub for calling outside of 2MiB range? If so then, looks
like it.

[Bug c/119894] New: -fno-PIC -Os on loongarch creates code incompatible with plts

2025-04-22 Thread phcoder at gmail dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119894

Bug ID: 119894
   Summary: -fno-PIC -Os on loongarch creates code incompatible
with plts
   Product: gcc
   Version: 14.2.1
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c
  Assignee: unassigned at gcc dot gnu.org
  Reporter: phcoder at gmail dot com
  Target Milestone: ---

Created attachment 61170
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=61170&action=edit
Test program

When compiling test program (attached) as loongarch64-linux-gnu-gcc  -Os -g
-shared -o misc.so misc.c  -nostdlib -fno-PIC. We get the following code:

 364:   57ff8fffbl  -116# 2f0 
/home/phcoder/grub-extras/misc.c:50
 368:   28c0206cld.d$t0, $sp, 8
 36c:   02c005efaddi.d  $t3, $t3, 1

So gcc relies on grub_divmod64@plt to preserve $t3. $t3 is preserved in
generated code of grub_divmod64 but insertion of PLT by the linker throws
things off as plt has following code:
 2f0:   1c00010fpcaddu12i   $t3, 8
 2f4:   28f441efld.d$t3, $t3, -752
 2f8:   4c0001edjirl$t1, $t3, 0

As per ABI specification $t3 is caller-preserved so gcc needs to take insertion
of PLTs into account when reusing the registers.