[PATCH] elf: Avoid relocating non-allocated code sections
Jan Beulich
jbeulich@suse.com
Thu Jul 30 11:21:34 GMT 2026
On 30.07.2026 12:48, H.J. Lu wrote:
> On Thu, Jul 30, 2026 at 2:57 PM Jan Beulich <jbeulich@suse.com> wrote:
>> On 30.07.2026 02:33, H.J. Lu wrote:
>>> Since non-allocated code sections can't be properly relocated, issue an
>>> error on such input.
>>>
>>> PR ld/34444
>>> * elflink.c (elf_link_input_bfd): Issue an error on a non-allocated
>>> code section.
>>
>> In addition to what Alan said - why would text sections be different here?
>> Iirc this isn't the first time that you want to treat them specially when
>> there's no provision for that in the spec. Once that restriction was
>> removed, I expect your check would trigger on e.g. debugging sections,
>> indicating that it's wrong to have.
>>
>> Jan
>
> _bfd_elf_link_iterate_on_relocs has
>
> for (o = abfd->sections; o != NULL; o = o->next)
> {
> Elf_Internal_Rela *internal_relocs;
> bool ok;
>
> /* Don't check relocations in excluded sections. Don't do
> anything special with non-loaded, non-alloced sections.
> In particular, any relocs in such sections should not
> affect GOT and PLT reference counting (ie. we don't
> allow them to create GOT or PLT entries), there's no
> possibility or desire to optimize TLS relocs, and
> there's not much point in propagating relocs to shared
> libs that the dynamic linker won't relocate. */
> if ((o->flags & SEC_ALLOC) == 0
> || (o->flags & SEC_RELOC) == 0
> || (o->flags & SEC_EXCLUDE) != 0
> || o->reloc_count == 0
> || ((info->strip == strip_all || info->strip == strip_debugger)
> && (o->flags & SEC_DEBUGGING) != 0)
> || bfd_is_abs_section (o->output_section))
> continue;
>
> internal_relocs = _bfd_elf_link_info_read_relocs
> (abfd, info, o, NULL, NULL,
> _bfd_elf_link_keep_memory (info));
> if (internal_relocs == NULL)
> return false;
>
> ok = action (abfd, info, o, internal_relocs);
>
> When non-alloced sections use GOT or PLT, their
> usages aren't counted. Then elf_link_input_bfd
> calls relocate_section on these sections. When
> x86 backend tries to use GOT or PLT to resolve
> relocations in non-alloced sections, everything goes
> downhill from there.
So that may be where a correction is needed then.
> This patch:
>
> diff --git a/bfd/elflink.c b/bfd/elflink.c
> index 0af9837a28c..13eddee672a 100644
> --- a/bfd/elflink.c
> +++ b/bfd/elflink.c
> @@ -4299,8 +4299,7 @@ _bfd_elf_link_iterate_on_relocs
> possibility or desire to optimize TLS relocs, and
> there's not much point in propagating relocs to shared
> libs that the dynamic linker won't relocate. */
> - if ((o->flags & SEC_ALLOC) == 0
> - || (o->flags & SEC_RELOC) == 0
> + if ((o->flags & SEC_RELOC) == 0
> || (o->flags & SEC_EXCLUDE) != 0
> || o->reloc_count == 0
> || ((info->strip == strip_all || info->strip == strip_debugger)
>
> avoids the crash. But I don't think it is correct.
Indeed.
Jan
More information about the Binutils
mailing list