loongson-community / discussions

Cross-community issue tracker & discussions / 跨社区工单追踪 & 讨论场所
7 stars 0 forks source link

[Binutils] ld -z pack‐relative‐relocs support #12

Closed xry111 closed 1 week ago

xry111 commented 8 months ago

Background: https://maskray.me/blog/2021-10-31-relative-relocations-and-relr

I've tried to implement this for LoongArch but the attempt miserably failed. The BFD source code is much more difficult to understand than GCC or LLVM...

MaskRay commented 8 months ago

The BFD source code is much more difficult to understand than GCC or LLVM...

Absolutely!

The PowerPC64 implementation may be useful: https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=1657026ccd8aa05a97cb35f1d9bff646937a244e

There are a few follow-up commits to remove redundant R_PPC64_NONE.

xry111 commented 4 months ago

It seems we are too different from PPC or x86...

Is there some doc for BFD analogous to GCC Internal? "info bfd" seems too outdated.

xry111 commented 1 month ago

AArch64 implementation https://sourceware.org/pipermail/binutils/2024-May/134368.html can be a reference and its commit message explains some BFD mess.

xry111 commented 1 month ago

A very simple PoC has been linked successfully, but still not ready for review:

Relocation section '.relr.dyn' at offset 0x190 contains 2 entries which relocate 3 locations:
Index: Entry            Address           Symbolic Address
0000:  0000000000008018 0000000000008018  p
0001:  0000000000000007 0000000000008020  p + 0x8
                        0000000000008028  p + 0x10
xry111 commented 4 weeks ago

Now libc.so is linked successfully:

Relocation section '.relr.dyn' at offset 0x206a8 contains 31 entries which relocate 958 locations:
Index: Entry            Address           Symbolic Address
0000:  00000000001a94b0 00000000001a94b0  .tdata
0001:  b23c00000fefc0dd 00000000001a94c0  .tbss
                        00000000001a94c8  .tbss + 0x8
                        00000000001a94d0  .tbss + 0x10
                        00000000001a94e0  __dso_handle
                        00000000001a94e8  _nl_C_LC_ADDRESS
                        00000000001a9520  _nl_C_LC_ADDRESS + 0x38
                        00000000001a9528  _nl_C_LC_ADDRESS + 0x40
                        00000000001a9530  _nl_C_LC_ADDRESS + 0x48
                        00000000001a9538  _nl_C_LC_ADDRESS + 0x50
                        00000000001a9540  _nl_C_LC_ADDRESS + 0x58
... ...

and Glibc test suite passes. But there are 8 R_LARCH_NONE in it, indicating some over-allocation...

xry111 commented 4 weeks ago

Oops the binutils trunk is already generating R_LARCH_NONE even without my changes.

xry111 commented 4 weeks ago

https://github.com/xry111/binutils-gdb/commits/xry111/loongarch-relr-2024/

xry111 commented 3 weeks ago

A modified Linux kernel boots fine with CONFIG_RELR=y. But there are some unpacked R_LARCH_RELATIVE against the .la.abs section of vmlinux. Need some investigation...

BTW there are many R_LARCH_NONE in vmlinux with Binutils-2.42. Not sure why. IIRC this is also happening with RISC-V vmlinux.

xry111 commented 3 weeks ago

A modified Linux kernel boots fine with CONFIG_RELR=y. But there are some unpacked R_LARCH_RELATIVE against the .la.abs section of vmlinux. Need some investigation...

By adding .align 8 into the .la.abs section these relative relocs are packed.

xry111 commented 1 week ago

Done: https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=d89ecf33ab6dd86290b43dc3751d66f263614e68