From nobody Wed May 1 23:49:40 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1624074320725260.8401137518824; Fri, 18 Jun 2021 20:45:20 -0700 (PDT) Received: from localhost ([::1]:54496 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1luRv5-0002jz-Jk for importer2@patchew.org; Fri, 18 Jun 2021 23:45:19 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33010) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1luRtS-000083-K3 for qemu-devel@nongnu.org; Fri, 18 Jun 2021 23:43:38 -0400 Received: from mail-pf1-x432.google.com ([2607:f8b0:4864:20::432]:43770) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1luRtN-00080q-D9 for qemu-devel@nongnu.org; Fri, 18 Jun 2021 23:43:38 -0400 Received: by mail-pf1-x432.google.com with SMTP id a127so9195616pfa.10 for ; Fri, 18 Jun 2021 20:43:32 -0700 (PDT) Received: from localhost.localdomain ([71.212.149.176]) by smtp.gmail.com with ESMTPSA id z6sm10045154pgs.24.2021.06.18.20.43.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Jun 2021 20:43:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NFyiLCGJg5Xo3SqM6ckKPEkOJUNRw/fc9LhzEEFhQ7g=; b=GnTPI02Ze0kaGpPWSFxBVUpYR5KTg6tCO08UEaNwHuxSxSBd2ORwCixEnFr4RC1fOJ DH+uG9APHkyEOxuP59vG6Z+U+au1XCVfAwu8er0OtrillEW4VvzDYWlt0DTZV8DVO/1h z9wNCdprtuXs8DUZYnj9Z8B8bjVgaQyIEkVKjlx5sj9/93FNtCddjzJ5n3Qfgez4b1u8 KxtDzvlr+zHaz5/kODHC0wSSpSpvM3UXEsJXAjF/XLr40JdP3RULwPKdNQEPdMFyfuT3 VUnopY5PFTzTOlj9gKVueWWfzd1gdnw0PjA7ufTeLgaT59rCEtHgHPI31/tnWqV3Ufog QNNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=NFyiLCGJg5Xo3SqM6ckKPEkOJUNRw/fc9LhzEEFhQ7g=; b=QSQneVyCdcr0kIUjc0Eluia+cx542B2b6DnOmX12mlaMbvxsFQ3oCuuuKBOLe+FsJn fzTCt8oXtYN8zAeMyVKGOLJhfZgUzKrNQkXS6TMMHtdibA9w0zSKB1mNDj37jh7aowZ6 icYKgn4x3jPdYMj65oDz+DeppP9SUh4DBx3RFJIqQtYx4hmv8xh3lC6cd3O+zLPv1y+3 1zVBXgvNYoSQ+/KDJ70/iIHc88HOafojp7Xiy14ljup2+jm1HpdPqA0eXiX6bFGxbmB9 jeRkGnPmdR0oa7Lwns/0UnFCbkKQSL8BZO3NYwO54kY+4RYUdrGoepT63hUgSsoawE9P mrCA== X-Gm-Message-State: AOAM533cCDjG6JLfs4TArQjbCyi8e5okDbhkJA2pUXCbidvjQp/h/1C7 Kl/X2GkErtQI8ypOkHJI9FYcn30kFX006g== X-Google-Smtp-Source: ABdhPJyREZWJNhVQp+g4+hmCGOlMI2bKurW3sc247ztpi6ygPGoRJ3RAJ6bmd9IQ7jRl8CNKgPsiKQ== X-Received: by 2002:a63:4c5e:: with SMTP id m30mr13000355pgl.153.1624074211389; Fri, 18 Jun 2021 20:43:31 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH 01/12] linux-user: Fix style problems in linuxload.c Date: Fri, 18 Jun 2021 20:43:18 -0700 Message-Id: <20210619034329.532318-2-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210619034329.532318-1-richard.henderson@linaro.org> References: <20210619034329.532318-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::432; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x432.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: alex.bennee@linaro.org, laurent@vivier.eu Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Signed-off-by: Richard Henderson Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- linux-user/linuxload.c | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/linux-user/linuxload.c b/linux-user/linuxload.c index a27e1d0d8b..9d4eb5e94b 100644 --- a/linux-user/linuxload.c +++ b/linux-user/linuxload.c @@ -1,59 +1,57 @@ /* Code for loading Linux executables. Mostly linux kernel code. */ =20 #include "qemu/osdep.h" - #include "qemu.h" =20 #define NGROUPS 32 =20 /* ??? This should really be somewhere else. */ -abi_long memcpy_to_target(abi_ulong dest, const void *src, - unsigned long len) +abi_long memcpy_to_target(abi_ulong dest, const void *src, unsigned long l= en) { void *host_ptr; =20 host_ptr =3D lock_user(VERIFY_WRITE, dest, len, 0); - if (!host_ptr) + if (!host_ptr) { return -TARGET_EFAULT; + } memcpy(host_ptr, src, len); unlock_user(host_ptr, dest, 1); return 0; } =20 -static int count(char ** vec) +static int count(char **vec) { - int i; + int i; =20 - for(i =3D 0; *vec; i++) { + for (i =3D 0; *vec; i++) { vec++; } - - return(i); + return i; } =20 static int prepare_binprm(struct linux_binprm *bprm) { - struct stat st; + struct stat st; int mode; int retval; =20 - if(fstat(bprm->fd, &st) < 0) { - return(-errno); + if (fstat(bprm->fd, &st) < 0) { + return -errno; } =20 mode =3D st.st_mode; - if(!S_ISREG(mode)) { /* Must be regular file */ - return(-EACCES); + if (!S_ISREG(mode)) { /* Must be regular file */ + return -EACCES; } - if(!(mode & 0111)) { /* Must have at least one execute bit set */ - return(-EACCES); + if (!(mode & 0111)) { /* Must have at least one execute bit set */ + return -EACCES; } =20 bprm->e_uid =3D geteuid(); bprm->e_gid =3D getegid(); =20 /* Set-uid? */ - if(mode & S_ISUID) { + if (mode & S_ISUID) { bprm->e_uid =3D st.st_uid; } =20 @@ -125,8 +123,8 @@ abi_ulong loader_build_argptr(int envc, int argc, abi_u= long sp, } =20 int loader_exec(int fdexec, const char *filename, char **argv, char **envp, - struct target_pt_regs * regs, struct image_info *infop, - struct linux_binprm *bprm) + struct target_pt_regs *regs, struct image_info *infop, + struct linux_binprm *bprm) { int retval; =20 @@ -139,7 +137,7 @@ int loader_exec(int fdexec, const char *filename, char = **argv, char **envp, =20 retval =3D prepare_binprm(bprm); =20 - if(retval>=3D0) { + if (retval >=3D 0) { if (bprm->buf[0] =3D=3D 0x7f && bprm->buf[1] =3D=3D 'E' && bprm->buf[2] =3D=3D 'L' @@ -157,11 +155,11 @@ int loader_exec(int fdexec, const char *filename, cha= r **argv, char **envp, } } =20 - if(retval>=3D0) { + if (retval >=3D 0) { /* success. Initialize important registers */ do_init_thread(regs, infop); return retval; } =20 - return(retval); + return retval; } --=20 2.25.1 From nobody Wed May 1 23:49:40 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1624074544019749.0611969879429; Fri, 18 Jun 2021 20:49:04 -0700 (PDT) Received: from localhost ([::1]:40424 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1luRyg-0003ia-W9 for importer2@patchew.org; Fri, 18 Jun 2021 23:49:03 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33074) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1luRtX-00008y-4T for qemu-devel@nongnu.org; Fri, 18 Jun 2021 23:43:44 -0400 Received: from mail-pg1-x536.google.com ([2607:f8b0:4864:20::536]:35551) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1luRtN-00081H-FJ for qemu-devel@nongnu.org; Fri, 18 Jun 2021 23:43:42 -0400 Received: by mail-pg1-x536.google.com with SMTP id v7so9460941pgl.2 for ; Fri, 18 Jun 2021 20:43:33 -0700 (PDT) Received: from localhost.localdomain ([71.212.149.176]) by smtp.gmail.com with ESMTPSA id z6sm10045154pgs.24.2021.06.18.20.43.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Jun 2021 20:43:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=+OXX5KjIYz/ZS9gLgWEfQeUt72TgJXhxPXE04khIgeU=; b=S/GO4BjOsoDKWuxkTN3vkKjQvtSHxdJU+cp65gGSYdH8lpEeAV5c7cnIL1WjBP4wdx 5nsJCXf8LFwN+xhblY2eeqDKUp0awxxIUOL3Nl8QFq0E5kUC7CUbWdbOmx3+r6zTjnmq xE7WG1Meb/uLClwRX5aQElP/TXMLeq3L/KtcyF+aMNcIZmRcGjmkFah5ybjrS5bzKgSn pLzUvvrbF7XszymWpihKOsGvH3tPeTqgrffuvJc8xP+qi5/QqYqicUk+CxRj8QVX8IbD dEG1dng/6O9bp/MSh5ML4bL8mFOoQsuZyojezQdT2PfHdQK2QUVTVTgzQgci0Q3PBfuC k2Sw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=+OXX5KjIYz/ZS9gLgWEfQeUt72TgJXhxPXE04khIgeU=; b=rHEJYOJDSM4QN514MkYLlvI49VA5XK6KDVvb7ig8PcQSGN1txmfKM02HigRkbohuUJ FNmPnyh5xqIRZ5JWjNOpvjbI1mMZUWl+INIavBuoxVePfztLr7gpJMFiVYSfVdapTxy3 hMUjc22fg2ZTnmxL/hDydNoUmxCYrNTesesuWN9irGwjiiCAfuEwwRqRwG3cs9FNO3dM FGiipcZph4HfbSNfMsuzHLHVIaJJMjQnob4CacX8iP+jfnsV45NFgjtLbbe3HIjFeO4r iHl+1VbvTTy+fnFb9MbUpzNNFWVtBkCp7d7HiufiMCxOvh6a5sbWz+liHL84wTph+h/Q NR3g== X-Gm-Message-State: AOAM531IJoxFI50f8jJma79HQN0+Rx7XYz3wqTBQZhAXaQVPO+6NEMI/ Tu26PuYSPHQ5L/Wy8m2ee3LrHqXWSuF+6A== X-Google-Smtp-Source: ABdhPJx3sXRu4H/APChTtefwLlKrHhJShhefIU2mAB8InZ9n/n9kSidrdXMQARjBg12zvtQdEP3pGQ== X-Received: by 2002:aa7:962f:0:b029:2ed:cf:1f90 with SMTP id r15-20020aa7962f0000b02902ed00cf1f90mr8006496pfg.76.1624074211934; Fri, 18 Jun 2021 20:43:31 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH 02/12] linux-user: Introduce imgsrc_read, imgsrc_read_alloc Date: Fri, 18 Jun 2021 20:43:19 -0700 Message-Id: <20210619034329.532318-3-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210619034329.532318-1-richard.henderson@linaro.org> References: <20210619034329.532318-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::536; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x536.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: alex.bennee@linaro.org, laurent@vivier.eu Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Introduced and initialized, but not yet really used. These will tidy the current tests vs BPRM_BUF_SIZE. Signed-off-by: Richard Henderson --- linux-user/qemu.h | 50 ++++++++++++++++++++++++++++++++++-------- linux-user/linuxload.c | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 9 deletions(-) diff --git a/linux-user/qemu.h b/linux-user/qemu.h index 9e5e2aa499..f4cdfb16b3 100644 --- a/linux-user/qemu.h +++ b/linux-user/qemu.h @@ -168,6 +168,37 @@ extern unsigned long mmap_min_addr; =20 /* ??? See if we can avoid exposing so much of the loader internals. */ =20 +typedef struct { + const void *cache; + unsigned int cache_size; + int fd; +} ImageSource; + +/** + * imgsrc_read: Read from ImageSource + * @dst: destination for read + * @offset: offset within file for read + * @len: size of the read + * @img: ImageSource to read from + * @errp: Error details. + * + * Read into @dst, using the cache when possible. + */ +bool imgsrc_read(void *dst, off_t offset, size_t len, + const ImageSource *img, Error **errp); + +/** + * imgsrc_read_alloc: Read from ImageSource + * @offset: offset within file for read + * @size: size of the read + * @img: ImageSource to read from + * @errp: Error details. + * + * Read into newly allocated memory, using the cache when possible. + */ +void *imgsrc_read_alloc(off_t offset, size_t len, + const ImageSource *img, Error **errp); + /* Read a good amount of data initially, to hopefully get all the program headers loaded. */ #define BPRM_BUF_SIZE 1024 @@ -177,15 +208,16 @@ extern unsigned long mmap_min_addr; * used when loading binaries. */ struct linux_binprm { - char buf[BPRM_BUF_SIZE] __attribute__((aligned)); - abi_ulong p; - int fd; - int e_uid, e_gid; - int argc, envc; - char **argv; - char **envp; - char * filename; /* Name of binary */ - int (*core_dump)(int, const CPUArchState *); /* coredump routine */ + char buf[BPRM_BUF_SIZE] __attribute__((aligned)); + ImageSource src; + abi_ulong p; + int fd; + int e_uid, e_gid; + int argc, envc; + char **argv; + char **envp; + char *filename; /* Name of binary */ + int (*core_dump)(int, const CPUArchState *); /* coredump routine */ }; =20 typedef struct IOCTLEntry IOCTLEntry; diff --git a/linux-user/linuxload.c b/linux-user/linuxload.c index 9d4eb5e94b..3b0bafc490 100644 --- a/linux-user/linuxload.c +++ b/linux-user/linuxload.c @@ -2,6 +2,7 @@ =20 #include "qemu/osdep.h" #include "qemu.h" +#include "qapi/error.h" =20 #define NGROUPS 32 =20 @@ -74,6 +75,10 @@ static int prepare_binprm(struct linux_binprm *bprm) /* Make sure the rest of the loader won't read garbage. */ memset(bprm->buf + retval, 0, BPRM_BUF_SIZE - retval); } + + bprm->src.cache =3D bprm->buf; + bprm->src.cache_size =3D retval; + return retval; } =20 @@ -129,6 +134,7 @@ int loader_exec(int fdexec, const char *filename, char = **argv, char **envp, int retval; =20 bprm->fd =3D fdexec; + bprm->src.fd =3D fdexec; bprm->filename =3D (char *)filename; bprm->argc =3D count(argv); bprm->argv =3D argv; @@ -163,3 +169,43 @@ int loader_exec(int fdexec, const char *filename, char= **argv, char **envp, =20 return retval; } + +bool imgsrc_read(void *dst, off_t offset, size_t len, + const ImageSource *img, Error **errp) +{ + ssize_t ret; + + if (offset + len <=3D img->cache_size) { + memcpy(dst, img->cache + offset, len); + return true; + } + + if (img->fd < 0) { + error_setg(errp, "read past end of buffer"); + return false; + } + + ret =3D pread(img->fd, dst, len, offset); + if (ret =3D=3D len) { + return true; + } + if (ret < 0) { + error_setg_errno(errp, errno, "Error reading file header"); + } else { + error_setg(errp, "Incomplete read of file header"); + } + return false; +} + +void *imgsrc_read_alloc(off_t offset, size_t len, + const ImageSource *img, Error **errp) +{ + void *alloc =3D g_malloc(len); + bool ok =3D imgsrc_read(alloc, offset, len, img, errp); + + if (!ok) { + g_free(alloc); + alloc =3D NULL; + } + return alloc; +} --=20 2.25.1 From nobody Wed May 1 23:49:40 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1624074450454780.887188045435; Fri, 18 Jun 2021 20:47:30 -0700 (PDT) Received: from localhost ([::1]:34038 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1luRxB-0007wm-Ed for importer2@patchew.org; Fri, 18 Jun 2021 23:47:29 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33066) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1luRtW-00008S-J8 for qemu-devel@nongnu.org; Fri, 18 Jun 2021 23:43:42 -0400 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]:44608) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1luRtN-00081L-QD for qemu-devel@nongnu.org; Fri, 18 Jun 2021 23:43:42 -0400 Received: by mail-pl1-x62c.google.com with SMTP id x22so4133218pll.11 for ; Fri, 18 Jun 2021 20:43:33 -0700 (PDT) Received: from localhost.localdomain ([71.212.149.176]) by smtp.gmail.com with ESMTPSA id z6sm10045154pgs.24.2021.06.18.20.43.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Jun 2021 20:43:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=6XhZDg9Govvz9gc3VGnWdnVC8clvh9XH69vOvHyBLl0=; b=B7DgDyNsPlzK/y/kHNSUjiAzoQ+BvbREj7iI3LOJH8DD5nkMFIa3ehRtPeedKUMFjH Iigd7J7UxHGzekuObvChgosi/3s7KeLKnPYmpP+F1+SlnIbIe+USHiKhGvN2nh4qTnHH 0fa8kNUf5r3Ib3QjcYj8w3FPmLsuL1zRZhwQLXibnXRDkvc8p1DUfZGKiq0xhqTZ7BDV UjlXAlBTF657xmCpupqVrnzuW1Gl/Ai4aM+coDhwuIY0aT60S9eRtbyQ/DcDc/aUIoD5 nBWe0NJ7G5onuyx36v6v57MVme+2dRzbQilWaLHpggWxhRzmJGvvgAB6I+KAP3Wq+02U US1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=6XhZDg9Govvz9gc3VGnWdnVC8clvh9XH69vOvHyBLl0=; b=enxD4EweCsRrlzL4U/nzG/vB63CaBoY0n10C2rT6Rq8unowXNvl3uuW+u9kTCl+bGC Y51YE9BiXnhczCrdfJkfyfdo1AbLMDQOOdABdmdBP9SdVG1NklBvNsi7fXJ26m0EkEAX kelKT7Am4FK1KXIbmR0ANmfsIAv3kOCet8N0OKyb0DO1QB1XueoygMp/Ot4KQjsDKMdp pqlxtvGmNkJn6k8YhqZwuVEODMS5A684Yg1fxAiPX5YHAs5AVaWGx09VIoke8UH8Igvr AXMJAJ8802JFIZzd2sMklEySuKHM3u5AnZIXRTopsPBjYCov3LkxdpXwESgU2jAOqPrZ tvBQ== X-Gm-Message-State: AOAM530pex/2BnZM5w+h44VgW9YMLAjgugzJBmM1R2qv3SH/u8tcmXRt kCNbXu8/EWLSIUyBH+u9HrevpwA8SmhHcA== X-Google-Smtp-Source: ABdhPJw5gDlKAm5dULQ843UV5fA6SudcFsr7BiNXQMlasLu/RgSXJrc+7jfMYV7sh2qh9tbSt99eVQ== X-Received: by 2002:a17:90a:31c4:: with SMTP id j4mr25823220pjf.105.1624074212535; Fri, 18 Jun 2021 20:43:32 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH 03/12] linux-user: Tidy loader_exec Date: Fri, 18 Jun 2021 20:43:20 -0700 Message-Id: <20210619034329.532318-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210619034329.532318-1-richard.henderson@linaro.org> References: <20210619034329.532318-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::62c; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x62c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: alex.bennee@linaro.org, laurent@vivier.eu Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Reorg the if cases to reduce indentation. Test for 4 bytes in the file before checking the signatures. Signed-off-by: Richard Henderson --- linux-user/linuxload.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/linux-user/linuxload.c b/linux-user/linuxload.c index 3b0bafc490..8b93b9704c 100644 --- a/linux-user/linuxload.c +++ b/linux-user/linuxload.c @@ -143,31 +143,31 @@ int loader_exec(int fdexec, const char *filename, cha= r **argv, char **envp, =20 retval =3D prepare_binprm(bprm); =20 - if (retval >=3D 0) { - if (bprm->buf[0] =3D=3D 0x7f - && bprm->buf[1] =3D=3D 'E' - && bprm->buf[2] =3D=3D 'L' - && bprm->buf[3] =3D=3D 'F') { - retval =3D load_elf_binary(bprm, infop); -#if defined(TARGET_HAS_BFLT) - } else if (bprm->buf[0] =3D=3D 'b' - && bprm->buf[1] =3D=3D 'F' - && bprm->buf[2] =3D=3D 'L' - && bprm->buf[3] =3D=3D 'T') { - retval =3D load_flt_binary(bprm, infop); -#endif - } else { - return -ENOEXEC; - } + if (retval < 4) { + return -ENOEXEC; } - - if (retval >=3D 0) { - /* success. Initialize important registers */ - do_init_thread(regs, infop); + if (bprm->buf[0] =3D=3D 0x7f + && bprm->buf[1] =3D=3D 'E' + && bprm->buf[2] =3D=3D 'L' + && bprm->buf[3] =3D=3D 'F') { + retval =3D load_elf_binary(bprm, infop); +#if defined(TARGET_HAS_BFLT) + } else if (bprm->buf[0] =3D=3D 'b' + && bprm->buf[1] =3D=3D 'F' + && bprm->buf[2] =3D=3D 'L' + && bprm->buf[3] =3D=3D 'T') { + retval =3D load_flt_binary(bprm, infop); +#endif + } else { + return -ENOEXEC; + } + if (retval < 0) { return retval; } =20 - return retval; + /* Success. Initialize important registers. */ + do_init_thread(regs, infop); + return 0; } =20 bool imgsrc_read(void *dst, off_t offset, size_t len, --=20 2.25.1 From nobody Wed May 1 23:49:40 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1624074324171286.24556468497747; Fri, 18 Jun 2021 20:45:24 -0700 (PDT) Received: from localhost ([::1]:54882 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1luRv9-0002zQ-5S for importer2@patchew.org; Fri, 18 Jun 2021 23:45:23 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33058) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1luRtV-00008J-UW for qemu-devel@nongnu.org; Fri, 18 Jun 2021 23:43:42 -0400 Received: from mail-pl1-x630.google.com ([2607:f8b0:4864:20::630]:33643) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1luRtP-00081j-10 for qemu-devel@nongnu.org; Fri, 18 Jun 2021 23:43:41 -0400 Received: by mail-pl1-x630.google.com with SMTP id f10so3546124plg.0 for ; Fri, 18 Jun 2021 20:43:34 -0700 (PDT) Received: from localhost.localdomain ([71.212.149.176]) by smtp.gmail.com with ESMTPSA id z6sm10045154pgs.24.2021.06.18.20.43.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Jun 2021 20:43:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=dtXXZ8iOGwpaQfUOm6DW075wk6Tcr8F02y3NmtzbotE=; b=oma6t+J6HZwI1G5DffLzF9PdZihCwD5yizlAwk/JNOeRv87FMG5v7B0Z2dDbC9HE89 Z6xSQ7iqyjumfDXLJXzPcja39w00px12wHyMDdPSBZhDLv4/dHRQoKL8UCsHma1x+PWe fuHGI34Q7+flia9cmetY6Srz5n8cM78tuSECthmNFBgzX84vya9Bwzkwq0UCLsT1XEst WI+q0u32TD4lkOWVkwFQ9RHH55TOl1zpYqQpoNnHjMCHN9gqij31Od2Pi37OlJb/YRk4 40qs51WYJrtDIwHqZjM+ZMPomVzhRJRQnKVUa0dR/JXedD6BYYnFCCVDOq8z8XBEtz6/ ChQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=dtXXZ8iOGwpaQfUOm6DW075wk6Tcr8F02y3NmtzbotE=; b=ZxTnEh6NYxlyajyhfS7vttjsGcSu1MIe0FaNG5K68UwWBs2ONs0Jmr59bSdU5NRinZ SXw3YLZVuKMwvhpOL7q1H8ezKu4WAB6YfdXGsCEBrmImpE201salactNEcD14e9XUydm soj1n9zX3CkjHUGGWWoZSPu41KqcgAH8gQ5Rf5NM9EP3eJwbKFj5SSw6Kzj8yqtIFHdp NeRQqj+OBeiZHHaTa1whNr8BlNggxbw+noSWsWpXviuMrS3bv7fwBfav1hn5JmuzO609 0fSr4elzCqLBrGUPFGkvCt8bhLxaecg2k+H2SZywXL2U52F7pFkTY2+/jUgEV87HFIMU s7Eg== X-Gm-Message-State: AOAM532YThW1H56D94Jm4MytKYld5KDkwyrBH6IAv9hzbBzysmsW48gZ hhUALpvUVT2gSDYXonb+xWNxi1y02iK25A== X-Google-Smtp-Source: ABdhPJy68PQvmsUop4drCfpO/8YVJRXp15+svNguey1sqydTgj2SxHbTcpF1X8IjjWQ8HRGSy41Rgw== X-Received: by 2002:a17:90b:393:: with SMTP id ga19mr14289824pjb.182.1624074213153; Fri, 18 Jun 2021 20:43:33 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH 04/12] linux-user: Do not clobber bprm_buf swapping ehdr Date: Fri, 18 Jun 2021 20:43:21 -0700 Message-Id: <20210619034329.532318-5-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210619034329.532318-1-richard.henderson@linaro.org> References: <20210619034329.532318-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::630; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x630.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: alex.bennee@linaro.org, laurent@vivier.eu Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Rearrange the allocation of storage for ehdr between load_elf_image and load_elf_binary. The same set of copies are done, but we don't modify bprm_buf, which will be important later. Signed-off-by: Richard Henderson Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- linux-user/elfload.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index c0236a0b09..1f9a69703a 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2622,16 +2622,17 @@ static bool parse_elf_properties(int image_fd, On return: INFO values will be filled in, as necessary or available. */ =20 static void load_elf_image(const char *image_name, int image_fd, - struct image_info *info, char **pinterp_name, + struct image_info *info, struct elfhdr *ehdr, + char **pinterp_name, char bprm_buf[BPRM_BUF_SIZE]) { - struct elfhdr *ehdr =3D (struct elfhdr *)bprm_buf; struct elf_phdr *phdr; abi_ulong load_addr, load_bias, loaddr, hiaddr, error; int i, retval, prot_exec; Error *err =3D NULL; =20 /* First of all, some simple consistency checks */ + memcpy(ehdr, bprm_buf, sizeof(*ehdr)); if (!elf_check_ident(ehdr)) { error_setg(&err, "Invalid ELF image for this architecture"); goto exit_errmsg; @@ -2944,6 +2945,7 @@ static void load_elf_image(const char *image_name, in= t image_fd, static void load_elf_interp(const char *filename, struct image_info *info, char bprm_buf[BPRM_BUF_SIZE]) { + struct elfhdr ehdr; int fd, retval; Error *err =3D NULL; =20 @@ -2965,7 +2967,7 @@ static void load_elf_interp(const char *filename, str= uct image_info *info, memset(bprm_buf + retval, 0, BPRM_BUF_SIZE - retval); } =20 - load_elf_image(filename, fd, info, NULL, bprm_buf); + load_elf_image(filename, fd, info, &ehdr, NULL, bprm_buf); } =20 static int symfind(const void *s0, const void *s1) @@ -3157,8 +3159,14 @@ uint32_t get_elf_eflags(int fd) =20 int load_elf_binary(struct linux_binprm *bprm, struct image_info *info) { + /* + * We need a copy of the elf header for passing to create_elf_tables. + * We will have overwritten the original when we re-use bprm->buf + * while loading the interpreter. Allocate the storage for this now + * and let elf_load_image do any swapping that may be required. + */ + struct elfhdr ehdr; struct image_info interp_info; - struct elfhdr elf_ex; char *elf_interpreter =3D NULL; char *scratch; =20 @@ -3170,12 +3178,7 @@ int load_elf_binary(struct linux_binprm *bprm, struc= t image_info *info) info->start_mmap =3D (abi_ulong)ELF_START_MMAP; =20 load_elf_image(bprm->filename, bprm->fd, info, - &elf_interpreter, bprm->buf); - - /* ??? We need a copy of the elf header for passing to create_elf_tabl= es. - If we do nothing, we'll have overwritten this when we re-use bprm->= buf - when we load the interpreter. */ - elf_ex =3D *(struct elfhdr *)bprm->buf; + &ehdr, &elf_interpreter, bprm->buf); =20 /* Do this so that we can load the interpreter, if need be. We will change some of these later */ @@ -3245,7 +3248,7 @@ int load_elf_binary(struct linux_binprm *bprm, struct= image_info *info) target_mprotect(tramp_page, TARGET_PAGE_SIZE, PROT_READ | PROT_EXE= C); } =20 - bprm->p =3D create_elf_tables(bprm->p, bprm->argc, bprm->envc, &elf_ex, + bprm->p =3D create_elf_tables(bprm->p, bprm->argc, bprm->envc, &ehdr, info, (elf_interpreter ? &interp_info : NU= LL)); info->start_stack =3D bprm->p; =20 --=20 2.25.1 From nobody Wed May 1 23:49:40 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1624074873284755.5118780962723; Fri, 18 Jun 2021 20:54:33 -0700 (PDT) Received: from localhost ([::1]:50064 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1luS40-0001s0-5i for importer2@patchew.org; Fri, 18 Jun 2021 23:54:32 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33126) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1luRtd-0000Ft-UO for qemu-devel@nongnu.org; Fri, 18 Jun 2021 23:43:49 -0400 Received: from mail-pg1-x531.google.com ([2607:f8b0:4864:20::531]:36744) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1luRtP-000828-1M for qemu-devel@nongnu.org; Fri, 18 Jun 2021 23:43:49 -0400 Received: by mail-pg1-x531.google.com with SMTP id e33so9455258pgm.3 for ; Fri, 18 Jun 2021 20:43:34 -0700 (PDT) Received: from localhost.localdomain ([71.212.149.176]) by smtp.gmail.com with ESMTPSA id z6sm10045154pgs.24.2021.06.18.20.43.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Jun 2021 20:43:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=8e5E/z+pH2CRQ8YHwtnQOyFDongtbxSl8xpH1CVFvZE=; b=rxgkwup73HhdSZG1sioe3h2l9TZqUGcR0WDnA315Q0+IFMjuPiO2vBXdF1Cc+2MLMb sO/aM8SMbPSeLnF5x4GE1ob6RtZY24/nzsJ1LdP9KetmfTPLi0LXBLMaKDFDbYZEuv1e yEK/B25kamA+etydZ5h0M6+12uC1y5/nF5tMT90TzSmnaGtxQ6a3/wTU3kaJwAT8RUuR 6vJbBuV0GtqWQ5K4ZFKIpxIOTPOxznT5FgBwcUtTXKkEOm0GWAe4s45x7svU1KuVgTZ6 qlSGntoAv7rRTI7yVpY0+oa3li9bXvT4NPcoPgd7zXG2HyivJgW7O/9/q0foxMhWN6YR 3u4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=8e5E/z+pH2CRQ8YHwtnQOyFDongtbxSl8xpH1CVFvZE=; b=CdSaGsRmrVIogqZ1Ex25mS7nW4LZpkTHk+Ywg91NXbiB8LzEzzQh64OoLNJ1beI6Y+ znFTL8K8FeF03TAInhwOJ6bbOQWmtcW+sFvHS5Pp0uR2dVbfGm2yuvT4sMcpuxH08fKX xwA7DksjDqm1y3KLSIFxzYl46jBgO8y1v9C34HdvEMyTI2qqVEgFi3+/oF/N0glRZh2A R5H0puokWap7TYlWc/henTgHKzr7R1ga6LzkuOwTSD2zGjFfd4OVml71M9sRAArA/oHN APEfGYFt057Fj7bZOiYM5Q2FghpQr4H4NyS6SAcsRj/NshK/mC1r9dxHmnx3/xm+sM/u UTtQ== X-Gm-Message-State: AOAM533HnNkJbf0selZvohJh2DAi3wnDTc3/JfiVpb+4/O8ivnPrrWYb i9aNa5zp/aj6k1f18rCg9c1I/VrjHBjTiw== X-Google-Smtp-Source: ABdhPJwzxVIAeOn25xr0IYK39KM4H3Gs5659vBxnHEn6pFoqCUgUCu+8UY9HEvvgL9jDlIYoEDWdWA== X-Received: by 2002:aa7:8c59:0:b029:301:81b0:16ee with SMTP id e25-20020aa78c590000b029030181b016eemr616338pfd.24.1624074213787; Fri, 18 Jun 2021 20:43:33 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH 05/12] linux-user: Use ImageSource in load_elf_image Date: Fri, 18 Jun 2021 20:43:22 -0700 Message-Id: <20210619034329.532318-6-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210619034329.532318-1-richard.henderson@linaro.org> References: <20210619034329.532318-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::531; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x531.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: alex.bennee@linaro.org, laurent@vivier.eu Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Change parse_elf_properties as well, as the bprm_buf argument ties the two functions closely. Signed-off-by: Richard Henderson --- linux-user/elfload.c | 124 ++++++++++++++++--------------------------- 1 file changed, 47 insertions(+), 77 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 1f9a69703a..425420f0cc 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2525,10 +2525,9 @@ static bool parse_elf_property(const uint32_t *data,= int *off, int datasz, } =20 /* Process NT_GNU_PROPERTY_TYPE_0. */ -static bool parse_elf_properties(int image_fd, +static bool parse_elf_properties(const ImageSource *src, struct image_info *info, const struct elf_phdr *phdr, - char bprm_buf[BPRM_BUF_SIZE], Error **errp) { union { @@ -2556,14 +2555,8 @@ static bool parse_elf_properties(int image_fd, return false; } =20 - if (phdr->p_offset + n <=3D BPRM_BUF_SIZE) { - memcpy(¬e, bprm_buf + phdr->p_offset, n); - } else { - ssize_t len =3D pread(image_fd, ¬e, n, phdr->p_offset); - if (len !=3D n) { - error_setg_errno(errp, errno, "Error reading file header"); - return false; - } + if (!imgsrc_read(¬e, phdr->p_offset, n, src, errp)) { + return false; } =20 /* @@ -2609,30 +2602,34 @@ static bool parse_elf_properties(int image_fd, } } =20 -/* Load an ELF image into the address space. +/** + * load_elf_image: Load an ELF image into the address space. + * @image_name: the filename of the image, to use in error messages. + * @src: the ImageSource from which to read. + * @info: info collected from the loaded image. + * @ehdr: the ELF header, not yet bswapped. + * @pinterp_name: record any PT_INTERP string found. + * + * On return: @info values will be filled in, as necessary or available. + */ =20 - IMAGE_NAME is the filename of the image, to use in error messages. - IMAGE_FD is the open file descriptor for the image. - - BPRM_BUF is a copy of the beginning of the file; this of course - contains the elf file header at offset 0. It is assumed that this - buffer is sufficiently aligned to present no problems to the host - in accessing data at aligned offsets within the buffer. - - On return: INFO values will be filled in, as necessary or available. */ - -static void load_elf_image(const char *image_name, int image_fd, +static void load_elf_image(const char *image_name, const ImageSource *src, struct image_info *info, struct elfhdr *ehdr, - char **pinterp_name, - char bprm_buf[BPRM_BUF_SIZE]) + char **pinterp_name) { - struct elf_phdr *phdr; + g_autofree struct elf_phdr *phdr =3D NULL; abi_ulong load_addr, load_bias, loaddr, hiaddr, error; - int i, retval, prot_exec; + int i, prot_exec; Error *err =3D NULL; =20 - /* First of all, some simple consistency checks */ - memcpy(ehdr, bprm_buf, sizeof(*ehdr)); + /* + * First of all, some simple consistency checks. + * Note that we rely on the bswapped ehdr staying in bprm_buf, + * for later use by load_elf_binary and create_elf_tables. + */ + if (!imgsrc_read(ehdr, 0, sizeof(*ehdr), src, &err)) { + goto exit_errmsg; + } if (!elf_check_ident(ehdr)) { error_setg(&err, "Invalid ELF image for this architecture"); goto exit_errmsg; @@ -2643,15 +2640,11 @@ static void load_elf_image(const char *image_name, = int image_fd, goto exit_errmsg; } =20 - i =3D ehdr->e_phnum * sizeof(struct elf_phdr); - if (ehdr->e_phoff + i <=3D BPRM_BUF_SIZE) { - phdr =3D (struct elf_phdr *)(bprm_buf + ehdr->e_phoff); - } else { - phdr =3D (struct elf_phdr *) alloca(i); - retval =3D pread(image_fd, phdr, i, ehdr->e_phoff); - if (retval !=3D i) { - goto exit_read; - } + phdr =3D imgsrc_read_alloc(ehdr->e_phoff, + ehdr->e_phnum * sizeof(struct elf_phdr), + src, &err); + if (phdr =3D=3D NULL) { + goto exit_errmsg; } bswap_phdr(phdr, ehdr->e_phnum); =20 @@ -2687,17 +2680,10 @@ static void load_elf_image(const char *image_name, = int image_fd, goto exit_errmsg; } =20 - interp_name =3D g_malloc(eppnt->p_filesz); - - if (eppnt->p_offset + eppnt->p_filesz <=3D BPRM_BUF_SIZE) { - memcpy(interp_name, bprm_buf + eppnt->p_offset, - eppnt->p_filesz); - } else { - retval =3D pread(image_fd, interp_name, eppnt->p_filesz, - eppnt->p_offset); - if (retval !=3D eppnt->p_filesz) { - goto exit_read; - } + interp_name =3D imgsrc_read_alloc(eppnt->p_offset, eppnt->p_fi= lesz, + src, &err); + if (interp_name =3D=3D NULL) { + goto exit_errmsg; } if (interp_name[eppnt->p_filesz - 1] !=3D 0) { error_setg(&err, "Invalid PT_INTERP entry"); @@ -2705,7 +2691,7 @@ static void load_elf_image(const char *image_name, in= t image_fd, } *pinterp_name =3D g_steal_pointer(&interp_name); } else if (eppnt->p_type =3D=3D PT_GNU_PROPERTY) { - if (!parse_elf_properties(image_fd, info, eppnt, bprm_buf, &er= r)) { + if (!parse_elf_properties(src, info, eppnt, &err)) { goto exit_errmsg; } } @@ -2847,7 +2833,7 @@ static void load_elf_image(const char *image_name, in= t image_fd, vaddr_len =3D TARGET_ELF_PAGELENGTH(eppnt->p_filesz + vadd= r_po); error =3D target_mmap(vaddr_ps, vaddr_len, elf_prot, MAP_PRIVATE | MAP_FIXED, - image_fd, eppnt->p_offset - vaddr_po); + src->fd, eppnt->p_offset - vaddr_po); =20 if (error =3D=3D -1) { goto exit_mmap; @@ -2893,20 +2879,11 @@ static void load_elf_image(const char *image_name, = int image_fd, #ifdef TARGET_MIPS } else if (eppnt->p_type =3D=3D PT_MIPS_ABIFLAGS) { Mips_elf_abiflags_v0 abiflags; - if (eppnt->p_filesz < sizeof(Mips_elf_abiflags_v0)) { - error_setg(&err, "Invalid PT_MIPS_ABIFLAGS entry"); + + if (!imgsrc_read(&abiflags, eppnt->p_offset, sizeof(abiflags), + src, &err)) { goto exit_errmsg; } - if (eppnt->p_offset + eppnt->p_filesz <=3D BPRM_BUF_SIZE) { - memcpy(&abiflags, bprm_buf + eppnt->p_offset, - sizeof(Mips_elf_abiflags_v0)); - } else { - retval =3D pread(image_fd, &abiflags, sizeof(Mips_elf_abif= lags_v0), - eppnt->p_offset); - if (retval !=3D sizeof(Mips_elf_abiflags_v0)) { - goto exit_read; - } - } bswap_mips_abiflags(&abiflags); info->fp_abi =3D abiflags.fp_abi; #endif @@ -2919,21 +2896,14 @@ static void load_elf_image(const char *image_name, = int image_fd, } =20 if (qemu_log_enabled()) { - load_symbols(ehdr, image_fd, load_bias); + load_symbols(ehdr, src->fd, load_bias); } =20 mmap_unlock(); =20 - close(image_fd); + close(src->fd); return; =20 - exit_read: - if (retval >=3D 0) { - error_setg(&err, "Incomplete read of file header"); - } else { - error_setg_errno(&err, errno, "Error reading file header"); - } - goto exit_errmsg; exit_mmap: error_setg_errno(&err, errno, "Error mapping file"); goto exit_errmsg; @@ -2946,6 +2916,7 @@ static void load_elf_interp(const char *filename, str= uct image_info *info, char bprm_buf[BPRM_BUF_SIZE]) { struct elfhdr ehdr; + ImageSource src; int fd, retval; Error *err =3D NULL; =20 @@ -2963,11 +2934,11 @@ static void load_elf_interp(const char *filename, s= truct image_info *info, exit(-1); } =20 - if (retval < BPRM_BUF_SIZE) { - memset(bprm_buf + retval, 0, BPRM_BUF_SIZE - retval); - } + src.fd =3D fd; + src.cache =3D bprm_buf; + src.cache_size =3D retval; =20 - load_elf_image(filename, fd, info, &ehdr, NULL, bprm_buf); + load_elf_image(filename, &src, info, &ehdr, NULL); } =20 static int symfind(const void *s0, const void *s1) @@ -3177,8 +3148,7 @@ int load_elf_binary(struct linux_binprm *bprm, struct= image_info *info) =20 info->start_mmap =3D (abi_ulong)ELF_START_MMAP; =20 - load_elf_image(bprm->filename, bprm->fd, info, - &ehdr, &elf_interpreter, bprm->buf); + load_elf_image(bprm->filename, &bprm->src, info, &ehdr, &elf_interpret= er); =20 /* Do this so that we can load the interpreter, if need be. We will change some of these later */ --=20 2.25.1 From nobody Wed May 1 23:49:40 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1624074614601725.2948262137517; Fri, 18 Jun 2021 20:50:14 -0700 (PDT) Received: from localhost ([::1]:44994 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1luRzp-0006ik-DY for importer2@patchew.org; Fri, 18 Jun 2021 23:50:13 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33088) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1luRtZ-00009P-Dn for qemu-devel@nongnu.org; Fri, 18 Jun 2021 23:43:45 -0400 Received: from mail-pj1-x102f.google.com ([2607:f8b0:4864:20::102f]:44000) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1luRtQ-00082H-Ub for qemu-devel@nongnu.org; Fri, 18 Jun 2021 23:43:44 -0400 Received: by mail-pj1-x102f.google.com with SMTP id x21-20020a17090aa395b029016e25313bfcso7040285pjp.2 for ; Fri, 18 Jun 2021 20:43:35 -0700 (PDT) Received: from localhost.localdomain ([71.212.149.176]) by smtp.gmail.com with ESMTPSA id z6sm10045154pgs.24.2021.06.18.20.43.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Jun 2021 20:43:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=C8iPVs7HJyqae3VBQhrLESrT2Lay4oj3eIp3fHt5b/o=; b=JiaXbgl2DWOceQh6kfQsIWz94H9ekP2VWAVw7TZlaEofnW6twKumQxksUu7/kJCHqn NWpDVMu/93kCjkRpvunqxirpLkLQ/sSNGpWTab0o+hQmovPinSX6ehPMNruF8r23uNnd FZeNnQ14wCvGlu0T/YKfvSHJpwQK1JeWsRna1Qn3TEAJvJCLPBNWsTmfQLw4F2Tu7rvp kkqds0qCpjquzWSeXEgxXKqsxwGO9OgRMpCp0ih2dnMYm9agoE/GTIQq1BLblYSRo4G1 B7y8cJ7MiTqcjmHlQxchukS9QrT8NasRlxms+mfHTtHTujQI+4i/4J1d4BWlT/Z3iYa7 Ljew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=C8iPVs7HJyqae3VBQhrLESrT2Lay4oj3eIp3fHt5b/o=; b=me9Ok5qWuhCypw0PCaQi1odwH4CbKQ/+UcKuDS8u76j/QVmG5YzHS3hzt2H4uJa4ef AHvpWzTgB5FZvlqz0V2YX+NbqYPv5po8cQza3KAq2SgiatmKHOrZH0xjiQ88rxG6oJPy 9TEyZMw0feMGvjdLZ2+AXE3o3snkTwjgjCVSihlheXO8w9OX6OHjp8BSK6zjzqdoPNgm MxtwJx0FEbnd6uB6tFoZVQ6B42sy1mWwD1MKsd+bFOHNFBAYcNYMw6yK7d7hrh1DqUCR S4a9URqmQ8RkclT44wvztDfI3kVBN325wak5JG83bx1oZF3ZHIVTNe4wYbN+rDnVqyNA ZxKg== X-Gm-Message-State: AOAM530KPENnoB4f00p32gVOg6R5ojwgcKs69NW0YwrOo67M2wXaJ2yw +0WXAwrAVH/maz18OwwLB7gM2ndM6TQnpA== X-Google-Smtp-Source: ABdhPJx99vk1wfIylrqqZZ3GwDoqMLApjK+ZD2GUh6xdpfuBDJc2L3UKOg7g5YLZxMHmfU2ak6SW7w== X-Received: by 2002:a17:90a:b795:: with SMTP id m21mr14491730pjr.143.1624074214390; Fri, 18 Jun 2021 20:43:34 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH 06/12] linux-user: Use ImageSource in load_symbols Date: Fri, 18 Jun 2021 20:43:23 -0700 Message-Id: <20210619034329.532318-7-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210619034329.532318-1-richard.henderson@linaro.org> References: <20210619034329.532318-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::102f; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x102f.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: alex.bennee@linaro.org, laurent@vivier.eu Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Aside from the section headers, we're unlikely to hit the ImageSource cache on guest executables. But the interface for imgsrc_read_* is better. Signed-off-by: Richard Henderson --- linux-user/elfload.c | 87 ++++++++++++++++++++++++-------------------- 1 file changed, 48 insertions(+), 39 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 425420f0cc..3c31a5e3b0 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -1729,7 +1729,8 @@ static inline void bswap_mips_abiflags(Mips_elf_abifl= ags_v0 *abiflags) { } #ifdef USE_ELF_CORE_DUMP static int elf_core_dump(int, const CPUArchState *); #endif /* USE_ELF_CORE_DUMP */ -static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias); +static void load_symbols(struct elfhdr *hdr, const ImageSource *src, + abi_ulong load_bias); =20 /* Verify the portions of EHDR within E_IDENT for the target. This can be performed before bswapping the entire header. */ @@ -2896,7 +2897,7 @@ static void load_elf_image(const char *image_name, co= nst ImageSource *src, } =20 if (qemu_log_enabled()) { - load_symbols(ehdr, src->fd, load_bias); + load_symbols(ehdr, src, load_bias); } =20 mmap_unlock(); @@ -2984,19 +2985,20 @@ static int symcmp(const void *s0, const void *s1) } =20 /* Best attempt to load symbols from this ELF object. */ -static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias) +static void load_symbols(struct elfhdr *hdr, const ImageSource *src, + abi_ulong load_bias) { int i, shnum, nsyms, sym_idx =3D 0, str_idx =3D 0; - uint64_t segsz; - struct elf_shdr *shdr; + g_autofree struct elf_shdr *shdr =3D NULL; char *strings =3D NULL; - struct syminfo *s =3D NULL; - struct elf_sym *new_syms, *syms =3D NULL; + struct elf_sym *syms =3D NULL; + struct elf_sym *new_syms; + uint64_t segsz; =20 shnum =3D hdr->e_shnum; - i =3D shnum * sizeof(struct elf_shdr); - shdr =3D (struct elf_shdr *)alloca(i); - if (pread(fd, shdr, i, hdr->e_shoff) !=3D i) { + shdr =3D imgsrc_read_alloc(hdr->e_shoff, shnum * sizeof(struct elf_shd= r), + src, NULL); + if (shdr =3D=3D NULL) { return; } =20 @@ -3014,31 +3016,33 @@ static void load_symbols(struct elfhdr *hdr, int fd= , abi_ulong load_bias) =20 found: /* Now know where the strtab and symtab are. Snarf them. */ - s =3D g_try_new(struct syminfo, 1); - if (!s) { - goto give_up; - } =20 segsz =3D shdr[str_idx].sh_size; - s->disas_strtab =3D strings =3D g_try_malloc(segsz); - if (!strings || - pread(fd, strings, segsz, shdr[str_idx].sh_offset) !=3D segsz) { + strings =3D g_try_malloc(segsz); + if (!strings) { + goto give_up; + } + if (!imgsrc_read(strings, shdr[str_idx].sh_offset, segsz, src, NULL)) { goto give_up; } =20 segsz =3D shdr[sym_idx].sh_size; - syms =3D g_try_malloc(segsz); - if (!syms || pread(fd, syms, segsz, shdr[sym_idx].sh_offset) !=3D segs= z) { - goto give_up; - } - if (segsz / sizeof(struct elf_sym) > INT_MAX) { - /* Implausibly large symbol table: give up rather than ploughing - * on with the number of symbols calculation overflowing + /* + * Implausibly large symbol table: give up rather than ploughing + * on with the number of symbols calculation overflowing. */ goto give_up; } nsyms =3D segsz / sizeof(struct elf_sym); + syms =3D g_try_malloc(segsz); + if (!syms) { + goto give_up; + } + if (!imgsrc_read(syms, shdr[sym_idx].sh_offset, segsz, src, NULL)) { + goto give_up; + } + for (i =3D 0; i < nsyms; ) { bswap_sym(syms + i); /* Throw away entries which we do not need. */ @@ -3063,10 +3067,12 @@ static void load_symbols(struct elfhdr *hdr, int fd= , abi_ulong load_bias) goto give_up; } =20 - /* Attempt to free the storage associated with the local symbols - that we threw away. Whether or not this has any effect on the - memory allocation depends on the malloc implementation and how - many symbols we managed to discard. */ + /* + * Attempt to free the storage associated with the local symbols + * that we threw away. Whether or not this has any effect on the + * memory allocation depends on the malloc implementation and how + * many symbols we managed to discard. + */ new_syms =3D g_try_renew(struct elf_sym, syms, nsyms); if (new_syms =3D=3D NULL) { goto give_up; @@ -3075,20 +3081,23 @@ static void load_symbols(struct elfhdr *hdr, int fd= , abi_ulong load_bias) =20 qsort(syms, nsyms, sizeof(*syms), symcmp); =20 - s->disas_num_syms =3D nsyms; -#if ELF_CLASS =3D=3D ELFCLASS32 - s->disas_symtab.elf32 =3D syms; -#else - s->disas_symtab.elf64 =3D syms; -#endif - s->lookup_symbol =3D lookup_symbolxx; - s->next =3D syminfos; - syminfos =3D s; + { + struct syminfo *s =3D g_new(struct syminfo, 1); =20 + s->disas_strtab =3D strings; + s->disas_num_syms =3D nsyms; +#if ELF_CLASS =3D=3D ELFCLASS32 + s->disas_symtab.elf32 =3D syms; +#else + s->disas_symtab.elf64 =3D syms; +#endif + s->lookup_symbol =3D lookup_symbolxx; + s->next =3D syminfos; + syminfos =3D s; + } return; =20 -give_up: - g_free(s); + give_up: g_free(strings); g_free(syms); } --=20 2.25.1 From nobody Wed May 1 23:49:40 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1624074323189117.11127233086529; Fri, 18 Jun 2021 20:45:23 -0700 (PDT) Received: from localhost ([::1]:54752 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1luRv8-0002uD-5y for importer2@patchew.org; Fri, 18 Jun 2021 23:45:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33090) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1luRtZ-00009Q-ER for qemu-devel@nongnu.org; Fri, 18 Jun 2021 23:43:45 -0400 Received: from mail-pf1-x42b.google.com ([2607:f8b0:4864:20::42b]:35474) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1luRtQ-00082N-Ui for qemu-devel@nongnu.org; Fri, 18 Jun 2021 23:43:45 -0400 Received: by mail-pf1-x42b.google.com with SMTP id h12so9240416pfe.2 for ; Fri, 18 Jun 2021 20:43:35 -0700 (PDT) Received: from localhost.localdomain ([71.212.149.176]) by smtp.gmail.com with ESMTPSA id z6sm10045154pgs.24.2021.06.18.20.43.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Jun 2021 20:43:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=YC2foJgnALUaFU8F6U+DDhXWrRa+DNA7R4W0GBliIEU=; b=AausL1d+dIJbIcyEOLHzxetr1Gqs9u+R7IU/7/O6HOHcDY6VmHkcsTmnLF0ftUhZXr CaVdpZZarNPBlqUYHdfm84aMZzY08JLzSu8wrn90TaGgrE4zyRP32zHXyNDP0a7jvPWu Sq+sGVKlzgyrqnMmcAnfGhl07KJU66O3AOdNdkeCYVsOk42Ay/EQKbkfKM6Kqyyp3Wrd O8YuuLwluwzAzwH6yk8sB6JXXBFr8ibNEpiEz3JwtGv4KhfY/ukvW3OIJjWxkIlrZaCt pUtbLl8PhZyYqcdecCJgA8nTLbRa4b1yqfGJgaecnB/4jyIjBMQ+r68lBxL9jerEK4mH Ak+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=YC2foJgnALUaFU8F6U+DDhXWrRa+DNA7R4W0GBliIEU=; b=RiQFF5vfXoyFo8u5CCIC5vxUELsCDnkwxKbUx10jErAObix2tXmuAYWwulLtRAsfTq OdkbWh9X3Kqz9PmrHhVIqyoYhXpMrMLNXI0S0KMWKLWwN1qxrqmPxz9AXiF/sCvtI3B7 HctvrRVXf/Y4HKvOtY1V0wwBuLD90f5u5MdQKA6XB73z2VLdYDkDeAMP34x3gHf8H90Q jD0SvQHeJMPgAzqEKUr2ZkEqwPEHpmQtO6PTKwHEMHhtr1YlUKlHfrHE00PfqArj0EHG AWu09dJSaroJ02gHC8tD5DLda9ML4aAwh/V8Qh6NjQ4QCBoSnKtY/02/P/NlwQuGoLC3 QDZQ== X-Gm-Message-State: AOAM533az+NhZ8j3mKJ2Q84Op+1PAnPpsgBXwUD5Mv2QQ2bsBuROX0Yr OybmgIfDrlNWE+m3f8xi6LAMchPS1xu3DQ== X-Google-Smtp-Source: ABdhPJwmO2VdPmtSS4QzbZVGQjB5d/G8iAuN1S9OXWNIP4/E4hgvCNBKg9G0krt/SEGrFmONWZUqVg== X-Received: by 2002:a63:6d8d:: with SMTP id i135mr11084192pgc.350.1624074214962; Fri, 18 Jun 2021 20:43:34 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH 07/12] linux-user: Replace bprm->fd with bprm->src.fd Date: Fri, 18 Jun 2021 20:43:24 -0700 Message-Id: <20210619034329.532318-8-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210619034329.532318-1-richard.henderson@linaro.org> References: <20210619034329.532318-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::42b; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x42b.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: alex.bennee@linaro.org, laurent@vivier.eu Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" There are only a couple of uses of bprm->fd remaining. Migrate to the other field. Signed-off-by: Richard Henderson Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- linux-user/qemu.h | 1 - linux-user/flatload.c | 8 ++++---- linux-user/linuxload.c | 5 ++--- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/linux-user/qemu.h b/linux-user/qemu.h index f4cdfb16b3..dafaae6293 100644 --- a/linux-user/qemu.h +++ b/linux-user/qemu.h @@ -211,7 +211,6 @@ struct linux_binprm { char buf[BPRM_BUF_SIZE] __attribute__((aligned)); ImageSource src; abi_ulong p; - int fd; int e_uid, e_gid; int argc, envc; char **argv; diff --git a/linux-user/flatload.c b/linux-user/flatload.c index 3e5594cf89..58d0d9352c 100644 --- a/linux-user/flatload.c +++ b/linux-user/flatload.c @@ -460,7 +460,7 @@ static int load_flat_file(struct linux_binprm * bprm, DBG_FLT("BINFMT_FLAT: ROM mapping of file (we hope)\n"); =20 textpos =3D target_mmap(0, text_len, PROT_READ|PROT_EXEC, - MAP_PRIVATE, bprm->fd, 0); + MAP_PRIVATE, bprm->src.fd, 0); if (textpos =3D=3D -1) { fprintf(stderr, "Unable to mmap process text\n"); return -1; @@ -487,7 +487,7 @@ static int load_flat_file(struct linux_binprm * bprm, } else #endif { - result =3D target_pread(bprm->fd, datapos, + result =3D target_pread(bprm->src.fd, datapos, data_len + (relocs * sizeof(abi_ulong)), fpos); } @@ -537,10 +537,10 @@ static int load_flat_file(struct linux_binprm * bprm, else #endif { - result =3D target_pread(bprm->fd, textpos, + result =3D target_pread(bprm->src.fd, textpos, text_len, 0); if (result >=3D 0) { - result =3D target_pread(bprm->fd, datapos, + result =3D target_pread(bprm->src.fd, datapos, data_len + (relocs * sizeof(abi_ulong)), ntohl(hdr->data_start)); } diff --git a/linux-user/linuxload.c b/linux-user/linuxload.c index 8b93b9704c..d0d3f2ed0e 100644 --- a/linux-user/linuxload.c +++ b/linux-user/linuxload.c @@ -36,7 +36,7 @@ static int prepare_binprm(struct linux_binprm *bprm) int mode; int retval; =20 - if (fstat(bprm->fd, &st) < 0) { + if (fstat(bprm->src.fd, &st) < 0) { return -errno; } =20 @@ -66,7 +66,7 @@ static int prepare_binprm(struct linux_binprm *bprm) bprm->e_gid =3D st.st_gid; } =20 - retval =3D read(bprm->fd, bprm->buf, BPRM_BUF_SIZE); + retval =3D read(bprm->src.fd, bprm->buf, BPRM_BUF_SIZE); if (retval < 0) { perror("prepare_binprm"); exit(-1); @@ -133,7 +133,6 @@ int loader_exec(int fdexec, const char *filename, char = **argv, char **envp, { int retval; =20 - bprm->fd =3D fdexec; bprm->src.fd =3D fdexec; bprm->filename =3D (char *)filename; bprm->argc =3D count(argv); --=20 2.25.1 From nobody Wed May 1 23:49:40 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1624074451408240.22662784099157; Fri, 18 Jun 2021 20:47:31 -0700 (PDT) Received: from localhost ([::1]:34120 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1luRxC-0007zo-Dq for importer2@patchew.org; Fri, 18 Jun 2021 23:47:30 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33142) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1luRtf-0000Lt-B0 for qemu-devel@nongnu.org; Fri, 18 Jun 2021 23:43:51 -0400 Received: from mail-pj1-x102b.google.com ([2607:f8b0:4864:20::102b]:36587) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1luRtQ-00082z-Uk for qemu-devel@nongnu.org; Fri, 18 Jun 2021 23:43:51 -0400 Received: by mail-pj1-x102b.google.com with SMTP id s17-20020a17090a8811b029016e89654f93so9303329pjn.1 for ; Fri, 18 Jun 2021 20:43:36 -0700 (PDT) Received: from localhost.localdomain ([71.212.149.176]) by smtp.gmail.com with ESMTPSA id z6sm10045154pgs.24.2021.06.18.20.43.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Jun 2021 20:43:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=cGX+M1DEqpXLqG7oFtvr0mh7D0289/3sneXY8fkD+4E=; b=XtqvGzx59fKnmiS+05znM1izSdDTphG8emkM6gFVmZOrzw2XwNry1NILLklLfIi2Bx 5XUTvYKRW6YT3kgcg4YqHDV2Tv+trZcJG6zBy1ZbqxDJFP4STGTOBOWzXJTB4Keawh6u Uc71w8qX8DpRqLlV9RZ8Zf2cFvcqdfPe8arAQeP9Ci3Bhdkok2mIIg3oyLOPynsO/QQr JFhCqCtRwfrYvA2fBsAXh+98JGKV7tnd83seKQYzJg0W0mklEh06ip0XJArK44ec+m3b kuAeRFm6SXRshvVeXGJ6BaS95ruA38LIzu/Cf2yczsUcKgGTc/mhO6VyKFaQnJAiBzqV CQmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=cGX+M1DEqpXLqG7oFtvr0mh7D0289/3sneXY8fkD+4E=; b=WcDJIJcGBFOssoaZTPBn4gqydZX42Ry1jy1Egrt03/Bl0F/7MKXFyfQvUFDYh2PhIP xH+pZHsPdsnICwAZTBlgmdC9m1vXHH/h+C5pL0kgPl6WIKy7bYDAdnK0leBOoz0+rwZQ 3LEYCLRqC1ntI5MAlSBFgWiRDRCF4zaotROFjRGFBPuEyaglVIGg7eqqh43BsTaaaYt3 brNLBQslhj6DiHPsFFHHfbwH0QT8k+mGxOZha+Idh3WauG2/qyMGCmn7/dEiKfEFnYT9 qCnMC68TtAYcWuMGs1WUGRJP49YPZS1Roh2PgxItvyx4yd3wXvpwhXYdG9i+FtJyueAP Cxeg== X-Gm-Message-State: AOAM533dpAVd3JOtv4mpoQJg842ACqAh0aTrsQ2tZKoUUH6EiZu/gax8 N4fLz3reGoeJuJwcxE60RnkCB8snDkN2PA== X-Google-Smtp-Source: ABdhPJxw2D/aBlME9pSVwEq/o78yjHtiZ/StzFl5keQpI9rgeXMN/pWY8LZMyk2h4j2FDwQqVdxHDQ== X-Received: by 2002:a17:90a:6b42:: with SMTP id x2mr14204400pjl.16.1624074215409; Fri, 18 Jun 2021 20:43:35 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH 08/12] linux-user: Introduce imgsrc_mmap Date: Fri, 18 Jun 2021 20:43:25 -0700 Message-Id: <20210619034329.532318-9-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210619034329.532318-1-richard.henderson@linaro.org> References: <20210619034329.532318-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::102b; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x102b.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: alex.bennee@linaro.org, laurent@vivier.eu Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Signed-off-by: Richard Henderson --- linux-user/qemu.h | 11 +++++++++++ linux-user/elfload.c | 4 ++-- linux-user/linuxload.c | 44 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/linux-user/qemu.h b/linux-user/qemu.h index dafaae6293..255182e133 100644 --- a/linux-user/qemu.h +++ b/linux-user/qemu.h @@ -199,6 +199,17 @@ bool imgsrc_read(void *dst, off_t offset, size_t len, void *imgsrc_read_alloc(off_t offset, size_t len, const ImageSource *img, Error **errp); =20 +/** + * imgsrc_mmap: Map from ImageSource + * + * If @src has a file descriptor, pass on to target_mmap. Otherwise, + * this is "mapping" from a host buffer, which resolves to memcpy. + * Therefore, flags must be MAP_PRIVATE | MAP_FIXED; the argument is + * retained for clarity. + */ +abi_long imgsrc_mmap(abi_ulong start, abi_ulong len, int prot, + int flags, const ImageSource *src, abi_ulong offset); + /* Read a good amount of data initially, to hopefully get all the program headers loaded. */ #define BPRM_BUF_SIZE 1024 diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 3c31a5e3b0..8a3a7ae3ac 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2832,9 +2832,9 @@ static void load_elf_image(const char *image_name, co= nst ImageSource *src, */ if (eppnt->p_filesz !=3D 0) { vaddr_len =3D TARGET_ELF_PAGELENGTH(eppnt->p_filesz + vadd= r_po); - error =3D target_mmap(vaddr_ps, vaddr_len, elf_prot, + error =3D imgsrc_mmap(vaddr_ps, vaddr_len, elf_prot, MAP_PRIVATE | MAP_FIXED, - src->fd, eppnt->p_offset - vaddr_po); + src, eppnt->p_offset - vaddr_po); =20 if (error =3D=3D -1) { goto exit_mmap; diff --git a/linux-user/linuxload.c b/linux-user/linuxload.c index d0d3f2ed0e..a437a22b49 100644 --- a/linux-user/linuxload.c +++ b/linux-user/linuxload.c @@ -208,3 +208,47 @@ void *imgsrc_read_alloc(off_t offset, size_t len, } return alloc; } + +abi_long imgsrc_mmap(abi_ulong start, abi_ulong len, int prot, + int flags, const ImageSource *src, abi_ulong offset) +{ + abi_long ret; + int prot_write; + void *haddr; + + assert(flags =3D=3D (MAP_PRIVATE | MAP_FIXED)); + + if (src->fd >=3D 0) { + return target_mmap(start, len, prot, flags, src->fd, offset); + } + + /* + * This case is for the vdso; we don't expect bad images. + * The mmap may extend beyond the end of the image, especially + * to the end of the page. Zero fill. + */ + assert(offset < src->cache_size); + + prot_write =3D prot | PROT_WRITE; + ret =3D target_mmap(start, len, prot_write, flags | MAP_ANON, -1, 0); + if (ret =3D=3D -1) { + return ret; + } + + haddr =3D lock_user(VERIFY_WRITE, start, len, 0); + assert(haddr !=3D NULL); + if (offset + len < src->cache_size) { + memcpy(haddr, src->cache + offset, len); + } else { + size_t rest =3D src->cache_size - offset; + memcpy(haddr, src->cache + offset, rest); + memset(haddr + rest, 0, len - rest); + } + unlock_user(haddr, start, len); + + if (prot !=3D prot_write) { + target_mprotect(start, len, prot); + } + + return ret; +} --=20 2.25.1 From nobody Wed May 1 23:49:40 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 162407445078939.36142154472134; Fri, 18 Jun 2021 20:47:30 -0700 (PDT) Received: from localhost ([::1]:34004 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1luRxB-0007vM-Or for importer2@patchew.org; Fri, 18 Jun 2021 23:47:29 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33102) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1luRta-0000A6-Bb for qemu-devel@nongnu.org; Fri, 18 Jun 2021 23:43:46 -0400 Received: from mail-pg1-x529.google.com ([2607:f8b0:4864:20::529]:47051) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1luRtR-000834-8p for qemu-devel@nongnu.org; Fri, 18 Jun 2021 23:43:46 -0400 Received: by mail-pg1-x529.google.com with SMTP id n12so709214pgs.13 for ; Fri, 18 Jun 2021 20:43:36 -0700 (PDT) Received: from localhost.localdomain ([71.212.149.176]) by smtp.gmail.com with ESMTPSA id z6sm10045154pgs.24.2021.06.18.20.43.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Jun 2021 20:43:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=30EwpAZh1z9iPoV3TY4t8s4Mj5ydqdR77HqBIYGD76w=; b=Crp2RXyiG0vty9/gKv7xrj8Srv3eulFo6gYjIreOl7z9bF1dkf7CYUs/EX/XKoAPB4 5WQfR8ZUWdhse/0pKapJrFaAduLrKRPoVSVhOWZ2Z17kX2swvRkwSSI4T9ACYLCktVqH PHIGwvLvfLNFE0x4ImtG8OqkMzEJ5qsLSfym94ZLwSw1aou2iuYXDHdKt6TgFbjb9r6N q5nDomF4K8ZFnom7nsWRL6gjxJuBskmCxzVUhieWigXQdROEKklrIpO9FxSCZQb4MX/P UiTKlqv6mQIlJidyl+K2oFAqvSf5QB6MbniD9hytyfNfZDgOUEnItU2ISrjE8Ey/ge2N JDJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=30EwpAZh1z9iPoV3TY4t8s4Mj5ydqdR77HqBIYGD76w=; b=k6F3RwKwyDeGl3jZzXENAQwpSwl4HtyQrT9n1JA7JHQG1q2w2EIGkS8OmP534O+rQg l3alO9CIee0nFswuiUm4ngdrXgR6aW5rXCuRN6H68Fov/q59PZli8J1SuyaKE0yhCx59 364/ot5BJaiKNSVybU5k+5Wwwng8lqWjTHVaQ+x+Z/0yRiknKWM9CFN6tX6eFurEq9yv 3B5+RCo+1T3J7w/bvsZheLrQkrC6bIJHBYUMTfEn9jwacIhaCfGiEb8A6Pz4rLW5RNLS gSTGOoBf88dp6uxP6X8pzRAKD07hoTyOCeAaNEITGCppHOTJ8rY30D6HYGFO6HyRKtyd WD1A== X-Gm-Message-State: AOAM531lU1wyCtx3jgB2kD+Xk3bCv+0gCnS3SFFxmPr2TAb3BP/fA00s lCPeVgBxAMYXUtE/dRGxU51F/SMiUyWuPQ== X-Google-Smtp-Source: ABdhPJwGWs6F5Vr4gjpbRWuXcPDTQmj2QUxCpjhPgYYKuLStXY5R2f2pFvDrDd7kvV5aJ6r/C7eCqA== X-Received: by 2002:a63:d053:: with SMTP id s19mr13150063pgi.326.1624074216063; Fri, 18 Jun 2021 20:43:36 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH 09/12] linux-user: Load vdso image if available Date: Fri, 18 Jun 2021 20:43:26 -0700 Message-Id: <20210619034329.532318-10-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210619034329.532318-1-richard.henderson@linaro.org> References: <20210619034329.532318-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::529; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x529.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: alex.bennee@linaro.org, laurent@vivier.eu Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" The vdso image will be pre-processed into a C data array, with a simple list of relocations to perform, and identifying the location of signal trampolines. Signed-off-by: Richard Henderson --- linux-user/elfload.c | 75 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 67 insertions(+), 8 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 8a3a7ae3ac..a6ad454617 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -1966,7 +1966,8 @@ static abi_ulong loader_build_fdpic_loadmap(struct im= age_info *info, abi_ulong s static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc, struct elfhdr *exec, struct image_info *info, - struct image_info *interp_info) + struct image_info *interp_info, + struct image_info *vdso_info) { abi_ulong sp; abi_ulong u_argc, u_argv, u_envp, u_auxv; @@ -2038,8 +2039,12 @@ static abi_ulong create_elf_tables(abi_ulong p, int = argc, int envc, } =20 size =3D (DLINFO_ITEMS + 1) * 2; - if (k_platform) + if (k_platform) { size +=3D 2; + } + if (vdso_info) { + size +=3D 4; + } #ifdef DLINFO_ARCH_ITEMS size +=3D DLINFO_ARCH_ITEMS * 2; #endif @@ -2116,6 +2121,10 @@ static abi_ulong create_elf_tables(abi_ulong p, int = argc, int envc, if (u_platform) { NEW_AUX_ENT(AT_PLATFORM, u_platform); } + if (vdso_info) { + NEW_AUX_ENT(AT_SYSINFO, vdso_info->entry); + NEW_AUX_ENT(AT_SYSINFO_EHDR, vdso_info->load_addr); + } NEW_AUX_ENT (AT_NULL, 0); #undef NEW_AUX_ENT =20 @@ -2942,6 +2951,53 @@ static void load_elf_interp(const char *filename, st= ruct image_info *info, load_elf_image(filename, &src, info, &ehdr, NULL); } =20 +#ifndef HAVE_VDSO +#define HAVE_VDSO 0 +static uint8_t vdso_image[] =3D { }; +static uint32_t vdso_relocs[] =3D { }; +#define vdso_sigreturn 0 +#define vdso_rt_sigreturn 0 +#endif + +static void load_elf_vdso(struct image_info *info) +{ + ImageSource src; + struct elfhdr ehdr; + abi_ulong load_bias, load_addr; + + src.fd =3D -1; + src.cache =3D vdso_image; + src.cache_size =3D sizeof(vdso_image); + + load_elf_image("", &src, info, &ehdr, NULL); + load_addr =3D info->load_addr; + load_bias =3D info->load_bias; + + /* + * We need to relocate the VDSO image. The one built into the kernel + * is built for a fixed address. The one built for QEMU is not, since + * that requires close control of the guest address space. + * We pre-processed the image to locate all of the addresses that need + * to be updated. + */ + for (size_t i =3D 0, n =3D ARRAY_SIZE(vdso_relocs); i < n; i++) { + abi_ulong *addr =3D g2h_untagged(load_addr + vdso_relocs[i]); + *addr =3D tswapal(tswapal(*addr) + load_bias); + } + + /* Install signal trampolines, if present. */ + if (vdso_sigreturn) { + default_sigreturn =3D load_addr + vdso_sigreturn; + } + if (vdso_rt_sigreturn) { + default_rt_sigreturn =3D load_addr + vdso_rt_sigreturn; + } + + /* Mark the VDSO writable segment read-only. */ + target_mprotect(info->start_data, info->end_data - info->start_data, + PROT_READ); +} + static int symfind(const void *s0, const void *s1) { target_ulong addr =3D *(target_ulong *)s0; @@ -3146,7 +3202,7 @@ int load_elf_binary(struct linux_binprm *bprm, struct= image_info *info) * and let elf_load_image do any swapping that may be required. */ struct elfhdr ehdr; - struct image_info interp_info; + struct image_info interp_info, vdso_info; char *elf_interpreter =3D NULL; char *scratch; =20 @@ -3216,10 +3272,12 @@ int load_elf_binary(struct linux_binprm *bprm, stru= ct image_info *info) } =20 /* - * TODO: load a vdso, which would also contain the signal trampolines. - * Otherwise, allocate a private page to hold them. + * Load a vdso if available, which will amongst other things contain t= he + * signal trampolines. Otherwise, allocate a separate page for them. */ - if (TARGET_ARCH_HAS_SIGTRAMP_PAGE) { + if (HAVE_VDSO) { + load_elf_vdso(&vdso_info); + } else if (TARGET_ARCH_HAS_SIGTRAMP_PAGE) { abi_ulong tramp_page =3D target_mmap(0, TARGET_PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); @@ -3227,8 +3285,9 @@ int load_elf_binary(struct linux_binprm *bprm, struct= image_info *info) target_mprotect(tramp_page, TARGET_PAGE_SIZE, PROT_READ | PROT_EXE= C); } =20 - bprm->p =3D create_elf_tables(bprm->p, bprm->argc, bprm->envc, &ehdr, - info, (elf_interpreter ? &interp_info : NU= LL)); + bprm->p =3D create_elf_tables(bprm->p, bprm->argc, bprm->envc, &ehdr, = info, + elf_interpreter ? &interp_info : NULL, + HAVE_VDSO ? &vdso_info : NULL); info->start_stack =3D bprm->p; =20 /* If we have an interpreter, set that as the program's entry point. --=20 2.25.1 From nobody Wed May 1 23:49:40 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1624074547235776.2688335375575; Fri, 18 Jun 2021 20:49:07 -0700 (PDT) Received: from localhost ([::1]:40618 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1luRyk-0003qE-6z for importer2@patchew.org; Fri, 18 Jun 2021 23:49:06 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33144) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1luRtg-0000QY-8I for qemu-devel@nongnu.org; Fri, 18 Jun 2021 23:43:52 -0400 Received: from mail-pf1-x42d.google.com ([2607:f8b0:4864:20::42d]:39465) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1luRtS-000839-7z for qemu-devel@nongnu.org; Fri, 18 Jun 2021 23:43:52 -0400 Received: by mail-pf1-x42d.google.com with SMTP id g192so1535307pfb.6 for ; Fri, 18 Jun 2021 20:43:37 -0700 (PDT) Received: from localhost.localdomain ([71.212.149.176]) by smtp.gmail.com with ESMTPSA id z6sm10045154pgs.24.2021.06.18.20.43.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Jun 2021 20:43:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Xf7gfODhEHLAK19nF0i1E5avDqb1sECHeoNiF4HXyew=; b=qRFoLw/pun0mqOgFyNOYnYtQG1wLhEr3uqKtZjDoNW+5aagq/WUv3jH4RI8/nv81Ha /HNoHJdI3ndQu7M9VMM+M8oD7CfwOrwCJFjfKWNl1aN1ftFRNV1RTgew3Obd8Y9UXe+x llBXnNqhsqm+UCKLjmv2D2VrfO2jfWG4YepfWCUzIx9kEBjxoJjoMxjIAOAd/Lqnh6OK 79+zs+IyA4YAvPsHiBszQ93qgAuJZeCWyV4XYfFOW4TMx8WKysLHA7iOPXDr47kb0j/K sF1YMCs/rVlWDon37UHQm44aMT84hC+2CkDu5bqhtyUkIl1HvSUT/a1ut8+u6ddO+TBI abGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Xf7gfODhEHLAK19nF0i1E5avDqb1sECHeoNiF4HXyew=; b=Ps8FelFrvhwgim9p6yLZjEDXQZGn9xl+/CW9fi6RJKIDlHxksmrDR1SS4033IxJb5o XHr19keou6gxSMctP+An5Kng5L8AWFv//T0DKLveF2l/FXyyEEICigYvI1OGYGc4aYyn q85174OpirlP3WCNE/irynKQay/U/Gwye1dPwPeYJ6LzSRk+Jqci4YzBHQo1zDsIvjF2 OlAEY2/lXgcpepAygcYthvXwbWJ++ljyRi9Maa+Nsqe3HsJnejsRVDHuNp7QQ1RmVJQo imHpxQkPk4ZpsMQdnHjR1hF3ZOowOxyPv6HABS/wa2GYBdFwvcO7wQ5VYZdzGiyI9Smd R31A== X-Gm-Message-State: AOAM53242gmkQSwl1Yb7hSnF+QPxGzCSrEWuRfuPx/GfcOeKLKYglRor pI+3esq/qFi371jJjAS+rMYTDbtHuyBpyw== X-Google-Smtp-Source: ABdhPJwfz47vCpLkpdF+is3yFSxYjk5JBWI8c1dSrFzCWmXyT9HIb1fTaugkpdHMIbMNWDjo2bydlQ== X-Received: by 2002:a63:5553:: with SMTP id f19mr12828301pgm.419.1624074216535; Fri, 18 Jun 2021 20:43:36 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH 10/12] linux-user: Add gen-vdso tool Date: Fri, 18 Jun 2021 20:43:27 -0700 Message-Id: <20210619034329.532318-11-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210619034329.532318-1-richard.henderson@linaro.org> References: <20210619034329.532318-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::42d; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x42d.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: alex.bennee@linaro.org, laurent@vivier.eu Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This tool will be used for post-processing the linked vdso image, turning it into something that is easy to include into elfload.c. Signed-off-by: Richard Henderson --- linux-user/gen-vdso.c | 168 ++++++++++++++++++ linux-user/gen-vdso-elfn.c.inc | 299 +++++++++++++++++++++++++++++++++ linux-user/meson.build | 6 +- 3 files changed, 472 insertions(+), 1 deletion(-) create mode 100644 linux-user/gen-vdso.c create mode 100644 linux-user/gen-vdso-elfn.c.inc diff --git a/linux-user/gen-vdso.c b/linux-user/gen-vdso.c new file mode 100644 index 0000000000..ccbb6c4725 --- /dev/null +++ b/linux-user/gen-vdso.c @@ -0,0 +1,168 @@ +/* + * Post-process a vdso elf image for inclusion into qemu. + * + * Copyright 2021 Linaro, Ltd. + *=20 + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include +#include +#include +#include +#include +#include +#include +#include "elf.h" + + +#define bswap_(p) _Generic(*(p), \ + uint16_t: __builtin_bswap16, \ + uint32_t: __builtin_bswap32, \ + uint64_t: __builtin_bswap64, \ + int16_t: __builtin_bswap16, \ + int32_t: __builtin_bswap32, \ + int64_t: __builtin_bswap64) +#define bswaps(p) (*(p) =3D bswap_(p)(*(p))) + +static void output_reloc(FILE *outf, void *buf, void *loc) +{ + fprintf(outf, " 0x%08lx,\n", loc - buf); +} + +#define N 32 +#define elfN(x) elf32_##x +#define ElfN(x) Elf32_##x +#include "gen-vdso-elfn.c.inc" +#undef N +#undef elfN +#undef ElfN + +#define N 64 +#define elfN(x) elf64_##x +#define ElfN(x) Elf64_##x +#include "gen-vdso-elfn.c.inc" +#undef N +#undef elfN +#undef ElfN + + +int main(int ac, char **av) +{ + FILE *inf, *outf; + long total_len; + const char *inf_name; + const char *outf_name; + unsigned char *buf; + bool need_bswap; + + if (ac !=3D 3) { + fprintf(stderr, "usage: input-file output-file\n"); + return EXIT_FAILURE; + } + inf_name =3D av[1]; + outf_name =3D av[2]; + + /* + * Open the input and output files. + */ + inf =3D fopen(inf_name, "rb"); + if (inf =3D=3D NULL) { + goto perror_inf; + } + outf =3D fopen(outf_name, "w"); + if (outf =3D=3D NULL) { + goto perror_outf; + } + + /* + * Read the input file into a buffer. + * We expect the vdso to be small, on the order of one page, + * therefore we do not expect a partial read. + */ + fseek(inf, 0, SEEK_END); + total_len =3D ftell(inf); + fseek(inf, 0, SEEK_SET); + + buf =3D malloc(total_len); + if (buf =3D=3D NULL) { + goto perror_inf; + } + + errno =3D 0; + if (fread(buf, 1, total_len, inf) !=3D total_len) { + if (errno) { + goto perror_inf; + } + fprintf(stderr, "%s: incomplete read\n", inf_name); + return EXIT_FAILURE; + } + fclose(inf); + + /* + * Write out the vdso image now, before we make local changes. + */ + + fputs("/* Automatically generated from linux-user/gen-vdso.c. */\n" + "\n" + "static const uint8_t vdso_image[] =3D {", + outf); + for (long i =3D 0; i < total_len; ++i) { + if (i % 12 =3D=3D 0) { + fputs("\n ", outf); + } + fprintf(outf, " 0x%02x,", buf[i]); + } + fputs("\n};\n\n", outf); + + /* + * Identify which elf flavor we're processing. + * The first 16 bytes of the file are e_ident. + */ + + if (buf[EI_MAG0] !=3D ELFMAG0 || buf[EI_MAG1] !=3D ELFMAG1 || + buf[EI_MAG2] !=3D ELFMAG2 || buf[EI_MAG3] !=3D ELFMAG3) { + fprintf(stderr, "%s: not an elf file\n", inf_name); + return EXIT_FAILURE; + } + switch (buf[EI_DATA]) { + case ELFDATA2LSB: + need_bswap =3D BYTE_ORDER !=3D LITTLE_ENDIAN; + break; + case ELFDATA2MSB: + need_bswap =3D BYTE_ORDER !=3D BIG_ENDIAN; + break; + default: + fprintf(stderr, "%s: invalid elf EI_DATA (%u)\n", + inf_name, buf[EI_DATA]); + return EXIT_FAILURE; + } + switch (buf[EI_CLASS]) { + case ELFCLASS32: + elf32_process(outf, buf, total_len, need_bswap); + break; + case ELFCLASS64: + elf64_process(outf, buf, total_len, need_bswap); + break; + default: + fprintf(stderr, "%s: invalid elf EI_CLASS (%u)\n", + inf_name, buf[EI_CLASS]); + return EXIT_FAILURE; + } + + /* + * Everything should have gone well. + */ + if (fclose(outf)) { + goto perror_outf; + } + return EXIT_SUCCESS; + + perror_inf: + perror(inf_name); + return EXIT_FAILURE; + + perror_outf: + perror(outf_name); + return EXIT_FAILURE; +} diff --git a/linux-user/gen-vdso-elfn.c.inc b/linux-user/gen-vdso-elfn.c.inc new file mode 100644 index 0000000000..4e9277aeff --- /dev/null +++ b/linux-user/gen-vdso-elfn.c.inc @@ -0,0 +1,299 @@ +/* + * Post-process a vdso elf image for inclusion into qemu. + * Elf size specialization. + * + * Copyright 2021 Linaro, Ltd. + *=20 + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +static void elfN(bswap_ehdr)(ElfN(Ehdr) *ehdr) +{ + bswaps(&ehdr->e_type); /* Object file type */ + bswaps(&ehdr->e_machine); /* Architecture */ + bswaps(&ehdr->e_version); /* Object file version */ + bswaps(&ehdr->e_entry); /* Entry point virtual address */ + bswaps(&ehdr->e_phoff); /* Program header table file offset = */ + bswaps(&ehdr->e_shoff); /* Section header table file offset = */ + bswaps(&ehdr->e_flags); /* Processor-specific flags */ + bswaps(&ehdr->e_ehsize); /* ELF header size in bytes */ + bswaps(&ehdr->e_phentsize); /* Program header table entry size */ + bswaps(&ehdr->e_phnum); /* Program header table entry count = */ + bswaps(&ehdr->e_shentsize); /* Section header table entry size */ + bswaps(&ehdr->e_shnum); /* Section header table entry count = */ + bswaps(&ehdr->e_shstrndx); /* Section header string table index= */ +} + +static void elfN(bswap_phdr)(ElfN(Phdr) *phdr) +{ + bswaps(&phdr->p_type); /* Segment type */ + bswaps(&phdr->p_flags); /* Segment flags */ + bswaps(&phdr->p_offset); /* Segment file offset */ + bswaps(&phdr->p_vaddr); /* Segment virtual address */ + bswaps(&phdr->p_paddr); /* Segment physical address */ + bswaps(&phdr->p_filesz); /* Segment size in file */ + bswaps(&phdr->p_memsz); /* Segment size in memory */ + bswaps(&phdr->p_align); /* Segment alignment */ +} + +static void elfN(bswap_shdr)(ElfN(Shdr) *shdr) +{ + bswaps(&shdr->sh_name); + bswaps(&shdr->sh_type); + bswaps(&shdr->sh_flags); + bswaps(&shdr->sh_addr); + bswaps(&shdr->sh_offset); + bswaps(&shdr->sh_size); + bswaps(&shdr->sh_link); + bswaps(&shdr->sh_info); + bswaps(&shdr->sh_addralign); + bswaps(&shdr->sh_entsize); +} + +static void elfN(bswap_sym)(ElfN(Sym) *sym) +{ + bswaps(&sym->st_name); + bswaps(&sym->st_value); + bswaps(&sym->st_size); + bswaps(&sym->st_shndx); +} + +static void elfN(bswap_dyn)(ElfN(Dyn) *dyn) +{ + bswaps(&dyn->d_tag); /* Dynamic type tag */ + bswaps(&dyn->d_un.d_ptr); /* Dynamic ptr or val, in union */ +} + +static void elfN(process)(FILE *outf, void *buf, long total_len, + bool need_bswap) +{ + ElfN(Ehdr) *ehdr =3D buf; + ElfN(Phdr) *phdr; + ElfN(Shdr) *shdr; + unsigned phnum, shnum; + unsigned dynamic_ofs =3D 0; + unsigned dynamic_addr =3D 0; + unsigned dynsym_addr =3D 0; + unsigned sigreturn_addr =3D 0; + unsigned rt_sigreturn_addr =3D 0; + unsigned first_segsz =3D 0; + int errors =3D 0; + + if (need_bswap) { + elfN(bswap_ehdr)(ehdr); + } + + phnum =3D ehdr->e_phnum; + phdr =3D buf + ehdr->e_phoff; + if (need_bswap) { + for (unsigned i =3D 0; i < phnum; ++i) { + elfN(bswap_phdr)(phdr + i); + } + } + + shnum =3D ehdr->e_shnum; + shdr =3D buf + ehdr->e_shoff; + if (need_bswap) { + for (unsigned i =3D 0; i < shnum; ++i) { + elfN(bswap_shdr)(shdr + i); + } + } + + /* + * Validate the VDSO is created as we expect: that PT_PHDR, + * PT_DYNAMIC, and PT_NOTE located in a writable data segment. + * PHDR and DYNAMIC require relocation, and NOTE will get the + * linux version number. + */ + for (unsigned i =3D 0; i < phnum; ++i) { + if (phdr[i].p_type =3D=3D PT_LOAD && phdr[i].p_vaddr =3D=3D 0) { + if (first_segsz !=3D 0) { + fprintf(stderr, "Multiple load segments covering EHDR\n"); + errors++; + } + if (phdr[i].p_offset !=3D 0) { + fprintf(stderr, "First vdso segment does not cover EHDR\n"= ); + errors++; + } + if (phdr[i].p_vaddr !=3D 0) { + fprintf(stderr, "First vdso segment not loaded at address = 0\n"); + errors++; + } + if ((phdr[i].p_flags & (PF_R | PF_W)) !=3D (PF_R | PF_W)) { + fprintf(stderr, "First vdso segment is not read-write\n"); + errors++; + } + first_segsz =3D phdr[i].p_filesz; + if (first_segsz < ehdr->e_phoff + phnum * sizeof(*phdr)) { + fprintf(stderr, "First vdso segment does not cover PHDRs\n= "); + errors++; + } + } + } + for (unsigned i =3D 0; i < phnum; ++i) { + const char *which; + + switch (phdr[i].p_type) { + case PT_PHDR: + which =3D "PT_PHDR"; + break; + case PT_NOTE: + which =3D "PT_NOTE"; + break; + case PT_DYNAMIC: + dynamic_ofs =3D phdr[i].p_offset; + dynamic_addr =3D phdr[i].p_vaddr; + which =3D "PT_DYNAMIC"; + break; + default: + continue; + } + if (first_segsz < phdr[i].p_vaddr + phdr[i].p_filesz) { + fprintf(stderr, "First vdso segment does not cover %s\n", whic= h); + errors++; + } + } + if (errors) { + exit(EXIT_FAILURE); + } + =20 + /* + * We need to relocate the VDSO image. The one built into the kernel + * is built for a fixed address. The one we built for QEMU is not, + * since that requires close control of the guest address space. + * + * ??? One might think that we'd need to relocate ehdr->e_entry, + * but for some reason glibc does that one itself, though that + * is also available via the AT_SYSINFO entry. + * + * Output relocation addresses as we go. + */ + fputs("static const unsigned vdso_relocs[] =3D {\n", outf); + + /* Relocate the program headers. */ + for (unsigned i =3D 0; i < phnum; ++i) { + output_reloc(outf, buf, &phdr[i].p_vaddr); + output_reloc(outf, buf, &phdr[i].p_paddr); + } + + /* Relocate the DYNAMIC entries. */ + if (dynamic_addr) { + ElfN(Dyn) *dyn =3D buf + dynamic_ofs; + __typeof(dyn->d_tag) tag; + + do { + + if (need_bswap) { + elfN(bswap_dyn)(dyn); + } + tag =3D dyn->d_tag; + + switch (tag) { + case DT_SYMTAB: + dynsym_addr =3D dyn->d_un.d_val; + /* fall through */ + case DT_HASH: + case DT_STRTAB: + case DT_VERDEF: + case DT_VERSYM: + case DT_PLTGOT: + case DT_ADDRRNGLO ... DT_ADDRRNGHI: + /* These entries store an address in the entry. */ + output_reloc(outf, buf, &dyn->d_un.d_val); + break; + + case DT_NULL: + case DT_STRSZ: + case DT_SONAME: + case DT_DEBUG: + case DT_FLAGS: + case DT_FLAGS_1: + case DT_BIND_NOW: + case DT_VERDEFNUM: + case DT_VALRNGLO ... DT_VALRNGHI: + /* These entries store an integer in the entry. */ + break; + + case DT_SYMENT: + if (dyn->d_un.d_val !=3D sizeof(ElfN(Sym))) { + fprintf(stderr, "VDSO has incorrect dynamic symbol siz= e\n"); + errors++; + } + break; + + case DT_REL: + case DT_RELSZ: + case DT_RELENT: + case DT_RELA: + case DT_RELASZ: + case DT_RELAENT: + case DT_TEXTREL: + /* + * These entries indicate that the VDSO was built incorrec= tly. + * It should not have any real relocations. + */ + fprintf(stderr, "VDSO has dynamic relocations\n"); + errors++; + break; + + case DT_NEEDED: + case DT_VERNEED: + case DT_PLTREL: + case DT_JMPREL: + case DT_RPATH: + case DT_RUNPATH: + fprintf(stderr, "VDSO has external dependencies\n"); + errors++; + break; + + default: + /* This is probably something target specific. */ + fprintf(stderr, "VDSO has unknown DYNAMIC entry (%lx)\n", + (unsigned long)tag); + errors++; + break; + } + dyn++; + } while (tag !=3D DT_NULL); + if (errors) { + exit(EXIT_FAILURE); + } + } + + if (dynsym_addr) { + __typeof(shdr->sh_size) dynsym_n =3D 0; + ElfN(Sym) *sym =3D NULL; + const char *str =3D NULL; + + for (unsigned i =3D 0; i < shnum; ++i) { + if (shdr[i].sh_addr =3D=3D dynsym_addr) { + dynsym_n =3D shdr[i].sh_size / sizeof(*sym); + sym =3D buf + shdr[i].sh_offset; + str =3D buf + shdr[shdr[i].sh_link].sh_offset; + break; + } + } + + for (unsigned i =3D 0; i < dynsym_n; ++i) { + if (need_bswap) { + elfN(bswap_sym)(sym + i); + } + + /* Relocate the dynamic symbol table. */ + output_reloc(outf, buf, &sym[i].st_value); + + /* Locate the signal return symbols. */ + const char *name =3D str + sym[i].st_name; + if (strcmp("__kernel_sigreturn", name) =3D=3D 0) { + sigreturn_addr =3D sym[i].st_value; + } else if (strcmp("__kernel_rt_sigreturn", name) =3D=3D 0) { + rt_sigreturn_addr =3D sym[i].st_value; + } + } + } + + fputs("};\n\n", outf); /* end vdso_relocs. */ + + fprintf(outf, "#define vdso_sigreturn 0x%x\n", sigreturn_addr); + fprintf(outf, "#define vdso_rt_sigreturn 0x%x\n", rt_sigreturn_addr); +} diff --git a/linux-user/meson.build b/linux-user/meson.build index 9549f81682..c5e6ab058b 100644 --- a/linux-user/meson.build +++ b/linux-user/meson.build @@ -18,9 +18,13 @@ linux_user_ss.add(when: 'TARGET_HAS_BFLT', if_true: file= s('flatload.c')) linux_user_ss.add(when: 'TARGET_I386', if_true: files('vm86.c')) linux_user_ss.add(when: 'CONFIG_ARM_COMPATIBLE_SEMIHOSTING', if_true: file= s('semihost.c')) =20 - syscall_nr_generators =3D {} =20 +gen_vdso_exe =3D executable('gen-vdso', 'gen-vdso.c', + native: true, build_by_default: false) +gen_vdso =3D generator(gen_vdso_exe, output: '@BASENAME@.c.inc', + arguments: ['@INPUT@', '@OUTPUT@']) + subdir('alpha') subdir('arm') subdir('hppa') --=20 2.25.1 From nobody Wed May 1 23:49:41 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 162407474616854.337038059461406; Fri, 18 Jun 2021 20:52:26 -0700 (PDT) Received: from localhost ([::1]:47894 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1luS1x-0000Ka-5S for importer2@patchew.org; Fri, 18 Jun 2021 23:52:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33120) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1luRtd-0000E0-1F for qemu-devel@nongnu.org; Fri, 18 Jun 2021 23:43:49 -0400 Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]:36804) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1luRtS-00083V-BD for qemu-devel@nongnu.org; Fri, 18 Jun 2021 23:43:48 -0400 Received: by mail-pl1-x634.google.com with SMTP id x10so5680348plg.3 for ; Fri, 18 Jun 2021 20:43:37 -0700 (PDT) Received: from localhost.localdomain ([71.212.149.176]) by smtp.gmail.com with ESMTPSA id z6sm10045154pgs.24.2021.06.18.20.43.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Jun 2021 20:43:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=jUXae3SraHm4OVG+O8NmkBHPeUZIHDauOpN34upW9DU=; b=Hxp8zVdKo0LHgnRJjsiw2rdWR5HgloN8+Hfji0V5HsaPQx92mKxV2mrYvrJbED9MI/ buqXCi6b7VOjaeSIe0ZCtGM/MvsLc6zCpOFtDcxEqzIQ46DsDdO82U8NldodHks1ht2C UD3Xfv/A6r5pWaONEoQRHY4RoXecueAwPRQXdvk4/d018gHAbv5W0yc20XcNi8l2hYGE CAZsF2cRajFQbo9amEPrz+o7rOZVRbYdN1auXErRoPM6jRaBailyAe0Pzv5hgLnXoAqm zyx+jfMmDjh7cf8um/7JKeNEc6GnrK3DuySZ3cCgHgOhK3g5v3wAbsASWRCsmcx10B+V uujg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=jUXae3SraHm4OVG+O8NmkBHPeUZIHDauOpN34upW9DU=; b=Wpk+ZdNFJz5PIVDvCu/Juj5gomjmaidy7NeYv1l5I5SmvAT9zOLOVlkv/ILQEBe/GX gr8pkD8fSCImxFC0hIBSGxgVnQwDXDJJ8ddp4WZtwj33Y6W+fc+hxqtqfZQuGzzjfJjo 8++JWoyloDb2SfePLv55KaQODTWKl/hbrzvbJABILJ1HFAtd9C5PuRq1JM1e40pmLSwL TiBsnVXABhdcnVruM2wJJfqqA4QG9Y1AZPSH3Wq6caBpetvVHxKJaHYaV4UUnm4Dj/Ai JCFxVCC5Xnr9ivWLTV2lOAGoF/tjMe6sV2yGAVHMVdJ+raYOSh8u8I9J7bvEaBPJBXFc +RAA== X-Gm-Message-State: AOAM533wY6QBgdJkREqH8rvMGDsiarIlj14eWW/CswnDrMf5sejzRivl 26WW1ne0eFq/KEd8u3OqPpU3zf1Gt5CMPg== X-Google-Smtp-Source: ABdhPJzxl16x2N4awANguc1rOSJE+HU6wXHFSyTdlF9Nswz7ajgicPeI4WRjC6enAPQgPn/NzTRrZg== X-Received: by 2002:a17:902:d4d0:b029:113:fb3d:3644 with SMTP id o16-20020a170902d4d0b0290113fb3d3644mr7545662plg.58.1624074216979; Fri, 18 Jun 2021 20:43:36 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH 11/12] linux-user/x86_64: Add vdso Date: Fri, 18 Jun 2021 20:43:28 -0700 Message-Id: <20210619034329.532318-12-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210619034329.532318-1-richard.henderson@linaro.org> References: <20210619034329.532318-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::634; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x634.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: alex.bennee@linaro.org, laurent@vivier.eu Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Building the vdso itself is not actually wired up to anything, since we require a cross-compiler. Just check in that file for now. Signed-off-by: Richard Henderson --- linux-user/elfload.c | 3 + linux-user/x86_64/Makefile.vdso | 3 + linux-user/x86_64/meson.build | 6 ++ linux-user/x86_64/vdso.S | 122 ++++++++++++++++++++++++++++++++ linux-user/x86_64/vdso.ld | 74 +++++++++++++++++++ linux-user/x86_64/vdso.so | Bin 0 -> 5912 bytes 6 files changed, 208 insertions(+) create mode 100644 linux-user/x86_64/Makefile.vdso create mode 100644 linux-user/x86_64/vdso.S create mode 100644 linux-user/x86_64/vdso.ld create mode 100755 linux-user/x86_64/vdso.so diff --git a/linux-user/elfload.c b/linux-user/elfload.c index a6ad454617..b70a5c48a2 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -154,6 +154,9 @@ static uint32_t get_elf_hwcap(void) #define ELF_CLASS ELFCLASS64 #define ELF_ARCH EM_X86_64 =20 +#define HAVE_VDSO 1 +#include "vdso.c.inc" + static inline void init_thread(struct target_pt_regs *regs, struct image_i= nfo *infop) { regs->rax =3D 0; diff --git a/linux-user/x86_64/Makefile.vdso b/linux-user/x86_64/Makefile.v= dso new file mode 100644 index 0000000000..6111d6f21c --- /dev/null +++ b/linux-user/x86_64/Makefile.vdso @@ -0,0 +1,3 @@ +vdso.so: vdso.S vdso.ld Makefile.vdso + $(CC) -nostdlib -shared -Wl,-T,vdso.ld -Wl,--build-id=3Dnone \ + -Wl,-h,linux-vdso.so.1 -Wl,--hash-style=3Dboth vdso.S -o $@ diff --git a/linux-user/x86_64/meson.build b/linux-user/x86_64/meson.build index 203af9a60c..f6a0015953 100644 --- a/linux-user/x86_64/meson.build +++ b/linux-user/x86_64/meson.build @@ -3,3 +3,9 @@ syscall_nr_generators +=3D { arguments: [ meson.current_source_dir() / 'syscallhd= r.sh', '@INPUT@', '@OUTPUT@', '@EXTRA_ARGS@' ], output: '@BASENAME@_nr.h') } + +gen =3D [ + gen_vdso.process('vdso.so') +] + +linux_user_ss.add(when: 'TARGET_X86_64', if_true: gen) diff --git a/linux-user/x86_64/vdso.S b/linux-user/x86_64/vdso.S new file mode 100644 index 0000000000..7d0d653526 --- /dev/null +++ b/linux-user/x86_64/vdso.S @@ -0,0 +1,122 @@ +/* + * x86-64 linux replacement vdso. + * + * Copyright 2021 Linaro, Ltd. + *=20 + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include + + .globl __vdso_clock_gettime + .type __vdso_clock_gettime, @function + .balign 16 + .cfi_startproc +__vdso_clock_gettime: + mov $__NR_clock_gettime, %eax + syscall + ret + .cfi_endproc + .size __vdso_clock_gettime, . - __vdso_clock_gettime + +clock_gettime =3D __vdso_clock_gettime + .weak clock_gettime + + .globl __vdso_clock_getres + .type __vdso_clock_getres, @function + .balign 16 + .cfi_startproc +__vdso_clock_getres: + mov $__NR_clock_getres, %eax + syscall + ret + .cfi_endproc + .size __vdso_clock_getres, . - __vdso_clock_getres + +clock_getres =3D __vdso_clock_getres + .weak clock_getres + + .globl __vdso_gettimeofday + .type __vdso_gettimeofday, @function + .balign 16 + .cfi_startproc +__vdso_gettimeofday: + mov $__NR_gettimeofday, %eax + syscall + ret + .cfi_endproc + .size __vdso_gettimeofday, . - __vdso_gettimeofday + +gettimeofday =3D __vdso_gettimeofday + .weak gettimeofday + + + .globl __vdso_time + .type __vdso_time, @function + .balign 16 + .cfi_startproc +__vdso_time: + mov $__NR_time, %eax + syscall + ret + .cfi_endproc + .size __vdso_time, . - __vdso_time + +time =3D __vdso_time + .weak time + + + .globl __vdso_getcpu + .type __vdso_getcpu, @function + .balign 16 + .cfi_startproc +__vdso_getcpu: + /* + * ??? There is no syscall number for this allocated on x64. + * We can handle this several ways: + * + * (1) Invent a syscall number for use within qemu. + * It should be easy enough to pick a number that + * is well out of the way of the kernel numbers. + * + * (2) Force the emulated cpu to support the rdtscp insn, + * and initialize the TSC_AUX value the appropriate value. + * + * (3) Pretend that we're always running on cpu 0. + * + * This last is the one that's implemented here, with the + * tiny bit of extra code to support rdtscp in place. + */ + xor %ecx, %ecx /* rdtscp w/ tsc_aux =3D 0 */ + + /* if (cpu !=3D NULL) *cpu =3D (ecx & 0xfff); */ + test %rdi, %rdi + jz 1f + mov %ecx, %eax + and $0xfff, %eax + mov %eax, (%rdi) + + /* if (node !=3D NULL) *node =3D (ecx >> 12); */ +1: test %rsi, %rsi + jz 2f + shr $12, %ecx + mov %ecx, (%rsi) + +2: xor %eax, %eax + ret + .cfi_endproc + .size __vdso_getcpu, . - __vdso_getcpu + +getcpu =3D __vdso_getcpu + .weak getcpu + +/* + * ??? Perhaps add elf notes. E.g. + * + * #include + * ELFNOTE_START(Linux, 0, "a") + * .long LINUX_VERSION_CODE + * ELFNOTE_END + * + * but what version number would we set for QEMU? + */ diff --git a/linux-user/x86_64/vdso.ld b/linux-user/x86_64/vdso.ld new file mode 100644 index 0000000000..baf261f045 --- /dev/null +++ b/linux-user/x86_64/vdso.ld @@ -0,0 +1,74 @@ +/* + * Linker script for linux x86-64 replacement vdso. + * + * Copyright 2021 Linaro, Ltd. + *=20 + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +VERSION { + LINUX_2.6 { + global: + clock_gettime; + __vdso_clock_gettime; + gettimeofday; + __vdso_gettimeofday; + getcpu; + __vdso_getcpu; + time; + __vdso_time; + clock_getres; + __vdso_clock_getres; + + local: *; + }; +} + + +PHDRS { + phdr PT_PHDR FLAGS(4) PHDRS; + data PT_LOAD FLAGS(6) FILEHDR PHDRS; + text PT_LOAD FLAGS(5); + dynamic PT_DYNAMIC FLAGS(4); + eh_frame_hdr PT_GNU_EH_FRAME; + note PT_NOTE FLAGS(4); +} + +SECTIONS { + /* ??? We can't really prelink to any address without knowing + something about the virtual memory space of the host, since + that leaks over into the available memory space of the guest. = */ + . =3D SIZEOF_HEADERS; + + /* The following, including the FILEHDRS and PHDRS, are modified + when we relocate the binary. We want them to be initially + writable for the relocation; we'll force them read-only after. = */ + .note : { *(.note*) } :data :note + .dynamic : { *(.dynamic) } :data :dynamic + .dynsym : { *(.dynsym) } :data + .data : { + /* There ought not be any real read-write data. + But since we manipulated the segment layout, + we have to put these sections somewhere. */ + *(.data*) + *(.sdata*) + *(.got.plt) *(.got) + *(.gnu.linkonce.d.*) + *(.bss*) + *(.dynbss*) + *(.gnu.linkonce.b.*) + } + + .rodata : { *(.rodata*) } + .hash : { *(.hash) } + .gnu.hash : { *(.gnu.hash) } + .dynstr : { *(.dynstr) } + .gnu.version : { *(.gnu.version) } + .gnu.version_d : { *(.gnu.version_d) } + .gnu.version_r : { *(.gnu.version_r) } + .eh_frame_hdr : { *(.eh_frame_hdr) } :data :eh_frame_hdr + .eh_frame : { *(.eh_frame) } :data + + . =3D ALIGN(4096); + .text : { *(.text*) } :text =3D0x90909090 +} diff --git a/linux-user/x86_64/vdso.so b/linux-user/x86_64/vdso.so new file mode 100755 index 0000000000000000000000000000000000000000..0cdb8f1706893abf221e42d3443= d9f5d49fef080 GIT binary patch literal 5912 zcmeHL&ud&&6h4#5G>PWNw2COv8bm7;9TF)LD3qy9;>5Ng1_DtiuhYzHCzzShnU^w=3D zQbCLwDVjwW#YJgxFIg5hDiyK*1ybnF3T~t>3@+61eDlsXyt#d~g5c_%rNM0somwpb*kfKPhUU?gKao$oB`^^tD zV((?&WWo`m*nAMz9Q71#@Igx3FNf2SSj#;e&>c8vfA=3D^Zj7) ze_G$d<&O2g;6yC${|40ODXhl!o%PzU{2t}U9R3OO&pP}g=3DI`=3DbZP;tf zr!VXZV!4fIV+XKH@zz`N{pj}1?5CF(J{d2+e{|!gp^x{zd$D=3D> z++&{|seZKo+kN}CPx+Ol4_e}RXG@Kmx4c?ge#1NC2f<3kzl*H<4RYpEJ6$TSlZ(da z`0|@;vbs`TYd(+EIsKoIW3zLMOWq5)7j=3DF;v41+Cma@OAU;Z-mu=3DXFLNvD6KyFYQGu+a(zHotqOHKHn_uduNbWWN4+aC3O# ztLy6ePnOo-;MCz!zqI~3r+)3`(?T8G&G&Zna5tan=3D;CgEm&EDWv??X);BNo^j{fiF z2c!U@;8;4M|2PEA0ZrnrSAWK<-JKtLBTnz)H-DUtd{+559Sr?~Jg(k7X#TFgoHgF{ z&sm-^h!-_#nRRU=3DzpH~^GTzO9Lisp-jPnWbZXWTbUYk37eD;WH#Fyt5z3GBCvvBzM zv{#r~h=3D@T`pK+qngruuaBT_V0v$nOUdLB`2g0=3Dah{J6c^*c8TMoN=3D3M$fy+@IeUM6Q$hT|BDsI7=3DhX?-k;t z-HA1F=3Dl{BnV@`~}*SVvm>747ZUv}d8Jw-f6M??4is1NSX^ThT+27K)S!FwDvJ~!l| zb0-HpX^weY7|ybW^y2+lSDZib+0|ypuIve$=3DQ~W;aQAPw(sA*7?Hs##pZLE4>E9DK literal 0 HcmV?d00001 --=20 2.25.1 From nobody Wed May 1 23:49:41 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1624074964290321.07233242996404; Fri, 18 Jun 2021 20:56:04 -0700 (PDT) Received: from localhost ([::1]:52294 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1luS5T-0003OG-7I for importer2@patchew.org; Fri, 18 Jun 2021 23:56:03 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33130) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1luRte-0000G7-06 for qemu-devel@nongnu.org; Fri, 18 Jun 2021 23:43:50 -0400 Received: from mail-pf1-x42e.google.com ([2607:f8b0:4864:20::42e]:39466) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1luRtT-00083s-0P for qemu-devel@nongnu.org; Fri, 18 Jun 2021 23:43:49 -0400 Received: by mail-pf1-x42e.google.com with SMTP id g192so1535332pfb.6 for ; Fri, 18 Jun 2021 20:43:38 -0700 (PDT) Received: from localhost.localdomain ([71.212.149.176]) by smtp.gmail.com with ESMTPSA id z6sm10045154pgs.24.2021.06.18.20.43.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Jun 2021 20:43:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=kSp7KfgnjyVafFIoY3p7Wtj26SoZF+TYJt49SIfQL/o=; b=KVi5qQRhrgEDWhuK/xKMiaDbCXyel4gzapVmvNo+0y0YDXjZgXD3EPLhBIuJNsHMSu RPdyTTBKR3m+igs/+VylIlogfvOJq3HU0K9l/T56RRG71m+vTVemYLJgMrk3+DE45MRM Asm03Z7o5IP/ZN+D96MsuBTIy1ejIYeP80XACPwdTgQiPi+7RdbBFXBjQTTG7Y7fRjRy pISt1kb/eYkVJAxYYnr+Tmpxyio/dyYUJ4EO4ghJneVAech894EdlPVEAQZUbuKkhHtO 15UepEzQMA8SUhwaBThLRAM2UUUNDAqak0gdo6AoaqBjcQeaxoif/C+3DGDKrWWG+qv7 NmVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=kSp7KfgnjyVafFIoY3p7Wtj26SoZF+TYJt49SIfQL/o=; b=ZOC031IRECpqEeqGbqn7pE8qoQo4YW5x/WPON4R4ca+72kXPGnwVcA0ySmlOX/F3Rb Ca0JhyT+8282CePZ5gX1pT/9yB+0dei9IwcfsAOWXzwfvYmuZBuURqy3FrO9axMfrUtj JzK44fdGVkAx2CDSDrKPZa5G001H8nr+neAKbg1wW2foHyNa7El8mspm2sdi4HVoLhld tfg4K9Q0pF/PBNql13NcnE2Qfox9iI6gp1H445vjFYzGeaGQjflDB/G+BhkQOJOyBhOW vzVIB2jpCnQUXKaoBihj7ubjWF3fv7ll+/SAsVwGfQPYUiExdzJfT7T/ztNNrW4a1cTb mrEA== X-Gm-Message-State: AOAM532pYGrIwS9dQalfcmyiQKNbwfg98LEmblSAFGSx++ixfHr4U+UB 4nrImaC+plDDHcJ45BUhwksWtZGDcd9RDA== X-Google-Smtp-Source: ABdhPJztlzsvRBSGN796AGtt8dReIGXywGydszl2hnZfvJW0/MvXOB69GHek7lDhN+tVAyvY4A+22w== X-Received: by 2002:a63:ed17:: with SMTP id d23mr12903165pgi.107.1624074217674; Fri, 18 Jun 2021 20:43:37 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH 12/12] linux-user/hppa: Add vdso and use it for rt_sigreturn Date: Fri, 18 Jun 2021 20:43:29 -0700 Message-Id: <20210619034329.532318-13-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210619034329.532318-1-richard.henderson@linaro.org> References: <20210619034329.532318-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::42e; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x42e.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: alex.bennee@linaro.org, laurent@vivier.eu Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Building the vdso itself is not actually wired up to anything, since we require a cross-compiler. Just check in that file for now. Drop the now-unused 9 trampoline words, and describe the frame without the trampoline in __kernel_rt_sigreturn. Signed-off-by: Richard Henderson --- linux-user/elfload.c | 3 + linux-user/hppa/signal.c | 8 +- linux-user/hppa/Makefile.vdso | 4 + linux-user/hppa/meson.build | 6 ++ linux-user/hppa/vdso.S | 149 ++++++++++++++++++++++++++++++++++ linux-user/hppa/vdso.ld | 75 +++++++++++++++++ linux-user/hppa/vdso.so | Bin 0 -> 5192 bytes 7 files changed, 238 insertions(+), 7 deletions(-) create mode 100644 linux-user/hppa/Makefile.vdso create mode 100644 linux-user/hppa/vdso.S create mode 100644 linux-user/hppa/vdso.ld create mode 100755 linux-user/hppa/vdso.so diff --git a/linux-user/elfload.c b/linux-user/elfload.c index b70a5c48a2..6cea556b70 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -1457,6 +1457,9 @@ static inline void init_thread(struct target_pt_regs = *regs, #define STACK_GROWS_DOWN 0 #define STACK_ALIGNMENT 64 =20 +#define HAVE_VDSO 1 +#include "vdso.c.inc" + static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop) { diff --git a/linux-user/hppa/signal.c b/linux-user/hppa/signal.c index 0e266f472d..44e2db6d3e 100644 --- a/linux-user/hppa/signal.c +++ b/linux-user/hppa/signal.c @@ -40,7 +40,6 @@ struct target_ucontext { }; =20 struct target_rt_sigframe { - abi_uint tramp[9]; target_siginfo_t info; struct target_ucontext uc; /* hidden location of upper halves of pa2.0 64-bit gregs */ @@ -138,14 +137,9 @@ void setup_rt_frame(int sig, struct target_sigaction *= ka, =20 setup_sigcontext(&frame->uc.tuc_mcontext, env); =20 - __put_user(0x34190000, frame->tramp + 0); /* ldi 0,%r25 */ - __put_user(0x3414015a, frame->tramp + 1); /* ldi __NR_rt_sigreturn,%r2= 0 */ - __put_user(0xe4008200, frame->tramp + 2); /* be,l 0x100(%sr2,%r0) */ - __put_user(0x08000240, frame->tramp + 3); /* nop */ - unlock_user_struct(frame, frame_addr, 1); =20 - env->gr[2] =3D h2g(frame->tramp); + env->gr[2] =3D default_rt_sigreturn; env->gr[30] =3D sp; env->gr[26] =3D sig; env->gr[25] =3D h2g(&frame->info); diff --git a/linux-user/hppa/Makefile.vdso b/linux-user/hppa/Makefile.vdso new file mode 100644 index 0000000000..ce92d51a37 --- /dev/null +++ b/linux-user/hppa/Makefile.vdso @@ -0,0 +1,4 @@ +vdso.so: vdso.S vdso.ld Makefile.vdso + hppa-linux-gnu-gcc -nostdlib -shared -Wl,-T,vdso.ld \ + -Wl,-h,linux-vdso.so.1 -Wl,--build-id=3Dnone \ + -Wl,--hash-style=3Dboth vdso.S -o $@ diff --git a/linux-user/hppa/meson.build b/linux-user/hppa/meson.build index 4709508a09..3febe8523a 100644 --- a/linux-user/hppa/meson.build +++ b/linux-user/hppa/meson.build @@ -3,3 +3,9 @@ syscall_nr_generators +=3D { arguments: [ meson.current_source_dir() / 'syscallhdr.= sh', '@INPUT@', '@OUTPUT@', '@EXTRA_ARGS@' ], output: '@BASENAME@_nr.h') } + +gen =3D [ + gen_vdso.process('vdso.so') +] + +linux_user_ss.add(when: 'TARGET_HPPA', if_true: gen) diff --git a/linux-user/hppa/vdso.S b/linux-user/hppa/vdso.S new file mode 100644 index 0000000000..eeae2c999a --- /dev/null +++ b/linux-user/hppa/vdso.S @@ -0,0 +1,149 @@ +/* + * hppa linux kernel vdso replacement. + * + * Copyright 2021 Linaro, Ltd. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include + + .text + +#define sizeof_rt_sigframe 696 +#define offsetof_sigcontext 152 +#define offsetof_sigcontext_gr offsetof_sigcontext + 4 +#define offsetof_sigcontext_fr offsetof_sigcontext_gr + 32 * 4 +#define offsetof_sigcontext_iasq offsetof_sigcontext_fr + 32 * 8 +#define offsetof_sigcontext_iaoq offsetof_sigcontext_iasq + 8 +#define offsetof_sigcontext_sar offsetof_sigcontext_iaoq + 8 + + /* + * While this frame is marked as a signal frame, that only applies + * to how this return address is handled for the outer frame. + * The return address that arrived here, from the inner frame, is + * not marked as a signal frame and so the unwinder still tries to + * subtract 1 to examine the presumed call insn. Thus we must + * extend the unwind info to a nop before the start. + */ + + .cfi_startproc simple + .cfi_signal_frame + + /* Compare pa32_fallback_frame_state from libgcc. */ + + /* Record the size of the stack frame. */ + .cfi_def_cfa 30, -sizeof_rt_sigframe + + /* Record save offset of general registers. */ + .cfi_offset 1, offsetof_sigcontext_gr + 1 * 4 + .cfi_offset 2, offsetof_sigcontext_gr + 2 * 4 + .cfi_offset 3, offsetof_sigcontext_gr + 3 * 4 + .cfi_offset 4, offsetof_sigcontext_gr + 4 * 4 + .cfi_offset 5, offsetof_sigcontext_gr + 5 * 4 + .cfi_offset 6, offsetof_sigcontext_gr + 6 * 4 + .cfi_offset 7, offsetof_sigcontext_gr + 7 * 4 + .cfi_offset 8, offsetof_sigcontext_gr + 8 * 4 + .cfi_offset 9, offsetof_sigcontext_gr + 9 * 4 + .cfi_offset 10, offsetof_sigcontext_gr + 10 * 4 + .cfi_offset 11, offsetof_sigcontext_gr + 11 * 4 + .cfi_offset 12, offsetof_sigcontext_gr + 12 * 4 + .cfi_offset 13, offsetof_sigcontext_gr + 13 * 4 + .cfi_offset 14, offsetof_sigcontext_gr + 14 * 4 + .cfi_offset 15, offsetof_sigcontext_gr + 15 * 4 + .cfi_offset 16, offsetof_sigcontext_gr + 16 * 4 + .cfi_offset 17, offsetof_sigcontext_gr + 17 * 4 + .cfi_offset 18, offsetof_sigcontext_gr + 18 * 4 + .cfi_offset 19, offsetof_sigcontext_gr + 19 * 4 + .cfi_offset 20, offsetof_sigcontext_gr + 20 * 4 + .cfi_offset 21, offsetof_sigcontext_gr + 21 * 4 + .cfi_offset 22, offsetof_sigcontext_gr + 22 * 4 + .cfi_offset 23, offsetof_sigcontext_gr + 23 * 4 + .cfi_offset 24, offsetof_sigcontext_gr + 24 * 4 + .cfi_offset 25, offsetof_sigcontext_gr + 25 * 4 + .cfi_offset 26, offsetof_sigcontext_gr + 26 * 4 + .cfi_offset 27, offsetof_sigcontext_gr + 27 * 4 + .cfi_offset 28, offsetof_sigcontext_gr + 28 * 4 + .cfi_offset 29, offsetof_sigcontext_gr + 29 * 4 + .cfi_offset 30, offsetof_sigcontext_gr + 30 * 4 + .cfi_offset 31, offsetof_sigcontext_gr + 31 * 4 + + /* Record save offset of fp registers, left and right halves. */ + .cfi_offset 32, offsetof_sigcontext_fr + 4 * 8 + .cfi_offset 33, offsetof_sigcontext_fr + 4 * 8 + 4 + .cfi_offset 34, offsetof_sigcontext_fr + 5 * 8 + .cfi_offset 35, offsetof_sigcontext_fr + 5 * 8 + 4 + .cfi_offset 36, offsetof_sigcontext_fr + 6 * 8 + .cfi_offset 37, offsetof_sigcontext_fr + 6 * 8 + 4 + .cfi_offset 38, offsetof_sigcontext_fr + 7 * 8 + .cfi_offset 39, offsetof_sigcontext_fr + 7 * 8 + 4 + .cfi_offset 40, offsetof_sigcontext_fr + 8 * 8 + .cfi_offset 41, offsetof_sigcontext_fr + 8 * 8 + 4 + .cfi_offset 42, offsetof_sigcontext_fr + 9 * 8 + .cfi_offset 43, offsetof_sigcontext_fr + 9 * 8 + 4 + .cfi_offset 44, offsetof_sigcontext_fr + 10 * 8 + .cfi_offset 45, offsetof_sigcontext_fr + 10 * 8 + 4 + .cfi_offset 46, offsetof_sigcontext_fr + 11 * 8 + .cfi_offset 47, offsetof_sigcontext_fr + 11 * 8 + 4 + .cfi_offset 48, offsetof_sigcontext_fr + 12 * 8 + .cfi_offset 49, offsetof_sigcontext_fr + 12 * 8 + 4 + .cfi_offset 50, offsetof_sigcontext_fr + 13 * 8 + .cfi_offset 51, offsetof_sigcontext_fr + 13 * 8 + 4 + .cfi_offset 52, offsetof_sigcontext_fr + 14 * 8 + .cfi_offset 53, offsetof_sigcontext_fr + 14 * 8 + 4 + .cfi_offset 54, offsetof_sigcontext_fr + 15 * 8 + .cfi_offset 55, offsetof_sigcontext_fr + 15 * 8 + 4 + .cfi_offset 56, offsetof_sigcontext_fr + 16 * 8 + .cfi_offset 57, offsetof_sigcontext_fr + 16 * 8 + 4 + .cfi_offset 58, offsetof_sigcontext_fr + 17 * 8 + .cfi_offset 59, offsetof_sigcontext_fr + 17 * 8 + 4 + .cfi_offset 60, offsetof_sigcontext_fr + 18 * 8 + .cfi_offset 61, offsetof_sigcontext_fr + 18 * 8 + 4 + .cfi_offset 62, offsetof_sigcontext_fr + 19 * 8 + .cfi_offset 63, offsetof_sigcontext_fr + 19 * 8 + 4 + .cfi_offset 64, offsetof_sigcontext_fr + 20 * 8 + .cfi_offset 65, offsetof_sigcontext_fr + 20 * 8 + 4 + .cfi_offset 66, offsetof_sigcontext_fr + 21 * 8 + .cfi_offset 67, offsetof_sigcontext_fr + 21 * 8 + 4 + .cfi_offset 68, offsetof_sigcontext_fr + 22 * 8 + .cfi_offset 69, offsetof_sigcontext_fr + 22 * 8 + 4 + .cfi_offset 70, offsetof_sigcontext_fr + 23 * 8 + .cfi_offset 71, offsetof_sigcontext_fr + 23 * 8 + 4 + .cfi_offset 72, offsetof_sigcontext_fr + 24 * 8 + .cfi_offset 73, offsetof_sigcontext_fr + 24 * 8 + 4 + .cfi_offset 74, offsetof_sigcontext_fr + 25 * 8 + .cfi_offset 75, offsetof_sigcontext_fr + 25 * 8 + 4 + .cfi_offset 76, offsetof_sigcontext_fr + 26 * 8 + .cfi_offset 77, offsetof_sigcontext_fr + 26 * 8 + 4 + .cfi_offset 78, offsetof_sigcontext_fr + 27 * 8 + .cfi_offset 79, offsetof_sigcontext_fr + 27 * 8 + 4 + .cfi_offset 80, offsetof_sigcontext_fr + 28 * 8 + .cfi_offset 81, offsetof_sigcontext_fr + 28 * 8 + 4 + .cfi_offset 82, offsetof_sigcontext_fr + 29 * 8 + .cfi_offset 83, offsetof_sigcontext_fr + 29 * 8 + 4 + .cfi_offset 84, offsetof_sigcontext_fr + 30 * 8 + .cfi_offset 85, offsetof_sigcontext_fr + 30 * 8 + 4 + .cfi_offset 86, offsetof_sigcontext_fr + 31 * 8 + .cfi_offset 87, offsetof_sigcontext_fr + 31 * 8 + 4 + + /* Record save offset of %sar */ + .cfi_offset 88, offsetof_sigcontext_sar + + /* Record save offset of return address, iaoq[0]. */ + .cfi_return_column 89 + .cfi_offset 89, offsetof_sigcontext_iaoq + + nop + +__kernel_rt_sigreturn: + + ldi 0, %r25 + ldi __NR_rt_sigreturn, %r20 + be,l 0x100(%sr2, %r0), %sr0, %r31 + nop + + .cfi_endproc + + .size __kernel_rt_sigreturn, . - __kernel_rt_sigreturn + .type __kernel_rt_sigreturn, @function + .globl __kernel_rt_sigreturn diff --git a/linux-user/hppa/vdso.ld b/linux-user/hppa/vdso.ld new file mode 100644 index 0000000000..fed994c3eb --- /dev/null +++ b/linux-user/hppa/vdso.ld @@ -0,0 +1,75 @@ +/* + * Linker script for linux hppa vdso. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see . + */ + +/* + * Note that the kernel does not implement a vdso for hppa. + * Mirror the symbol that other targets use for this, e.g. i386. + */ + +VERSION { + QEMU { + global: + __kernel_rt_sigreturn; + local: *; + }; +} + + +PHDRS { + phdr PT_PHDR FLAGS(4) PHDRS; + data PT_LOAD FLAGS(6) FILEHDR PHDRS; + text PT_LOAD FLAGS(5); + dynamic PT_DYNAMIC FLAGS(4); + note PT_NOTE FLAGS(4); + eh_frame_hdr PT_GNU_EH_FRAME; +} + +SECTIONS { + . =3D SIZEOF_HEADERS; + + /* The following, including the FILEHDRS and PHDRS, are modified + when we relocate the binary. We want them to be initially + writable for the relocation; we'll force them read-only after. = */ + .note : { *(.note*) } :data :note + .dynamic : { *(.dynamic) } :data :dynamic + .dynsym : { *(.dynsym) } :data + .data : { + /* There ought not be any real read-write data. + But since we manipulated the segment layout, + we have to put these sections somewhere. */ + *(.data*) + *(.sdata*) + *(.got.plt) *(.got) + *(.gnu.linkonce.d.*) + *(.bss*) + *(.dynbss*) + *(.gnu.linkonce.b.*) + } + + .rodata : { *(.rodata) } + .hash : { *(.hash) } + .gnu.hash : { *(.gnu.hash) } + .dynstr : { *(.dynstr) } + .gnu.version : { *(.gnu.version) } + .gnu.version_d : { *(.gnu.version_d) } + .gnu.version_r : { *(.gnu.version_r) } + .eh_frame_hdr : { *(.eh_frame_hdr) } :data :eh_frame_hdr + .eh_frame : { *(.eh_frame) } :data + + . =3D ALIGN(4096); + .text : { *(.text*) } :text +} diff --git a/linux-user/hppa/vdso.so b/linux-user/hppa/vdso.so new file mode 100755 index 0000000000000000000000000000000000000000..a314dc70f2ae8548bfb1e413703= 9f983fad094e6 GIT binary patch literal 5192 zcmeHLS#MiK6rOQ1r7WRN6$wxP@lZs8)^rJ_&;oH{J1IDuleA3;+`6?Jnt|BMun=3DUf z({)XK>e{Y>LRQ8OLH}(A?}JXZ^(p5f9nW|_wqwtIW!AM>->&BwF7a(SH8lO#?>O>z zubECkC`O{}ndlBTH@P#0;CUiOU|4P@`0@Y&rSLnovdk!!3 z_drZY#FuwQoGj5to06=3DKX~>|#9zSsM?2*!;pM~RW%esZ^fa7{jG2icIy%Dz{2J(fG z($djPaVUbn<)S0r(#>@tQ&FO#tor5jarlTM&r1CKFNF|Qy;s9xjHJ&YYb(}3CvMnJ z)d@dY{KN#gJ=3DG~HD|=3D+^!YjX@*tvM}+FjS}zJAY=3Dy-OdC?7LzAvIENxu6ShSqjitn zcxct(n~to0{N^Wad2-EDYoEULncJSd{ki(*qc1cZZG18IQvBt_v9{ywuXMbcI??%B z*X!MHtbcRE$&GLIyxn{1u6O$0z5Bg;-giFO^kMp=3D%~M-G&U}*nbn9pRpXa{Fe|hg$ z+rA$7X7F@D)eouYuxhwZHM%NRRB=3DxwMpVvqFh_8{oKZr>|Tg***_T&KUhxL5SA=3DJ#E@)Z5B zIhTUu6n|lGeg*0Inl@%^ZDTIdalR#h{P`L2^NhWS683y!_ZjuHU)tZT&Z+|=3D6b#G9Q&D%uiv znqw*9G$q>_qDiMMk?4$fIbG3)WZd!R=3Deb915h;!hdg;xOp6kn;)iPs+^k9CAh(JLW zWZFxMNPl4@l1mqJzTb0w&uG>y=3D7$Qi3@0NZ*_^Z0O%G;dcOPl`(#KllG&(UfvNL#NO`pi5R_QXH){sy-o1nd9+ literal 0 HcmV?d00001 --=20 2.25.1