From patchwork Tue Jul 6 23:49:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 470614 Delivered-To: patch@linaro.org Received: by 2002:a17:906:971a:0:0:0:0 with SMTP id k26csp5551957ejx; Tue, 6 Jul 2021 17:03:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwk3+82nhoMtJe9WIIr64NdzBmHczP6uTbJFWmPDuRR1SxKJQcgVXGyEu2UDUEZVU5L7/mA X-Received: by 2002:aa7:c1c7:: with SMTP id d7mr26465408edp.183.1625616191200; Tue, 06 Jul 2021 17:03:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625616191; cv=none; d=google.com; s=arc-20160816; b=OsK/Buqi71INvKaXBpHcEsjC2PmrQekdYqfivFk44kxsHxoAto0COTm3U+Ngo80JYB YNkkUih4WOOln2+BHR146Fti007q5CKDW+qOz0JFvGgQ50MOsHgV/hj42lbkXKo1Ssei gbLUIs0cxO6QJP0llrqkC4r14S8P/mue1cIdfduzYpqeDxmudTcibSjJeiItbE9lIxwI IwvgnIjUXmtls0cWEXFTg0NaYkBL31xlV5ghOR5yHp6MC5YOVcW10CdJrnU4QDvwHG9A 4oB7nF7YR1o72K+s3E32PLyNugDPAmLVXpz/u8TWPquPSFrEsM/LsgPDFKoQud8FMBde GKwg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=tFcDGdeVO1+L5j0MeCoRvjHxmx8xNNu9D1iWMQlVWww=; b=cY6RUau8fj+yvY1ucRlN9Q3v3bZSO3HqBkFEaeRG4Vwaj6DwAZSgXEUngdmPUiBUsI KQtWBYTQ7KzgTeg9KFPAcDIwk9j0wVxy2et4t1vTh1tL7XF54FRw3kbbga+NKCn0gLhp 4LhhfSh6F5sQns4+5mR5XYwtvXGAFAYWlZthgqyipdWHMNCifOxqJvWKf2hm4u4t2dty LbyxYS30+HOwuGvMjJGt0BMX1qY9+vkN8Yo9WdPdySQYEqBuJY/NFYNhwdDanjx7uRkx Q/rT1K45BXcTk0CoTdDErKC8aT/h+1PNkHDQ5E44fw+n7yP1OHwxwO+ONKiBpeKeRpU0 NGkA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=kPFvBsX4; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id r21si18983751ejo.665.2021.07.06.17.03.11 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 06 Jul 2021 17:03:11 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=kPFvBsX4; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:50462 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m0v1y-0003yj-4G for patch@linaro.org; Tue, 06 Jul 2021 20:03:10 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:52966) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m0uox-0000tz-ML for qemu-devel@nongnu.org; Tue, 06 Jul 2021 19:49:43 -0400 Received: from mail-pg1-x534.google.com ([2607:f8b0:4864:20::534]:39933) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1m0uov-0006ca-KW for qemu-devel@nongnu.org; Tue, 06 Jul 2021 19:49:43 -0400 Received: by mail-pg1-x534.google.com with SMTP id a2so402871pgi.6 for ; Tue, 06 Jul 2021 16:49:41 -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=tFcDGdeVO1+L5j0MeCoRvjHxmx8xNNu9D1iWMQlVWww=; b=kPFvBsX4P4djFhjN+9L3FbeHGQaxNd7Q2ogpgUdUoPht39Yg38NMmCCRT9B8Yh1Bfh sy9Q1PhWwF4nOQUOPQBgryZ5aY+G6Le249ytuaauDfU5t2aDa/YxHOWGy3atgSH8fLv+ iesp/LvqHxsrSUF6uKbfBYVcYrgmIDiqysnJGcTardUHztkwNalZ56WhG6XL0g6+A2Al NIQz2HB8hH90mbr6PVD1nS2y+88WiAw88rat05Xu+h3B0/O57TNZJTkwBpf127C5FcLw 3Qaao70vQZbvJR1QOhKqMQsxA51Cw8lOtTAHc/TU4NxornpIK2loLRi6bZmHQyCqUNtp dYMA== 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=tFcDGdeVO1+L5j0MeCoRvjHxmx8xNNu9D1iWMQlVWww=; b=PzSRnQY6NNt41G/JRv41kvOO1HQOvcuYgrXWQM9ThXMFU7Ym0Uy1GUt0gHGYiT7nCM za9l5Re2ysUCbM4VYst9ywsfexO9Zzi4mDtjX2aDvQUVyYcasxMpZoK3tk3wmUTPqT8S N/yXrgd0Hb7YAHwNLR8HUrfEKcMpcyloU9A4Hx2gjYzGwg7/JjjpZAt3K4WXYzE6D9kS U76vDDq5Nux+qxoe907iHH9yp59sSWR+q4FbO5MGbVO7zBqiOKKtad4U7xfyfqXBUsT0 f0MCiisHlS0K2A84LvJP7zU4ebupmhvWLJGViBRRIh/Ul2+y1xGHUNMrRwq5nZeCXVNE fzoQ== X-Gm-Message-State: AOAM532cA5a77srbkmt4GqcWhl1geMfFMJQZ+DQR9ePmZIJKo1tUqCkx 7pRhh9pPP6/9LT9e2VTp7pfM+66+3gdHuw== X-Received: by 2002:a62:3344:0:b029:24c:735c:4546 with SMTP id z65-20020a6233440000b029024c735c4546mr22734657pfz.1.1625615380380; Tue, 06 Jul 2021 16:49:40 -0700 (PDT) Received: from localhost.localdomain ([71.212.149.176]) by smtp.gmail.com with ESMTPSA id b3sm18058114pfi.179.2021.07.06.16.49.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jul 2021 16:49:40 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH v2 10/36] linux-user: Load vdso image if available Date: Tue, 6 Jul 2021 16:49:06 -0700 Message-Id: <20210706234932.356913-11-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210706234932.356913-1-richard.henderson@linaro.org> References: <20210706234932.356913-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::534; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x534.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+patch=linaro.org@nongnu.org Sender: "Qemu-devel" 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 | 80 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 72 insertions(+), 8 deletions(-) -- 2.25.1 diff --git a/linux-user/elfload.c b/linux-user/elfload.c index ec4b5f9d54..f76281fe40 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -30,6 +30,15 @@ #define TARGET_ARCH_HAS_SIGTRAMP_PAGE 0 #endif +typedef struct { + const uint8_t *image; + const uint32_t *relocs; + unsigned image_size; + unsigned reloc_count; + unsigned sigreturn_ofs; + unsigned rt_sigreturn_ofs; +} VdsoImageInfo; + #define ELF_OSABI ELFOSABI_SYSV /* from personality.h */ @@ -1971,7 +1980,8 @@ static abi_ulong loader_build_fdpic_loadmap(struct image_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; @@ -2043,8 +2053,12 @@ static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc, } size = (DLINFO_ITEMS + 1) * 2; - if (k_platform) + if (k_platform) { size += 2; + } + if (vdso_info) { + size += 2; + } #ifdef DLINFO_ARCH_ITEMS size += DLINFO_ARCH_ITEMS * 2; #endif @@ -2121,6 +2135,9 @@ 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_EHDR, vdso_info->load_addr); + } NEW_AUX_ENT (AT_NULL, 0); #undef NEW_AUX_ENT @@ -2947,6 +2964,49 @@ static void load_elf_interp(const char *filename, struct image_info *info, load_elf_image(filename, &src, info, &ehdr, NULL); } +#ifndef vdso_image_info +#define vdso_image_info() NULL +#endif + +static void load_elf_vdso(struct image_info *info, const VdsoImageInfo *vdso) +{ + ImageSource src; + struct elfhdr ehdr; + abi_ulong load_bias, load_addr; + + src.fd = -1; + src.cache = vdso->image; + src.cache_size = vdso->image_size; + + load_elf_image("", &src, info, &ehdr, NULL); + load_addr = info->load_addr; + load_bias = 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 (unsigned i = 0, n = vdso->reloc_count; i < n; i++) { + abi_ulong *addr = g2h_untagged(load_addr + vdso->relocs[i]); + *addr = tswapal(tswapal(*addr) + load_bias); + } + + /* Install signal trampolines, if present. */ + if (vdso->sigreturn_ofs) { + default_sigreturn = load_addr + vdso->sigreturn_ofs; + } + if (vdso->rt_sigreturn_ofs) { + default_rt_sigreturn = load_addr + vdso->rt_sigreturn_ofs; + } + + /* 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 = *(target_ulong *)s0; @@ -3151,7 +3211,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 = NULL; char *scratch; @@ -3221,10 +3281,13 @@ int load_elf_binary(struct linux_binprm *bprm, struct image_info *info) } /* - * 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 the + * signal trampolines. Otherwise, allocate a separate page for them. */ - if (TARGET_ARCH_HAS_SIGTRAMP_PAGE) { + const VdsoImageInfo *vdso = vdso_image_info(); + if (vdso) { + load_elf_vdso(&vdso_info, vdso); + } else if (TARGET_ARCH_HAS_SIGTRAMP_PAGE) { abi_ulong tramp_page = target_mmap(0, TARGET_PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); @@ -3232,8 +3295,9 @@ int load_elf_binary(struct linux_binprm *bprm, struct image_info *info) target_mprotect(tramp_page, TARGET_PAGE_SIZE, PROT_READ | PROT_EXEC); } - bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &ehdr, - info, (elf_interpreter ? &interp_info : NULL)); + bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &ehdr, info, + elf_interpreter ? &interp_info : NULL, + vdso ? &vdso_info : NULL); info->start_stack = bprm->p; /* If we have an interpreter, set that as the program's entry point.