https://sourceware.org/bugzilla/show_bug.cgi?id=35099

            Bug ID: 35099
           Summary: ld -r segfaults on sparc64 over an ifunc defined and
                    referenced in different objects
           Product: binutils
           Version: 2.48 (HEAD)
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: ld
          Assignee: unassigned at sourceware dot org
          Reporter: stian at itx dot no
  Target Milestone: ---
              Host: sparc64-unknown-linux-gnu
            Target: sparc64-unknown-linux-gnu
             Build: sparc64-unknown-linux-gnu

ld from master faults in a relocatable link on sparc64 when an
STT_GNU_IFUNC symbol is defined in one input object and referenced from
another. Debian hits it building glibc 2.43 with binutils
2.47.50.20261120: the libc_pic.os rule, which is "ld -r --whole-archive
libc_pic.a", dies with SIGSEGV.

Two small objects are enough to show it. def.c:

  static void impl (void) { }
  static void *resolver (void) { return (void *) impl; }
  void foo (void) __attribute__ ((ifunc ("resolver")));

ref.c:

  extern void foo (void);
  void bar (void) { foo (); }

  $ cc -fPIC -c def.c -o def.o
  $ cc -fPIC -c ref.c -o ref.o
  $ ld -r def.o ref.o -o out.os
  Segmentation fault

It does need the two objects. A symbol defined and referenced in the
same object never gets ref_regular set, and the faulting branch tests
it.

  #0  allocate_dynrelocs (h=..., inf=...) at bfd/elfxx-sparc.c:2075
  #1  bfd_link_hash_traverse (...) at bfd/linker.c:693
  #3  _bfd_sparc_elf_late_size_sections (...) at bfd/elfxx-sparc.c:2501
  #4  bfd_elf_size_dynamic_sections (...) at bfd/elflink.c:7804
  #5  ldelf_before_allocation (...) at ld/ldelf.c:1898

allocate_dynrelocs takes its PLT branch on

  h->type == STT_GNU_IFUNC && h->def_regular && h->ref_regular

and dereferences htab->elf.splt, or htab->elf.iplt if splt is NULL. At
the fault both are NULL. _bfd_sparc_elf_check_relocs is the only caller
of create_ifunc_sections on sparc, and it returns early for a
relocatable link.

_bfd_sparc_elf_late_size_sections used to be a no-op for -r. Its guard
is "if (dynobj == NULL) return true;" and nothing set dynobj in a
relocatable link. Commit 447d46c9ad "Set dynobj when creating stub bfd"
sets htab->dynobj = stub_file->the_bfd in ldelf_after_open_output for
every ELF link, so the guard stopped firing.

I bisected it on sparc64 with those two objects. 2.47 (6ce87bbc52) and
447d46c9ad^ (95d6c06bd6) link fine, 447d46c9ad and master (10070d2c08)
fault.

x86-64 is not affected, since _bfd_x86_elf_link_setup_gnu_properties
creates .iplt there and allocate_dynrelocs always has a section to
size.

The patcch I have returns early from _bfd_sparc_elf_late_size_sections
for a relocatable link, and links one relocatable object in
ld-ifunc/ifunc.exp. I will post it to [email protected].

Leaving dynobj alone in ldelf_after_open_output for a relocatable link
fixes it too, for every ELF target at once, but that re-opens the
BFD_ASSERT (htab->dynobj != NULL) added by 447d46c9ad.

-- 
You are receiving this mail because:
You are on the CC list for the bug.

Reply via email to