[PATCH] x86-64: Increase output copy relocation section alignment
Alan Modra
amodra@gmail.com
Mon Feb 17 13:01:51 GMT 2025
On Mon, Feb 17, 2025 at 06:14:29PM +0800, H.J. Lu wrote:
> PR ld/32690
> * elfxx-x86.c (_bfd_x86_elf_adjust_dynamic_symbol): Adjust the
> output copy relocation section alignment if needed.
This is a tricky situation, isn't it? Good job tracking down exactly
what was happening! The alignment would propagate to the output
section on the next sizing pass, but that's too late for the relro
code on the current pass.
> --- a/bfd/elfxx-x86.c
> +++ b/bfd/elfxx-x86.c
> @@ -3390,6 +3390,7 @@ _bfd_x86_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
> struct elf_dyn_relocs *p;
> const struct elf_backend_data *bed
> = get_elf_backend_data (info->output_bfd);
> + bool ret;
>
> eh = (struct elf_x86_link_hash_entry *) h;
>
> @@ -3608,7 +3609,23 @@ _bfd_x86_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
> h->needs_copy = 1;
> }
>
> - return _bfd_elf_adjust_dynamic_copy (info, h, s);
> + ret = _bfd_elf_adjust_dynamic_copy (info, h, s);
> + if (bed->target_id == X86_64_ELF_DATA)
Why just for x86_64?
> + {
> + /* NB: Increase the output copy relocation section alignment so
> + that relro base and end values are properly computed. */
> + asection *output_sec
> + = bfd_get_linker_section (info->output_bfd, s->name);
Also, can't you use output_sec = s->output_section here?
> + if (output_sec)
> + {
> + unsigned int power_of_two = bfd_section_alignment (s);
> + /* Adjust the section alignment if needed. */
> + if (power_of_two > bfd_section_alignment (output_sec)
> + && !bfd_set_section_alignment (output_sec, power_of_two))
> + return false;
> + }
> + }
> + return ret;
> }
>
> void
In fact, can't you bump the output section alignment in
_bfd_elf_adjust_dynamic_copy for all targets?
diff --git a/bfd/elflink.c b/bfd/elflink.c
index c9468ff42ac..61750e325c5 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -3389,6 +3389,14 @@ _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
/* Adjust the section alignment if needed. */
if (!bfd_set_section_alignment (dynbss, power_of_two))
return false;
+ /* Adjust output section alignment too, so that layout adjusts
+ for alignment on the current lang_size_sections pass. This
+ is important for lang_size_relro_segment which will use the
+ input section alignment. */
+ asection *osec = dynbss->output_section;
+ if (osec && power_of_two > bfd_section_alignment (osec)
+ && !bfd_set_section_alignment (osec, power_of_two))
+ return false;
}
/* We make sure that the symbol will be aligned properly. */
--
Alan Modra
More information about the Binutils
mailing list