From patchwork Tue Aug 8 21:08:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 711584 Delivered-To: patch@linaro.org Received: by 2002:ab3:63c7:0:b0:238:c968:2573 with SMTP id m7csp2882303ltf; Tue, 8 Aug 2023 14:11:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGYDwjpsMSTBOLScbfqOjJtVuDY/FIKwXsoUK5NKCnHqEapkDmwsnuX+RiX546zgG1XuD8j X-Received: by 2002:a05:620a:4103:b0:767:22eb:6ed7 with SMTP id j3-20020a05620a410300b0076722eb6ed7mr1105412qko.4.1691529079210; Tue, 08 Aug 2023 14:11:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691529079; cv=none; d=google.com; s=arc-20160816; b=WeN38/o27Fl2k1JMDgdRJJXrEbg5oqC1BIKPz7yyQQUn92+2K1p+NMAETpNFAOSqod osNbfsOYpRaeL+SonwC6idVoUw+9Y6y42/eFbh6DS5NcT7Y4R6vw+PL6QWBN/xf6c6/e v/9n6EuL0ji3qjrxhbwqfEI35BgHOZE1kPF0KsoAWhy86AE65fJqTYXJZi7AMdFxPLk4 6NONJnkV6+GMf8FPr6/S9gdfOw0bRAkXk8cUFgJMlCk+RGV31yz+XiyOmhuIzyxRwTm0 DRN2E9I6wDyEJqHZMtF33GzB7mDfU3/v+P1K+s4SZpj28qmFCiskoxYYcF26UA/87MX3 /URw== 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=gdGMnmtdSmtXaxIRFinY8s6zOHM1AhXo+8V5ig/K9Os=; fh=e1NRMjxVMtV0F5wYJj2s+gr60lsrqkDmE/ulJ4pjdtQ=; b=TsSExPTU+HzHli3LKxkpUj2czJ+gQhJYes5+4R7DQ25YawMNiInFrj/bTB9cvqddRg RbRJWBDkZOga57uc7HbcO8BCdcHyNaa84vJqSdC0kiYlPPq4Ff8X02F9CAOq/caYxvGA vN+xQ/W3Gcy3NYKjz9BetUIXoBgKhWOEibmKnJQntqb3E5oVPOgirl6rIOuqU4gbouTY /fXxHm6PsC4o6VEJIghiUz0mOCCC4vRcNPM+geJvbwMBkqr08AVk4MiwJGhC6659GXIe ZHpDYP+ZpbZJZCBmUl+wmeFHpnc0XmHGOG3NlHA9Tqu13fxMwQLmWxfQnsBTDINZPgU3 BxNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=UjDpVlIi; 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 s8-20020a05620a29c800b0076d042d4d09si3760536qkp.751.2023.08.08.14.11.18 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 08 Aug 2023 14:11:19 -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=UjDpVlIi; 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 1qTTwx-0007gw-I1; Tue, 08 Aug 2023 17:09:07 -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 1qTTwv-0007fq-KA for qemu-devel@nongnu.org; Tue, 08 Aug 2023 17:09:05 -0400 Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qTTwt-0005w6-Tj for qemu-devel@nongnu.org; Tue, 08 Aug 2023 17:09:05 -0400 Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-1bb7b8390e8so39599115ad.2 for ; Tue, 08 Aug 2023 14:09:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691528942; x=1692133742; 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=gdGMnmtdSmtXaxIRFinY8s6zOHM1AhXo+8V5ig/K9Os=; b=UjDpVlIiYsF6ilISjjrc5o9WMy5YabFGkw6/jtCyrTgMj+Y8zgEmJQRo9IhYNigZjo cvPypdt49XcwHtrW47BfZm3DQpPpP+MCvdJL1bJSPXmx5mtHI3BVXgL0HMOx4dackIKR mjSuPV8F2vE8986LG5SdTuIWehi82/cIu8k0qRp8wDqaDj6sQWBlrMTK8oVhma9+5KOL 974UXP/culj2J2EXY50eFV1ck6yMfzzwIKlBiJUDxgeYfULWVtxzu0XSMkIc0l0H64T/ 20532FQ919OK7RU/NdJCJiUJsnx5C7HA6Fylbvnanj6rMVqlOJpbHNK/H8hE9m7Kn8pk e5Xg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691528942; x=1692133742; 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=gdGMnmtdSmtXaxIRFinY8s6zOHM1AhXo+8V5ig/K9Os=; b=fET6X+09IOsxZl6YstMWGD5euhjXq6SlSwdrjhefSeOHcmjCdjsZ2ZnL5qayhbffSn T3Slzef1i1YvI5SON47DHSqFZo3mZYu2P7nFIPRAqnGyPfR4pFE0O2NV4DJXFY1+RtWU UVakTZx9SdXOGF1vPrvr6Q8qSEoglIZO+D7ctXJXMjrkCPTOCRcOlnNoDz2wLjGRT+UK 5an1EFbrtuDGZveX0pQRUMcf4WcFAQbrks/+X0jHvUyWudaANLidShpHMB+Xq9zgqSN3 hLIzeG83Gfa3qRSNGTlUMrfwOKrd2zajikEPeiSmNjM2ijXkRU4oNJo4YVsCoLtfz51T H84g== X-Gm-Message-State: AOJu0YywWnAesmmxLnE9J0jwxSeP1tApjyRGTc6m5h83/PnGb9eyXqjj asJrQ+zUwK1X5Ge7CHy/E2HTC2k1wNJQlBchgV8= X-Received: by 2002:a17:902:e885:b0:1b8:72b2:fd3b with SMTP id w5-20020a170902e88500b001b872b2fd3bmr888619plg.54.1691528942605; Tue, 08 Aug 2023 14:09:02 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:63dc:2a47:f4bc:4a95]) by smtp.gmail.com with ESMTPSA id k11-20020a170902694b00b001b3fb2f0296sm9437533plt.120.2023.08.08.14.09.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Aug 2023 14:09:02 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: Helge Deller , Akihiko Odaki Subject: [PULL 05/14] linux-user: Use elf_et_dyn_base for ET_DYN with interpreter Date: Tue, 8 Aug 2023 14:08:47 -0700 Message-Id: <20230808210856.95568-6-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230808210856.95568-1-richard.henderson@linaro.org> References: <20230808210856.95568-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62f; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x62f.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 1b4bb2d5af..d1b278d799 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -3107,6 +3107,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. @@ -3136,11 +3138,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; + } } } @@ -3155,13 +3178,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);