Bug 93195 - -fpatchable-function-entries : __patchable_function_entries should consider comdat groups
Summary: -fpatchable-function-entries : __patchable_function_entries should consider c...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 10.0
: P3 normal
Target Milestone: 11.0
Assignee: Not yet assigned to anyone
URL: https://gcc.gnu.org/ml/gcc-patches/20...
Keywords: patch, wrong-code
: 93197 93536 (view as bug list)
Depends on: 93536
Blocks:
  Show dependency tree / graph
 
Reported: 2020-01-08 07:26 UTC by Fangrui Song
Modified: 2025-09-04 17:15 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-02-03 00:00:00


Attachments
A patch (5.20 KB, patch)
2020-02-03 21:27 UTC, H.J. Lu
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Fangrui Song 2020-01-08 07:26:20 UTC
% cat a.cc
inline void foo() {}
void bar() { foo(); }
% cat b.cc
inline void foo() {}
void bar1() { foo(); }
% g++ -fpatchable-function-entry=1 -c a.cc b.cc

Linkers don't allow a relocation to a discarded symbol (foo).

% ld.bfd a.o b.o
...
`.text._Z3foov' referenced in section `__patchable_function_entries' of b.o: defined in discarded section `.text._Z3foov[_Z3foov]' of b.o

% gold a.o b.o
b.o(__patchable_function_entries+0x0): error: relocation refers to local symbol "" [5], which is defined in a discarded section
  section group signature: "_Z3foov"
  prevailing definition is from a.o

% ld.lld a.o b.o
ld.lld: error: relocation refers to a discarded section: .text._Z3foov
>>> defined in b.o
>>> referenced by b.cc
>>>               b.o:(__patchable_function_entries+0x0)
Comment 1 Fangrui Song 2020-02-01 17:54:55 UTC
This is similar to --gc-sections (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93536) but a bit different.

The only reasonable fix I can think of is to place __patchable_function_entries in the same section group.
The ELF spec says:

> A symbol table entry with STB_LOCAL binding that is defined relative to one of a group's sections, and that is contained in a symbol table section that is not part of the group, must be discarded if the group members are discarded. References to this symbol table entry from outside the group are not allowed.

Both GCC and clang reference a .L local symbol in __patchable_function_entries. The __patchable_function_entries must be discarded when the associated text section is discarded.
We don't want __patchable_function_entries.foo __patchable_function_entries.bar because that can waste lots of bytes in .shstrtab .


clang -fpatchable-function-entry=2 -S a.cc b.cc

# COMDAT and SHF_LINK_ORDER are used at the same time
	.section	__patchable_function_entries,"awo",@progbits,_Z3barv,unique,0
	.p2align	3
	.quad	.Lfunc_begin0

	.section	__patchable_function_entries,"aGwo",@progbits,_Z3foov,comdat,_Z3foov,unique,1
	.p2align	3
	.quad	.Lfunc_begin1

Because GNU as and ld don't have the features yet. So when -no-integrated-as is specified (the output is expected to be consumable by GNU as)

clang -fpatchable-function-entry=2 -no-integrated-as -S a.cc b.cc

## The assembler will combine sections with the same name
## If either .Lfunc_begin0 or .Lfunc_begin1 is discarded, the linker will report an error.
	.section	__patchable_function_entries,"aw",@progbits
	.p2align	3
	.quad	.Lfunc_begin0

	.section	__patchable_function_entries,"aw",@progbits
	.p2align	3
	.quad	.Lfunc_begin1
Comment 2 H.J. Lu 2020-02-03 21:26:03 UTC
*** Bug 93197 has been marked as a duplicate of this bug. ***
Comment 3 H.J. Lu 2020-02-03 21:27:02 UTC
Created attachment 47773 [details]
A patch
Comment 4 H.J. Lu 2020-02-07 02:59:22 UTC
*** Bug 93536 has been marked as a duplicate of this bug. ***
Comment 5 Jakub Jelinek 2020-05-07 11:56:19 UTC
GCC 10.1 has been released.
Comment 6 Richard Biener 2020-07-23 06:51:58 UTC
GCC 10.2 is released, adjusting target milestone.
Comment 7 GCC Commits 2020-12-02 13:38:52 UTC
The master branch has been updated by H.J. Lu <hjl@gcc.gnu.org>:

https://gcc.gnu.org/g:694d4a6d0c466d0fbc97920a9c6641a7b349ca35

commit r11-5656-g694d4a6d0c466d0fbc97920a9c6641a7b349ca35
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Wed Dec 2 05:32:37 2020 -0800

    Use the section flag 'o' for __patchable_function_entries
    
    This commit in GNU binutils 2.35:
    
    https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=commit;h=b7d072167715829eed0622616f6ae0182900de3e
    
    added the section flag 'o' to .section directive:
    
    .section __patchable_function_entries,"awo",@progbits,foo
    
    which specifies the symbol name which the section references.  Assembler
    creates a unique __patchable_function_entries section with the section,
    where foo is defined, as its linked-to section.  Linker keeps a section
    if its linked-to section is kept during garbage collection.
    
    This patch checks assembler support for the section flag 'o' and uses
    it to implement __patchable_function_entries section.  Since Solaris may
    use GNU assembler with Solairs ld.  Even if GNU assembler supports the
    section flag 'o', it doesn't mean that Solairs ld supports it.  This
    feature is disabled for Solairs targets.
    
    gcc/
    
            PR middle-end/93195
            PR middle-end/93197
            * configure.ac (HAVE_GAS_SECTION_LINK_ORDER): New.  Define 1 if
            the assembler supports the section flag 'o' for specifying
            section with link-order.
            * output.h (SECTION_LINK_ORDER): New.  Defined to 0x8000000.
            (SECTION_MACH_DEP): Changed from 0x8000000 to 0x10000000.
            * targhooks.c (default_print_patchable_function_entry): Pass
            SECTION_LINK_ORDER to switch_to_section if the section flag 'o'
            works.  Pass current_function_decl to switch_to_section.
            * varasm.c (default_elf_asm_named_section): Use 'o' flag for
            SECTION_LINK_ORDER if assembler supports it.
            * config.in: Regenerated.
            * configure: Likewise.
            * doc/sourcebuild.texi: Document o_flag_in_section.
    
    gcc/testsuite/
    
            PR middle-end/93195
            * g++.dg/pr93195a.C: New test.
            * g++.dg/pr93195b.C: Likewise.
            * lib/target-supports.exp
            (check_effective_target_o_flag_in_section): New proc.
Comment 8 H.J. Lu 2020-12-02 13:39:24 UTC
Fixed for GCC 11.
Comment 9 Jakub Jelinek 2020-12-15 21:01:15 UTC
I believe this broke building the kernel, see
https://gcc.gnu.org/pipermail/gcc-patches/2020-December/561974.html
for details.
Comment 10 Fangrui Song 2020-12-17 03:59:41 UTC
(In reply to Jakub Jelinek from comment #9)
> I believe this broke building the kernel, see
> https://gcc.gnu.org/pipermail/gcc-patches/2020-December/561974.html
> for details.

For

> ld: .init.data has both ordered [`__patchable_function_entries' in init/main.o] and unordered [`.init.data' in ./drivers/firmware/efi/libstub/vsprintf.stub.o] sections

ld should be flexible in mixed SHF_LINK_ORDER & non-SHF_LINK_ORDER components in an output section
https://sourceware.org/bugzilla/show_bug.cgi?id=26256