[PATCH v2] LoongArch: Detect relocations out of section range

mengqinggang mengqinggang@loongson.cn
Thu Nov 26 11:23:54 GMT 2026


Hi Harshit,

Thanks a lot for the feedback. I'll post some new patches to address 
these issues soon.


在 2026/11/26 17:04, Harshit Kumar 写道:
> Hi mengqinggang,
>
> On Wed, Nov 25, 2026 at 04:06:58PM +0800, mengqinggang wrote:
>> Use bfd_reloc_offset_in_range in loongarch_elf_relocate_section to
>> reject input relocations whose fields extend outside their section.
> Thanks for picking this up again.  I ran v2 through the checks I used
> for the AArch64 and ARM versions, on a loongarch64-linux-gnu cross of
> master ba2f46b2e4:
>
>    - check-ld: only the new test changes (943 -> 944 expected passes,
>      no failures).
>    - An ASan run over every LoongArch relocation type (ELF32 and ELF64;
>      a field crossing the end, starting at the end, or far away; static,
>      --relax, -shared and -r): all 2,700 links with an out-of-bounds
>      access on master are rejected, and no object whose fields lie
>      inside their section is rejected.
>    - Code built with clang -mrelax and -mno-relax links byte-identically
>      with and without the patch.
>
> Three things v2 doesn't touch, all already on master, in case they are
> useful for a follow-up:
>
>    - An unterminated ULEB128 field (an R_LARCH_ADD_ULEB128 /
>      R_LARCH_SUB_ULEB128 pair on a single 0x80 byte) still links with
>      no error.
>    - loongarch_elf_relax_section reads rel + 1 and rel + 3 for a
>      PC_HI20-type relocation, but the "(i + 2) == sec->reloc_count - 1"
>      test only stops the third-from-last relocation, so for the last two
>      it reads past the relocation array (ASan: heap-buffer-overflow READ
>      of size 8 at elfnn-loongarch.c:6320 for an ELF32 object with a
>      single R_LARCH_PCALA_HI20).
>    - Non-PIC code linked with -shared makes ld segfault after the "can
>      not be used when making a shared object" error:
>      loongarch_elf_relocate_section calls loongarch_elf_append_rela with
>      a NULL sreloc (elfnn-loongarch.c:3821).  clang -fno-pic output is
>      enough to show it.
>
> Thanks,
> Harshit



More information about the Binutils mailing list