From nobody Sat Sep 26 20:50:57 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=1788455106; cv=none; d=zohomail.com; s=zohoarc; b=IHHaS4QklLIKivPNO6WA6YSp9aYjgGDG+0SG5TiaoNPI1QYyj6Jjw67GF9HGpZ/PkggWmFt7VzYxuo3HeGsCHjc8b/B6sFSm4VjMUaFqD7InXlnO7C4Sk3rSzjxPNPsgzqP5hxudOs4cVVWJo366gOuY2vMcGkJtp3IRYdUsdH0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1788455106; 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=Yr1xuAHbIhRQR2HquIZ+fNcRwi6DyU7KLH9171HGnOo=; b=JAtahA8ySolPZ5NxcBDjcoFpejSrc372yOrLmCbi50DXmNvEfUF8q2Mb8ap4Y5qMnCJztbloF0lXcHNUVMP2Gtfi0yadUmuKCzpN78Tmpo0kxj6qGyJGajvHZR9S2jUZHQcubH5F7yXIIT4zPkuNQxkVM4i6d8YZ7jRRdKf1K7s= 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 1788455106787698.142634176584; Thu, 3 Sep 2026 10:05:06 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1x2ArO-0002Dq-EQ; Thu, 03 Sep 2026 13:04:22 -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 1x2ArN-0002DM-5j for qemu-devel@nongnu.org; Thu, 03 Sep 2026 13:04:21 -0400 Received: from mail-pg1-x532.google.com ([2607:f8b0:4864:20::532]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1x2ArH-0004yP-Ip for qemu-devel@nongnu.org; Thu, 03 Sep 2026 13:04:20 -0400 Received: by mail-pg1-x532.google.com with SMTP id 41be03b00d2f7-cb5b8572b70so185587a12.2 for ; Thu, 03 Sep 2026 10:04:15 -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 41be03b00d2f7-cc43d39e4ddsm1227860a12.5.2026.09.03.10.04.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Sep 2026 10:04:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1788455054; x=1789059854; 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=Yr1xuAHbIhRQR2HquIZ+fNcRwi6DyU7KLH9171HGnOo=; b=E3pwl69YcLZ2Z8uHqSLhiiCstkbbFPVtau0dZjekdLHBdgVOgvVD5MBqYt6Y/Uwm0n SXl0j8D/KI5rEEKbs7SN7juBnOCfOIaMLfKjZvQuZCjJVPGtDOLqw2q49V019RAY+FUp ajcyonI13PI7hjQCyF2J9JMdvb8NO/lQitc0AO47IQFN+kSFQjf5sHAhgre0tdWa33dO 7qdof4IWN6Q0VJx8kjjA3b6AZnWpLxtJk9GjXBaMVL3Ymyv/ivSYJBQRJCuL7ZHo1LEz 49uVUccmVC8RiDqEsL/jemqCt8WHJG3M8S7w1xf3XMZkK3oqI8TenXqfZDUkE4CitH1p 5lmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788455054; x=1789059854; 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=Yr1xuAHbIhRQR2HquIZ+fNcRwi6DyU7KLH9171HGnOo=; b=hfB7mmuO7ERA6C/ii+YBTDXrgm0lKvh4pUUbO8teCl4OEzikzA94Rn3HdNcz1ojPei 8sTMkRrYiED3xqZS3WPh4K96sXitqGrAE9Lc/3YMk9veu3KiwWGKbYFyJHVYZsgw4WNR CgdF2CyPQV/UkB7YU3HhqPbh8WT4v361Esvv7oUSH6088Gvmxv8mZdH57jEheRQ5tsDG KqbQfE3l9o/KRdfhJe3qLDrJnYGASDKiVmqnUlC5Gu60i8wEudD/1TTByoxu1TwJgdK9 B6MVbdTAUEY6L3ZHxdXEfU6eKSuTqc05Z6d4WHYPSqJFuMrcoJDqswEMvwn1WT2IfPvc qA2Q== X-Gm-Message-State: AFuF++lAaM6I7WYaa9wF0/ACnC0fqtJovDs3eXAu2zA170STvoOCBkr6 Z8FzeYBzQ3MSn5QU2RnnmQI58DAKfKB8S4tk+kl7iuhhLQpi3WSJ3jpVoDOc42xSAjGneN4Y0yh rJjWecRVp5aDesqDTMFoeNaDpgsNWJtCWpk+lCJFe5Vanu7ySNiqQgQwnPWqx1MaojzEwO1UvnI I6Ev4x3X0Z5X1nb6KAxfA/wgxNrYcMo/fB8a3NhU6HNA== X-Gm-Gg: AYBFou0M2RCA2shPWlgJptz7dCAdIZQNCtEYSxkE9inIrzSOYSLTZqsGrkCPfUGOU+5 WSe66F/CERp/z7DLlhfJrp5TErdImvqm3jl9vtdezu09b0gXtpgHXcDtnZgMjcghNAwOf+qSjOZ HBEgz7ffUVCElKz8DHnWP2etLGVgFj3ZSsBaR3wWv4LMKbyCQXpONa6OsfZLtUQDIPTdW5D4O2k ynPA2T/l9AExDk5VDNk2R1kz8gMRY2GcKVdVBa5uhZIFpkNA0wRiusiDpimh5A5pdudcj5VBSWH kTRXx+aMDIEw6c3eYzqpoGwqwHTK60tLJY+4CAwP3snwTwHKWoo6dmCBrHUIMcZSSA795AMVe0I O/aY0WXGT5tot19EA9Pa9g2F5wUwIpr1IrMh/ZxIxIHfpwJhLih6WVRrpgvto8zta6jEOQd2COp ntAr0rhGX3fVtTGzRHjo2JQJ+cNtIGGbwPsjd2yy8wfG4oGgLf1AgjWxGMiMFZoNM/Cqw4dyolL yLEr0hZgG3PBmrnXBmIMQCBXSIrWLx8YbydMcof0VcHtJhuYuWJdUwDsNN+qoo= X-Received: by 2002:a05:6a20:2d22:b0:3d3:aec2:4dcb with SMTP id adf61e73a8af0-3d9b051aa7dmr24391920637.23.1788455053803; Thu, 03 Sep 2026 10:04:13 -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 1/2] tcg/riscv64: Validate AUIPC relocation range Date: Fri, 4 Sep 2026 01:04:04 +0800 Message-ID: <20260903170405.3632015-2-max.chou@sifive.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260903170405.3632015-1-max.chou@sifive.com> References: <20260903170405.3632015-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::532; envelope-from=max.chou@sifive.com; helo=mail-pg1-x532.google.com 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: 1788455108368158500 Content-Type: text/plain; charset="utf-8" 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") Signed-off-by: Max Chou Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- 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 a439ba5c20e..1c41f8ffade 100644 --- a/tcg/riscv64/tcg-target.c.inc +++ b/tcg/riscv64/tcg-target.c.inc @@ -634,14 +634,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, + intptr_t *hi, intptr_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 high; + *lo =3D low; + + return true; +} + +static bool reloc_call(tcg_insn_unit *src_rw, const tcg_insn_unit *target) +{ + intptr_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.7 From nobody Sat Sep 26 20:50:57 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=1788455149; cv=none; d=zohomail.com; s=zohoarc; b=HUYMIXfGbC+883dPrimz7/UGmZwtsw1r2/xhu60ZQAeZb+Jx7jewZw7Cf9iq7qihq2C79aESqRx8BMS3TByaELXCx3s7/Gc1VCbUZdySFqBlrl5fL4D4HbDozfW2dd+wKlM/Q6RCvOcpHAQuf9Hs2k5PtfuYkPhdFhV3LCX+87Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1788455149; 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=kWbk6bJ4gzcYZbDe8UfozS5vSTG9P9yfPb8F48deIsI=; b=ndYaqVXa22cvZLsUeqcWl1jZ6QlYC8nx+3FFY9xKOyy6QuxdQqtn9rE7x8QJsDVGjZla9FT7xwuRuSoT8c4pOvthsnfr5ttHzfWBub/kqdQBoeR843LFMKuUL/IVAaypcJg7Hr0dpm656H5e/PwKWSJvVRHx2+DrJSikC2W0nQQ= 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 1788455149044414.0210423691191; Thu, 3 Sep 2026 10:05:49 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1x2AsJ-0003Qs-3R; Thu, 03 Sep 2026 13:05:19 -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 1x2AsH-0003QN-UA for qemu-devel@nongnu.org; Thu, 03 Sep 2026 13:05:18 -0400 Received: from mail-pg1-x52f.google.com ([2607:f8b0:4864:20::52f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1x2ArJ-0004zE-N1 for qemu-devel@nongnu.org; Thu, 03 Sep 2026 13:05:17 -0400 Received: by mail-pg1-x52f.google.com with SMTP id 41be03b00d2f7-cc1bc88a20eso234929a12.3 for ; Thu, 03 Sep 2026 10:04:17 -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 41be03b00d2f7-cc43d39e4ddsm1227860a12.5.2026.09.03.10.04.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Sep 2026 10:04:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1788455056; x=1789059856; 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=kWbk6bJ4gzcYZbDe8UfozS5vSTG9P9yfPb8F48deIsI=; b=Z8iZf2yf2W+/Cjaw9+MqT8xGv7DIH28kVDDoJidNLWc5/utn9FggrUPfAEjhEOb+4s IxIf6EJprb88HXv/modWuYM2XwR1p6n6BcXKsBohmQ2WPLi8VudyTyDrwUcupYgWn6Jc CofD83gtWC3P7VUODXEgcZSsw9fW4xllYVor3e6P4tpQMB8kQplPu5BhLBo+HvmVJBE6 mZUGa8hcqmvT+5GXMC5d0qICx/Rt7owN5n5gtjUjYSOlxL7aLT6NzbfbOPBVrVgQx8qh YzXQGJxztBbQ+9/0MmSmHedpAUHpgOtF4eUYoyK2bHtTnXWP0KkMsDT2BAWAsVdT4FoD 2Ddg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788455056; x=1789059856; 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=kWbk6bJ4gzcYZbDe8UfozS5vSTG9P9yfPb8F48deIsI=; b=awR+nyi+sxro62FztgKgZHglMSV3DTwhUv6wN/l5mai22riwnkjXpA4Xwf9nR3l7ma 6/vH7yOHRIfZ6Pj1wq/+wy/GQn2SNNJVLIGragJKHdY8C4jPBx6YYhU+EYx7vaIYC8WC N6REHioaEHrS4bhTV1BtNigN6eLWCFNbEPoBr041DythofSEvkhddMZsrkgmC8t3eYhL ClslvS4SfyZWKU1+2RGaUdT3ffd/QKx79q9FCiQTjsg6yBOC2dIH9EMupOWwLBZPxxhZ yO+x07sY/eqfzFRAtQQ/Exmoxvz0uDfWPZ5+pHIES5B5WDEsdTiT9YFs2pVSulMlqnTG lq6w== X-Gm-Message-State: AFuF++n8CNfBftquEVrxMaSmMtogF1OpZD1z4fiVH8Ny1efuXPFL/+m+ bDtgRfGcDvJh/YLx7vI7F6lscJ+t37sDY7cqB5blvj5LZDR6j9JRvz2Y+DBEzlR6Nx1LxNOmGlz /VgcbmAN8oXiP9qbzrPlsB1AmRhNegr4U63NU2XAc0C0S3atoYNoSo6YUn5+TfXsF5yqOAowwQh JDFeEmdiB2CBUkLYRVPA6DxOeVqussurgcZ+4v6Hz47A== X-Gm-Gg: AYBFou0qLmdP+06SItV8bWlLn4fgzfhlGg9O846aEG1jRT57XFXTx29vtp1vevWYgpj g2VplxYNDbckfkKliWp9h0yOuKyjAe+Swm9190/yv5zHE6LIKR+vmCrQce28qn5E6DVsTfbcuT/ nJ/rGuqzy7EMvAgJBt2/WczOw3exXoYlGdsw0B987MUA8QyafEHQWEn2gu7zIl/3pGaE7As2NNe RbxYxjiVfAjoks/HKo6rivThguJZCInkMTPgzH5LCRb6ocw7LaR10pW34SbwLsQeMjnIJgrqZ0S dgSZJFyDAzYqU8aUHZOqESVFKFEE0Pk3d0lO1c3dJFsmpPYfRQad72m14yCC5dlim/MTV8jtz9S spV5oQUCegqOvPnDyYCBveyhSYPTTGJw7VCl+mIpwnObNI6+drzU+3ccQLwQQ/P2+GFwblp6aJt qE9TYoIMq6616tXNGZZ4+fEYjSz08jqj6PFbaJ0xj1tzekFREqNgCmy5IZDH0G1mkpRN664CyT8 KOsY/7wvK+YsQ7EB7aqpf5ztAjQvYvybZl3/dzGyBGag1UqlqxFmJmwAg3JvfU= X-Received: by 2002:a05:6a21:a4c2:b0:3d0:d0ed:f141 with SMTP id adf61e73a8af0-3da35d4c9b2mr2068876637.2.1788455056114; Thu, 03 Sep 2026 10:04:16 -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 2/2] tcg/riscv64: Fall back when AUIPC pairs are out of range Date: Fri, 4 Sep 2026 01:04:05 +0800 Message-ID: <20260903170405.3632015-3-max.chou@sifive.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260903170405.3632015-1-max.chou@sifive.com> References: <20260903170405.3632015-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::52f; envelope-from=max.chou@sifive.com; helo=mail-pg1-x52f.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: 1788455150401158500 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 1c41f8ffade..12efb44c3d7 100644 --- a/tcg/riscv64/tcg-target.c.inc +++ b/tcg/riscv64/tcg-target.c.inc @@ -667,6 +667,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) +{ + intptr_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) { @@ -803,7 +818,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; @@ -824,12 +840,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 @@ -1583,7 +1596,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 @@ -1591,14 +1604,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.7