__patchable_function_entries is not a GC root, and not referenced by a retained section. It will thus be garbage collected. The only solution I can think of requires fixes to both GCC and GNU ld. * GNU ld: implement interaction between SHF_LINK_ORDER and --gc-sections https://sourceware.org/bugzilla/show_bug.cgi?id=24526 * GCC: Create one __patchable_function_entry section for each function. For each function `foo`, + If foo needs to be placed in a comdat group, place __patchable_function_entry in the comdat group + Otherwise, set the SHF_LINK_ORDER flag of __patchable_function_entry and set its sh_link to reference the section containing `foo`
I opened a linker bug: https://sourceware.org/bugzilla/show_bug.cgi?id=25490
A patch to support the section flag 'o' in .section directive is posted at https://sourceware.org/ml/binutils/2020-02/msg00038.html
Fixing PR 93195 will also fix this: [hjl@gnu-cfl-1 pr93197]$ cat a.c void foo() {} [hjl@gnu-cfl-1 pr93197]$ cat b.c extern void foo(void); void _start () { foo (); } [hjl@gnu-cfl-1 pr93197]$ make /export/build/gnu/tools-build/gcc-gitlab-debug/build-x86_64-linux/gcc/xgcc -B/export/build/gnu/tools-build/gcc-gitlab-debug/build-x86_64-linux/gcc/ -fpatchable-function-entry=1 -S a.c /export/build/gnu/tools-build/gcc-gitlab-debug/build-x86_64-linux/gcc/xgcc -B/export/build/gnu/tools-build/gcc-gitlab-debug/build-x86_64-linux/gcc/ -fpatchable-function-entry=1 -S b.c /export/build/gnu/tools-build/gcc-gitlab-debug/build-x86_64-linux/gcc/xgcc -B/export/build/gnu/tools-build/gcc-gitlab-debug/build-x86_64-linux/gcc/ -fpatchable-function-entry=1 -c -o a.o a.s /export/build/gnu/tools-build/gcc-gitlab-debug/build-x86_64-linux/gcc/xgcc -B/export/build/gnu/tools-build/gcc-gitlab-debug/build-x86_64-linux/gcc/ -fpatchable-function-entry=1 -c -o b.o b.s ld --gc-sections -o x a.o b.o readelf -SW x | grep __patchable_function_entries [ 4] __patchable_function_entries PROGBITS 0000000000404000 003000 000010 00 WAL 2 0 8 [hjl@gnu-cfl-1 pr93197]$ cat a.s .file "a.c" .text .globl foo .type foo, @function foo: .section __patchable_function_entries,"awo",@progbits,foo .align 8 .quad .LPFE1 .text .LPFE1: nop .LFB0: .cfi_startproc pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 nop popq %rbp .cfi_def_cfa 7, 8 ret .cfi_endproc .LFE0: .size foo, .-foo .ident "GCC: (GNU) 10.0.1 20200203 (experimental)" .section .note.GNU-stack,"",@progbits [hjl@gnu-cfl-1 pr93197]$ cat b.s .file "b.c" .text .globl _start .type _start, @function _start: .section __patchable_function_entries,"awo",@progbits,_start .align 8 .quad .LPFE1 .text .LPFE1: nop .LFB0: .cfi_startproc pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 call foo nop popq %rbp .cfi_def_cfa 7, 8 ret .cfi_endproc .LFE0: .size _start, .-_start .ident "GCC: (GNU) 10.0.1 20200203 (experimental)" .section .note.GNU-stack,"",@progbits [hjl@gnu-cfl-1 pr93197]$ *** This bug has been marked as a duplicate of bug 93195 ***
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.