public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
* [glibc] hurd: Make it possible to call memcpy very early
@ 2023-04-30 23:21 Samuel Thibault
  0 siblings, 0 replies; only message in thread
From: Samuel Thibault @ 2023-04-30 23:21 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=2bc516020ff8642d1352e99f0f25fef002457079

commit 2bc516020ff8642d1352e99f0f25fef002457079
Author: Sergey Bugaev <bugaevc@gmail.com>
Date:   Sat Apr 29 23:18:21 2023 +0300

    hurd: Make it possible to call memcpy very early
    
    Normally, in static builds, the first code that runs is _start, in e.g.
    sysdeps/x86_64/start.S, which quickly calls __libc_start_main, passing
    it the argv etc. Among the first things __libc_start_main does is
    initializing the tunables (based on env), then CPU features, and then
    calls _dl_relocate_static_pie (). Specifically, this runs ifunc
    resolvers to pick, based on the CPU features discovered earlier, the
    most suitable implementation of "string" functions such as memcpy.
    
    Before that point, calling memcpy (or other ifunc-resolved functions)
    will not work.
    
    In the Hurd port, things are more complex. In order to get argv/env for
    our process, glibc normally needs to do an RPC to the exec server,
    unless our args/env are already located on the stack (which is what
    happens to bootstrap processes spawned by GNU Mach). Fetching our
    argv/env from the exec server has to be done before the call to
    __libc_start_main, since we need to know what our argv/env are to pass
    them to __libc_start_main.
    
    On the other hand, the implementation of the RPC (and other initial
    setup needed on the Hurd before __libc_start_main can be run) is not
    very trivial. In particular, it may (and on x86_64, will) use memcpy.
    But as described above, calling memcpy before __libc_start_main can not
    work, since the GOT entry for it is not yet initialized at that point.
    
    Work around this by pre-filling the GOT entry with the baseline version
    of memcpy, __memcpy_sse2_unaligned. This makes it possible for early
    calls to memcpy to just work. The initial value of the GOT entry is
    unused on x86_64, and changing it won't interfere with the relocation
    being performed later: once _dl_relocate_static_pie () is called, the
    baseline version will get replaced with the most suitable one, and that
    is what subsequent calls of memcpy are going to call.
    
    Checked on x86_64-gnu.
    
    Signed-off-by: Sergey Bugaev <bugaevc@gmail.com>
    Message-Id: <20230429201822.2605207-6-bugaevc@gmail.com>

Diff:
---
 sysdeps/mach/hurd/x86_64/static-start.S | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/sysdeps/mach/hurd/x86_64/static-start.S b/sysdeps/mach/hurd/x86_64/static-start.S
index 982d3d52e4..cc8e2410ea 100644
--- a/sysdeps/mach/hurd/x86_64/static-start.S
+++ b/sysdeps/mach/hurd/x86_64/static-start.S
@@ -19,6 +19,9 @@
 	.text
 	.globl _start
 _start:
+
+	leaq __memcpy_sse2_unaligned(%rip), %rax
+	movq %rax, memcpy@GOTPCREL(%rip)
 	call _hurd_stack_setup
 	xorq %rdx, %rdx
 	jmp _start1

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-04-30 23:21 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-30 23:21 [glibc] hurd: Make it possible to call memcpy very early Samuel Thibault

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).