]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
hurd: Make it possible to call memcpy very early
authorSergey Bugaev <bugaevc@gmail.com>
Sat, 29 Apr 2023 20:18:21 +0000 (23:18 +0300)
committerSamuel Thibault <samuel.thibault@ens-lyon.org>
Sun, 30 Apr 2023 23:21:23 +0000 (01:21 +0200)
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>

sysdeps/mach/hurd/x86_64/static-start.S

index 982d3d52e4568360eb73b469331fe85e622e7fa9..cc8e2410ea12350e0b70b4021ea1f1a34902408f 100644 (file)
@@ -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