From patchwork Sun Aug 6 03:37:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 710870 Delivered-To: patch@linaro.org Received: by 2002:a05:6359:d30:b0:129:c516:61db with SMTP id gp48csp826079rwb; Sat, 5 Aug 2023 20:38:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEImUXlFh8D0Onvds2xLTTnXKDDsDOATNKx/dFpfoEhBuhKQbsl4J5Zzpe3bslJ0Tn/qZx8 X-Received: by 2002:ac8:5bd0:0:b0:403:b382:613f with SMTP id b16-20020ac85bd0000000b00403b382613fmr6704028qtb.44.1691293128512; Sat, 05 Aug 2023 20:38:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691293128; cv=none; d=google.com; s=arc-20160816; b=hEXM19PBjgN5wc/Bjr4mZ3iiL0ecDoIwIJ9CMpdv0FzYDhl/i0YnTvoYYescOOEsGd NsMCzD2Q43iozT1VuJPvr/Iq5pRMJtY7LWOicgC70ECFCoW7riTSU3O2rYVf7j3c9h1w 5dY3zMm2biPMGqagk83n6C2br3QUI9s69QKZbO5pATVI6zT2ZUwIyLNuQbscIrGGdHgg 4ZtwY5LXz7SHX1EvPunpxkC1/wnQz9sLlW1XEgMoc6U8LwwaFvdI+5XdJsgsu5drT3b+ ZEndMKHTFGesQSJzF+WbDSHg0j7Qf6FcXkFikSUguHzA/tFh8+frI+ft0I7J4+O9h5K2 GQFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to: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:cc:to :from:dkim-signature; bh=gDTWs7pBzjEM4yDIiLCCxRn3NjvKLf/PjlJHIm+hkqo=; fh=e1NRMjxVMtV0F5wYJj2s+gr60lsrqkDmE/ulJ4pjdtQ=; b=NsW37xHVi4o78UKAeR8N66UUHWz4TygJJtdbe1sSKW/yhCe2udja14/7oodLU+96Rv IDqDGmb7DMsXcWBckw5tCYTl8YzAte9zIH7HhC0dV/p6c6jw57o06J6BZTjIo5u8rfwp RjTyN0VK1Y+/IUly2yVjByQ+YYDr/zCq57OA+FaBEjpdEOUP8uBXLcaHF2cpWextv6Wu cyea5ZzRQZ7oWAHukXB4FYdH1WUwTfrJVIKlRGE0d1K7WNkDD1GkPWn9foNRJmm5Ixcn GBXd68InZQFSVocHORMEwr0vLRmjKyemW1+B9ZhpeyCYeXSAOxxj+hRZkpUAzv9DZ73o JTIw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=efVtzpR1; 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=pass (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 i18-20020ac85c12000000b0040fd1f4aa51si3358993qti.48.2023.08.05.20.38.48 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sat, 05 Aug 2023 20:38:48 -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=pass header.i=@linaro.org header.s=google header.b=efVtzpR1; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qSUaF-0003iS-LB; Sat, 05 Aug 2023 23:37:35 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qSUaD-0003gg-85 for qemu-devel@nongnu.org; Sat, 05 Aug 2023 23:37:33 -0400 Received: from mail-pj1-x1031.google.com ([2607:f8b0:4864:20::1031]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qSUaB-0007Zq-Ki for qemu-devel@nongnu.org; Sat, 05 Aug 2023 23:37:33 -0400 Received: by mail-pj1-x1031.google.com with SMTP id 98e67ed59e1d1-2680eee423aso1672764a91.2 for ; Sat, 05 Aug 2023 20:37:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691293050; x=1691897850; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=gDTWs7pBzjEM4yDIiLCCxRn3NjvKLf/PjlJHIm+hkqo=; b=efVtzpR1l/tn9lMq2lyqAy5nYGl8zods7/8Z8/Z+GtAMM96FLCvR2qZ5gwmniUr8L0 IoA9WzzZkrVlqeDeLg36DU/86aI3/hatHyIX5fwydciy0ifkSqg8Y2ZfbqX0a4VzyD8R l4s2qvgrvAij9XwDjo5vT+yLARk0GI+FYRXknL5nW6bNCbi1LXGPKCGcCCKqu62XqtQk tcibedTwoejHta5tQvNrmFIUaRlQnJISrC9rF9UGDEEf08FQMx688mdlaVoQ+LEtRUdG cH5mI6k6QCEJZ7KK/LKnj6pDS1iCLMXpKmwqXbxnJRZyl6pssKLSHoXMdlmQT3apOO19 IS6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691293050; x=1691897850; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gDTWs7pBzjEM4yDIiLCCxRn3NjvKLf/PjlJHIm+hkqo=; b=Cxh8wE8t7X+H9gbtC1jbFgXJEhujHq4/WWDE2YiLRuCrvKU7872Bsg8ERX3CTrMzMD thZxLExm5Kp7mpvPKZGLN6MbuI9Bw7GqbBnkcU130vGQF+Ct+Q8GjU4ZWGdP+RPPo5X8 dTjL/GGPl/QtbRwE0aVDzj33Smyvgu4TcZD3pXgorKQavi4HzlWxCdg1Cb3yspD0bMPr oBtUTXvj207Bn6zTt0PAq+Ciu0Z6aFrDBvaxkOOmlEIhpghcMqO+Dwgpu+WwVpKkbHPo CbxNH3AHGgcfE+wfI/yCg9mX39tB6HYJRrZUrTvx1uIZsZG1EBPGezspWtXzVFRWLUx9 VEXw== X-Gm-Message-State: AOJu0Ywjx834d3nlxKedh63kOntjeF754HffCa4oE9UwiTOOrLRCo/mr ljR4Vpw0kvRsM3G5tbWt9UQjgSmLG/gy/pauEtQ= X-Received: by 2002:a17:90a:d30b:b0:268:553f:1938 with SMTP id p11-20020a17090ad30b00b00268553f1938mr4372119pju.4.1691293050478; Sat, 05 Aug 2023 20:37:30 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:9454:a46f:1c22:a7c6]) by smtp.gmail.com with ESMTPSA id a5-20020a17090a740500b00262e604724dsm6306451pjg.50.2023.08.05.20.37.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Aug 2023 20:37:30 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: Helge Deller , Akihiko Odaki Subject: [PULL 17/24] linux-user: Use elf_et_dyn_base for ET_DYN with interpreter Date: Sat, 5 Aug 2023 20:37:08 -0700 Message-Id: <20230806033715.244648-18-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230806033715.244648-1-richard.henderson@linaro.org> References: <20230806033715.244648-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::1031; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x1031.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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Follow the lead of the linux kernel in fs/binfmt_elf.c, in which an ET_DYN executable which uses an interpreter (usually a PIE executable) is loaded away from where the interpreter itself will be loaded. Tested-by: Helge Deller Reviewed-by: Helge Deller Reviewed-by: Akihiko Odaki Signed-off-by: Richard Henderson --- linux-user/elfload.c | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 0c64aad8a5..a3aa08a13e 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -3106,6 +3106,8 @@ static void load_elf_image(const char *image_name, int image_fd, } } + load_addr = loaddr; + if (pinterp_name != NULL) { /* * This is the main executable. @@ -3135,11 +3137,32 @@ static void load_elf_image(const char *image_name, int image_fd, */ probe_guest_base(image_name, loaddr, hiaddr); } else { + abi_ulong align; + /* * The binary is dynamic, but we still need to * select guest_base. In this case we pass a size. */ probe_guest_base(image_name, 0, hiaddr - loaddr); + + /* + * Avoid collision with the loader by providing a different + * default load address. + */ + load_addr += elf_et_dyn_base; + + /* + * TODO: Better support for mmap alignment is desirable. + * Since we do not have complete control over the guest + * address space, we prefer the kernel to choose some address + * rather than force the use of LOAD_ADDR via MAP_FIXED. + * But without MAP_FIXED we cannot guarantee alignment, + * only suggest it. + */ + align = pow2ceil(info->alignment); + if (align) { + load_addr &= -align; + } } } @@ -3154,13 +3177,13 @@ static void load_elf_image(const char *image_name, int image_fd, * * Otherwise this is ET_DYN, and we are searching for a location * that can hold the memory space required. If the image is - * pre-linked, LOADDR will be non-zero, and the kernel should + * pre-linked, LOAD_ADDR will be non-zero, and the kernel should * honor that address if it happens to be free. * * In both cases, we will overwrite pages in this range with mappings * from the executable. */ - load_addr = target_mmap(loaddr, (size_t)hiaddr - loaddr + 1, PROT_NONE, + load_addr = target_mmap(load_addr, (size_t)hiaddr - loaddr + 1, PROT_NONE, MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | (ehdr->e_type == ET_EXEC ? MAP_FIXED_NOREPLACE : 0), -1, 0);