Bug 1791321

Summary: gcc: weakref attribute introduces the need for copy or text relocations on ppc64
Product: Red Hat Enterprise Linux 7 Reporter: Florian Weimer <fweimer>
Component: gccAssignee: Marek Polacek <mpolacek>
Status: CLOSED WONTFIX QA Contact: Alexandra Petlanová Hájková <ahajkova>
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: 7.9CC: fweimer, jakub, kdudka, law, ohudlick, sipoyare, vmukhame
Target Milestone: rc   
Target Release: ---   
Hardware: ppc64   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2020-04-28 19:42:58 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Florian Weimer 2020-01-15 14:13:04 UTC
This is essentially a clone of bug 1790475, filed for tracking purposes.

This program:

static void alias (void) __attribute__ ((weakref ("implementation")));
const void *const ptr = alias;

Produces this construct:

        .section        ".toc","aw"
        .align 3
        .section        ".text"
        .machine power7
        .globl ptr
        .weakref        alias, implementation
        .section        .rodata
        .align 3
        .type   ptr, @object
        .size   ptr, 8
ptr:
        .quad   alias
        .ident  "GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-39)"
        .section        .note.GNU-stack,"",@progbits

The problem is that with the POWER ELFv1 ABI, the address of implementation cannot be resolved by the link editor. It can live in a DSO.  Until recently, binutils ld would produce a copy relocation for the OPD entry to cope with that, but those only work with lazy binding (see the binutils ld bug or bug 1790475). With the binutils fix, we get a text relocation instead. But that this is not an improvement.

With GCC 7, I get this instead, which avoids the problem entirely:

        .section        ".text"
        .globl ptr
        .weakref        alias, implementation
        .section        .data.rel.ro,"aw",@progbits
        .align 3
        .type   ptr, @object
        .size   ptr, 8
ptr:
        .quad   alias
        .ident  "GCC: (GNU) 7.3.1 20180303 (Red Hat 7.3.1-5)"
        .section        .note.GNU-stack,"",@progbits

I think we should verify that this is really fixed in later GCC versions which are included in DTS, so that we can safely pick up the upstream binutils fix there eventually, without generating text relocations.

I don't know if we should backport that fix into system GCC, but we should at least consider it.

Comment 2 Jakub Jelinek 2020-01-15 18:42:58 UTC
This got fixed with https://gcc.gnu.org/r206784 aka https://gcc.gnu.org/ml/gcc-patches/2014-01/msg01169.html
Although it seems that the patch was written mainly with the other direction in mind, i.e. that even when
TREE_PUBLIC (tem) && targetm.binds_local_p (tem) (such as when the decl is hidden visibility) we could use reloc & 1
rather than reloc & 2, the weakref case is when !TREE_PUBLIC (tem) && !targetm.binds_local_p (tem) and in that case
it is just local convention that TREE_PUBLIC is clear, because the weakref target might not bind locally and so we need it in
writable or relro section rather than rodata.
So, I think we want to stay on the safe side, we could always patch it to
  if (TREE_PUBLIC (tem) || !targetm.binds_local_p (tem))
    reloc |= 2;
  else
    reloc |= 1;