[PATCH v2] gas: Add --unique-pushsection
Fangrui Song
i@maskray.me
Sat May 17 19:06:29 GMT 2025
On Sat, May 17, 2025 at 12:38 AM Jan Beulich <jbeulich@suse.com> wrote:
>
> On 16.05.2025 23:15, H.J. Lu wrote:
> > Linker kernel uses .pushsection directive extensively. But when
> > -ffunction-sections is used to build kernel, for
> >
> > __attribute__((always_inline))
> > static void inline
> > test_section(void)
> > {
> > asm goto("jmp 1f\n"
> > "\t.pushsection .alt_section, \"ax\"\n"
> > "1:\n"
> > "\tjmp %l[t_label]\n"
> > "\t.popsection\n"
> > : :
> > : : t_label);
> > t_label:
> > return;
> > }
> >
> > void
> > foo(void)
> > {
> > test_section();
> > }
> >
> > void
> > bar(void)
> > {
> > test_section();
> > }
> >
> > we get
> >
> > .text
> > .section .text.foo,"ax",@progbits
> > .p2align 4
> > .globl foo
> > .type foo, @function
> > foo:
> > .LFB1:
> > .cfi_startproc
> > jmp 1f
> > .pushsection .alt_section, "ax"
> > 1:
> > jmp .L2
> > .popsection
> >
> > .L2:
> > ret
> > .L3:
> > .cfi_endproc
> > .LFE1:
> > .size foo, .-foo
> > .section .text.bar,"ax",@progbits
> > .p2align 4
> > .globl bar
> > .type bar, @function
> > bar:
> > .LFB2:
> > .cfi_startproc
> > jmp 1f
> > .pushsection .alt_section, "ax"
> > 1:
> > jmp .L6
> > .popsection
> >
> > .L6:
> > ret
> > .L7:
> > .cfi_endproc
> > .LFE2:
> > .size bar, .-bar
> >
> > Functions foo and bar are placed in the different sections. But since
> > ".pushsection .alt_section" always generates the same .alt_section
> > section for foo and bar, --gc-sections doesn't remove foo when bar is
> > referenced while foo isn't. To help linker to remove the unreferenced
> > functions with .pushsection directive:
> >
> > Add --unique-pushsection option to ELF assembler to append the current
> > section name to the new section name for .pushsection.
> >
> > Another option is to replace ".pushsection .alt_section" with
> > ".pushsection .alt_section%S" and compile with -Wa,--sectname-subst.
>
> As before, this being enough, I see no reason why we would need new
> logic to deal with a sub-case. Unless Nick or Alan view this
> differently, this not NOT approved.
>
> Jan
I'm wary of the global behavior-changing option, as its deployment
complexity makes it an impractical choice.
Users generally prefer syntax that applies to specific fragments of
their assembly code.
As an alternative, ,unique (binutils 2.35) works with .pushsection and
can be used in the Linux kernel.
The unique ID can be derived from %=
https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#Special-format-strings
The latest mainline Linux kernel currently requires binutils 2.30
(https://www.kernel.org/doc/html/next/process/changes.html), which
predates the introduction of ,unique .
While -ffunction-sections -fdata-sections requires an opt-in and
unpopular CONFIG_LD_DEAD_CODE_DATA_ELIMINATION , using %= probably
requires some C macros.
More information about the Binutils
mailing list