From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x131.google.com (mail-lf1-x131.google.com [IPv6:2a00:1450:4864:20::131]) by sourceware.org (Postfix) with ESMTPS id A5007385843A for ; Sat, 29 Apr 2023 20:18:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A5007385843A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-lf1-x131.google.com with SMTP id 2adb3069b0e04-4ec9c7c6986so1465291e87.0 for ; Sat, 29 Apr 2023 13:18:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1682799510; x=1685391510; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=H2591BAA3jw7pAecNpT5f05AJDJoNxD4ZezWLXPmZ4Y=; b=S+FHR/GHAtb2vlh2Hxh00wE6pog6wRo2Hi22kVa8LAdZX/ExJGV3uqnyx7AX3jbg6h Nou9yJPxO0+UyJ073Kx4B20Cqx0nRVD20PiTaCxGBJ5PLegLKQ1yfo6NLwp3D0m+a+yt WhV7dRU6/xqQRjR+uJZX5hDf1JspkRCZBZSc+rrhtlBq8fCLysSajT3nooe0HRJOgvkw gDZk/QUhzXbAHDDWzrnwp1LM7/8SGnP/BTFTZMwxAPtLoWukz91Ll47ec6uSkQe5CuN9 7i6NxZ1efuUTbu+zwQvsUWSffbZOuyMKFyeRlVtJNSH57H8faiTuZftvuvmNuj6UCD41 tNKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682799510; x=1685391510; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=H2591BAA3jw7pAecNpT5f05AJDJoNxD4ZezWLXPmZ4Y=; b=cWJCHX85YCEfEQ1sKrghQObQGGiSJfjc47dlvi335Syaj4hcHoav7Zuax6Nrrj/iyq TrsJpRmQglzYxKE9l8qnXexoTMk4MVj9DNtw5bwF7Da9v8bKHEbESh4EgjeV4ao5ECvV UVn6u56TOhAbAux+lhB9He8PrEhLoPzYFxKlSIfk+gCG5ESmFoSFY2GiQ1TH/I6yGb8U osx3LUy2KCODTX+hfCbht0tHJ14g20cazV75ucT37ZDSFoyjp/003qx9iCygu3u1yqRc gPfHMWCY8cHkgBRUeW6c/ADf/OyA2xoxfGfJwWHuIUGFx8lydFKfcK92yj9QyYB23RXs 3Ehw== X-Gm-Message-State: AC+VfDxGsmhJp0ej1UYWwhUwjXCqPBpBiMzKApJUd590JahB93WBEoaK 7gcl6AtDofZQ13O6iHmb6CEzy0JUk6pHtg== X-Google-Smtp-Source: ACHHUZ5PRmkPCatibNycXh/zUBDWr3PiSYdcDn9zlL6CfaBR2FwVSuEY1xehXxnY6so3zJS+dyRsfA== X-Received: by 2002:a19:ad4d:0:b0:4ea:fa26:2378 with SMTP id s13-20020a19ad4d000000b004eafa262378mr2331646lfd.23.1682799509804; Sat, 29 Apr 2023 13:18:29 -0700 (PDT) Received: from surface-pro-6.. ([2a00:1370:818c:4a57:a7ac:6f45:1787:abcc]) by smtp.gmail.com with ESMTPSA id h25-20020ac250d9000000b004eb09820adbsm3905451lfm.105.2023.04.29.13.18.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 29 Apr 2023 13:18:29 -0700 (PDT) From: Sergey Bugaev To: libc-alpha@sourceware.org Cc: bug-hurd@gnu.org, Samuel Thibault Subject: [RFC PATCH v3 5/6] hurd: Make it possible to call memcpy very early Date: Sat, 29 Apr 2023 23:18:21 +0300 Message-Id: <20230429201822.2605207-6-bugaevc@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230429201822.2605207-1-bugaevc@gmail.com> References: <20230429201822.2605207-1-bugaevc@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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 --- Changes since v1: - drop the stpncpy, since it's apparently not required during early startup; - as a result of the above, there are no longer any changes to the i386 version; - drop the PIC/non-PIC split, we can always use %rip-relative addressing on x86_64; - as mentioned somewhere in the v1 thread, I have, since posting the v1, actually gone and checked that the relocations do work and the proper, more effecient memcpy version does get installed into the GOT slot and invoked whenever anything calls memcpy; - convinced myself that this is not a terrible hack but rather an OK solution; - worked out how this would be done on an architecture that (like i386, unlike x86_64) does need the original value in the GOT to perform the relocation, but (unlike i386, like x86_64) still uses an ifunc-selected memcpy in static builds: namely, we'd simply put the original ifunc address back into the GOT slot a few lines below, after the call to _hurd_stack_setup. 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 982d3d52..cc8e2410 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 -- 2.40.1