Skip to content

Adjust relocations after relaxation on LoongArch#1585

Open
CSharperMantle wants to merge 3 commits into
rui314:mainfrom
CSharperMantle:feat-la-relax-reloc
Open

Adjust relocations after relaxation on LoongArch#1585
CSharperMantle wants to merge 3 commits into
rui314:mainfrom
CSharperMantle:feat-la-relax-reloc

Conversation

@CSharperMantle
Copy link
Copy Markdown

@CSharperMantle CSharperMantle commented May 17, 2026

This PR adds relocation adjustment to discard stale ones after relaxation. For simplicity, this PR does not delete old relocation entries. Instead, we set them to R_LARCH_NONE to make them a no-op.

Fixes #1584. With this applied, relaxed instructions in https://github.com/CSharperMantle/mold-issue-1584 are attached with correct relocations.

$ make CC=clang CFLAGS='--target=loongarch64-unknown-linux-gnu' MOLD=~/workspace/mold-dev/build/mold disasm
clang  --target=loongarch64-unknown-linux-gnu -c repro.s -o repro.o
clang -nostdlib -static --ld-path=/home/csmantle/workspace/mold-dev/build/mold -Wl,--emit-relocs -o repro repro.o
objdump --disassemble --reloc repro

repro:     file format elf64-loongarch


Disassembly of section .text:

0000000000210278 <_start>:
  210278:       19f7ffcc        pcaddi          $t0, -16386
                        210278: R_LARCH_NONE    sym
                        210278: R_LARCH_RELAX   *ABS*
                        210278: R_LARCH_PCREL20_S2      sym
                        210278: R_LARCH_RELAX   *ABS*
  21027c:       19f7ffad        pcaddi          $t1, -16387
                        21027c: R_LARCH_NONE    sym
                        21027c: R_LARCH_RELAX   *ABS*
                        21027c: R_LARCH_PCREL20_S2      sym
                        21027c: R_LARCH_RELAX   *ABS*
  210280:       0381740b        li.w            $a7, 0x5d
  210284:       03800004        li.w            $a0, 0x0
  210288:       002b0000        syscall         0x0

v2 (44859df):

@CSharperMantle
Copy link
Copy Markdown
Author

Will look into CI failures today.

@CSharperMantle
Copy link
Copy Markdown
Author

CSharperMantle commented May 18, 2026

I think we're hitting a case of llvm/llvm-project#107749 here, where an INTERP section appears in the absence of DYNAMICs.

$ loongarch64-linux-gnu-readelf -l build/out/test/loongarch64/arch-loongarch64-emit-relocs-relax/exe

Elf file type is EXEC (Executable file)
Entry point 0x2102d8
There are 10 program headers, starting at offset 64

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  PHDR           0x0000000000000040 0x0000000000200040 0x0000000000200040
                 0x0000000000000230 0x0000000000000230  R      0x8
  INTERP         0x0000000000000270 0x0000000000200270 0x0000000000200270
                 0x0000000000000025 0x0000000000000025  R      0x1
      [Requesting program interpreter: /lib64/ld-linux-loongarch-lp64d.so.1]
  NOTE           0x0000000000000298 0x0000000000200298 0x0000000000200298
                 0x0000000000000024 0x0000000000000024  R      0x4
  LOAD           0x0000000000000000 0x0000000000200000 0x0000000000200000
                 0x00000000000002d8 0x00000000000002d8  R      0x10000
  LOAD           0x00000000000002d8 0x00000000002102d8 0x00000000002102d8
                 0x0000000000000040 0x0000000000000040  R E    0x10000
  LOAD           0x0000000000000318 0x0000000000220318 0x0000000000220318
                 0x0000000000000010 0x000000000000fce8  RW     0x10000
  LOAD           0x0000000000000328 0x0000000000230328 0x0000000000230328
                 0x0000000000000020 0x0000000000000020  RW     0x10000
  GNU_EH_FRAME   0x00000000000002c4 0x00000000002002c4 0x00000000002002c4
                 0x000000000000000c 0x000000000000000c  R      0x4
  GNU_STACK      0x0000000000000000 0x0000000000000000 0x0000000000000000
                 0x0000000000000000 0x0000000000000000  RW     0x1
  GNU_RELRO      0x0000000000000318 0x0000000000220318 0x0000000000220318
                 0x0000000000000010 0x000000000000fce8  R      0x1

 Section to Segment mapping:
  Segment Sections...
   00
   01     .interp
   02     .note.gnu.build-id
   03     .interp .note.gnu.build-id .eh_frame .eh_frame_hdr .rodata
   04     .text
   05     .got .relro_padding
   06     .data .got.plt
   07     .eh_frame_hdr
   08
   09     .got .relro_padding

with the following backtrace:

#0  0x00007ffff7fa9ef0 in audit_list_add_dynamic_tag (list=0x7ffffffcecc0, main_map=0x7ffff7fcd270, tag=1879047932) at rtld.c:224                           
#1  dl_main (phdr=<optimized out>, phnum=<optimized out>, user_entry=<optimized out>, auxv=<optimized out>) at rtld.c:1766                                             
#2  0x00007ffff7fa6c54 in _dl_sysdep_start (start_argptr=start_argptr@entry=0x7ffffffcf4c0, dl_main=dl_main@entry=0x7ffff7fa88d0 <dl_main>) at ../sysdeps/unix/sysv/linux/dl-sysdep.c:140                                    
#3  0x00007ffff7fa7964 in _dl_start_final (arg=arg@entry=0x7ffffffcf4c0, info=info@entry=0x7ffffffcefd0) at rtld.c:496
#4  0x00007ffff7fa85cc in _dl_start (arg=0x7ffffffcf4c0) at rtld.c:583
#5  0x00007ffff7fa724c in _start () from /lib64/ld-linux-loongarch-lp64d.so.1

I'll add --static as a workaround in v2, as mentioned in llvm/llvm-project#107749.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stale relocation after relaxation on LoongArch

1 participant