Re: [PATCH] elf: Avoid relocating non-allocated code sections
"H.J. Lu" <[email protected]> Thu, 30 Jul 2026 18:48:59 +0800
| Newsgroups | gmane.comp.gnu.binutils |
|---|---|
| Message-ID | <CAMe9rOoh3XhQnT_wY_4aUjXYtiSSohjegtF5Th3VaaRBRrYneg@mail.gmail.com> |
On Thu, Jul 30, 2026 at 2:57=E2=80=AFPM Jan Beulich <[email protected]> wro= te: > > 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 =3D abfd->sections; o !=3D NULL; o =3D 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) =3D=3D 0 || (o->flags & SEC_RELOC) =3D=3D 0 || (o->flags & SEC_EXCLUDE) !=3D 0 || o->reloc_count =3D=3D 0 || ((info->strip =3D=3D strip_all || info->strip =3D=3D strip= _debugger) && (o->flags & SEC_DEBUGGING) !=3D 0) || bfd_is_abs_section (o->output_section)) continue; internal_relocs =3D _bfd_elf_link_info_read_relocs (abfd, info, o, NULL, NULL, _bfd_elf_link_keep_memory (info)); if (internal_relocs =3D=3D NULL) return false; ok =3D 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. 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) =3D=3D 0 - || (o->flags & SEC_RELOC) =3D=3D 0 + if ((o->flags & SEC_RELOC) =3D=3D 0 || (o->flags & SEC_EXCLUDE) !=3D 0 || o->reloc_count =3D=3D 0 || ((info->strip =3D=3D strip_all || info->strip =3D=3D strip_debu= gger) avoids the crash. But I don't think it is correct. --=20 H.J.