Message ID | 20240206170538.2937169-1-syq@gcc.gnu.org |
---|---|
State | Accepted |
Headers |
Return-Path: <binutils-bounces+ouuuleilei=gmail.com@sourceware.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1682038dyb; Tue, 6 Feb 2024 09:06:33 -0800 (PST) X-Google-Smtp-Source: AGHT+IHs7MsT1GILh81cVt9ejko5EP+9sl9Fq4LU/IBymc7mXUNd1xfbkPt9xkDhgG7d2ZYIrtgM X-Received: by 2002:ae9:c118:0:b0:785:4de5:e0cf with SMTP id z24-20020ae9c118000000b007854de5e0cfmr3102196qki.13.1707239193504; Tue, 06 Feb 2024 09:06:33 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707239193; cv=pass; d=google.com; s=arc-20160816; b=L+cPAqZNGj5CbrA0RBzCCgTtvQ5Rby/U7eByARTTYxWXY4Cy7DmJIkUQo2oY8CV17M qiWF26PUu0PnjZLhiH7kiKXM/+1x0CnjyBevlFXwiKR1iJzSutPMd8vTdRCriVXjwLOc DcMJQIiWwbc5hypzAfF8y9+k954yl/0782qq91gf58eNfcrN7XMiaD2NJ1CJFjnRubkj +ZvH7/ofLYd/S/K+VWo19ipJpbeZBl9J/JgEle9bR0q4Cbzw9Gx7s10qAJeGvbqWCdZy o0ycTB6XfMWjpVpgPTD9BB2lilcm2B3QW2eVR2BM+NNecxPz+HlDA1IXwGhrYDKv9IHc zT/Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:arc-filter :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=J2GrSNV4t2MVFqSdx9UT04/0nMISwutYvWnYHoc2dJE=; fh=+mrl7TY4YYs4G0r0AS85gFSQFaWgChGS/5FJqlgjPfo=; b=X+LwT4ImJfT2UGo0bSE58u99z10efBnIXQYNpQBK4tjyyZpaaIiybJeKTLSPU2BRFm +PdCPfXAB6lldn6jsteaVIfHU7RWdVYiO1cfPHOihZfoVKbyulL//7DKEr3MO1RkB1XL aCtNRJiHkySLydKMfscY0SNuICgQNbziO3lOby/mZ4Nbt5ei6P6MNksocPSWNz/RrcKI inK7ec4fSXgjF5Qn2XLcpM++EsUtooFGtx/qIlqVboOmj/lh2VnmLN643wsXgJ3FiS+7 Cqq2GFoquozoA89M8pVADBVTRpmGZcvoFUFIhMjXBeOuO9spCyE0CrQGXRykLRxsD0SZ GKZw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=jFh6KGXU; arc=pass (i=1); spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org X-Forwarded-Encrypted: i=1; AJvYcCWsbhoybraa1DE82HBXoq3Y0AsuNkQYec6Ogl8s5kXFjyVDGRQYrtuu2Nkb8Qxxsj9RknF2ORMCGg7JFrO0oRfTWTGElg== Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id k13-20020ac85fcd000000b0042bf6462ae9si2730906qta.734.2024.02.06.09.06.33 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 09:06:33 -0800 (PST) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=jFh6KGXU; arc=pass (i=1); spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 28E4B3858418 for <ouuuleilei@gmail.com>; Tue, 6 Feb 2024 17:06:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 28E4B3858418 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1707239193; bh=J2GrSNV4t2MVFqSdx9UT04/0nMISwutYvWnYHoc2dJE=; h=From:To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From; b=jFh6KGXUQYgIbn7u9EWIp+5u/mLo2c/rfO/MWujmzQolDcRQmEYwXwfpmog4cMa/n MDL2KRGDvPeirFUm9ckcbCblxsq4n/Bx9soNqZURORI2H/gTZhHZYhAqLrBNmPtwzX V9V8iAZPyqxob6O7XshU9S86QrM+xozptnvBcEc0= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) by sourceware.org (Postfix) with ESMTPS id 5042D3858C29 for <binutils@sourceware.org>; Tue, 6 Feb 2024 17:06:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5042D3858C29 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 5042D3858C29 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=209.85.210.181 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707239182; cv=none; b=KNqFfDN3z8queH387vHzq4l4qK9NjprQu+884SRyXx/DlrL/7bumGG5jISmufS1UkeMWmIfqYG/Ds+m4VhhGPkFSgZF3vFH78/V1pb6eqZzh8hpVgyVhMKFN+kTz6vsTkhSxvEQwc41ELmUz/JTavaD2JdGw3HU2w5E6LaaV3lo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707239182; c=relaxed/simple; bh=+fnlN91qawTZT2xnqtA1Hqb1ly3R6s0Hhs+t6nY6gzI=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=An8+wDO6+sH0jpxHeCb+j6q8yrQJYzuWy1detBCK7ZIbQSquzcjapWfmbHAci+iFtsGIILaZC7yP+fbx7cgdyiODQXKamEJjCaSkL26liORlBkyKW2tMP96xBvE4GaTO7lBnYD/1iTmiHWNN4y7+ztfdOj6ezXQMh8CCP96JoW4= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-6e038bdf6b1so1788270b3a.3 for <binutils@sourceware.org>; Tue, 06 Feb 2024 09:06:20 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707239179; x=1707843979; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=J2GrSNV4t2MVFqSdx9UT04/0nMISwutYvWnYHoc2dJE=; b=DyPp5Y/rQZj4pUOLQ3nUNHYarFcl9XVRjlFcKQPXk5Cu/OHam9BYcePJAU0cwcTqus XeI9GaEbFXVjrmBYNUY7jYx0K2hc9/DPmSQ1NXhNQxnDjAkUIgjtTcKZhV3MoNVP9iU1 8B5XZeLPtuBBQauu6MLCKCnhNMFQuedQ9izPj4wCXidc+5yRjK2FfL3SuyHHtZSKFhfT ZQCuYVEckHHzUqU+aQO2NpEET2wyL5X7KH1PoSiwj9kDRAsVIjkostBdHqcSCFjRMxsM /WCLOIbIQ985+bmRq8BHwlWPJ9n+Z8Ub5L0Rjsxlg9tBssyHjScfGUOalLfJz7Fe34/x 36Qw== X-Gm-Message-State: AOJu0YzlNRznyJ8euPhY1jsalVQg3XQ+7ixrzCXhaTW7W1Z+9N57AD6i YWHpahN7EySSHXznFj0GpAITgplpq0xlg7KkcQFJf4OWIMsefMEO X-Received: by 2002:aa7:8490:0:b0:6e0:42de:c8d6 with SMTP id u16-20020aa78490000000b006e042dec8d6mr142313pfn.14.1707239150770; Tue, 06 Feb 2024 09:05:50 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCW9Ed00DI8ps/IGEpVKeQ9fn5CetFXQWUnKR7OQElhEIIiVYPJpLLsgoZ9sYP5M+/1gzjMHyCBxWGV8oi+DCeY/p/WCYnvPgmC9f8XhuRwfN6/yDhD5 Received: from localhost.localdomain ([149.248.38.156]) by smtp.gmail.com with ESMTPSA id jw16-20020a056a00929000b006db05eb1301sm2196559pfb.21.2024.02.06.09.05.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 09:05:50 -0800 (PST) From: YunQiang Su <syq@gcc.gnu.org> To: nickc@redhat.com Cc: binutils@sourceware.org, macro@orcam.me.uk, xry111@xry111.site Subject: [PATCH v4] MIPS/Gas: Disallow branch to absolute address for PIC Date: Wed, 7 Feb 2024 01:05:38 +0800 Message-Id: <20240206170538.2937169-1-syq@gcc.gnu.org> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Binutils mailing list <binutils.sourceware.org> List-Unsubscribe: <https://sourceware.org/mailman/options/binutils>, <mailto:binutils-request@sourceware.org?subject=unsubscribe> List-Archive: <https://sourceware.org/pipermail/binutils/> List-Post: <mailto:binutils@sourceware.org> List-Help: <mailto:binutils-request@sourceware.org?subject=help> List-Subscribe: <https://sourceware.org/mailman/listinfo/binutils>, <mailto:binutils-request@sourceware.org?subject=subscribe> Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790170044606117071 X-GMAIL-MSGID: 1790170044606117071 |
Series |
[v4] MIPS/Gas: Disallow branch to absolute address for PIC
|
|
Checks
Context | Check | Description |
---|---|---|
snail/binutils-gdb-check | success | Github commit url |
Commit Message
YunQiang Su
Feb. 6, 2024, 5:05 p.m. UTC
The asm code like b (0) will generate binary like: 00000000 <.text>: 0: 1000ffff b 0x0 0: R_MIPS_PC16 *ABS* 4: 00000000 nop If this object is linked into an PIC/PIC dynamic executable, this branch instruction will jump to a wrong address, with low 16bit unset. Maybe we need a new dynamic relocation, which is not even defined by MIPS psABI, and I don't think that it is worth at all. See PR31343. --- gas/config/tc-mips.c | 9 +++++++++ 1 file changed, 9 insertions(+)
Comments
On Wed, 7 Feb 2024, YunQiang Su wrote: > The asm code like > b (0) > will generate binary like: > > 00000000 <.text>: > 0: 1000ffff b 0x0 > 0: R_MIPS_PC16 *ABS* > 4: 00000000 nop > If this object is linked into an PIC/PIC dynamic executable, > this branch instruction will jump to a wrong address, with low > 16bit unset. NAK, `mips_pic' may be set to SVR4_PIC and still produce de facto non-PIC code, as per the original SVR4 MIPS ABI (with lazy binding stubs rather than PLT). We continue supporting this mode. Originally NO_PIC was intended for bare metal code only, statically linked only, which was later extended to Linux, etc. with the addition of PLT and copy reloc support. PR31343 has to be sorted in the linker. Maciej
Maciej W. Rozycki <macro@orcam.me.uk> 于2024年2月7日周三 03:20写道: > > On Wed, 7 Feb 2024, YunQiang Su wrote: > > > The asm code like > > b (0) > > will generate binary like: > > > > 00000000 <.text>: > > 0: 1000ffff b 0x0 > > 0: R_MIPS_PC16 *ABS* > > 4: 00000000 nop > > If this object is linked into an PIC/PIC dynamic executable, > > this branch instruction will jump to a wrong address, with low > > 16bit unset. > > NAK, `mips_pic' may be set to SVR4_PIC and still produce de facto non-PIC > code, as per the original SVR4 MIPS ABI (with lazy binding stubs rather > than PLT). We continue supporting this mode. > > Originally NO_PIC was intended for bare metal code only, statically > linked only, which was later extended to Linux, etc. with the addition of > PLT and copy reloc support. > > PR31343 has to be sorted in the linker. > So, what's the expected behavior? Let's use the example: .set noreorder .set nomicromips .set nomips16 b (4) INSN0 INSN1 INSN2 INSN3 At least we have 2 choice: 1. jump to INSN0, if we treat (4) same with (. + 4). 2. jump to INSN1, if we treat (4) as the offset in ISA document: "An 18-bit signed offset (the 16-bit offset field shifted left 2 bits) is added to the address of the instruction following the branch (not the branch itself), in the branch delay slot, to form a PC-relative effective target address." Currently, this syntax is not supported by ld even for static at all: xx: b (8) ssnop ssnop ssnop ssnop mipsel-linux-gnu-gcc -mno-shared -mno-abicalls -static -nostdlib 1.s warning: cannot find entry symbol __start; defaulting to 00400110 /tmp/ccdHuq6V.o: in function `xx': (.text+0x0): relocation truncated to fit: R_MIPS_PC16 against `*UND*' I don't think that we should generate such confusion binary at all. So, I prefer to disable this syntax for all cases, even for static objects: only branch to label is allowed.
On Wed, 7 Feb 2024, YunQiang Su wrote: > > NAK, `mips_pic' may be set to SVR4_PIC and still produce de facto non-PIC > > code, as per the original SVR4 MIPS ABI (with lazy binding stubs rather > > than PLT). We continue supporting this mode. > > > > Originally NO_PIC was intended for bare metal code only, statically > > linked only, which was later extended to Linux, etc. with the addition of > > PLT and copy reloc support. > > > > PR31343 has to be sorted in the linker. > > > > So, what's the expected behavior? The static linker is supposed to report, as a warning or an error (depending on what BFD's policy is), that it cannot resolve a relocation, either due to an overflow or due to the inability to emit a suitable dynamic relocation so as to defer the calculation to the dynamic loader. > Let's use the example: > .set noreorder > .set nomicromips > .set nomips16 > b (4) > INSN0 > INSN1 > INSN2 > INSN3 > > At least we have 2 choice: > 1. jump to INSN0, if we treat (4) same with (. + 4). > 2. jump to INSN1, if we treat (4) as the offset in ISA document: > "An 18-bit signed offset (the 16-bit offset field shifted left 2 > bits) is added > to the address of the instruction following the branch (not the > branch itself), > in the branch delay slot, to form a PC-relative effective target address." > > Currently, this syntax is not supported by ld even for static at all: > > xx: > b (8) > ssnop > ssnop > ssnop > ssnop > > mipsel-linux-gnu-gcc -mno-shared -mno-abicalls -static -nostdlib 1.s > warning: cannot find entry symbol __start; defaulting to 00400110 > /tmp/ccdHuq6V.o: in function `xx': > (.text+0x0): relocation truncated to fit: R_MIPS_PC16 against `*UND*' I gave valid use examples in PR31343 that link correctly. I can image a tiny bare-metal app using branches to hardcoded locations for one reason or another. Tools ought not to stand in the way (a general engineering principle and one of the GNU project in particular too). Maciej
diff --git a/gas/config/tc-mips.c b/gas/config/tc-mips.c index 43c12de2c8a..f3f5c1f469d 100644 --- a/gas/config/tc-mips.c +++ b/gas/config/tc-mips.c @@ -7560,6 +7560,9 @@ append_insn (struct mips_cl_insn *ip, expressionS *address_expr, ip->insn_opcode |= ((address_expr->X_add_number >> shift) & 0xffff); } + if (mips_pic != NO_PIC && address_expr->X_op == O_constant) + as_bad (_("PIC code branch to absolute address (0x%lx)"), + (unsigned long) address_expr->X_add_number); } break; @@ -7577,6 +7580,9 @@ append_insn (struct mips_cl_insn *ip, expressionS *address_expr, (unsigned long) address_expr->X_add_number); ip->insn_opcode |= ((address_expr->X_add_number >> shift) & 0x1fffff); + if (mips_pic != NO_PIC && address_expr->X_op == O_constant) + as_bad (_("PIC code branch to absolute address (0x%lx)"), + (unsigned long) address_expr->X_add_number); } break; @@ -7594,6 +7600,9 @@ append_insn (struct mips_cl_insn *ip, expressionS *address_expr, (unsigned long) address_expr->X_add_number); ip->insn_opcode |= ((address_expr->X_add_number >> shift) & 0x3ffffff); + if (mips_pic != NO_PIC && address_expr->X_op == O_constant) + as_bad (_("PIC code branch to absolute address (0x%lx)"), + (unsigned long) address_expr->X_add_number); } break;