unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v3] Use __executable_start as the lowest address for profiling [BZ #28153]
  2021-07-31 17:11     ` Florian Weimer via Libc-alpha
@ 2021-07-31 17:43       ` H.J. Lu via Libc-alpha
  0 siblings, 0 replies; 4+ messages in thread
From: H.J. Lu via Libc-alpha @ 2021-07-31 17:43 UTC (permalink / raw)
  To: Florian Weimer; +Cc: H.J. Lu via Libc-alpha

[-- Attachment #1: Type: text/plain, Size: 1976 bytes --]

On Sat, Jul 31, 2021 at 10:11 AM Florian Weimer <fweimer@redhat.com> wrote:
>
> * H. J. Lu:
>
> > On Sat, Jul 31, 2021 at 9:36 AM Florian Weimer <fweimer@redhat.com> wrote:
> >>
> >> * H. J. Lu via Libc-alpha:
> >>
> >> > diff --git a/sysdeps/aarch64/start.S b/sysdeps/aarch64/start.S
> >> > index 417da8802b..e46e01ed0b 100644
> >> > --- a/sysdeps/aarch64/start.S
> >> > +++ b/sysdeps/aarch64/start.S
> >> > @@ -42,7 +42,7 @@
> >> >                                       NULL
> >> >   */
> >> >
> >> > -     .text
> >> > +     .section .text.unlikely,"ax",%progbits
> >> >  ENTRY(_start)
> >> >       /* Create an initial frame with 0 LR and FP */
> >> >       cfi_undefined (x30)
> >>
> >> I don't think it's correct to place code that runs during every process
> >> start into .text.unlikely.  Surely we can avoid that page fault.
> >>
> >> Can we fix the ENTRY_POINT assumption in profiling instead?
> >
> > We can do
> >
> > diff --git a/csu/gmon-start.c b/csu/gmon-start.c
> > index b3432885b3..83322fd586 100644
> > --- a/csu/gmon-start.c
> > +++ b/csu/gmon-start.c
> > @@ -48,7 +48,7 @@
> >  #ifdef ENTRY_POINT_DECL
> >  ENTRY_POINT_DECL(extern)
> >  #else
> > -extern char ENTRY_POINT[];
> > +extern char entry_point[] asm (__SYMBOL_PREFIX "main");
> >  #endif
> >  extern char etext[];
> >
> > @@ -56,7 +56,7 @@ extern char etext[];
> >  # ifdef ENTRY_POINT_DECL
> >  #  define TEXT_START ENTRY_POINT
> >  # else
> > -#  define TEXT_START &ENTRY_POINT
> > +#  define TEXT_START &entry_point
> >  # endif
> >  #endif
> >
> > But this may only work with BFD linker which places .text.startup
> > section before .text section.
>
> Can we get the linker to emit a symbol at the start of the text section?

We already have __executable_start which is pretty close to what we
need.   Like this.

> Like it does for orphan sections?
>
> Then we can use a weak symbol reference in gmon-start.c and use the new
> symbol if it is available.
>
> Thanks,
> Florian
>


-- 
H.J.

[-- Attachment #2: v3-0001-Use-__executable_start-as-the-lowest-address-for-.patch --]
[-- Type: application/x-patch, Size: 3003 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v3] Use __executable_start as the lowest address for profiling [BZ #28153]
@ 2021-08-05 12:09 H.J. Lu via Libc-alpha
  2021-08-06  0:16 ` Fangrui Song via Libc-alpha
  0 siblings, 1 reply; 4+ messages in thread
From: H.J. Lu via Libc-alpha @ 2021-08-05 12:09 UTC (permalink / raw)
  To: libc-alpha

Glibc assumes that ENTRY_POINT is the lowest address for which we need
to keep profiling records and BFD linker uses a linker script to place
the input sections.

Starting from GCC 4.6, the main function is placed in .text.startup
section and starting from binutils 2.22, BFD linker with

commit add44f8d5c5c05e08b11e033127a744d61c26aee
Author: Alan Modra <amodra@gmail.com>
Date:   Thu Nov 25 03:03:02 2010 +0000

            * scripttempl/elf.sc: Group .text.exit, text.startup and .text.hot
            sections.

places .text.startup section before .text section, which leave the main
function out of profiling records.

Starting from binutils 2.15, linker provides __executable_start to mark
the lowest address of the executable.  Use __executable_start as the
lowest address to keep the main function in profiling records. This fixes
[BZ #28153].

Tested on Linux/x86-64, Linux/x32 and Linux/i686 as well as with
build-many-glibcs.py.
---
 csu/gmon-start.c              | 10 +++++++++-
 gmon/tst-gmon-gprof.sh        |  2 ++
 gmon/tst-gmon-static-gprof.sh |  2 ++
 3 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/csu/gmon-start.c b/csu/gmon-start.c
index b3432885b3..344606a676 100644
--- a/csu/gmon-start.c
+++ b/csu/gmon-start.c
@@ -52,6 +52,11 @@ extern char ENTRY_POINT[];
 #endif
 extern char etext[];
 
+/* Use __executable_start as the lowest address to keep profiling records
+   if it provided by the linker.  */
+extern const char executable_start[] asm ("__executable_start")
+  __attribute__ ((weak, visibility ("hidden")));
+
 #ifndef TEXT_START
 # ifdef ENTRY_POINT_DECL
 #  define TEXT_START ENTRY_POINT
@@ -92,7 +97,10 @@ __gmon_start__ (void)
   called = 1;
 
   /* Start keeping profiling records.  */
-  __monstartup ((u_long) TEXT_START, (u_long) &etext);
+  if (&executable_start != NULL)
+    __monstartup ((u_long) &executable_start, (u_long) &etext);
+  else
+    __monstartup ((u_long) TEXT_START, (u_long) &etext);
 
   /* Call _mcleanup before exiting; it will write out gmon.out from the
      collected data.  */
diff --git a/gmon/tst-gmon-gprof.sh b/gmon/tst-gmon-gprof.sh
index 9d371582b9..dc0be02110 100644
--- a/gmon/tst-gmon-gprof.sh
+++ b/gmon/tst-gmon-gprof.sh
@@ -39,12 +39,14 @@ trap cleanup 0
 cat > "$expected" <<EOF
 f1 2000
 f2 1000
+f3 1
 EOF
 
 # Special version for powerpc with function descriptors.
 cat > "$expected_dot" <<EOF
 .f1 2000
 .f2 1000
+.f3 1
 EOF
 
 "$GPROF" -C "$program" "$data" \
diff --git a/gmon/tst-gmon-static-gprof.sh b/gmon/tst-gmon-static-gprof.sh
index 79218df967..4cc99c80d0 100644
--- a/gmon/tst-gmon-static-gprof.sh
+++ b/gmon/tst-gmon-static-gprof.sh
@@ -39,6 +39,7 @@ trap cleanup 0
 cat > "$expected" <<EOF
 f1 2000
 f2 1000
+f3 1
 main 1
 EOF
 
@@ -46,6 +47,7 @@ EOF
 cat > "$expected_dot" <<EOF
 .f1 2000
 .f2 1000
+.f3 1
 .main 1
 EOF
 
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v3] Use __executable_start as the lowest address for profiling [BZ #28153]
  2021-08-05 12:09 [PATCH v3] Use __executable_start as the lowest address for profiling [BZ #28153] H.J. Lu via Libc-alpha
@ 2021-08-06  0:16 ` Fangrui Song via Libc-alpha
  2021-08-06  7:21   ` Fangrui Song via Libc-alpha
  0 siblings, 1 reply; 4+ messages in thread
From: Fangrui Song via Libc-alpha @ 2021-08-06  0:16 UTC (permalink / raw)
  To: H.J. Lu; +Cc: libc-alpha


On 2021-08-05, H.J. Lu via Libc-alpha wrote:
>Glibc assumes that ENTRY_POINT is the lowest address for which we need
>to keep profiling records and BFD linker uses a linker script to place
>the input sections.
>
>Starting from GCC 4.6, the main function is placed in .text.startup
>section and starting from binutils 2.22, BFD linker with
>
>commit add44f8d5c5c05e08b11e033127a744d61c26aee
>Author: Alan Modra <amodra@gmail.com>
>Date:   Thu Nov 25 03:03:02 2010 +0000
>
>            * scripttempl/elf.sc: Group .text.exit, text.startup and .text.hot
>            sections.
>
>places .text.startup section before .text section, which leave the main
>function out of profiling records.
>
>Starting from binutils 2.15, linker provides __executable_start to mark
>the lowest address of the executable.  Use __executable_start as the
>lowest address to keep the main function in profiling records. This fixes
>[BZ #28153].
>
>Tested on Linux/x86-64, Linux/x32 and Linux/i686 as well as with
>build-many-glibcs.py.
>---
> csu/gmon-start.c              | 10 +++++++++-
> gmon/tst-gmon-gprof.sh        |  2 ++
> gmon/tst-gmon-static-gprof.sh |  2 ++
> 3 files changed, 13 insertions(+), 1 deletion(-)

LGTM

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v3] Use __executable_start as the lowest address for profiling [BZ #28153]
  2021-08-06  0:16 ` Fangrui Song via Libc-alpha
@ 2021-08-06  7:21   ` Fangrui Song via Libc-alpha
  0 siblings, 0 replies; 4+ messages in thread
From: Fangrui Song via Libc-alpha @ 2021-08-06  7:21 UTC (permalink / raw)
  To: H.J. Lu; +Cc: libc-alpha

On 2021-08-05, Fangrui Song wrote:
>
>On 2021-08-05, H.J. Lu via Libc-alpha wrote:
>>Glibc assumes that ENTRY_POINT is the lowest address for which we need
>>to keep profiling records and BFD linker uses a linker script to place
>>the input sections.
>>
>>Starting from GCC 4.6, the main function is placed in .text.startup
>>section and starting from binutils 2.22, BFD linker with
>>
>>commit add44f8d5c5c05e08b11e033127a744d61c26aee
>>Author: Alan Modra <amodra@gmail.com>
>>Date:   Thu Nov 25 03:03:02 2010 +0000
>>
>>           * scripttempl/elf.sc: Group .text.exit, text.startup and .text.hot
>>           sections.
>>
>>places .text.startup section before .text section, which leave the main
>>function out of profiling records.
>>
>>Starting from binutils 2.15, linker provides __executable_start to mark
>>the lowest address of the executable.  Use __executable_start as the
>>lowest address to keep the main function in profiling records. This fixes
>>[BZ #28153].
>>
>>Tested on Linux/x86-64, Linux/x32 and Linux/i686 as well as with
>>build-many-glibcs.py.
>>---
>>csu/gmon-start.c              | 10 +++++++++-
>>gmon/tst-gmon-gprof.sh        |  2 ++
>>gmon/tst-gmon-static-gprof.sh |  2 ++
>>3 files changed, 13 insertions(+), 1 deletion(-)
>
>LGTM

[I had not subscribed the list until few days ago]

Comment to v2: gold/ld.lld don't necessarily place .text.unlikely
before other text sections.
GNU ld's fixed section ordering may make certain optimization difficult.

RISC architectures typically create range extension thunks to overcome the
limitation of short range branches. Hot code can usually be moved to the
middle to increase the chance that one range extension thunk can be
shared by more code.

8MiB cold
2MiB hot
8MiB cold

(
.text.sorted.* is a recent addition emulation link-time layout for
improving instruction cache and TLB. An optimized case may require the
sections to be interleaved with others.)

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-08-06  7:21 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-05 12:09 [PATCH v3] Use __executable_start as the lowest address for profiling [BZ #28153] H.J. Lu via Libc-alpha
2021-08-06  0:16 ` Fangrui Song via Libc-alpha
2021-08-06  7:21   ` Fangrui Song via Libc-alpha
  -- strict thread matches above, loose matches on Subject: below --
2021-07-31 15:13 [PATCH v2] Place ENTRY_POINT in .text.unlikely section " H.J. Lu via Libc-alpha
2021-07-31 16:36 ` Florian Weimer via Libc-alpha
2021-07-31 17:06   ` H.J. Lu via Libc-alpha
2021-07-31 17:11     ` Florian Weimer via Libc-alpha
2021-07-31 17:43       ` [PATCH v3] Use __executable_start as the lowest address for profiling " H.J. Lu via Libc-alpha

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).