From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7803) id 28ED03858298; Wed, 23 Nov 2022 02:46:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 28ED03858298 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Nelson Chu To: bfd-cvs@sourceware.org Subject: [binutils-gdb] RISC-V: Make R_RISCV_SUB6 conforms to riscv ABI standard X-Act-Checkin: binutils-gdb X-Git-Author: Xiao Zeng X-Git-Refname: refs/heads/master X-Git-Oldrev: ba64682044d3828909fd5356f0282abaaefa6425 X-Git-Newrev: 06f0a892a5260d8fe93550ed96364cc76fef971d Message-Id: <20221123024657.28ED03858298@sourceware.org> Date: Wed, 23 Nov 2022 02:46:57 +0000 (GMT) X-BeenThere: binutils-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 23 Nov 2022 02:46:57 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D06f0a892a526= 0d8fe93550ed96364cc76fef971d commit 06f0a892a5260d8fe93550ed96364cc76fef971d Author: Xiao Zeng Date: Mon Nov 21 20:00:37 2022 +0800 RISC-V: Make R_RISCV_SUB6 conforms to riscv ABI standard =20 According to the riscv psabi, R_RISCV_SUB6 only allows 6 least signific= ant bits are valid, but since binutils implementation, we usually get 8 bits field for it. That means, the high 2 bits could be other field and have different purpose. Therefore, we should filter the 8 bits to 6 bits be= fore calculate, and then only encode the valid 6 bits back. By the way, we = also need the out-of-range check for R_RISCV_SUB6, and the overflow checks f= or all R_RISCV_ADD/SUB/SET relocations, but we can add them in the future = patches. =20 Passing riscv-gnu-toolchain regressions. =20 bfd/ChangeLog: =20 * elfnn-riscv.c (riscv_elf_relocate_section): Take the R_RISCV_= SUB6 lower 6 bits as the significant bit. * elfxx-riscv.c (riscv_elf_add_sub_reloc): Likewise. Diff: --- bfd/elfnn-riscv.c | 9 +++++++++ bfd/elfxx-riscv.c | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c index 0570a971b5a..a2d85dbe939 100644 --- a/bfd/elfnn-riscv.c +++ b/bfd/elfnn-riscv.c @@ -2427,6 +2427,15 @@ riscv_elf_relocate_section (bfd *output_bfd, break; =20 case R_RISCV_SUB6: + { + bfd_vma old_value =3D bfd_get (howto->bitsize, input_bfd, + contents + rel->r_offset); + relocation =3D (old_value & ~howto->dst_mask) + | (((old_value & howto->dst_mask) - relocation) + & howto->dst_mask); + } + break; + case R_RISCV_SUB8: case R_RISCV_SUB16: case R_RISCV_SUB32: diff --git a/bfd/elfxx-riscv.c b/bfd/elfxx-riscv.c index afbde56b9e5..ff9607e7966 100644 --- a/bfd/elfxx-riscv.c +++ b/bfd/elfxx-riscv.c @@ -994,6 +994,10 @@ riscv_elf_add_sub_reloc (bfd *abfd, relocation =3D old_value + relocation; break; case R_RISCV_SUB6: + relocation =3D (old_value & ~howto->dst_mask) + | (((old_value & howto->dst_mask) - relocation) + & howto->dst_mask); + break; case R_RISCV_SUB8: case R_RISCV_SUB16: case R_RISCV_SUB32: