From: "H.J. Lu" <hjl.tools@gmail.com>
To: Szabolcs Nagy <szabolcs.nagy@arm.com>
Cc: GNU C Library <libc-alpha@sourceware.org>
Subject: Re: [PATCH v3 5/5] csu: Move static pie self relocation later [BZ #27072]
Date: Thu, 14 Jan 2021 07:52:05 -0800 [thread overview]
Message-ID: <CAMe9rOpshWH9k_jRB5W2y=SU2=piRgVC+W_CEqSNmxs307vDeg@mail.gmail.com> (raw)
In-Reply-To: <CAMe9rOptgHK+gs0oBu3zVW8vDAo=i+GxCCfU6rEr8G45JQ98RQ@mail.gmail.com>
On Thu, Jan 14, 2021 at 7:49 AM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> On Tue, Jan 12, 2021 at 2:55 PM H.J. Lu <hjl.tools@gmail.com> wrote:
> >
> > On Tue, Jan 12, 2021 at 9:27 AM Szabolcs Nagy via Libc-alpha
> > <libc-alpha@sourceware.org> wrote:
> > >
> > > IFUNC resolvers may depend on tunables and cpu feature setup so
> > > move static pie self relocation after those.
> > >
> > > It is hard to guarantee that the ealy startup code does not rely
> > > on relocations so this is a bit fragile. It would be more robust
> > > to handle RELATIVE relocs early and only IRELATIVE relocs later,
> > > but the current relocation processing code cannot do that.
> > >
> > > The early startup code before relocation processing includes
> > >
> > > _dl_aux_init (auxvec);
> > > __libc_init_secure ();
> > > __tunables_init (__environ);
> > > ARCH_INIT_CPU_FEATURES ();
> > >
> > > These are simple enough that RELATIVE relocs can be avoided.
> > >
> > > __ehdr_start may require RELATIVE relocation so it was moved
> > > later, fortunately ehdr and phdr are not used in the early code.
> > > ---
> > > csu/libc-start.c | 44 +++++++++++++++++++++++++-------------------
> > > 1 file changed, 25 insertions(+), 19 deletions(-)
> > >
> > > diff --git a/csu/libc-start.c b/csu/libc-start.c
> > > index db859c3bed..fb64cdb2c9 100644
> > > --- a/csu/libc-start.c
> > > +++ b/csu/libc-start.c
> > > @@ -142,8 +142,6 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
> > > int result;
> > >
> > > #ifndef SHARED
> > > - _dl_relocate_static_pie ();
> > > -
> > > char **ev = &argv[argc + 1];
> > >
> > > __environ = ev;
> > > @@ -165,24 +163,7 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
> > > }
> > > # endif
> > > _dl_aux_init (auxvec);
> > > - if (GL(dl_phdr) == NULL)
> > > # endif
> > > - {
> > > - /* Starting from binutils-2.23, the linker will define the
> > > - magic symbol __ehdr_start to point to our own ELF header
> > > - if it is visible in a segment that also includes the phdrs.
> > > - So we can set up _dl_phdr and _dl_phnum even without any
> > > - information from auxv. */
> > > -
> > > - extern const ElfW(Ehdr) __ehdr_start
> > > - __attribute__ ((weak, visibility ("hidden")));
> > > - if (&__ehdr_start != NULL)
> > > - {
> > > - assert (__ehdr_start.e_phentsize == sizeof *GL(dl_phdr));
> > > - GL(dl_phdr) = (const void *) &__ehdr_start + __ehdr_start.e_phoff;
> > > - GL(dl_phnum) = __ehdr_start.e_phnum;
> > > - }
> > > - }
> > >
> > > /* Initialize very early so that tunables can use it. */
> > > __libc_init_secure ();
> > > @@ -191,6 +172,11 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
> > >
> > > ARCH_INIT_CPU_FEATURES ();
> > >
> > > + /* Do static pie self relocation after tunables and cpu features
> > > + are setup for ifunc resolvers. Before this point relocations
> > > + must be avoided. */
> > > + _dl_relocate_static_pie ();
> > > +
> > > /* Perform IREL{,A} relocations. */
> > > ARCH_SETUP_IREL ();
> > >
> > > @@ -202,6 +188,26 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
> > > hwcap and platform fields available in the TCB. */
> > > ARCH_APPLY_IREL ();
> > >
> > > +# ifdef HAVE_AUX_VECTOR
> > > + if (GL(dl_phdr) == NULL)
> > > +# endif
> > > + {
> > > + /* Starting from binutils-2.23, the linker will define the
> > > + magic symbol __ehdr_start to point to our own ELF header
> > > + if it is visible in a segment that also includes the phdrs.
> > > + So we can set up _dl_phdr and _dl_phnum even without any
> > > + information from auxv. */
> > > +
> > > + extern const ElfW(Ehdr) __ehdr_start
> > > + __attribute__ ((weak, visibility ("hidden")));
> > > + if (&__ehdr_start != NULL)
> > > + {
> > > + assert (__ehdr_start.e_phentsize == sizeof *GL(dl_phdr));
> > > + GL(dl_phdr) = (const void *) &__ehdr_start + __ehdr_start.e_phoff;
> > > + GL(dl_phnum) = __ehdr_start.e_phnum;
> > > + }
> > > + }
> > > +
> > > /* Set up the stack checker's canary. */
> > > uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard (_dl_random);
> > > # ifdef THREAD_SET_STACK_GUARD
> > > --
> > > 2.17.1
> > >
> >
> > LGTM.
> >
> > Thanks.
> >
>
> Unfortunately, this failed on i686:
>
> (gdb) r
> Starting program:
> /export/build/gnu/tools-build/glibc-32bit-static-pie-gitlab/build-i686-linux/elf/sln
>
> Program received signal SIGSEGV, Segmentation fault.
> 0xefec0550 in ?? ()
>
> Breakpoint 1, __libc_start_main (main=0xf7f64760 <main>, argc=1,
> argv=0xffffc704, init=0xf7f670e0 <__libc_csu_init>,
> fini=0xf7f67190 <__libc_csu_fini>, rtld_fini=0x0, stack_end=0xffffc6fc)
> at ../csu/libc-start.c:145
> 145 char **ev = &argv[argc + 1];
> (gdb) next
> 147 __environ = ev;
> (gdb)
> 151 __libc_stack_end = stack_end;
> (gdb)
> 160 while (*evp++ != NULL)
> (gdb)
> 165 _dl_aux_init (auxvec);
> (gdb)
> 169 __libc_init_secure ();
> (gdb)
> 171 __tunables_init (__environ);
> (gdb)
> 173 ARCH_INIT_CPU_FEATURES ();
> (gdb)
> 178 _dl_relocate_static_pie ();
> (gdb)
> 181 ARCH_SETUP_IREL ();
> (gdb)
> 184 ARCH_SETUP_TLS ();
> (gdb)
> 203 if (&__ehdr_start != NULL)
> (gdb)
> 212 uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard (_dl_random);
> (gdb)
> 223 DL_SYSDEP_OSCHECK (__libc_fatal);
> (gdb)
>
> Program received signal SIGSEGV, Segmentation fault.
> 0xefec0550 in ?? ()
> (gdb)
>
> --
> H.J.
(gdb) si
uname () at ../sysdeps/unix/syscall-template.S:120
120 T_PSEUDO (SYSCALL_SYMBOL, SYSCALL_NAME, SYSCALL_NARGS)
(gdb) si
0xf7fba3a2 120 T_PSEUDO (SYSCALL_SYMBOL, SYSCALL_NAME, SYSCALL_NARGS)
(gdb) si
0xf7fba3a6 120 T_PSEUDO (SYSCALL_SYMBOL, SYSCALL_NAME, SYSCALL_NARGS)
(gdb) si
0xf7fba3ab 120 T_PSEUDO (SYSCALL_SYMBOL, SYSCALL_NAME, SYSCALL_NARGS)
(gdb) si
0xefec0550 in ?? ()
(gdb) disass uname
Dump of assembler code for function uname:
0xf7fba3a0 <+0>: mov %ebx,%edx
0xf7fba3a2 <+2>: mov 0x4(%esp),%ebx
0xf7fba3a6 <+6>: mov $0x7a,%eax
0xf7fba3ab <+11>: call *%gs:0x10 <<<<<<<<<<<< This may not be setup yet.
0xf7fba3b2 <+18>: mov %edx,%ebx
0xf7fba3b4 <+20>: cmp $0xfffff001,%eax
0xf7fba3b9 <+25>: jae 0xf7f9efd0 <__syscall_error>
0xf7fba3bf <+31>: ret
End of assembler dump.
(gdb)
--
H.J.
next prev parent reply other threads:[~2021-01-14 15:52 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-01-12 17:21 [PATCH v3 0/5] fix ifunc with static pie " Szabolcs Nagy
2021-01-12 17:21 ` [PATCH v3 1/5] configure: Require PI_STATIC_AND_HIDDEN for static pie Szabolcs Nagy
2021-01-12 18:38 ` Adhemerval Zanella
2021-01-12 17:22 ` [PATCH v3 2/5] Make libc symbols hidden in static PIE Szabolcs Nagy
2021-01-12 23:09 ` H.J. Lu
2021-01-13 0:02 ` H.J. Lu
2021-01-13 0:33 ` H.J. Lu
2021-01-13 1:19 ` H.J. Lu
2021-01-13 9:50 ` Szabolcs Nagy
2021-01-14 11:17 ` Szabolcs Nagy
2021-01-14 15:39 ` H.J. Lu
2021-01-15 3:36 ` H.J. Lu
2021-01-15 4:29 ` H.J. Lu
2021-01-15 11:25 ` Szabolcs Nagy
2021-01-15 13:43 ` H.J. Lu
2021-01-15 14:27 ` Szabolcs Nagy
2021-01-15 15:28 ` H.J. Lu
2021-01-15 22:42 ` H.J. Lu
2021-01-16 0:41 ` H.J. Lu
2021-01-16 13:18 ` H.J. Lu
2021-01-18 16:22 ` Szabolcs Nagy
2021-01-12 17:22 ` [PATCH v3 3/5] elf: Make the tunable struct definition internal only Szabolcs Nagy
2021-01-13 17:38 ` Adhemerval Zanella
2021-01-12 17:22 ` [PATCH v3 4/5] elf: Avoid RELATIVE relocs in __tunables_init Szabolcs Nagy
2021-01-13 17:42 ` Adhemerval Zanella
2021-01-12 17:23 ` [PATCH v3 5/5] csu: Move static pie self relocation later [BZ #27072] Szabolcs Nagy
2021-01-12 22:55 ` H.J. Lu
2021-01-14 15:49 ` H.J. Lu
2021-01-14 15:52 ` H.J. Lu [this message]
2021-01-14 16:01 ` H.J. Lu
2021-01-14 16:26 ` H.J. Lu
2021-01-14 17:19 ` Szabolcs Nagy
2021-01-14 17:59 ` Szabolcs Nagy
2021-01-14 17:05 ` Szabolcs Nagy
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAMe9rOpshWH9k_jRB5W2y=SU2=piRgVC+W_CEqSNmxs307vDeg@mail.gmail.com' \
--to=hjl.tools@gmail.com \
--cc=libc-alpha@sourceware.org \
--cc=szabolcs.nagy@arm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).