Bug 95095 - Feature request: support -fno-unique-section-names
Summary: Feature request: support -fno-unique-section-names
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: unknown
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-05-12 21:57 UTC by Fangrui Song
Modified: 2021-01-25 20:11 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Fangrui Song 2020-05-12 21:57:51 UTC
-ffunction-sections produces sections .text.foo , .text.bar , etc, which can take significant amount of string table space.

In clang, -fno-unique-section-names emits multiple ".text" sections which can share the section name. Multiple sections with the same name require the new GNU as feature https://sourceware.org/bugzilla/show_bug.cgi?id=25380 (binutils 2.35).

For .text.exit.* .text.unlikely.* .text.hot.* .text.startup.* , the preferred sections are .text.exit. .text.unlikely. .text.hot. .text.startup. The trailing dots can avoid a linker problem described in https://reviews.llvm.org/D79600

--------- pasted below for your convenience

GNU ld's internal linker script uses (https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=add44f8d5c5c05e08b11e033127a744d61c26aee)

.text           :
{
  *(.text.unlikely .text.*_unlikely .text.unlikely.*)
  *(.text.exit .text.exit.*)
  *(.text.startup .text.startup.*)
  *(.text.hot .text.hot.*)
  *(SORT(.text.sorted.*))
  *(.text .stub .text.* .gnu.linkonce.t.*)
  /* .gnu.warning sections are handled specially by elf.em.  */
  *(.gnu.warning)
}
Because *(.text.exit .text.exit.*) is ordered before *(.text .text.*), in a -ffunction-sections build, the C library function exit will be placed before other functions.
gold's -z keep-text-section-prefix has the same problem.

In lld, -z keep-text-section-prefix recognizes .text.{exit,hot,startup,unlikely,unknown}.*, but not .text.{exit,hot,startup,unlikely,unknown}, to avoid the strange placement problem.
Comment 1 Fangrui Song 2020-05-24 22:08:47 UTC
I just learned that `int main() {}` compiles to .text.startup in -O2 or -Os

It seems that .text.startup. may be better to not accidentally move a C function named `startup` (`startup.` is not a valid C identifier)
Comment 2 Segher Boessenkool 2021-01-16 20:49:13 UTC
Can't we use ".text%name" for -ffunction-sections, like we did originally,
in 1996?  See cf4403481dd6.  This does not conflict with other section
names, and does not have all the problems you get from doing anything that
is not a simple prefix.
Comment 3 Fangrui Song 2021-01-16 21:00:56 UTC
(In reply to Segher Boessenkool from comment #2)
> Can't we use ".text%name" for -ffunction-sections, like we did originally,
> in 1996?  See cf4403481dd6.  This does not conflict with other section
> names, and does not have all the problems you get from doing anything that
> is not a simple prefix.

A function named 'foo' compiles to '.text%foo'? It might have been better to avoid conflicts with '.text.startup' '.text.hot' etc but now such a change would just inconvenience users (think of various Linux kernel linker script fragments).

.text%name does not address -fno-unique-section-names.
Comment 4 Segher Boessenkool 2021-01-22 00:57:37 UTC
On Sat, Jan 16, 2021 at 09:00:56PM +0000, i at maskray dot me wrote:
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95095
> 
> --- Comment #3 from Fangrui Song <i at maskray dot me> ---
> (In reply to Segher Boessenkool from comment #2)
> > Can't we use ".text%name" for -ffunction-sections, like we did originally,
> > in 1996?  See cf4403481dd6.  This does not conflict with other section
> > names, and does not have all the problems you get from doing anything that
> > is not a simple prefix.
> 
> A function named 'foo' compiles to '.text%foo'? It might have been better to
> avoid conflicts with '.text.startup' '.text.hot' etc but now such a change
> would just inconvenience users (think of various Linux kernel linker script
> fragments).

The conflicts that show up from time to time *also* inconvenience users,
and that is an ongoing inconvenience.

> .text%name does not address -fno-unique-section-names.

And neither does ".text.name.".

Something like this is a prerequisite of such an option.

Also, please use a different name, one that does not start with "no-"?
It isn't about unique names at all anyway, it is about having multiple
sections with the same name (that then can get separately GCed, because
of new linker features).


Segher
Comment 5 Fangrui Song 2021-01-22 01:38:17 UTC
Linux kernel

include/asm-generic/vmlinux.lds.h currently has
#define TEXT_TEXT                                                       \
                ALIGN_FUNCTION();                                       \
                *(.text.hot .text.hot.*)                                \
                *(TEXT_MAIN .text.fixup)                                \
                *(.text.unlikely .text.unlikely.*)                      \
                *(.text.unknown .text.unknown.*)                        \
                NOINSTR_TEXT                                            \
                *(.text..refcount)                                      \
                *(.ref.text)                                            \
        MEM_KEEP(init.text*)                                            \
        MEM_KEEP(exit.text*)                                            \

If you change .text.* to .text%* , this script will need a change, along with other projects which use or adapt GNU ld's built-in linker script

  .text           :
  {
    *(.text.unlikely .text.*_unlikely .text.unlikely.*)
    *(.text.exit .text.exit.*)
    *(.text.startup .text.startup.*)
    *(.text.hot .text.hot.*)
    *(SORT(.text.sorted.*))
    *(.text .stub .text.* .gnu.linkonce.t.*)
    /* .gnu.warning sections are handled specially by elf.em.  */
    *(.gnu.warning)
  }

By default, -fno-unique-section-names produces '.text' instead of '.text.foo' in the normal -ffunction-sections case.

For PGO, -fno-unique-section-names produces '.text.hot.' instead of '.text.hot.foo' in the normal -ffunction-sections case.

'.text.hot.' is an attempt to distinguish PGO caused 'hot' from a regular functions named 'hot'.
Comment 6 Segher Boessenkool 2021-01-25 16:30:01 UTC
I was under the impression this unique section thing needed the trailing
dot thing.  This probably is not true.

I still think the old "%" thing is much superior to the trailing dot thing,
but that then is orthogonal to the "unique section" thing, so let's ignore
it now :-)

It still remains that this flag needs a name that says what it *does*, as I
mentioned at the end of Comment 4.
Comment 7 Fangrui Song 2021-01-25 17:58:37 UTC
(In reply to Segher Boessenkool from comment #6)
> I was under the impression this unique section thing needed the trailing
> dot thing.  This probably is not true.
> 
> I still think the old "%" thing is much superior to the trailing dot thing,
> but that then is orthogonal to the "unique section" thing, so let's ignore
> it now :-)
> 
> It still remains that this flag needs a name that says what it *does*, as I
> mentioned at the end of Comment 4.

-ffunction-sections -fno-unique-section-names =>

.text.%
.text.startup.%
.text.hot.%
.text.cold.%
...

?

I agree that it is superior. If GCC wants to support this scheme, that looks fine to me. It is likely that I can migrate Clang to this scheme as well.

I think

.text%
.text.startup%
.text.hot%
.text.cold%
...

is slightly worse.
Comment 8 Segher Boessenkool 2021-01-25 19:04:18 UTC
I say nothing like that.  I say that
  .text.hot.
is nasty (is easily mistaken for .text.hot).

I also say that and that named-per-function sections are better as
  .text%name
than as
  .text.name
(just as they were long ago), because this doesn't conflict with things like
  .text.hot
(and there is a very long history of such conflicts giving real-world problems).
Comment 9 Fangrui Song 2021-01-25 20:11:11 UTC
(In reply to Segher Boessenkool from comment #8)
> I say nothing like that.  I say that
>   .text.hot.
> is nasty (is easily mistaken for .text.hot).
> 
> I also say that and that named-per-function sections are better as
>   .text%name
> than as
>   .text.name
> (just as they were long ago), because this doesn't conflict with things like
>   .text.hot
> (and there is a very long history of such conflicts giving real-world
> problems).

.text%name and .text.hot%name will break existing output section descriptions for .text

My scheme .text.% .text.hot.% is backward compatible.