From patchwork Fri Aug 4 01:45:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 710223 Delivered-To: patch@linaro.org Received: by 2002:a05:6359:606:b0:129:c516:61db with SMTP id eh6csp233362rwb; Thu, 3 Aug 2023 18:48:12 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHEIGS9bEia1UaPefj7SpXtJsVaHnPQT3lZMhckUYXq8C1JDCSMTfCepGFiNlqTL6eXGzbt X-Received: by 2002:a37:93c4:0:b0:76c:cac4:5560 with SMTP id v187-20020a3793c4000000b0076ccac45560mr358673qkd.52.1691113692406; Thu, 03 Aug 2023 18:48:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691113692; cv=none; d=google.com; s=arc-20160816; b=ZlVPK1UdPoepR2wD9evfcr4+8u5OkNwc7ccJQEU0A/l5BmTTuF0Kyu0Iodndppt9CI rhuafy3HjUg96P39s/VSpNuTBB9j6VG4pDTliCZ7Yv3MRMzi6gkMj3ZGaLY8s0cS8DFO b9cXJFL+Uur4VdA5OzWo0sMTw4Iq1BAFPlmxzfVKXFS4sKGj9F7hHKZTeaDjgoB1Ct7I TTm4224hkwOPw/clzOf8YXUoPl3RjzXdFkx1yyIB3VyuMdLBOkpfkNmmZF3FpvQs+O8K X7PUuZ2FnSThppr55RDciqqu46Xbzbi8ekzHPDvUg5tVC8ao5TBPzmlXBY7bIKhIhpMQ si+A== 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=SHlFA2vEENUlTkxsWGE73YfmKuW2ikvOlFn8aOT1vJk=; fh=oY0Hqs0mb1Sbw8XbQT7Frqsb6/GXc1LASz4nvWEMVJQ=; b=EE9y5YeTVWq6iJ2pmVeuxdNHLbckkzUmBiISPEx70FNoVrdlSIH74P8YHwkK8KDCAp faerRYIirA1B6LchDNkBuCXy/tq6CZ7GFwPaWDYav5S/fhiS2M9N9EQ63G4uQaMWHgEy dA5rsSzhx+tjUYgJ/5C4rcttJbJLcVB6bmwYIY1ToyjW244IlTtvZtsa8RHT2t0UJpQh sMHK1JGy2bLMxCby1STn4wbzu2XKdp3lvvX5Q9ItAqMcgGj8KaLxfxhpfa8dNsHVN7qA DaFbqWP7OZUkIk8Iz3F8B8m6H0HiG9EzrG+++qkIEzVKBGqFbJ9Op3shcvMt1CgN2Yc9 hkng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=nCZYn1C3; 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 bk35-20020a05620a1a2300b0076ce5b9f0f6si603336qkb.472.2023.08.03.18.48.12 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 03 Aug 2023 18:48:12 -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=nCZYn1C3; 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 1qRjst-0002ga-Jp; Thu, 03 Aug 2023 21:45:43 -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 1qRjsk-0002d3-NS for qemu-devel@nongnu.org; Thu, 03 Aug 2023 21:45:38 -0400 Received: from mail-pf1-x42b.google.com ([2607:f8b0:4864:20::42b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qRjsg-0005Aj-6v for qemu-devel@nongnu.org; Thu, 03 Aug 2023 21:45:31 -0400 Received: by mail-pf1-x42b.google.com with SMTP id d2e1a72fcca58-686b643df5dso1190679b3a.1 for ; Thu, 03 Aug 2023 18:45:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691113528; x=1691718328; 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=SHlFA2vEENUlTkxsWGE73YfmKuW2ikvOlFn8aOT1vJk=; b=nCZYn1C3NhI2n5o28vX8ZWHhYzmqp00AbepRBHDyp3W52uqlaPIn982+3Ejaiqx+aK S1m5bWXDZPvZqYjfNckLCZrOjSwE7gpAkVrUg8eCrg1AC2qcKnD9FfZaTWq+h7M8BVHQ GU3fZ/0CNnprDeU3OglKGJP5E278xqh6aGKEkj8iNETcSZyWKNx2RQD0nLcVKl3Fd4mf vFTv/3bHiwzq+ufxKEQeWjUr7KCH+59Ahgm0EPkrGi8XrBDrK6e+weS5fjoXpWlkpHer Q71653k35/ZZZnFFvtrjPaLVmwEp5LWSrje0jYtlmK6583KcVped5WSfpsoqYhbL2aPZ 0m8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691113528; x=1691718328; 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=SHlFA2vEENUlTkxsWGE73YfmKuW2ikvOlFn8aOT1vJk=; b=khzWLTiRqyRpNZWoGcDRIkLl1azeeClMoHkn4+gvGV0G71p7kLfhGtB9Za1QL4ndCv lycoR7l2woiDCOZqNsRyJ4KqVDfpPk1lk9jh9m1BcPkrPARsPwWt7b8/my23h6IFjVrX Z3jh9RQud6aJhyVd6mjrRSNycigDuEyVlTyCGLwTs15QTxcOLn80u9lN1V1T5CG8roY5 jepf0RKvR1sxMD1UTOkBMPu+9h2RlaXmsHsjGQVpo39VW6O20TL7+RWYkJAdByQrn01j dwX6XOYhdBIBNYJtpIH6B5C19Ar1ZXZloF2b1UugQppVedsR8pdPisMluUMe//JdtrIp 5RiQ== X-Gm-Message-State: AOJu0Yx3yTN9UYsSwKoaQA+hBQIhkYSBcSrwBGiz67UuZL/pu8UX39ZM 7c1+z3KdvYAAv3z+XiebPou3iD92sSWCsLuG+HY= X-Received: by 2002:a05:6a00:2da7:b0:687:596e:fa72 with SMTP id fb39-20020a056a002da700b00687596efa72mr402303pfb.5.1691113528068; Thu, 03 Aug 2023 18:45:28 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:9d52:4fe8:10ce:5c3]) by smtp.gmail.com with ESMTPSA id c5-20020aa78c05000000b0066f37665a63sm456138pfd.73.2023.08.03.18.45.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Aug 2023 18:45:27 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: joel@jms.id.au, akihiko.odaki@daynix.com, laurent@vivier.eu, deller@gmx.de Subject: [PATCH v8 12/17] linux-user: Use elf_et_dyn_base for ET_DYN with interpreter Date: Thu, 3 Aug 2023 18:45:12 -0700 Message-Id: <20230804014517.6361-13-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230804014517.6361-1-richard.henderson@linaro.org> References: <20230804014517.6361-1-richard.henderson@linaro.org> MIME-Version: 1.0 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, T_SCC_BODY_TEXT_LINE=-0.01 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. Signed-off-by: Richard Henderson Reviewed-by: Akihiko Odaki Reviewed-by: Helge Deller --- 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);