From patchwork Tue Jun 15 06:33:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kunihiko Hayashi X-Patchwork-Id: 460486 Delivered-To: patch@linaro.org Received: by 2002:a02:735a:0:0:0:0:0 with SMTP id a26csp3980506jae; Mon, 14 Jun 2021 23:33:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyz2sZR1yLXhV6Gnze2qBNvN1CDosbWmWSX8hkgiD/hyBi3SCp3BSdJt4pDQcy2EtX0H/7Y X-Received: by 2002:a17:906:2612:: with SMTP id h18mr19254303ejc.417.1623738804778; Mon, 14 Jun 2021 23:33:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623738804; cv=none; d=google.com; s=arc-20160816; b=kr/+PMA/dgu+e/IHcYK5R5vDqAPmmCVQGijVOdRYt+IMQ3cwugu00u9HmbqeBitjv2 dgs83JcZYyGmN8QfnNjMnJeCCip0XGSVIyNh0XNO2M0cEStvBw9HFXwkTfQ0iBO7KOuy 3n3Xcr/Io5EzBVVV/wiHE7C8XqJC77/l0PWKCB0uDuo1LbWYM+s5gBqLqFQ81vghViBA Fl6J2+8XxYlVCZ2wEfu6lLIEZ4ZFEY1bU8nT5y8GF92/I+lNO/bldiaSi39hJ1sDHsHN vAGTk3l9qf1W5cU2b7Ut58QOT0VEjeF006sLPxXVnqDvTpDXkutmtdV6jXnq4rr/FDB/ 1vwQ== 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:message-id:date:subject:cc:to :from; bh=nuzvF/CFhg8l6y69s/PSXzILxS6Z/bOSQAacfiBBJSI=; b=nFcAqVvfvyQ7xJpH5Zke5wfbycy18RRQGGkAtXl42awd8tq3WV4M2mxw/+Qh/U+ddp hvxu+nWUPJ4bSrtT2PLlvzi1ezaA6TERi+c4jlyn2rul6aVmrWfHrHW+VL2ASiAHlhjb JkkpMqp4SqgYXJAVBHJ95TDWT0CUFo0afO11EUIpbUff/PYyNoUuW8lNJXW9E6ffs2S5 TtfLxxLvHIjG3T+5FnD5vN/W3YEZ9t6+GeuCEvbmwY0JlmUxNjqWKEfX7lmEREHg/pcm uTtfncvz1V0EqYxboyLAuf/WYd7Ss7laMYrzuJ6Pp/hoRdYESjhSd3/z+dlLeBnGNYjj 68Qg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id de26si422959ejc.458.2021.06.14.23.33.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Jun 2021 23:33:24 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id C8F76801BE; Tue, 15 Jun 2021 08:33:20 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=socionext.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: by phobos.denx.de (Postfix, from userid 109) id D8CE2801BE; Tue, 15 Jun 2021 08:33:18 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from mx.socionext.com (mx.socionext.com [202.248.49.38]) by phobos.denx.de (Postfix) with ESMTP id 2381F801BE for ; Tue, 15 Jun 2021 08:33:13 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=socionext.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=hayashi.kunihiko@socionext.com Received: from unknown (HELO iyokan2-ex.css.socionext.com) ([172.31.9.54]) by mx.socionext.com with ESMTP; 15 Jun 2021 15:33:11 +0900 Received: from mail.mfilter.local (m-filter-1 [10.213.24.61]) by iyokan2-ex.css.socionext.com (Postfix) with ESMTP id A41982043ACA; Tue, 15 Jun 2021 15:33:11 +0900 (JST) Received: from 172.31.9.51 (172.31.9.51) by m-FILTER with ESMTP; Tue, 15 Jun 2021 15:33:11 +0900 Received: from plum.e01.socionext.com (unknown [10.212.243.119]) by kinkan2.css.socionext.com (Postfix) with ESMTP id 076BCB62BA; Tue, 15 Jun 2021 15:33:11 +0900 (JST) From: Kunihiko Hayashi To: u-boot@lists.denx.de Cc: Stephen Warren , Michal Simek , Joe Hershberger , Tom Rini , Kunihiko Hayashi , Marek Vasut Subject: [PATCH] arm64: Fix relocation of env_addr if POSITION_INDEPENDENT=y Date: Tue, 15 Jun 2021 15:33:02 +0900 Message-Id: <1623738782-10072-1-git-send-email-hayashi.kunihiko@socionext.com> X-Mailer: git-send-email 2.7.4 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean If both POSITION_INDEPENDENT and SYS_RELOC_GD_ENV_ADDR are enabled, wherever original env is placed anywhere, it should be relocated to the right address. Relocation offset gd->reloc_off is calculated with SYS_TEXT_BASE in setup_reloc() and env address gd->env_addr is relocated by the offset in initr_reloc_global_data(). gd->env_addr = (orig env) + gd->reloc_off = (orig env) + (gd->relocaddr - SYS_TEXT_BASE) However, SYS_TEXT_BASE isn't always runtime base address when POSITION_INDEPENDENT is enabled. So the relocated env_addr might point to wrong address. For example, if SYS_TEXT_BASE is zero, gd->env_addr is out of memory location and memory exception will occur. There is a difference between linked address such as SYS_TEXT_BASE and runtime base address. In _main, the difference is calculated as "run-vs-link" offset. The env_addr should also be added to the offset to fix the address. gd->env_addr = (orig env) + ("run-vs-link" offset) + gd->reloc_off = (orig env) + (SYS_TEXT_BASE - _start) + (gd->relocaddr - SYS_TEXT_BASE) = (orig env) + (gd->relocaddr - _start) Cc: Marek Vasut Signed-off-by: Kunihiko Hayashi --- arch/arm/lib/crt0_64.S | 5 +++++ lib/asm-offsets.c | 2 ++ 2 files changed, 7 insertions(+) This patch is based on the previous topic: "env: Leave invalid env for nowhere location" https://patchwork.ozlabs.org/project/uboot/patch/1620828554-24013-1-git-send-email-hayashi.kunihiko@socionext.com/ -- 2.7.4 Acked-by: Marek Vasut Tested-by: Marek Vasut diff --git a/arch/arm/lib/crt0_64.S b/arch/arm/lib/crt0_64.S index 9d2319c..680e674 100644 --- a/arch/arm/lib/crt0_64.S +++ b/arch/arm/lib/crt0_64.S @@ -112,6 +112,11 @@ ENTRY(_main) ldr x9, _TEXT_BASE /* x9 <- Linked value of _start */ sub x9, x9, x0 /* x9 <- Run-vs-link offset */ add lr, lr, x9 +#if defined(CONFIG_SYS_RELOC_GD_ENV_ADDR) + ldr x0, [x18, #GD_ENV_ADDR] /* x0 <- gd->env_addr */ + add x0, x0, x9 + str x0, [x18, #GD_ENV_ADDR] +#endif #endif /* Add in link-vs-relocation offset */ ldr x9, [x18, #GD_RELOC_OFF] /* x9 <- gd->reloc_off */ diff --git a/lib/asm-offsets.c b/lib/asm-offsets.c index ee592cf..c691066 100644 --- a/lib/asm-offsets.c +++ b/lib/asm-offsets.c @@ -41,5 +41,7 @@ int main(void) DEFINE(GD_NEW_GD, offsetof(struct global_data, new_gd)); + DEFINE(GD_ENV_ADDR, offsetof(struct global_data, env_addr)); + return 0; }