From nobody Sat Sep 26 20:51:00 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=sifive.com ARC-Seal: i=1; a=rsa-sha256; t=1788512113; cv=none; d=zohomail.com; s=zohoarc; b=adTuu0uYtUaHo4yKnRelxZLaI/EnFVo5m04BsWVMyUNek2yo7H7OQqwxiio/aXXyP7FyAEGBBz3IR/r3ZKeXtv7nsNlmDT+oA4IxLuPpdNyV6Ekygm6vpzK4Oun6Asjbq8TxXojeeS4MDy0T+CrC4Mb7jb1s5k/4rgbQKFZnY6U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1788512113; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=cKiuKxyyWJSuIyXVWzlv/wqmaebtjtdx2ZKx6moBL3s=; b=ROqUJIW1kRLUWGrXX5msOW/8ovZmUZWEMzi8r8bAfGDGueeZciIln06EfnCCLgG+0B6eM4t8K8lG5t7dQO0NtKERkOiuD7jDOQFv97etNo3DQvC9XZAUA3eygS15ORR0Zi+mo74nUqVgQocVbj0MTu4LqTSYueBja5b1eC3skYo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1788512113740870.6129365394833; Fri, 4 Sep 2026 01:55:13 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1x2Ph1-0005JW-V6; Fri, 04 Sep 2026 04:54:39 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1x2Ph0-0005In-Rf for qemu-devel@nongnu.org; Fri, 04 Sep 2026 04:54:38 -0400 Received: from mail-pj1-x1029.google.com ([2607:f8b0:4864:20::1029]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1x2Pgz-0007Xy-Bp for qemu-devel@nongnu.org; Fri, 04 Sep 2026 04:54:38 -0400 Received: by mail-pj1-x1029.google.com with SMTP id 98e67ed59e1d1-398d2b28acfso562240a91.1 for ; Fri, 04 Sep 2026 01:54:36 -0700 (PDT) Received: from duncan.localdomain (114-35-142-126.hinet-ip.hinet.net. [114.35.142.126]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-39ae8f1448esm4782912a91.4.2026.09.04.01.54.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Sep 2026 01:54:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1788512076; x=1789116876; darn=nongnu.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to:content-type; bh=cKiuKxyyWJSuIyXVWzlv/wqmaebtjtdx2ZKx6moBL3s=; b=RYT0FlQNJP46fqe03scRbKcEC9+0OBt6bJIRVblBS+Kv1EDmG4yL4ES5aS/mPEctnV V+Y0KVC8G1cn44uSABooreoAs+aWS6kgCERHUIgaCktQ94D2dk5QSwBMG038HC4zx1V1 SrQM+Z0N1+aRdMdy+66ZwyHrs+Nq3e20ulX0IUXXBmRhI4j0DOPMYFiGhnVrt2HZyXtw yn1zvvlgos8sVuviO/Zp9KdaZA9xgkNdq4OnAUuIB4eVf2IaJrJZmnBe/osIOQA6LLra HaPtQqk7e7EP4tgHKWuKwHRBEoTLwmmkgSgps6Pk//Qear+0xxr+ov/FpRteDEs3xwED AAIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788512076; x=1789116876; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=cKiuKxyyWJSuIyXVWzlv/wqmaebtjtdx2ZKx6moBL3s=; b=BXovTJxprOPQUlVvAEick32onOllo6Ic5wjKgTAo70weLxA+v46UFAJAzbni4nghaX o/pgfrRI0d8b/DV7TbSJZhnrtutD2wn3cKyRO5dr6beoAfMo0Klxs88nffGj8oEbfHIW rlIEzuCLCNLd5xYtFPqjSsfg+TI4thJ46WOOAexPCEoSA6bbv7QPApLDvmt2eIsCJqmJ /HFZJ2u554VUEEGh1r0S6n6tYQUoGITXB9HvGka6Y3v52aXSk4wat8FrvQsRgylm4L34 HhRtU5YZZDUkKchb98bvD7nNoBm0FzxcyQc+KxwtCC1QHtF5GsZcsJ3UulSxvD5V+9IQ jBSA== X-Gm-Message-State: AFuF++lllXMfWnElHsQ97Gq5EVCcgSd3jPchfuhktA60iKEpYswr6kWN j9/XGjcNDykqYWeTrlvbBhqrAtrdi+bie009XmLp8IhPJKPWIx+HQdEjeONFsavMkZN575GKJez DZ9MBksmIXZ48HsRk6E9Ca77qcwuDQnfjFe/9ZUSKcqnEYfhRu9uUS1Luw6ayBcNoIdFJS/DaSz 6eFhsixw+61WyF8IcVGxPpKFRWJnIQyzQr+BFy3y5CNQ== X-Gm-Gg: AYBFou1YGwO45Gg1azE3dnyDiFFx/RTkAjPix3w8IbvQ59NoQde5eTiJjgHSyWr0oY0 qN10cwYkAcuiJ6gQtNDBPzFLF+sQMGFdzds5CXYYXCNjByOBQMVqHcukwLWcwUItq/hGa+bljOL tR4zVq68NNnbeM4GHjut8Juuk5a+T7V3o5kTOXgPaGJaR/pgNZSgbdB81ouGHHQLQtRf+Bu7Ml6 swgKoFKu07EQpowA2KIsOS8yQ5RSo7RMp8Jf9pMFsj6b72t01yyVyijbnmHSWSmiz/a/VJuqGBV iNG0cUSDhfyPsHiQXBxSE/8dk8Ju0oOckg3ZFTYrGjeRVNKrJ/96UM2EoY8tjmYnHcGFnUgFWVJ qxUeD1I71CCKiIlDTZkNIB7FH05kXn7GXaB2wOiWlpSClkpkhZZmDK9Mv2JfFcmK+TyFCJ+ex4Z 8mi/6UoRGve5bAhuWUfizqb/nZcyhseFPZ1ZRuX+9Rp/PwurGG+4DGaF8r7xRoH41Rr3JSPoNJA Qn9/HEPcil76NOMy9tI4/Hd+wPV1/nmPPJvIjWl6aHPUKxLfz6Vn5teEyNs9pA= X-Received: by 2002:a17:90b:4c4e:b0:399:1b64:e0d7 with SMTP id 98e67ed59e1d1-39b262aaf32mr7108644a91.19.1788512075489; Fri, 04 Sep 2026 01:54:35 -0700 (PDT) From: Max Chou To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Palmer Dabbelt , Alistair Francis , Daniel Henrique Barboza , Richard Henderson , Max Chou , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PATCH v2 1/2] tcg/riscv64: Validate AUIPC relocation range Date: Fri, 4 Sep 2026 16:54:25 +0800 Message-ID: <20260904085426.3686492-2-max.chou@sifive.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260904085426.3686492-1-max.chou@sifive.com> References: <20260904085426.3686492-1-max.chou@sifive.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1029; envelope-from=max.chou@sifive.com; helo=mail-pj1-x1029.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=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @sifive.com) X-ZM-MESSAGEID: 1788512116468158500 reloc_call splits a PC-relative offset into a signed 12-bit low immediate and an AUIPC contribution. When the low immediate carries into bit 31, the existing 32-bit arithmetic can accept an unencodable positive 0x80000000 AUIPC contribution. Keep the split in pointer-width arithmetic and reject it unless the rounded upper contribution is representable as signed 32-bit. Factor this validation into split_auipc_offset so reloc_call retains its existing failure contract. Fixes: dfa8e74f9463 ("tcg/riscv: Add the relocation functions") Reviewed-by: Philippe Mathieu-Daud=C3=A9 Signed-off-by: Max Chou --- tcg/riscv64/tcg-target.c.inc | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/tcg/riscv64/tcg-target.c.inc b/tcg/riscv64/tcg-target.c.inc index 2ce9d47a633..76db0de94c3 100644 --- a/tcg/riscv64/tcg-target.c.inc +++ b/tcg/riscv64/tcg-target.c.inc @@ -641,14 +641,31 @@ static bool reloc_jimm20(tcg_insn_unit *src_rw, const= tcg_insn_unit *target) return false; } =20 -static bool reloc_call(tcg_insn_unit *src_rw, const tcg_insn_unit *target) +static bool split_auipc_offset(tcg_insn_unit *src_rw, + const tcg_insn_unit *target, + uint32_t *hi, uint32_t *lo) { const tcg_insn_unit *src_rx =3D tcg_splitwx_to_rx(src_rw); intptr_t offset =3D (intptr_t)target - (intptr_t)src_rx; - int32_t lo =3D sextreg(offset, 0, 12); - int32_t hi =3D offset - lo; + intptr_t low =3D sextreg(offset, 0, 12); + intptr_t high =3D offset - low; + + /* AUIPC sign-extends its 20-bit immediate after shifting by 12. */ + if (high !=3D sextreg(high, 0, 32)) { + return false; + } + + *hi =3D (uint32_t)high; + *lo =3D (uint32_t)low; + + return true; +} + +static bool reloc_call(tcg_insn_unit *src_rw, const tcg_insn_unit *target) +{ + uint32_t hi, lo; =20 - if (offset =3D=3D hi + lo) { + if (split_auipc_offset(src_rw, target, &hi, &lo)) { src_rw[0] |=3D encode_uimm20(hi); src_rw[1] |=3D encode_imm12(lo); return true; --=20 2.43.0 From nobody Sat Sep 26 20:51:00 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=sifive.com ARC-Seal: i=1; a=rsa-sha256; t=1788512110; cv=none; d=zohomail.com; s=zohoarc; b=PpWsO7xi54lmngr07sVOia4xcZOyGMy1HCq5A3Q+OlJs24mfGBLq1J+TlYtbjudC9JORwizqm9wewqTxW0JbmakkZXI8gc570wETKENjBLR7G+KNpnS+ikxNuRAmOZDgPx5xzFYPo/81q9vk0J28FQTeWMvBhjbJsgtb+wXyzSk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1788512110; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=RRja5k1q9ApEjl0FZYo2g1/y3PvPqD42W1nTIrwMKLc=; b=nuORAvWZPmU+HWqCZ86B4Dn6dqJWCRElkpdn+eZmrFefOi6QoXq43eYAGjlI2RD+XUWE6ustDHyH0oByEEcc+0IC9rG38gjTimBNIxSRNgQgC4UnSmBHyUzBqrTL8v1nU5SLmtDUQnuffk5MX65tqyeMoMiKUjyn+JEMjL3XzeE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1788512110238811.1747998631068; Fri, 4 Sep 2026 01:55:10 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1x2Ph6-0005Ki-DW; Fri, 04 Sep 2026 04:54:44 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1x2Ph3-0005Jb-D4 for qemu-devel@nongnu.org; Fri, 04 Sep 2026 04:54:41 -0400 Received: from mail-pj1-x102c.google.com ([2607:f8b0:4864:20::102c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1x2Ph1-0007Yu-Ph for qemu-devel@nongnu.org; Fri, 04 Sep 2026 04:54:41 -0400 Received: by mail-pj1-x102c.google.com with SMTP id 98e67ed59e1d1-39647aa9d52so865141a91.0 for ; Fri, 04 Sep 2026 01:54:39 -0700 (PDT) Received: from duncan.localdomain (114-35-142-126.hinet-ip.hinet.net. [114.35.142.126]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-39ae8f1448esm4782912a91.4.2026.09.04.01.54.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Sep 2026 01:54:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1788512078; x=1789116878; darn=nongnu.org; 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:content-type; bh=RRja5k1q9ApEjl0FZYo2g1/y3PvPqD42W1nTIrwMKLc=; b=QMRKEiB1eCkWssUfZ8URVXogcX/tANsPAtYqXEoQcbhSmv7vXd/E1sA4gguQcgIUGc MyhNXD8vXz+T+KDz8VXAE6tsg0qz74f3qhFgQ81hCnlbeUCBlzjD/wixJS1qV+gtr4jV gAs5AimR1yPkqFHjSMjeKknOjOpQeMPGtkplgC7uLgFl8rv5QnWM0O+CbuEBZTeH0Gii i9Fn3pDQvck6POhG7CingdIBoq3LUWab9HUZxFWuvMHGgQFsYVOqxJojxsvjaZ8lKrWa ADRUiHcAo4bEEASeRuAwq68z2CTngaNJRIBcGGqy0L22+B6lFrNVUa4I54TIlEF0NJPh 7RKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788512078; x=1789116878; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=RRja5k1q9ApEjl0FZYo2g1/y3PvPqD42W1nTIrwMKLc=; b=VZRmns9zYJfSbb9NEVzxqC8TRTkakDEEpzEWlSYrvWC19lMKK0mwzevcs2iEBoy9Bf SBnbQlSAdz7VaUVDeN4P+aCXbcwM7oK4XApOo/M/HpTBXYnJ8T+waQKr9UtarwUNYLmy HOuYf1DXd/0IFBSag6PjVRakY6r4iALW1aBRAaX+2ews8HKmicwPbgsn35kKkSAzQego ENyjt6L+slW1BPb1ZE+pTYwt6XRjU3UTr29YcYqZcCJbYZBUZehgWpW+cnUCbOuqAw61 rayx2IVrjZ4S43BEcaxTexteu7dvnaaDwUEU3ot0lm1QJqCMS/mIiKgahdAA7UZ/hcdr CUBg== X-Gm-Message-State: AFuF++l+hbWAOv18P+K1TuwQbRqFkgM4izmV5zVzICmO5GyEiciuktqt RmpffL7B1TtaX2MWaBc+BK85nbaMJtLWmRRn9JM7xWLMpeTzJAUroy6sK9VU+Mxz5nR5EcxzRpG t0Zn8RG55G/6quylP3YHp+aAJRo9BrAwwWIXc5keJluVZ0+5Dvi+Y/hnqtfIPEOmhXkfu7jViwy 6ovPO5DUt4RlnW/uhPnIr8wivWkOoHjmxS4o0d2vJR1g== X-Gm-Gg: AYBFou2BubT+0s+oACN/Hsv89quRJ3OA7INgvuOsFSgjuFB65OeEMi0w3PMxiBv/++O dPwFtD9sroFebC1w5DYdaqd1GBrmWIH1QzxI2TMTQGGgjuuxFYIw0orRWmE4UG3pSrJNfOGfzgv LZkXadUT4EsoFY6etWjtBNhSa9FdIsRELUDrI7LGFvaMxQrShoMKeK3gw+yHWc44jWIBPAnaUYT NzN02Vm1v1kujXZ4cdnCIORYglz0fumHu5rcuzT12lo2LnJYRdYTi+crJzWmc+fKFL9ZA7huRwu zeHNu3iDuiwdQZNHsVQxR2I3qWbYp6/2dVLUDGyThkWwOi9okd7h6PSCcfCXlMn7gi6FoCdjWm1 hvLJGjMNHrx+1rrsNBC/8iUtLuwBTOGPQYWzsGdNnJ5+jXIYAd97Fj1Ho5en9ShOgXsCH+VJiqR PSxUptWTUJb7EN6NP4lRXSkpr/vNlghPWMMHYGU/1M095pgd8o1V3nyai+tAqiNFbyGtgLaoPfb h8lXu2TAkJ6Nur3JF8hy397OY9MJoRhamtwsU2W8kjwpkFjkDHeMcZEcMR6604= X-Received: by 2002:a17:90b:1d44:b0:38f:cab0:9aa9 with SMTP id 98e67ed59e1d1-39b0803b0efmr12284521a91.13.1788512078187; Fri, 04 Sep 2026 01:54:38 -0700 (PDT) From: Max Chou To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Palmer Dabbelt , Alistair Francis , Daniel Henrique Barboza , Richard Henderson , Max Chou Subject: [PATCH v2 2/2] tcg/riscv64: Fall back when AUIPC pairs are out of range Date: Fri, 4 Sep 2026 16:54:26 +0800 Message-ID: <20260904085426.3686492-3-max.chou@sifive.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260904085426.3686492-1-max.chou@sifive.com> References: <20260904085426.3686492-1-max.chou@sifive.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::102c; envelope-from=max.chou@sifive.com; helo=mail-pj1-x102c.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: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @sifive.com) X-ZM-MESSAGEID: 1788512112760158500 Content-Type: text/plain; charset="utf-8" tcg_out_movi and tcg_out_call_int only check whether a PC-relative displacement is signed 32-bit before emitting an AUIPC pair. This does not account for rounding the signed 12-bit immediate, which can require an unencodable positive 0x80000000 AUIPC contribution. Validate the split and emit the pair only when it fits. Otherwise, tcg_out_movi keeps its existing full-address materialization and tcg_out_call_int uses its existing indirect far-call sequence. Fixes: dfa8e74f9463 ("tcg/riscv: Add the relocation functions") Signed-off-by: Max Chou --- tcg/riscv64/tcg-target.c.inc | 43 +++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/tcg/riscv64/tcg-target.c.inc b/tcg/riscv64/tcg-target.c.inc index 76db0de94c3..4228203d36f 100644 --- a/tcg/riscv64/tcg-target.c.inc +++ b/tcg/riscv64/tcg-target.c.inc @@ -674,6 +674,21 @@ static bool reloc_call(tcg_insn_unit *src_rw, const tc= g_insn_unit *target) return false; } =20 +static bool tcg_out_auipc_pair(TCGContext *s, const tcg_insn_unit *target, + RISCVInsn opc, TCGReg base, + TCGReg rd, TCGReg rs1) +{ + uint32_t hi, lo; + + if (!split_auipc_offset(s->code_ptr, target, &hi, &lo)) { + return false; + } + tcg_out_opc_upper(s, OPC_AUIPC, base, hi); + tcg_out_opc_imm(s, opc, rd, rs1, lo); + + return true; +} + static bool patch_reloc(tcg_insn_unit *code_ptr, int type, intptr_t value, intptr_t addend) { @@ -808,7 +823,8 @@ static void tcg_out_movi(TCGContext *s, TCGType type, T= CGReg rd, tcg_target_long val) { tcg_target_long lo, hi, tmp; - int shift, ret; + int shift; + bool ret; =20 if (type =3D=3D TCG_TYPE_I32) { val =3D (int32_t)val; @@ -829,12 +845,9 @@ static void tcg_out_movi(TCGContext *s, TCGType type, = TCGReg rd, return; } =20 - tmp =3D tcg_pcrel_diff(s, (void *)val); - if (tmp =3D=3D (int32_t)tmp) { - tcg_out_opc_upper(s, OPC_AUIPC, rd, 0); - tcg_out_opc_imm(s, OPC_ADDI, rd, rd, 0); - ret =3D reloc_call(s->code_ptr - 2, (const tcg_insn_unit *)val); - tcg_debug_assert(ret =3D=3D true); + ret =3D tcg_out_auipc_pair(s, (const tcg_insn_unit *)val, OPC_ADDI, rd= , rd, + rd); + if (ret) { return; } =20 @@ -1588,7 +1601,7 @@ static void tcg_out_call_int(TCGContext *s, const tcg= _insn_unit *arg, bool tail) { TCGReg link =3D tail ? TCG_REG_ZERO : TCG_REG_RA; ptrdiff_t offset =3D tcg_pcrel_diff(s, arg); - int ret; + bool ret; =20 init_setting_vtype(s); =20 @@ -1596,14 +1609,14 @@ static void tcg_out_call_int(TCGContext *s, const t= cg_insn_unit *arg, bool tail) if (offset =3D=3D sextreg(offset, 0, 20)) { /* short jump: -2097150 to 2097152 */ tcg_out_opc_jump(s, OPC_JAL, link, offset); - } else if (offset =3D=3D (int32_t)offset) { - /* long jump: -2147483646 to 2147483648 */ - tcg_out_opc_upper(s, OPC_AUIPC, TCG_REG_TMP0, 0); - tcg_out_opc_imm(s, OPC_JALR, link, TCG_REG_TMP0, 0); - ret =3D reloc_call(s->code_ptr - 2, arg); - tcg_debug_assert(ret =3D=3D true); } else { - /* far jump: 64-bit */ + ret =3D tcg_out_auipc_pair(s, arg, OPC_JALR, TCG_REG_TMP0, link, + TCG_REG_TMP0); + if (ret) { + return; + } + + /* Far jump: 64-bit. */ tcg_target_long imm =3D sextreg((tcg_target_long)arg, 0, 12); tcg_target_long base =3D (tcg_target_long)arg - imm; tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP0, base); --=20 2.43.0