[PATCH v2 0/9] ld: Implement DT_RELR for x86
Fangrui Song
i@maskray.me
Sat Jan 8 21:53:13 GMT 2022
On 2022-01-08, H.J. Lu via Binutils wrote:
>Hi Nick,
>
>I'd like to enable DT_RELR for x86 in binutils 2.38.
>
>
>H.J.
>---
>Changes in v2:
>
>1. Drop the _bfd_elf_link_iterate_on_relocs patch, which has been checked
>into master branch.
>2. Also pack R_*_RELATIVE relocations against dynamic symbols.
>3. Skip relocation against IFUNC symbol earlier.
>4. Don't require the --relax option enabled.
>5. Add more DT_RELR tests:
> a. Add a test with relative relocation in section with 1-byte
> alignment.
> b. Add a test with -z pack-relative-relocs --no-relax.
> c. Add tests for packing R_*_RELATIVE relocations against dynamic
> symbols.
>
>DT_RELR encodes consecutive R_*_RELATIVE relocations in GOT (the global
>offset table) and data sections in a compact format:
>
>https://groups.google.com/g/generic-abi/c/bX460iggiKg
>
>On some targets, R_*_RELATIVE relocations are counted and the GOT offsets
>are allocated when setting the dynamic section sizes after seeing all
>relocations. R_*_RELATIVE relocations are generated while relocating
>sections after section layout has been finalized.
>
>For x86 targets, the old check_relocs is renamed to scan_relocs and a
>new check_relocs is added to chek input sections and create dynamic
>relocation sections so that they will be mapped to output sections.
>scan_relocs is now called from elf_backend_always_size_sections.
>
>On some targets, the DT_RELR section size can be computed only after all
>symbols addresses can be determined:
>
>1. Update ldelf_map_segments to pass need_layout to
>_bfd_elf_map_sections_to_segments which will size DT_RELR section and
>set need_layout to true if the DT_RELR section size is changed.
>2. Set the preliminary DT_RELR section size before mapping sections to
>segments and set the final DT_RELR section size after regular symbol
>processing is done.
>
>On x86, DT_RELR is implemented with linker relaxation:
>
>1. During linker relaxation, we scan input relocations with the same
>logic in relocate_section to determine if a relative relocation should
>be generated and save the relative relocation candidate information for
>sizing the DT_RELR section later after all symbols addresses can be
>determined. For these relative relocations which can't be placed in
>the DT_RELR section, they will be placed in the rela.dyn/rel.dyn
>section.
>2. When DT_RELR is enabled, _bfd_elf_map_sections_to_segments calls a
>backend function to size the DT_RELR section which will compute the
>DT_RELR section size and tell ldelf_map_segments to layout sections
>again when the DT_RELR section size has been increased.
>3. After regular symbol processing is finished, bfd_elf_final_link calls
>a backend function to finish the DT_RELR section.
>
>When DT_RELR is enabled, to avoid random run-time crash with older glibc
>binaries without DT_RELR support, add a GLIBC_ABI_DT_RELR symbol version,
>which is provided by glibc with DT_RELR support, dependency on the shared
>C library if it provides a GLIBC_2.XX symbol version.
>
>It can build DT_RELR enabled glibc successfully on x86-64, x32 and
>i686.
>
>H.J. Lu (9):
> elf: Add .relr.dyn to special_sections_r
> elf: Extract _bfd_elf_process_reverse_copy
> elf: Pass need_layout to _bfd_elf_map_sections_to_segments
> ld: Initial DT_RELR support
> elf: Add size_relative_relocs and finish_relative_relocs
> elf: Support DT_RELR in linker tests
> x86: Add DT_RELR support
> ld: Add simple DT_RELR tests
> ld: Add glibc dependency for DT_RELR
Thank you so much for the heroic work.
I cannot think of other items.
When linking clang, there is no longer RELA R_X86_64_RELATIVE. Nice!
clang -fsanitize=memory built ld passed.
gcc -fsanitize=address built ld passed (ld has pre-existing leaks. This
patch series does not add more leaks.)
Reviewed-by: Fangrui Song <i@maskray.me>
(perhaps binutils doesn't use this...)
More information about the Libc-alpha
mailing list